Commit 7378b55deda3ce4704448474b3a383a738317de3

Authored by mj
1 parent f4c442e7d6
Exists in master and in 1 other branch 02-merge

Squashed 'repos/robbyrussell/oh-my-zsh/' changes from 22632aa..00344f7

00344f7 Merge branch 'ferenczy-cygwin'
63d7500 Added setting of the window title in Cygwin
b457ae2 Merge pull request #4651 from ionelmc/patch-1
f7ed724 Merge pull request #3321 from moul/pr-tmux-cssh
d55111f Support all kinds of putty-like terminal strings.
78af29e Merge branch 'marking-master'
584e0a6 Use shwordsplit in open_command()
afdfe23 Add empty string parameter to start command
0a79f1e make this work in the git-for-windows SDK
a19ec10 Merge pull request #4649 from psprint/master
39e4dfb Updated README.md
c885290 Renamed README.txt to README.md
711c1bc Add support for "putty" $TERM in termsupport.zsh
c2b4d9f znt: n-history supports multi-line cmds and starts with current buffer
afd28bf Merge pull request #4368 from psprint/master
fea74b4 Fix open_command nohup call
ef031df Merge pull request #4207 from mpscholten/master
46824b3 Merge pull request #4191 from apjanke/vi-mode-fix-smkx
d3b9520 Merge branch 'apjanke-z-allow-custom'
2205aa4 z plugin: fix loading from custom location
85d9495 Merge pull request #4181 from posva/z
96108e1 Merge branch 'apjanke-histsubstr-update-2015_04_26'
7e39839 history-substring-search: update to upstream version 2015-09-28
d7e6ba0 history-substring-search: add a script to automatically update OMZ from the upstream repo
befb5b6 Merge pull request #4634 from mfaerevaag/master
424800e Merge pull request #4539 from Gnouc/master
90e1630 Merge pull request #4443 from apjanke/github-update
dfe10af Merge branch 'apjanke-termsupport-protect-subshell'
f0981d5 termsupport: protect subshell with quotes
d2e1c71 Merge pull request #4534 from mcornella/fix-open_command-nohup-tmux
ebfba0e Drop the use of nohup on OSX for tmux compatibility
7d9648e [wd] update minor version (v0.4.2)
8ea7735 vi-mode: remove line-init/finish widgets that broken terminfo keybindings
24492a2 Update github plugin to work with current hub versions
d336396 Updated z version
c25002a Removing `source` command
4c292ea Initial commit of Zsh Navigation Tools
53a6335 Change coding style
45cd2df Update uninstall.sh
d351c76 Also accept any forms of yes as an answer to the "do you want to update?" prompt
b7fcdc7 [tmux-cssh plugin] Initial version

git-subtree-dir: repos/robbyrussell/oh-my-zsh
git-subtree-split: 00344f7628099ad614a9db1b4a4d71155dc932ef

Showing 47 changed files with 3165 additions and 592 deletions Side-by-side Diff

... ... @@ -16,19 +16,28 @@ function take() {
16 16 }
17 17  
18 18 function open_command() {
  19 + emulate -L zsh
  20 + setopt shwordsplit
  21 +
19 22 local open_cmd
20 23  
21 24 # define the open command
22 25 case "$OSTYPE" in
23   - darwin*) open_cmd="open" ;;
24   - cygwin*) open_cmd="cygstart" ;;
25   - linux*) open_cmd="xdg-open" ;;
  26 + darwin*) open_cmd='open' ;;
  27 + cygwin*) open_cmd='cygstart' ;;
  28 + linux*) open_cmd='xdg-open' ;;
  29 + msys*) open_cmd='start ""' ;;
26 30 *) echo "Platform $OSTYPE not supported"
27 31 return 1
28 32 ;;
29 33 esac
30 34  
31   - nohup $open_cmd "$@" &>/dev/null
  35 + # don't use nohup on OSX
  36 + if [[ "$OSTYPE" == darwin* ]]; then
  37 + $open_cmd "$@" &>/dev/null
  38 + else
  39 + nohup $open_cmd "$@" &>/dev/null
  40 + fi
32 41 }
33 42  
34 43 #
... ... @@ -67,7 +76,7 @@ function try_alias_value() {
67 76 #
68 77 # Arguments:
69 78 # 1. name - The variable to set
70   -# 2. val - The default value
  79 +# 2. val - The default value
71 80 # Return value:
72 81 # 0 if the variable exists, 3 if it was set
73 82 #
... ... @@ -81,12 +90,12 @@ function default() {
81 90 #
82 91 # Arguments:
83 92 # 1. name - The env variable to set
84   -# 2. val - The default value
  93 +# 2. val - The default value
85 94 # Return value:
86 95 # 0 if the env variable exists, 3 if it was set
87 96 #
88 97 function env_default() {
89   - env | grep -q "^$1=" && return 0
  98 + env | grep -q "^$1=" && return 0
90 99 export "$1=$2" && return 3
91 100 }
92 101  
... ... @@ -101,7 +110,7 @@ zmodload zsh/langinfo
101 110 #
102 111 # By default, reserved characters and unreserved "mark" characters are
103 112 # not escaped by this function. This allows the common usage of passing
104   -# an entire URL in, and encoding just special characters in it, with
  113 +# an entire URL in, and encoding just special characters in it, with
105 114 # the expectation that reserved and mark characters are used appropriately.
106 115 # The -r and -m options turn on escaping of the reserved and mark characters,
107 116 # respectively, which allows arbitrary strings to be fully escaped for
... ... @@ -111,8 +120,8 @@ zmodload zsh/langinfo
111 120 # Returns nonzero if encoding failed.
112 121 #
113 122 # Usage:
114   -# omz_urlencode [-r] [-m] <string>
115   -#
  123 +# omz_urlencode [-r] [-m] [-P] <string>
  124 +#
116 125 # -r causes reserved characters (;/?:@&=+$,) to be escaped
117 126 #
118 127 # -m causes "mark" characters (_.!~*''()-) to be escaped
... ... @@ -177,8 +186,8 @@ function omz_urlencode() {
177 186 # URL-decode a string
178 187 #
179 188 # Decodes a RFC 2396 URL-encoded (%-escaped) string.
180   -# This decodes the '+' and '%' escapes in the input string, and leaves
181   -# other characters unchanged. Does not enforce that the input is a
  189 +# This decodes the '+' and '%' escapes in the input string, and leaves
  190 +# other characters unchanged. Does not enforce that the input is a
182 191 # valid URL-encoded string. This is a convenience to allow callers to
183 192 # pass in a full URL or similar strings and decode them for human
184 193 # presentation.
... ... @@ -196,7 +205,7 @@ function omz_urldecode {
196 205 local caller_encoding=$langinfo[CODESET]
197 206 local LC_ALL=C
198 207 export LC_ALL
199   -
  208 +
200 209 # Change + back to ' '
201 210 local tmp=${encoded_url:gs/+/ /}
202 211 # Protect other escapes to pass through the printf unchanged
... ... @@ -220,4 +229,3 @@ function omz_urldecode {
220 229  
221 230 echo -E "$decoded"
222 231 }
223   -
... ... @@ -9,7 +9,7 @@
9 9 function title {
10 10 emulate -L zsh
11 11 setopt prompt_subst
12   -
  12 +
13 13 [[ "$EMACS" == *term* ]] && return
14 14  
15 15 # if $2 is unset use $1 as default
... ... @@ -18,7 +18,7 @@ function title {
18 18  
19 19 if [[ "$TERM" == screen* ]]; then
20 20 print -Pn "\ek$1:q\e\\" #set screen hardstatus, usually truncated at 20 chars
21   - elif [[ "$TERM" == xterm* ]] || [[ "$TERM" == rxvt* ]] || [[ "$TERM" == ansi ]] || [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
  21 + elif [[ "$TERM" == xterm* ]] || [[ "$TERM" == putty* ]] || [[ "$TERM" == rxvt* ]] || [[ "$TERM" == ansi ]] || [[ "$TERM" == cygwin ]] || [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
22 22 print -Pn "\e]2;$2:q\a" #set window name
23 23 print -Pn "\e]1;$1:q\a" #set icon (=tab) name
24 24 fi
... ... @@ -74,7 +74,7 @@ if [[ &quot;$TERM_PROGRAM&quot; == &quot;Apple_Terminal&quot; ]] &amp;&amp; [[ -z &quot;$INSIDE_EMACS&quot; ]]; then
74 74 # the host name to disambiguate local vs. remote paths.
75 75  
76 76 # Percent-encode the pathname.
77   - local URL_PATH=$(omz_urlencode -P $PWD)
  77 + local URL_PATH="$(omz_urlencode -P $PWD)"
78 78 [[ $? != 0 ]] && return 1
79 79 local PWD_URL="file://$HOST$URL_PATH"
80 80 # Undocumented Terminal.app-specific control sequence
plugins/github/README.md
... ... @@ -0,0 +1,46 @@
  1 +# github
  2 +
  3 +This plugin supports working with GitHub the command line. It provides a few things:
  4 +
  5 +* Sets up the `hub` wrapper and completions for the `git` command if you have `hub` installed.
  6 +* Completion for the `github` Ruby gem.
  7 +* Convenience functions for working with repos and URLs.
  8 +
  9 +### Functions
  10 +
  11 +* `empty_gh` - Creates a new empty repo (with a `README.md`) and pushes it to GitHub
  12 +* `new_gh` - Initializes an existing directory as a repo and pushes it to GitHub
  13 +* `exist_gh` - Takes an existing repo and pushes it to GitHub
  14 +* `git.io` - Shortens a URL using [git.io](http://git.io)
  15 +
  16 +
  17 +## Installation
  18 +
  19 +[Hub](http://github.com/github/hub) needs to be installed if you want to use it. On OS X with Homebrew, this can be done with `brew install hub`. The `hub` completion definition needs to be added to your `$FPATH` before initializing OMZ.
  20 +
  21 +The [`github` Ruby gem](http://github.com/defunkt/github-gem) needs to be installed if you want to use it.
  22 +
  23 +### Configuration
  24 +
  25 +These settings affect `github`'s behavior.
  26 +
  27 +#### Environment variables
  28 +
  29 +* `$GITHUB_USER`
  30 +* `$GITHUB_PASSWORD`
  31 +
  32 +#### Git configuration options
  33 +
  34 +* `github.user` - GitHub username for repo operations
  35 +
  36 +See `man hub` for more details.
  37 +
  38 +### Homebrew installation note
  39 +
  40 +If you have installed `hub` using Homebrew, its completions may not be on your `$FPATH` if you are using the system `zsh`. Homebrew installs `zsh` completion definitions to `/usr/local/share/zsh/site-functions`, which on `$FPATH` for the Homebrew-installed `zsh`, but not for the system `zsh`. If you want it to work with the system `zsh`, add this to your `~/.zshrc` before it sources `oh-my-zsh.sh`.
  41 +
  42 +```zsh
  43 +if (( ! ${fpath[(I)/usr/local/share/zsh/site-functions]} )); then
  44 + FPATH=/usr/local/share/zsh/site-functions:$FPATH
  45 +fi
  46 +```
plugins/github/_github
... ... @@ -1,40 +0,0 @@
1   -#compdef github
2   -#autoload
3   -
4   -# in order to make this work, you will need to have the github gem installed
5   -# http://github.com/defunkt/github-gem
6   -
7   -# github zsh completion, based on homebrew completion
8   -
9   -local -a _1st_arguments
10   -_1st_arguments=(
11   - 'browse:Open this repo in a web browser'
12   - 'clone:Clone a repo'
13   - 'config:Automatically set configuration info, or pass args to specify'
14   - 'create-from-local:Create a new GitHub repository from the current local repository'
15   - 'create:Create a new empty GitHub repository'
16   - 'fetch:Fetch from a remote to a local branch'
17   - 'fetch_all:Fetch all refs from a user'
18   - 'fork:Forks a GitHub repository'
19   - 'home:Open this repos master branch in a web browser'
20   - 'ignore:Ignore a SHA from github network commits'
21   - 'info:Info about this project'
22   - 'issues:Project issues tools'
23   - 'network:Project network tools - sub-commands : web [user], list, fetch, commits'
24   - 'open:Open the given user/project in a web browser'
25   - 'pull-request:Generate the text for a pull request'
26   - 'pull:Pull from a remote'
27   - 'search:Search GitHub for the given repository name'
28   - 'track:Track another users repository'
29   -)
30   -
31   -local expl
32   -local -a pkgs installed_pkgs
33   -
34   -_arguments \
35   - '*:: :->subcmds' && return 0
36   -
37   -if (( CURRENT == 1 )); then
38   - _describe -t commands "github subcommand" _1st_arguments
39   - return
40   -fi
plugins/github/github.plugin.zsh
1   -# Setup hub function for git, if it is available; http://github.com/defunkt/hub
2   -if [ "$commands[(I)hub]" ] && [ "$commands[(I)ruby]" ]; then
3   - # Autoload _git completion functions
4   - if declare -f _git > /dev/null; then
5   - _git
6   - fi
7   -
8   - if declare -f _git_commands > /dev/null; then
9   - _hub_commands=(
10   - 'alias:show shell instructions for wrapping git'
11   - 'pull-request:open a pull request on GitHub'
12   - 'fork:fork origin repo on GitHub'
13   - 'create:create new repo on GitHub for the current project'
14   - 'browse:browse the project on GitHub'
15   - 'compare:open GitHub compare view'
16   - )
17   - # Extend the '_git_commands' function with hub commands
18   - eval "$(declare -f _git_commands | sed -e 's/base_commands=(/base_commands=(${_hub_commands} /')"
19   - fi
20   - # eval `hub alias -s zsh`
21   - function git(){
22   - if ! (( $+_has_working_hub )); then
23   - hub --version &> /dev/null
24   - _has_working_hub=$(($? == 0))
25   - fi
26   - if (( $_has_working_hub )) ; then
27   - hub "$@"
28   - else
29   - command git "$@"
30   - fi
31   - }
  1 +# Set up hub wrapper for git, if it is available; http://github.com/github/hub
  2 +if [ "$commands[(I)hub]" ]; then
  3 + if hub --version &>/dev/null; then
  4 + eval $(hub alias -s zsh)
  5 + fi
32 6 fi
33 7  
34 8 # Functions #################################################################
35 9  
36   -# https://github.com/dbb
  10 +# Based on https://github.com/dbb/githome/blob/master/.config/zsh/functions
37 11  
38   -
39   -# empty_gh [NAME_OF_REPO]
  12 +# empty_gh <NAME_OF_REPO>
40 13 #
41 14 # Use this when creating a new repo from scratch.
  15 +# Creates a new repo with a blank README.md in it and pushes it up to GitHub.
42 16 empty_gh() { # [NAME_OF_REPO]
43   - repo=$1
44   - ghuser=$( git config github.user )
  17 + emulate -L zsh
  18 + local repo=$1
45 19  
46   - mkdir "$repo"
47   - cd "$repo"
48   - git init
49   - touch README
50   - git add README
51   - git commit -m 'Initial commit.'
52   - git remote add origin git@github.com:${ghuser}/${repo}.git
53   - git push -u origin master
  20 + mkdir "$repo"
  21 + touch "$repo/README.md"
  22 + new_gh "$repo"
54 23 }
55 24  
56 25 # new_gh [DIRECTORY]
... ... @@ -58,16 +27,25 @@ empty_gh() { # [NAME_OF_REPO]
58 27 # Use this when you have a directory that is not yet set up for git.
59 28 # This function will add all non-hidden files to git.
60 29 new_gh() { # [DIRECTORY]
61   - cd "$1"
62   - ghuser=$( git config github.user )
  30 + emulate -L zsh
  31 + local repo="$1"
  32 + cd "$repo" \
  33 + || return
63 34  
64   - git init
65   - # add all non-dot files
66   - print '.*'"\n"'*~' >> .gitignore
67   - git add ^.*
68   - git commit -m 'Initial commit.'
69   - git remote add origin git@github.com:${ghuser}/${repo}.git
70   - git push -u origin master
  35 + git init \
  36 + || return
  37 + # add all non-dot files
  38 + print '.*'"\n"'*~' >> .gitignore
  39 + git add [^.]* \
  40 + || return
  41 + git add .gitignore \
  42 + || return
  43 + git commit -m 'Initial commit.' \
  44 + || return
  45 + hub create \
  46 + || return
  47 + git push -u origin master \
  48 + || return
71 49 }
72 50  
73 51 # exist_gh [DIRECTORY]
... ... @@ -75,13 +53,13 @@ new_gh() { # [DIRECTORY]
75 53 # Use this when you have a git repo that's ready to go and you want to add it
76 54 # to your GitHub.
77 55 exist_gh() { # [DIRECTORY]
78   - cd "$1"
79   - name=$( git config user.name )
80   - ghuser=$( git config github.user )
81   - repo=$1
  56 + emulate -L zsh
  57 + local repo=$1
  58 + cd "$repo"
82 59  
83   - git remote add origin git@github.com:${ghuser}/${repo}.git
84   - git push -u origin master
  60 + hub create \
  61 + || return
  62 + git push -u origin master
85 63 }
86 64  
87 65 # git.io "GitHub URL"
... ... @@ -91,7 +69,10 @@ exist_gh() { # [DIRECTORY]
91 69 # source: https://github.com/nvogel/dotzsh
92 70 # documentation: https://github.com/blog/985-git-io-github-url-shortener
93 71 #
94   -git.io() {curl -i -s http://git.io -F "url=$1" | grep "Location" | cut -f 2 -d " "}
  72 +git.io() {
  73 + emulate -L zsh
  74 + curl -i -s http://git.io -F "url=$1" | grep "Location" | cut -f 2 -d " "
  75 +}
95 76  
96 77 # End Functions #############################################################
97 78  
plugins/history-substring-search/README.markdown
... ... @@ -1,7 +0,0 @@
1   -To activate this script, please include it the `plugins` variable within `~/.zshrc`
2   -
3   - `plugins=(git history-substring-search)`
4   -
5   -See the "history-substring-search.zsh" file for more information:
6   -
7   - `sed -n '2,/^$/s/^#//p' history-substring-search.zsh | more`
plugins/history-substring-search/README.md
... ... @@ -0,0 +1,149 @@
  1 +zsh-history-substring-search
  2 +==============================================================================
  3 +
  4 +This is a clean-room implementation of the [Fish shell][1]'s history search
  5 +feature, where you can type in any part of any previously entered command
  6 +and press the UP and DOWN arrow keys to cycle through the matching commands.
  7 +You can also use K and J in VI mode or ^P and ^N in EMACS mode for the same.
  8 +
  9 +[1]: http://fishshell.com
  10 +[2]: http://www.zsh.org/mla/users/2009/msg00818.html
  11 +[3]: http://sourceforge.net/projects/fizsh/
  12 +[4]: https://github.com/robbyrussell/oh-my-zsh/pull/215
  13 +[5]: https://github.com/zsh-users/zsh-history-substring-search
  14 +[6]: https://github.com/zsh-users/zsh-syntax-highlighting
  15 +
  16 +------------------------------------------------------------------------------
  17 +Requirements
  18 +------------------------------------------------------------------------------
  19 +
  20 +* [ZSH](http://zsh.sourceforge.net) 4.3 or newer
  21 +
  22 +------------------------------------------------------------------------------
  23 +Usage
  24 +------------------------------------------------------------------------------
  25 +
  26 +1. Load this script into your interactive ZSH session:
  27 +
  28 + % source zsh-history-substring-search.zsh
  29 +
  30 + If you want to use [zsh-syntax-highlighting][6] along with this script,
  31 + then make sure that you load it *before* you load this script:
  32 +
  33 + % source zsh-syntax-highlighting.zsh
  34 + % source zsh-history-substring-search.zsh
  35 +
  36 +2. Bind keyboard shortcuts to this script's functions:
  37 +
  38 + # bind UP and DOWN arrow keys
  39 + zmodload zsh/terminfo
  40 + bindkey "$terminfo[kcuu1]" history-substring-search-up
  41 + bindkey "$terminfo[kcud1]" history-substring-search-down
  42 +
  43 + # bind UP and DOWN arrow keys (compatibility fallback
  44 + # for Ubuntu 12.04, Fedora 21, and MacOSX 10.9 users)
  45 + bindkey '^[[A' history-substring-search-up
  46 + bindkey '^[[B' history-substring-search-down
  47 +
  48 + # bind P and N for EMACS mode
  49 + bindkey -M emacs '^P' history-substring-search-up
  50 + bindkey -M emacs '^N' history-substring-search-down
  51 +
  52 + # bind k and j for VI mode
  53 + bindkey -M vicmd 'k' history-substring-search-up
  54 + bindkey -M vicmd 'j' history-substring-search-down
  55 +
  56 +3. Type any part of any previous command and then:
  57 +
  58 + * Press the UP arrow key to select the nearest command that (1) contains
  59 + your query and (2) is older than the current command in the command
  60 + history.
  61 +
  62 + * Press the DOWN arrow key to select the nearest command that (1)
  63 + contains your query and (2) is newer than the current command in the
  64 + command history.
  65 +
  66 + * Press ^U (the Control and U keys simultaneously) to abort the search.
  67 +
  68 +4. If a matching command spans more than one line of text, press the LEFT
  69 + arrow key to move the cursor away from the end of the command, and then:
  70 +
  71 + * Press the UP arrow key to move the cursor to the line above. When the
  72 + cursor reaches the first line of the command, pressing the UP arrow
  73 + key again will cause this script to perform another search.
  74 +
  75 + * Press the DOWN arrow key to move the cursor to the line below. When
  76 + the cursor reaches the last line of the command, pressing the DOWN
  77 + arrow key again will cause this script to perform another search.
  78 +
  79 +------------------------------------------------------------------------------
  80 +Configuration
  81 +------------------------------------------------------------------------------
  82 +
  83 +This script defines the following global variables. You may override their
  84 +default values only after having loaded this script into your ZSH session.
  85 +
  86 +* HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND is a global variable that defines
  87 + how the query should be highlighted inside a matching command. Its default
  88 + value causes this script to highlight using bold, white text on a magenta
  89 + background. See the "Character Highlighting" section in the zshzle(1) man
  90 + page to learn about the kinds of values you may assign to this variable.
  91 +
  92 +* HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND is a global variable that
  93 + defines how the query should be highlighted when no commands in the
  94 + history match it. Its default value causes this script to highlight using
  95 + bold, white text on a red background. See the "Character Highlighting"
  96 + section in the zshzle(1) man page to learn about the kinds of values you
  97 + may assign to this variable.
  98 +
  99 +* HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS is a global variable that defines
  100 + how the command history will be searched for your query. Its default value
  101 + causes this script to perform a case-insensitive search. See the "Globbing
  102 + Flags" section in the zshexpn(1) man page to learn about the kinds of
  103 + values you may assign to this variable.
  104 +
  105 +To always receive _unique_ search results, use `setopt HIST_IGNORE_ALL_DUPS`.
  106 +Alternatively, use `setopt HIST_FIND_NO_DUPS` which makes this plugin skip
  107 +duplicate _adjacent_ search results as you cycle through them---however, this
  108 +does not guarantee that search results are unique: if your search results were
  109 +"Dog", "Dog", "HotDog", "Dog", then cycling them gives "Dog", "HotDog", "Dog".
  110 +Notice that the "Dog" search result appeared twice as you cycled through them!
  111 +If you wish to avoid this limitation, then use `setopt HIST_IGNORE_ALL_DUPS`.
  112 +
  113 +------------------------------------------------------------------------------
  114 +History
  115 +------------------------------------------------------------------------------
  116 +
  117 +This script was originally written by [Peter Stephenson][2], who published it
  118 +to the ZSH users mailing list (thereby making it public domain) in September
  119 +2009. It was later revised by Guido van Steen and released under the BSD
  120 +license (see below) as part of [the fizsh project][3] in January 2011.
  121 +
  122 +It was later extracted from fizsh release 1.0.1, refactored heavily, and
  123 +repackaged as both an [oh-my-zsh plugin][4] and as an independently loadable
  124 +[ZSH script][5] by Suraj N. Kurapati in 2011.
  125 +
  126 +It was [further developed][4] by Guido van Steen, Suraj N. Kurapati, Sorin
  127 +Ionescu, and Vincent Guerci in 2011.
  128 +
  129 +------------------------------------------------------------------------------
  130 +Oh My Zsh Distribution Notes
  131 +------------------------------------------------------------------------------
  132 +
  133 +What you are looking at now is Oh My Zsh's repackaging of zsh-history-substring-search
  134 +as an OMZ module inside the Oh My Zsh distribution.
  135 +
  136 +The upstream repo, zsh-users/zsh-history-substring-search, can be found on GitHub at
  137 +https://github.com/zsh-users/zsh-history-substring-search.
  138 +
  139 +This downstream copy was last updated from the following upstream commit:
  140 +
  141 + SHA: 2c295432175990c1bb4e90bc13f609daa67a25d6
  142 + Commit date: 2015-09-28 10:47:34 -0700
  143 +
  144 +Everything above this section is a copy of the original upstream's README, so things
  145 +may differ slightly when you're using this inside OMZ. In particular, you do not
  146 +need to set up key bindings for the up and down arrows yourself in `~/.zshrc`; the OMZ
  147 +plugin does that for you. You may still want to set up additional emacs- or vi-specific
  148 +bindings as mentioned above.
  149 +
plugins/history-substring-search/history-substring-search.plugin.zsh
1   -# This file integrates the history-substring-search script into oh-my-zsh.
  1 +# This file integrates the zsh-history-substring-search script into oh-my-zsh.
2 2  
3   -source "$ZSH/plugins/history-substring-search/history-substring-search.zsh"
  3 +source "${0:r:r}.zsh"
4 4  
5 5 if test "$CASE_SENSITIVE" = true; then
6 6 unset HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS
... ... @@ -10,3 +10,14 @@ if test &quot;$DISABLE_COLOR&quot; = true; then
10 10 unset HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
11 11 unset HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
12 12 fi
  13 +
  14 +
  15 +# Bind terminal-specific up and down keys
  16 +
  17 +if [[ -n "$terminfo[kcuu1]" ]]; then
  18 + bindkey "$terminfo[kcuu1]" history-substring-search-up
  19 +fi
  20 +if [[ -n "$terminfo[kcud1]" ]]; then
  21 + bindkey "$terminfo[kcud1]" history-substring-search-down
  22 +fi
  23 +
plugins/history-substring-search/history-substring-search.zsh
1 1 #!/usr/bin/env zsh
2   -#
3   -# This is a clean-room implementation of the Fish[1] shell's history search
4   -# feature, where you can type in any part of any previously entered command
5   -# and press the UP and DOWN arrow keys to cycle through the matching commands.
6   -#
7   -#-----------------------------------------------------------------------------
8   -# Usage
9   -#-----------------------------------------------------------------------------
10   -#
11   -# 1. Load this script into your interactive ZSH session:
12   -#
13   -# % source history-substring-search.zsh
14   -#
15   -# If you want to use the zsh-syntax-highlighting[6] script along with this
16   -# script, then make sure that you load it *before* you load this script:
17   -#
18   -# % source zsh-syntax-highlighting.zsh
19   -# % source history-substring-search.zsh
20   -#
21   -# 2. Type any part of any previous command and then:
22   -#
23   -# * Press the UP arrow key to select the nearest command that (1) contains
24   -# your query and (2) is older than the current command in the command
25   -# history.
26   -#
27   -# * Press the DOWN arrow key to select the nearest command that (1)
28   -# contains your query and (2) is newer than the current command in the
29   -# command history.
30   -#
31   -# * Press ^U (the Control and U keys simultaneously) to abort the search.
32   -#
33   -# 3. If a matching command spans more than one line of text, press the LEFT
34   -# arrow key to move the cursor away from the end of the command, and then:
35   -#
36   -# * Press the UP arrow key to move the cursor to the line above. When the
37   -# cursor reaches the first line of the command, pressing the UP arrow
38   -# key again will cause this script to perform another search.
39   -#
40   -# * Press the DOWN arrow key to move the cursor to the line below. When
41   -# the cursor reaches the last line of the command, pressing the DOWN
42   -# arrow key again will cause this script to perform another search.
43   -#
44   -#-----------------------------------------------------------------------------
45   -# Configuration
46   -#-----------------------------------------------------------------------------
47   -#
48   -# This script defines the following global variables. You may override their
49   -# default values only after having loaded this script into your ZSH session.
50   -#
51   -# * HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND is a global variable that defines
52   -# how the query should be highlighted inside a matching command. Its default
53   -# value causes this script to highlight using bold, white text on a magenta
54   -# background. See the "Character Highlighting" section in the zshzle(1) man
55   -# page to learn about the kinds of values you may assign to this variable.
56   -#
57   -# * HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND is a global variable that
58   -# defines how the query should be highlighted when no commands in the
59   -# history match it. Its default value causes this script to highlight using
60   -# bold, white text on a red background. See the "Character Highlighting"
61   -# section in the zshzle(1) man page to learn about the kinds of values you
62   -# may assign to this variable.
63   -#
64   -# * HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS is a global variable that defines
65   -# how the command history will be searched for your query. Its default value
66   -# causes this script to perform a case-insensitive search. See the "Globbing
67   -# Flags" section in the zshexpn(1) man page to learn about the kinds of
68   -# values you may assign to this variable.
69   -#
70   -#-----------------------------------------------------------------------------
71   -# History
72   -#-----------------------------------------------------------------------------
73   -#
74   -# This script was originally written by Peter Stephenson[2], who published it
75   -# to the ZSH users mailing list (thereby making it public domain) in September
76   -# 2009. It was later revised by Guido van Steen and released under the BSD
77   -# license (see below) as part of the fizsh[3] project in January 2011.
78   -#
79   -# It was later extracted from fizsh[3] release 1.0.1, refactored heavily, and
80   -# repackaged as both an oh-my-zsh plugin[4] and as an independently loadable
81   -# ZSH script[5] by Suraj N. Kurapati in 2011.
82   -#
83   -# It was further developed[4] by Guido van Steen, Suraj N. Kurapati, Sorin
84   -# Ionescu, and Vincent Guerci in 2011.
85   -#
86   -# [1]: http://fishshell.com
87   -# [2]: http://www.zsh.org/mla/users/2009/msg00818.html
88   -# [3]: http://sourceforge.net/projects/fizsh/
89   -# [4]: https://github.com/robbyrussell/oh-my-zsh/pull/215
90   -# [5]: https://github.com/sunaku/zsh-history-substring-search
91   -# [6]: https://github.com/nicoulaj/zsh-syntax-highlighting
92   -#
93 2 ##############################################################################
94 3 #
95 4 # Copyright (c) 2009 Peter Stephenson
... ... @@ -140,7 +49,7 @@ HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS=&#39;i&#39;
140 49 # the main ZLE widgets
141 50 #-----------------------------------------------------------------------------
142 51  
143   -function history-substring-search-up() {
  52 +history-substring-search-up() {
144 53 _history-substring-search-begin
145 54  
146 55 _history-substring-search-up-history ||
... ... @@ -150,7 +59,7 @@ function history-substring-search-up() {
150 59 _history-substring-search-end
151 60 }
152 61  
153   -function history-substring-search-down() {
  62 +history-substring-search-down() {
154 63 _history-substring-search-begin
155 64  
156 65 _history-substring-search-down-history ||
... ... @@ -163,14 +72,6 @@ function history-substring-search-down() {
163 72 zle -N history-substring-search-up
164 73 zle -N history-substring-search-down
165 74  
166   -zmodload zsh/terminfo
167   -if [[ -n "$terminfo[kcuu1]" ]]; then
168   - bindkey "$terminfo[kcuu1]" history-substring-search-up
169   -fi
170   -if [[ -n "$terminfo[kcud1]" ]]; then
171   - bindkey "$terminfo[kcud1]" history-substring-search-down
172   -fi
173   -
174 75 #-----------------------------------------------------------------------------
175 76 # implementation details
176 77 #-----------------------------------------------------------------------------
... ... @@ -185,32 +86,20 @@ zmodload -F zsh/parameter
185 86 #
186 87 if [[ $+functions[_zsh_highlight] -eq 0 ]]; then
187 88 #
188   - # Dummy implementation of _zsh_highlight()
189   - # that simply removes existing highlights
190   - #
191   - function _zsh_highlight() {
192   - region_highlight=()
193   - }
194   -
195   - #
196   - # Remove existing highlights when the user
197   - # inserts printable characters into $BUFFER
  89 + # Dummy implementation of _zsh_highlight() that
  90 + # simply removes any existing highlights when the
  91 + # user inserts printable characters into $BUFFER.
198 92 #
199   - function ordinary-key-press() {
  93 + _zsh_highlight() {
200 94 if [[ $KEYS == [[:print:]] ]]; then
201 95 region_highlight=()
202 96 fi
203   - zle .self-insert
204 97 }
205   - zle -N self-insert ordinary-key-press
206 98  
207 99 #
208   - # Override ZLE widgets to invoke _zsh_highlight()
  100 + # The following snippet was taken from the zsh-syntax-highlighting project:
209 101 #
210   - # https://github.com/nicoulaj/zsh-syntax-highlighting/blob/
211   - # bb7fcb79fad797a40077bebaf6f4e4a93c9d8163/zsh-syntax-highlighting.zsh#L121
212   - #
213   - #--------------8<-------------------8<-------------------8<-----------------
  102 + # https://github.com/zsh-users/zsh-syntax-highlighting/blob/56b134f5d62ae3d4e66c7f52bd0cc2595f9b305b/zsh-syntax-highlighting.zsh#L126-161
214 103 #
215 104 # Copyright (c) 2010-2011 zsh-syntax-highlighting contributors
216 105 # All rights reserved.
... ... @@ -241,50 +130,53 @@ if [[ $+functions[_zsh_highlight] -eq 0 ]]; then
241 130 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
242 131 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
243 132 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
244   -
245   - # Load ZSH module zsh/zleparameter, needed to override user defined widgets.
246   - zmodload zsh/zleparameter 2>/dev/null || {
247   - echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter, exiting.' >&2
248   - return -1
249   - }
250   -
251   - # Override ZLE widgets to make them invoke _zsh_highlight.
252   - for event in ${${(f)"$(zle -la)"}:#(_*|orig-*|.run-help|.which-command)}; do
253   - if [[ "$widgets[$event]" == completion:* ]]; then
254   - eval "zle -C orig-$event ${${${widgets[$event]}#*:}/:/ } ; $event() { builtin zle orig-$event && _zsh_highlight } ; zle -N $event"
255   - else
256   - case $event in
257   - accept-and-menu-complete)
258   - eval "$event() { builtin zle .$event && _zsh_highlight } ; zle -N $event"
259   - ;;
260   -
261   - # The following widgets should NOT remove any previously
262   - # applied highlighting. Therefore we do not remap them.
263   - .forward-char|.backward-char|.up-line-or-history|.down-line-or-history)
264   - ;;
265   -
266   - .*)
267   - clean_event=$event[2,${#event}] # Remove the leading dot in the event name
268   - case ${widgets[$clean_event]-} in
269   - (completion|user):*)
270   - ;;
271   - *)
272   - eval "$clean_event() { builtin zle $event && _zsh_highlight } ; zle -N $clean_event"
273   - ;;
274   - esac
275   - ;;
276   - *)
277   - ;;
  133 + #
  134 + #--------------8<-------------------8<-------------------8<-----------------
  135 + # Rebind all ZLE widgets to make them invoke _zsh_highlights.
  136 + _zsh_highlight_bind_widgets()
  137 + {
  138 + # Load ZSH module zsh/zleparameter, needed to override user defined widgets.
  139 + zmodload zsh/zleparameter 2>/dev/null || {
  140 + echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter.' >&2
  141 + return 1
  142 + }
  143 +
  144 + # Override ZLE widgets to make them invoke _zsh_highlight.
  145 + local cur_widget
  146 + for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|run-help|which-command|beep|yank*)}; do
  147 + case $widgets[$cur_widget] in
  148 +
  149 + # Already rebound event: do nothing.
  150 + user:$cur_widget|user:_zsh_highlight_widget_*);;
  151 +
  152 + # User defined widget: override and rebind old one with prefix "orig-".
  153 + user:*) eval "zle -N orig-$cur_widget ${widgets[$cur_widget]#*:}; \
  154 + _zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
  155 + zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
  156 +
  157 + # Completion widget: override and rebind old one with prefix "orig-".
  158 + completion:*) eval "zle -C orig-$cur_widget ${${widgets[$cur_widget]#*:}/:/ }; \
  159 + _zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
  160 + zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
  161 +
  162 + # Builtin widget: override and make it call the builtin ".widget".
  163 + builtin) eval "_zsh_highlight_widget_$cur_widget() { builtin zle .$cur_widget -- \"\$@\" && _zsh_highlight }; \
  164 + zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
  165 +
  166 + # Default: unhandled case.
  167 + *) echo "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" >&2 ;;
278 168 esac
279   - fi
280   - done
281   - unset event clean_event
  169 + done
  170 + }
282 171 #-------------->8------------------->8------------------->8-----------------
  172 +
  173 + _zsh_highlight_bind_widgets
283 174 fi
284 175  
285   -function _history-substring-search-begin() {
  176 +_history-substring-search-begin() {
286 177 setopt localoptions extendedglob
287   - _history_substring_search_move_cursor_eol=false
  178 +
  179 + _history_substring_search_refresh_display=
288 180 _history_substring_search_query_highlight=
289 181  
290 182 #
... ... @@ -308,12 +200,10 @@ function _history-substring-search-begin() {
308 200 #
309 201 # Find all occurrences of the search query in the history file.
310 202 #
311   - # (k) turns it an array of line numbers.
312   - #
313   - # (on) seems to remove duplicates, which are default
314   - # options. They can be turned off by (ON).
  203 + # (k) returns the "keys" (history index numbers) instead of the values
  204 + # (Oa) reverses the order, because (R) returns results reversed.
315 205 #
316   - _history_substring_search_matches=(${(kon)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]})
  206 + _history_substring_search_matches=(${(kOa)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]})
317 207  
318 208 #
319 209 # Define the range of values that $_history_substring_search_match_index
... ... @@ -349,12 +239,15 @@ function _history-substring-search-begin() {
349 239 fi
350 240 }
351 241  
352   -function _history-substring-search-end() {
  242 +_history-substring-search-end() {
353 243 setopt localoptions extendedglob
  244 +
354 245 _history_substring_search_result=$BUFFER
355 246  
356   - # move the cursor to the end of the command line
357   - if [[ $_history_substring_search_move_cursor_eol == true ]]; then
  247 + # the search was succesful so display the result properly by clearing away
  248 + # existing highlights and moving the cursor to the end of the result buffer
  249 + if [[ $_history_substring_search_refresh_display -eq 1 ]]; then
  250 + region_highlight=()
358 251 CURSOR=${#BUFFER}
359 252 fi
360 253  
... ... @@ -379,10 +272,10 @@ function _history-substring-search-end() {
379 272 # read -k -t 200 && zle -U $REPLY
380 273  
381 274 # Exit successfully from the history-substring-search-* widgets.
382   - true
  275 + return 0
383 276 }
384 277  
385   -function _history-substring-search-up-buffer() {
  278 +_history-substring-search-up-buffer() {
386 279 #
387 280 # Check if the UP arrow was pressed to move the cursor within a multi-line
388 281 # buffer. This amounts to three tests:
... ... @@ -405,13 +298,13 @@ function _history-substring-search-up-buffer() {
405 298  
406 299 if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xlbuflines -ne 1 ]]; then
407 300 zle up-line-or-history
408   - return true
  301 + return 0
409 302 fi
410 303  
411   - false
  304 + return 1
412 305 }
413 306  
414   -function _history-substring-search-down-buffer() {
  307 +_history-substring-search-down-buffer() {
415 308 #
416 309 # Check if the DOWN arrow was pressed to move the cursor within a multi-line
417 310 # buffer. This amounts to three tests:
... ... @@ -434,13 +327,13 @@ function _history-substring-search-down-buffer() {
434 327  
435 328 if [[ $#buflines -gt 1 && $CURSOR -ne $#BUFFER && $#xrbuflines -ne 1 ]]; then
436 329 zle down-line-or-history
437   - return true
  330 + return 0
438 331 fi
439 332  
440   - false
  333 + return 1
441 334 }
442 335  
443   -function _history-substring-search-up-history() {
  336 +_history-substring-search-up-history() {
444 337 #
445 338 # Behave like up in ZSH, except clear the $BUFFER
446 339 # when beginning of history is reached like in Fish.
... ... @@ -453,16 +346,16 @@ function _history-substring-search-up-history() {
453 346  
454 347 # going up from somewhere below the top of history
455 348 else
456   - zle up-history
  349 + zle up-line-or-history
457 350 fi
458 351  
459   - return true
  352 + return 0
460 353 fi
461 354  
462   - false
  355 + return 1
463 356 }
464 357  
465   -function _history-substring-search-down-history() {
  358 +_history-substring-search-down-history() {
466 359 #
467 360 # Behave like down-history in ZSH, except clear the
468 361 # $BUFFER when end of history is reached like in Fish.
... ... @@ -472,21 +365,31 @@ function _history-substring-search-down-history() {
472 365 # going down from the absolute top of history
473 366 if [[ $HISTNO -eq 1 && -z $BUFFER ]]; then
474 367 BUFFER=${history[1]}
475   - _history_substring_search_move_cursor_eol=true
  368 + _history_substring_search_refresh_display=1
476 369  
477 370 # going down from somewhere above the bottom of history
478 371 else
479   - zle down-history
  372 + zle down-line-or-history
480 373 fi
481 374  
482   - return true
  375 + return 0
483 376 fi
484 377  
485   - false
  378 + return 1
  379 +}
  380 +
  381 +_history-substring-search-not-found() {
  382 + #
  383 + # Nothing matched the search query, so put it back into the $BUFFER while
  384 + # highlighting it accordingly so the user can revise it and search again.
  385 + #
  386 + _history_substring_search_old_buffer=$BUFFER
  387 + BUFFER=$_history_substring_search_query
  388 + _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
486 389 }
487 390  
488   -function _history-substring-search-up-search() {
489   - _history_substring_search_move_cursor_eol=true
  391 +_history-substring-search-up-search() {
  392 + _history_substring_search_refresh_display=1
490 393  
491 394 #
492 395 # Highlight matches during history-substring-up-search:
... ... @@ -542,9 +445,7 @@ function _history-substring-search-up-search() {
542 445 # to highlight the current buffer.
543 446 #
544 447 (( _history_substring_search_match_index-- ))
545   - _history_substring_search_old_buffer=$BUFFER
546   - BUFFER=$_history_substring_search_query
547   - _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
  448 + _history-substring-search-not-found
548 449  
549 450 elif [[ $_history_substring_search_match_index -eq $_history_substring_search_matches_count_plus ]]; then
550 451 #
... ... @@ -561,11 +462,30 @@ function _history-substring-search-up-search() {
561 462 (( _history_substring_search_match_index-- ))
562 463 BUFFER=$_history_substring_search_old_buffer
563 464 _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
  465 +
  466 + else
  467 + #
  468 + # We are at the beginning of history and there are no further matches.
  469 + #
  470 + _history-substring-search-not-found
  471 + return
  472 + fi
  473 +
  474 + #
  475 + # When HIST_FIND_NO_DUPS is set, meaning that only unique command lines from
  476 + # history should be matched, make sure the new and old results are different.
  477 + # But when HIST_IGNORE_ALL_DUPS is set, ZSH already ensures a unique history.
  478 + #
  479 + if [[ ! -o HIST_IGNORE_ALL_DUPS && -o HIST_FIND_NO_DUPS && $BUFFER == $_history_substring_search_result ]]; then
  480 + #
  481 + # Repeat the current search so that a different (unique) match is found.
  482 + #
  483 + _history-substring-search-up-search
564 484 fi
565 485 }
566 486  
567   -function _history-substring-search-down-search() {
568   - _history_substring_search_move_cursor_eol=true
  487 +_history-substring-search-down-search() {
  488 + _history_substring_search_refresh_display=1
569 489  
570 490 #
571 491 # Highlight matches during history-substring-up-search:
... ... @@ -622,9 +542,7 @@ function _history-substring-search-down-search() {
622 542 # to highlight the current buffer.
623 543 #
624 544 (( _history_substring_search_match_index++ ))
625   - _history_substring_search_old_buffer=$BUFFER
626   - BUFFER=$_history_substring_search_query
627   - _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
  545 + _history-substring-search-not-found
628 546  
629 547 elif [[ $_history_substring_search_match_index -eq 0 ]]; then
630 548 #
... ... @@ -641,6 +559,25 @@ function _history-substring-search-down-search() {
641 559 (( _history_substring_search_match_index++ ))
642 560 BUFFER=$_history_substring_search_old_buffer
643 561 _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
  562 +
  563 + else
  564 + #
  565 + # We are at the end of history and there are no further matches.
  566 + #
  567 + _history-substring-search-not-found
  568 + return
  569 + fi
  570 +
  571 + #
  572 + # When HIST_FIND_NO_DUPS is set, meaning that only unique command lines from
  573 + # history should be matched, make sure the new and old results are different.
  574 + # But when HIST_IGNORE_ALL_DUPS is set, ZSH already ensures a unique history.
  575 + #
  576 + if [[ ! -o HIST_IGNORE_ALL_DUPS && -o HIST_FIND_NO_DUPS && $BUFFER == $_history_substring_search_result ]]; then
  577 + #
  578 + # Repeat the current search so that a different (unique) match is found.
  579 + #
  580 + _history-substring-search-down-search
644 581 fi
645 582 }
646 583  
plugins/history-substring-search/update-from-upstream.zsh
... ... @@ -0,0 +1,127 @@
  1 +#!/usr/bin/env zsh
  2 +#
  3 +# update-from-upstream.zsh
  4 +#
  5 +# This script updates the Oh My Zsh version of the zsh-history-substring-search
  6 +# plugin from the independent upstream repo. This is to be run by OMZ developers
  7 +# when they want to pull in new changes from upstream to OMZ. It is not run
  8 +# during normal use of the plugin.
  9 +#
  10 +# The official upstream repo is zsh-users/zsh-history-substring-search
  11 +# https://github.com/zsh-users/zsh-history-substring-search
  12 +#
  13 +# This is a zsh script, not a function. Call it with `zsh update-from-upstream.zsh`
  14 +# from the command line, running it from within the plugin directory.
  15 +#
  16 +# You can set the environment variable REPO_PATH to point it at an upstream
  17 +# repo you have already prepared. Otherwise, it will do a clean checkout of
  18 +# upstream's HEAD to a temporary local repo and use that.
  19 +
  20 +
  21 +# Just bail on any error so we don't have to do extra checking.
  22 +# This is a developer-use script, so terse output like that should
  23 +# be fine.
  24 +set -e
  25 +
  26 +
  27 +upstream_basename=zsh-history-substring-search
  28 +plugin_basename=history-substring-search
  29 +UPSTREAM_REPO=zsh-users/$upstream_basename
  30 +need_repo_cleanup=false
  31 +upstream_github_url="https://github.com/$UPSTREAM_REPO"
  32 +
  33 +if [[ -z "$UPSTREAM_REPO_PATH" ]]; then
  34 + # Do a clean checkout
  35 + my_tempdir=$(mktemp -d -t omz-update-histsubstrsrch)
  36 + UPSTREAM_REPO_PATH="$my_tempdir/$upstream_basename"
  37 + git clone "$upstream_github_url" "$UPSTREAM_REPO_PATH"
  38 + need_repo_cleanup=true
  39 + print "Checked out upstream repo to $UPSTREAM_REPO_PATH"
  40 +else
  41 + print "Using existing $upstream_basename repo at $UPSTREAM_REPO_PATH"
  42 +fi
  43 +
  44 +upstream="$UPSTREAM_REPO_PATH"
  45 +
  46 +# Figure out what we're pulling in
  47 +upstream_sha=$(cd $upstream && git rev-parse HEAD)
  48 +upstream_commit_date=$(cd $upstream && git log -1 --pretty=format:%ci)
  49 +upstream_just_date=${${=upstream_commit_date}[1]}
  50 +print "upstream SHA: $upstream_sha"
  51 +print "upstream commit time: $upstream_commit_date"
  52 +print "upstream commit date: $upstream_just_date"
  53 +print
  54 +
  55 +# Copy the files over, using the OMZ plugin's names where needed
  56 +cp -v "$upstream"/* .
  57 +mv -v zsh-history-substring-search.zsh $plugin_basename.zsh
  58 +mv -v zsh-history-substring-search.plugin.zsh $plugin_basename.plugin.zsh
  59 +
  60 +if [[ $need_repo_cleanup == true ]]; then
  61 + print "Removing temporary repo at $my_tempdir"
  62 + rm -rf "$my_tempdir"
  63 +fi
  64 +
  65 +# Do OMZ-specific edits
  66 +
  67 +print
  68 +print "Updating files with OMZ-specific stuff"
  69 +print
  70 +
  71 +# OMZ binds the keys as part of the plugin loading
  72 +
  73 +cat >> $plugin_basename.plugin.zsh <<EOF
  74 +
  75 +
  76 +# Bind terminal-specific up and down keys
  77 +
  78 +if [[ -n "\$terminfo[kcuu1]" ]]; then
  79 + bindkey "\$terminfo[kcuu1]" history-substring-search-up
  80 +fi
  81 +if [[ -n "\$terminfo[kcud1]" ]]; then
  82 + bindkey "\$terminfo[kcud1]" history-substring-search-down
  83 +fi
  84 +
  85 +EOF
  86 +
  87 +# Tack OMZ-specific notes on to readme
  88 +
  89 +thin_line="------------------------------------------------------------------------------"
  90 +cat >> README.md <<EOF
  91 +
  92 +$thin_line
  93 +Oh My Zsh Distribution Notes
  94 +$thin_line
  95 +
  96 +What you are looking at now is Oh My Zsh's repackaging of zsh-history-substring-search
  97 +as an OMZ module inside the Oh My Zsh distribution.
  98 +
  99 +The upstream repo, $UPSTREAM_REPO, can be found on GitHub at
  100 +$upstream_github_url.
  101 +
  102 +This downstream copy was last updated from the following upstream commit:
  103 +
  104 + SHA: $upstream_sha
  105 + Commit date: $upstream_commit_date
  106 +
  107 +Everything above this section is a copy of the original upstream's README, so things
  108 +may differ slightly when you're using this inside OMZ. In particular, you do not
  109 +need to set up key bindings for the up and down arrows yourself in \`~/.zshrc\`; the OMZ
  110 +plugin does that for you. You may still want to set up additional emacs- or vi-specific
  111 +bindings as mentioned above.
  112 +
  113 +EOF
  114 +
  115 +# Announce success and generate git commit messages
  116 +
  117 +cat <<EOF
  118 +Done OK
  119 +
  120 +Now you can check the results and commit like this:
  121 +
  122 + git add *
  123 + git commit -m "history-substring-search: update to upstream version $upstream_just_date" \\
  124 + -m "Updates OMZ's copy to commit $upstream_sha from $UPSTREAM_REPO"
  125 +
  126 +EOF
  127 +
plugins/tmux-cssh/_tmux-cssh
... ... @@ -0,0 +1,25 @@
  1 +#compdef tmux-cssh
  2 +
  3 +# tmux-cssh autocompletion for oh-my-zsh
  4 +# Requires: tmux-cssh installed
  5 +# Author: Manfred Touron (@moul)
  6 +
  7 +_arguments \
  8 +'(-h --help)'{-h,--help}'[This help.]' \
  9 +'(-u --user)'{-u,--user}'[User to use.]' \
  10 +'(-c --certificate)'{-c,--certificate}'[Path to ssh-certificate to use.]' \
  11 +'(-sc --ssh)'{-sc,--ssh}'[SSH-connection-string, multiple.]' \
  12 +'(-sa --ssh)'{-sa,--ssh}'[SSH connection arguments, used on every session.]' \
  13 +'(-ts --tmux)'{-ts,--tmux}'[Alternative tmux-session-name, default: tmux-cssh]' \
  14 +'(-ns --new)'{-ns,--new}'[Initializes a new session, like -ts \[name\].]' \
  15 +'(-q --quiet)'{-q,--quiet}'[Quiet-mode.]' \
  16 +'(-f --filename)'{-f,--filename}'[Filename of textfile to get -sc connection-strings from, line separated.]' \
  17 +'(-cs --config)'{-cs,--config}'[Name of config-settings which should be get from config-file "$HOME/.tmux-cssh". Which can be a grep-regular expression to find the name(s).]' \
  18 + ':hosts:_hosts' \
  19 + '*:: :->subcmds' \
  20 + && return 0
  21 +
  22 +if (( CURRENT == 1 )); then
  23 + _describe -t commands "tmux-cssh command"
  24 + return
  25 +fi
plugins/vi-mode/vi-mode.plugin.zsh
1   -# Ensures that $terminfo values are valid and updates editor information when
2   -# the keymap changes.
3   -function zle-keymap-select zle-line-init zle-line-finish {
4   - # The terminal must be in application mode when ZLE is active for $terminfo
5   - # values to be valid.
6   - if (( ${+terminfo[smkx]} )); then
7   - printf '%s' ${terminfo[smkx]}
8   - fi
9   - if (( ${+terminfo[rmkx]} )); then
10   - printf '%s' ${terminfo[rmkx]}
11   - fi
12   -
  1 +# Updates editor information when the keymap changes.
  2 +function zle-keymap-select() {
13 3 zle reset-prompt
14 4 zle -R
15 5 }
... ... @@ -19,8 +9,6 @@ TRAPWINCH() {
19 9 zle && { zle reset-prompt; zle -R }
20 10 }
21 11  
22   -zle -N zle-line-init
23   -zle -N zle-line-finish
24 12 zle -N zle-keymap-select
25 13 zle -N edit-command-line
26 14  
... ... @@ -8,7 +8,7 @@
8 8 # @github.com/mfaerevaag/wd
9 9  
10 10 # version
11   -readonly WD_VERSION=0.4
  11 +readonly WD_VERSION=0.4.2
12 12  
13 13 # colors
14 14 readonly WD_BLUE="\033[96m"
... ... @@ -143,7 +143,7 @@ wd_warp()
143 143 fi
144 144 elif [[ ${points[$point]} != "" ]]
145 145 then
146   - cd ${points[$point]}
  146 + cd ${points[$point]/#\~/$HOME}
147 147 else
148 148 wd_exit_fail "Unknown warp point '${point}'"
149 149 fi
... ... @@ -169,7 +169,7 @@ wd_add()
169 169 elif [[ ${points[$2]} == "" ]] || $force
170 170 then
171 171 wd_remove $point > /dev/null
172   - printf "%q:%s\n" "${point}" "${PWD}" >> $WD_CONFIG
  172 + printf "%q:%s\n" "${point}" "${PWD/#$HOME/~}" >> $WD_CONFIG
173 173  
174 174 wd_print_msg $WD_GREEN "Warp point added"
175 175  
... ... @@ -203,6 +203,21 @@ wd_list_all()
203 203 {
204 204 wd_print_msg $WD_BLUE "All warp points:"
205 205  
  206 + entries=$(sed "s:${HOME}:~:g" $WD_CONFIG)
  207 +
  208 + max_warp_point_length=0
  209 + while IFS= read -r line
  210 + do
  211 + arr=(${(s,:,)line})
  212 + key=${arr[1]}
  213 +
  214 + length=${#key}
  215 + if [[ length -gt max_warp_point_length ]]
  216 + then
  217 + max_warp_point_length=$length
  218 + fi
  219 + done <<< $entries
  220 +
206 221 while IFS= read -r line
207 222 do
208 223 if [[ $line != "" ]]
... ... @@ -213,16 +228,16 @@ wd_list_all()
213 228  
214 229 if [[ -z $wd_quiet_mode ]]
215 230 then
216   - printf "%20s -> %s\n" $key $val
  231 + printf "%${max_warp_point_length}s -> %s\n" $key $val
217 232 fi
218 233 fi
219   - done <<< $(sed "s:${HOME}:~:g" $WD_CONFIG)
  234 + done <<< $entries
220 235 }
221 236  
222 237 wd_ls()
223 238 {
224 239 wd_getdir $1
225   - ls $dir
  240 + ls ${dir/#\~/$HOME}
226 241 }
227 242  
228 243 wd_path()
... ... @@ -248,6 +263,7 @@ wd_show()
248 263 local wd_matches
249 264 wd_matches=()
250 265 # do a reverse lookup to check whether PWD is in $points
  266 + PWD="${PWD/$HOME/~}"
251 267 if [[ ${points[(r)$PWD]} == $PWD ]]
252 268 then
253 269 for name in ${(k)points}
... ... @@ -6,7 +6,7 @@ NAME
6 6 z - jump around
7 7  
8 8 SYNOPSIS
9   - z [-chlrt] [regex1 regex2 ... regexn]
  9 + z [-chlrtx] [regex1 regex2 ... regexn]
10 10  
11 11 AVAILABILITY
12 12 bash, zsh
... ... @@ -15,10 +15,13 @@ DESCRIPTION
15 15 Tracks your most used directories, based on 'frecency'.
16 16  
17 17 After a short learning phase, z will take you to the most 'frecent'
18   - directory that matches ALL of the regexes given on the command line.
  18 + directory that matches ALL of the regexes given on the command line, in
  19 + order.
  20 +
  21 + For example, z foo bar would match /foo/bar but not /bar/foo.
19 22  
20 23 OPTIONS
21   - -c restrict matches to subdirectories of the current directory.
  24 + -c restrict matches to subdirectories of the current directory
22 25  
23 26 -h show a brief help message
24 27  
... ... @@ -28,10 +31,12 @@ OPTIONS
28 31  
29 32 -t match by recent access only
30 33  
  34 + -x remove the current directory from the datafile
  35 +
31 36 EXAMPLES
32 37 z foo cd to most frecent dir matching foo
33 38  
34   - z foo bar cd to most frecent dir matching foo and bar
  39 + z foo bar cd to most frecent dir matching foo, then bar
35 40  
36 41 z -r foo cd to highest ranked dir matching foo
37 42  
... ... @@ -55,8 +60,9 @@ NOTES
55 60 Set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
56 61 Set $_Z_NO_PROMPT_COMMAND to handle PROMPT_COMMAND/precmd your-
57 62 self.
58   - Set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
59   - (These settings should go in .bashrc/.zshrc before the lines
  63 + Set $_Z_EXCLUDE_DIRS to an array of directory trees to exclude.
  64 + Set $_Z_OWNER to allow usage when in 'sudo -s' mode.
  65 + (These settings should go in .bashrc/.zshrc before the line
60 66 added above.)
61 67 Install the provided man page z.1 somewhere like
62 68 /usr/local/man/man1.
... ... @@ -64,12 +70,12 @@ NOTES
64 70 Aging:
65 71 The rank of directories maintained by z undergoes aging based on a sim-
66 72 ple formula. The rank of each entry is incremented every time it is
67   - accessed. When the sum of ranks is greater than 6000, all ranks are
68   - multiplied by 0.99. Entries with a rank lower than 1 are forgotten.
  73 + accessed. When the sum of ranks is over 9000, all ranks are multiplied
  74 + by 0.99. Entries with a rank lower than 1 are forgotten.
69 75  
70 76 Frecency:
71   - Frecency is a portmantaeu of 'recent' and 'frequency'. It is a weighted
72   - rank that depends on how often and how recently something occured. As
  77 + Frecency is a portmanteau of 'recent' and 'frequency'. It is a weighted
  78 + rank that depends on how often and how recently something occurred. As
73 79 far as I know, Mozilla came up with the term.
74 80  
75 81 To z, a directory that has low ranking but has been accessed recently
... ... @@ -107,20 +113,23 @@ ENVIRONMENT
107 113 resolving of symlinks. If it is not set, symbolic links will be
108 114 resolved when added to the datafile.
109 115  
110   - In bash, z prepends a command to the PROMPT_COMMAND environment vari-
111   - able to maintain its database. In zsh, z appends a function _z_precmd
112   - to the precmd_functions array.
  116 + In bash, z appends a command to the PROMPT_COMMAND environment variable
  117 + to maintain its database. In zsh, z appends a function _z_precmd to the
  118 + precmd_functions array.
113 119  
114 120 The environment variable $_Z_NO_PROMPT_COMMAND can be set if you want
115 121 to handle PROMPT_COMMAND or precmd yourself.
116 122  
117 123 The environment variable $_Z_EXCLUDE_DIRS can be set to an array of
118   - directories to exclude from tracking. $HOME is always excluded. Direc-
119   - tories must be full paths without trailing slashes.
  124 + directory trees to exclude from tracking. $HOME is always excluded.
  125 + Directories must be full paths without trailing slashes.
  126 +
  127 + The environment variable $_Z_OWNER can be set to your username, to
  128 + allow usage of z when your sudo enviroment keeps $HOME set.
120 129  
121 130 FILES
122   - Data is stored in $HOME/.z. This can be overridden by setting the
123   - $_Z_DATA environment variable. When initialized, z will raise an error
  131 + Data is stored in $HOME/.z. This can be overridden by setting the
  132 + $_Z_DATA environment variable. When initialized, z will raise an error
124 133 if this path is a directory, and not function correctly.
125 134  
126 135 A man page (z.1) is provided.
... ... @@ -4,7 +4,7 @@ NAME
4 4 z \- jump around
5 5 .SH
6 6 SYNOPSIS
7   -z [\-chlrt] [regex1 regex2 ... regexn]
  7 +z [\-chlrtx] [regex1 regex2 ... regexn]
8 8 .SH
9 9 AVAILABILITY
10 10 bash, zsh
... ... @@ -13,12 +13,14 @@ DESCRIPTION
13 13 Tracks your most used directories, based on 'frecency'.
14 14 .P
15 15 After a short learning phase, \fBz\fR will take you to the most 'frecent'
16   -directory that matches ALL of the regexes given on the command line.
  16 +directory that matches ALL of the regexes given on the command line, in order.
  17 +
  18 +For example, \fBz foo bar\fR would match \fB/foo/bar\fR but not \fB/bar/foo\fR.
17 19 .SH
18 20 OPTIONS
19 21 .TP
20 22 \fB\-c\fR
21   -restrict matches to subdirectories of the current directory.
  23 +restrict matches to subdirectories of the current directory
22 24 .TP
23 25 \fB\-h\fR
24 26 show a brief help message
... ... @@ -31,13 +33,16 @@ match by rank only
31 33 .TP
32 34 \fB\-t\fR
33 35 match by recent access only
  36 +.TP
  37 +\fB\-x\fR
  38 +remove the current directory from the datafile
34 39 .SH EXAMPLES
35 40 .TP 14
36 41 \fBz foo\fR
37 42 cd to most frecent dir matching foo
38 43 .TP 14
39 44 \fBz foo bar\fR
40   -cd to most frecent dir matching foo and bar
  45 +cd to most frecent dir matching foo, then bar
41 46 .TP 14
42 47 \fBz -r foo\fR
43 48 cd to highest ranked dir matching foo
... ... @@ -76,10 +81,13 @@ Set \fB$_Z_NO_RESOLVE_SYMLINKS\fR to prevent symlink resolution.
76 81 Set \fB$_Z_NO_PROMPT_COMMAND\fR to handle \fBPROMPT_COMMAND/precmd\fR yourself.
77 82 .RE
78 83 .RS
79   -Set \fB$_Z_EXCLUDE_DIRS\fR to an array of directories to exclude.
  84 +Set \fB$_Z_EXCLUDE_DIRS\fR to an array of directory trees to exclude.
  85 +.RE
  86 +.RS
  87 +Set \fB$_Z_OWNER\fR to allow usage when in 'sudo -s' mode.
80 88 .RE
81 89 .RS
82   -(These settings should go in .bashrc/.zshrc before the lines added above.)
  90 +(These settings should go in .bashrc/.zshrc before the line added above.)
83 91 .RE
84 92 .RS
85 93 Install the provided man page \fBz.1\fR somewhere like \fB/usr/local/man/man1\fR.
... ... @@ -88,12 +96,12 @@ Install the provided man page \fBz.1\fR somewhere like \fB/usr/local/man/man1\fR
88 96 Aging:
89 97 The rank of directories maintained by \fBz\fR undergoes aging based on a simple
90 98 formula. The rank of each entry is incremented every time it is accessed. When
91   -the sum of ranks is greater than 6000, all ranks are multiplied by 0.99. Entries
92   -with a rank lower than 1 are forgotten.
  99 +the sum of ranks is over 9000, all ranks are multiplied by 0.99. Entries with a
  100 +rank lower than 1 are forgotten.
93 101 .SS
94 102 Frecency:
95   -Frecency is a portmantaeu of 'recent' and 'frequency'. It is a weighted rank
96   -that depends on how often and how recently something occured. As far as I
  103 +Frecency is a portmanteau of 'recent' and 'frequency'. It is a weighted rank
  104 +that depends on how often and how recently something occurred. As far as I
97 105 know, Mozilla came up with the term.
98 106 .P
99 107 To \fBz\fR, a directory that has low ranking but has been accessed recently
... ... @@ -131,7 +139,7 @@ The environment variable \fB$_Z_NO_RESOLVE_SYMLINKS\fR can be set to prevent
131 139 resolving of symlinks. If it is not set, symbolic links will be resolved when
132 140 added to the datafile.
133 141 .P
134   -In bash, \fBz\fR prepends a command to the \fBPROMPT_COMMAND\fR environment
  142 +In bash, \fBz\fR appends a command to the \fBPROMPT_COMMAND\fR environment
135 143 variable to maintain its database. In zsh, \fBz\fR appends a function
136 144 \fB_z_precmd\fR to the \fBprecmd_functions\fR array.
137 145 .P
... ... @@ -139,8 +147,11 @@ The environment variable \fB$_Z_NO_PROMPT_COMMAND\fR can be set if you want to
139 147 handle \fBPROMPT_COMMAND\fR or \fBprecmd\fR yourself.
140 148 .P
141 149 The environment variable \fB$_Z_EXCLUDE_DIRS\fR can be set to an array of
142   -directories to exclude from tracking. \fB$HOME\fR is always excluded.
  150 +directory trees to exclude from tracking. \fB$HOME\fR is always excluded.
143 151 Directories must be full paths without trailing slashes.
  152 +.P
  153 +The environment variable \fB$_Z_OWNER\fR can be set to your username, to
  154 +allow usage of \fBz\fR when your sudo enviroment keeps \fB$HOME\fR set.
144 155 .SH
145 156 FILES
146 157 Data is stored in \fB$HOME/.z\fR. This can be overridden by setting the
plugins/z/z.plugin.zsh
1   -_load_z() {
2   - source $1/z.sh
3   -}
4   -
5   -[[ -f $ZSH_CUSTOM/plugins/z/z.plugin.zsh ]] && _load_z $ZSH_CUSTOM/plugins/z
6   -[[ -f $ZSH/plugins/z/z.plugin.zsh ]] && _load_z $ZSH/plugins/z
  1 +source "${0:h}/z.sh"
... ... @@ -3,29 +3,25 @@
3 3 # maintains a jump-list of the directories you actually use
4 4 #
5 5 # INSTALL:
6   -# * put something like this in your .bashrc/.zshrc:
7   -# . /path/to/z.sh
8   -# * cd around for a while to build up the db
9   -# * PROFIT!!
10   -# * optionally:
11   -# set $_Z_CMD in .bashrc/.zshrc to change the command (default z).
12   -# set $_Z_DATA in .bashrc/.zshrc to change the datafile (default ~/.z).
13   -# set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
14   -# set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself.
15   -# set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
  6 +# * put something like this in your .bashrc/.zshrc:
  7 +# . /path/to/z.sh
  8 +# * cd around for a while to build up the db
  9 +# * PROFIT!!
  10 +# * optionally:
  11 +# set $_Z_CMD in .bashrc/.zshrc to change the command (default z).
  12 +# set $_Z_DATA in .bashrc/.zshrc to change the datafile (default ~/.z).
  13 +# set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
  14 +# set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself.
  15 +# set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
  16 +# set $_Z_OWNER to your username if you want use z while sudo with $HOME kept
16 17 #
17 18 # USE:
18   -# * z foo # cd to most frecent dir matching foo
19   -# * z foo bar # cd to most frecent dir matching foo and bar
20   -# * z -r foo # cd to highest ranked dir matching foo
21   -# * z -t foo # cd to most recently accessed dir matching foo
22   -# * z -l foo # list matches instead of cd
23   -# * z -c foo # restrict matches to subdirs of $PWD
24   -
25   -case $- in
26   - *i*) ;;
27   - *) echo 'ERROR: z.sh is meant to be sourced, not directly executed.'
28   -esac
  19 +# * z foo # cd to most frecent dir matching foo
  20 +# * z foo bar # cd to most frecent dir matching foo and bar
  21 +# * z -r foo # cd to highest ranked dir matching foo
  22 +# * z -t foo # cd to most recently accessed dir matching foo
  23 +# * z -l foo # list matches instead of cd
  24 +# * z -c foo # restrict matches to subdirs of $PWD
29 25  
30 26 [ -d "${_Z_DATA:-$HOME/.z}" ] && {
31 27 echo "ERROR: z.sh's datafile (${_Z_DATA:-$HOME/.z}) is a directory."
... ... @@ -33,196 +29,215 @@ esac
33 29  
34 30 _z() {
35 31  
36   - local datafile="${_Z_DATA:-$HOME/.z}"
37   -
38   - # bail out if we don't own ~/.z (we're another user but our ENV is still set)
39   - [ -f "$datafile" -a ! -O "$datafile" ] && return
40   -
41   - # add entries
42   - if [ "$1" = "--add" ]; then
43   - shift
44   -
45   - # $HOME isn't worth matching
46   - [ "$*" = "$HOME" ] && return
47   -
48   - # don't track excluded dirs
49   - local exclude
50   - for exclude in "${_Z_EXCLUDE_DIRS[@]}"; do
51   - [ "$*" = "$exclude" ] && return
52   - done
53   -
54   - # maintain the file
55   - local tempfile
56   - tempfile="$(mktemp "$datafile.XXXXXX")" || return
57   - while read line; do
58   - [ -d "${line%%\|*}" ] && echo $line
59   - done < "$datafile" | awk -v path="$*" -v now="$(date +%s)" -F"|" '
60   - BEGIN {
61   - rank[path] = 1
62   - time[path] = now
63   - }
64   - $2 >= 1 {
65   - if( $1 == path ) {
66   - rank[$1] = $2 + 1
67   - time[$1] = now
68   - } else {
69   - rank[$1] = $2
70   - time[$1] = $3
71   - }
72   - count += $2
73   - }
74   - END {
75   - if( count > 6000 ) {
76   - for( i in rank ) print i "|" 0.99*rank[i] "|" time[i] # aging
77   - } else for( i in rank ) print i "|" rank[i] "|" time[i]
78   - }
79   - ' 2>/dev/null >| "$tempfile"
80   - if [ $? -ne 0 -a -f "$datafile" ]; then
81   - env rm -f "$tempfile"
82   - else
83   - env mv -f "$tempfile" "$datafile"
84   - fi
85   -
86   - # tab completion
87   - elif [ "$1" = "--complete" ]; then
88   - while read line; do
89   - [ -d "${line%%\|*}" ] && echo $line
90   - done < "$datafile" | awk -v q="$2" -F"|" '
91   - BEGIN {
92   - if( q == tolower(q) ) nocase = 1
93   - split(substr(q,3),fnd," ")
94   - }
95   - {
96   - if( nocase ) {
97   - for( i in fnd ) tolower($1) !~ tolower(fnd[i]) && $1 = ""
98   - } else {
99   - for( i in fnd ) $1 !~ fnd[i] && $1 = ""
100   - }
101   - if( $1 ) print $1
102   - }
103   - ' 2>/dev/null
104   -
105   - else
106   - # list/go
107   - while [ "$1" ]; do case "$1" in
108   - --) while [ "$1" ]; do shift; local fnd="$fnd $1";done;;
109   - -*) local opt=${1:1}; while [ "$opt" ]; do case ${opt:0:1} in
110   - c) local fnd="^$PWD $fnd";;
111   - h) echo "${_Z_CMD:-z} [-chlrt] args" >&2; return;;
112   - l) local list=1;;
113   - r) local typ="rank";;
114   - t) local typ="recent";;
115   - esac; opt=${opt:1}; done;;
116   - *) local fnd="$fnd $1";;
117   - esac; local last=$1; shift; done
118   - [ "$fnd" -a "$fnd" != "^$PWD " ] || local list=1
119   -
120   - # if we hit enter on a completion just go there
121   - case "$last" in
122   - # completions will always start with /
123   - /*) [ -z "$list" -a -d "$last" ] && cd "$last" && return;;
124   - esac
125   -
126   - # no file yet
127   - [ -f "$datafile" ] || return
128   -
129   - local cd
130   - cd="$(while read line; do
131   - [ -d "${line%%\|*}" ] && echo $line
132   - done < "$datafile" | awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
133   - function frecent(rank, time) {
134   - dx = t-time
135   - if( dx < 3600 ) return rank*4
136   - if( dx < 86400 ) return rank*2
137   - if( dx < 604800 ) return rank/2
138   - return rank/4
139   - }
140   - function output(files, toopen, override) {
141   - if( list ) {
142   - cmd = "sort -n >&2"
143   - for( i in files ) if( files[i] ) printf "%-10s %s\n", files[i], i | cmd
144   - if( override ) printf "%-10s %s\n", "common:", override > "/dev/stderr"
145   - } else {
146   - if( override ) toopen = override
147   - print toopen
148   - }
149   - }
150   - function common(matches) {
151   - # shortest match
152   - for( i in matches ) {
153   - if( matches[i] && (!short || length(i) < length(short)) ) short = i
154   - }
155   - if( short == "/" ) return
156   - # shortest match must be common to each match. escape special characters in
157   - # a copy when testing, so we can return the original.
158   - clean_short = short
159   - gsub(/[\(\)\[\]\|]/, "\\\\&", clean_short)
160   - for( i in matches ) if( matches[i] && i !~ clean_short ) return
161   - return short
162   - }
163   - BEGIN { split(q, a, " "); oldf = noldf = -9999999999 }
164   - {
165   - if( typ == "rank" ) {
166   - f = $2
167   - } else if( typ == "recent" ) {
168   - f = $3-t
169   - } else f = frecent($2, $3)
170   - wcase[$1] = nocase[$1] = f
171   - for( i in a ) {
172   - if( $1 !~ a[i] ) delete wcase[$1]
173   - if( tolower($1) !~ tolower(a[i]) ) delete nocase[$1]
174   - }
175   - if( wcase[$1] && wcase[$1] > oldf ) {
176   - cx = $1
177   - oldf = wcase[$1]
178   - } else if( nocase[$1] && nocase[$1] > noldf ) {
179   - ncx = $1
180   - noldf = nocase[$1]
181   - }
182   - }
183   - END {
184   - if( cx ) {
185   - output(wcase, cx, common(wcase))
186   - } else if( ncx ) output(nocase, ncx, common(nocase))
187   - }
188   - ')"
189   - [ $? -gt 0 ] && return
190   - [ "$cd" ] && cd "$cd"
191   - fi
  32 + local datafile="${_Z_DATA:-$HOME/.z}"
  33 +
  34 + # bail if we don't own ~/.z and $_Z_OWNER not set
  35 + [ -z "$_Z_OWNER" -a -f "$datafile" -a ! -O "$datafile" ] && return
  36 +
  37 + # add entries
  38 + if [ "$1" = "--add" ]; then
  39 + shift
  40 +
  41 + # $HOME isn't worth matching
  42 + [ "$*" = "$HOME" ] && return
  43 +
  44 + # don't track excluded directory trees
  45 + local exclude
  46 + for exclude in "${_Z_EXCLUDE_DIRS[@]}"; do
  47 + case "$*" in "$exclude*") return;; esac
  48 + done
  49 +
  50 + # maintain the data file
  51 + local tempfile="$datafile.$RANDOM"
  52 + while read line; do
  53 + # only count directories
  54 + [ -d "${line%%\|*}" ] && echo $line
  55 + done < "$datafile" | awk -v path="$*" -v now="$(date +%s)" -F"|" '
  56 + BEGIN {
  57 + rank[path] = 1
  58 + time[path] = now
  59 + }
  60 + $2 >= 1 {
  61 + # drop ranks below 1
  62 + if( $1 == path ) {
  63 + rank[$1] = $2 + 1
  64 + time[$1] = now
  65 + } else {
  66 + rank[$1] = $2
  67 + time[$1] = $3
  68 + }
  69 + count += $2
  70 + }
  71 + END {
  72 + if( count > 9000 ) {
  73 + # aging
  74 + for( x in rank ) print x "|" 0.99*rank[x] "|" time[x]
  75 + } else for( x in rank ) print x "|" rank[x] "|" time[x]
  76 + }
  77 + ' 2>/dev/null >| "$tempfile"
  78 + # do our best to avoid clobbering the datafile in a race condition
  79 + if [ $? -ne 0 -a -f "$datafile" ]; then
  80 + env rm -f "$tempfile"
  81 + else
  82 + [ "$_Z_OWNER" ] && chown $_Z_OWNER:$(id -ng $_Z_OWNER) "$tempfile"
  83 + env mv -f "$tempfile" "$datafile" || env rm -f "$tempfile"
  84 + fi
  85 +
  86 + # tab completion
  87 + elif [ "$1" = "--complete" -a -s "$datafile" ]; then
  88 + while read line; do
  89 + [ -d "${line%%\|*}" ] && echo $line
  90 + done < "$datafile" | awk -v q="$2" -F"|" '
  91 + BEGIN {
  92 + if( q == tolower(q) ) imatch = 1
  93 + q = substr(q, 3)
  94 + gsub(" ", ".*", q)
  95 + }
  96 + {
  97 + if( imatch ) {
  98 + if( tolower($1) ~ tolower(q) ) print $1
  99 + } else if( $1 ~ q ) print $1
  100 + }
  101 + ' 2>/dev/null
  102 +
  103 + else
  104 + # list/go
  105 + while [ "$1" ]; do case "$1" in
  106 + --) while [ "$1" ]; do shift; local fnd="$fnd${fnd:+ }$1";done;;
  107 + -*) local opt=${1:1}; while [ "$opt" ]; do case ${opt:0:1} in
  108 + c) local fnd="^$PWD $fnd";;
  109 + h) echo "${_Z_CMD:-z} [-chlrtx] args" >&2; return;;
  110 + x) sed -i -e "\:^${PWD}|.*:d" "$datafile";;
  111 + l) local list=1;;
  112 + r) local typ="rank";;
  113 + t) local typ="recent";;
  114 + esac; opt=${opt:1}; done;;
  115 + *) local fnd="$fnd${fnd:+ }$1";;
  116 + esac; local last=$1; [ "$#" -gt 0 ] && shift; done
  117 + [ "$fnd" -a "$fnd" != "^$PWD " ] || local list=1
  118 +
  119 + # if we hit enter on a completion just go there
  120 + case "$last" in
  121 + # completions will always start with /
  122 + /*) [ -z "$list" -a -d "$last" ] && cd "$last" && return;;
  123 + esac
  124 +
  125 + # no file yet
  126 + [ -f "$datafile" ] || return
  127 +
  128 + local cd
  129 + cd="$(while read line; do
  130 + [ -d "${line%%\|*}" ] && echo $line
  131 + done < "$datafile" | awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
  132 + function frecent(rank, time) {
  133 + # relate frequency and time
  134 + dx = t - time
  135 + if( dx < 3600 ) return rank * 4
  136 + if( dx < 86400 ) return rank * 2
  137 + if( dx < 604800 ) return rank / 2
  138 + return rank / 4
  139 + }
  140 + function output(files, out, common) {
  141 + # list or return the desired directory
  142 + if( list ) {
  143 + cmd = "sort -n >&2"
  144 + for( x in files ) {
  145 + if( files[x] ) printf "%-10s %s\n", files[x], x | cmd
  146 + }
  147 + if( common ) {
  148 + printf "%-10s %s\n", "common:", common > "/dev/stderr"
  149 + }
  150 + } else {
  151 + if( common ) out = common
  152 + print out
  153 + }
  154 + }
  155 + function common(matches) {
  156 + # find the common root of a list of matches, if it exists
  157 + for( x in matches ) {
  158 + if( matches[x] && (!short || length(x) < length(short)) ) {
  159 + short = x
  160 + }
  161 + }
  162 + if( short == "/" ) return
  163 + # use a copy to escape special characters, as we want to return
  164 + # the original. yeah, this escaping is awful.
  165 + clean_short = short
  166 + gsub(/\[\(\)\[\]\|\]/, "\\\\&", clean_short)
  167 + for( x in matches ) if( matches[x] && x !~ clean_short ) return
  168 + return short
  169 + }
  170 + BEGIN {
  171 + gsub(" ", ".*", q)
  172 + hi_rank = ihi_rank = -9999999999
  173 + }
  174 + {
  175 + if( typ == "rank" ) {
  176 + rank = $2
  177 + } else if( typ == "recent" ) {
  178 + rank = $3 - t
  179 + } else rank = frecent($2, $3)
  180 + if( $1 ~ q ) {
  181 + matches[$1] = rank
  182 + } else if( tolower($1) ~ tolower(q) ) imatches[$1] = rank
  183 + if( matches[$1] && matches[$1] > hi_rank ) {
  184 + best_match = $1
  185 + hi_rank = matches[$1]
  186 + } else if( imatches[$1] && imatches[$1] > ihi_rank ) {
  187 + ibest_match = $1
  188 + ihi_rank = imatches[$1]
  189 + }
  190 + }
  191 + END {
  192 + # prefer case sensitive
  193 + if( best_match ) {
  194 + output(matches, best_match, common(matches))
  195 + } else if( ibest_match ) {
  196 + output(imatches, ibest_match, common(imatches))
  197 + }
  198 + }
  199 + ')"
  200 + [ $? -gt 0 ] && return
  201 + [ "$cd" ] && cd "$cd"
  202 + fi
192 203 }
193 204  
194 205 alias ${_Z_CMD:-z}='_z 2>&1'
195 206  
196 207 [ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P"
197 208  
198   -if compctl &> /dev/null; then
199   - [ "$_Z_NO_PROMPT_COMMAND" ] || {
200   - # zsh populate directory list, avoid clobbering any other precmds
201   - if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then
202   - _z_precmd() {
203   - _z --add "${PWD:a}"
  209 +if type compctl >/dev/null 2>&1; then
  210 + # zsh
  211 + [ "$_Z_NO_PROMPT_COMMAND" ] || {
  212 + # populate directory list, avoid clobbering any other precmds.
  213 + if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then
  214 + _z_precmd() {
  215 + _z --add "${PWD:a}"
  216 + }
  217 + else
  218 + _z_precmd() {
  219 + _z --add "${PWD:A}"
  220 + }
  221 + fi
  222 + [[ -n "${precmd_functions[(r)_z_precmd]}" ]] || {
  223 + precmd_functions[$(($#precmd_functions+1))]=_z_precmd
  224 + }
  225 + }
  226 + _z_zsh_tab_completion() {
  227 + # tab completion
  228 + local compl
  229 + read -l compl
  230 + reply=(${(f)"$(_z --complete "$compl")"})
204 231 }
205   - else
206   - _z_precmd() {
207   - _z --add "${PWD:A}"
  232 + compctl -U -K _z_zsh_tab_completion _z
  233 +elif type complete >/dev/null 2>&1; then
  234 + # bash
  235 + # tab completion
  236 + complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z}
  237 + [ "$_Z_NO_PROMPT_COMMAND" ] || {
  238 + # populate directory list. avoid clobbering other PROMPT_COMMANDs.
  239 + grep "_z --add" <<< "$PROMPT_COMMAND" >/dev/null || {
  240 + PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''_z --add "$(command pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null;'
  241 + }
208 242 }
209   - fi
210   - precmd_functions+=(_z_precmd)
211   - }
212   - # zsh tab completion
213   - _z_zsh_tab_completion() {
214   - local compl
215   - read -l compl
216   - reply=(${(f)"$(_z --complete "$compl")"})
217   - }
218   - compctl -U -K _z_zsh_tab_completion _z
219   -elif complete &> /dev/null; then
220   - # bash tab completion
221   - complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z}
222   - [ "$_Z_NO_PROMPT_COMMAND" ] || {
223   - # bash populate directory list. avoid clobbering other PROMPT_COMMANDs.
224   - echo $PROMPT_COMMAND | grep -q "_z --add" || {
225   - PROMPT_COMMAND='_z --add "$(pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null;'"$PROMPT_COMMAND"
226   - }
227   - }
228 243 fi
plugins/zsh-navigation-tools/.config/znt/README.txt
... ... @@ -0,0 +1 @@
  1 +These are skeletons, configuration is read from ~/.config/znt/*
plugins/zsh-navigation-tools/.config/znt/n-aliases.conf
... ... @@ -0,0 +1,5 @@
  1 +# How should be current element of the list drawn. Possible values: reverse,
  2 +# underline. Default (without option set) is reverse
  3 +# On Linux virtual terminal this will be enforced to reverse (because of poor
  4 +# underline support on that terminal)
  5 +# local active_text=underline
plugins/zsh-navigation-tools/.config/znt/n-cd.conf
... ... @@ -0,0 +1,26 @@
  1 +# Hotlist
  2 +local hotlist
  3 +hotlist=(
  4 + ~/.config/znt
  5 + /usr/share/zsh/site-functions
  6 + /usr/share/zsh
  7 + /usr/local/share/zsh/site-functions
  8 + /usr/local/share/zsh
  9 + /usr/local/bin
  10 + /usr/lib
  11 +)
  12 +
  13 +# Suppress adding (to directory stack) directories visited by n-cd
  14 +# Value 0 is the default (directories will be added to dirstack)
  15 +local NCD_DONT_PUSHD=0
  16 +
  17 +# How should be current element of the list drawn. Possible values: reverse,
  18 +# underline. Default (without option set) is reverse
  19 +# On Linux virtual terminal this will be enforced to reverse (because of poor
  20 +# underline support on that terminal)
  21 +# local active_text=underline
  22 +
  23 +# Colorize last segments of the paths
  24 +# (#s) is ^, (#e) is $, # is *, ## is + (comparing to regex)
  25 +local NLIST_COLORING_PATTERN="[a-zA-Z0-9 ._-]##/#(#e)"
  26 +local NLIST_COLORING_COLOR=$'\x1b[00;33m'
plugins/zsh-navigation-tools/.config/znt/n-env.conf
... ... @@ -0,0 +1,9 @@
  1 +# How should be current element of the list drawn. Possible values: reverse,
  2 +# underline. Default (without option set) is reverse
  3 +# On Linux virtual terminal this will be enforced to reverse (because of poor
  4 +# underline support on that terminal)
  5 +# local active_text=underline
  6 +
  7 +# (#s) is ^, (#e) is $, # is *, ## is + (comparing to regex)
  8 +local NLIST_COLORING_PATTERN="[a-zA-Z0-9_]##"
  9 +local NLIST_COLORING_MATCH_MULTIPLE=0
plugins/zsh-navigation-tools/.config/znt/n-functions.conf
... ... @@ -0,0 +1,10 @@
  1 +# Which editor to use, zed or vared
  2 +# vared is the default
  3 +local feditor="zed"
  4 +# local feditor="vared"
  5 +
  6 +# How should be current element of the list drawn. Possible values: reverse,
  7 +# underline. Default (without option set) is reverse
  8 +# On Linux virtual terminal this will be enforced to reverse (because of poor
  9 +# underline support on that terminal)
  10 +# local active_text=underline
plugins/zsh-navigation-tools/.config/znt/n-history.conf
... ... @@ -0,0 +1,10 @@
  1 +# How should be current element of the list drawn. Possible values: reverse,
  2 +# underline. Default (without option set) is reverse
  3 +# On Linux virtual terminal this will be enforced to reverse (because of poor
  4 +# underline support on that terminal)
  5 +local active_text=underline
  6 +
  7 +# Highlight a few keywords
  8 +local NLIST_COLORING_PATTERN="(while|for |sudo|make|(#s)git|vim(#e)|vim |emacs(#e)|emacs )"
  9 +local NLIST_COLORING_COLOR=$'\x1b[00;33m'
  10 +local NLIST_COLORING_MATCH_MULTIPLE=1
plugins/zsh-navigation-tools/.config/znt/n-kill.conf
... ... @@ -0,0 +1,13 @@
  1 +# How should be current element of the list drawn. Possible values: reverse,
  2 +# underline. Default (without option set) is reverse
  3 +# On Linux virtual terminal this will be enforced to reverse (because of poor
  4 +# underline support on that terminal)
  5 +# local active_text=underline
  6 +
  7 +# Colorize first number column and last path segment
  8 +# This doesn't cover scripts named "[0-9]## *", which should be very rare
  9 +# (#s) is ^, (#e) is $, # is *, ## is + (comparing to regex)
  10 +# | is alternative, but only in ()
  11 +local NLIST_COLORING_PATTERN="((#s) #[0-9]## |[[][^]]#](#e)|[^ 0-9/?\\\\][^/\\\\]#(#e)|[^ /\\\\]##[^0-9/\\\\ ]##[^/\\\\]#(#e))"
  12 +local NLIST_COLORING_COLOR=$'\x1b[00;33m'
  13 +local NLIST_COLORING_MATCH_MULTIPLE=1
plugins/zsh-navigation-tools/.config/znt/n-list.conf
... ... @@ -0,0 +1,3 @@
  1 +# Should the list (text, borders) be drawn in bold
  2 +# Value 1 is the default
  3 +local bold=1
plugins/zsh-navigation-tools/.config/znt/n-options.conf
... ... @@ -0,0 +1,5 @@
  1 +# How should be current element of the list drawn. Possible values: reverse,
  2 +# underline. Default (without option set) is reverse
  3 +# On Linux virtual terminal this will be enforced to reverse (because of poor
  4 +# underline support on that terminal)
  5 +# local active_text=underline
plugins/zsh-navigation-tools/.config/znt/n-panelize.conf
... ... @@ -0,0 +1,5 @@
  1 +# How should be current element of the list drawn. Possible values: reverse,
  2 +# underline. Default (without option set) is reverse
  3 +# On Linux virtual terminal this will be enforced to reverse (because of poor
  4 +# underline support on that terminal)
  5 +# local active_text=underline
plugins/zsh-navigation-tools/LICENSE
... ... @@ -0,0 +1,700 @@
  1 +This software is dual-licensed under MIT and GPLv3.
  2 +
  3 +MIT License
  4 +-----------
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy
  7 +of this software and associated documentation files (the "Software"), to deal
  8 +in the Software without restriction, including without limitation the rights
  9 +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 +copies of the Software, and to permit persons to whom the Software is
  11 +furnished to do so, subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in
  14 +all copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19 +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22 +THE SOFTWARE.
  23 +
  24 +GPLv3 License
  25 +--------------
  26 +
  27 + GNU GENERAL PUBLIC LICENSE
  28 + Version 3, 29 June 2007
  29 +
  30 + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
  31 + Everyone is permitted to copy and distribute verbatim copies
  32 + of this license document, but changing it is not allowed.
  33 +
  34 + Preamble
  35 +
  36 + The GNU General Public License is a free, copyleft license for
  37 +software and other kinds of works.
  38 +
  39 + The licenses for most software and other practical works are designed
  40 +to take away your freedom to share and change the works. By contrast,
  41 +the GNU General Public License is intended to guarantee your freedom to
  42 +share and change all versions of a program--to make sure it remains free
  43 +software for all its users. We, the Free Software Foundation, use the
  44 +GNU General Public License for most of our software; it applies also to
  45 +any other work released this way by its authors. You can apply it to
  46 +your programs, too.
  47 +
  48 + When we speak of free software, we are referring to freedom, not
  49 +price. Our General Public Licenses are designed to make sure that you
  50 +have the freedom to distribute copies of free software (and charge for
  51 +them if you wish), that you receive source code or can get it if you
  52 +want it, that you can change the software or use pieces of it in new
  53 +free programs, and that you know you can do these things.
  54 +
  55 + To protect your rights, we need to prevent others from denying you
  56 +these rights or asking you to surrender the rights. Therefore, you have
  57 +certain responsibilities if you distribute copies of the software, or if
  58 +you modify it: responsibilities to respect the freedom of others.
  59 +
  60 + For example, if you distribute copies of such a program, whether
  61 +gratis or for a fee, you must pass on to the recipients the same
  62 +freedoms that you received. You must make sure that they, too, receive
  63 +or can get the source code. And you must show them these terms so they
  64 +know their rights.
  65 +
  66 + Developers that use the GNU GPL protect your rights with two steps:
  67 +(1) assert copyright on the software, and (2) offer you this License
  68 +giving you legal permission to copy, distribute and/or modify it.
  69 +
  70 + For the developers' and authors' protection, the GPL clearly explains
  71 +that there is no warranty for this free software. For both users' and
  72 +authors' sake, the GPL requires that modified versions be marked as
  73 +changed, so that their problems will not be attributed erroneously to
  74 +authors of previous versions.
  75 +
  76 + Some devices are designed to deny users access to install or run
  77 +modified versions of the software inside them, although the manufacturer
  78 +can do so. This is fundamentally incompatible with the aim of
  79 +protecting users' freedom to change the software. The systematic
  80 +pattern of such abuse occurs in the area of products for individuals to
  81 +use, which is precisely where it is most unacceptable. Therefore, we
  82 +have designed this version of the GPL to prohibit the practice for those
  83 +products. If such problems arise substantially in other domains, we
  84 +stand ready to extend this provision to those domains in future versions
  85 +of the GPL, as needed to protect the freedom of users.
  86 +
  87 + Finally, every program is threatened constantly by software patents.
  88 +States should not allow patents to restrict development and use of
  89 +software on general-purpose computers, but in those that do, we wish to
  90 +avoid the special danger that patents applied to a free program could
  91 +make it effectively proprietary. To prevent this, the GPL assures that
  92 +patents cannot be used to render the program non-free.
  93 +
  94 + The precise terms and conditions for copying, distribution and
  95 +modification follow.
  96 +
  97 + TERMS AND CONDITIONS
  98 +
  99 + 0. Definitions.
  100 +
  101 + "This License" refers to version 3 of the GNU General Public License.
  102 +
  103 + "Copyright" also means copyright-like laws that apply to other kinds of
  104 +works, such as semiconductor masks.
  105 +
  106 + "The Program" refers to any copyrightable work licensed under this
  107 +License. Each licensee is addressed as "you". "Licensees" and
  108 +"recipients" may be individuals or organizations.
  109 +
  110 + To "modify" a work means to copy from or adapt all or part of the work
  111 +in a fashion requiring copyright permission, other than the making of an
  112 +exact copy. The resulting work is called a "modified version" of the
  113 +earlier work or a work "based on" the earlier work.
  114 +
  115 + A "covered work" means either the unmodified Program or a work based
  116 +on the Program.
  117 +
  118 + To "propagate" a work means to do anything with it that, without
  119 +permission, would make you directly or secondarily liable for
  120 +infringement under applicable copyright law, except executing it on a
  121 +computer or modifying a private copy. Propagation includes copying,
  122 +distribution (with or without modification), making available to the
  123 +public, and in some countries other activities as well.
  124 +
  125 + To "convey" a work means any kind of propagation that enables other
  126 +parties to make or receive copies. Mere interaction with a user through
  127 +a computer network, with no transfer of a copy, is not conveying.
  128 +
  129 + An interactive user interface displays "Appropriate Legal Notices"
  130 +to the extent that it includes a convenient and prominently visible
  131 +feature that (1) displays an appropriate copyright notice, and (2)
  132 +tells the user that there is no warranty for the work (except to the
  133 +extent that warranties are provided), that licensees may convey the
  134 +work under this License, and how to view a copy of this License. If
  135 +the interface presents a list of user commands or options, such as a
  136 +menu, a prominent item in the list meets this criterion.
  137 +
  138 + 1. Source Code.
  139 +
  140 + The "source code" for a work means the preferred form of the work
  141 +for making modifications to it. "Object code" means any non-source
  142 +form of a work.
  143 +
  144 + A "Standard Interface" means an interface that either is an official
  145 +standard defined by a recognized standards body, or, in the case of
  146 +interfaces specified for a particular programming language, one that
  147 +is widely used among developers working in that language.
  148 +
  149 + The "System Libraries" of an executable work include anything, other
  150 +than the work as a whole, that (a) is included in the normal form of
  151 +packaging a Major Component, but which is not part of that Major
  152 +Component, and (b) serves only to enable use of the work with that
  153 +Major Component, or to implement a Standard Interface for which an
  154 +implementation is available to the public in source code form. A
  155 +"Major Component", in this context, means a major essential component
  156 +(kernel, window system, and so on) of the specific operating system
  157 +(if any) on which the executable work runs, or a compiler used to
  158 +produce the work, or an object code interpreter used to run it.
  159 +
  160 + The "Corresponding Source" for a work in object code form means all
  161 +the source code needed to generate, install, and (for an executable
  162 +work) run the object code and to modify the work, including scripts to
  163 +control those activities. However, it does not include the work's
  164 +System Libraries, or general-purpose tools or generally available free
  165 +programs which are used unmodified in performing those activities but
  166 +which are not part of the work. For example, Corresponding Source
  167 +includes interface definition files associated with source files for
  168 +the work, and the source code for shared libraries and dynamically
  169 +linked subprograms that the work is specifically designed to require,
  170 +such as by intimate data communication or control flow between those
  171 +subprograms and other parts of the work.
  172 +
  173 + The Corresponding Source need not include anything that users
  174 +can regenerate automatically from other parts of the Corresponding
  175 +Source.
  176 +
  177 + The Corresponding Source for a work in source code form is that
  178 +same work.
  179 +
  180 + 2. Basic Permissions.
  181 +
  182 + All rights granted under this License are granted for the term of
  183 +copyright on the Program, and are irrevocable provided the stated
  184 +conditions are met. This License explicitly affirms your unlimited
  185 +permission to run the unmodified Program. The output from running a
  186 +covered work is covered by this License only if the output, given its
  187 +content, constitutes a covered work. This License acknowledges your
  188 +rights of fair use or other equivalent, as provided by copyright law.
  189 +
  190 + You may make, run and propagate covered works that you do not
  191 +convey, without conditions so long as your license otherwise remains
  192 +in force. You may convey covered works to others for the sole purpose
  193 +of having them make modifications exclusively for you, or provide you
  194 +with facilities for running those works, provided that you comply with
  195 +the terms of this License in conveying all material for which you do
  196 +not control copyright. Those thus making or running the covered works
  197 +for you must do so exclusively on your behalf, under your direction
  198 +and control, on terms that prohibit them from making any copies of
  199 +your copyrighted material outside their relationship with you.
  200 +
  201 + Conveying under any other circumstances is permitted solely under
  202 +the conditions stated below. Sublicensing is not allowed; section 10
  203 +makes it unnecessary.
  204 +
  205 + 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
  206 +
  207 + No covered work shall be deemed part of an effective technological
  208 +measure under any applicable law fulfilling obligations under article
  209 +11 of the WIPO copyright treaty adopted on 20 December 1996, or
  210 +similar laws prohibiting or restricting circumvention of such
  211 +measures.
  212 +
  213 + When you convey a covered work, you waive any legal power to forbid
  214 +circumvention of technological measures to the extent such circumvention
  215 +is effected by exercising rights under this License with respect to
  216 +the covered work, and you disclaim any intention to limit operation or
  217 +modification of the work as a means of enforcing, against the work's
  218 +users, your or third parties' legal rights to forbid circumvention of
  219 +technological measures.
  220 +
  221 + 4. Conveying Verbatim Copies.
  222 +
  223 + You may convey verbatim copies of the Program's source code as you
  224 +receive it, in any medium, provided that you conspicuously and
  225 +appropriately publish on each copy an appropriate copyright notice;
  226 +keep intact all notices stating that this License and any
  227 +non-permissive terms added in accord with section 7 apply to the code;
  228 +keep intact all notices of the absence of any warranty; and give all
  229 +recipients a copy of this License along with the Program.
  230 +
  231 + You may charge any price or no price for each copy that you convey,
  232 +and you may offer support or warranty protection for a fee.
  233 +
  234 + 5. Conveying Modified Source Versions.
  235 +
  236 + You may convey a work based on the Program, or the modifications to
  237 +produce it from the Program, in the form of source code under the
  238 +terms of section 4, provided that you also meet all of these conditions:
  239 +
  240 + a) The work must carry prominent notices stating that you modified
  241 + it, and giving a relevant date.
  242 +
  243 + b) The work must carry prominent notices stating that it is
  244 + released under this License and any conditions added under section
  245 + 7. This requirement modifies the requirement in section 4 to
  246 + "keep intact all notices".
  247 +
  248 + c) You must license the entire work, as a whole, under this
  249 + License to anyone who comes into possession of a copy. This
  250 + License will therefore apply, along with any applicable section 7
  251 + additional terms, to the whole of the work, and all its parts,
  252 + regardless of how they are packaged. This License gives no
  253 + permission to license the work in any other way, but it does not
  254 + invalidate such permission if you have separately received it.
  255 +
  256 + d) If the work has interactive user interfaces, each must display
  257 + Appropriate Legal Notices; however, if the Program has interactive
  258 + interfaces that do not display Appropriate Legal Notices, your
  259 + work need not make them do so.
  260 +
  261 + A compilation of a covered work with other separate and independent
  262 +works, which are not by their nature extensions of the covered work,
  263 +and which are not combined with it such as to form a larger program,
  264 +in or on a volume of a storage or distribution medium, is called an
  265 +"aggregate" if the compilation and its resulting copyright are not
  266 +used to limit the access or legal rights of the compilation's users
  267 +beyond what the individual works permit. Inclusion of a covered work
  268 +in an aggregate does not cause this License to apply to the other
  269 +parts of the aggregate.
  270 +
  271 + 6. Conveying Non-Source Forms.
  272 +
  273 + You may convey a covered work in object code form under the terms
  274 +of sections 4 and 5, provided that you also convey the
  275 +machine-readable Corresponding Source under the terms of this License,
  276 +in one of these ways:
  277 +
  278 + a) Convey the object code in, or embodied in, a physical product
  279 + (including a physical distribution medium), accompanied by the
  280 + Corresponding Source fixed on a durable physical medium
  281 + customarily used for software interchange.
  282 +
  283 + b) Convey the object code in, or embodied in, a physical product
  284 + (including a physical distribution medium), accompanied by a
  285 + written offer, valid for at least three years and valid for as
  286 + long as you offer spare parts or customer support for that product
  287 + model, to give anyone who possesses the object code either (1) a
  288 + copy of the Corresponding Source for all the software in the
  289 + product that is covered by this License, on a durable physical
  290 + medium customarily used for software interchange, for a price no
  291 + more than your reasonable cost of physically performing this
  292 + conveying of source, or (2) access to copy the
  293 + Corresponding Source from a network server at no charge.
  294 +
  295 + c) Convey individual copies of the object code with a copy of the
  296 + written offer to provide the Corresponding Source. This
  297 + alternative is allowed only occasionally and noncommercially, and
  298 + only if you received the object code with such an offer, in accord
  299 + with subsection 6b.
  300 +
  301 + d) Convey the object code by offering access from a designated
  302 + place (gratis or for a charge), and offer equivalent access to the
  303 + Corresponding Source in the same way through the same place at no
  304 + further charge. You need not require recipients to copy the
  305 + Corresponding Source along with the object code. If the place to
  306 + copy the object code is a network server, the Corresponding Source
  307 + may be on a different server (operated by you or a third party)
  308 + that supports equivalent copying facilities, provided you maintain
  309 + clear directions next to the object code saying where to find the
  310 + Corresponding Source. Regardless of what server hosts the
  311 + Corresponding Source, you remain obligated to ensure that it is
  312 + available for as long as needed to satisfy these requirements.
  313 +
  314 + e) Convey the object code using peer-to-peer transmission, provided
  315 + you inform other peers where the object code and Corresponding
  316 + Source of the work are being offered to the general public at no
  317 + charge under subsection 6d.
  318 +
  319 + A separable portion of the object code, whose source code is excluded
  320 +from the Corresponding Source as a System Library, need not be
  321 +included in conveying the object code work.
  322 +
  323 + A "User Product" is either (1) a "consumer product", which means any
  324 +tangible personal property which is normally used for personal, family,
  325 +or household purposes, or (2) anything designed or sold for incorporation
  326 +into a dwelling. In determining whether a product is a consumer product,
  327 +doubtful cases shall be resolved in favor of coverage. For a particular
  328 +product received by a particular user, "normally used" refers to a
  329 +typical or common use of that class of product, regardless of the status
  330 +of the particular user or of the way in which the particular user
  331 +actually uses, or expects or is expected to use, the product. A product
  332 +is a consumer product regardless of whether the product has substantial
  333 +commercial, industrial or non-consumer uses, unless such uses represent
  334 +the only significant mode of use of the product.
  335 +
  336 + "Installation Information" for a User Product means any methods,
  337 +procedures, authorization keys, or other information required to install
  338 +and execute modified versions of a covered work in that User Product from
  339 +a modified version of its Corresponding Source. The information must
  340 +suffice to ensure that the continued functioning of the modified object
  341 +code is in no case prevented or interfered with solely because
  342 +modification has been made.
  343 +
  344 + If you convey an object code work under this section in, or with, or
  345 +specifically for use in, a User Product, and the conveying occurs as
  346 +part of a transaction in which the right of possession and use of the
  347 +User Product is transferred to the recipient in perpetuity or for a
  348 +fixed term (regardless of how the transaction is characterized), the
  349 +Corresponding Source conveyed under this section must be accompanied
  350 +by the Installation Information. But this requirement does not apply
  351 +if neither you nor any third party retains the ability to install
  352 +modified object code on the User Product (for example, the work has
  353 +been installed in ROM).
  354 +
  355 + The requirement to provide Installation Information does not include a
  356 +requirement to continue to provide support service, warranty, or updates
  357 +for a work that has been modified or installed by the recipient, or for
  358 +the User Product in which it has been modified or installed. Access to a
  359 +network may be denied when the modification itself materially and
  360 +adversely affects the operation of the network or violates the rules and
  361 +protocols for communication across the network.
  362 +
  363 + Corresponding Source conveyed, and Installation Information provided,
  364 +in accord with this section must be in a format that is publicly
  365 +documented (and with an implementation available to the public in
  366 +source code form), and must require no special password or key for
  367 +unpacking, reading or copying.
  368 +
  369 + 7. Additional Terms.
  370 +
  371 + "Additional permissions" are terms that supplement the terms of this
  372 +License by making exceptions from one or more of its conditions.
  373 +Additional permissions that are applicable to the entire Program shall
  374 +be treated as though they were included in this License, to the extent
  375 +that they are valid under applicable law. If additional permissions
  376 +apply only to part of the Program, that part may be used separately
  377 +under those permissions, but the entire Program remains governed by
  378 +this License without regard to the additional permissions.
  379 +
  380 + When you convey a copy of a covered work, you may at your option
  381 +remove any additional permissions from that copy, or from any part of
  382 +it. (Additional permissions may be written to require their own
  383 +removal in certain cases when you modify the work.) You may place
  384 +additional permissions on material, added by you to a covered work,
  385 +for which you have or can give appropriate copyright permission.
  386 +
  387 + Notwithstanding any other provision of this License, for material you
  388 +add to a covered work, you may (if authorized by the copyright holders of
  389 +that material) supplement the terms of this License with terms:
  390 +
  391 + a) Disclaiming warranty or limiting liability differently from the
  392 + terms of sections 15 and 16 of this License; or
  393 +
  394 + b) Requiring preservation of specified reasonable legal notices or
  395 + author attributions in that material or in the Appropriate Legal
  396 + Notices displayed by works containing it; or
  397 +
  398 + c) Prohibiting misrepresentation of the origin of that material, or
  399 + requiring that modified versions of such material be marked in
  400 + reasonable ways as different from the original version; or
  401 +
  402 + d) Limiting the use for publicity purposes of names of licensors or
  403 + authors of the material; or
  404 +
  405 + e) Declining to grant rights under trademark law for use of some
  406 + trade names, trademarks, or service marks; or
  407 +
  408 + f) Requiring indemnification of licensors and authors of that
  409 + material by anyone who conveys the material (or modified versions of
  410 + it) with contractual assumptions of liability to the recipient, for
  411 + any liability that these contractual assumptions directly impose on
  412 + those licensors and authors.
  413 +
  414 + All other non-permissive additional terms are considered "further
  415 +restrictions" within the meaning of section 10. If the Program as you
  416 +received it, or any part of it, contains a notice stating that it is
  417 +governed by this License along with a term that is a further
  418 +restriction, you may remove that term. If a license document contains
  419 +a further restriction but permits relicensing or conveying under this
  420 +License, you may add to a covered work material governed by the terms
  421 +of that license document, provided that the further restriction does
  422 +not survive such relicensing or conveying.
  423 +
  424 + If you add terms to a covered work in accord with this section, you
  425 +must place, in the relevant source files, a statement of the
  426 +additional terms that apply to those files, or a notice indicating
  427 +where to find the applicable terms.
  428 +
  429 + Additional terms, permissive or non-permissive, may be stated in the
  430 +form of a separately written license, or stated as exceptions;
  431 +the above requirements apply either way.
  432 +
  433 + 8. Termination.
  434 +
  435 + You may not propagate or modify a covered work except as expressly
  436 +provided under this License. Any attempt otherwise to propagate or
  437 +modify it is void, and will automatically terminate your rights under
  438 +this License (including any patent licenses granted under the third
  439 +paragraph of section 11).
  440 +
  441 + However, if you cease all violation of this License, then your
  442 +license from a particular copyright holder is reinstated (a)
  443 +provisionally, unless and until the copyright holder explicitly and
  444 +finally terminates your license, and (b) permanently, if the copyright
  445 +holder fails to notify you of the violation by some reasonable means
  446 +prior to 60 days after the cessation.
  447 +
  448 + Moreover, your license from a particular copyright holder is
  449 +reinstated permanently if the copyright holder notifies you of the
  450 +violation by some reasonable means, this is the first time you have
  451 +received notice of violation of this License (for any work) from that
  452 +copyright holder, and you cure the violation prior to 30 days after
  453 +your receipt of the notice.
  454 +
  455 + Termination of your rights under this section does not terminate the
  456 +licenses of parties who have received copies or rights from you under
  457 +this License. If your rights have been terminated and not permanently
  458 +reinstated, you do not qualify to receive new licenses for the same
  459 +material under section 10.
  460 +
  461 + 9. Acceptance Not Required for Having Copies.
  462 +
  463 + You are not required to accept this License in order to receive or
  464 +run a copy of the Program. Ancillary propagation of a covered work
  465 +occurring solely as a consequence of using peer-to-peer transmission
  466 +to receive a copy likewise does not require acceptance. However,
  467 +nothing other than this License grants you permission to propagate or
  468 +modify any covered work. These actions infringe copyright if you do
  469 +not accept this License. Therefore, by modifying or propagating a
  470 +covered work, you indicate your acceptance of this License to do so.
  471 +
  472 + 10. Automatic Licensing of Downstream Recipients.
  473 +
  474 + Each time you convey a covered work, the recipient automatically
  475 +receives a license from the original licensors, to run, modify and
  476 +propagate that work, subject to this License. You are not responsible
  477 +for enforcing compliance by third parties with this License.
  478 +
  479 + An "entity transaction" is a transaction transferring control of an
  480 +organization, or substantially all assets of one, or subdividing an
  481 +organization, or merging organizations. If propagation of a covered
  482 +work results from an entity transaction, each party to that
  483 +transaction who receives a copy of the work also receives whatever
  484 +licenses to the work the party's predecessor in interest had or could
  485 +give under the previous paragraph, plus a right to possession of the
  486 +Corresponding Source of the work from the predecessor in interest, if
  487 +the predecessor has it or can get it with reasonable efforts.
  488 +
  489 + You may not impose any further restrictions on the exercise of the
  490 +rights granted or affirmed under this License. For example, you may
  491 +not impose a license fee, royalty, or other charge for exercise of
  492 +rights granted under this License, and you may not initiate litigation
  493 +(including a cross-claim or counterclaim in a lawsuit) alleging that
  494 +any patent claim is infringed by making, using, selling, offering for
  495 +sale, or importing the Program or any portion of it.
  496 +
  497 + 11. Patents.
  498 +
  499 + A "contributor" is a copyright holder who authorizes use under this
  500 +License of the Program or a work on which the Program is based. The
  501 +work thus licensed is called the contributor's "contributor version".
  502 +
  503 + A contributor's "essential patent claims" are all patent claims
  504 +owned or controlled by the contributor, whether already acquired or
  505 +hereafter acquired, that would be infringed by some manner, permitted
  506 +by this License, of making, using, or selling its contributor version,
  507 +but do not include claims that would be infringed only as a
  508 +consequence of further modification of the contributor version. For
  509 +purposes of this definition, "control" includes the right to grant
  510 +patent sublicenses in a manner consistent with the requirements of
  511 +this License.
  512 +
  513 + Each contributor grants you a non-exclusive, worldwide, royalty-free
  514 +patent license under the contributor's essential patent claims, to
  515 +make, use, sell, offer for sale, import and otherwise run, modify and
  516 +propagate the contents of its contributor version.
  517 +
  518 + In the following three paragraphs, a "patent license" is any express
  519 +agreement or commitment, however denominated, not to enforce a patent
  520 +(such as an express permission to practice a patent or covenant not to
  521 +sue for patent infringement). To "grant" such a patent license to a
  522 +party means to make such an agreement or commitment not to enforce a
  523 +patent against the party.
  524 +
  525 + If you convey a covered work, knowingly relying on a patent license,
  526 +and the Corresponding Source of the work is not available for anyone
  527 +to copy, free of charge and under the terms of this License, through a
  528 +publicly available network server or other readily accessible means,
  529 +then you must either (1) cause the Corresponding Source to be so
  530 +available, or (2) arrange to deprive yourself of the benefit of the
  531 +patent license for this particular work, or (3) arrange, in a manner
  532 +consistent with the requirements of this License, to extend the patent
  533 +license to downstream recipients. "Knowingly relying" means you have
  534 +actual knowledge that, but for the patent license, your conveying the
  535 +covered work in a country, or your recipient's use of the covered work
  536 +in a country, would infringe one or more identifiable patents in that
  537 +country that you have reason to believe are valid.
  538 +
  539 + If, pursuant to or in connection with a single transaction or
  540 +arrangement, you convey, or propagate by procuring conveyance of, a
  541 +covered work, and grant a patent license to some of the parties
  542 +receiving the covered work authorizing them to use, propagate, modify
  543 +or convey a specific copy of the covered work, then the patent license
  544 +you grant is automatically extended to all recipients of the covered
  545 +work and works based on it.
  546 +
  547 + A patent license is "discriminatory" if it does not include within
  548 +the scope of its coverage, prohibits the exercise of, or is
  549 +conditioned on the non-exercise of one or more of the rights that are
  550 +specifically granted under this License. You may not convey a covered
  551 +work if you are a party to an arrangement with a third party that is
  552 +in the business of distributing software, under which you make payment
  553 +to the third party based on the extent of your activity of conveying
  554 +the work, and under which the third party grants, to any of the
  555 +parties who would receive the covered work from you, a discriminatory
  556 +patent license (a) in connection with copies of the covered work
  557 +conveyed by you (or copies made from those copies), or (b) primarily
  558 +for and in connection with specific products or compilations that
  559 +contain the covered work, unless you entered into that arrangement,
  560 +or that patent license was granted, prior to 28 March 2007.
  561 +
  562 + Nothing in this License shall be construed as excluding or limiting
  563 +any implied license or other defenses to infringement that may
  564 +otherwise be available to you under applicable patent law.
  565 +
  566 + 12. No Surrender of Others' Freedom.
  567 +
  568 + If conditions are imposed on you (whether by court order, agreement or
  569 +otherwise) that contradict the conditions of this License, they do not
  570 +excuse you from the conditions of this License. If you cannot convey a
  571 +covered work so as to satisfy simultaneously your obligations under this
  572 +License and any other pertinent obligations, then as a consequence you may
  573 +not convey it at all. For example, if you agree to terms that obligate you
  574 +to collect a royalty for further conveying from those to whom you convey
  575 +the Program, the only way you could satisfy both those terms and this
  576 +License would be to refrain entirely from conveying the Program.
  577 +
  578 + 13. Use with the GNU Affero General Public License.
  579 +
  580 + Notwithstanding any other provision of this License, you have
  581 +permission to link or combine any covered work with a work licensed
  582 +under version 3 of the GNU Affero General Public License into a single
  583 +combined work, and to convey the resulting work. The terms of this
  584 +License will continue to apply to the part which is the covered work,
  585 +but the special requirements of the GNU Affero General Public License,
  586 +section 13, concerning interaction through a network will apply to the
  587 +combination as such.
  588 +
  589 + 14. Revised Versions of this License.
  590 +
  591 + The Free Software Foundation may publish revised and/or new versions of
  592 +the GNU General Public License from time to time. Such new versions will
  593 +be similar in spirit to the present version, but may differ in detail to
  594 +address new problems or concerns.
  595 +
  596 + Each version is given a distinguishing version number. If the
  597 +Program specifies that a certain numbered version of the GNU General
  598 +Public License "or any later version" applies to it, you have the
  599 +option of following the terms and conditions either of that numbered
  600 +version or of any later version published by the Free Software
  601 +Foundation. If the Program does not specify a version number of the
  602 +GNU General Public License, you may choose any version ever published
  603 +by the Free Software Foundation.
  604 +
  605 + If the Program specifies that a proxy can decide which future
  606 +versions of the GNU General Public License can be used, that proxy's
  607 +public statement of acceptance of a version permanently authorizes you
  608 +to choose that version for the Program.
  609 +
  610 + Later license versions may give you additional or different
  611 +permissions. However, no additional obligations are imposed on any
  612 +author or copyright holder as a result of your choosing to follow a
  613 +later version.
  614 +
  615 + 15. Disclaimer of Warranty.
  616 +
  617 + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
  618 +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
  619 +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
  620 +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
  621 +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  622 +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
  623 +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
  624 +ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  625 +
  626 + 16. Limitation of Liability.
  627 +
  628 + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
  629 +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
  630 +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
  631 +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  632 +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
  633 +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
  634 +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
  635 +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
  636 +SUCH DAMAGES.
  637 +
  638 + 17. Interpretation of Sections 15 and 16.
  639 +
  640 + If the disclaimer of warranty and limitation of liability provided
  641 +above cannot be given local legal effect according to their terms,
  642 +reviewing courts shall apply local law that most closely approximates
  643 +an absolute waiver of all civil liability in connection with the
  644 +Program, unless a warranty or assumption of liability accompanies a
  645 +copy of the Program in return for a fee.
  646 +
  647 + END OF TERMS AND CONDITIONS
  648 +
  649 + How to Apply These Terms to Your New Programs
  650 +
  651 + If you develop a new program, and you want it to be of the greatest
  652 +possible use to the public, the best way to achieve this is to make it
  653 +free software which everyone can redistribute and change under these terms.
  654 +
  655 + To do so, attach the following notices to the program. It is safest
  656 +to attach them to the start of each source file to most effectively
  657 +state the exclusion of warranty; and each file should have at least
  658 +the "copyright" line and a pointer to where the full notice is found.
  659 +
  660 + <one line to give the program's name and a brief idea of what it does.>
  661 + Copyright (C) <year> <name of author>
  662 +
  663 + This program is free software: you can redistribute it and/or modify
  664 + it under the terms of the GNU General Public License as published by
  665 + the Free Software Foundation, either version 3 of the License, or
  666 + (at your option) any later version.
  667 +
  668 + This program is distributed in the hope that it will be useful,
  669 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  670 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  671 + GNU General Public License for more details.
  672 +
  673 + You should have received a copy of the GNU General Public License
  674 + along with this program. If not, see <http://www.gnu.org/licenses/>.
  675 +
  676 +Also add information on how to contact you by electronic and paper mail.
  677 +
  678 + If the program does terminal interaction, make it output a short
  679 +notice like this when it starts in an interactive mode:
  680 +
  681 + <program> Copyright (C) <year> <name of author>
  682 + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
  683 + This is free software, and you are welcome to redistribute it
  684 + under certain conditions; type `show c' for details.
  685 +
  686 +The hypothetical commands `show w' and `show c' should show the appropriate
  687 +parts of the General Public License. Of course, your program's commands
  688 +might be different; for a GUI interface, you would use an "about box".
  689 +
  690 + You should also get your employer (if you work as a programmer) or school,
  691 +if any, to sign a "copyright disclaimer" for the program, if necessary.
  692 +For more information on this, and how to apply and follow the GNU GPL, see
  693 +<http://www.gnu.org/licenses/>.
  694 +
  695 + The GNU General Public License does not permit incorporating your program
  696 +into proprietary programs. If your program is a subroutine library, you
  697 +may consider it more useful to permit linking proprietary applications with
  698 +the library. If this is what you want to do, use the GNU Lesser General
  699 +Public License instead of this License. But first, please read
  700 +<http://www.gnu.org/philosophy/why-not-lgpl.html>.
plugins/zsh-navigation-tools/README.md
... ... @@ -0,0 +1,111 @@
  1 +# Zsh Navigation Tools
  2 +
  3 +http://imageshack.com/a/img633/7967/ps6rKR.png
  4 +
  5 +Set of tools like n-history โ€“ย multi-word history searcher, n-cd โ€“ directory
  6 +bookmark manager, n-kill โ€“ย htop like kill utility, and more. Based on
  7 +n-list, a tool generating selectable curses-based list of elements that has
  8 +access to current Zsh session, i.e. has broad capabilities to work together
  9 +with it. Feature highlights include incremental multi-word searching, ANSI
  10 +coloring, unique mode, horizontal scroll, non-selectable elements, grepping and
  11 +various integrations with Zsh.
  12 +
  13 +## History Widget
  14 +
  15 +To have n-history as multi-word incremental searcher bound to Ctrl-R copy znt-*
  16 +files into the */site-functions dir (unless you use Oh My Zsh) and
  17 +add:
  18 +
  19 + autoload znt-history-widget
  20 + zle -N znt-history-widget
  21 + bindkey "^R" znt-history-widget
  22 +
  23 +to .zshrc. This is done automatically when using Oh My Zsh. Two other
  24 +widgets exist, znt-cd-widget and znt-kill-widget, they can be too assigned
  25 +to key combinations (no need for autoload when using Oh My Zsh):
  26 +
  27 + zle -N znt-cd-widget
  28 + bindkey "^T" znt-cd-widget
  29 + zle -N znt-kill-widget
  30 + bindkey "^Y" znt-kill-widget
  31 +
  32 +Oh My Zsh stores history into ~/.zsh_history. When you switch to OMZ you could
  33 +want to copy your previous data (from e.g. ~/.zhistory) into the new location.
  34 +
  35 +## Introduction
  36 +
  37 +The tools are:
  38 +
  39 +- n-aliases - browses aliases, relegates editing to vared
  40 +- n-cd - browses dirstack and bookmarked directories, allows to enter selected directory
  41 +- n-functions - browses functions, relegates editing to zed or vared
  42 +- n-history - browses history, allows to edit and run commands from it
  43 +- n-kill - browses processes list, allows to send signal to selected process
  44 +- n-env - browses environment, relegates editing to vared
  45 +- n-options - browses options, allows to toggle their state
  46 +- n-panelize - loads output of given command into the list for browsing
  47 +
  48 +All tools support horizontal scroll with <,>, {,}, h,l or left and right
  49 +cursors. Other keys are:
  50 +
  51 +- [,] - jump directory bookmarks in n-cd and typical signals in n-kill
  52 +- Ctrl-d, Ctrl-u - half page up or down
  53 +- Ctrl-p, Ctrl-n - previous and next (also done with vim's j,k)
  54 +- Ctrl-l - redraw of whole display
  55 +- g, G - beginning and end of the list
  56 +- Ctrl-o, o - enter uniq mode (no duplicate lines)
  57 +- / - start incremental search
  58 +- Enter - finish incremental search, retaining filter
  59 +- Esc - exit incremental search, clearing filter
  60 +- Ctrl-w (in incremental search) - delete whole word
  61 +- Ctrl-k (in incremental search) - delete whole line
  62 +
  63 +## Programming
  64 +
  65 +The function n-list is used as follows:
  66 +
  67 + n-list {element1} [element2] ... [elementN]
  68 +
  69 +This is all that is needed to be done to have the features like ANSI coloring,
  70 +incremental multi-word search, unique mode, horizontal scroll, non-selectable
  71 +elements (grepping is done outside n-list, see the tools for how it can be
  72 +done). To set up non-selectable entries add their indices into array
  73 +NLIST_NONSELECTABLE_ELEMENTS:
  74 +
  75 + typeset -a NLIST_NONSELECTABLE_ELEMENTS
  76 + NLIST_NONSELECTABLE_ELEMENTS=( 1 )
  77 +
  78 +Result is stored as $reply[REPLY] ($ isn't needed before REPLY because
  79 +of arithmetic context inside []). The returned array might be different from
  80 +input arguments as n-list can process them via incremental search or uniq
  81 +mode. $REPLY is the index in that possibly processed array. If $REPLY
  82 +equals -1 it means that no selection have been made (user quitted via q
  83 +key).
  84 +
  85 +To set up entries that can be jumped to with [,] keys add their indices to
  86 +NLIST_HOP_INDEXES array:
  87 +
  88 + typeset -a NLIST_HOP_INDEXES
  89 + NLIST_HOP_INDEXES=( 1 10 )
  90 +
  91 +n-list can automatically colorize entries according to a Zsh pattern.
  92 +Following example will colorize all numbers with blue:
  93 +
  94 + local NLIST_COLORING_PATTERN="[0-9]##"
  95 + local NLIST_COLORING_COLOR=$'\x1b[00;34m'
  96 + local NLIST_COLORING_END_COLOR=$'\x1b[0m'
  97 + local NLIST_COLORING_MATCH_MULTIPLE=1
  98 + n-list "This is a number 123" "This line too has a number: 456"
  99 +
  100 +Blue is the default color, it doesn't have to be set. See zshexpn man page
  101 +for more information on Zsh patterns. Briefly, comparing to regular
  102 +expressions, (#s) is ^, (#e) is $, # is *, ## is +. Alternative
  103 +will work when in parenthesis, i.e. (a|b). BTW by using this method you can
  104 +colorize output of the tools, via their config files (check out e.g. n-cd.conf,
  105 +it uses this).
  106 +
  107 +## Performance
  108 +ZNT is fastest with Zsh before 5.0.8 and starting from 5.2 (the version yet to
  109 +be released).
  110 +
  111 +# vim:filetype=conf
plugins/zsh-navigation-tools/n-aliases
... ... @@ -0,0 +1,47 @@
  1 +# Copy this file into /usr/share/zsh/site-functions/
  2 +# and add 'autoload n-aliases` to .zshrc
  3 +#
  4 +# This function allows to choose an alias for edition with vared
  5 +#
  6 +# Uses n-list
  7 +
  8 +emulate -L zsh
  9 +
  10 +setopt extendedglob
  11 +zmodload zsh/curses
  12 +zmodload zsh/parameter
  13 +
  14 +local IFS="
  15 +"
  16 +
  17 +unset NLIST_COLORING_PATTERN
  18 +
  19 +[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
  20 +[ -f ~/.config/znt/n-aliases.conf ] && . ~/.config/znt/n-aliases.conf
  21 +
  22 +local list
  23 +local selected
  24 +
  25 +NLIST_REMEMBER_STATE=0
  26 +
  27 +list=( "${(@k)aliases}" )
  28 +list=( "${(@M)list:#(#i)*$1*}" )
  29 +
  30 +local NLIST_GREP_STRING="$1"
  31 +
  32 +if [ "$#list" -eq 0 ]; then
  33 + echo "No matching aliases"
  34 + return 1
  35 +fi
  36 +
  37 +list=( "${(@i)list}" )
  38 +n-list "$list[@]"
  39 +
  40 +if [ "$REPLY" -gt 0 ]; then
  41 + selected="$reply[REPLY]"
  42 + echo "Editing \`$selected':"
  43 + print -rs "vared aliases\\[$selected\\]"
  44 + vared aliases\[$selected\]
  45 +fi
  46 +
  47 +# vim: set filetype=zsh:
plugins/zsh-navigation-tools/n-cd
... ... @@ -0,0 +1,68 @@
  1 +# Copy this file into /usr/share/zsh/site-functions/
  2 +# and add 'autoload n-cd` to .zshrc
  3 +#
  4 +# This function allows to choose a directory from pushd stack
  5 +#
  6 +# Uses n-list
  7 +
  8 +emulate -L zsh
  9 +
  10 +setopt extendedglob pushdignoredups
  11 +
  12 +zmodload zsh/curses
  13 +local IFS="
  14 +"
  15 +
  16 +# Unset before configuration is read
  17 +unset NLIST_COLORING_PATTERN
  18 +
  19 +[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
  20 +[ -f ~/.config/znt/n-cd.conf ] && . ~/.config/znt/n-cd.conf
  21 +
  22 +local list
  23 +local selected
  24 +
  25 +NLIST_REMEMBER_STATE=0
  26 +
  27 +list=( `dirs -p` )
  28 +list=( "${(@M)list:#(#i)*$1*}" )
  29 +
  30 +local NLIST_GREP_STRING="$1"
  31 +
  32 +[ "$#list" -eq 0 ] && echo "No matching directories"
  33 +
  34 +if [ "$#hotlist" -ge 1 ]; then
  35 + typeset -a NLIST_NONSELECTABLE_ELEMENTS NLIST_HOP_INDEXES
  36 + local tmp_list_size="$#list"
  37 + NLIST_NONSELECTABLE_ELEMENTS=( $(( tmp_list_size+1 )) $(( tmp_list_size+2 )) )
  38 + list=( "$list[@]" "" $'\x1b[00;31m'"Hotlist"$'\x1b[00;00m': "$hotlist[@]" )
  39 + (( tmp_list_size+=3 ))
  40 + local middle_hop=$(( (tmp_list_size+$#list) / 2 ))
  41 + [[ "$middle_hop" -eq $tmp_list_size || "$middle_hop" -eq $#list ]] && middle_hop=""
  42 + [ "$tmp_list_size" -eq $#list ] && tmp_list_size=""
  43 + NLIST_HOP_INDEXES=( 1 $tmp_list_size $middle_hop $#list )
  44 +else
  45 + [ "$#list" -eq 0 ] && return 1
  46 +fi
  47 +
  48 +n-list "${list[@]}"
  49 +
  50 +if [ "$REPLY" -gt 0 ]; then
  51 + selected="$reply[REPLY]"
  52 + selected="${selected/#\~/$HOME}"
  53 +
  54 + (( NCD_DONT_PUSHD )) && setopt NO_AUTO_PUSHD
  55 + cd "$selected"
  56 + (( NCD_DONT_PUSHD )) && setopt AUTO_PUSHD
  57 +
  58 + # ZLE?
  59 + if [ "${(t)CURSOR}" = "integer-local-special" ]; then
  60 + zle -M "You have selected $selected"
  61 + else
  62 + echo "You have selected $selected"
  63 + fi
  64 +else
  65 + [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
  66 +fi
  67 +
  68 +# vim: set filetype=zsh:
plugins/zsh-navigation-tools/n-env
... ... @@ -0,0 +1,47 @@
  1 +# Copy this file into /usr/share/zsh/site-functions/
  2 +# and add 'autoload n-env` to .zshrc
  3 +#
  4 +# This function allows to choose an environment variable
  5 +# for edition with vared
  6 +#
  7 +# Uses n-list
  8 +
  9 +emulate -L zsh
  10 +
  11 +setopt extendedglob
  12 +unsetopt equals
  13 +zmodload zsh/curses
  14 +
  15 +local IFS="
  16 +"
  17 +
  18 +[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
  19 +[ -f ~/.config/znt/n-env.conf ] && . ~/.config/znt/n-env.conf
  20 +
  21 +local list
  22 +local selected
  23 +
  24 +NLIST_REMEMBER_STATE=0
  25 +
  26 +list=( `env` )
  27 +list=( "${(@M)list:#(#i)*$1*}" )
  28 +
  29 +local NLIST_GREP_STRING="$1"
  30 +
  31 +if [ "$#list" -eq 0 ]; then
  32 + echo "No matching variables"
  33 + return 1
  34 +fi
  35 +
  36 +list=( "${(@i)list}" )
  37 +n-list "$list[@]"
  38 +
  39 +if [ "$REPLY" -gt 0 ]; then
  40 + selected="$reply[REPLY]"
  41 + selected="${selected%%=*}"
  42 + echo "Editing \`$selected':"
  43 + print -rs "vared \"$selected\""
  44 + vared "$selected"
  45 +fi
  46 +
  47 +# vim: set filetype=zsh:
plugins/zsh-navigation-tools/n-functions
... ... @@ -0,0 +1,54 @@
  1 +# Copy this file into /usr/share/zsh/site-functions/
  2 +# and add 'autoload n-functions` to .zshrc
  3 +#
  4 +# This function allows to choose a function for edition with vared
  5 +#
  6 +# Uses n-list
  7 +
  8 +emulate -L zsh
  9 +
  10 +setopt extendedglob
  11 +zmodload zsh/curses
  12 +zmodload zsh/parameter
  13 +
  14 +local IFS="
  15 +"
  16 +
  17 +unset NLIST_COLORING_PATTERN
  18 +
  19 +[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
  20 +[ -f ~/.config/znt/n-functions.conf ] && . ~/.config/znt/n-functions.conf
  21 +
  22 +local list
  23 +local selected
  24 +
  25 +NLIST_REMEMBER_STATE=0
  26 +
  27 +list=( "${(@k)functions}" )
  28 +list=( "${(@M)list:#(#i)*$1*}" )
  29 +
  30 +local NLIST_GREP_STRING="$1"
  31 +
  32 +if [ "$#list" -eq 0 ]; then
  33 + echo "No matching functions"
  34 + return 1
  35 +fi
  36 +
  37 +list=( "${(@i)list}" )
  38 +n-list "$list[@]"
  39 +
  40 +if [ "$REPLY" -gt 0 ]; then
  41 + selected="$reply[REPLY]"
  42 + if [ "$feditor" = "zed" ]; then
  43 + echo "Editing \`$selected' (ESC ZZ or Ctrl-x-w to finish):"
  44 + autoload zed
  45 + print -rs "zed -f -- \"$selected\""
  46 + zed -f -- "$selected"
  47 + else
  48 + echo "Editing \`$selected':"
  49 + print -rs "vared functions\\[$selected\\]"
  50 + vared functions\[$selected\]
  51 + fi
  52 +fi
  53 +
  54 +# vim: set filetype=zsh:
plugins/zsh-navigation-tools/n-history
... ... @@ -0,0 +1,54 @@
  1 +# Copy this file into /usr/share/zsh/site-functions/
  2 +# and add 'autoload n-history` to .zshrc
  3 +#
  4 +# This function allows to browse Z shell's history and use the
  5 +# entries
  6 +#
  7 +# Uses n-list
  8 +
  9 +emulate -L zsh
  10 +
  11 +setopt extendedglob
  12 +zmodload zsh/curses
  13 +zmodload zsh/parameter
  14 +
  15 +local IFS="
  16 +"
  17 +
  18 +unset NLIST_COLORING_PATTERN
  19 +
  20 +[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
  21 +[ -f ~/.config/znt/n-history.conf ] && . ~/.config/znt/n-history.conf
  22 +
  23 +local list
  24 +local selected
  25 +
  26 +NLIST_REMEMBER_STATE=0
  27 +
  28 +list=( "$history[@]" )
  29 +list=( "${(@M)list:#(#i)*$1*}" )
  30 +
  31 +if [ "$#list" -eq 0 ]; then
  32 + echo "No matching history entries"
  33 + return 1
  34 +fi
  35 +
  36 +local NLIST_GREP_STRING="$1"
  37 +local NLIST_REPLACE_NEWLINES="1"
  38 +n-list "${list[@]}"
  39 +
  40 +if [ "$REPLY" -gt 0 ]; then
  41 + selected="$reply[REPLY]"
  42 + # ZLE?
  43 + if [ "${(t)CURSOR}" = "integer-local-special" ]; then
  44 + zle redisplay
  45 + zle kill-whole-line
  46 + zle -U "$selected"
  47 + else
  48 + print -zr "$selected"
  49 + fi
  50 +else
  51 + [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
  52 +fi
  53 +
  54 +# vim: set filetype=zsh:
plugins/zsh-navigation-tools/n-kill
... ... @@ -0,0 +1,96 @@
  1 +# Copy this file into /usr/share/zsh/site-functions/
  2 +# and add 'autoload n-kill` to .zshrc
  3 +#
  4 +# This function allows to choose a process and a signal to send to it
  5 +#
  6 +# Uses n-list
  7 +
  8 +emulate -L zsh
  9 +
  10 +setopt extendedglob
  11 +zmodload zsh/curses
  12 +
  13 +local IFS="
  14 +"
  15 +
  16 +[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
  17 +[ -f ~/.config/znt/n-kill.conf ] && . ~/.config/znt/n-kill.conf
  18 +
  19 +typeset -A signals
  20 +signals=(
  21 + 1 "1 - HUP"
  22 + 2 "2 - INT"
  23 + 3 "3 - QUIT"
  24 + 6 "6 - ABRT"
  25 + 9 "9 - KILL"
  26 + 14 "14 - ALRM"
  27 + 15 "15 - TERM"
  28 + 17 "17 - STOP"
  29 + 19 "19 - CONT"
  30 +)
  31 +
  32 +local list
  33 +local selected
  34 +local signal
  35 +local -a signal_names
  36 +local title
  37 +
  38 +NLIST_REMEMBER_STATE=0
  39 +
  40 +typeset -a NLIST_NONSELECTABLE_ELEMENTS
  41 +NLIST_NONSELECTABLE_ELEMENTS=( 1 )
  42 +
  43 +type ps 2>/dev/null 1>&2 || { echo >&2 "Error: \`ps' not found"; return 1 }
  44 +
  45 +case "$(uname)" in
  46 + CYGWIN*) list=( `command ps -Wa` ) ;;
  47 + *) list=( `command ps -o pid,uid,command -A` ) ;;
  48 +esac
  49 +
  50 +# Ask of PID
  51 +title=$'\x1b[00;31m'"${list[1]}"$'\x1b[00;00m\0'
  52 +shift list
  53 +list=( "$title" "${(@M)list:#(#i)*$1*}" )
  54 +
  55 +local NLIST_GREP_STRING="$1"
  56 +
  57 +if [ "$#list" -eq 1 ]; then
  58 + echo "No matching processes"
  59 + return 1
  60 +fi
  61 +
  62 +n-list "$list[@]"
  63 +
  64 +# Got answer? (could be Ctrl-C or 'q')
  65 +if [ "$REPLY" -gt 0 ]; then
  66 + selected="$reply[REPLY]"
  67 + selected="${selected## #}"
  68 + pid="${selected%% *}"
  69 +
  70 + # Now ask of signal
  71 + signal_names=( ${(vin)signals} )
  72 + typeset -a NLIST_HOP_INDEXES
  73 + NLIST_HOP_INDEXES=( 3 6 8 )
  74 + unset NLIST_COLORING_PATTERN
  75 + n-list $'\x1b[00;31mSelect signal:\x1b[00;00m' "$signal_names[@]"
  76 +
  77 + if [ "$REPLY" -gt 0 ]; then
  78 + selected="$reply[REPLY]"
  79 + signal="${(k)signals[(r)$selected]}"
  80 +
  81 + # ZLE?
  82 + if [ "${(t)CURSOR}" = "integer-local-special" ]; then
  83 + zle redisplay
  84 + zle kill-whole-line
  85 + zle -U "kill -$signal $pid"
  86 + else
  87 + print -zr "kill -$signal $pid"
  88 + fi
  89 + else
  90 + [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
  91 + fi
  92 +else
  93 + [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
  94 +fi
  95 +
  96 +# vim: set filetype=zsh:
plugins/zsh-navigation-tools/n-list
... ... @@ -0,0 +1,425 @@
  1 +# $1, $2, ... - elements of the list
  2 +# $NLIST_NONSELECTABLE_ELEMENTS - array of indexes (1-based) that cannot be selected
  3 +# $REPLY is the output variable - contains index (1-based) or -1 when no selection
  4 +#
  5 +# Copy this file into /usr/share/zsh/site-functions/
  6 +# and add 'autoload n-list` to .zshrc
  7 +#
  8 +# This function outputs a list of elements that can be
  9 +# navigated with keyboard. Uses curses library
  10 +
  11 +emulate -LR zsh
  12 +
  13 +setopt typesetsilent extendedglob noshortloops
  14 +
  15 +_nlist_has_terminfo=0
  16 +
  17 +zmodload zsh/curses
  18 +zmodload zsh/terminfo 2>/dev/null && _nlist_has_terminfo=1
  19 +
  20 +trap "REPLY=-2; reply=(); return" TERM INT QUIT
  21 +trap "_nlist_exit" EXIT
  22 +
  23 +# Drawing and input
  24 +autoload n-list-draw n-list-input
  25 +
  26 +# Cleanup before any exit
  27 +_nlist_exit() {
  28 + setopt localoptions
  29 + setopt extendedglob
  30 +
  31 + [[ "$REPLY" = -(#c0,1)[0-9]## ]] || REPLY="-1"
  32 + zcurses 2>/dev/null delwin inner
  33 + zcurses 2>/dev/null delwin main
  34 + zcurses 2>/dev/null refresh
  35 + zcurses end
  36 + _nlist_alternate_screen 0
  37 + _nlist_cursor_visibility 1
  38 + unset _nlist_has_terminfo
  39 +}
  40 +
  41 +# Outputs a message in the bottom of the screen
  42 +_nlist_status_msg() {
  43 + # -1 for border, -1 for 0-based indexing
  44 + zcurses move main $(( term_height - 1 - 1 )) 2
  45 + zcurses clear main eol
  46 + zcurses string main "$1"
  47 + #status_msg_strlen is localized in caller
  48 + status_msg_strlen=$#1
  49 +}
  50 +
  51 +# Prefer tput, then module terminfo
  52 +_nlist_cursor_visibility() {
  53 + if type tput 2>/dev/null 1>&2; then
  54 + [ "$1" = "1" ] && { tput cvvis; tput cnorm }
  55 + [ "$1" = "0" ] && tput civis
  56 + elif [ "$_nlist_has_terminfo" = "1" ]; then
  57 + [ "$1" = "1" ] && { [ -n $terminfo[cvvis] ] && echo -n $terminfo[cvvis];
  58 + [ -n $terminfo[cnorm] ] && echo -n $terminfo[cnorm] }
  59 + [ "$1" = "0" ] && [ -n $terminfo[civis] ] && echo -n $terminfo[civis]
  60 + fi
  61 +}
  62 +
  63 +# Reason for this function is that on some systems
  64 +# smcup and rmcup are not knowing why left empty
  65 +_nlist_alternate_screen() {
  66 + [ "$_nlist_has_terminfo" -ne "1" ] && return
  67 + [[ "$1" = "1" && -n "$terminfo[smcup]" ]] && return
  68 + [[ "$1" = "0" && -n "$terminfo[rmcup]" ]] && return
  69 +
  70 + case "$TERM" in
  71 + *rxvt*)
  72 + [ "$1" = "1" ] && echo -n $'\x1b7\x1b[?47h'
  73 + [ "$1" = "0" ] && echo -n $'\x1b[2J\x1b[?47l\x1b8'
  74 + ;;
  75 + *)
  76 + [ "$1" = "1" ] && echo -n $'\x1b[?1049h'
  77 + [ "$1" = "0" ] && echo -n $'\x1b[?1049l'
  78 + # just to remember two other that work: $'\x1b7\x1b[r\x1b[?47h', $'\x1b[?47l\x1b8'
  79 + ;;
  80 + esac
  81 +}
  82 +
  83 +_nlist_compute_user_vars_difference() {
  84 + if [[ "${(t)NLIST_NONSELECTABLE_ELEMENTS}" != "array" &&
  85 + "${(t)NLIST_NONSELECTABLE_ELEMENTS}" != "array-local" ]]
  86 + then
  87 + last_element_difference=0
  88 + current_difference=0
  89 + else
  90 + last_element_difference=$#NLIST_NONSELECTABLE_ELEMENTS
  91 + current_difference=0
  92 + local idx
  93 + for idx in "${(n)NLIST_NONSELECTABLE_ELEMENTS[@]}"; do
  94 + [ "$idx" -le "$NLIST_CURRENT_IDX" ] && current_difference+=1 || break
  95 + done
  96 + fi
  97 +}
  98 +
  99 +# List was processed, check if variables aren't off range
  100 +_nlist_verify_vars() {
  101 + [ "$NLIST_CURRENT_IDX" -gt "$last_element" ] && NLIST_CURRENT_IDX="$last_element"
  102 + [[ "$NLIST_CURRENT_IDX" -eq 0 && "$last_element" -ne 0 ]] && NLIST_CURRENT_IDX=1
  103 + (( NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN=0+((NLIST_CURRENT_IDX-1)/page_height)*page_height+1 ))
  104 +}
  105 +
  106 +# Compute the variables which are shown to the user
  107 +_nlist_setup_user_vars() {
  108 + if [ "$1" = "1" ]; then
  109 + # Basic values when there are no non-selectables
  110 + NLIST_USER_CURRENT_IDX="$NLIST_CURRENT_IDX"
  111 + NLIST_USER_LAST_ELEMENT="$last_element"
  112 + else
  113 + _nlist_compute_user_vars_difference
  114 + NLIST_USER_CURRENT_IDX=$(( NLIST_CURRENT_IDX - current_difference ))
  115 + NLIST_USER_LAST_ELEMENT=$(( last_element - last_element_difference ))
  116 + fi
  117 +}
  118 +
  119 +_nlist_coloring_list_into_col_list() {
  120 + local col=$'\x1b[00;34m' reset=$'\x1b[0m'
  121 + [ -n "$NLIST_COLORING_COLOR" ] && col="$NLIST_COLORING_COLOR"
  122 + [ -n "$NLIST_COLORING_END_COLOR" ] && reset="$NLIST_COLORING_END_COLOR"
  123 +
  124 + if [ "$NLIST_COLORING_MATCH_MULTIPLE" -eq 1 ]; then
  125 + col_list=( "${(@)list//(#mi)$~NLIST_COLORING_PATTERN/$col${MATCH}$reset}" )
  126 + else
  127 + col_list=( "${(@)list/(#mi)$~NLIST_COLORING_PATTERN/$col${MATCH}$reset}" )
  128 + fi
  129 +}
  130 +
  131 +#
  132 +# Main code
  133 +#
  134 +
  135 +# Check if there is proper input
  136 +if [ "$#" -lt 1 ]; then
  137 + echo "Usage: n-list element_1 ..."
  138 + return 1
  139 +fi
  140 +
  141 +REPLY="-1"
  142 +typeset -ga reply
  143 +reply=()
  144 +
  145 +integer term_height="$LINES"
  146 +integer term_width="$COLUMNS"
  147 +if [[ "$term_height" -lt 1 || "$term_width" -lt 1 ]]; then
  148 + local stty_out=$( stty size )
  149 + term_height="${stty_out% *}"
  150 + term_width="${stty_out#* }"
  151 +fi
  152 +integer inner_height=term_height-3
  153 +integer inner_width=term_width-3
  154 +integer page_height=inner_height
  155 +integer page_width=inner_width
  156 +
  157 +typeset -a list col_list disp_list
  158 +integer last_element=$#
  159 +local action
  160 +local final_key
  161 +integer selection
  162 +integer last_element_difference=0
  163 +integer current_difference=0
  164 +local prev_search_buffer=""
  165 +integer prev_uniq_mode=0
  166 +integer prev_start_idx=-1
  167 +
  168 +# Ability to remember the list between calls
  169 +if [[ -z "$NLIST_REMEMBER_STATE" || "$NLIST_REMEMBER_STATE" -eq 0 || "$NLIST_REMEMBER_STATE" -eq 2 ]]; then
  170 + NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN=1
  171 + NLIST_CURRENT_IDX=1
  172 + NLIST_IS_SEARCH_MODE=0
  173 + NLIST_SEARCH_BUFFER=""
  174 + NLIST_TEXT_OFFSET=0
  175 + NLIST_IS_UNIQ_MODE=0
  176 +
  177 + # Zero - because it isn't known, unless we
  178 + # confirm that first element is selectable
  179 + NLIST_USER_CURRENT_IDX=0
  180 + [[ ${NLIST_NONSELECTABLE_ELEMENTS[(r)1]} != 1 ]] && NLIST_USER_CURRENT_IDX=1
  181 + NLIST_USER_LAST_ELEMENT=$(( last_element - $#NLIST_NONSELECTABLE_ELEMENTS ))
  182 +
  183 + # 2 is init once, then remember
  184 + [ "$NLIST_REMEMBER_STATE" -eq 2 ] && NLIST_REMEMBER_STATE=1
  185 +fi
  186 +
  187 +if [ "$NLIST_START_IN_SEARCH_MODE" -eq 1 ]; then
  188 + NLIST_START_IN_SEARCH_MODE=0
  189 + NLIST_IS_SEARCH_MODE=1
  190 +fi
  191 +
  192 +if [ -n "$NLIST_SET_SEARCH_TO" ]; then
  193 + NLIST_SEARCH_BUFFER="$NLIST_SET_SEARCH_TO"
  194 + NLIST_SET_SEARCH_TO=""
  195 +fi
  196 +
  197 +if [ "$NLIST_START_IN_UNIQ_MODE" -eq 1 ]; then
  198 + NLIST_START_IN_UNIQ_MODE=0
  199 + NLIST_IS_UNIQ_MODE=1
  200 +fi
  201 +
  202 +_nlist_alternate_screen 1
  203 +zcurses init
  204 +zcurses delwin main 2>/dev/null
  205 +zcurses delwin inner 2>/dev/null
  206 +zcurses addwin main "$term_height" "$term_width" 0 0
  207 +zcurses addwin inner "$inner_height" "$inner_width" 1 2
  208 +zcurses bg main white/black
  209 +zcurses bg inner white/black
  210 +if [ "$NLIST_IS_SEARCH_MODE" -ne 1 ]; then
  211 + _nlist_cursor_visibility 0
  212 +fi
  213 +
  214 +#
  215 +# Listening for input
  216 +#
  217 +
  218 +local key keypad
  219 +
  220 +# Clear input buffer
  221 +zcurses timeout main 0
  222 +zcurses input main key keypad
  223 +zcurses timeout main -1
  224 +key=""
  225 +keypad=""
  226 +
  227 +list=( "$@" )
  228 +last_element="$#list"
  229 +
  230 +integer is_colored=0
  231 +if [[ -z "$NLIST_SEARCH_BUFFER" && -n "$NLIST_COLORING_PATTERN" ]]; then
  232 + is_colored=1
  233 + _nlist_coloring_list_into_col_list
  234 +fi
  235 +
  236 +while (( 1 )); do
  237 + # Do searching (filtering with string)
  238 + if [ -n "$NLIST_SEARCH_BUFFER" ]; then
  239 + # Compute new list, col_list ?
  240 + if [[ "$NLIST_SEARCH_BUFFER" != "$prev_search_buffer" || "$NLIST_IS_UNIQ_MODE" -ne "$prev_uniq_mode" ]]; then
  241 + prev_search_buffer="$NLIST_SEARCH_BUFFER"
  242 + prev_uniq_mode="$NLIST_IS_UNIQ_MODE"
  243 + # regenerating list -> regenerating disp_list
  244 + prev_start_idx=-1
  245 +
  246 + # Take all elements, including duplicates and non-selectables
  247 + typeset +U list
  248 + list=( "$@" )
  249 +
  250 + # Remove non-selectable elements
  251 + [ "$#NLIST_NONSELECTABLE_ELEMENTS" -gt 0 ] && for i in "${(nO)NLIST_NONSELECTABLE_ELEMENTS[@]}"; do
  252 + list[$i]=()
  253 + done
  254 +
  255 + # Remove duplicates
  256 + [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && typeset -U list
  257 +
  258 + last_element="$#list"
  259 +
  260 + # Next do the filtering
  261 + local search_buffer="${NLIST_SEARCH_BUFFER%% ##}"
  262 + search_buffer="${search_buffer## ##}"
  263 + search_buffer="${search_buffer//(#m)[][*?|#~^()><\\]/\\$MATCH}"
  264 + if [ -n "$search_buffer" ]; then
  265 + # Patterns will be *foo*~^*bar* and foo|bar)
  266 + local search_pattern="${search_buffer// ##/*~^*}"
  267 + local colsearch_pattern="${search_buffer// ##/|}"
  268 +
  269 + list=( "${(@M)list:#(#i)*$~search_pattern*}" )
  270 + last_element="$#list"
  271 +
  272 + local red=$'\x1b[00;31m' reset=$'\x1b[00;00m'
  273 + col_list=( "${(@)list//(#mi)($~colsearch_pattern)/$red${MATCH}$reset}" )
  274 + else
  275 + col_list=( "$list[@]" )
  276 + fi
  277 +
  278 + # Called after processing list
  279 + _nlist_verify_vars
  280 + fi
  281 +
  282 + _nlist_setup_user_vars 1
  283 +
  284 + integer end_idx=$(( NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN + page_height - 1 ))
  285 + [ "$end_idx" -gt "$last_element" ] && end_idx=last_element
  286 +
  287 + if [ "$prev_start_idx" -ne "$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" ]; then
  288 + prev_start_idx="$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN"
  289 + disp_list=( "${(@)col_list[NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN, end_idx]}" )
  290 +
  291 + # We have display list, lets replace newlines with "\n" when needed (1/3)
  292 + [ "$NLIST_REPLACE_NEWLINES" -eq 1 ] && disp_list=( "${(@)disp_list//$'\n'/\\n}" )
  293 + fi
  294 +
  295 + # Output colored list
  296 + n-list-draw "$(( (NLIST_CURRENT_IDX-1) % page_height + 1 ))" \
  297 + "$page_height" "$page_width" 0 0 "$NLIST_TEXT_OFFSET" inner \
  298 + "$disp_list[@]"
  299 + else
  300 + # There is no search, but there was in previous loop
  301 + # OR
  302 + # Uniq mode was entered or left out
  303 + # -> compute new list (maybe also col_list)
  304 + if [[ -n "$prev_search_buffer" || "$NLIST_IS_UNIQ_MODE" -ne "$prev_uniq_mode" ]]; then
  305 + prev_search_buffer=""
  306 + prev_uniq_mode="$NLIST_IS_UNIQ_MODE"
  307 + # regenerating list -> regenerating disp_list
  308 + prev_start_idx=-1
  309 +
  310 + # Take all elements, including duplicates and non-selectables
  311 + typeset +U list
  312 + list=( "$@" )
  313 +
  314 + # Remove non-selectable elements only when in uniq mode
  315 + [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && [ "$#NLIST_NONSELECTABLE_ELEMENTS" -gt 0 ] &&
  316 + for i in "${(nO)NLIST_NONSELECTABLE_ELEMENTS[@]}"; do
  317 + list[$i]=()
  318 + done
  319 +
  320 + # Remove duplicates when in uniq mode
  321 + [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && typeset -U list
  322 +
  323 + # Apply coloring pattern (when not with search query)
  324 + is_colored=0
  325 + if [ -n "$NLIST_COLORING_PATTERN" ]; then
  326 + is_colored=1
  327 + _nlist_coloring_list_into_col_list
  328 + fi
  329 +
  330 + last_element="$#list"
  331 + # Called after processing list
  332 + _nlist_verify_vars
  333 + fi
  334 +
  335 + # "1" - shouldn't bother with non-selectables
  336 + _nlist_setup_user_vars "$NLIST_IS_UNIQ_MODE"
  337 +
  338 + integer end_idx=$(( NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN + page_height - 1 ))
  339 + [ "$end_idx" -gt "$last_element" ] && end_idx=last_element
  340 +
  341 + if [ "$is_colored" -eq 0 ]; then
  342 + if [ "$prev_start_idx" -ne "$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" ]; then
  343 + prev_start_idx="$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN"
  344 + disp_list=( "${(@)list[NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN, end_idx]}" )
  345 +
  346 + # We have display list, lets replace newlines with "\n" when needed (2/3)
  347 + [ "$NLIST_REPLACE_NEWLINES" -eq 1 ] && disp_list=( "${(@)disp_list//$'\n'/\\n}" )
  348 + fi
  349 + else
  350 + if [ "$prev_start_idx" -ne "$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" ]; then
  351 + prev_start_idx="$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN"
  352 + disp_list=( "${(@)col_list[NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN, end_idx]}" )
  353 +
  354 + # We have display list, lets replace newlines with "\n" when needed (3/3)
  355 + [ "$NLIST_REPLACE_NEWLINES" -eq 1 ] && disp_list=( "${(@)disp_list//$'\n'/\\n}" )
  356 + fi
  357 + fi
  358 +
  359 + # Output the list
  360 + n-list-draw "$(( (NLIST_CURRENT_IDX-1) % page_height + 1 ))" \
  361 + "$page_height" "$page_width" 0 0 "$NLIST_TEXT_OFFSET" inner \
  362 + "$disp_list[@]"
  363 + fi
  364 +
  365 + local status_msg_strlen
  366 + if [ "$NLIST_IS_SEARCH_MODE" = "1" ]; then
  367 + local _txt2=""
  368 + [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && _txt2="[-UNIQ-] "
  369 + _nlist_status_msg "${_txt2}Filtering with: ${NLIST_SEARCH_BUFFER// /+}"
  370 + elif [[ ${NLIST_NONSELECTABLE_ELEMENTS[(r)$NLIST_CURRENT_IDX]} != $NLIST_CURRENT_IDX ||
  371 + -n "$NLIST_SEARCH_BUFFER" || "$NLIST_IS_UNIQ_MODE" -eq 1 ]]; then
  372 + local _txt="" _txt2=""
  373 + [ -n "$NLIST_GREP_STRING" ] && _txt=" [$NLIST_GREP_STRING]"
  374 + [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && _txt2="[-UNIQ-] "
  375 + _nlist_status_msg "${_txt2}Current #$NLIST_USER_CURRENT_IDX (of #$NLIST_USER_LAST_ELEMENT entries)$_txt"
  376 + else
  377 + _nlist_status_msg ""
  378 + fi
  379 +
  380 + zcurses border main
  381 + zcurses refresh main inner
  382 + zcurses move main $(( term_height - 1 - 1 )) $(( status_msg_strlen + 2 ))
  383 +
  384 + # Wait for input
  385 + zcurses input main key keypad
  386 +
  387 + # Get the special (i.e. "keypad") key or regular key
  388 + if [ -n "$key" ]; then
  389 + final_key="$key"
  390 + elif [ -n "$keypad" ]; then
  391 + final_key="$keypad"
  392 + else
  393 + _nlist_status_msg "Inproper input detected"
  394 + zcurses refresh main inner
  395 + fi
  396 +
  397 + n-list-input "$NLIST_CURRENT_IDX" "$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" \
  398 + "$page_height" "$page_width" "$last_element" "$NLIST_TEXT_OFFSET" \
  399 + "$final_key" "$NLIST_IS_SEARCH_MODE" "$NLIST_SEARCH_BUFFER" \
  400 + "$NLIST_IS_UNIQ_MODE"
  401 +
  402 + selection="$reply[1]"
  403 + action="$reply[2]"
  404 + NLIST_CURRENT_IDX="$reply[3]"
  405 + NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN="$reply[4]"
  406 + NLIST_TEXT_OFFSET="$reply[5]"
  407 + NLIST_IS_SEARCH_MODE="$reply[6]"
  408 + NLIST_SEARCH_BUFFER="$reply[7]"
  409 + NLIST_IS_UNIQ_MODE="$reply[8]"
  410 +
  411 + if [ "$action" = "SELECT" ]; then
  412 + REPLY="$selection"
  413 + reply=( "$list[@]" )
  414 + break
  415 + elif [ "$action" = "QUIT" ]; then
  416 + REPLY=-1
  417 + reply=( "$list[@]" )
  418 + break
  419 + elif [ "$action" = "REDRAW" ]; then
  420 + zcurses clear main redraw
  421 + zcurses clear inner redraw
  422 + fi
  423 +done
  424 +
  425 +# vim: set filetype=zsh:
plugins/zsh-navigation-tools/n-list-draw
... ... @@ -0,0 +1,131 @@
  1 +# Copy this file into /usr/share/zsh/site-functions/
  2 +# and add 'autoload n-list-draw` to .zshrc
  3 +#
  4 +# This is an internal function not for direct use
  5 +
  6 +emulate -L zsh
  7 +
  8 +zmodload zsh/curses
  9 +
  10 +setopt typesetsilent extendedglob
  11 +
  12 +_nlist_print_with_ansi() {
  13 + local win="$1" text="$2" out col chunk Xout
  14 + integer text_offset="$3" max_text_len="$4" text_len=0 no_match=0 nochunk_text_len to_skip_from_chunk to_chop_off_from_chunk before_len
  15 +
  16 + # 1 - non-escaped text, 2 - first number in the escaped text, with ;
  17 + # 3 - second number, 4 - text after whole escape text
  18 +
  19 + typeset -a c
  20 + c=( black red green yellow blue magenta cyan white )
  21 +
  22 + while [[ -n "$text" && "$no_match" -eq 0 ]]; do
  23 + if [[ "$text" = (#b)([^$'\x1b']#)$'\x1b'\[([0-9](#c0,2))(#B)(\;|)(#b)([0-9](#c0,2))m(*) ]]; then
  24 + # Text for further processing
  25 + text="$match[4]"
  26 + # Text chunk to output now
  27 + out="$match[1]"
  28 + # Save color
  29 + col="$match[2]"
  30 + (( match[3] >= 30 && match[3] <= 37 )) && col="$match[3]"
  31 + else
  32 + out="$text"
  33 + no_match=1
  34 + fi
  35 +
  36 + if [ -n "$out" ]; then
  37 +################ Expand tabs ################
  38 + chunk="$out"
  39 + before_len="$text_len"
  40 + Xout=""
  41 +
  42 + while [ -n "$chunk" ]; do
  43 + [[ "$chunk" = (#b)([^$'\t']#)$'\t'(*) ]] && {
  44 + (( all_text_len=((before_len+${#match[1]})/8+1)*8 ))
  45 +
  46 + Xout+="${(r:all_text_len-before_len:: :)match[1]}"
  47 +
  48 + before_len+=all_text_len-before_len
  49 + chunk="$match[2]"
  50 + } || {
  51 + Xout+="$chunk"
  52 + break
  53 + }
  54 + done
  55 +#############################################
  56 +
  57 + # Input text length without the current chunk
  58 + nochunk_text_len=text_len
  59 + # Input text length up to current chunk
  60 + text_len+="$#Xout"
  61 +
  62 + # Should start displaying with this chunk?
  63 + # I.e. stop skipping left part of the input text?
  64 + if (( text_len > text_offset )); then
  65 + to_skip_from_chunk=text_offset-nochunk_text_len
  66 +
  67 + # LEFT - is chunk off the left skip boundary? +1 for 1-based index in string
  68 + (( to_skip_from_chunk > 0 )) && Xout="${Xout[to_skip_from_chunk+1,-1]}"
  69 +
  70 + # RIGHT - is text off the screen?
  71 + if (( text_len-text_offset > max_text_len )); then
  72 + to_chop_off_from_chunk=0+(text_len-text_offset)-max_text_len
  73 + Xout="${Xout[1,-to_chop_off_from_chunk-1]}"
  74 + fi
  75 +
  76 + [ -n "$Xout" ] && zcurses string "$win" "$Xout"
  77 + fi
  78 + fi
  79 +
  80 + if (( no_match == 0 )); then
  81 + if (( col >= 30 && col <= 37 )); then
  82 + zcurses attr "$win" $c[col-29]/black
  83 + elif [[ "$col" -eq 0 ]]; then
  84 + zcurses attr "$win" white/black
  85 + fi
  86 + fi
  87 + done
  88 +}
  89 +
  90 +integer highlight="$1"
  91 +integer page_height="$2"
  92 +integer page_width="$3"
  93 +local y_offset="$4"
  94 +local x_offset="$5"
  95 +local text_offset="$6"
  96 +local win="$7"
  97 +shift 7
  98 +integer max_text_len=page_width-x_offset
  99 +
  100 +[ "$bold" = "0" ] && bold="" || bold="+bold"
  101 +[[ "$active_text" = "underline" || "$active_text" = "reverse" ]] || active_text="reverse"
  102 +# With Linux terminal underline won't work properly
  103 +[ "$TERM" = "linux" ] && active_text="reverse"
  104 +
  105 +integer max_idx=page_height
  106 +integer end_idx=max_idx
  107 +[ "$end_idx" -gt "$#" ] && end_idx="$#"
  108 +integer y=y_offset
  109 +
  110 +zcurses attr "$win" $bold white/black
  111 +
  112 +integer i text_len
  113 +local text
  114 +for (( i=1; i<=end_idx; i++ )); do
  115 + zcurses move "$win" $y "$x_offset"
  116 +
  117 + [ "$i" = "$highlight" ] && zcurses attr "$win" +"$active_text"
  118 + _nlist_print_with_ansi "$win" "$@[i]" "$text_offset" "$max_text_len"
  119 + zcurses clear "$win" eol
  120 + [ "$i" = "$highlight" ] && zcurses attr "$win" -"$active_text"
  121 +
  122 + y+=1
  123 +done
  124 +
  125 +if [ "$end_idx" -lt "$max_idx" ]; then
  126 + zcurses move "$win" $y "$x_offset"
  127 + zcurses clear "$win" eol
  128 +fi
  129 +
  130 +zcurses attr "$win" white/black
  131 +# vim: set filetype=zsh:
plugins/zsh-navigation-tools/n-list-input
... ... @@ -0,0 +1,238 @@
  1 +# Copy this file into /usr/share/zsh/site-functions/
  2 +# and add 'autoload n-list-input` to .zshrc
  3 +#
  4 +# This is an internal function not for direct use
  5 +
  6 +emulate -L zsh
  7 +
  8 +zmodload zsh/curses
  9 +
  10 +setopt typesetsilent
  11 +
  12 +# Compute first to show index
  13 +_nlist_compute_first_to_show_idx() {
  14 + from_what_idx_list_is_shown=0+((current_idx-1)/page_height)*page_height+1
  15 +}
  16 +
  17 +typeset -ga reply
  18 +reply=( -1 '' )
  19 +integer current_idx="$1"
  20 +integer from_what_idx_list_is_shown="$2"
  21 +integer page_height="$3"
  22 +integer page_width="$4"
  23 +integer last_element="$5"
  24 +integer hscroll="$6"
  25 +local key="$7"
  26 +integer search="$8"
  27 +local buffer="$9"
  28 +integer uniq_mode="$10"
  29 +
  30 +#
  31 +# Listening for input
  32 +#
  33 +
  34 +if [ "$search" = "0" ]; then
  35 +
  36 +case "$key" in
  37 + (UP|k|$'\C-P')
  38 + # Are there any elements before the current one?
  39 + [ "$current_idx" -gt 1 ] && current_idx=current_idx-1;
  40 + _nlist_compute_first_to_show_idx
  41 + ;;
  42 + (DOWN|j|$'\C-N')
  43 + # Are there any elements after the current one?
  44 + [ "$current_idx" -lt "$last_element" ] && current_idx=current_idx+1;
  45 + _nlist_compute_first_to_show_idx
  46 + ;;
  47 + (PPAGE)
  48 + current_idx=current_idx-page_height
  49 + [ "$current_idx" -lt 1 ] && current_idx=1;
  50 + _nlist_compute_first_to_show_idx
  51 + ;;
  52 + (NPAGE|" ")
  53 + current_idx=current_idx+page_height
  54 + [ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
  55 + _nlist_compute_first_to_show_idx
  56 + ;;
  57 + ($'\C-U')
  58 + current_idx=current_idx-page_height/2
  59 + [ "$current_idx" -lt 1 ] && current_idx=1;
  60 + _nlist_compute_first_to_show_idx
  61 + ;;
  62 + ($'\C-D')
  63 + current_idx=current_idx+page_height/2
  64 + [ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
  65 + _nlist_compute_first_to_show_idx
  66 + ;;
  67 + (HOME|g)
  68 + current_idx=1
  69 + _nlist_compute_first_to_show_idx
  70 + ;;
  71 + (END|G)
  72 + current_idx=last_element
  73 + _nlist_compute_first_to_show_idx
  74 + ;;
  75 + ($'\n')
  76 + # Is that element selectable?
  77 + # Check for this only when there is no search
  78 + if [[ "$NLIST_SEARCH_BUFFER" != "" || "$NLIST_IS_UNIQ_MODE" -eq 1 ||
  79 + ${NLIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} != $current_idx ]]
  80 + then
  81 + # Save current element in the result variable
  82 + reply=( $current_idx SELECT )
  83 + fi
  84 + ;;
  85 + (q)
  86 + reply=( -1 QUIT )
  87 + ;;
  88 + (/)
  89 + search=1
  90 + _nlist_cursor_visibility 1
  91 + ;;
  92 + ($'\t')
  93 + reply=( $current_idx LEAVE )
  94 + ;;
  95 + ($'\C-L')
  96 + reply=( -1 REDRAW )
  97 + ;;
  98 + (\])
  99 + [[ "${(t)NLIST_HOP_INDEXES}" = "array" || "${(t)NLIST_HOP_INDEXES}" = "array-local" ]] &&
  100 + [ -z "$NLIST_SEARCH_BUFFER" ] && [ "$NLIST_IS_UNIQ_MODE" -eq 0 ] &&
  101 + for idx in "${(n)NLIST_HOP_INDEXES[@]}"; do
  102 + if [ "$idx" -gt "$current_idx" ]; then
  103 + current_idx=$idx
  104 + _nlist_compute_first_to_show_idx
  105 + break
  106 + fi
  107 + done
  108 + ;;
  109 + (\[)
  110 + [[ "${(t)NLIST_HOP_INDEXES}" = "array" || "${(t)NLIST_HOP_INDEXES}" = "array-local" ]] &&
  111 + [ -z "$NLIST_SEARCH_BUFFER" ] && [ "$NLIST_IS_UNIQ_MODE" -eq 0 ] &&
  112 + for idx in "${(nO)NLIST_HOP_INDEXES[@]}"; do
  113 + if [ "$idx" -lt "$current_idx" ]; then
  114 + current_idx=$idx
  115 + _nlist_compute_first_to_show_idx
  116 + break
  117 + fi
  118 + done
  119 + ;;
  120 + ('<'|'{'|LEFT|'h')
  121 + hscroll=hscroll-7
  122 + [ "$hscroll" -lt 0 ] && hscroll=0
  123 + ;;
  124 + ('>'|'}'|RIGHT|'l')
  125 + hscroll+=7
  126 + ;;
  127 + ($'\E')
  128 + buffer=""
  129 + ;;
  130 + (o|$'\C-O')
  131 + uniq_mode=1-uniq_mode
  132 + ;;
  133 + (*)
  134 + ;;
  135 +esac
  136 +
  137 +else
  138 +
  139 +case "$key" in
  140 + ($'\n')
  141 + search=0
  142 + _nlist_cursor_visibility 0
  143 + ;;
  144 + ($'\C-L')
  145 + reply=( -1 REDRAW )
  146 + ;;
  147 +
  148 + #
  149 + # Slightly limited navigation
  150 + #
  151 +
  152 + (UP|$'\C-P')
  153 + [ "$current_idx" -gt 1 ] && current_idx=current_idx-1;
  154 + _nlist_compute_first_to_show_idx
  155 + ;;
  156 + (DOWN|$'\C-N')
  157 + [ "$current_idx" -lt "$last_element" ] && current_idx=current_idx+1;
  158 + _nlist_compute_first_to_show_idx
  159 + ;;
  160 + (PPAGE)
  161 + current_idx=current_idx-page_height
  162 + [ "$current_idx" -lt 1 ] && current_idx=1;
  163 + _nlist_compute_first_to_show_idx
  164 + ;;
  165 + (NPAGE)
  166 + current_idx=current_idx+page_height
  167 + [ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
  168 + _nlist_compute_first_to_show_idx
  169 + ;;
  170 + ($'\C-U')
  171 + current_idx=current_idx-page_height/2
  172 + [ "$current_idx" -lt 1 ] && current_idx=1;
  173 + _nlist_compute_first_to_show_idx
  174 + ;;
  175 + ($'\C-D')
  176 + current_idx=current_idx+page_height/2
  177 + [ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
  178 + _nlist_compute_first_to_show_idx
  179 + ;;
  180 + (HOME)
  181 + current_idx=1
  182 + _nlist_compute_first_to_show_idx
  183 + ;;
  184 + (END)
  185 + current_idx=last_element
  186 + _nlist_compute_first_to_show_idx
  187 + ;;
  188 + (LEFT)
  189 + hscroll=hscroll-7
  190 + [ "$hscroll" -lt 0 ] && hscroll=0
  191 + ;;
  192 + (RIGHT)
  193 + hscroll+=7
  194 + ;;
  195 + (F1|F2|F3|F4|F5|F6|F7|F8|F9|F10)
  196 + # ignore
  197 + ;;
  198 +
  199 + #
  200 + # The input
  201 + #
  202 +
  203 + ($'\b'|$'\C-?'|BACKSPACE)
  204 + buffer="${buffer%?}"
  205 + ;;
  206 + ($'\C-W')
  207 + [ "$buffer" = "${buffer% *}" ] && buffer="" || buffer="${buffer% *}"
  208 + ;;
  209 + ($'\C-K')
  210 + buffer=""
  211 + ;;
  212 + ($'\E')
  213 + buffer=""
  214 + search=0
  215 + _nlist_cursor_visibility 0
  216 + ;;
  217 + ($'\C-O')
  218 + uniq_mode=1-uniq_mode
  219 + ;;
  220 + (*)
  221 + if [[ $#key == 1 && $((#key)) -lt 31 ]]; then
  222 + # ignore all other control keys
  223 + else
  224 + buffer+="$key"
  225 + fi
  226 + ;;
  227 +esac
  228 +
  229 +fi
  230 +
  231 +reply[3]="$current_idx"
  232 +reply[4]="$from_what_idx_list_is_shown"
  233 +reply[5]="$hscroll"
  234 +reply[6]="$search"
  235 +reply[7]="$buffer"
  236 +reply[8]="$uniq_mode"
  237 +
  238 +# vim: set filetype=zsh:
plugins/zsh-navigation-tools/n-options
... ... @@ -0,0 +1,84 @@
  1 +# Copy this file into /usr/share/zsh/site-functions/
  2 +# and add 'autoload n-options` to .zshrc
  3 +#
  4 +# This function allows to browse and toggle shell's options
  5 +#
  6 +# Uses n-list
  7 +
  8 +#emulate -L zsh
  9 +
  10 +zmodload zsh/curses
  11 +
  12 +local IFS="
  13 +"
  14 +
  15 +unset NLIST_COLORING_PATTERN
  16 +
  17 +[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
  18 +[ -f ~/.config/znt/n-options.conf ] && . ~/.config/znt/n-options.conf
  19 +
  20 +# TODO restore options
  21 +unsetopt localoptions
  22 +
  23 +integer kshoptionprint=0
  24 +[[ -o kshoptionprint ]] && kshoptionprint=1
  25 +setopt kshoptionprint
  26 +
  27 +local list
  28 +local selected
  29 +local option
  30 +local state
  31 +
  32 +# 0 - don't remember, 1 - remember, 2 - init once, then remember
  33 +NLIST_REMEMBER_STATE=2
  34 +
  35 +local NLIST_GREP_STRING="${1:=}"
  36 +
  37 +while (( 1 )); do
  38 + list=( `setopt` )
  39 + list=( "${(M)list[@]:#*${1:=}*}" )
  40 + list=( "${list[@]:#kshoptionprint*}" )
  41 +
  42 + if [ "$#list" -eq 0 ]; then
  43 + echo "No matching options"
  44 + break
  45 + fi
  46 +
  47 + local red=$'\x1b[00;31m' green=$'\x1b[00;32m' reset=$'\x1b[00;00m'
  48 + list=( "${list[@]/ off/${red} off$reset}" )
  49 + #list=( "${list[@]/ on/${green} on$reset}" )
  50 + list=( "${(i)list[@]}" )
  51 +
  52 + n-list "${list[@]}"
  53 +
  54 + if [ "$REPLY" -gt 0 ]; then
  55 + [[ -o ksharrays ]] && selected="${reply[$(( REPLY - 1 ))]}" || selected="${reply[$REPLY]}"
  56 + option="${selected%% *}"
  57 + state="${selected##* }"
  58 +
  59 + if [[ -o globsubst ]]; then
  60 + unsetopt globsubst
  61 + state="${state%$reset}"
  62 + setopt globsubst
  63 + else
  64 + state="${state%$reset}"
  65 + fi
  66 +
  67 + # Toggle the option
  68 + if [ "$state" = "on" ]; then
  69 + echo "Setting |$option| to off"
  70 + unsetopt "$option"
  71 + else
  72 + echo "Setting |$option| to on"
  73 + setopt "$option"
  74 + fi
  75 + else
  76 + break
  77 + fi
  78 +done
  79 +
  80 +NLIST_REMEMBER_STATE=0
  81 +
  82 +[[ "$kshoptionprint" -eq 0 ]] && unsetopt kshoptionprint
  83 +
  84 +# vim: set filetype=zsh:
plugins/zsh-navigation-tools/n-panelize
... ... @@ -0,0 +1,61 @@
  1 +# Copy this file into /usr/share/zsh/site-functions/
  2 +# and add 'autoload n-panelize` to .zshrc
  3 +#
  4 +# This function somewhat reminds the panelize feature from Midnight Commander
  5 +# It allows browsing output of arbitrary command. Example usage:
  6 +# v-panelize ls /usr/local/bin
  7 +#
  8 +# Uses n-list
  9 +
  10 +emulate -L zsh
  11 +
  12 +setopt extendedglob
  13 +zmodload zsh/curses
  14 +
  15 +local IFS="
  16 +"
  17 +
  18 +unset NLIST_COLORING_PATTERN
  19 +
  20 +[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
  21 +[ -f ~/.config/znt/n-panelize.conf ] && . ~/.config/znt/n-panelize.conf
  22 +
  23 +local list
  24 +local selected
  25 +
  26 +NLIST_REMEMBER_STATE=0
  27 +
  28 +if [ -t 0 ]; then
  29 + # Check if there is proper input
  30 + if [ "$#" -lt 1 ]; then
  31 + echo "Usage: n-panelize {command} [option|argument] ... or command | n-panelize"
  32 + return 1
  33 + fi
  34 +
  35 + list=( `"$@"` )
  36 + # TODO: $? doesn't reach user
  37 + [ "$?" -eq 127 ] && return $?
  38 +else
  39 + # Check if can reattach to terminal
  40 + if [[ ! -c /dev/tty && ! -t 2 ]]; then
  41 + echo "No terminal available (no /dev/tty)"
  42 + return 1
  43 + fi
  44 +
  45 + list=( "${(@f)"$(<&0)"}" )
  46 +
  47 + if [[ ! -c /dev/tty ]]; then
  48 + exec <&2
  49 + else
  50 + exec </dev/tty
  51 + fi
  52 +fi
  53 +
  54 +n-list "${list[@]}"
  55 +
  56 +if [ "$REPLY" -gt 0 ]; then
  57 + selected="$reply[REPLY]"
  58 + print -zr "# $selected"
  59 +fi
  60 +
  61 +# vim: set filetype=zsh:
plugins/zsh-navigation-tools/znt-cd-widget
... ... @@ -0,0 +1,8 @@
  1 +autoload znt-usetty-wrapper n-cd
  2 +local NLIST_START_IN_SEARCH_MODE=0
  3 +local NLIST_START_IN_UNIQ_MODE=0
  4 +
  5 +znt-usetty-wrapper n-cd "$@"
  6 +
  7 +unset NLIST_START_IN_SEARCH_MODE
  8 +unset NLIST_START_IN_UNIQ_MODE
plugins/zsh-navigation-tools/znt-history-widget
... ... @@ -0,0 +1,10 @@
  1 +autoload znt-usetty-wrapper n-history
  2 +local NLIST_START_IN_SEARCH_MODE=1
  3 +local NLIST_START_IN_UNIQ_MODE=1
  4 +local NLIST_SET_SEARCH_TO="$BUFFER"
  5 +
  6 +znt-usetty-wrapper n-history "$@"
  7 +
  8 +unset NLIST_START_IN_SEARCH_MODE
  9 +unset NLIST_START_IN_UNIQ_MODE
  10 +unset NLIST_SET_SEARCH_TO
plugins/zsh-navigation-tools/znt-kill-widget
... ... @@ -0,0 +1,8 @@
  1 +autoload znt-usetty-wrapper n-kill
  2 +local NLIST_START_IN_SEARCH_MODE=0
  3 +local NLIST_START_IN_UNIQ_MODE=0
  4 +
  5 +znt-usetty-wrapper n-kill "$@"
  6 +
  7 +unset NLIST_START_IN_SEARCH_MODE
  8 +unset NLIST_START_IN_UNIQ_MODE
plugins/zsh-navigation-tools/znt-usetty-wrapper
... ... @@ -0,0 +1,40 @@
  1 +emulate -L zsh
  2 +
  3 +zmodload zsh/curses
  4 +
  5 +test_fd0() {
  6 + true <&0
  7 +}
  8 +
  9 +local restore=0 FD
  10 +
  11 +# Reattach to terminal
  12 +if [ ! -t 0 ]; then
  13 + # Check if can reattach to terminal in any way
  14 + if [[ ! -c /dev/tty && ! -t 2 ]]; then
  15 + echo "No terminal available (no /dev/tty and no terminal at stderr)"
  16 + return 1
  17 + fi
  18 +
  19 + if test_fd0 2>/dev/null; then
  20 + exec {FD}<&0
  21 + restore=2
  22 + else
  23 + restore=1
  24 + fi
  25 +
  26 + if [[ ! -c /dev/tty ]]; then
  27 + exec <&2
  28 + else
  29 + exec </dev/tty
  30 + fi
  31 +fi
  32 +
  33 +# Run the command
  34 +"$@"
  35 +
  36 +# Restore FD state
  37 +(( restore == 1 )) && exec <&-
  38 +(( restore == 2 )) && exec <&$FD && exec {FD}<&-
  39 +
  40 +# vim: set filetype=zsh:
plugins/zsh-navigation-tools/zsh-navigation-tools.plugin.zsh
... ... @@ -0,0 +1,38 @@
  1 +#!/usr/bin/env zsh
  2 +
  3 +REPO_DIR="${0%/*}"
  4 +CONFIG_DIR="$HOME/.config/znt"
  5 +
  6 +#
  7 +# Copy configs
  8 +#
  9 +
  10 +if ! test -d "$HOME/.config"; then
  11 + mkdir "$HOME/.config"
  12 +fi
  13 +
  14 +if ! test -d "$CONFIG_DIR"; then
  15 + mkdir "$CONFIG_DIR"
  16 +fi
  17 +
  18 +set n-aliases.conf n-env.conf n-history.conf n-list.conf n-panelize.conf n-cd.conf n-functions.conf n-kill.conf n-options.conf
  19 +
  20 +for i; do
  21 + if ! test -f "$CONFIG_DIR/$i"; then
  22 + cp "$REPO_DIR/.config/znt/$i" "$CONFIG_DIR"
  23 + fi
  24 +done
  25 +
  26 +#
  27 +# Load functions
  28 +#
  29 +
  30 +autoload n-aliases n-cd n-env n-functions n-history n-kill n-list n-list-draw n-list-input n-options n-panelize
  31 +autoload znt-usetty-wrapper znt-history-widget znt-cd-widget znt-kill-widget
  32 +alias naliases=n-aliases ncd=n-cd nenv=n-env nfunctions=n-functions nhistory=n-history
  33 +alias nkill=n-kill noptions=n-options npanelize=n-panelize
  34 +
  35 +zle -N znt-history-widget
  36 +bindkey '^R' znt-history-widget
  37 +setopt AUTO_PUSHD HIST_IGNORE_DUPS PUSHD_IGNORE_DUPS
  38 +
tools/check_for_upgrade.sh
... ... @@ -46,7 +46,7 @@ then
46 46 else
47 47 echo "[Oh My Zsh] Would you like to check for updates? [Y/n]: \c"
48 48 read line
49   - if [ "$line" = Y ] || [ "$line" = y ] || [ -z "$line" ]; then
  49 + if [[ "$line" == Y* ]] || [[ "$line" == y* ]] || [ -z "$line" ]; then
50 50 _upgrade_zsh
51 51 else
52 52 _update_zsh_update
1 1 read -r -p "Are you sure you want to remove Oh My Zsh? [y/N] " confirmation
2   -if [ "$confirmation" != y ] && [ "$confirmation" != Y ]
3   -then
  2 +if [ "$confirmation" != y ] && [ "$confirmation" != Y ]; then
4 3 echo "Uninstall cancelled"
5 4 exit
6 5 fi
7 6  
8 7 echo "Removing ~/.oh-my-zsh"
9   -if [ -d ~/.oh-my-zsh ]
10   -then
  8 +if [ -d ~/.oh-my-zsh ]; then
11 9 rm -rf ~/.oh-my-zsh
12 10 fi
13 11  
14 12 echo "Looking for original zsh config..."
15   -if [ -f ~/.zshrc.pre-oh-my-zsh ] || [ -h ~/.zshrc.pre-oh-my-zsh ]
16   -then
  13 +if [ -f ~/.zshrc.pre-oh-my-zsh ] || [ -h ~/.zshrc.pre-oh-my-zsh ]; then
17 14 echo "Found ~/.zshrc.pre-oh-my-zsh -- Restoring to ~/.zshrc";
18 15  
19   - if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]
20   - then
21   - ZSHRC_SAVE=".zshrc.omz-uninstalled-`date +%Y%m%d%H%M%S`";
  16 + if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
  17 + ZSHRC_SAVE=".zshrc.omz-uninstalled-$(date +%Y%m%d%H%M%S)";
22 18 echo "Found ~/.zshrc -- Renaming to ~/${ZSHRC_SAVE}";
23   - mv ~/.zshrc ~/${ZSHRC_SAVE};
  19 + mv ~/.zshrc ~/"${ZSHRC_SAVE}";
24 20 fi
25 21  
26 22 mv ~/.zshrc.pre-oh-my-zsh ~/.zshrc;
27 23  
28   - source ~/.zshrc;
  24 + echo "Your original zsh config was restored. Please restart your session."
29 25 else
30   - if hash chsh >/dev/null 2>&1
31   - then
  26 + if hash chsh >/dev/null 2>&1; then
32 27 echo "Switching back to bash"
33 28 chsh -s /bin/bash
34 29 else