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