fd534eb979aa296dd94a78040e77559a4c4dcdde
- Author
- Marc Cornellà <marc.cornella@live.com>
- Committer
- Marc Cornellà <marc.cornella@live.com>
- Date
Message
Diff
This diff is truncated to protect this page.
1diff --git a/plugins/history-substring-search/README.md b/plugins/history-substring-search/README.md
2index 0ffb21351b2b637c7a7788205c2a02ee2593a711..6d8b56425affe877d1c102186eb042895568fb3e 100644
3--- a/plugins/history-substring-search/README.md
4+++ b/plugins/history-substring-search/README.md
5@@ -1,25 +1,44 @@
6-zsh-history-substring-search
7-==============================================================================
8+# zsh-history-substring-search
9
10 This is a clean-room implementation of the [Fish shell][1]'s history search
11-feature, where you can type in any part of any previously entered command
12-and press the UP and DOWN arrow keys to cycle through the matching commands.
13-You can also use K and J in VI mode or ^P and ^N in EMACS mode for the same.
14-
15-[1]: https://fishshell.com
16-[2]: https://www.zsh.org/mla/users/2009/msg00818.html
17-[3]: https://sourceforge.net/projects/fizsh/
18-[4]: https://github.com/ohmyzsh/ohmyzsh/pull/215
19+feature, where you can type in any part of any command from history and then
20+press chosen keys, such as the UP and DOWN arrows, to cycle through matches.
21+
22+[1]: http://fishshell.com
23+[2]: http://www.zsh.org/mla/users/2009/msg00818.html
24+[3]: http://sourceforge.net/projects/fizsh/
25+[4]: https://github.com/robbyrussell/oh-my-zsh/pull/215
26 [5]: https://github.com/zsh-users/zsh-history-substring-search
27 [6]: https://github.com/zsh-users/zsh-syntax-highlighting
28
29-------------------------------------------------------------------------------
30+
31 Requirements
32 ------------------------------------------------------------------------------
33
34 * [ZSH](http://zsh.sourceforge.net) 4.3 or newer
35
36+Install
37 ------------------------------------------------------------------------------
38+
39+Using the [Homebrew]( https://brew.sh ) package manager:
40+
41+ brew install zsh-history-substring-search
42+ echo 'source /usr/local/share/zsh-history-substring-search/zsh-history-substring-search.zsh' >> ~/.zshrc
43+
44+Using [Oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh):
45+
46+1. Clone this repository in oh-my-zsh's plugins directory:
47+
48+ git clone https://github.com/zsh-users/zsh-history-substring-search ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-history-substring-search
49+
50+2. Activate the plugin in `~/.zshrc`:
51+
52+ plugins=( [plugins...] history-substring-search)
53+
54+3. Source `~/.zshrc` to take changes into account:
55+
56+ source ~/.zshrc
57+
58 Usage
59 ------------------------------------------------------------------------------
60
61@@ -33,117 +52,147 @@ Usage
62 % source zsh-syntax-highlighting.zsh
63 % source zsh-history-substring-search.zsh
64
65-2. Bind keyboard shortcuts to this script's functions:
66+2. Bind keyboard shortcuts to this script's functions.
67+
68+ Users typically bind their UP and DOWN arrow keys to this script, thus:
69+ * Run `cat -v` in your favorite terminal emulator to observe key codes.
70+ (**NOTE:** In some cases, `cat -v` shows the wrong key codes. If the
71+ key codes shown by `cat -v` don't work for you, press `<C-v><UP>` and
72+ `<C-v><DOWN>` at your ZSH command line prompt for correct key codes.)
73+ * Press the UP arrow key and observe what is printed in your terminal.
74+ * Press the DOWN arrow key and observe what is printed in your terminal.
75+ * Press the Control and C keys simultaneously to terminate the `cat -v`.
76+ * Use your observations from the previous steps to create key bindings.
77+ For example, if you observed `^[[A` for UP and `^[[B` for DOWN, then:
78+
79+ bindkey '^[[A' history-substring-search-up
80+ bindkey '^[[B' history-substring-search-down
81
82- # bind UP and DOWN arrow keys
83- zmodload zsh/terminfo
84- bindkey "$terminfo[kcuu1]" history-substring-search-up
85- bindkey "$terminfo[kcud1]" history-substring-search-down
86+ However, if the observed values don't work, you can try using terminfo:
87
88- # bind UP and DOWN arrow keys (compatibility fallback
89- # for Ubuntu 12.04, Fedora 21, and MacOSX 10.9 users)
90- bindkey '^[[A' history-substring-search-up
91- bindkey '^[[B' history-substring-search-down
92+ bindkey "$terminfo[kcuu1]" history-substring-search-up
93+ bindkey "$terminfo[kcud1]" history-substring-search-down
94
95- # bind P and N for EMACS mode
96- bindkey -M emacs '^P' history-substring-search-up
97- bindkey -M emacs '^N' history-substring-search-down
98+ You might also want to bind the Control-P/N keys for use in EMACS mode:
99
100- # bind k and j for VI mode
101- bindkey -M vicmd 'k' history-substring-search-up
102- bindkey -M vicmd 'j' history-substring-search-down
103+ bindkey -M emacs '^P' history-substring-search-up
104+ bindkey -M emacs '^N' history-substring-search-down
105diff --git a/plugins/history-substring-search/history-substring-search.plugin.zsh b/plugins/history-substring-search/history-substring-search.plugin.zsh
106index 7883a65f39a3e3db9dcabe5e5411482fc5313404..05bdbcd75f7f263f9c2b820c270e530d021b89cb 100644
107--- a/plugins/history-substring-search/history-substring-search.plugin.zsh
108+++ b/plugins/history-substring-search/history-substring-search.plugin.zsh
109@@ -1,20 +1,9 @@
110-# This file integrates the zsh-history-substring-search script into oh-my-zsh.
111-
112-source "${0:r:r}.zsh"
113-
114-if test "$CASE_SENSITIVE" = true; then
115- unset HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS
116-fi
117-
118-if test "$DISABLE_COLOR" = true; then
119- unset HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
120- unset HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
121-fi
122+0=${(%):-%N}
123+source ${0:A:h}/zsh-history-substring-search.zsh
124
125
126 # Bind terminal-specific up and down keys
127-# Bind in both emacs and vi modes so it works in both, and is not
128-# sensitive to whether this is loaded before or after the vi-mode plugin
129+
130 if [[ -n "$terminfo[kcuu1]" ]]; then
131 bindkey -M emacs "$terminfo[kcuu1]" history-substring-search-up
132 bindkey -M viins "$terminfo[kcuu1]" history-substring-search-up
133diff --git a/plugins/history-substring-search/history-substring-search.zsh b/plugins/history-substring-search/history-substring-search.zsh
134index 3b8afd31767fe93c17d60bf83284839e3e10b412..a791cc4da950626896afed68c582f9a624d5f2d5 100644
135--- a/plugins/history-substring-search/history-substring-search.zsh
136+++ b/plugins/history-substring-search/history-substring-search.zsh
137@@ -6,6 +6,8 @@
138 # Copyright (c) 2011 Suraj N. Kurapati
139 # Copyright (c) 2011 Sorin Ionescu
140 # Copyright (c) 2011 Vincent Guerci
141+# Copyright (c) 2016 Geza Lore
142+# Copyright (c) 2017 Bengt Brodersen
143 # All rights reserved.
144 #
145 # Redistribution and use in source and binary forms, with or without
146@@ -38,12 +40,30 @@
147 ##############################################################################
148
149 #-----------------------------------------------------------------------------
150-# configuration variables
151+# declare global configuration variables
152 #-----------------------------------------------------------------------------
153
154-HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold'
155-HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'
156-HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i'
157+typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold'
158+typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'
159+typeset -g HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i'
160+typeset -g HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=''
161+typeset -g HISTORY_SUBSTRING_SEARCH_FUZZY=''
162+
163+#-----------------------------------------------------------------------------
164+# declare internal global variables
165+#-----------------------------------------------------------------------------
166+
167+typeset -g BUFFER MATCH MBEGIN MEND CURSOR
168+typeset -g _history_substring_search_refresh_display
169+typeset -g _history_substring_search_query_highlight
170+typeset -g _history_substring_search_result
171+typeset -g _history_substring_search_query
172+typeset -g -a _history_substring_search_query_parts
173+typeset -g -a _history_substring_search_raw_matches
174+typeset -g -i _history_substring_search_raw_match_index
175+typeset -g -a _history_substring_search_matches
176+typeset -g -i _history_substring_search_match_index
177+typeset -g -A _history_substring_search_unique_filter
178
179 #-----------------------------------------------------------------------------
180 # the main ZLE widgets
181@@ -180,62 +200,104 @@ _history-substring-search-begin() {
182 _history_substring_search_query_highlight=
183
184 #
185- # Continue using the previous $_history_substring_search_result by default,
186- # unless the current query was cleared or a new/different query was entered.
187+ # If the buffer is the same as the previously displayed history substring
188+ # search result, then just keep stepping through the match list. Otherwise
189+ # start a new search.
190 #
191- if [[ -z $BUFFER || $BUFFER != $_history_substring_search_result ]]; then
192- #
193- # For the purpose of highlighting we will also keep
194- # a version without doubly-escaped meta characters.
195- #
196- _history_substring_search_query=$BUFFER
197+ if [[ -n $BUFFER && $BUFFER == ${_history_substring_search_result:-} ]]; then
198+ return;
199+ fi
200
201+ #
202+ # Clear the previous result.
203+ #
204+ _history_substring_search_result=''
205+
206+ if [[ -z $BUFFER ]]; then
207 #
208- # $BUFFER contains the text that is in the command-line currently.
209- # we put an extra "\\" before meta characters such as "\(" and "\)",
210- # so that they become "\\\(" and "\\\)".
211+ # If the buffer is empty, we will just act like up-history/down-history
212+ # in ZSH, so we do not need to actually search the history. This should
213+ # speed things up a little.
214 #
215- _history_substring_search_query_escaped=${BUFFER//(#m)[\][()|\\*?#<>~^]/\\$MATCH}
216+ _history_substring_search_query=
217+ _history_substring_search_query_parts=()
218+ _history_substring_search_raw_matches=()
219
220+ else
221 #
222- # Find all occurrences of the search query in the history file.
223- #
224- # (k) returns the "keys" (history index numbers) instead of the values
225- # (Oa) reverses the order, because (R) returns results reversed.
226+ # For the purpose of highlighting we keep a copy of the original
227+ # query string.
228 #
229- _history_substring_search_matches=(${(kOa)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]})
230+ _history_substring_search_query=$BUFFER
231
232 #
233- # Define the range of values that $_history_substring_search_match_index
234- # can take: [0, $_history_substring_search_matches_count_plus].
235+ # compose search pattern
236 #