|
49 | 49 | : ${HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=''} |
50 | 50 | : ${HISTORY_SUBSTRING_SEARCH_FUZZY=''} |
51 | 51 | : ${HISTORY_SUBSTRING_SEARCH_PREFIXED=''} |
| 52 | +: ${HISTORY_SUBSTRING_SEARCH_MATCH_FUNCTION=''} |
52 | 53 |
|
53 | 54 | #----------------------------------------------------------------------------- |
54 | 55 | # declare internal global variables |
@@ -245,6 +246,30 @@ if [[ $+functions[_zsh_highlight] -eq 0 ]]; then |
245 | 246 | unfunction _history-substring-search-function-callable |
246 | 247 | fi |
247 | 248 |
|
| 249 | +_history-substring-get-raw-matches() { |
| 250 | + # |
| 251 | + # Escape and join query parts with wildcard character '*' as separator |
| 252 | + # `(j:CHAR:)` join array to string with CHAR as separator |
| 253 | + # |
| 254 | + local search_pattern="${(j:*:)_history_substring_search_query_parts[@]//(#m)[\][()|\\*?#<>~^]/\\$MATCH}*" |
| 255 | + |
| 256 | + # |
| 257 | + # Support anchoring history search to the beginning of the command |
| 258 | + # |
| 259 | + if [[ -z $HISTORY_SUBSTRING_SEARCH_PREFIXED ]]; then |
| 260 | + search_pattern="*${search_pattern}" |
| 261 | + fi |
| 262 | + |
| 263 | + # |
| 264 | + # Find all occurrences of the search pattern in the history file. |
| 265 | + # |
| 266 | + # (k) returns the "keys" (history index numbers) instead of the values |
| 267 | + # (R) returns values in reverse older, so the index of the youngest |
| 268 | + # matching history entry is at the head of the list. |
| 269 | + # |
| 270 | + _history_substring_search_raw_matches=(${(k)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)${search_pattern}]}) |
| 271 | +} |
| 272 | + |
248 | 273 | _history-substring-search-begin() { |
249 | 274 | setopt localoptions extendedglob |
250 | 275 |
|
@@ -294,27 +319,7 @@ _history-substring-search-begin() { |
294 | 319 | _history_substring_search_query_parts=(${==_history_substring_search_query}) |
295 | 320 | fi |
296 | 321 |
|
297 | | - # |
298 | | - # Escape and join query parts with wildcard character '*' as seperator |
299 | | - # `(j:CHAR:)` join array to string with CHAR as seperator |
300 | | - # |
301 | | - local search_pattern="${(j:*:)_history_substring_search_query_parts[@]//(#m)[\][()|\\*?#<>~^]/\\$MATCH}*" |
302 | | - |
303 | | - # |
304 | | - # Support anchoring history search to the beginning of the command |
305 | | - # |
306 | | - if [[ -z $HISTORY_SUBSTRING_SEARCH_PREFIXED ]]; then |
307 | | - search_pattern="*${search_pattern}" |
308 | | - fi |
309 | | - |
310 | | - # |
311 | | - # Find all occurrences of the search pattern in the history file. |
312 | | - # |
313 | | - # (k) returns the "keys" (history index numbers) instead of the values |
314 | | - # (R) returns values in reverse older, so the index of the youngest |
315 | | - # matching history entry is at the head of the list. |
316 | | - # |
317 | | - _history_substring_search_raw_matches=(${(k)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)${search_pattern}]}) |
| 322 | + ${HISTORY_SUBSTRING_SEARCH_MATCH_FUNCTION:_history-substring-get-raw-matches} |
318 | 323 | fi |
319 | 324 |
|
320 | 325 | # |
@@ -655,7 +660,13 @@ _history-substring-search-found() { |
655 | 660 | # 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
656 | 661 | # to highlight the current buffer. |
657 | 662 | # |
658 | | - BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]] |
| 663 | + if [[ -n $HISTORY_SUBSTRING_SEARCH_MATCH_FUNCTION ]]; then |
| 664 | + # Custom provider functions provide entries directly |
| 665 | + BUFFER=$_history_substring_search_matches[$_history_substring_search_match_index] |
| 666 | + else |
| 667 | + # Builtin search provides the index to matching history entries |
| 668 | + BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]] |
| 669 | + fi |
659 | 670 | _history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND |
660 | 671 | } |
661 | 672 |
|
|
0 commit comments