From a6df12242456734d736730960dc310905af779ab Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Thu, 7 Oct 2010 16:31:42 -0400 Subject: [PATCH] [menu] Update manual. Add file completion example. --- Makefile | 1 + cmd/wmii.sh.sh | 17 ++++-- examples/Makefile | 7 +++ examples/wimenu-file-completion.sh | 98 ++++++++++++++++++++++++++++++ man/wimenu.1 | 17 ++++-- man/wimenu.man1 | 16 ++--- rc/wmiirc.sh | 11 ++-- 7 files changed, 142 insertions(+), 25 deletions(-) create mode 100644 examples/Makefile create mode 100644 examples/wimenu-file-completion.sh diff --git a/Makefile b/Makefile index d6cf80bb..9962f06d 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ include $(ROOT)/mk/wmii.mk DIRS = \ doc \ + examples \ man \ lib \ cmd \ diff --git a/cmd/wmii.sh.sh b/cmd/wmii.sh.sh index 5ade5466..4eae7420 100755 --- a/cmd/wmii.sh.sh +++ b/cmd/wmii.sh.sh @@ -85,21 +85,26 @@ _wi_script() { } _wi_text() { - cat <<'!' -Event Key - Key "$@" -! eval "cat <&2 + [ -n "$eval" ] && eval "$(echo "$_wi_events" | awk "$(_wi_script)")" } +wi_events <<'!' +Event Key + Key "$@" +! + wi_fatal() { echo $scriptname: Fatal: $* exit 1 diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 00000000..8922c847 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,7 @@ +ROOT=.. +include $(ROOT)/mk/hdr.mk +include $(ROOT)/mk/wmii.mk + +DOCS = wimenu-file-completion.sh +DOCDIR = $(DOC)/examples + diff --git a/examples/wimenu-file-completion.sh b/examples/wimenu-file-completion.sh new file mode 100644 index 00000000..18f2efb8 --- /dev/null +++ b/examples/wimenu-file-completion.sh @@ -0,0 +1,98 @@ +#!/bin/sh +# This script will launch wimenu and provide command +# completion for the first argument and filename completion +# for each following argument, and execute the result. +# Program name completion requires that a program list already +# exist in $(wmiir namespace)/.proglist + +fifo="$HOME/.wmii/menu_fifo" +mkfifo $fifo 2>/dev/null + +script=$(cat <<'!' + BEGIN { + progs = "cat $(wmiir namespace)/.proglist" + + # Print the first set of completions to wimenu’s fifo + print read(progs) >fifo + fflush(fifo) + } + + # Process the input and provide the completions + { + # Skip the trailing part of the command. + # If there is none, this is the result. + if (!getline rest) { + print + exit + } + + if (!match($0, /.*[ \t]/)) + # First argument, provide the program list + update(0, progs) + else { + # Set the offset to the location of the last + # space, and save that part of the completion + offset = RLENGTH + str = substr($0, offset + 1) + + # If we're completing a sub-directory, adjust + # the offset to the position of the last / + if (match(str, ".*/")) + offset += RLENGTH + + # If the last component of the path begins with + # a ., include hidden files + arg = "" + if(match(str, "(^|/)\\.[^/]*$")) + arg = "-A" + + # Substitute ~/ for $HOME/ + sub("^~/", ENVIRON["HOME"] "/", str) + + # Strip the trailing filename + sub("[^/]+$", "", str) + + update(offset, "ls " arg quote(str)) + } + } + + # Push out a new set of completions + function update(offset, cmd) { + # Only push out the completion if the offset or the + # option of ls has changed. The behavior will be the + # same regardless, but this is a minor optimization + if (offset != loffset || cmd != lcmd) { + loffset = offset + lcmd = cmd + + cmpl = read(cmd) + print offset >fifo + print cmpl >fifo + fflush(fifo) + } + } + + # Quote a string. This should work in any Bourne + # or POSIX compatible shell. + function quote(str) { + if (!match(str, /[\[\](){}$'"^#~!&;*?|<>]/)) + return str + gsub(/\\/, "'\\\\'", str) + gsub(/'/, "'\\''", str) + return "'" str "'" + } + + # Read the output of a command and return it + function read(cmd) { + if (cmd in cache) + return cache[cmd] + res = "" + while (cmd | getline) + res = res quote($0) "\n" + close(cmd) + return cache[cmd] = res + } +! +) +res="$(wimenu -c "$@" <$fifo | awk -v "fifo=$fifo" "$script")" +exec ${SHELL:-sh} -c "exec $res" diff --git a/man/wimenu.1 b/man/wimenu.1 index 6be0268e..47d85451 100644 --- a/man/wimenu.1 +++ b/man/wimenu.1 @@ -42,6 +42,11 @@ sliding window before this limit is imposed. \-p \fI\fR The string \fI\fR will be show before the input field when the menu is opened. +.TP +\-r \fI\fR +Display completion items as a vertical list, one per +row, rather than a horizontal list, side\-by\-side. A +maximum of \fI\fR rows will be displayed. .SH ADVANCED ARGUMENTS @@ -151,9 +156,7 @@ the following script provides the appropriate completions: print cmds >fifo; fflush(fifo) } - # Store the last line we get and print it when done - { last = $0 } - END { print last } + { print; fflush() } # Push out a new set of completions function update(str, opts) { @@ -173,14 +176,14 @@ the following script provides the appropriate completions: # Skip the trailing part of the command getline rest } - \&' + \&' | tail -1 .fi .P In theory, this facility can be used for myriad purposes, including hijacking the programmable completion facilities of -most shells. +most shells. See also the provided examples\fI[1]\fR. .SH ENVIRONMENT .TP @@ -196,7 +199,9 @@ provided. wmii(1), wmiir(1), wistrug(1), wmii9menu(1), dmenu(1) .P -\fI[1]\fR http://www.suckless.org/wiki/wmii/tips/9p_tips +\fI[1]\fR http://www.suckless.org/wiki/wmii/tips/9p_tips +.P +\fI[2]\fR @EXAMPLES@ .\" man code generated by txt2tags 2.5 (http://txt2tags.sf.net) diff --git a/man/wimenu.man1 b/man/wimenu.man1 index afe43b08..16d6b1ef 100644 --- a/man/wimenu.man1 +++ b/man/wimenu.man1 @@ -41,6 +41,10 @@ following. More advanced options are documented below. : -p The string will be show before the input field when the menu is opened. +: -r + Display completion items as a vertical list, one per + row, rather than a horizontal list, side-by-side. A + maximum of rows will be displayed. : = ADVANCED ARGUMENTS = @@ -130,9 +134,7 @@ wimenu -c fifo; fflush(fifo) } - # Store the last line we get and print it when done - { last = $0 } - END { print last } + { print; fflush() } # Push out a new set of completions function update(str, opts) { @@ -152,12 +154,12 @@ wimenu -c