1 ;;; em-hist.el --- history list management
3 ;; Copyright (C) 1999-2011 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 <http://www.gnu.org/licenses/>.
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.
66 (eshell-defgroup eshell-hist nil
67 "This module provides command history management."
68 :tag
"History list management"
69 :group
'eshell-module
)
73 (defcustom eshell-hist-load-hook
'(eshell-hist-initialize)
74 "A list of functions to call when loading `eshell-hist'."
78 (defcustom eshell-hist-unload-hook
82 (remove-hook 'kill-emacs-hook
'eshell-save-some-history
))))
83 "A hook that gets run when `eshell-hist' is unloaded."
87 (defcustom eshell-history-file-name
88 (expand-file-name "history" eshell-directory-name
)
89 "If non-nil, name of the file to read/write input history.
90 See also `eshell-read-history' and `eshell-write-history'.
91 If it is nil, Eshell will use the value of HISTFILE."
95 (defcustom eshell-history-size
128
96 "Size of the input history ring. If nil, use envvar HISTSIZE."
100 (defcustom eshell-hist-ignoredups nil
101 "If non-nil, don't add input matching the last on the input ring.
102 This mirrors the optional behavior of bash."
106 (defcustom eshell-save-history-on-exit t
107 "Determine if history should be automatically saved.
108 History is always preserved after sanely exiting an Eshell buffer.
109 However, when Emacs is being shut down, this variable determines
110 whether to prompt the user.
111 If set to nil, it means never save history on termination of Emacs.
112 If set to `ask', ask if any Eshell buffers are open at exit time.
113 If set to t, history will always be saved, silently."
114 :type
'(choice (const :tag
"Never" nil
)
115 (const :tag
"Ask" ask
)
116 (const :tag
"Always save" t
))
119 (defcustom eshell-input-filter
122 (not (string-match "\\`\\s-*\\'" str
))))
123 "Predicate for filtering additions to input history.
124 Takes one argument, the input. If non-nil, the input may be saved on
125 the input history list. Default is to save anything that isn't all
130 (put 'eshell-input-filter
'risky-local-variable t
)
132 (defcustom eshell-hist-match-partial t
133 "If non-nil, movement through history is constrained by current input.
134 Otherwise, typing <M-p> and <M-n> will always go to the next history
135 element, regardless of any text on the command line. In that case,
136 <C-c M-r> and <C-c M-s> still offer that functionality."
140 (defcustom eshell-hist-move-to-end t
141 "If non-nil, move to the end of the buffer before cycling history."
145 (defcustom eshell-hist-event-designator
146 "^!\\(!\\|-?[0-9]+\\|\\??[^:^$%*?]+\\??\\|#\\)"
147 "The regexp used to identifier history event designators."
151 (defcustom eshell-hist-word-designator
152 "^:?\\([0-9]+\\|[$^%*]\\)?\\(\\*\\|-[0-9]*\\|[$^%*]\\)?"
153 "The regexp used to identify history word designators."
157 (defcustom eshell-hist-modifier
158 "^\\(:\\([hretpqx&g]\\|s/\\([^/]*\\)/\\([^/]*\\)/\\)\\)*"
159 "The regexp used to identity history modifiers."
163 (defcustom eshell-hist-rebind-keys-alist
164 '(([(control ?p
)] . eshell-previous-input
)
165 ([(control ?n
)] . eshell-next-input
)
166 ([(control up
)] . eshell-previous-input
)
167 ([(control down
)] . eshell-next-input
)
168 ([(control ?r
)] . eshell-isearch-backward
)
169 ([(control ?s
)] . eshell-isearch-forward
)
170 ([(meta ?r
)] . eshell-previous-matching-input
)
171 ([(meta ?s
)] . eshell-next-matching-input
)
172 ([(meta ?p
)] . eshell-previous-matching-input-from-input
)
173 ([(meta ?n
)] . eshell-next-matching-input-from-input
)
174 ([up] . eshell-previous-matching-input-from-input)
175 ([down] . eshell-next-matching-input-from-input))
176 "History keys to bind differently if point is in input text."
177 :type '(repeat (cons (vector :tag "Keys to bind"
178 (repeat :inline t sexp))
179 (function :tag "Command")))
182 ;;; Internal Variables:
184 (defvar eshell-history-ring nil)
185 (defvar eshell-history-index nil)
186 (defvar eshell-matching-input-from-input-string "")
187 (defvar eshell-save-history-index nil)
189 (defvar eshell-isearch-map nil)
191 (unless eshell-isearch-map
192 (setq eshell-isearch-map (copy-keymap isearch-mode-map))
193 (define-key eshell-isearch-map [(control ?m)] 'eshell-isearch-return)
194 (define-key eshell-isearch-map [return] 'eshell-isearch-return)
195 (define-key eshell-isearch-map [(control ?r)] 'eshell-isearch-repeat-backward)
196 (define-key eshell-isearch-map [(control ?s)] 'eshell-isearch-repeat-forward)
197 (define-key eshell-isearch-map [(control ?g)] 'eshell-isearch-abort)
198 (define-key eshell-isearch-map [backspace] 'eshell-isearch-delete-char)
199 (define-key eshell-isearch-map [delete] 'eshell-isearch-delete-char)
200 (defvar eshell-isearch-cancel-map)
201 (define-prefix-command 'eshell-isearch-cancel-map)
202 (define-key eshell-isearch-map [(control ?c)] 'eshell-isearch-cancel-map)
203 (define-key eshell-isearch-cancel-map [(control ?c)] 'eshell-isearch-cancel))
205 (defvar eshell-rebind-keys-alist)
209 (defun eshell-hist-initialize ()
210 "Initialize the history management code for one Eshell buffer."
211 (add-hook 'eshell-expand-input-functions
212 'eshell-expand-history-references nil t)
214 (when (eshell-using-module 'eshell-cmpl)
215 (add-hook 'pcomplete-try-first-hook
216 'eshell-complete-history-reference nil t))
218 (if (and (eshell-using-module 'eshell-rebind)
219 (not eshell-non-interactive-p))
220 (let ((rebind-alist eshell-rebind-keys-alist))
221 (make-local-variable 'eshell-rebind-keys-alist)
222 (setq eshell-rebind-keys-alist
223 (append rebind-alist eshell-hist-rebind-keys-alist))
224 (set (make-local-variable 'search-invisible) t)
225 (set (make-local-variable 'search-exit-option) t)
226 (add-hook 'isearch-mode-hook
229 (if (>= (point) eshell-last-output-end)
230 (setq overriding-terminal-local-map
231 eshell-isearch-map)))) nil t)
232 (add-hook 'isearch-mode-end-hook
235 (setq overriding-terminal-local-map nil))) nil t))
236 (define-key eshell-mode-map [up] 'eshell-previous-matching-input-from-input
)
237 (define-key eshell-mode-map
[down] 'eshell-next-matching-input-from-input)
238 (define-key eshell-mode-map [(control up)] 'eshell-previous-input)
239 (define-key eshell-mode-map [(control down)] 'eshell-next-input)
240 (define-key eshell-mode-map [(meta ?r)] 'eshell-previous-matching-input)
241 (define-key eshell-mode-map [(meta ?s)] 'eshell-next-matching-input)
242 (define-key eshell-command-map [(meta ?r)]
243 'eshell-previous-matching-input-from-input)
244 (define-key eshell-command-map [(meta ?s)]
245 'eshell-next-matching-input-from-input)
246 (if eshell-hist-match-partial
248 (define-key eshell-mode-map [(meta ?p)]
249 'eshell-previous-matching-input-from-input)
250 (define-key eshell-mode-map [(meta ?n)]
251 'eshell-next-matching-input-from-input)
252 (define-key eshell-command-map [(meta ?p)] 'eshell-previous-input)
253 (define-key eshell-command-map [(meta ?n)] 'eshell-next-input))
254 (define-key eshell-mode-map [(meta ?p)] 'eshell-previous-input)
255 (define-key eshell-mode-map [(meta ?n)] 'eshell-next-input)
256 (define-key eshell-command-map [(meta ?p)]
257 'eshell-previous-matching-input-from-input)
258 (define-key eshell-command-map [(meta ?n)]
259 'eshell-next-matching-input-from-input)))
261 (make-local-variable 'eshell-history-size)
262 (or eshell-history-size
263 (setq eshell-history-size (getenv "HISTSIZE")))
265 (make-local-variable 'eshell-history-file-name)
266 (or eshell-history-file-name
267 (setq eshell-history-file-name (getenv "HISTFILE")))
269 (make-local-variable 'eshell-history-index)
270 (make-local-variable 'eshell-save-history-index)
272 (if (minibuffer-window-active-p (selected-window))
273 (set (make-local-variable 'eshell-save-history-on-exit) nil)
274 (set (make-local-variable 'eshell-history-ring) nil)
275 (if eshell-history-file-name
276 (eshell-read-history nil t))
278 (add-hook 'eshell-exit-hook 'eshell-write-history nil t))
280 (unless eshell-history-ring
281 (setq eshell-history-ring (make-ring eshell-history-size)))
283 (add-hook 'eshell-exit-hook 'eshell-write-history nil t)
285 (add-hook 'kill-emacs-hook 'eshell-save-some-history)
287 (make-local-variable 'eshell-input-filter-functions)
288 (add-hook 'eshell-input-filter-functions 'eshell-add-to-history nil t)
290 (define-key eshell-command-map [(control ?l)] 'eshell-list-history)
291 (define-key eshell-command-map [(control ?x)] 'eshell-get-next-from-history))
293 (defun eshell-save-some-history ()
294 "Save the history for any open Eshell buffers."
295 (eshell-for buf (buffer-list)
296 (if (buffer-live-p buf)
297 (with-current-buffer buf
299 eshell-history-file-name
300 eshell-save-history-on-exit
301 (or (eq eshell-save-history-on-exit t)
303 (format "Save input history for Eshell buffer `%s'? "
304 (buffer-name buf)))))
305 (eshell-write-history))))))
307 (defun eshell/history (&rest args)
308 "List in help buffer the buffer's input history."
309 (eshell-init-print-buffer)
310 (eshell-eval-using-options
312 '((?r "read" nil read-history
313 "read from history file to current history list")
314 (?w "write" nil write-history
315 "write current history list to history file")
316 (?a "append" nil append-history
317 "append current history list to history file")
318 (?h "help" nil nil "display this usage message")
319 :usage "[n] [-rwa [filename]]"
321 "When Eshell is started, history is read from `eshell-history-file-name'.
322 This is also the location where history info will be saved by this command,
323 unless a different file is specified on the command line.")
324 (and (or (not (ring-p eshell-history-ring))
325 (ring-empty-p eshell-history-ring))
326 (error "No history"))
327 (let (length command file)
328 (when (and args (string-match "^[0-9]+$" (car args)))
329 (setq length (min (eshell-convert (car args))
330 (ring-length eshell-history-ring))
333 (or read-history write-history append-history)
334 (error "history: extra arguments"))
335 (when (and args (stringp (car args)))
336 (setq file (car args)
339 (read-history (eshell-read-history file))
340 (write-history (eshell-write-history file))
341 (append-history (eshell-write-history file t))
344 (index (1- (or length (ring-length eshell-history-ring))))
345 (ref (- (ring-length eshell-history-ring) index)))
346 ;; We have to build up a list ourselves from the ring vector.
348 (eshell-buffered-print
349 (format "%5d %s\n" ref (eshell-get-history index)))
350 (setq index (1- index)
355 (defun eshell-put-history (input &optional ring at-beginning)
356 "Put a new input line into the history ring."
357 (unless ring (setq ring eshell-history-ring))
359 (ring-insert-at-beginning ring input)
360 (ring-insert ring input)))
362 (defun eshell-get-history (index &optional ring)
363 "Get an input line from the history ring."
364 (ring-ref (or ring eshell-history-ring) index))
366 (defun eshell-add-input-to-history (input)
367 "Add the string INPUT to the history ring.
368 Input is entered into the input history ring, if the value of
369 variable `eshell-input-filter' returns non-nil when called on the
371 (if (and (funcall eshell-input-filter input)
372 (or (null eshell-hist-ignoredups)
373 (not (ring-p eshell-history-ring))
374 (ring-empty-p eshell-history-ring)
375 (not (string-equal (eshell-get-history 0) input))))
376 (eshell-put-history input))
377 (setq eshell-save-history-index eshell-history-index)
378 (setq eshell-history-index nil))
380 (defun eshell-add-command-to-history ()
381 "Add the command entered at `eshell-command's prompt to the history ring.
382 The command is added to the input history ring, if the value of
383 variable `eshell-input-filter' returns non-nil when called on the
386 This function is supposed to be called from the minibuffer, presumably
387 as a minibuffer-exit-hook."
388 (eshell-add-input-to-history
389 (buffer-substring (minibuffer-prompt-end) (point-max))))
391 (defun eshell-add-to-history ()
392 "Add last Eshell command to the history ring.
393 The command is entered into the input history ring, if the value of
394 variable `eshell-input-filter' returns non-nil when called on the
396 (when (> (1- eshell-last-input-end) eshell-last-input-start)
397 (let ((input (buffer-substring eshell-last-input-start
398 (1- eshell-last-input-end))))
399 (eshell-add-input-to-history input))))
401 (defun eshell-read-history (&optional filename silent)
402 "Sets the buffer's `eshell-history-ring' from a history file.
403 The name of the file is given by the variable
404 `eshell-history-file-name'. The history ring is of size
405 `eshell-history-size', regardless of file size. If
406 `eshell-history-file-name' is nil this function does nothing.
408 If the optional argument SILENT is non-nil, we say nothing about a
409 failure to read the history file.
411 This function is useful for major mode commands and mode hooks.
413 The structure of the history file should be one input command per
414 line, with the most recent command last. See also
415 `eshell-hist-ignoredups' and `eshell-write-history'."
416 (let ((file (or filename eshell-history-file-name)))
421 ((not (file-readable-p file))
423 (message "Cannot read history file %s" file)))
426 (size eshell-history-size)
427 (ring (make-ring size))
428 (ignore-dups eshell-hist-ignoredups))
430 (insert-file-contents file)
431 ;; Save restriction in case file is already visited...
432 ;; Watch for those date stamps in history files!
433 (goto-char (point-max))
434 (while (and (< count size)
435 (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$"
437 (let ((history (match-string 1)))
438 (if (or (null ignore-dups)
440 (not (string-equal (ring-ref ring 0) history)))
441 (ring-insert-at-beginning
442 ring (subst-char-in-string ?\177 ?\n history))))
443 (setq count (1+ count))))
444 (setq eshell-history-ring ring
445 eshell-history-index nil))))))
447 (defun eshell-write-history (&optional filename append)
448 "Writes the buffer's `eshell-history-ring' to a history file.
449 The name of the file is given by the variable
450 `eshell-history-file-name'. The original contents of the file are
451 lost if `eshell-history-ring' is not empty. If
452 `eshell-history-file-name' is nil this function does nothing.
454 Useful within process sentinels.
456 See also `eshell-read-history'."
457 (let ((file (or filename eshell-history-file-name)))
461 (null eshell-history-ring)
462 (ring-empty-p eshell-history-ring))
464 ((not (file-writable-p file))
465 (message "Cannot write history file %s" file))
467 (let* ((ring eshell-history-ring)
468 (index (ring-length ring)))
469 ;; Write it all out into a buffer first. Much faster, but
470 ;; messier, than writing it one line at a time.
473 (setq index (1- index))
474 (let ((start (point)))
475 (insert (ring-ref ring index) ?\n)
476 (subst-char-in-region start (1- (point)) ?\n ?\177)))
477 (eshell-with-private-file-modes
478 (write-region (point-min) (point-max) file append
481 (defun eshell-list-history ()
482 "List in help buffer the buffer's input history."
486 (if (re-search-backward "!\\(.+\\)" (line-beginning-position) t)
487 (setq prefix (match-string 1)
488 prelen (length prefix))))
489 (if (or (not (ring-p eshell-history-ring))
490 (ring-empty-p eshell-history-ring))
491 (message "No history")
493 (history-buffer " *Input History*")
494 (index (1- (ring-length eshell-history-ring)))
495 (conf (current-window-configuration)))
496 ;; We have to build up a list ourselves from the ring vector.
498 (let ((hist (eshell-get-history index)))
500 (and (>= (length hist) prelen)
501 (string= (substring hist 0 prelen) prefix)))
502 (setq history (cons hist history))))
503 (setq index (1- index)))
504 ;; Change "completion" to "history reference"
505 ;; to make the display accurate.
506 (with-output-to-temp-buffer history-buffer
507 (display-completion-list history prefix)
508 (set-buffer history-buffer)
510 (while (search-backward "completion" nil 'move)
511 (replace-match "history reference")))
513 (message "Hit space to flush")
514 (let ((ch (read-event)))
516 (set-window-configuration conf)
517 (setq unread-command-events (list ch))))))))
519 (defun eshell-hist-word-reference (ref)
520 "Return the word designator index referred to by REF."
522 ((string-match "^[0-9]+$" ref)
523 (string-to-number ref))
524 ((string= "^" ref) 1)
525 ((string= "$" ref) nil)
527 (error "`%%' history word designator not yet implemented"))))
529 (defun eshell-hist-parse-arguments (&optional silent b e)
530 "Parse current command arguments in a history-code-friendly way."
531 (let ((end (or e (point)))
532 (begin (or b (save-excursion (eshell-bol) (point))))
537 (unless (catch 'eshell-incomplete
539 (setq args (eshell-parse-arguments begin end))))
542 (while (< (point) end)
543 (if (get-text-property (point) 'arg-begin)
544 (nconc posb (list (point))))
545 (if (get-text-property (point) 'arg-end)
547 (list (if (= (1+ (point)) end)
551 (setq posb (cdr posb)
553 (assert (= (length posb) (length args)))
554 (assert (<= (length posb) (length pose))))
555 (setq hist (buffer-substring-no-properties begin end))
556 (let ((b posb) (e pose))
559 (list (substring hist (- (car b) begin)
563 (setq textargs (cdr textargs))
564 (assert (= (length textargs) (length args)))
565 (list textargs posb pose))))
567 (defun eshell-expand-history-references (beg end)
568 "Parse and expand any history references in current input."
569 (let ((result (eshell-hist-parse-arguments t beg end)))
571 (let ((textargs (nreverse (nth 0 result)))
572 (posb (nreverse (nth 1 result)))
573 (pose (nreverse (nth 2 result))))
576 (let ((str (eshell-history-reference (car textargs))))
577 (unless (eq str (car textargs))
578 (goto-char (car posb))
579 (insert-and-inherit str)
580 (delete-char (- (car pose) (car posb)))))
581 (setq textargs (cdr textargs)
583 pose (cdr pose))))))))
585 (defvar pcomplete-stub)
586 (defvar pcomplete-last-completion-raw)
587 (declare-function pcomplete-actual-arg "pcomplete")
589 (defun eshell-complete-history-reference ()
590 "Complete a history reference, by completing the event designator."
591 (let ((arg (pcomplete-actual-arg)))
592 (when (string-match "\\`![^:^$*%]*\\'" arg)
593 (setq pcomplete-stub (substring arg 1)
594 pcomplete-last-completion-raw t)
595 (throw 'pcomplete-completions
597 (index (1- (ring-length eshell-history-ring)))
598 (stublen (length pcomplete-stub)))
599 ;; We have to build up a list ourselves from the ring
602 (let ((hist (eshell-get-history index)))
603 (if (and (>= (length hist) stublen)
604 (string= (substring hist 0 stublen)
606 (string-match "^\\([^:^$*% \t\n]+\\)" hist))
607 (setq history (cons (match-string 1 hist)
609 (setq index (1- index)))
610 (let ((fhist (list t)))
611 ;; uniqify the list, but preserve the order
613 (unless (member (car history) fhist)
614 (nconc fhist (list (car history))))
615 (setq history (cdr history)))
618 (defun eshell-history-reference (reference)
619 "Expand directory stack REFERENCE.
620 The syntax used here was taken from the Bash info manual.
621 Returns the resultant reference, or the same string REFERENCE if none
623 ;; `^string1^string2^'
624 ;; Quick Substitution. Repeat the last command, replacing
625 ;; STRING1 with STRING2. Equivalent to `!!:s/string1/string2/'
626 (if (and (eshell-using-module 'eshell-pred)
627 (string-match "\\^\\([^^]+\\)\\^\\([^^]+\\)\\^?\\s-*$"
629 (setq reference (format "!!:s/%s/%s/"
630 (match-string 1 reference)
631 (match-string 2 reference))))
633 ;; Start a history substitution, except when followed by a
634 ;; space, tab, the end of the line, = or (.
635 (if (not (string-match "^![^ \t\n=\(]" reference))
637 (setq eshell-history-index nil)
638 (let ((event (eshell-hist-parse-event-designator reference)))
640 (error "Could not find history event `%s'" reference))
641 (setq eshell-history-index (car event)
642 reference (substring reference (cdr event))
643 event (eshell-get-history eshell-history-index))
644 (if (not (string-match "^[:^$*%]" reference))
646 (let ((word (eshell-hist-parse-word-designator
649 (error "Unable to honor word designator `%s'" reference))
650 (unless (string-match "^[:^$*%][[$^*%0-9-]" reference)
652 (setq event (car word)
653 reference (substring reference (cdr word)))
654 (if (not (and (eshell-using-module 'eshell-pred)
655 (string-match "^:" reference)))
657 (eshell-hist-parse-modifier event reference)))))))
659 (defun eshell-hist-parse-event-designator (reference)
660 "Parse a history event designator beginning in REFERENCE."
661 (let* ((index (string-match eshell-hist-event-designator reference))
662 (end (and index (match-end 0))))
664 (error "Invalid history event designator `%s'" reference))
665 (let* ((event (match-string 1 reference))
668 ((string= event "!") (ring-length eshell-history-ring))
669 ((string= event "#") (error "!# not yet implemented"))
670 ((string-match "^-?[0-9]+$" event)
671 (let ((num (string-to-number event)))
673 (- (ring-length eshell-history-ring) num)
675 ((string-match "^\\(\\??\\)\\([^?]+\\)\\??$" event)
676 (let ((pref (if (> (length (match-string 1 event)) 0)
678 (str (match-string 2 event)))
680 (eshell-previous-matching-input-string-position
681 (concat pref (regexp-quote str)) 1))))
683 (error "Failed to parse event designator `%s'" event)))))
684 (and pos (cons pos end)))))
686 (defun eshell-hist-parse-word-designator (hist reference)
687 "Parse a history word designator beginning for HIST in REFERENCE."
688 (let* ((index (string-match eshell-hist-word-designator reference))
689 (end (and index (match-end 0))))
690 (unless (memq (aref reference 0) '(?: ?^ ?$ ?* ?%))
691 (error "Invalid history word designator `%s'" reference))
692 (let ((nth (match-string 1 reference))
693 (mth (match-string 2 reference))
697 (setq textargs (car (eshell-hist-parse-arguments nil here (point))))
698 (delete-region here (point))
699 (if (string= nth "*")
701 (error "Invalid history word designator `%s'"
703 (setq nth 1 mth "-$")))
707 (setq nth 0 mth "$"))
708 (if (string= mth "-")
709 (setq mth (- (length textargs) 2))
710 (if (string= mth "*")
712 (if (not (and (> (length mth) 1)
713 (eq (aref mth 0) ?-)))
714 (error "Invalid history word designator `%s'"
716 (setq mth (substring mth 1))))))
717 (unless (numberp nth)
718 (setq nth (eshell-hist-word-reference nth)))
719 (unless (numberp mth)
720 (setq mth (eshell-hist-word-reference mth)))
721 (cons (mapconcat 'identity (eshell-sublist textargs nth mth) "")
724 (defun eshell-hist-parse-modifier (hist reference)
725 "Parse a history modifier beginning for HIST in REFERENCE."
726 (let ((here (point)))
730 (narrow-to-region here (point))
731 (goto-char (point-min))
732 (let ((modifiers (cdr (eshell-parse-modifiers))))
733 (eshell-for mod modifiers
734 (setq hist (funcall mod hist)))
736 (delete-region here (point)))))
738 (defun eshell-get-next-from-history ()
739 "After fetching a line from input history, this fetches the next.
740 In other words, this recalls the input line after the line you
741 recalled last. You can use this to repeat a sequence of input lines."
743 (if eshell-save-history-index
745 (setq eshell-history-index (1+ eshell-save-history-index))
746 (eshell-next-input 1))
747 (message "No previous history command")))
749 (defun eshell-search-arg (arg)
750 ;; First make sure there is a ring and that we are after the process
752 (if (and eshell-hist-move-to-end
753 (< (point) eshell-last-output-end))
754 (goto-char eshell-last-output-end))
755 (cond ((or (null eshell-history-ring)
756 (ring-empty-p eshell-history-ring))
757 (error "Empty input ring"))
759 ;; arg of zero resets search from beginning, and uses arg of
761 (setq eshell-history-index nil)
766 (defun eshell-search-start (arg)
767 "Index to start a directional search, starting at `eshell-history-index'."
768 (if eshell-history-index
769 ;; If a search is running, offset by 1 in direction of arg
770 (mod (+ eshell-history-index (if (> arg 0) 1 -1))
771 (ring-length eshell-history-ring))
772 ;; For a new search, start from beginning or end, as appropriate
774 0 ; First elt for forward search
775 ;; Last elt for backward search
776 (1- (ring-length eshell-history-ring)))))
778 (defun eshell-previous-input-string (arg)
779 "Return the string ARG places along the input ring.
780 Moves relative to `eshell-history-index'."
781 (eshell-get-history (if eshell-history-index
782 (mod (+ arg eshell-history-index)
783 (ring-length eshell-history-ring))
786 (defun eshell-previous-input (arg)
787 "Cycle backwards through input history."
789 (eshell-previous-matching-input "." arg))
791 (defun eshell-next-input (arg)
792 "Cycle forwards through input history."
794 (eshell-previous-input (- arg)))
796 (defun eshell-previous-matching-input-string (regexp arg)
797 "Return the string matching REGEXP ARG places along the input ring.
798 Moves relative to `eshell-history-index'."
799 (let* ((pos (eshell-previous-matching-input-string-position regexp arg)))
800 (if pos (eshell-get-history pos))))
802 (defun eshell-previous-matching-input-string-position
803 (regexp arg &optional start)
804 "Return the index matching REGEXP ARG places along the input ring.
805 Moves relative to START, or `eshell-history-index'."
806 (if (or (not (ring-p eshell-history-ring))
807 (ring-empty-p eshell-history-ring))
808 (error "No history"))
809 (let* ((len (ring-length eshell-history-ring))
810 (motion (if (> arg 0) 1 -1))
811 (n (mod (- (or start (eshell-search-start arg)) motion) len))
812 (tried-each-ring-item nil)
813 (case-fold-search (eshell-under-windows-p))
815 ;; Do the whole search as many times as the argument says.
816 (while (and (/= arg 0) (not tried-each-ring-item))
819 n (mod (+ n motion) len))
820 ;; If we haven't reached a match, step some more.
821 (while (and (< n len) (not tried-each-ring-item)
822 (not (string-match regexp (eshell-get-history n))))
823 (setq n (mod (+ n motion) len)
824 ;; If we have gone all the way around in this search.
825 tried-each-ring-item (= n prev)))
826 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
827 ;; Now that we know which ring element to use, if we found it,
829 (if (string-match regexp (eshell-get-history n))
832 (defun eshell-previous-matching-input (regexp arg)
833 "Search backwards through input history for match for REGEXP.
834 \(Previous history elements are earlier commands.)
835 With prefix argument N, search for Nth previous match.
836 If N is negative, find the next or Nth next match."
837 (interactive (eshell-regexp-arg "Previous input matching (regexp): "))
838 (setq arg (eshell-search-arg arg))
839 (if (> eshell-last-output-end (point))
840 (error "Point not located after prompt"))
841 (let ((pos (eshell-previous-matching-input-string-position regexp arg)))
842 ;; Has a match been found?
845 (setq eshell-history-index pos)
846 (unless (minibuffer-window-active-p (selected-window))
847 (message "History item: %d" (- (ring-length eshell-history-ring) pos)))
848 ;; Can't use kill-region as it sets this-command
849 (delete-region eshell-last-output-end (point))
850 (insert-and-inherit (eshell-get-history pos)))))
852 (defun eshell-next-matching-input (regexp arg)
853 "Search forwards through input history for match for REGEXP.
854 \(Later history elements are more recent commands.)
855 With prefix argument N, search for Nth following match.
856 If N is negative, find the previous or Nth previous match."
857 (interactive (eshell-regexp-arg "Next input matching (regexp): "))
858 (eshell-previous-matching-input regexp (- arg)))
860 (defun eshell-previous-matching-input-from-input (arg)
861 "Search backwards through input history for match for current input.
862 \(Previous history elements are earlier commands.)
863 With prefix argument N, search for Nth previous match.
864 If N is negative, search forwards for the -Nth following match."
866 (if (not (memq last-command '(eshell-previous-matching-input-from-input
867 eshell-next-matching-input-from-input)))
868 ;; Starting a new search
869 (setq eshell-matching-input-from-input-string
870 (buffer-substring (save-excursion (eshell-bol) (point))
872 eshell-history-index nil))
873 (eshell-previous-matching-input
874 (concat "^" (regexp-quote eshell-matching-input-from-input-string))
877 (defun eshell-next-matching-input-from-input (arg)
878 "Search forwards through input history for match for current input.
879 \(Following history elements are more recent commands.)
880 With prefix argument N, search for Nth following match.
881 If N is negative, search backwards for the -Nth previous match."
883 (eshell-previous-matching-input-from-input (- arg)))
885 (defun eshell-test-imatch ()
886 "If isearch match good, put point at the beginning and return non-nil."
887 (if (get-text-property (point) 'history)
888 (progn (beginning-of-line) t)
889 (let ((before (point)))
891 (if (and (not (bolp))
901 (defun eshell-return-to-prompt ()
902 "Once a search string matches, insert it at the end and go there."
903 (setq isearch-other-end nil)
904 (let ((found (eshell-test-imatch)) before)
905 (while (and (not found)
907 (funcall (if isearch-forward
910 isearch-string nil t)))
911 (setq found (eshell-test-imatch)))
914 (goto-char eshell-last-output-end)
915 (delete-region (point) (point-max)))
916 (setq before (point))
917 (let ((text (buffer-substring-no-properties
918 (point) (line-end-position)))
919 (orig (marker-position eshell-last-output-end)))
920 (goto-char eshell-last-output-end)
921 (delete-region (point) (point-max))
922 (when (and text (> (length text) 0))
924 (put-text-property (1- (point)) (point)
925 'last-search-pos before)
926 (set-marker eshell-last-output-end orig)
927 (goto-char eshell-last-output-end))))))
929 (defun eshell-prepare-for-search ()
930 "Make sure the old history file is at the beginning of the buffer."
931 (unless (get-text-property (point-min) 'history)
933 (goto-char (point-min))
934 (let ((end (copy-marker (point) t)))
935 (insert-file-contents eshell-history-file-name)
936 (set-text-properties (point-min) end
937 '(history t invisible t))))))
939 (defun eshell-isearch-backward (&optional invert)
940 "Do incremental regexp search backward through past commands."
942 (let ((inhibit-read-only t) end)
943 (eshell-prepare-for-search)
944 (goto-char (point-max))
945 (set-marker eshell-last-output-end (point))
946 (delete-region (point) (point-max)))
947 (isearch-mode invert t 'eshell-return-to-prompt))
949 (defun eshell-isearch-repeat-backward (&optional invert)
950 "Do incremental regexp search backward through past commands."
952 (let ((old-pos (get-text-property (1- (point-max))
959 (setq isearch-forward invert)
960 (isearch-search-and-update)))
962 (defun eshell-isearch-forward ()
963 "Do incremental regexp search backward through past commands."
965 (eshell-isearch-backward t))
967 (defun eshell-isearch-repeat-forward ()
968 "Do incremental regexp search backward through past commands."
970 (eshell-isearch-repeat-backward t))
972 (defun eshell-isearch-cancel ()
974 (goto-char eshell-last-output-end)
975 (delete-region (point) (point-max))
976 (call-interactively 'isearch-cancel))
978 (defun eshell-isearch-abort ()
980 (goto-char eshell-last-output-end)
981 (delete-region (point) (point-max))
982 (call-interactively 'isearch-abort))
984 (defun eshell-isearch-delete-char ()
987 (isearch-delete-char)))
989 (defun eshell-isearch-return ()
997 ;; generated-autoload-file: "esh-groups.el"
1000 ;;; em-hist.el ends here