zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] new completions for head and tail commands
@ 2015-10-27 13:44 Jun T.
  2015-10-27 20:34 ` Daniel Shahaf
  0 siblings, 1 reply; 4+ messages in thread
From: Jun T. @ 2015-10-27 13:44 UTC (permalink / raw)
  To: zsh-workers


---
Here are new completion functions for 'head' and 'tail' commands.

 Completion/Unix/Command/_head | 45 ++++++++++++++++++++++++++++++
 Completion/Unix/Command/_tail | 65 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+)
 create mode 100644 Completion/Unix/Command/_head
 create mode 100644 Completion/Unix/Command/_tail

diff --git a/Completion/Unix/Command/_head b/Completion/Unix/Command/_head
new file mode 100644
index 0000000..1f0fab9
--- /dev/null
+++ b/Completion/Unix/Command/_head
@@ -0,0 +1,45 @@
+#compdef head
+
+local curcontext=$curcontext state state_descr line expl opts args ret=1
+typeset -A opt_args
+
+if _pick_variant gnu=GNU unix --version; then
+  args=(
+    '(-n --lines -c --bytes)'{-c+,--bytes=}'[print the first (or with -, all but the last) specified bytes]:number of bytes:->number'
+    '(-n --lines -c --bytes)'{-n+,--lines=}'[print the first (or with -, all but the last) specified lines]:number of lines:->number'
+    '(-q --quiet --silent -v --verbose)'{-q,--quiet,--silent}'[never print headers giving file names]'
+    '(-q --quiet --silent -v --verbose)'{-v,--verbose}'[always print headers giving file names]'
+    '(- *)--help[display help and exit]'
+    '(- *)--version[output version information and exit]'
+  )
+else
+  opts='-A "-*"'
+  args=( '(-c)-n+[display the first specified lines]:number of lines' )
+  case $OSTYPE in
+    (freebsd*|darwin*|dragonfly*|netbsd*)
+      args+=( '(-n)-c+[display the first specified bytes]:number of bytes' )
+      ;;
+  esac
+fi
+
+_arguments -C -s -S $opts : $args '*:file:_files' && return 0
+
+case $state in
+  (number)
+    local mlt sign digit
+    mlt='multiplier:multiplier:((b\:512 K\:1024 KB\:1000 M\:1024\^2'
+    mlt+=' MB\:1000\^2 G\:1024\^3 GB\:1000\^3 T\:1024\^4 TB\:1000\^4))'
+    sign='sign:sign:((-\:"print all but the last specified bytes/lines"'
+    sign+=' +\:"print the first specified bytes/lines (default)"))'
+    digit='digits:digit:(0 1 2 3 4 5 6 7 8 9)'
+    if compset -P '*[0-9]'; then
+      _alternative $mlt $digit && ret=0
+    elif [[ -z $PREFIX ]]; then
+      _alternative $sign $digit && ret=0
+    elif compset -P '(+|-)'; then
+      _alternative $digit && ret=0
+    fi
+    ;;
+esac
+
+return ret
diff --git a/Completion/Unix/Command/_tail b/Completion/Unix/Command/_tail
new file mode 100644
index 0000000..3cab26c
--- /dev/null
+++ b/Completion/Unix/Command/_tail
@@ -0,0 +1,65 @@
+#compdef tail
+
+local curcontext=$curcontext state state_descr line expl opts args ret=1
+typeset -A opt_args
+
+if _pick_variant gnu=GNU unix --version; then
+  args=(
+    '(-n --lines -c --bytes)'{-c+,--bytes=}'[print the last specified bytes; with +, start at the specified byte]:number of bytes:->number'
+    '(-n --lines -c --bytes)'{-n+,--lines=}'[print the last specified lines; with +, start at the specified line]:number of lines:->number'
+    '(-F -f)--follow=-[output appended data as the file grows]::how:(name descriptor)'
+    '(-F --follow)-f[same as --follow=descriptor]'
+    '(-f --follow --retry)-F[same as --follow=name --retry]'
+    '--max-unchanged-stats=[with --follow=name, check file rename after the specified number of iterations]:number of iterations'
+    '(-s --sleep-interval)'{-s+,--sleep-interval=}'[with -f, sleep the specfied seconds between iterations]:seconds'
+    '--pid=[with -f, terminate after the specified process dies]:pid:_pids'
+    '(-q --quiet --silent -v --verbose)'{-q,--quiet,--silent}'[never output headers giving file names]'
+    '(-q --quiet --silent -v --verbose)'{-v,--verbose}'[always output headers giving file names]'
+    '--retry[keep trying to open a file even when it becomes inaccessible]'
+    '(- *)--help[display help and exit]'
+    '(- *)--version[output version information and exit]'
+  )
+else
+  opts='-A "-*"'
+  args=(
+    '(-b -n)-c+[start at the specified byte]:bytes relative to the end (with +, beginning) of file'
+    '(-b -c)-n+[start at the specified line]:lines relative to the end (with +, beginning) of file'
+    '(-F -r)-f[wait for new data to be appended to the file]'
+  )
+  case $OSTYPE in
+    (freebsd*|darwin*|dragonfly*|netbsd*|openbsd*)
+      args+=(
+	'(-f -F)-r[display the file in reverse order]'
+	'(-c -n)-b+[start at the specified block (512-byte)]:blocks relative to the end (with +, beginning) of file'
+      )
+      ;|
+    (freebsd*|darwin*|dragonfly*|netbsd*)
+      args+=( '(-f -r)-F[implies -f, but also detect file rename]' )
+      ;|
+    (freebsd*|darwin*|dragonfly*)
+      args+=( '-q[suppress the headers when displaying multiple files]' )
+      ;;
+  esac
+fi
+
+_arguments -C -s -S $opts : $args '*:file:_files' && return 0
+
+case $state in
+  (number)
+    local mlt sign digit
+    mlt='multiplier:multiplier:((b\:512 K\:1024 KB\:1000 M\:1024\^2'
+    mlt+=' MB\:1000\^2 G\:1024\^3 GB\:1000\^3 T\:1024\^4 TB\:1000\^4))'
+    sign='sign:sign:((+\:"start at the specified byte/line"'
+    sign+=' -\:"output the last specified bytes/lines (default)"))'
+    digit='digits:digit:(0 1 2 3 4 5 6 7 8 9)'
+    if compset -P '*[0-9]'; then
+      _alternative $mlt $digit && ret=0
+    elif [[ -z $PREFIX ]]; then
+      _alternative $sign $digit && ret=0
+    elif compset -P '(+|-)'; then
+      _alternative $digit && ret=0
+    fi
+    ;;
+esac
+
+return ret
-- 
1.9.5 (Apple Git-50.3)



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] new completions for head and tail commands
  2015-10-27 13:44 [PATCH] new completions for head and tail commands Jun T.
@ 2015-10-27 20:34 ` Daniel Shahaf
  2015-10-27 20:54   ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Shahaf @ 2015-10-27 20:34 UTC (permalink / raw)
  To: Jun T.; +Cc: zsh-workers

Jun T. wrote on Tue, Oct 27, 2015 at 22:44:47 +0900:
> +  opts='-A "-*"'
> +  args=( '(-c)-n+[display the first specified lines]:number of lines' )
> +  case $OSTYPE in
> +    (freebsd*|darwin*|dragonfly*|netbsd*)
> +      args+=( '(-n)-c+[display the first specified bytes]:number of bytes' )
> +      ;;
> +  esac
> +fi
> +
> +_arguments -C -s -S $opts : $args '*:file:_files' && return 0

Should that be ${=opts}?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] new completions for head and tail commands
  2015-10-27 20:34 ` Daniel Shahaf
@ 2015-10-27 20:54   ` Bart Schaefer
  2015-10-28 12:56     ` Jun T.
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2015-10-27 20:54 UTC (permalink / raw)
  To: Zsh hackers list

On Tue, Oct 27, 2015 at 1:34 PM, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> Jun T. wrote on Tue, Oct 27, 2015 at 22:44:47 +0900:
>> +  opts='-A "-*"'
>> +  args=( '(-c)-n+[display the first specified lines]:number of lines' )
>> +  case $OSTYPE in
>> +    (freebsd*|darwin*|dragonfly*|netbsd*)
>> +      args+=( '(-n)-c+[display the first specified bytes]:number of bytes' )
>> +      ;;
>> +  esac
>> +fi
>> +
>> +_arguments -C -s -S $opts : $args '*:file:_files' && return 0
>
> Should that be ${=opts}?

Or opts=(-A "-*") more likely.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] new completions for head and tail commands
  2015-10-27 20:54   ` Bart Schaefer
@ 2015-10-28 12:56     ` Jun T.
  0 siblings, 0 replies; 4+ messages in thread
From: Jun T. @ 2015-10-28 12:56 UTC (permalink / raw)
  To: zsh-workers


On 2015/10/28, at 5:54, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Tue, Oct 27, 2015 at 1:34 PM, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
>> Jun T. wrote on Tue, Oct 27, 2015 at 22:44:47 +0900:
>>> +  opts='-A "-*"'
>>> (snip)
>>> +_arguments -C -s -S $opts : $args '*:file:_files' && return 0
>> 
>> Should that be ${=opts}?
> 
> Or opts=(-A "-*") more likely.

Thanks.
I will commit/push with the fix opts=(-A "-*") (which I believe the
correct one). But there remains a behavior which I still don't understand.

Using opts=(-A "-*") is equivalent to
[1] _arguments  -A '-*'
while ${=opts} may correspond to
[2] _arguments  -A '"-*"'
i.e., the pattern includes double quotes. My original is equivalent to
[3] _arguments  '-A "-*"'
which looks quite wrong but seems to (accidentally) work due to a strange
option parsing of _arguments (so I didn't notice the error while testing).

All of the above work in the sense that, on non-gnu systems,

% tail file1 -<TAB>

does not complete any options. But if there is a file whose name starts
with a '-' (say, '-abc'), then [2][3] behave differently from [1].
With [2] or [3], the <TAB> completes the file name '-abc', while with [1]
nothing is offered (I get a message 'no more arguments' due to a setting
in my .zshrc). The same applies to

% tail -- -<TAB>

I'm not sure whether the behavior of [1] (not offering '-abc') is the
expected one or not, and have no idea why '-abc' is offered by [2][3].


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-10-28 12:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-27 13:44 [PATCH] new completions for head and tail commands Jun T.
2015-10-27 20:34 ` Daniel Shahaf
2015-10-27 20:54   ` Bart Schaefer
2015-10-28 12:56     ` Jun T.

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).