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