1emulate -L zsh
2setopt extendedglob
3
4local -a word_functions
5
6word_functions=(backward-kill-word backward-word
7  capitalize-word down-case-word
8  forward-word kill-word
9  transpose-words up-case-word)
10
11[[ -z $1 ]] && autoload -Uz read-from-minibuffer
12
13local REPLY detail f wordstyle teststyle
14
15if ! zle -l $word_functions[1]; then
16  for f in $word_functions; do
17    autoload -Uz $f-match
18    zle -N $f $f-match
19  done
20fi
21
22
23while true; do
24  if [[ -n $WIDGET && -z $1 ]]; then
25    read-from-minibuffer -k1 "Word styles (hit return for more detail):
26(b)ash (n)ormal (s)hell (w)hitespace (d)efault (q)uit
27(B), (N), (S), (W) as above with subword matching
28${detail}? " || return 1
29  else
30    REPLY=$1
31  fi
32
33  detail=
34
35  case $REPLY in
36    ([bB]*)
37    # bash style
38    wordstyle=standard
39    zstyle ':zle:*' word-chars ''
40    zstyle ':zle:*' skip-whitespace-first true
41    ;;
42
43    ([nN]*)
44    # normal zsh style
45    wordstyle=standard
46    zstyle ':zle:*' word-chars "$WORDCHARS"
47    zstyle ':zle:*' skip-whitespace-first false
48    ;;
49
50    ([sS]*)
51    # shell command arguments or special tokens
52    wordstyle=shell
53    zstyle ':zle:*' skip-whitespace-first false
54    ;;
55
56    ([wW]*)
57    # whitespace-delimited
58    wordstyle=space
59    zstyle ':zle:*' skip-whitespace-first false
60    ;;
61
62    (d*)
63    # default: could also return widgets to builtins here
64    wordstyle=
65    zstyle -d ':zle:*' word-chars
66    zstyle -d ':zle:*' skip-whitespace-first
67    ;;
68
69    (q*)
70    # quit without setting
71    return 1
72    ;;
73
74    (*)
75    detail="\
76(b)ash:       Word characters are alphanumerics only
77(n)ormal:     Word characters are alphanumerics plus \$WORDCHARS
78(s)hell:      Words are command arguments using shell syntax
79(w)hitespace: Words are whitespace-delimited
80(d)efault:    Use default, no special handling (usually same as \`n')
81(q)uit:       Quit without setting a new style
82"
83    if [[ -z $WIDGET || -n $1 ]]; then
84      print "Usage: $0 word-style
85where word-style is one of the characters in parentheses:
86$detail" >&2
87      return 1
88    fi
89    continue
90    ;;
91  esac
92
93  if [[ -n $wordstyle ]]; then
94    if [[ $REPLY = [[:upper:]]* ]]; then
95      wordstyle+=-subword
96    fi
97    zstyle ':zle:*' word-style $wordstyle
98  fi
99  return
100done
101