(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / eshell / em-hist.el
blob573d06337db27c9f522c79beb7744acb7463672c
1 ;;; em-hist.el --- history list management
3 ;; Copyright (C) 1999, 2000, 2004 Free Software Foundation
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 2, or (at your option)
12 ;; 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; 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.
24 (provide 'em-hist)
26 (eval-when-compile (require 'esh-maint))
27 (require 'eshell)
29 (defgroup eshell-hist nil
30 "This module provides command history management."
31 :tag "History list management"
32 :group 'eshell-module)
34 ;;; Commentary:
36 ;; Eshell's history facility imitates the syntax used by bash
37 ;; ([(bash)History Interaction]). Thus:
39 ;; !ls ; repeat the last command beginning with 'ls'
40 ;; !?ls ; repeat the last command containing ls
41 ;; echo !ls:2 ; echo the second arg of the last 'ls' command
42 ;; !ls<tab> ; complete against all possible words in this
43 ;; ; position, by looking at the history list
44 ;; !ls<C-c SPC> ; expand any matching history input at point
46 ;; Also, most of `comint-mode's keybindings are accepted:
48 ;; M-r ; search backward for a previous command by regexp
49 ;; M-s ; search forward for a previous command by regexp
50 ;; M-p ; access the last command entered, repeatable
51 ;; M-n ; access the first command entered, repeatable
53 ;; C-c M-r ; using current input, find a matching command thus, with
54 ;; ; 'ls' as the current input, it will go back to the same
55 ;; ; command that '!ls' would have selected
56 ;; C-c M-s ; same, but in reverse order
58 ;; Note that some of these keybindings are only available if the
59 ;; `eshell-rebind' is not in use, in which case M-p does what C-c M-r
60 ;; normally would do, and C-p is used instead of M-p. It may seem
61 ;; confusing, but the intention is to make the most useful
62 ;; functionality the most easily accessible. If `eshell-rebind' is
63 ;; not being used, history navigation will use comint's keybindings;
64 ;; if it is, history navigation tries to use similar keybindings to
65 ;; bash. This is all configurable, of course.
67 ;;; Code:
69 (require 'ring)
70 (require 'esh-opt)
71 (require 'em-pred)
73 ;;; User Variables:
75 (defcustom eshell-hist-load-hook '(eshell-hist-initialize)
76 "*A list of functions to call when loading `eshell-hist'."
77 :type 'hook
78 :group 'eshell-hist)
80 (defcustom eshell-hist-unload-hook
81 (list
82 (function
83 (lambda ()
84 (remove-hook 'kill-emacs-hook 'eshell-save-some-history))))
85 "*A hook that gets run when `eshell-hist' is unloaded."
86 :type 'hook
87 :group 'eshell-hist)
89 (defcustom eshell-history-file-name
90 (concat eshell-directory-name "history")
91 "*If non-nil, name of the file to read/write input history.
92 See also `eshell-read-history' and `eshell-write-history'.
93 If it is nil, Eshell will use the value of HISTFILE."
94 :type 'file
95 :group 'eshell-hist)
97 (defcustom eshell-history-size 128
98 "*Size of the input history ring. If nil, use envvar HISTSIZE."
99 :type 'integer
100 :group 'eshell-hist)
102 (defcustom eshell-hist-ignoredups nil
103 "*If non-nil, don't add input matching the last on the input ring.
104 This mirrors the optional behavior of bash."
105 :type 'boolean
106 :group 'eshell-hist)
108 (defcustom eshell-save-history-on-exit 'ask
109 "*Determine if history should be automatically saved.
110 History is always preserved after sanely exiting an Eshell buffer.
111 However, when Emacs is being shut down, this variable determines
112 whether to prompt the user.
113 If set to nil, it means never save history on termination of Emacs.
114 If set to `ask', ask if any Eshell buffers are open at exit time.
115 If set to t, history will always be saved, silently."
116 :type '(choice (const :tag "Never" nil)
117 (const :tag "Ask" ask)
118 (const :tag "Always save" t))
119 :group 'eshell-hist)
121 (defcustom eshell-input-filter
122 (function
123 (lambda (str)
124 (not (string-match "\\`\\s-*\\'" str))))
125 "*Predicate for filtering additions to input history.
126 Takes one argument, the input. If non-nil, the input may be saved on
127 the input history list. Default is to save anything that isn't all
128 whitespace."
129 :type 'function
130 :group 'eshell-hist)
132 (put 'eshell-input-filter 'risky-local-variable t)
134 (defcustom eshell-hist-match-partial t
135 "*If non-nil, movement through history is constrained by current input.
136 Otherwise, typing <M-p> and <M-n> will always go to the next history
137 element, regardless of any text on the command line. In that case,
138 <C-c M-r> and <C-c M-s> still offer that functionality."
139 :type 'boolean
140 :group 'eshell-hist)
142 (defcustom eshell-hist-move-to-end t
143 "*If non-nil, move to the end of the buffer before cycling history."
144 :type 'boolean
145 :group 'eshell-hist)
147 (defcustom eshell-hist-event-designator
148 "^!\\(!\\|-?[0-9]+\\|\\??[^:^$%*?]+\\??\\|#\\)"
149 "*The regexp used to identifier history event designators."
150 :type 'regexp
151 :group 'eshell-hist)
153 (defcustom eshell-hist-word-designator
154 "^:?\\([0-9]+\\|[$^%*]\\)?\\(\\*\\|-[0-9]*\\|[$^%*]\\)?"
155 "*The regexp used to identify history word designators."
156 :type 'regexp
157 :group 'eshell-hist)
159 (defcustom eshell-hist-modifier
160 "^\\(:\\([hretpqx&g]\\|s/\\([^/]*\\)/\\([^/]*\\)/\\)\\)*"
161 "*The regexp used to identity history modifiers."
162 :type 'regexp
163 :group 'eshell-hist)
165 (defcustom eshell-hist-rebind-keys-alist
166 '(([(control ?p)] . eshell-previous-input)
167 ([(control ?n)] . eshell-next-input)
168 ([(control up)] . eshell-previous-input)
169 ([(control down)] . eshell-next-input)
170 ([(control ?r)] . eshell-isearch-backward)
171 ([(control ?s)] . eshell-isearch-forward)
172 ([(meta ?r)] . eshell-previous-matching-input)
173 ([(meta ?s)] . eshell-next-matching-input)
174 ([(meta ?p)] . eshell-previous-matching-input-from-input)
175 ([(meta ?n)] . eshell-next-matching-input-from-input)
176 ([up] . eshell-previous-matching-input-from-input)
177 ([down] . eshell-next-matching-input-from-input))
178 "*History keys to bind differently if point is in input text."
179 :type '(repeat (cons (vector :tag "Keys to bind"
180 (repeat :inline t sexp))
181 (function :tag "Command")))
182 :group 'eshell-hist)
184 ;;; Internal Variables:
186 (defvar eshell-history-ring nil)
187 (defvar eshell-history-index nil)
188 (defvar eshell-matching-input-from-input-string "")
189 (defvar eshell-save-history-index nil)
191 (defvar eshell-isearch-map nil)
193 (unless eshell-isearch-map
194 (setq eshell-isearch-map (copy-keymap isearch-mode-map))
195 (define-key eshell-isearch-map [(control ?m)] 'eshell-isearch-return)
196 (define-key eshell-isearch-map [return] 'eshell-isearch-return)
197 (define-key eshell-isearch-map [(control ?r)] 'eshell-isearch-repeat-backward)
198 (define-key eshell-isearch-map [(control ?s)] 'eshell-isearch-repeat-forward)
199 (define-key eshell-isearch-map [(control ?g)] 'eshell-isearch-abort)
200 (define-key eshell-isearch-map [backspace] 'eshell-isearch-delete-char)
201 (define-key eshell-isearch-map [delete] 'eshell-isearch-delete-char)
202 (defvar eshell-isearch-cancel-map)
203 (define-prefix-command 'eshell-isearch-cancel-map)
204 (define-key eshell-isearch-map [(control ?c)] 'eshell-isearch-cancel-map)
205 (define-key eshell-isearch-cancel-map [(control ?c)] 'eshell-isearch-cancel))
207 ;;; Functions:
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 (symbol-value 'eshell-rebind-keys-alist)))
221 (make-local-variable 'eshell-rebind-keys-alist)
222 (set '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
227 (function
228 (lambda ()
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
233 (function
234 (lambda ()
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
247 (progn
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
298 (if (and eshell-mode
299 eshell-history-file-name
300 eshell-save-history-on-exit
301 (or (eq eshell-save-history-on-exit t)
302 (y-or-n-p
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
311 "history" args
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]]"
320 :post-usage
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))
331 args (cdr args)))
332 (and length
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)
337 args (cdr args)))
338 (cond
339 (read-history (eshell-read-history file))
340 (write-history (eshell-write-history file))
341 (append-history (eshell-write-history file t))
343 (let* ((history nil)
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.
347 (while (>= index 0)
348 (eshell-buffered-print
349 (format "%5d %s\n" ref (eshell-get-history index)))
350 (setq index (1- index)
351 ref (1+ ref)))))))
352 (eshell-flush)
353 nil))
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))
358 (if at-beginning
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
370 input."
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
384 command.
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
395 command."
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)))
417 (cond
418 ((or (null file)
419 (equal file ""))
420 nil)
421 ((not (file-readable-p file))
422 (or silent
423 (message "Cannot read history file %s" file)))
425 (let* ((count 0)
426 (size eshell-history-size)
427 (ring (make-ring size))
428 (ignore-dups eshell-hist-ignoredups))
429 (with-temp-buffer
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]*$"
436 nil t))
437 (let ((history (match-string 1)))
438 (if (or (null ignore-dups)
439 (ring-empty-p ring)
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)))
458 (cond
459 ((or (null file)
460 (equal file "")
461 (null eshell-history-ring)
462 (ring-empty-p eshell-history-ring))
463 nil)
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.
471 (with-temp-buffer
472 (while (> index 0)
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
479 'no-message))))))))
481 (defun eshell-list-history ()
482 "List in help buffer the buffer's input history."
483 (interactive)
484 (let (prefix prelen)
485 (save-excursion
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")
492 (let ((history nil)
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.
497 (while (>= index 0)
498 (let ((hist (eshell-get-history index)))
499 (if (or (not prefix)
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)
508 (set-buffer history-buffer)
509 (forward-line 3)
510 (while (search-backward "completion" nil 'move)
511 (replace-match "history reference")))
512 (eshell-redisplay)
513 (message "Hit space to flush")
514 (let ((ch (read-event)))
515 (if (eq ch ?\ )
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."
521 (cond
522 ((string-match "^[0-9]+$" ref)
523 (string-to-number ref))
524 ((string= "^" ref) 1)
525 ((string= "$" ref) nil)
526 ((string= "%" ref)
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))))
533 (posb (list t))
534 (pose (list t))
535 (textargs (list t))
536 hist args)
537 (unless (catch 'eshell-incomplete
538 (ignore
539 (setq args (eshell-parse-arguments begin end))))
540 (save-excursion
541 (goto-char begin)
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)
546 (nconc pose
547 (list (if (= (1+ (point)) end)
548 (1+ (point))
549 (point)))))
550 (forward-char))
551 (setq posb (cdr posb)
552 pose (cdr pose))
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))
557 (while b
558 (nconc textargs
559 (list (substring hist (- (car b) begin)
560 (- (car e) begin))))
561 (setq b (cdr b)
562 e (cdr e))))
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)))
570 (when result
571 (let ((textargs (nreverse (nth 0 result)))
572 (posb (nreverse (nth 1 result)))
573 (pose (nreverse (nth 2 result))))
574 (save-excursion
575 (while textargs
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)
582 posb (cdr posb)
583 pose (cdr pose))))))))
585 (defun eshell-complete-history-reference ()
586 "Complete a history reference, by completing the event designator."
587 (let ((arg (pcomplete-actual-arg)))
588 (when (string-match "\\`![^:^$*%]*\\'" arg)
589 (setq pcomplete-stub (substring arg 1)
590 pcomplete-last-completion-raw t)
591 (throw 'pcomplete-completions
592 (let ((history nil)
593 (index (1- (ring-length eshell-history-ring)))
594 (stublen (length pcomplete-stub)))
595 ;; We have to build up a list ourselves from the ring
596 ;; vector.
597 (while (>= index 0)
598 (let ((hist (eshell-get-history index)))
599 (if (and (>= (length hist) stublen)
600 (string= (substring hist 0 stublen)
601 pcomplete-stub)
602 (string-match "^\\([^:^$*% \t\n]+\\)" hist))
603 (setq history (cons (match-string 1 hist)
604 history))))
605 (setq index (1- index)))
606 (let ((fhist (list t)))
607 ;; uniqify the list, but preserve the order
608 (while history
609 (unless (member (car history) fhist)
610 (nconc fhist (list (car history))))
611 (setq history (cdr history)))
612 (cdr fhist)))))))
614 (defun eshell-history-reference (reference)
615 "Expand directory stack REFERENCE.
616 The syntax used here was taken from the Bash info manual.
617 Returns the resultant reference, or the same string REFERENCE if none
618 matched."
619 ;; `^string1^string2^'
620 ;; Quick Substitution. Repeat the last command, replacing
621 ;; STRING1 with STRING2. Equivalent to `!!:s/string1/string2/'
622 (if (and (eshell-using-module 'eshell-pred)
623 (string-match "\\^\\([^^]+\\)\\^\\([^^]+\\)\\^?\\s-*$"
624 reference))
625 (setq reference (format "!!:s/%s/%s/"
626 (match-string 1 reference)
627 (match-string 2 reference))))
628 ;; `!'
629 ;; Start a history substitution, except when followed by a
630 ;; space, tab, the end of the line, = or (.
631 (if (not (string-match "^![^ \t\n=\(]" reference))
632 reference
633 (setq eshell-history-index nil)
634 (let ((event (eshell-hist-parse-event-designator reference)))
635 (unless event
636 (error "Could not find history event `%s'" reference))
637 (setq eshell-history-index (car event)
638 reference (substring reference (cdr event))
639 event (eshell-get-history eshell-history-index))
640 (if (not (string-match "^[:^$*%]" reference))
641 event
642 (let ((word (eshell-hist-parse-word-designator
643 event reference)))
644 (unless word
645 (error "Unable to honor word designator `%s'" reference))
646 (unless (string-match "^[:^$*%][[$^*%0-9-]" reference)
647 (setcdr word 0))
648 (setq event (car word)
649 reference (substring reference (cdr word)))
650 (if (not (and (eshell-using-module 'eshell-pred)
651 (string-match "^:" reference)))
652 event
653 (eshell-hist-parse-modifier event reference)))))))
655 (defun eshell-hist-parse-event-designator (reference)
656 "Parse a history event designator beginning in REFERENCE."
657 (let* ((index (string-match eshell-hist-event-designator reference))
658 (end (and index (match-end 0))))
659 (unless index
660 (error "Invalid history event designator `%s'" reference))
661 (let* ((event (match-string 1 reference))
662 (pos
663 (cond
664 ((string= event "!") (ring-length eshell-history-ring))
665 ((string= event "#") (error "!# not yet implemented"))
666 ((string-match "^-?[0-9]+$" event)
667 (let ((num (string-to-number event)))
668 (if (>= num 0)
669 (- (ring-length eshell-history-ring) num)
670 (1- (abs num)))))
671 ((string-match "^\\(\\??\\)\\([^?]+\\)\\??$" event)
672 (let ((pref (if (> (length (match-string 1 event)) 0)
673 "" "^"))
674 (str (match-string 2 event)))
675 (save-match-data
676 (eshell-previous-matching-input-string-position
677 (concat pref (regexp-quote str)) 1))))
679 (error "Failed to parse event designator `%s'" event)))))
680 (and pos (cons pos end)))))
682 (defun eshell-hist-parse-word-designator (hist reference)
683 "Parse a history word designator beginning for HIST in REFERENCE."
684 (let* ((index (string-match eshell-hist-word-designator reference))
685 (end (and index (match-end 0))))
686 (unless (memq (aref reference 0) '(?: ?^ ?$ ?* ?%))
687 (error "Invalid history word designator `%s'" reference))
688 (let ((nth (match-string 1 reference))
689 (mth (match-string 2 reference))
690 (here (point))
691 textargs)
692 (insert hist)
693 (setq textargs (car (eshell-hist-parse-arguments nil here (point))))
694 (delete-region here (point))
695 (if (string= nth "*")
696 (if mth
697 (error "Invalid history word designator `%s'"
698 reference)
699 (setq nth 1 mth "-$")))
700 (if (not mth)
701 (if nth
702 (setq mth nth)
703 (setq nth 0 mth "$"))
704 (if (string= mth "-")
705 (setq mth (- (length textargs) 2))
706 (if (string= mth "*")
707 (setq mth "$")
708 (if (not (and (> (length mth) 1)
709 (eq (aref mth 0) ?-)))
710 (error "Invalid history word designator `%s'"
711 reference)
712 (setq mth (substring mth 1))))))
713 (unless (numberp nth)
714 (setq nth (eshell-hist-word-reference nth)))
715 (unless (numberp mth)
716 (setq mth (eshell-hist-word-reference mth)))
717 (cons (mapconcat 'identity (eshell-sublist textargs nth mth) "")
718 end))))
720 (defun eshell-hist-parse-modifier (hist reference)
721 "Parse a history modifier beginning for HIST in REFERENCE."
722 (let ((here (point)))
723 (insert reference)
724 (prog1
725 (save-restriction
726 (narrow-to-region here (point))
727 (goto-char (point-min))
728 (let ((modifiers (cdr (eshell-parse-modifiers))))
729 (eshell-for mod modifiers
730 (setq hist (funcall mod hist)))
731 hist))
732 (delete-region here (point)))))
734 (defun eshell-get-next-from-history ()
735 "After fetching a line from input history, this fetches the next.
736 In other words, this recalls the input line after the line you
737 recalled last. You can use this to repeat a sequence of input lines."
738 (interactive)
739 (if eshell-save-history-index
740 (progn
741 (setq eshell-history-index (1+ eshell-save-history-index))
742 (eshell-next-input 1))
743 (message "No previous history command")))
745 (defun eshell-search-arg (arg)
746 ;; First make sure there is a ring and that we are after the process
747 ;; mark
748 (if (and eshell-hist-move-to-end
749 (< (point) eshell-last-output-end))
750 (goto-char eshell-last-output-end))
751 (cond ((or (null eshell-history-ring)
752 (ring-empty-p eshell-history-ring))
753 (error "Empty input ring"))
754 ((zerop arg)
755 ;; arg of zero resets search from beginning, and uses arg of
756 ;; 1
757 (setq eshell-history-index nil)
760 arg)))
762 (defun eshell-search-start (arg)
763 "Index to start a directional search, starting at `eshell-history-index'."
764 (if eshell-history-index
765 ;; If a search is running, offset by 1 in direction of arg
766 (mod (+ eshell-history-index (if (> arg 0) 1 -1))
767 (ring-length eshell-history-ring))
768 ;; For a new search, start from beginning or end, as appropriate
769 (if (>= arg 0)
770 0 ; First elt for forward search
771 ;; Last elt for backward search
772 (1- (ring-length eshell-history-ring)))))
774 (defun eshell-previous-input-string (arg)
775 "Return the string ARG places along the input ring.
776 Moves relative to `eshell-history-index'."
777 (eshell-get-history (if eshell-history-index
778 (mod (+ arg eshell-history-index)
779 (ring-length eshell-history-ring))
780 arg)))
782 (defun eshell-previous-input (arg)
783 "Cycle backwards through input history."
784 (interactive "*p")
785 (eshell-previous-matching-input "." arg))
787 (defun eshell-next-input (arg)
788 "Cycle forwards through input history."
789 (interactive "*p")
790 (eshell-previous-input (- arg)))
792 (defun eshell-previous-matching-input-string (regexp arg)
793 "Return the string matching REGEXP ARG places along the input ring.
794 Moves relative to `eshell-history-index'."
795 (let* ((pos (eshell-previous-matching-input-string-position regexp arg)))
796 (if pos (eshell-get-history pos))))
798 (defun eshell-previous-matching-input-string-position
799 (regexp arg &optional start)
800 "Return the index matching REGEXP ARG places along the input ring.
801 Moves relative to START, or `eshell-history-index'."
802 (if (or (not (ring-p eshell-history-ring))
803 (ring-empty-p eshell-history-ring))
804 (error "No history"))
805 (let* ((len (ring-length eshell-history-ring))
806 (motion (if (> arg 0) 1 -1))
807 (n (mod (- (or start (eshell-search-start arg)) motion) len))
808 (tried-each-ring-item nil)
809 (case-fold-search (eshell-under-windows-p))
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)))
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.)
831 With prefix argument N, search for Nth previous match.
832 If 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)
840 (unless (minibuffer-window-active-p (selected-window))
841 (message "History item: %d" (- (ring-length eshell-history-ring) pos)))
842 ;; Can't use kill-region as it sets this-command
843 (delete-region eshell-last-output-end (point))
844 (insert-and-inherit (eshell-get-history pos)))))
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.)
849 With prefix argument N, search for Nth following match.
850 If 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)))
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.)
857 With prefix argument N, search for Nth previous match.
858 If 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))
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.)
874 With prefix argument N, search for Nth following match.
875 If N is negative, search backwards for the -Nth previous match."
876 (interactive "p")
877 (eshell-previous-matching-input-from-input (- arg)))
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))
888 (if isearch-forward
889 (progn
890 (end-of-line)
891 (forward-char))
892 (beginning-of-line)
893 (backward-char))))))
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))
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))))))
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))))))
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))
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)))
956 (defun eshell-isearch-forward ()
957 "Do incremental regexp search backward through past commands."
958 (interactive)
959 (eshell-isearch-backward t))
961 (defun eshell-isearch-repeat-forward ()
962 "Do incremental regexp search backward through past commands."
963 (interactive)
964 (eshell-isearch-repeat-backward t))
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))
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))
978 (defun eshell-isearch-delete-char ()
979 (interactive)
980 (save-excursion
981 (isearch-delete-char)))
983 (defun eshell-isearch-return ()
984 (interactive)
985 (isearch-done)
986 (eshell-send-input))
988 ;;; arch-tag: 1a847333-f864-4b96-9acd-b549d620b6c6
989 ;;; em-hist.el ends here