Remove unnecessary explicit subword-mode use from isearch
[emacs.git] / lisp / eshell / em-hist.el
blob62e2f57d0fd5374aa83a96b74c0344ef4fdda39c
1 ;;; em-hist.el --- history list management -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2018 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; Eshell's history facility imitates the syntax used by bash
25 ;; ([(bash)History Interaction]). Thus:
27 ;; !ls ; repeat the last command beginning with 'ls'
28 ;; !?ls ; repeat the last command containing ls
29 ;; echo !ls:2 ; echo the second arg of the last 'ls' command
30 ;; !ls<tab> ; complete against all possible words in this
31 ;; ; position, by looking at the history list
32 ;; !ls<C-c SPC> ; expand any matching history input at point
34 ;; Also, most of `comint-mode's keybindings are accepted:
36 ;; M-r ; search backward for a previous command by regexp
37 ;; M-s ; search forward for a previous command by regexp
38 ;; M-p ; access the last command entered, repeatable
39 ;; M-n ; access the first command entered, repeatable
41 ;; C-c M-r ; using current input, find a matching command thus, with
42 ;; ; 'ls' as the current input, it will go back to the same
43 ;; ; command that '!ls' would have selected
44 ;; C-c M-s ; same, but in reverse order
46 ;; Note that some of these keybindings are only available if the
47 ;; `eshell-rebind' is not in use, in which case M-p does what C-c M-r
48 ;; normally would do, and C-p is used instead of M-p. It may seem
49 ;; confusing, but the intention is to make the most useful
50 ;; functionality the most easily accessible. If `eshell-rebind' is
51 ;; not being used, history navigation will use comint's keybindings;
52 ;; if it is, history navigation tries to use similar keybindings to
53 ;; bash. This is all configurable, of course.
55 ;;; Code:
57 (eval-when-compile (require 'cl-lib))
58 (eval-when-compile (require 'subr-x)) ; `string-blank-p'
60 (require 'ring)
61 (require 'esh-opt)
62 (require 'em-pred)
63 (require 'eshell)
65 ;;;###autoload
66 (progn
67 (defgroup eshell-hist nil
68 "This module provides command history management."
69 :tag "History list management"
70 :group 'eshell-module))
72 ;;; User Variables:
74 (defcustom eshell-hist-load-hook nil
75 "A list of functions to call when loading `eshell-hist'."
76 :version "24.1" ; removed eshell-hist-initialize
77 :type 'hook
78 :group 'eshell-hist)
80 (defcustom eshell-hist-unload-hook
81 (list
82 (function
83 (lambda ()
84 (remove-hook 'kill-emacs-hook 'eshell-save-some-history))))
85 "A hook that gets run when `eshell-hist' is unloaded."
86 :type 'hook
87 :group 'eshell-hist)
89 (defcustom eshell-history-file-name
90 (expand-file-name "history" eshell-directory-name)
91 "If non-nil, name of the file to read/write input history.
92 See also `eshell-read-history' and `eshell-write-history'.
93 If it is nil, Eshell will use the value of HISTFILE."
94 :type '(choice (const :tag "Use HISTFILE" nil)
95 file)
96 :group 'eshell-hist)
98 (defcustom eshell-history-size 128
99 "Size of the input history ring. If nil, use envvar HISTSIZE."
100 :type '(choice (const :tag "Use HISTSIZE" nil)
101 integer)
102 :group 'eshell-hist)
104 (defcustom eshell-hist-ignoredups nil
105 "If non-nil, don't add input matching the last on the input ring.
106 This mirrors the optional behavior of bash."
107 :type 'boolean
108 :group 'eshell-hist)
110 (defcustom eshell-save-history-on-exit t
111 "Determine if history should be automatically saved.
112 History is always preserved after sanely exiting an Eshell buffer.
113 However, when Emacs is being shut down, this variable determines
114 whether to prompt the user.
115 If set to nil, it means never save history on termination of Emacs.
116 If set to `ask', ask if any Eshell buffers are open at exit time.
117 If set to t, history will always be saved, silently."
118 :type '(choice (const :tag "Never" nil)
119 (const :tag "Ask" ask)
120 (const :tag "Always save" t))
121 :group 'eshell-hist)
123 (defcustom eshell-input-filter 'eshell-input-filter-default
124 "Predicate for filtering additions to input history.
125 Takes one argument, the input. If non-nil, the input may be saved on
126 the input history list. Default is to save anything that isn't all
127 whitespace."
128 :type '(radio (function-item eshell-input-filter-default)
129 (function-item eshell-input-filter-initial-space)
130 (function :tag "Other function"))
131 :group 'eshell-hist)
133 (put 'eshell-input-filter 'risky-local-variable t)
135 (defcustom eshell-hist-match-partial t
136 "If non-nil, movement through history is constrained by current input.
137 Otherwise, typing <M-p> and <M-n> will always go to the next history
138 element, regardless of any text on the command line. In that case,
139 <C-c M-r> and <C-c M-s> still offer that functionality."
140 :type 'boolean
141 :group 'eshell-hist)
143 (defcustom eshell-hist-move-to-end t
144 "If non-nil, move to the end of the buffer before cycling history."
145 :type 'boolean
146 :group 'eshell-hist)
148 (defcustom eshell-hist-event-designator
149 "^!\\(!\\|-?[0-9]+\\|\\??[^:^$%*?]+\\??\\|#\\)"
150 "The regexp used to identifier history event designators."
151 :type 'regexp
152 :group 'eshell-hist)
154 (defcustom eshell-hist-word-designator
155 "^:?\\([0-9]+\\|[$^%*]\\)?\\(\\*\\|-[0-9]*\\|[$^%*]\\)?"
156 "The regexp used to identify history word designators."
157 :type 'regexp
158 :group 'eshell-hist)
160 (defcustom eshell-hist-modifier
161 "^\\(:\\([hretpqx&g]\\|s/\\([^/]*\\)/\\([^/]*\\)/\\)\\)*"
162 "The regexp used to identity history modifiers."
163 :type 'regexp
164 :group 'eshell-hist)
166 (defcustom eshell-hist-rebind-keys-alist
167 '(([(control ?p)] . eshell-previous-input)
168 ([(control ?n)] . eshell-next-input)
169 ([(control up)] . eshell-previous-input)
170 ([(control down)] . eshell-next-input)
171 ([(control ?r)] . eshell-isearch-backward)
172 ([(control ?s)] . eshell-isearch-forward)
173 ([(meta ?r)] . eshell-previous-matching-input)
174 ([(meta ?s)] . eshell-next-matching-input)
175 ([(meta ?p)] . eshell-previous-matching-input-from-input)
176 ([(meta ?n)] . eshell-next-matching-input-from-input)
177 ([up] . eshell-previous-matching-input-from-input)
178 ([down] . eshell-next-matching-input-from-input))
179 "History keys to bind differently if point is in input text."
180 :type '(repeat (cons (vector :tag "Keys to bind"
181 (repeat :inline t sexp))
182 (function :tag "Command")))
183 :group 'eshell-hist)
185 ;;; Internal Variables:
187 (defvar eshell-history-ring nil)
188 (defvar eshell-history-index nil)
189 (defvar eshell-matching-input-from-input-string "")
190 (defvar eshell-save-history-index nil)
192 (defvar eshell-isearch-map
193 (let ((map (copy-keymap isearch-mode-map)))
194 (define-key map [(control ?m)] 'eshell-isearch-return)
195 (define-key map [return] 'eshell-isearch-return)
196 (define-key map [(control ?r)] 'eshell-isearch-repeat-backward)
197 (define-key map [(control ?s)] 'eshell-isearch-repeat-forward)
198 (define-key map [(control ?g)] 'eshell-isearch-abort)
199 (define-key map [backspace] 'eshell-isearch-delete-char)
200 (define-key map [delete] 'eshell-isearch-delete-char)
201 (define-key map "\C-c\C-c" 'eshell-isearch-cancel)
202 map)
203 "Keymap used in isearch in Eshell.")
205 (defvar eshell-rebind-keys-alist)
207 ;;; Functions:
209 (defun eshell-input-filter-default (input)
210 "Do not add blank input to input history.
211 Returns non-nil if INPUT is blank."
212 (not (string-blank-p input)))
214 (defun eshell-input-filter-initial-space (input)
215 "Do not add input beginning with empty space to history.
216 Returns nil if INPUT is prepended by blank space, otherwise non-nil."
217 (not (string-match-p "\\`\\s-+" input)))
219 (defun eshell-hist-initialize ()
220 "Initialize the history management code for one Eshell buffer."
221 (when (eshell-using-module 'eshell-cmpl)
222 (add-hook 'pcomplete-try-first-hook
223 'eshell-complete-history-reference nil t))
225 (if (and (eshell-using-module 'eshell-rebind)
226 (not eshell-non-interactive-p))
227 (let ((rebind-alist eshell-rebind-keys-alist))
228 (make-local-variable 'eshell-rebind-keys-alist)
229 (setq eshell-rebind-keys-alist
230 (append rebind-alist eshell-hist-rebind-keys-alist))
231 (set (make-local-variable 'search-invisible) t)
232 (set (make-local-variable 'search-exit-option) t)
233 (add-hook 'isearch-mode-hook
234 (function
235 (lambda ()
236 (if (>= (point) eshell-last-output-end)
237 (setq overriding-terminal-local-map
238 eshell-isearch-map)))) nil t)
239 (add-hook 'isearch-mode-end-hook
240 (function
241 (lambda ()
242 (setq overriding-terminal-local-map nil))) nil t))
243 (define-key eshell-mode-map [up] 'eshell-previous-matching-input-from-input)
244 (define-key eshell-mode-map [down] 'eshell-next-matching-input-from-input)
245 (define-key eshell-mode-map [(control up)] 'eshell-previous-input)
246 (define-key eshell-mode-map [(control down)] 'eshell-next-input)
247 (define-key eshell-mode-map [(meta ?r)] 'eshell-previous-matching-input)
248 (define-key eshell-mode-map [(meta ?s)] 'eshell-next-matching-input)
249 (define-key eshell-command-map [(meta ?r)]
250 'eshell-previous-matching-input-from-input)
251 (define-key eshell-command-map [(meta ?s)]
252 'eshell-next-matching-input-from-input)
253 (if eshell-hist-match-partial
254 (progn
255 (define-key eshell-mode-map [(meta ?p)]
256 'eshell-previous-matching-input-from-input)
257 (define-key eshell-mode-map [(meta ?n)]
258 'eshell-next-matching-input-from-input)
259 (define-key eshell-command-map [(meta ?p)] 'eshell-previous-input)
260 (define-key eshell-command-map [(meta ?n)] 'eshell-next-input))
261 (define-key eshell-mode-map [(meta ?p)] 'eshell-previous-input)
262 (define-key eshell-mode-map [(meta ?n)] 'eshell-next-input)
263 (define-key eshell-command-map [(meta ?p)]
264 'eshell-previous-matching-input-from-input)
265 (define-key eshell-command-map [(meta ?n)]
266 'eshell-next-matching-input-from-input)))
268 (make-local-variable 'eshell-history-size)
269 (or eshell-history-size
270 (let ((hsize (getenv "HISTSIZE")))
271 (setq eshell-history-size
272 (if (and (stringp hsize)
273 (integerp (setq hsize (string-to-number hsize)))
274 (> hsize 0))
275 hsize
276 128))))
278 (make-local-variable 'eshell-history-file-name)
279 (or eshell-history-file-name
280 (setq eshell-history-file-name (getenv "HISTFILE")))
282 (make-local-variable 'eshell-history-index)
283 (make-local-variable 'eshell-save-history-index)
285 (if (minibuffer-window-active-p (selected-window))
286 (set (make-local-variable 'eshell-save-history-on-exit) nil)
287 (set (make-local-variable 'eshell-history-ring) nil)
288 (if eshell-history-file-name
289 (eshell-read-history nil t))
291 (add-hook 'eshell-exit-hook 'eshell-write-history nil t))
293 (unless eshell-history-ring
294 (setq eshell-history-ring (make-ring eshell-history-size)))
296 (add-hook 'eshell-exit-hook 'eshell-write-history nil t)
298 (add-hook 'kill-emacs-hook 'eshell-save-some-history)
300 (make-local-variable 'eshell-input-filter-functions)
301 (add-hook 'eshell-input-filter-functions 'eshell-add-to-history nil t)
303 (define-key eshell-command-map [(control ?l)] 'eshell-list-history)
304 (define-key eshell-command-map [(control ?x)] 'eshell-get-next-from-history))
306 (defun eshell-save-some-history ()
307 "Save the history for any open Eshell buffers."
308 (dolist (buf (buffer-list))
309 (if (buffer-live-p buf)
310 (with-current-buffer buf
311 (if (and eshell-mode
312 eshell-history-file-name
313 eshell-save-history-on-exit
314 (or (eq eshell-save-history-on-exit t)
315 (y-or-n-p
316 (format-message
317 "Save input history for Eshell buffer `%s'? "
318 (buffer-name buf)))))
319 (eshell-write-history))))))
321 (defun eshell/history (&rest args)
322 "List in help buffer the buffer's input history."
323 (eshell-init-print-buffer)
324 (eshell-eval-using-options
325 "history" args
326 '((?r "read" nil read-history
327 "read from history file to current history list")
328 (?w "write" nil write-history
329 "write current history list to history file")
330 (?a "append" nil append-history
331 "append current history list to history file")
332 (?h "help" nil nil "display this usage message")
333 :usage "[n] [-rwa [filename]]"
334 :post-usage
335 "When Eshell is started, history is read from `eshell-history-file-name'.
336 This is also the location where history info will be saved by this command,
337 unless a different file is specified on the command line.")
338 (and (or (not (ring-p eshell-history-ring))
339 (ring-empty-p eshell-history-ring))
340 (error "No history"))
341 (let (length file)
342 (when (and args (string-match "^[0-9]+$" (car args)))
343 (setq length (min (eshell-convert (car args))
344 (ring-length eshell-history-ring))
345 args (cdr args)))
346 (and length
347 (or read-history write-history append-history)
348 (error "history: extra arguments"))
349 (when (and args (stringp (car args)))
350 (setq file (car args)
351 args (cdr args)))
352 (cond
353 (read-history (eshell-read-history file))
354 (write-history (eshell-write-history file))
355 (append-history (eshell-write-history file t))
357 (let* ((index (1- (or length (ring-length eshell-history-ring))))
358 (ref (- (ring-length eshell-history-ring) index)))
359 ;; We have to build up a list ourselves from the ring vector.
360 (while (>= index 0)
361 (eshell-buffered-print
362 (format "%5d %s\n" ref (eshell-get-history index)))
363 (setq index (1- index)
364 ref (1+ ref)))))))
365 (eshell-flush)
366 nil))
368 (defun eshell-put-history (input &optional ring at-beginning)
369 "Put a new input line into the history ring."
370 (unless ring (setq ring eshell-history-ring))
371 (if at-beginning
372 (ring-insert-at-beginning ring input)
373 (ring-insert ring input)))
375 (defun eshell-get-history (index &optional ring)
376 "Get an input line from the history ring."
377 (ring-ref (or ring eshell-history-ring) index))
379 (defun eshell-add-input-to-history (input)
380 "Add the string INPUT to the history ring.
381 Input is entered into the input history ring, if the value of
382 variable `eshell-input-filter' returns non-nil when called on the
383 input."
384 (if (and (funcall eshell-input-filter input)
385 (or (null eshell-hist-ignoredups)
386 (not (ring-p eshell-history-ring))
387 (ring-empty-p eshell-history-ring)
388 (not (string-equal (eshell-get-history 0) input))))
389 (eshell-put-history input))
390 (setq eshell-save-history-index eshell-history-index)
391 (setq eshell-history-index nil))
393 (defun eshell-add-command-to-history ()
394 "Add the command entered at `eshell-command's prompt to the history ring.
395 The command is added to the input history ring, if the value of
396 variable `eshell-input-filter' returns non-nil when called on the
397 command.
399 This function is supposed to be called from the minibuffer, presumably
400 as a minibuffer-exit-hook."
401 (eshell-add-input-to-history
402 (buffer-substring (minibuffer-prompt-end) (point-max))))
404 (defun eshell-add-to-history ()
405 "Add last Eshell command to the history ring.
406 The command is entered into the input history ring, if the value of
407 variable `eshell-input-filter' returns non-nil when called on the
408 command."
409 (when (> (1- eshell-last-input-end) eshell-last-input-start)
410 (let ((input (buffer-substring eshell-last-input-start
411 (1- eshell-last-input-end))))
412 (eshell-add-input-to-history input))))
414 (defun eshell-read-history (&optional filename silent)
415 "Sets the buffer's `eshell-history-ring' from a history file.
416 The name of the file is given by the variable
417 `eshell-history-file-name'. The history ring is of size
418 `eshell-history-size', regardless of file size. If
419 `eshell-history-file-name' is nil this function does nothing.
421 If the optional argument SILENT is non-nil, we say nothing about a
422 failure to read the history file.
424 This function is useful for major mode commands and mode hooks.
426 The structure of the history file should be one input command per
427 line, with the most recent command last. See also
428 `eshell-hist-ignoredups' and `eshell-write-history'."
429 (let ((file (or filename eshell-history-file-name)))
430 (cond
431 ((or (null file)
432 (equal file ""))
433 nil)
434 ((not (file-readable-p file))
435 (or silent
436 (message "Cannot read history file %s" file)))
438 (let* ((count 0)
439 (size eshell-history-size)
440 (ring (make-ring size))
441 (ignore-dups eshell-hist-ignoredups))
442 (with-temp-buffer
443 (insert-file-contents file)
444 ;; Watch for those date stamps in history files!
445 (goto-char (point-max))
446 (while (and (< count size)
447 (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$"
448 nil t))
449 (let ((history (match-string 1)))
450 (if (or (null ignore-dups)
451 (ring-empty-p ring)
452 (not (string-equal (ring-ref ring 0) history)))
453 (ring-insert-at-beginning
454 ring (subst-char-in-string ?\177 ?\n history))))
455 (setq count (1+ count))))
456 (setq eshell-history-ring ring
457 eshell-history-index nil))))))
459 (defun eshell-write-history (&optional filename append)
460 "Writes the buffer's `eshell-history-ring' to a history file.
461 The name of the file is given by the variable
462 `eshell-history-file-name'. The original contents of the file are
463 lost if `eshell-history-ring' is not empty. If
464 `eshell-history-file-name' is nil this function does nothing.
466 Useful within process sentinels.
468 See also `eshell-read-history'."
469 (let ((file (or filename eshell-history-file-name)))
470 (cond
471 ((or (null file)
472 (equal file "")
473 (null eshell-history-ring)
474 (ring-empty-p eshell-history-ring))
475 nil)
476 ((not (file-writable-p file))
477 (message "Cannot write history file %s" file))
479 (let* ((ring eshell-history-ring)
480 (index (ring-length ring)))
481 ;; Write it all out into a buffer first. Much faster, but
482 ;; messier, than writing it one line at a time.
483 (with-temp-buffer
484 (while (> index 0)
485 (setq index (1- index))
486 (let ((start (point)))
487 ;; Remove properties before inserting, to avoid trouble
488 ;; with read-only strings (Bug#28700).
489 (insert (substring-no-properties (ring-ref ring index)) ?\n)
490 (subst-char-in-region start (1- (point)) ?\n ?\177)))
491 (eshell-with-private-file-modes
492 (write-region (point-min) (point-max) file append
493 'no-message))))))))
495 (defun eshell-list-history ()
496 "List in help buffer the buffer's input history."
497 (interactive)
498 (let (prefix prelen)
499 (save-excursion
500 (if (re-search-backward "!\\(.+\\)" (line-beginning-position) t)
501 (setq prefix (match-string 1)
502 prelen (length prefix))))
503 (if (or (not (ring-p eshell-history-ring))
504 (ring-empty-p eshell-history-ring))
505 (message "No history")
506 (let ((history nil)
507 (history-buffer " *Input History*")
508 (index (1- (ring-length eshell-history-ring)))
509 (conf (current-window-configuration)))
510 ;; We have to build up a list ourselves from the ring vector.
511 (while (>= index 0)
512 (let ((hist (eshell-get-history index)))
513 (if (or (not prefix)
514 (and (>= (length hist) prelen)
515 (string= (substring hist 0 prelen) prefix)))
516 (setq history (cons hist history))))
517 (setq index (1- index)))
518 ;; Change "completion" to "history reference"
519 ;; to make the display accurate.
520 (with-output-to-temp-buffer history-buffer
521 (display-completion-list
522 (completion-hilit-commonality history (length prefix)))
523 (set-buffer history-buffer)
524 (forward-line 3)
525 (while (search-backward "completion" nil 'move)
526 (replace-match "history reference")))
527 (eshell-redisplay)
528 (message "Hit space to flush")
529 (let ((ch (read-event)))
530 (if (eq ch ?\ )
531 (set-window-configuration conf)
532 (push ch unread-command-events)))))))
534 (defun eshell-hist-word-reference (ref)
535 "Return the word designator index referred to by REF."
536 (cond
537 ((string-match "^[0-9]+$" ref)
538 (string-to-number ref))
539 ((string= "^" ref) 1)
540 ((string= "$" ref) nil)
541 ((string= "%" ref)
542 (error "`%%' history word designator not yet implemented"))))
544 (defun eshell-hist-parse-arguments (&optional b e)
545 "Parse current command arguments in a history-code-friendly way."
546 (let ((end (or e (point)))
547 (begin (or b (save-excursion (eshell-bol) (point))))
548 (posb (list t))
549 (pose (list t))
550 (textargs (list t))
551 hist args)
552 (unless (catch 'eshell-incomplete
553 (ignore
554 (setq args (eshell-parse-arguments begin end))))
555 (save-excursion
556 (goto-char begin)
557 (while (< (point) end)
558 (if (get-text-property (point) 'arg-begin)
559 (nconc posb (list (point))))
560 (if (get-text-property (point) 'arg-end)
561 (nconc pose
562 (list (if (= (1+ (point)) end)
563 (1+ (point))
564 (point)))))
565 (forward-char))
566 (setq posb (cdr posb)
567 pose (cdr pose))
568 (cl-assert (= (length posb) (length args)))
569 (cl-assert (<= (length posb) (length pose))))
570 (setq hist (buffer-substring-no-properties begin end))
571 (let ((b posb) (e pose))
572 (while b
573 (nconc textargs
574 (list (substring hist (- (car b) begin)
575 (- (car e) begin))))
576 (setq b (cdr b)
577 e (cdr e))))
578 (setq textargs (cdr textargs))
579 (cl-assert (= (length textargs) (length args)))
580 (list textargs posb pose))))
582 (defun eshell-expand-history-references (beg end)
583 "Parse and expand any history references in current input."
584 (let ((result (eshell-hist-parse-arguments beg end))
585 (full-line (buffer-substring-no-properties beg end)))
586 (when result
587 (let ((textargs (nreverse (nth 0 result)))
588 (posb (nreverse (nth 1 result)))
589 (pose (nreverse (nth 2 result)))
590 (full-line-subst (eshell-history-substitution full-line)))
591 (save-excursion
592 (if full-line-subst
593 ;; Found a ^foo^bar substitution
594 (progn
595 (goto-char beg)
596 (insert-and-inherit full-line-subst)
597 (delete-char (- end beg)))
598 ;; Try to expand other substitutions
599 (while textargs
600 (let ((str (eshell-history-reference (car textargs))))
601 (unless (eq str (car textargs))
602 (goto-char (car posb))
603 (insert-and-inherit str)
604 (delete-char (- (car pose) (car posb)))))
605 (setq textargs (cdr textargs)
606 posb (cdr posb)
607 pose (cdr pose)))))))))
609 (defvar pcomplete-stub)
610 (defvar pcomplete-last-completion-raw)
611 (declare-function pcomplete-actual-arg "pcomplete")
613 (defun eshell-complete-history-reference ()
614 "Complete a history reference, by completing the event designator."
615 (let ((arg (pcomplete-actual-arg)))
616 (when (string-match "\\`![^:^$*%]*\\'" arg)
617 (setq pcomplete-stub (substring arg 1)
618 pcomplete-last-completion-raw t)
619 (throw 'pcomplete-completions
620 (let ((history nil)
621 (index (1- (ring-length eshell-history-ring)))
622 (stublen (length pcomplete-stub)))
623 ;; We have to build up a list ourselves from the ring
624 ;; vector.
625 (while (>= index 0)
626 (let ((hist (eshell-get-history index)))
627 (if (and (>= (length hist) stublen)
628 (string= (substring hist 0 stublen)
629 pcomplete-stub)
630 (string-match "^\\([^:^$*% \t\n]+\\)" hist))
631 (setq history (cons (match-string 1 hist)
632 history))))
633 (setq index (1- index)))
634 (let ((fhist (list t)))
635 ;; uniquify the list, but preserve the order
636 (while history
637 (unless (member (car history) fhist)
638 (nconc fhist (list (car history))))
639 (setq history (cdr history)))
640 (cdr fhist)))))))
642 (defun eshell-history-substitution (line)
643 "Expand quick hist substitutions formatted as ^foo^bar^.
644 Returns nil if string does not match quick substitution format,
645 and acts like !!:s/foo/bar/ otherwise."
646 ;; `^string1^string2^'
647 ;; Quick Substitution. Repeat the last command, replacing
648 ;; STRING1 with STRING2. Equivalent to `!!:s/string1/string2/'
649 (when (and (eshell-using-module 'eshell-pred)
650 (string-match
651 "^\\^\\([^^]+\\)\\^\\([^^]+\\)\\(?:\\^\\(.*\\)\\)?$"
652 line))
653 ;; Save trailing match as `eshell-history-reference' runs string-match.
654 (let ((matched-end (match-string 3 line)))
655 (concat
656 (eshell-history-reference
657 (format "!!:s/%s/%s/"
658 (match-string 1 line)
659 (match-string 2 line)))
660 matched-end))))
662 (defun eshell-history-reference (reference)
663 "Expand directory stack REFERENCE.
664 The syntax used here was taken from the Bash info manual.
665 Returns the resultant reference, or the same string REFERENCE if none
666 matched."
667 ;; `!'
668 ;; Start a history substitution, except when followed by a
669 ;; space, tab, the end of the line, = or (.
670 (if (not (string-match "^![^ \t\n=(]" reference))
671 reference
672 (setq eshell-history-index nil)
673 (let ((event (eshell-hist-parse-event-designator reference)))
674 (unless event
675 (error "Could not find history event `%s'" reference))
676 (setq eshell-history-index (car event)
677 reference (substring reference (cdr event))
678 event (eshell-get-history eshell-history-index))
679 (if (not (string-match "^[:^$*%]" reference))
680 event
681 (let ((word (eshell-hist-parse-word-designator
682 event reference)))
683 (unless word
684 (error "Unable to honor word designator `%s'" reference))
685 (unless (string-match "^[:^$*%][[$^*%0-9-]" reference)
686 (setcdr word 0))
687 (setq event (car word)
688 reference (substring reference (cdr word)))
689 (if (not (and (eshell-using-module 'eshell-pred)
690 (string-match "^:" reference)))
691 event
692 (eshell-hist-parse-modifier event reference)))))))
694 (defun eshell-hist-parse-event-designator (reference)
695 "Parse a history event designator beginning in REFERENCE."
696 (let* ((index (string-match eshell-hist-event-designator reference))
697 (end (and index (match-end 0))))
698 (unless index
699 (error "Invalid history event designator `%s'" reference))
700 (let* ((event (match-string 1 reference))
701 (pos
702 (cond
703 ((string= event "!") (ring-length eshell-history-ring))
704 ((string= event "#") (error "!# not yet implemented"))
705 ((string-match "^-?[0-9]+$" event)
706 (let ((num (string-to-number event)))
707 (if (>= num 0)
708 (- (ring-length eshell-history-ring) num)
709 (1- (abs num)))))
710 ((string-match "^\\(\\??\\)\\([^?]+\\)\\??$" event)
711 (let ((pref (if (> (length (match-string 1 event)) 0)
712 "" "^"))
713 (str (match-string 2 event)))
714 (save-match-data
715 (eshell-previous-matching-input-string-position
716 (concat pref (regexp-quote str)) 1))))
718 (error "Failed to parse event designator `%s'" event)))))
719 (and pos (cons pos end)))))
721 (defun eshell-hist-parse-word-designator (hist reference)
722 "Parse a history word designator beginning for HIST in REFERENCE."
723 (let* ((index (string-match eshell-hist-word-designator reference))
724 (end (and index (match-end 0))))
725 (unless (memq (aref reference 0) '(?: ?^ ?$ ?* ?%))
726 (error "Invalid history word designator `%s'" reference))
727 (let ((nth (match-string 1 reference))
728 (mth (match-string 2 reference))
729 (here (point))
730 textargs)
731 (insert hist)
732 (setq textargs (car (eshell-hist-parse-arguments here (point))))
733 (delete-region here (point))
734 (if (string= nth "*")
735 (if mth
736 (error "Invalid history word designator `%s'"
737 reference)
738 (setq nth 1 mth "-$")))
739 (if (not mth)
740 (if nth
741 (setq mth nth)
742 (setq nth 0 mth "$"))
743 (if (string= mth "-")
744 (setq mth (- (length textargs) 2))
745 (if (string= mth "*")
746 (setq mth "$")
747 (if (not (and (> (length mth) 1)
748 (eq (aref mth 0) ?-)))
749 (error "Invalid history word designator `%s'"
750 reference)
751 (setq mth (substring mth 1))))))
752 (unless (numberp nth)
753 (setq nth (eshell-hist-word-reference nth)))
754 (unless (numberp mth)
755 (setq mth (eshell-hist-word-reference mth)))
756 (cons (mapconcat 'identity (eshell-sublist textargs nth mth) " ")
757 end))))
759 (defun eshell-hist-parse-modifier (hist reference)
760 "Parse a history modifier beginning for HIST in REFERENCE."
761 (let ((here (point)))
762 (insert reference)
763 (prog1
764 (save-restriction
765 (narrow-to-region here (point))
766 (goto-char (point-min))
767 (let ((modifiers (cdr (eshell-parse-modifiers))))
768 (dolist (mod modifiers)
769 (setq hist (car (funcall mod (list hist)))))
770 hist))
771 (delete-region here (point)))))
773 (defun eshell-get-next-from-history ()
774 "After fetching a line from input history, this fetches the next.
775 In other words, this recalls the input line after the line you
776 recalled last. You can use this to repeat a sequence of input lines."
777 (interactive)
778 (if eshell-save-history-index
779 (progn
780 (setq eshell-history-index (1+ eshell-save-history-index))
781 (eshell-next-input 1))
782 (message "No previous history command")))
784 (defun eshell-search-arg (arg)
785 ;; First make sure there is a ring and that we are after the process
786 ;; mark
787 (if (and eshell-hist-move-to-end
788 (< (point) eshell-last-output-end))
789 (goto-char eshell-last-output-end))
790 (cond ((or (null eshell-history-ring)
791 (ring-empty-p eshell-history-ring))
792 (error "Empty input ring"))
793 ((zerop arg)
794 ;; arg of zero resets search from beginning, and uses arg of
795 ;; 1
796 (setq eshell-history-index nil)
799 arg)))
801 (defun eshell-search-start (arg)
802 "Index to start a directional search, starting at `eshell-history-index'."
803 (if eshell-history-index
804 ;; If a search is running, offset by 1 in direction of arg
805 (mod (+ eshell-history-index (if (> arg 0) 1 -1))
806 (ring-length eshell-history-ring))
807 ;; For a new search, start from beginning or end, as appropriate
808 (if (>= arg 0)
809 0 ; First elt for forward search
810 ;; Last elt for backward search
811 (1- (ring-length eshell-history-ring)))))
813 (defun eshell-previous-input-string (arg)
814 "Return the string ARG places along the input ring.
815 Moves relative to `eshell-history-index'."
816 (eshell-get-history (if eshell-history-index
817 (mod (+ arg eshell-history-index)
818 (ring-length eshell-history-ring))
819 arg)))
821 (defun eshell-previous-input (arg)
822 "Cycle backwards through input history."
823 (interactive "*p")
824 (eshell-previous-matching-input "." arg))
826 (defun eshell-next-input (arg)
827 "Cycle forwards through input history."
828 (interactive "*p")
829 (eshell-previous-input (- arg)))
831 (defun eshell-previous-matching-input-string (regexp arg)
832 "Return the string matching REGEXP ARG places along the input ring.
833 Moves relative to `eshell-history-index'."
834 (let* ((pos (eshell-previous-matching-input-string-position regexp arg)))
835 (if pos (eshell-get-history pos))))
837 (defun eshell-previous-matching-input-string-position
838 (regexp arg &optional start)
839 "Return the index matching REGEXP ARG places along the input ring.
840 Moves relative to START, or `eshell-history-index'."
841 (if (or (not (ring-p eshell-history-ring))
842 (ring-empty-p eshell-history-ring))
843 (error "No history"))
844 (let* ((len (ring-length eshell-history-ring))
845 (motion (if (> arg 0) 1 -1))
846 (n (mod (- (or start (eshell-search-start arg)) motion) len))
847 (tried-each-ring-item nil)
848 (case-fold-search (eshell-under-windows-p))
849 (prev nil))
850 ;; Do the whole search as many times as the argument says.
851 (while (and (/= arg 0) (not tried-each-ring-item))
852 ;; Step once.
853 (setq prev n
854 n (mod (+ n motion) len))
855 ;; If we haven't reached a match, step some more.
856 (while (and (< n len) (not tried-each-ring-item)
857 (not (string-match regexp (eshell-get-history n))))
858 (setq n (mod (+ n motion) len)
859 ;; If we have gone all the way around in this search.
860 tried-each-ring-item (= n prev)))
861 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
862 ;; Now that we know which ring element to use, if we found it,
863 ;; return that.
864 (if (string-match regexp (eshell-get-history n))
865 n)))
867 (defun eshell-previous-matching-input (regexp arg)
868 "Search backwards through input history for match for REGEXP.
869 \(Previous history elements are earlier commands.)
870 With prefix argument N, search for Nth previous match.
871 If N is negative, find the next or Nth next match."
872 (interactive (eshell-regexp-arg "Previous input matching (regexp): "))
873 (setq arg (eshell-search-arg arg))
874 (if (> eshell-last-output-end (point))
875 (error "Point not located after prompt"))
876 (let ((pos (eshell-previous-matching-input-string-position regexp arg)))
877 ;; Has a match been found?
878 (if (null pos)
879 (error "Not found")
880 (setq eshell-history-index pos)
881 (unless (minibuffer-window-active-p (selected-window))
882 (message "History item: %d" (- (ring-length eshell-history-ring) pos)))
883 ;; Can't use kill-region as it sets this-command
884 (delete-region eshell-last-output-end (point))
885 (insert-and-inherit (eshell-get-history pos)))))
887 (defun eshell-next-matching-input (regexp arg)
888 "Search forwards through input history for match for REGEXP.
889 \(Later history elements are more recent commands.)
890 With prefix argument N, search for Nth following match.
891 If N is negative, find the previous or Nth previous match."
892 (interactive (eshell-regexp-arg "Next input matching (regexp): "))
893 (eshell-previous-matching-input regexp (- arg)))
895 (defun eshell-previous-matching-input-from-input (arg)
896 "Search backwards through input history for match for current input.
897 \(Previous history elements are earlier commands.)
898 With prefix argument N, search for Nth previous match.
899 If N is negative, search forwards for the -Nth following match."
900 (interactive "p")
901 (if (not (memq last-command '(eshell-previous-matching-input-from-input
902 eshell-next-matching-input-from-input)))
903 ;; Starting a new search
904 (setq eshell-matching-input-from-input-string
905 (buffer-substring (save-excursion (eshell-bol) (point))
906 (point))
907 eshell-history-index nil))
908 (eshell-previous-matching-input
909 (concat "^" (regexp-quote eshell-matching-input-from-input-string))
910 arg))
912 (defun eshell-next-matching-input-from-input (arg)
913 "Search forwards through input history for match for current input.
914 \(Following history elements are more recent commands.)
915 With prefix argument N, search for Nth following match.
916 If N is negative, search backwards for the -Nth previous match."
917 (interactive "p")
918 (eshell-previous-matching-input-from-input (- arg)))
920 (defun eshell-test-imatch ()
921 "If isearch match good, put point at the beginning and return non-nil."
922 (if (get-text-property (point) 'history)
923 (progn (beginning-of-line) t)
924 (let ((before (point)))
925 (eshell-bol)
926 (if (and (not (bolp))
927 (<= (point) before))
929 (if isearch-forward
930 (progn
931 (end-of-line)
932 (forward-char))
933 (beginning-of-line)
934 (backward-char))))))
936 (defun eshell-return-to-prompt ()
937 "Once a search string matches, insert it at the end and go there."
938 (setq isearch-other-end nil)
939 (let ((found (eshell-test-imatch)) before)
940 (while (and (not found)
941 (setq before
942 (funcall (if isearch-forward
943 're-search-forward
944 're-search-backward)
945 isearch-string nil t)))
946 (setq found (eshell-test-imatch)))
947 (if (not found)
948 (progn
949 (goto-char eshell-last-output-end)
950 (delete-region (point) (point-max)))
951 (setq before (point))
952 (let ((text (buffer-substring-no-properties
953 (point) (line-end-position)))
954 (orig (marker-position eshell-last-output-end)))
955 (goto-char eshell-last-output-end)
956 (delete-region (point) (point-max))
957 (when (and text (> (length text) 0))
958 (insert text)
959 (put-text-property (1- (point)) (point)
960 'last-search-pos before)
961 (set-marker eshell-last-output-end orig)
962 (goto-char eshell-last-output-end))))))
964 (defun eshell-prepare-for-search ()
965 "Make sure the old history file is at the beginning of the buffer."
966 (unless (get-text-property (point-min) 'history)
967 (save-excursion
968 (goto-char (point-min))
969 (let ((end (copy-marker (point) t)))
970 (insert-file-contents eshell-history-file-name)
971 (set-text-properties (point-min) end
972 '(history t invisible t))))))
974 (defun eshell-isearch-backward (&optional invert)
975 "Do incremental regexp search backward through past commands."
976 (interactive)
977 (let ((inhibit-read-only t))
978 (eshell-prepare-for-search)
979 (goto-char (point-max))
980 (set-marker eshell-last-output-end (point))
981 (delete-region (point) (point-max)))
982 (isearch-mode invert t 'eshell-return-to-prompt))
984 (defun eshell-isearch-repeat-backward (&optional invert)
985 "Do incremental regexp search backward through past commands."
986 (interactive)
987 (let ((old-pos (get-text-property (1- (point-max))
988 'last-search-pos)))
989 (when old-pos
990 (goto-char old-pos)
991 (if invert
992 (end-of-line)
993 (backward-char)))
994 (setq isearch-forward invert)
995 (isearch-search-and-update)))
997 (defun eshell-isearch-forward ()
998 "Do incremental regexp search backward through past commands."
999 (interactive)
1000 (eshell-isearch-backward t))
1002 (defun eshell-isearch-repeat-forward ()
1003 "Do incremental regexp search backward through past commands."
1004 (interactive)
1005 (eshell-isearch-repeat-backward t))
1007 (defun eshell-isearch-cancel ()
1008 (interactive)
1009 (goto-char eshell-last-output-end)
1010 (delete-region (point) (point-max))
1011 (call-interactively 'isearch-cancel))
1013 (defun eshell-isearch-abort ()
1014 (interactive)
1015 (goto-char eshell-last-output-end)
1016 (delete-region (point) (point-max))
1017 (call-interactively 'isearch-abort))
1019 (defun eshell-isearch-delete-char ()
1020 (interactive)
1021 (save-excursion
1022 (isearch-delete-char)))
1024 (defun eshell-isearch-return ()
1025 (interactive)
1026 (isearch-done)
1027 (eshell-send-input))
1029 (provide 'em-hist)
1031 ;; Local Variables:
1032 ;; generated-autoload-file: "esh-groups.el"
1033 ;; End:
1035 ;;; em-hist.el ends here