mirrored from git://git.code.sf.net/p/zsh/code
-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy path_perforce
More file actions
3164 lines (2762 loc) · 101 KB
/
_perforce
File metadata and controls
3164 lines (2762 loc) · 101 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#compdef p4 p4d -value-,P4CLIENT,-default- -value-,P4PORT,-default- -value-,P4MERGE,-default- -value-,P4USER,-default-
# Maintainer: Peter Stephenson <pws@csr.com>.
# Increasingly loosely based on _cvs version 1.17.
# Completions currently based on Perforce release 2016.1.
# Styles, tags and contexts
# =========================
#
# If the `verbose' style is set (it is assumed by default), verbose
# descriptions are provided for many completed quantities derived
# dynamically such as subcommand names, labels, changes -- in fact,
# just about anything for which Perforce itself produces a verbose,
# one-line description. It may be turned off in the context of each
# subcommand e.g.
# zstyle ':completion:*:p4-labelsync:*' verbose false
# or for a particular tag, e.g. changes,
# zstyle ':completion:*:changes' verbose false
# or just for top-level completion (i.e. up to and including completion
# of the subcommand):
# zstyle ':completion:*:p4:*' verbose false
# or for p4 as a whole,
# zstyle ':completion:*:p4(-*|):*' verbose false
# This is actually handled by the `_describe' function underneath the
# Perforce completion system; it's mentioned here as verbosity adds
# significantly to a lot of the Perforce completions.
#
# Note that completing change numbers is not very useful if `verbose' is
# turned off. There is no speed advantage for turning it off, either.
# (Changes are also known as changelists or changesets. The functions
# and tags here all consistently use `changes'.)
#
# The style `max' can be set to a number which limits how many
# possibilities can be shown when selecting changes or jobs. This is
# handled within Perforce, so the completion code may limit the number even
# further. If not set explicitly, the value is taken to be 20 to avoid a
# huge database being output. Set it to a larger number if necessary.
# Setting it explicitly to zero removes the maximum. Because you see only
# the most recent, changes and jobs are shown in the order given by
# Perforce without further sorting.
#
# Completion of jobs can also be controlled by the `jobview' style.
# This uses the standard Perforce JobView syntax, and is applied
# in connection with the `max' style. In other words,
# if you set
# zstyle ':completion:*:p4-*:jobs' max 0
# zstyle ':completion:*:p4-*:jobs' jobview 'user=pws'
# then jobs to be completed will be those from the output of
# p4 jobs -e 'user=pws'
# i.e. those assigned to Perforce user `pws'.
#
# Completion of changes can be controlled by the `changes' style.
# This takes additional arguments to be passed to `p4 changes'.
# An obvious example is:
# zstyle ':completion:*:p4-*:changes' changes -u $USER
# to limit changes to the present user.
#
# The style `all-files' is used to tell the completion system to
# complete any file in a given context. This is for use in places
# where it would, for example, only complete files opened for editing.
# See the next section for more.
#
# The style `depot-files' tells the system to complete files by asking
# Perforce for a list where it would otherwise complete files locally by
# the standard mechanism --- basically any time you don't use // notation
# and there is no restriction e.g. to opened files only. There is likely
# to be a significant speed penalty for this; it is turned off by default
# in all contexts. The advantage is that it cuts out files not maintained
# by Perforce. (Again, note this is a style, not a tag.) Contexts
# where this might be particularly useful include p4-diff or p4-diff2.
#
# The tags depot-files and depot-dirs also exist; they are used whenever
# the system is completing files or directories by asking Perforce
# to list them, rather than by using normal file completion.
#
# The tag subdirs is used to complete the special `...' which tells
# Perforce to search all subdirectories. Hence you can turn this
# feature off by suitably manipulating your tags.
#
# The function will usually try to limit the files it lists by
# context; for example, to just opened files. By default it does
# this by retrieving the complete list from Perforce and then
# relying on the completion system to do the matching. If this is
# slow, it is possible to set the style "glob", in which case the
# matching is done within Perforce, potentially reducing the amount of
# searching of Perforce's internal database. The tag used for
# this is the same as the command used to retrieve the file name:
# integrated, opened, resolved, dirs, files. The disadvantage
# of doing the matching within Perforce is that no matcher specification
# is applied; for example, it's not possible to match a_u.c against
# admin_utils.c.
#
# Actually, a hybrid strategy is used when the glob style is not set: the
# directory is passed literally to Perforce, but the file or directory
# being matched is passed as "*", so that matching on the contents of the
# directory is performed by the completion system.
#
# Experiment suggests that the glob style isn't usually needed: only
# "p4 integrated" is likely to be significantly slowed if no limiting
# pattern is applied, and completing only integrated files is uncommon.
#
# Completion of files and their revisions
# =======================================
#
# File completion handles @ and # suffixes. If the filename is completed,
# typing @ or # removes the space which was automatically added.
# The context used has `at-suffix' or `hash-suffix' in the position
# before the tag to indicate suffix completion (as always, ^Xh will
# show you all possible contexts). This makes it possible
# to select changes, dates, labels and clients using the tag-order
# style. For example,
# zstyle ':completion:*:p4-*:at-suffix:*' tag-order changes '*'
# will force all completion after `@' to show changes first. Executing
# _next_tags (usually ^x^n) will cycle between that and the remaining
# tags (dates, labels, clients). I recommend, at least, keeping labels
# later than changes since the former are less useful and can take a long
# time to complete.
#
# A # is automatically quoted when handled in this way; if the file is
# typed by hand or the completion didn't finish (e.g. you typed a character
# in the middle of menu completion), you probably need to type `\#' by
# hand. The problem is that the completion system uses extended globbing
# and hence a pattern of the form `filename#' always matches `filename'
# (since e# matches any number of e's including one). Hence this can look
# like an expansion which expands to `filename'.
#
# After @, you can complete changes (note the use of the style `max'
# above), labels, clients or even dates, while after `#' you can
# complete numeric revisions or the special revision names head, none,
# have. These are available whether or not you completed the filename; if
# the file doesn't exist, numeric revisions won't work, but the rest will
# (though what Perforce will do with the resulting command is another matter).
#
# In addition, when completing after `file@', only changes specific to `file'
# will be shown (exactly the list of changes Perforce shows from the
# command `p4 changes file'). If this doesn't work, chances are that
# `file' does not exist. Having a multi-directory match (literal `...')
# in `file' should work fine, since `p4 changes' recognises all normal
# Perforce file syntax.
#
# Some perforce commands allow you to specify a range of revisions or
# changes as `file@1,@2' or `file#1,#2'. Currently, the second part of the
# revision range can always be completed (whether the command accepts them
# or not), but the comma after the first part of the range is only added
# automatically if the documentation suggests the command accepts ranges at
# that point. This is an auto-removable suffix, so it will disappear if
# you hit space or return. Typing a `#' at this point will insert a
# backslash, as before. The # and @ are never added automatically; you
# have to select one by hand.
#
# Perforce allows change and revision numbers to be preceded by =, <, <=, >
# or >=. See `p4 help undoc' for details. (In particular, `=' is
# an extremely useful shortcut when integrating single changes.)
# This syntax is handled, but currently the < and > must be quoted
# with a backslash, not by any other mechanism. For example,
# p4 files myfile@\>=3<TAB>
# will complete a change number. The valid syntax where the second
# change or revision in a range does not have the @ or # in front
# (for example `file@32183,32185') is not currently handled; the @
# must be repeated.
#
# File completion for some functions is restricted by the Perforce
# status of the file; for example, `p4 opened' only completes opened
# files (surprised?) However, you can set the style (N.B. not tag)
# `all-files'; so, for example, you can turn off the limit in this case by
# zstyle ':completion:*:p4-opened:*' all-files true
# Normally the `file-patterns' style would be used to control matching,
# but as the file types are not selected by globbing it doesn't work here
# However, if you set the all-files style, all filename completion is done
# by the standard mechanism; in this case, the `file-patterns' style works
# as usual. The style `ignored-patterns' is available in any case, even
# without `all-files'; this is therefore generally the one to use.
#
# The style `whole-path' allows you complete the entire path to a file
# at once. This is useful in cases such as opened files where the
# list of files is likely to be short but may include files with
# widely different paths. As with the `glob' style, the tag is the
# Perforce disposition of the file: integrated, opened, resolved, dirs,
# files. For example, with
# zstyle ':completion:*:p4-revert:*:opened' whole-path true
# completion after `p4 revert' will offer you the entire depot path
# to a file rather than just part of the path at once (with the
# usual methods of disambiguation). Directory completion is turned
# off during a `whole-path' completion. The `whole-path' style can
# also take the value `absolute'; this means that an initial `/'
# activates `whole-path' completion, otherwise a relative file path
# will be completed in the normal way. For example, with
# zstyle ':completion:*:p4-revert:*:opened' whole-path absolute
# then after `p4 revert <TAB>' you are offered open files in the
# current directory plus directories; after `p4 revert /<TAB>' you
# are offered all open files in depot syntax.
#
# With `p4 diff', the shell will spot if you have used an option that
# allows you to diff unopened files (such as -f) and in that case offer
# all files; otherwise, it just offers opened files.
#
# Completion of changes
# =====================
#
# There is various extra magic available any time change numbers
# are completed, regardless of how this was reached, i.e.
# `p4 fixes -c ...' and `p4 diff filename.c@...' are treated the same way.
# Note, however, these only work if you are at the point where a change
# number would be completed.
#
# Firstly, as mentioned above there is a maximum for the number of
# changes which will be shown, given by the style max, or defaulting to 20.
# Only the most recent changes will be shown. This is to avoid a speed
# penalty or clumsy output. If a positive numeric argument is given
# when changes are being completed, the maximum is set (unconditionally)
# to that number instead.
#
# It is also possible to give a negative numeric prefix to a listing widget
# (i.e. typically whatever is bound to ^D). If there is already a change
# number on the line, e.g. from cycling through a menu of choices, the full
# description for that change is shown in the format of a completion
# listing. [TODO: this could be made configurable with a style.]
#
# It may be necessary to abandon the current completion attempt before
# typing this to force the completion system to display the new text.
# Replacing delete-char-or-list with the following user defined widget
# (create with `zle -N ...') will force this for any negative prefix argument.
# (( ${NUMERIC:-0} < 0 )) && (( CURSOR = CURSOR ))
# zle delete-char-or-list
#
# Completion of jobs
# =================
#
# Completing jobs uses the same logic for the numeric prefix as completing
# changes: a positive prefix changes the maximum number of jobs which
# will be shown, and a negative prefix when listing shows the full
# text for the job whose name is currently inserted on the command line.
# In this case, the entire text of the word being completed is assumed
# to constitute the job name (which is almost certainly correct).
#
# Completion of dates
# ===================
#
# In a file revision specification it is possible to give a date
# in the form file@YYYY/MM/DD:hh:mm:ss, which may be completed. This
# is ever so slightly less silly than it sounds. Any component entered
# by hand with the appropriate suffix will be ignored; any component
# completed will be set to the current value. Hence you can easily
# specify, say, one month ago by using the completed value for all
# components except the month and setting that to one less. The shell
# will also happily append the appropriate suffix if you try to complete
# after anything which is already the appropriate width. (Perforce
# supports two-digit years, but these are confusing and no longer
# particularly useful as they refer to the twentieth century, so
# the shell does not.)
#
# Calls to p4
# ===========
#
# Much of the information from Perforce is provided by calls to p4
# commands. This is done via the _call_program interface, as described
# in the zshcompsys manual page. Hence a suitable context with the
# `command' style allows the user to take control of this call.
# The tags used are the name of the p4 command, or in the case of
# calls to help subcommands, `help-<subcommand>'. Note that if the
# value of the style begins with `-', the arguments to the perforce
# command are appended to the remaining words of the style before calling
# the command.
#
# Programmes taking p4-style arguments
# ====================================
#
# It is possible to use the _perforce completion with other commands
# which behave like a subcommand of p4 by setting the service type
# to p4-<subcommand>. For example,
# compdef _perforce p4cvsmap=p4-files
# says that the command `p4cvsmap' takes arguments like `p4 files'.
# Often the options will be different; if this is a problem, you
# will need to write your own completer which loads _perforce and
# calls its functions directly. You can add -global to the end
# of the service to say that the command also handles global
# Perforce options, comme ca:
# compdef _perforce p4reopen=p4-job-global
#
# Anything more complicated should be modelled on one of the
# _perforce_cmd_* handlers below. To get this to work, the full
# set of _perforce functions must be loaded; because of the way
# autoloading works, a trick is required: call "_perforce -l" which
# causes the function to be executed, loading all the associated
# functions as a side effect, but tells _perforce to return without
# generating any completions. For example, here is the completion
# for my `p4desc' function which is an enhanced version of `p4 describe'
# (without any handling for global Perforce arguments):
#
# #compdef p4desc
#
# _perforce -l
#
# _arguments -s : \
# '-d-[select diff option]:diff option:((b\:ignore\ blanks c\:context d\:basic\ diff n\:RCS s\:summary u\:unified w\:ignore\ all\ whitespace))' \
# '-s[short form]' \
# '-j[select by job]:job:_perforce_jobs' \
# '*::change:_perforce_changes'
#
# To add handling of global options to this, see the end of the _perforce
# function below. Something like:
#
# local -a _perforce_global_options _perforce_option_dispatch
# if _perforce_global_options; then
# _arguments -s : $_perforce_option_dispatch \
# '<other stuff>'
# fi
#
# TODO
# ====
#
# No mechanism is provided for completely ignoring certain files not
# handled by Perforce as with .cvsignore. This could be done ad hoc.
# However, the ignored-patterns style and the parameter $fignore are
# of course applied as usual, so setting ignored-patterns for the
# context `:completion:*:p4[-:]*' should work.
_perforce() {
# rely on localoptions
setopt nonomatch
local p4cmd==p4 match mbegin mend
integer _perforce_cmd_ind
# Localise variables used at different levels of the hierarchy.
local _perforce_exclude_change
if [[ $1 = -l ]]; then
# Run to load _perforce and associated functions but do
# nothing else.
return
fi
if [[ $service = -value-* ]]; then
# Completing parameter value.
# Some of these --- in particular P4PORT --- don't need
# the perforce server.
case $compstate[parameter] in
(P4PORT)
_perforce_hosts_ports
;;
(P4CLIENT)
_perforce_clients
;;
(P4MERGE)
_command_names -e
;;
(P4USER)
_perforce_users
;;
esac
# We do not handle values anywhere else.
return
fi
if [[ $p4cmd = '=p4' ]]; then
_message "p4 executable not found: completion not available"
return
fi
# If we are at or after the command word, remember the
# global arguments to p4 as we will need to pass these down
# when generating completion lists.
# This is both an array and a function, but luckily I never
# get confused...
local -a _perforce_global_options
local -a _perforce_option_dispatch
# If we are given a service of the form p4-cmd, treat this
# as if it was after `p4 cmd'. This provides an easy way in
# for scripts and functions that emulate the behaviour of
# p4 subcommands. Note we don't shorten the command line arguments.
if [[ $service = p4-(#b)(*) ]]; then
local curcontext="$curcontext"
local p4cmd=$words[1] cmd=$match[1] gbl
if [[ $cmd = (#b)(*)-global ]]; then
# Handles global options.
cmd=$match[1]
_perforce_global_options && gbl=1
fi
if (( $+functions[_perforce_cmd_$cmd] )); then
curcontext="${curcontext%:*:*}:p4-${cmd}:"
if [[ -n $gbl ]]; then
# We are handling global Perforce options as well as the
# arguments to the specific command.
# To handle the latter, we need the command name, plus
# all the arguments for the command with the global options
# removed. The function _perforce_service_dispatch handles
# this by unshifting the command ($p4cmd) into words,
# then dispatching for the Perforce subcommand $cmd.
#
# Has anyone noticed this is getting rather complicated?
_arguments -s : $_perforce_option_dispatch \
"*::p4-$cmd arguments: _perforce_service_dispatch $p4cmd $cmd"
else
_perforce_cmd_$cmd
fi
# Don't try to do full command handling.
return
else
_message "unhandled _perforce service: $service"
return 1
fi
fi
if [[ $service = p4d ]]; then
_arguments -s : \
'-d[run as daemon]' \
'-f[run as single threaded server]' \
'-i[run for inetd using sockets]' \
'-q[suppress startup message]' \
'-s[run as NT service]' \
'-xi[switch server database to unicode mode and quit]' \
'-xu[run database upgrade and quit]' \
'-c[run command and exit]:command of some sort: ' \
'-Id[specify description]:description: ' \
'-In[specify unique name]:name: ' \
'-jc[checkpoint, save and truncate journal]::optional prefix: ' \
'-jd[checkpoint, not saving journal]::optional file:_files' \
'-jj[save and truncate journal]::optional prefix: ' \
'-jr[incremental restore from checkpoint/journal]:'\
'file:_files:file:_files' \
'-z[gzip checkpoint and journal files]' \
'-h[show help]' \
'-V[print server version]' \
'-A[set audit log ($P4AUDIT)]:audit file:_files' \
'-J[set journal file ($P4JOURNAL) or "off"]:journal file:_files' \
'-L[set error log ($P4LOG or stderr)]:error log:_files' \
'-p[set port ($P4PORT o perforce:1666)]:port:_perforce_hosts_ports' \
'-r[set root directory ($P4ROOT)]:root directory:_path_files -g "*(/)"' \
'-v[debug level]:level: '
elif _perforce_global_options; then
_arguments -s : $_perforce_option_dispatch \
'1:perforce command:_perforce_commands'
else
(( _perforce_cmd_ind-- ))
(( CURRENT -= _perforce_cmd_ind ))
shift $_perforce_cmd_ind words
_perforce_command_args
fi
}
#
# Command and argument dispatchers
#
# Front end to _call_program to add in the global arguments
# passed to p4. The first argument is the tag, the remaining
# arguments are passed to p4. Typically the tag is the same
# as the first p4 argument.
(( $+functions[_perforce_call_p4] )) ||
_perforce_call_p4() {
local cp_tag=$1
shift
# This is for our own use for parsing, and we need English output,
# so...
local +x P4LANGUAGE
_call_program $cp_tag command p4 "${_perforce_global_options[@]}" "$@"
}
# The list of commands is cached in _perforce_cmd_list, but we
# only generate it via this function when we need it.
(( $+functions[_perforce_gen_cmd_list] )) ||
_perforce_gen_cmd_list() {
(( ${+_perforce_cmd_list} )) || typeset -ga _perforce_cmd_list
local hline line match mbegin mend
# Output looks like <tab>command-name<space>description in words...
# Ignore blank lines and the heading line beginning `Perforce...'
# Just gets run once, then cached, so don't bother optimising
# this to a grossly unreadable parameter substitution.
_perforce_cmd_list=()
_perforce_call_p4 help-commands help commands | while read -A hline; do
if (( ${#hline} < 2 )); then
if (( ${#_perforce_cmd_list} )); then
# Ignore comments after the main list of commands, separate by blank
# line.
break
else
continue
fi
fi
[[ $hline[1] = (#i)perforce ]] && continue
_perforce_cmd_list+=("${hline[1]}:${hline[2,-1]}")
done
# Also cache the server version for nefarious purposes.
_perforce_call_p4 info info | while read line; do
if [[ $line = (#b)"Server version: "*/*/(<->.<->)(.[^/]|)/*" "* ]]; then
_perforce_server_version=$match[1]
fi
done
# Unsupported commands: we could look through p4 help undoc, I suppose.
# I can't be bothered to check the date when they appeared any more,
# but let's at least check they're not already there.
local -a unsup
unsup=(
"attribute:set attributes for open file (EXPERIMENTAL)"
"dbschema:report meta database information"
"export:extract journal or checkpoint records"
"interchanges:report changes not yet integrated between branches"
"replicate:poll for journal changes and apply to another server"
"spec:allows limited changes to form specifications (admin)"
)
for line in $unsup; do
if [[ ${_perforce_cmd_list[(r)${line%%:*}:*]} = '' ]]; then
_perforce_cmd_list+=($line)
fi
done
}
(( $+functions[_perforce_commands] )) ||
_perforce_commands() {
(( ${#_perforce_cmd_list} )) || _perforce_gen_cmd_list
_describe -t p4-commands 'Perforce command' _perforce_cmd_list
}
(( $+functions[_perforce_command_args] )) ||
_perforce_command_args() {
local curcontext="$curcontext" cmd=${words[1]}
if (( $+functions[_perforce_cmd_$cmd] )); then
curcontext="${curcontext%:*:*}:p4-${cmd}:"
_perforce_cmd_$cmd
else
_message "unhandled perforce command: $cmd"
fi
}
(( $+functions[_perforce_service_dispatch] )) ||
_perforce_service_dispatch() {
# Put the original command name back, then dispatch for
# our Perforce handler.
words=($1 "$words[@]")
(( CURRENT++ ))
_perforce_cmd_$2
}
#
# Helper functions
#
(( $+functions[_perforce_global_options] )) ||
_perforce_global_options() {
# Options with arguments we need to pass down when calling
# p4 from completers. There are no options without arguments
# we need to pass. (Don't pass down -L language since we
# parse based on English output.)
local argopts_pass="cCdHpPu"
# Other options which have arguments but we shouldn't pass down.
# There are some debugging options, but they tend to get used
# with the argument in the same word as the option, in which
# case they will be handled OK anyway.
local argopts_ignore="Lx"
# The options we support in the form for _arguments.
# This is here for modularity and convenience, but note that since the
# actual dispatch takes place later, this is not local to this
# function and so must be made local in the caller.
_perforce_option_dispatch=(
'-c+[client]:client:_perforce_clients' \
'-C+[charset]:charset:_perforce_charsets' \
'-d+[current directory]:directory:_path_files -g "*(/)"' \
'-H+[hostname]:host:_perforce_hosts' \
'-G[python output]' \
'-L+[message language]:language: ' \
'-p+[server port]:port:_perforce_hosts_ports' \
'-P+[password on server]:password: ' \
'-s[output script tags]' \
'-u+[user]:user name:_perforce_users' \
'-x+[filename or -]:file:_perforce_files_or_minus' \
'-z+[select output format]:output format:(tag)'
)
integer i
# We need to try and check if we are before or after the
# subcommand, since some of the options with arguments, in particular -c,
# work differently. It didn't work if I just added '*::...' to the
# end of the arguments list, anyway.
for (( i = 2; i < CURRENT; i++ )); do
if [[ $words[i] = -[$argopts_pass$argopts_ignore] ]]; then
# word with following argument --- check this
# is less than the current word, else we are completing
# this and shouldn't pass it down
if [[ $(( i + 1 )) -lt $CURRENT && \
$words[i] = -[$argopts_pass] ]]; then
_perforce_global_options+=(${words[i,i+1]})
fi
(( i++ ))
elif [[ $words[i] = -[$argopts_pass]* ]]; then
# word including argument which we want to keep
_perforce_global_options+=(${words[i]})
elif [[ $words[i] != -* ]]; then
break
fi
done
(( _perforce_cmd_ind = i ))
(( _perforce_cmd_ind >= CURRENT ))
}
(( $+functions[_perforce_branches] )) ||
_perforce_branches() {
local bline match mbegin mend
local -a bl
bl=(${${${${(f)"$(_perforce_call_p4 branches branches 2>/dev/null)"}##Branch }//:/\\:}/ /:})
[[ $#bl -eq 1 && $bl[1] = '' ]] && bl=()
(( $#bl )) && _describe -t branches 'Perforce branch' bl
}
(( $+functions[_perforce_changes] )) ||
_perforce_changes() {
local cline match mbegin mend max ctype num comma file
local -a cl cstatus amax xargs
zstyle -s ":completion:${curcontext}:changes" max max || max=20
zstyle -a ":completion:${curcontext}:changes" changes xargs
if [[ ${NUMERIC:-0} -lt 0 && -z $compstate[insert] ]]; then
# Not inserting (i.e. just listing) and given a negative
# prefix argument. Instead of listing possible completions,
# show the full description for the change number on the line at
# the moment.
[[ $PREFIX = (|*[^[:digit:]])(#b)(<->) ]] && num+=$match[1]
[[ $SUFFIX = (#b)(<->)* ]] && num+=$match[1]
if [[ -n $num ]]; then
_message -r "$(_perforce_call_p4 describe describe $num)"
return 0
fi
elif [[ ${NUMERIC:-0} -gt 0 ]]; then
max=$NUMERIC
fi
(( max )) && amax=(-m $max)
# Hack: assume the arguments we want are at the end.
while [[ $argv[-1] = -t? ]]; do
case $argv[-1] in
# Change embedded in filename; extract that and remove
# the corresponding prefix. Remove possible `#'s, too,
# in case we are looking at a range.
(-tf)
file=${${(Q)PREFIX}%%[\#@]*}
compset -P '*@(|\\\<|\\\>)(|=)'
;;
# Changes already submitted
(-ts)
cstatus=(-s submitted)
ctype="submitted "
;;
# Changes still pending
(-tp)
cstatus=(-s pending)
ctype="pending "
;;
# Changes still pending and on the current client.
# Many uses of pending changes must be on the current client
# to be meaningful, in particular submitting a change or
# associating a file or operation with a particular change.
(-tc)
# Don't like this way of extracting the client to use,
# but I don't see a simpler one. We do at least make sure
# we call p4 with the same arguments as we will use with p4 changes.
cstatus=(-s pending -c "$(_perforce_call_p4 client client -o |
awk '/^Client:/ { print $2 }')")
ctype="local pending "
;;
# Changes that were shelved
(-tS)
cstatus=(-s shelved)
ctype="shelved "
;;
# Range allowed: append comma and supply rules for
# removing and handling subsequent `#'.
(-tR)
comma=(-S, -R _perforce_file_suffix)
esac
argv=($argv[1,-2])
done
# Limit to the 20 most recent changes by default to avoid huge
# output.
cl=(
${${${${${(f)"$(_perforce_call_p4 changes changes $amax $xargs $cstatus \$file)"}##Change\ }//:/\\:}//\ on\ /:}/\ by\ /\ }
)
# "default" can't have shelved files in it...
[[ $ctype = shelved* ]] || cl+=("default:change not yet numbered")
[[ $#cl -eq 1 && $cl[1] = '' ]] && cl=()
_describe -t changes "${ctype}change" cl -V changes-unsorted $comma
}
(( $+functions[_perforce_charsets] )) ||
_perforce_charsets() {
local expl
_wanted charset expl 'character set' \
compadd eucjp iso8859-1 shiftjis utf8 winansi
}
(( $+functions[_perforce_clients] )) ||
_perforce_clients() {
local -a slash cl xargs
zstyle -a ":completion:${curcontext}:clients" clients xargs
# Are we completing after an @, or a client view in a filespec?
if ! compset -P '*@'; then
compset -P '//' && slash=(-S/ -q)
fi
cl=(${${${${(f)"$(_perforce_call_p4 clients clients $xargs)"}##Client\ }//:/\\:}/\ /:})
[[ $#cl -eq 1 && $cl[1] = '' ]] && cl=()
_describe -t clients 'Perforce client' cl $slash
}
(( $+functions[_perforce_counters] )) ||
_perforce_counters() {
local cline match mbegin mend
local -a cl
cl=(${${${${(f)"$(_perforce_call_p4 counters counters)"}//:/\\:}/\ /:}/\=/current value})
[[ $#cl -eq 1 && $cl[1] = '' ]] && cl=()
_describe -t counters 'Perforce counter' cl
}
(( $+functions[_perforce_counter_values] )) ||
_perforce_counter_values() {
if [[ -n $words[CURRENT-1] ]]; then
local value="$(_perforce_call_p4 counter counter $words[CURRENT-1] 2>/dev/null)"
if [[ -n $value ]]; then
# No space. This allows stuff like incarg and decarg.
compstate[insert]=1
_wanted value expl 'counter value' compadd $value
fi
fi
}
(( $+functions[_perforce_dates] )) ||
_perforce_dates() {
# Only useful in a file spec after `@'.
compset -P '*@(|\\\<|\\\>)(|=)'
# Date/time now in format required by Perforce.
local now="$(date +%Y:%m:%d:%T)" name prefix
local -a nowarray offer opts matchpats suffixes names
nowarray=(${(s.:.)now})
names=( year month day\ of\ month hour minute second)
suffixes=( / / : : : '' )
integer i
prefix=${(Q)PREFIX}
for (( i = 6; i >= 1; i-- )); do
# Match from the most specific back.
# The following is one of those occasions where zsh
# substitution skips to the right answer without ever
# passing through the real world on the way.
if [[ $prefix = *${(j.*.)~suffixes[1,i-1]}* ]]; then
(( i > 1 )) && compset -P "*$suffixes[i-1]"
# If what's there already is the right length,
# just accept it and add the suffix.
prefix=${(Q)PREFIX}
if [[ ${#prefix} = ${#nowarray[i]} ]]; then
offer=($prefix)
else
offer=($nowarray[i])
fi
[[ -n $suffixes[i] ]] && opts=(-S $suffixes[i] -q)
name=$names[i]
break
fi
done
_describe -t dates $name offer $opts
}
(( $+functions[_perforce_dbtables] )) ||
_perforce_dbtables() {
local -a tables
tables=(archmap bodtext change changex counters depot domain have integ
integed ixtext label locks resolve rev revcx revdx revhx revsx trigger
user view working)
_describe -t db-table "DB table" tables
}
(( $+functions[_perforce_depots] )) ||
_perforce_depots() {
local dline match mbegin mend
local -a dl
dl=(${${${${(f)"$(_perforce_call_p4 depots depots)"}##Depot\ }//:/\\:}/\ /:})
[[ $#dl -eq 1 && $dl[1] = '' ]] && dl=()
_describe -t depots 'depot name' dl
}
(( $+functions[_perforce_files_or_minus] )) ||
_perforce_files_or_minus() {
_alternative 'minus:minus sign:(-)' 'files:file name:_files'
}
(( $+functions[_perforce_file_suffix] )) ||
_perforce_file_suffix() {
# Used with compadd -R to handle @ or # after a file name.
# Differs from compadd -r '...' in that it quotes `#' if typed.
[[ $1 = 1 ]] || return
if [[ $LBUFFER[-1] = [\ ,] ]]; then
if [[ $KEYS = '#' ]]; then
if [[ $LBUFFER[-1] = , ]]; then
# Range: no suffix removal but add a backslash
LBUFFER+=\\
else
# Suffix removal with an added backslash
LBUFFER="$LBUFFER[1,-2]\\"
fi
elif [[ $KEYS = (*[^[:print:]]*|[[:blank:]\;\&\|]) || \
( $KEYS = @ && $LBUFFER[-1] = ' ' ) ]] ; then
# Normal suffix removal
LBUFFER="$LBUFFER[1,-2]"
fi
elif [[ $LBUFFER[-1] = / ]]; then
# Normal suffix removal for directories.
if [[ $KEYS = (*[^[:print:]]*|[[:blank:]\;\&\|/]) ]]; then
LBUFFER="$LBUFFER[1,-2]"
fi
fi
}
# Helper function for the helper function for the helper functions
# for the helper function _perforce_files.
#
# Check if we should do whole-path completion.
# The argument is the Perforce disposition of files are looking at.
_perforce_whole_path() {
local wp
zstyle -s ":completion:${curcontext}:$1" whole-path wp
case $wp in
(true|yes|on|1)
return 0
;;
(absolute)
[[ ${(Q)PREFIX} = /* ]] && return 0
;;
esac
return 1
}
#
# Helper function for the helper function _perforce_retreive_files
# for the helper functions for the helper function _perforce files.
#
# This code retrieves the list of files with a glob filter and
# possibly a line filter specified by the caller. The line filter
# must match the entire line.
#
# Result returned in $files.
#
(( $+functions[_perforce_filter_files] )) ||
_perforce_filter_files() {
local call="$1"
local globfilter="$2"
local linefilter="$3"
local line
if [[ -n $linefilter ]]; then
files=(
${${${(f)"$(_perforce_call_p4 $call $call $globfilter 2>/dev/null)"}:#${~linefilter}}%%\#*}
)
else
files=(
${${(f)"$(_perforce_call_p4 $call $call $globfilter 2>/dev/null)"}%%\#*}
)
fi
}
#
# Helper function for the helper functions for the helper function
# _perforce_files. This is common code to retrieve a list of files
# from Perforce.
#
# First argument is the p4 subcommand used to list the files.
# This is also used as a tag for the style to decide whether
# to limit the list within Perforce. It may have a colon
# followed by a shell pattern to match lines to be excluded
# from the match.
#
# Remaining arguments are additional arguments to compadd.
#
(( $+functions[_perforce_retrieve_files] )) ||
_perforce_retrieve_files() {
local pfx exclude
local -a files match mbegin mend
if [[ $1 = (#b)([^:]##):(*) ]]; then
1=$match[1]
exclude=$match[2]
fi
if _perforce_whole_path $1; then
_perforce_filter_files $1 '' $exclude
elif zstyle -t ":completion:${curcontext}:$1" glob; then
# Limit the list by using Perforce to glob the pattern.
# This may be faster, but won't use matcher specs etc.
pfx=${(Q)PREFIX}
compset -P '*/'
_perforce_filter_files $1 '"$pfx*${(Q)SUFFIX}"' $exclude
files=(${files##*/})
else
# We need to limit the list to a directory.
if [[ $PREFIX = */* ]]; then
pfx="${(Q)${PREFIX%/*}}/*"
else
pfx="*"
fi
compset -P '*/'
_perforce_filter_files $1 '$pfx' $exclude
files=(${files##*/})
fi
[[ $#files -eq 1 && $files[1] = '' ]] && files=()
shift
compadd "$@" -a files
}
#
# Helper functions for the helper function _perforce_files. These files
# are low-level enough that they don't handle tags; this is done
# by the _alternative handler in _perforce_files.
#
(( $+functions[_perforce_integrated_files] )) ||
_perforce_integrated_files() {
local pfx=${(Q)PREFIX} type
local -a files
_perforce_retrieve_files integrated "$@"
}
(( $+functions[_perforce_opened_files] )) ||
_perforce_opened_files() {
local csuf
if [[ -n $_perforce_exclude_change ]]; then
if [[ $_perforce_exclude_change = default ]]; then
csuf=":* default change [^#]#"
else
csuf=":* change $_perforce_exclude_change [^#]#"
fi
fi
_perforce_retrieve_files opened$csuf "$@"
}
(( $+functions[_perforce_resolved_files] )) ||
_perforce_resolved_files() {
_perforce_retrieve_files resolved "$@"
}
# This has no other function than to offer to add the `...' used
# by Perforce to indicate a recursive search of directories.
# Bit pathetic, really.
#
# This has been causing odd effects with prefixes so is turned off.
#(( $+functions[_perforce_subdirs] )) ||
#_perforce_subdirs() {
# if [[ $PREFIX = */* ]]; then
# local dir=${PREFIX%%[^/]#}
# compadd "$@" -J subdirs -p $dir ...
# else
# compadd "$@" -J subdirs ...
# fi
#}