[hackers] [wmii] [menu] Update manual. Add file completion example. || Kris Maglione

From: <hg_AT_suckless.org>
Date: Fri, 8 Oct 2010 18:41:49 +0000 (UTC)

changeset: 2782:3a645190bda8
user: Kris Maglione <kris_AT_suckless.org>
date: Thu Oct 07 16:31:42 2010 -0400
files: Makefile cmd/wmii.sh.sh examples/Makefile examples/wimenu-file-completion.sh man/wimenu.1 man/wimenu.man1 rc/wmiirc.sh
description:
[menu] Update manual. Add file completion example.

diff -r 3a09477a41ff -r 3a645190bda8 Makefile
--- a/Makefile Thu Oct 07 16:31:41 2010 -0400
+++ b/Makefile Thu Oct 07 16:31:42 2010 -0400
@@ -4,6 +4,7 @@
 
 DIRS = \
         doc \
+ examples \
         man \
         lib \
         cmd \
diff -r 3a09477a41ff -r 3a645190bda8 cmd/wmii.sh.sh
--- a/cmd/wmii.sh.sh Thu Oct 07 16:31:41 2010 -0400
+++ b/cmd/wmii.sh.sh Thu Oct 07 16:31:42 2010 -0400
@@ -85,21 +85,26 @@
 }
 
 _wi_text() {
- cat <<'!'
-Event Key
- Key "$@"
-!
         eval "cat <<!
 $( (test ! -t 0 && cat; for a; do eval "$a"; done) | sed '/^[ ]/s/\([$`\\]\)/\\\1/g')
 !
 "
 }
 
+_wi_events=""
 wi_events() {
- #cho "$(_wi_text "$@" | awk "$(_wi_script)")" | cat -n
- eval "$(_wi_text "$@" | awk "$(_wi_script)")"
+ eval=""; [ "$1" = -e ] && eval=1 && shift
+ _wi_events="$(_wi_text "$@")
+$_wi_events"
+ # -n "$eval" ] && echo "$_wi_events" | awk "$(_wi_script)" >&2
+ [ -n "$eval" ] && eval "$(echo "$_wi_events" | awk "$(_wi_script)")"
 }
 
+wi_events <<'!'
+Event Key
+ Key "$@"
+!
+
 wi_fatal() {
         echo $scriptname: Fatal: $*
         exit 1
diff -r 3a09477a41ff -r 3a645190bda8 examples/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/Makefile Thu Oct 07 16:31:42 2010 -0400
@@ -0,0 +1,7 @@
+ROOT=..
+include $(ROOT)/mk/hdr.mk
+include $(ROOT)/mk/wmii.mk
+
+DOCS = wimenu-file-completion.sh
+DOCDIR = $(DOC)/examples
+
diff -r 3a09477a41ff -r 3a645190bda8 examples/wimenu-file-completion.sh
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/wimenu-file-completion.sh Thu Oct 07 16:31:42 2010 -0400
@@ -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 -r 3a09477a41ff -r 3a645190bda8 man/wimenu.1
--- a/man/wimenu.1 Thu Oct 07 16:31:41 2010 -0400
+++ b/man/wimenu.1 Thu Oct 07 16:31:42 2010 -0400
@@ -42,6 +42,11 @@
 \-p \fI<prompt>\fR
 The string \fI<prompt>\fR will be show before the input field
 when the menu is opened.
+.TP
+\-r \fI<rows>\fR
+Display completion items as a vertical list, one per
+row, rather than a horizontal list, side\-by\-side. A
+maximum of \fI<rows>\fR rows will be displayed.
 
 
 .SH ADVANCED ARGUMENTS
@@ -151,9 +156,7 @@
                   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 @@
                   # 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 @@
 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 -r 3a09477a41ff -r 3a645190bda8 man/wimenu.man1
--- a/man/wimenu.man1 Thu Oct 07 16:31:41 2010 -0400
+++ b/man/wimenu.man1 Thu Oct 07 16:31:42 2010 -0400
@@ -41,6 +41,10 @@
 : -p <prompt>
         The string <prompt> will be show before the input field
         when the menu is opened.
+: -r <rows>
+ Display completion items as a vertical list, one per
+ row, rather than a horizontal list, side-by-side. A
+ maximum of <rows> rows will be displayed.
 :
 
 = ADVANCED ARGUMENTS =
@@ -130,9 +134,7 @@
                 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) {
@@ -152,12 +154,12 @@
                 # Skip the trailing part of the command
                 getline rest
         }
-'
+' | tail -1
 ```
 
 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[1].
 
 = ENVIRONMENT =
 
@@ -170,5 +172,5 @@
 = SEE ALSO =
 wmii(1), wmiir(1), wistrug(1), wmii9menu(1), dmenu(1)
 
-[1] http://www.suckless.org/wiki/wmii/tips/9p_tips
-
+[1] http://www.suckless.org/wiki/wmii/tips/9p_tips +
+[2] @EXAMPLES@
diff -r 3a09477a41ff -r 3a645190bda8 rc/wmiirc.sh
--- a/rc/wmiirc.sh Thu Oct 07 16:31:41 2010 -0400
+++ b/rc/wmiirc.sh Thu Oct 07 16:31:42 2010 -0400
@@ -78,8 +78,8 @@
 echo colors $WMII_NORMCOLORS | wmiir create $noticebar
 
 # Event processing
-events() {
- cat <<'!'
+local_events | wi_events
+wi_events <<'!'
 # Events
 Event CreateTag
         echo colors "$WMII_NORMCOLORS$wi_newline" label "$@" | wmiir create "/lbar/$@"
@@ -237,15 +237,14 @@
         wmiir xwrite /ctl view $(wi_tags | sort -r | wi_nexttag)
 !
         for i in 0 1 2 3 4 5 6 7 8 9; do
- cat <<!
+ wi_events <<!
 Key $MODKEY-$i # Move to the numbered view
         wmiir xwrite /ctl view "$i"
 Key $MODKEY-Shift-$i # Retag selected client with the numbered tag
         wmiir xwrite /client/sel/ctl tags "$i"
 !
- done
-}
-wi_events events local_events
+done
+wi_events -e
 
 # WM Configuration
 wmiir write /ctl <<!
Received on Fri Oct 08 2010 - 20:41:49 CEST

This archive was generated by hypermail 2.2.0 : Fri Oct 08 2010 - 20:48:05 CEST