* lisp/emacs-lisp/bytecomp.el: Silence obsolete warnings more reliably.
[emacs.git] / lisp / eshell / em-hist.el
blobdb5e98062a9b1504fdc904e49e66eb92921050be
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/>.
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
58 (require 'cl))
60 (require 'ring)
61 (require 'esh-opt)
62 (require 'em-pred)
63 (require 'eshell)
65 ;;;###autoload
66 (eshell-defgroup eshell-hist nil
67 "This module provides command history management."
68 :tag "History list management"
69 :group 'eshell-module)
71 ;;; User Variables:
73 (defcustom eshell-hist-load-hook nil
74 "A list of functions to call when loading `eshell-hist'."
75 :version "24.1" ; removed eshell-hist-initialize
76 :type 'hook
77 :group 'eshell-hist)
79 (defcustom eshell-hist-unload-hook
80 (list
81 (function
82 (lambda ()
83 (remove-hook 'kill-emacs-hook 'eshell-save-some-history))))
84 "A hook that gets run when `eshell-hist' is unloaded."
85 :type 'hook
86 :group 'eshell-hist)
88 (defcustom eshell-history-file-name
89 (expand-file-name "history" eshell-directory-name)
90 "If non-nil, name of the file to read/write input history.
91 See also `eshell-read-history' and `eshell-write-history'.
92 If it is nil, Eshell will use the value of HISTFILE."
93 :type 'file
94 :group 'eshell-hist)
96 (defcustom eshell-history-size 128
97 "Size of the input history ring. If nil, use envvar HISTSIZE."
98 :type 'integer
99 :group 'eshell-hist)
101 (defcustom eshell-hist-ignoredups nil
102 "If non-nil, don't add input matching the last on the input ring.
103 This mirrors the optional behavior of bash."
104 :type 'boolean
105 :group 'eshell-hist)
107 (defcustom eshell-save-history-on-exit t
108 "Determine if history should be automatically saved.
109 History is always preserved after sanely exiting an Eshell buffer.
110 However, when Emacs is being shut down, this variable determines
111 whether to prompt the user.
112 If set to nil, it means never save history on termination of Emacs.
113 If set to `ask', ask if any Eshell buffers are open at exit time.
114 If set to t, history will always be saved, silently."
115 :type '(choice (const :tag "Never" nil)
116 (const :tag "Ask" ask)
117 (const :tag "Always save" t))
118 :group 'eshell-hist)
120 (defcustom eshell-input-filter
121 (function
122 (lambda (str)
123 (not (string-match "\\`\\s-*\\'" str))))
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 'function
129 :group 'eshell-hist)
131 (put 'eshell-input-filter 'risky-local-variable t)
133 (defcustom eshell-hist-match-partial t
134 "If non-nil, movement through history is constrained by current input.
135 Otherwise, typing <M-p> and <M-n> will always go to the next history
136 element, regardless of any text on the command line. In that case,
137 <C-c M-r> and <C-c M-s> still offer that functionality."
138 :type 'boolean
139 :group 'eshell-hist)
141 (defcustom eshell-hist-move-to-end t
142 "If non-nil, move to the end of the buffer before cycling history."
143 :type 'boolean
144 :group 'eshell-hist)
146 (defcustom eshell-hist-event-designator
147 "^!\\(!\\|-?[0-9]+\\|\\??[^:^$%*?]+\\??\\|#\\)"
148 "The regexp used to identifier history event designators."
149 :type 'regexp
150 :group 'eshell-hist)
152 (defcustom eshell-hist-word-designator
153 "^:?\\([0-9]+\\|[$^%*]\\)?\\(\\*\\|-[0-9]*\\|[$^%*]\\)?"
154 "The regexp used to identify history word designators."
155 :type 'regexp
156 :group 'eshell-hist)
158 (defcustom eshell-hist-modifier
159 "^\\(:\\([hretpqx&g]\\|s/\\([^/]*\\)/\\([^/]*\\)/\\)\\)*"
160 "The regexp used to identity history modifiers."
161 :type 'regexp
162 :group 'eshell-hist)
164 (defcustom eshell-hist-rebind-keys-alist
165 '(([(control ?p)] . eshell-previous-input)
166 ([(control ?n)] . eshell-next-input)
167 ([(control up)] . eshell-previous-input)
168 ([(control down)] . eshell-next-input)
169 ([(control ?r)] . eshell-isearch-backward)
170 ([(control ?s)] . eshell-isearch-forward)
171 ([(meta ?r)] . eshell-previous-matching-input)
172 ([(meta ?s)] . eshell-next-matching-input)
173 ([(meta ?p)] . eshell-previous-matching-input-from-input)
174 ([(meta ?n)] . eshell-next-matching-input-from-input)
175 ([up] . eshell-previous-matching-input-from-input)
176 ([down] . eshell-next-matching-input-from-input))
177 "History keys to bind differently if point is in input text."
178 :type '(repeat (cons (vector :tag "Keys to bind"
179 (repeat :inline t sexp))
180 (function :tag "Command")))
181 :group 'eshell-hist)
183 ;;; Internal Variables:
185 (defvar eshell-history-ring nil)
186 (defvar eshell-history-index nil)
187 (defvar eshell-matching-input-from-input-string "")
188 (defvar eshell-save-history-index nil)
190 (defvar eshell-isearch-map nil)
192 (unless eshell-isearch-map
193 (setq eshell-isearch-map (copy-keymap isearch-mode-map))
194 (define-key eshell-isearch-map [(control ?m)] 'eshell-isearch-return)
195 (define-key eshell-isearch-map [return] 'eshell-isearch-return)
196 (define-key eshell-isearch-map [(control ?r)] 'eshell-isearch-repeat-backward)
197 (define-key eshell-isearch-map [(control ?s)] 'eshell-isearch-repeat-forward)
198 (define-key eshell-isearch-map [(control ?g)] 'eshell-isearch-abort)
199 (define-key eshell-isearch-map [backspace] 'eshell-isearch-delete-char)
200 (define-key eshell-isearch-map [delete] 'eshell-isearch-delete-char)
201 (defvar eshell-isearch-cancel-map)
202 (define-prefix-command 'eshell-isearch-cancel-map)
203 (define-key eshell-isearch-map [(control ?c)] 'eshell-isearch-cancel-map)
204 (define-key eshell-isearch-cancel-map [(control ?c)] 'eshell-isearch-cancel))
206 (defvar eshell-rebind-keys-alist)
208 ;;; Functions:
210 (defun eshell-hist-initialize ()
211 "Initialize the history management code for one Eshell buffer."
212 (add-hook 'eshell-expand-input-functions
213 'eshell-expand-history-references nil t)
215 (when (eshell-using-module 'eshell-cmpl)
216 (add-hook 'pcomplete-try-first-hook
217 'eshell-complete-history-reference nil t))
219 (if (and (eshell-using-module 'eshell-rebind)
220 (not eshell-non-interactive-p))
221 (let ((rebind-alist eshell-rebind-keys-alist))
222 (make-local-variable 'eshell-rebind-keys-alist)
223 (setq eshell-rebind-keys-alist
224 (append rebind-alist eshell-hist-rebind-keys-alist))
225 (set (make-local-variable 'search-invisible) t)
226 (set (make-local-variable 'search-exit-option) t)
227 (add-hook 'isearch-mode-hook
228 (function
229 (lambda ()
230 (if (>= (point) eshell-last-output-end)
231 (setq overriding-terminal-local-map
232 eshell-isearch-map)))) nil t)
233 (add-hook 'isearch-mode-end-hook
234 (function
235 (lambda ()
236 (setq overriding-terminal-local-map nil))) nil t))
237 (define-key eshell-mode-map [up] 'eshell-previous-matching-input-from-input)
238 (define-key eshell-mode-map [down] 'eshell-next-matching-input-from-input)
239 (define-key eshell-mode-map [(control up)] 'eshell-previous-input)
240 (define-key eshell-mode-map [(control down)] 'eshell-next-input)
241 (define-key eshell-mode-map [(meta ?r)] 'eshell-previous-matching-input)
242 (define-key eshell-mode-map [(meta ?s)] 'eshell-next-matching-input)
243 (define-key eshell-command-map [(meta ?r)]
244 'eshell-previous-matching-input-from-input)
245 (define-key eshell-command-map [(meta ?s)]
246 'eshell-next-matching-input-from-input)
247 (if eshell-hist-match-partial
248 (progn
249 (define-key eshell-mode-map [(meta ?p)]
250 'eshell-previous-matching-input-from-input)
251 (define-key eshell-mode-map [(meta ?n)]
252 'eshell-next-matching-input-from-input)
253 (define-key eshell-command-map [(meta ?p)] 'eshell-previous-input)
254 (define-key eshell-command-map [(meta ?n)] 'eshell-next-input))
255 (define-key eshell-mode-map [(meta ?p)] 'eshell-previous-input)
256 (define-key eshell-mode-map [(meta ?n)] 'eshell-next-input)
257 (define-key eshell-command-map [(meta ?p)]
258 'eshell-previous-matching-input-from-input)
259 (define-key eshell-command-map [(meta ?n)]
260 'eshell-next-matching-input-from-input)))
262 (make-local-variable 'eshell-history-size)
263 (or eshell-history-size
264 (setq eshell-history-size (getenv "HISTSIZE")))
266 (make-local-variable 'eshell-history-file-name)
267 (or eshell-history-file-name
268 (setq eshell-history-file-name (getenv "HISTFILE")))
270 (make-local-variable 'eshell-history-index)
271 (make-local-variable 'eshell-save-history-index)
273 (if (minibuffer-window-active-p (selected-window))
274 (set (make-local-variable 'eshell-save-history-on-exit) nil)
275 (set (make-local-variable 'eshell-history-ring) nil)
276 (if eshell-history-file-name
277 (eshell-read-history nil t))
279 (add-hook 'eshell-exit-hook 'eshell-write-history nil t))
281 (unless eshell-history-ring
282 (setq eshell-history-ring (make-ring eshell-history-size)))
284 (add-hook 'eshell-exit-hook 'eshell-write-history nil t)
286 (add-hook 'kill-emacs-hook 'eshell-save-some-history)
288 (make-local-variable 'eshell-input-filter-functions)
289 (add-hook 'eshell-input-filter-functions 'eshell-add-to-history nil t)
291 (define-key eshell-command-map [(control ?l)] 'eshell-list-history)
292 (define-key eshell-command-map [(control ?x)] 'eshell-get-next-from-history))
294 (defun eshell-save-some-history ()
295 "Save the history for any open Eshell buffers."
296 (dolist (buf (buffer-list))
297 (if (buffer-live-p buf)
298 (with-current-buffer buf
299 (if (and eshell-mode
300 eshell-history-file-name
301 eshell-save-history-on-exit
302 (or (eq eshell-save-history-on-exit t)
303 (y-or-n-p
304 (format "Save input history for Eshell buffer `%s'? "
305 (buffer-name buf)))))
306 (eshell-write-history))))))
308 (defun eshell/history (&rest args)
309 "List in help buffer the buffer's input history."
310 (eshell-init-print-buffer)
311 (eshell-eval-using-options
312 "history" args
313 '((?r "read" nil read-history
314 "read from history file to current history list")
315 (?w "write" nil write-history
316 "write current history list to history file")
317 (?a "append" nil append-history
318 "append current history list to history file")
319 (?h "help" nil nil "display this usage message")
320 :usage "[n] [-rwa [filename]]"
321 :post-usage
322 "When Eshell is started, history is read from `eshell-history-file-name'.
323 This is also the location where history info will be saved by this command,
324 unless a different file is specified on the command line.")
325 (and (or (not (ring-p eshell-history-ring))
326 (ring-empty-p eshell-history-ring))
327 (error "No history"))
328 (let (length command file)
329 (when (and args (string-match "^[0-9]+$" (car args)))
330 (setq length (min (eshell-convert (car args))
331 (ring-length eshell-history-ring))
332 args (cdr args)))
333 (and length
334 (or read-history write-history append-history)
335 (error "history: extra arguments"))
336 (when (and args (stringp (car args)))
337 (setq file (car args)
338 args (cdr args)))
339 (cond
340 (read-history (eshell-read-history file))
341 (write-history (eshell-write-history file))
342 (append-history (eshell-write-history file t))
344 (let* ((history nil)
345 (index (1- (or length (ring-length eshell-history-ring))))
346 (ref (- (ring-length eshell-history-ring) index)))
347 ;; We have to build up a list ourselves from the ring vector.
348 (while (>= index 0)
349 (eshell-buffered-print
350 (format "%5d %s\n" ref (eshell-get-history index)))
351 (setq index (1- index)
352 ref (1+ ref)))))))
353 (eshell-flush)
354 nil))
356 (defun eshell-put-history (input &optional ring at-beginning)
357 "Put a new input line into the history ring."
358 (unless ring (setq ring eshell-history-ring))
359 (if at-beginning
360 (ring-insert-at-beginning ring input)
361 (ring-insert ring input)))
363 (defun eshell-get-history (index &optional ring)
364 "Get an input line from the history ring."
365 (ring-ref (or ring eshell-history-ring) index))
367 (defun eshell-add-input-to-history (input)
368 "Add the string INPUT to the history ring.
369 Input is entered into the input history ring, if the value of
370 variable `eshell-input-filter' returns non-nil when called on the
371 input."
372 (if (and (funcall eshell-input-filter input)
373 (or (null eshell-hist-ignoredups)
374 (not (ring-p eshell-history-ring))
375 (ring-empty-p eshell-history-ring)
376 (not (string-equal (eshell-get-history 0) input))))
377 (eshell-put-history input))
378 (setq eshell-save-history-index eshell-history-index)
379 (setq eshell-history-index nil))
381 (defun eshell-add-command-to-history ()
382 "Add the command entered at `eshell-command's prompt to the history ring.
383 The command is added to the input history ring, if the value of
384 variable `eshell-input-filter' returns non-nil when called on the
385 command.
387 This function is supposed to be called from the minibuffer, presumably
388 as a minibuffer-exit-hook."
389 (eshell-add-input-to-history
390 (buffer-substring (minibuffer-prompt-end) (point-max))))
392 (defun eshell-add-to-history ()
393 "Add last Eshell command to the history ring.
394 The command is entered into the input history ring, if the value of
395 variable `eshell-input-filter' returns non-nil when called on the
396 command."
397 (when (> (1- eshell-last-input-end) eshell-last-input-start)
398 (let ((input (buffer-substring eshell-last-input-start
399 (1- eshell-last-input-end))))
400 (eshell-add-input-to-history input))))
402 (defun eshell-read-history (&optional filename silent)
403 "Sets the buffer's `eshell-history-ring' from a history file.
404 The name of the file is given by the variable
405 `eshell-history-file-name'. The history ring is of size
406 `eshell-history-size', regardless of file size. If
407 `eshell-history-file-name' is nil this function does nothing.
409 If the optional argument SILENT is non-nil, we say nothing about a
410 failure to read the history file.
412 This function is useful for major mode commands and mode hooks.
414 The structure of the history file should be one input command per
415 line, with the most recent command last. See also
416 `eshell-hist-ignoredups' and `eshell-write-history'."
417 (let ((file (or filename eshell-history-file-name)))
418 (cond
419 ((or (null file)
420 (equal file ""))
421 nil)
422 ((not (file-readable-p file))
423 (or silent
424 (message "Cannot read history file %s" file)))
426 (let* ((count 0)
427 (size eshell-history-size)
428 (ring (make-ring size))
429 (ignore-dups eshell-hist-ignoredups))
430 (with-temp-buffer
431 (insert-file-contents file)
432 ;; Save restriction in case file is already visited...
433 ;; Watch for those date stamps in history files!
434 (goto-char (point-max))
435 (while (and (< count size)
436 (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$"
437 nil t))
438 (let ((history (match-string 1)))
439 (if (or (null ignore-dups)
440 (ring-empty-p ring)
441 (not (string-equal (ring-ref ring 0) history)))
442 (ring-insert-at-beginning
443 ring (subst-char-in-string ?\177 ?\n history))))
444 (setq count (1+ count))))
445 (setq eshell-history-ring ring
446 eshell-history-index nil))))))
448 (defun eshell-write-history (&optional filename append)
449 "Writes the buffer's `eshell-history-ring' to a history file.
450 The name of the file is given by the variable
451 `eshell-history-file-name'. The original contents of the file are
452 lost if `eshell-history-ring' is not empty. If
453 `eshell-history-file-name' is nil this function does nothing.
455 Useful within process sentinels.
457 See also `eshell-read-history'."
458 (let ((file (or filename eshell-history-file-name)))
459 (cond
460 ((or (null file)
461 (equal file "")
462 (null eshell-history-ring)
463 (ring-empty-p eshell-history-ring))
464 nil)
465 ((not (file-writable-p file))
466 (message "Cannot write history file %s" file))
468 (let* ((ring eshell-history-ring)
469 (index (ring-length ring)))
470 ;; Write it all out into a buffer first. Much faster, but
471 ;; messier, than writing it one line at a time.
472 (with-temp-buffer
473 (while (> index 0)
474 (setq index (1- index))
475 (let ((start (point)))
476 (insert (ring-ref ring index) ?\n)
477 (subst-char-in-region start (1- (point)) ?\n ?\177)))
478 (eshell-with-private-file-modes
479 (write-region (point-min) (point-max) file append
480 'no-message))))))))
482 (defun eshell-list-history ()
483 "List in help buffer the buffer's input history."
484 (interactive)
485 (let (prefix prelen)
486 (save-excursion
487 (if (re-search-backward "!\\(.+\\)" (line-beginning-position) t)
488 (setq prefix (match-string 1)
489 prelen (length prefix))))
490 (if (or (not (ring-p eshell-history-ring))
491 (ring-empty-p eshell-history-ring))
492 (message "No history")
493 (let ((history nil)
494 (history-buffer " *Input History*")
495 (index (1- (ring-length eshell-history-ring)))
496 (conf (current-window-configuration)))
497 ;; We have to build up a list ourselves from the ring vector.
498 (while (>= index 0)
499 (let ((hist (eshell-get-history index)))
500 (if (or (not prefix)
501 (and (>= (length hist) prelen)
502 (string= (substring hist 0 prelen) prefix)))
503 (setq history (cons hist history))))
504 (setq index (1- index)))
505 ;; Change "completion" to "history reference"
506 ;; to make the display accurate.
507 (with-output-to-temp-buffer history-buffer
508 (display-completion-list history prefix)
509 (set-buffer history-buffer)
510 (forward-line 3)
511 (while (search-backward "completion" nil 'move)
512 (replace-match "history reference")))
513 (eshell-redisplay)
514 (message "Hit space to flush")
515 (let ((ch (read-event)))
516 (if (eq ch ?\ )
517 (set-window-configuration conf)
518 (setq unread-command-events (list ch))))))))
520 (defun eshell-hist-word-reference (ref)
521 "Return the word designator index referred to by REF."
522 (cond
523 ((string-match "^[0-9]+$" ref)
524 (string-to-number ref))
525 ((string= "^" ref) 1)
526 ((string= "$" ref) nil)
527 ((string= "%" ref)
528 (error "`%%' history word designator not yet implemented"))))
530 (defun eshell-hist-parse-arguments (&optional silent b e)
531 "Parse current command arguments in a history-code-friendly way."
532 (let ((end (or e (point)))
533 (begin (or b (save-excursion (eshell-bol) (point))))
534 (posb (list t))
535 (pose (list t))
536 (textargs (list t))
537 hist args)
538 (unless (catch 'eshell-incomplete
539 (ignore
540 (setq args (eshell-parse-arguments begin end))))
541 (save-excursion
542 (goto-char begin)
543 (while (< (point) end)
544 (if (get-text-property (point) 'arg-begin)
545 (nconc posb (list (point))))
546 (if (get-text-property (point) 'arg-end)
547 (nconc pose
548 (list (if (= (1+ (point)) end)
549 (1+ (point))
550 (point)))))
551 (forward-char))
552 (setq posb (cdr posb)
553 pose (cdr pose))
554 (assert (= (length posb) (length args)))
555 (assert (<= (length posb) (length pose))))
556 (setq hist (buffer-substring-no-properties begin end))
557 (let ((b posb) (e pose))
558 (while b
559 (nconc textargs
560 (list (substring hist (- (car b) begin)
561 (- (car e) begin))))
562 (setq b (cdr b)
563 e (cdr e))))
564 (setq textargs (cdr textargs))
565 (assert (= (length textargs) (length args)))
566 (list textargs posb pose))))
568 (defun eshell-expand-history-references (beg end)
569 "Parse and expand any history references in current input."
570 (let ((result (eshell-hist-parse-arguments t beg end)))
571 (when result
572 (let ((textargs (nreverse (nth 0 result)))
573 (posb (nreverse (nth 1 result)))
574 (pose (nreverse (nth 2 result))))
575 (save-excursion
576 (while textargs
577 (let ((str (eshell-history-reference (car textargs))))
578 (unless (eq str (car textargs))
579 (goto-char (car posb))
580 (insert-and-inherit str)
581 (delete-char (- (car pose) (car posb)))))
582 (setq textargs (cdr textargs)
583 posb (cdr posb)
584 pose (cdr pose))))))))
586 (defvar pcomplete-stub)
587 (defvar pcomplete-last-completion-raw)
588 (declare-function pcomplete-actual-arg "pcomplete")
590 (defun eshell-complete-history-reference ()
591 "Complete a history reference, by completing the event designator."
592 (let ((arg (pcomplete-actual-arg)))
593 (when (string-match "\\`![^:^$*%]*\\'" arg)
594 (setq pcomplete-stub (substring arg 1)
595 pcomplete-last-completion-raw t)
596 (throw 'pcomplete-completions
597 (let ((history nil)
598 (index (1- (ring-length eshell-history-ring)))
599 (stublen (length pcomplete-stub)))
600 ;; We have to build up a list ourselves from the ring
601 ;; vector.
602 (while (>= index 0)
603 (let ((hist (eshell-get-history index)))
604 (if (and (>= (length hist) stublen)
605 (string= (substring hist 0 stublen)
606 pcomplete-stub)
607 (string-match "^\\([^:^$*% \t\n]+\\)" hist))
608 (setq history (cons (match-string 1 hist)
609 history))))
610 (setq index (1- index)))
611 (let ((fhist (list t)))
612 ;; uniquify the list, but preserve the order
613 (while history
614 (unless (member (car history) fhist)
615 (nconc fhist (list (car history))))
616 (setq history (cdr history)))
617 (cdr fhist)))))))
619 (defun eshell-history-reference (reference)
620 "Expand directory stack REFERENCE.
621 The syntax used here was taken from the Bash info manual.
622 Returns the resultant reference, or the same string REFERENCE if none
623 matched."
624 ;; `^string1^string2^'
625 ;; Quick Substitution. Repeat the last command, replacing
626 ;; STRING1 with STRING2. Equivalent to `!!:s/string1/string2/'
627 (if (and (eshell-using-module 'eshell-pred)
628 (string-match "\\^\\([^^]+\\)\\^\\([^^]+\\)\\^?\\s-*$"
629 reference))
630 (setq reference (format "!!:s/%s/%s/"
631 (match-string 1 reference)
632 (match-string 2 reference))))
633 ;; `!'
634 ;; Start a history substitution, except when followed by a
635 ;; space, tab, the end of the line, = or (.
636 (if (not (string-match "^![^ \t\n=\(]" reference))
637 reference
638 (setq eshell-history-index nil)
639 (let ((event (eshell-hist-parse-event-designator reference)))
640 (unless event
641 (error "Could not find history event `%s'" reference))
642 (setq eshell-history-index (car event)
643 reference (substring reference (cdr event))
644 event (eshell-get-history eshell-history-index))
645 (if (not (string-match "^[:^$*%]" reference))
646 event
647 (let ((word (eshell-hist-parse-word-designator
648 event reference)))
649 (unless word
650 (error "Unable to honor word designator `%s'" reference))
651 (unless (string-match "^[:^$*%][[$^*%0-9-]" reference)
652 (setcdr word 0))
653 (setq event (car word)
654 reference (substring reference (cdr word)))
655 (if (not (and (eshell-using-module 'eshell-pred)
656 (string-match "^:" reference)))
657 event
658 (eshell-hist-parse-modifier event reference)))))))
660 (defun eshell-hist-parse-event-designator (reference)
661 "Parse a history event designator beginning in REFERENCE."
662 (let* ((index (string-match eshell-hist-event-designator reference))
663 (end (and index (match-end 0))))
664 (unless index
665 (error "Invalid history event designator `%s'" reference))
666 (let* ((event (match-string 1 reference))
667 (pos
668 (cond
669 ((string= event "!") (ring-length eshell-history-ring))
670 ((string= event "#") (error "!# not yet implemented"))
671 ((string-match "^-?[0-9]+$" event)
672 (let ((num (string-to-number event)))
673 (if (>= num 0)
674 (- (ring-length eshell-history-ring) num)
675 (1- (abs num)))))
676 ((string-match "^\\(\\??\\)\\([^?]+\\)\\??$" event)
677 (let ((pref (if (> (length (match-string 1 event)) 0)
678 "" "^"))
679 (str (match-string 2 event)))
680 (save-match-data
681 (eshell-previous-matching-input-string-position
682 (concat pref (regexp-quote str)) 1))))
684 (error "Failed to parse event designator `%s'" event)))))
685 (and pos (cons pos end)))))
687 (defun eshell-hist-parse-word-designator (hist reference)
688 "Parse a history word designator beginning for HIST in REFERENCE."
689 (let* ((index (string-match eshell-hist-word-designator reference))
690 (end (and index (match-end 0))))
691 (unless (memq (aref reference 0) '(?: ?^ ?$ ?* ?%))
692 (error "Invalid history word designator `%s'" reference))
693 (let ((nth (match-string 1 reference))
694 (mth (match-string 2 reference))
695 (here (point))
696 textargs)
697 (insert hist)
698 (setq textargs (car (eshell-hist-parse-arguments nil here (point))))
699 (delete-region here (point))
700 (if (string= nth "*")
701 (if mth
702 (error "Invalid history word designator `%s'"
703 reference)
704 (setq nth 1 mth "-$")))
705 (if (not mth)
706 (if nth
707 (setq mth nth)
708 (setq nth 0 mth "$"))
709 (if (string= mth "-")
710 (setq mth (- (length textargs) 2))
711 (if (string= mth "*")
712 (setq mth "$")
713 (if (not (and (> (length mth) 1)
714 (eq (aref mth 0) ?-)))
715 (error "Invalid history word designator `%s'"
716 reference)
717 (setq mth (substring mth 1))))))
718 (unless (numberp nth)
719 (setq nth (eshell-hist-word-reference nth)))
720 (unless (numberp mth)
721 (setq mth (eshell-hist-word-reference mth)))
722 (cons (mapconcat 'identity (eshell-sublist textargs nth mth) "")
723 end))))
725 (defun eshell-hist-parse-modifier (hist reference)
726 "Parse a history modifier beginning for HIST in REFERENCE."
727 (let ((here (point)))
728 (insert reference)
729 (prog1
730 (save-restriction
731 (narrow-to-region here (point))
732 (goto-char (point-min))
733 (let ((modifiers (cdr (eshell-parse-modifiers))))
734 (dolist (mod modifiers)
735 (setq hist (funcall mod hist)))
736 hist))
737 (delete-region here (point)))))
739 (defun eshell-get-next-from-history ()
740 "After fetching a line from input history, this fetches the next.
741 In other words, this recalls the input line after the line you
742 recalled last. You can use this to repeat a sequence of input lines."
743 (interactive)
744 (if eshell-save-history-index
745 (progn
746 (setq eshell-history-index (1+ eshell-save-history-index))
747 (eshell-next-input 1))
748 (message "No previous history command")))
750 (defun eshell-search-arg (arg)
751 ;; First make sure there is a ring and that we are after the process
752 ;; mark
753 (if (and eshell-hist-move-to-end
754 (< (point) eshell-last-output-end))
755 (goto-char eshell-last-output-end))
756 (cond ((or (null eshell-history-ring)
757 (ring-empty-p eshell-history-ring))
758 (error "Empty input ring"))
759 ((zerop arg)
760 ;; arg of zero resets search from beginning, and uses arg of
761 ;; 1
762 (setq eshell-history-index nil)
765 arg)))
767 (defun eshell-search-start (arg)
768 "Index to start a directional search, starting at `eshell-history-index'."
769 (if eshell-history-index
770 ;; If a search is running, offset by 1 in direction of arg
771 (mod (+ eshell-history-index (if (> arg 0) 1 -1))
772 (ring-length eshell-history-ring))
773 ;; For a new search, start from beginning or end, as appropriate
774 (if (>= arg 0)
775 0 ; First elt for forward search
776 ;; Last elt for backward search
777 (1- (ring-length eshell-history-ring)))))
779 (defun eshell-previous-input-string (arg)
780 "Return the string ARG places along the input ring.
781 Moves relative to `eshell-history-index'."
782 (eshell-get-history (if eshell-history-index
783 (mod (+ arg eshell-history-index)
784 (ring-length eshell-history-ring))
785 arg)))
787 (defun eshell-previous-input (arg)
788 "Cycle backwards through input history."
789 (interactive "*p")
790 (eshell-previous-matching-input "." arg))
792 (defun eshell-next-input (arg)
793 "Cycle forwards through input history."
794 (interactive "*p")
795 (eshell-previous-input (- arg)))
797 (defun eshell-previous-matching-input-string (regexp arg)
798 "Return the string matching REGEXP ARG places along the input ring.
799 Moves relative to `eshell-history-index'."
800 (let* ((pos (eshell-previous-matching-input-string-position regexp arg)))
801 (if pos (eshell-get-history pos))))
803 (defun eshell-previous-matching-input-string-position
804 (regexp arg &optional start)
805 "Return the index matching REGEXP ARG places along the input ring.
806 Moves relative to START, or `eshell-history-index'."
807 (if (or (not (ring-p eshell-history-ring))
808 (ring-empty-p eshell-history-ring))
809 (error "No history"))
810 (let* ((len (ring-length eshell-history-ring))
811 (motion (if (> arg 0) 1 -1))
812 (n (mod (- (or start (eshell-search-start arg)) motion) len))
813 (tried-each-ring-item nil)
814 (case-fold-search (eshell-under-windows-p))
815 (prev nil))
816 ;; Do the whole search as many times as the argument says.
817 (while (and (/= arg 0) (not tried-each-ring-item))
818 ;; Step once.
819 (setq prev n
820 n (mod (+ n motion) len))
821 ;; If we haven't reached a match, step some more.
822 (while (and (< n len) (not tried-each-ring-item)
823 (not (string-match regexp (eshell-get-history n))))
824 (setq n (mod (+ n motion) len)
825 ;; If we have gone all the way around in this search.
826 tried-each-ring-item (= n prev)))
827 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
828 ;; Now that we know which ring element to use, if we found it,
829 ;; return that.
830 (if (string-match regexp (eshell-get-history n))
831 n)))
833 (defun eshell-previous-matching-input (regexp arg)
834 "Search backwards through input history for match for REGEXP.
835 \(Previous history elements are earlier commands.)
836 With prefix argument N, search for Nth previous match.
837 If N is negative, find the next or Nth next match."
838 (interactive (eshell-regexp-arg "Previous input matching (regexp): "))
839 (setq arg (eshell-search-arg arg))
840 (if (> eshell-last-output-end (point))
841 (error "Point not located after prompt"))
842 (let ((pos (eshell-previous-matching-input-string-position regexp arg)))
843 ;; Has a match been found?
844 (if (null pos)
845 (error "Not found")
846 (setq eshell-history-index pos)
847 (unless (minibuffer-window-active-p (selected-window))
848 (message "History item: %d" (- (ring-length eshell-history-ring) pos)))
849 ;; Can't use kill-region as it sets this-command
850 (delete-region eshell-last-output-end (point))
851 (insert-and-inherit (eshell-get-history pos)))))
853 (defun eshell-next-matching-input (regexp arg)
854 "Search forwards through input history for match for REGEXP.
855 \(Later history elements are more recent commands.)
856 With prefix argument N, search for Nth following match.
857 If N is negative, find the previous or Nth previous match."
858 (interactive (eshell-regexp-arg "Next input matching (regexp): "))
859 (eshell-previous-matching-input regexp (- arg)))
861 (defun eshell-previous-matching-input-from-input (arg)
862 "Search backwards through input history for match for current input.
863 \(Previous history elements are earlier commands.)
864 With prefix argument N, search for Nth previous match.
865 If N is negative, search forwards for the -Nth following match."
866 (interactive "p")
867 (if (not (memq last-command '(eshell-previous-matching-input-from-input
868 eshell-next-matching-input-from-input)))
869 ;; Starting a new search
870 (setq eshell-matching-input-from-input-string
871 (buffer-substring (save-excursion (eshell-bol) (point))
872 (point))
873 eshell-history-index nil))
874 (eshell-previous-matching-input
875 (concat "^" (regexp-quote eshell-matching-input-from-input-string))
876 arg))
878 (defun eshell-next-matching-input-from-input (arg)
879 "Search forwards through input history for match for current input.
880 \(Following history elements are more recent commands.)
881 With prefix argument N, search for Nth following match.
882 If N is negative, search backwards for the -Nth previous match."
883 (interactive "p")
884 (eshell-previous-matching-input-from-input (- arg)))
886 (defun eshell-test-imatch ()
887 "If isearch match good, put point at the beginning and return non-nil."
888 (if (get-text-property (point) 'history)
889 (progn (beginning-of-line) t)
890 (let ((before (point)))
891 (eshell-bol)
892 (if (and (not (bolp))
893 (<= (point) before))
895 (if isearch-forward
896 (progn
897 (end-of-line)
898 (forward-char))
899 (beginning-of-line)
900 (backward-char))))))
902 (defun eshell-return-to-prompt ()
903 "Once a search string matches, insert it at the end and go there."
904 (setq isearch-other-end nil)
905 (let ((found (eshell-test-imatch)) before)
906 (while (and (not found)
907 (setq before
908 (funcall (if isearch-forward
909 're-search-forward
910 're-search-backward)
911 isearch-string nil t)))
912 (setq found (eshell-test-imatch)))
913 (if (not found)
914 (progn
915 (goto-char eshell-last-output-end)
916 (delete-region (point) (point-max)))
917 (setq before (point))
918 (let ((text (buffer-substring-no-properties
919 (point) (line-end-position)))
920 (orig (marker-position eshell-last-output-end)))
921 (goto-char eshell-last-output-end)
922 (delete-region (point) (point-max))
923 (when (and text (> (length text) 0))
924 (insert text)
925 (put-text-property (1- (point)) (point)
926 'last-search-pos before)
927 (set-marker eshell-last-output-end orig)
928 (goto-char eshell-last-output-end))))))
930 (defun eshell-prepare-for-search ()
931 "Make sure the old history file is at the beginning of the buffer."
932 (unless (get-text-property (point-min) 'history)
933 (save-excursion
934 (goto-char (point-min))
935 (let ((end (copy-marker (point) t)))
936 (insert-file-contents eshell-history-file-name)
937 (set-text-properties (point-min) end
938 '(history t invisible t))))))
940 (defun eshell-isearch-backward (&optional invert)
941 "Do incremental regexp search backward through past commands."
942 (interactive)
943 (let ((inhibit-read-only t) end)
944 (eshell-prepare-for-search)
945 (goto-char (point-max))
946 (set-marker eshell-last-output-end (point))
947 (delete-region (point) (point-max)))
948 (isearch-mode invert t 'eshell-return-to-prompt))
950 (defun eshell-isearch-repeat-backward (&optional invert)
951 "Do incremental regexp search backward through past commands."
952 (interactive)
953 (let ((old-pos (get-text-property (1- (point-max))
954 'last-search-pos)))
955 (when old-pos
956 (goto-char old-pos)
957 (if invert
958 (end-of-line)
959 (backward-char)))
960 (setq isearch-forward invert)
961 (isearch-search-and-update)))
963 (defun eshell-isearch-forward ()
964 "Do incremental regexp search backward through past commands."
965 (interactive)
966 (eshell-isearch-backward t))
968 (defun eshell-isearch-repeat-forward ()
969 "Do incremental regexp search backward through past commands."
970 (interactive)
971 (eshell-isearch-repeat-backward t))
973 (defun eshell-isearch-cancel ()
974 (interactive)
975 (goto-char eshell-last-output-end)
976 (delete-region (point) (point-max))
977 (call-interactively 'isearch-cancel))
979 (defun eshell-isearch-abort ()
980 (interactive)
981 (goto-char eshell-last-output-end)
982 (delete-region (point) (point-max))
983 (call-interactively 'isearch-abort))
985 (defun eshell-isearch-delete-char ()
986 (interactive)
987 (save-excursion
988 (isearch-delete-char)))
990 (defun eshell-isearch-return ()
991 (interactive)
992 (isearch-done)
993 (eshell-send-input))
995 (provide 'em-hist)
997 ;; Local Variables:
998 ;; generated-autoload-file: "esh-groups.el"
999 ;; End:
1001 ;;; em-hist.el ends here