Mercurial > emacs
annotate lisp/eshell/em-hist.el @ 62535:2c54391a5a35
*** empty log message ***
author | Nick Roberts <nickrob@snap.net.nz> |
---|---|
date | Thu, 19 May 2005 20:57:04 +0000 |
parents | a2a95f34b949 |
children | 18a818a2ee7c 95879cc1ed20 |
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 |
53871
dbe1ef05fa24
(eshell-hist-word-reference): Fix format
Andreas Schwab <schwab@suse.de>
parents:
52401
diff
changeset
|
3 ;; Copyright (C) 1999, 2000, 2004 Free Software Foundation |
29876 | 4 |
32526 | 5 ;; Author: John Wiegley <johnw@gnu.org> |
6 | |
29876 | 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)) | |
55195
f243dc772a99
Add "(require 'eshell)", to get necessary features
Eli Zaretskii <eliz@gnu.org>
parents:
53871
diff
changeset
|
27 (require 'eshell) |
29876 | 28 |
29 (defgroup eshell-hist nil | |
30 "This module provides command history management." | |
31 :tag "History list management" | |
32 :group 'eshell-module) | |
33 | |
34 ;;; Commentary: | |
35 | |
36 ;; Eshell's history facility imitates the syntax used by bash | |
37 ;; ([(bash)History Interaction]). Thus: | |
38 ;; | |
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 | |
45 ;; | |
46 ;; Also, most of `comint-mode's keybindings are accepted: | |
47 ;; | |
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 | |
52 ;; | |
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 | |
57 ;; | |
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. | |
66 | |
67 ;;; Code: | |
68 | |
69 (require 'ring) | |
70 (require 'esh-opt) | |
71 (require 'em-pred) | |
72 | |
73 ;;; User Variables: | |
74 | |
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) | |
79 | |
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) | |
88 | |
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) | |
96 | |
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) | |
101 | |
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) | |
107 | |
49266
f4d32be2d504
(eshell-save-history-on-exit): Renamed `eshell-ask-to-save-history'
John Wiegley <johnw@newartisans.com>
parents:
46852
diff
changeset
|
108 (defcustom eshell-save-history-on-exit 'ask |
29876 | 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. | |
49266
f4d32be2d504
(eshell-save-history-on-exit): Renamed `eshell-ask-to-save-history'
John Wiegley <johnw@newartisans.com>
parents:
46852
diff
changeset
|
113 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
|
114 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
|
115 If set to t, history will always be saved, silently." |
29876 | 116 :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
|
117 (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
|
118 (const :tag "Always save" t)) |
29876 | 119 :group 'eshell-hist) |
120 | |
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) | |
131 | |
132 (put 'eshell-input-filter 'risky-local-variable t) | |
133 | |
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) | |
141 | |
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) | |
146 | |
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) | |
152 | |
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) | |
158 | |
159 (defcustom eshell-hist-modifier | |
160 "^\\(:\\([hretpqx&g]\\|s/\\([^/]*\\)/\\([^/]*\\)/\\)\\)*" | |
161 "*The regexp used to identity history modifiers." | |
162 :type 'regexp | |
163 :group 'eshell-hist) | |
164 | |
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) | |
183 | |
184 ;;; Internal Variables: | |
185 | |
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) | |
190 | |
191 (defvar eshell-isearch-map nil) | |
192 | |
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)) | |
206 | |
207 ;;; Functions: | |
208 | |
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) | |
213 | |
214 (when (eshell-using-module 'eshell-cmpl) | |
215 (add-hook 'pcomplete-try-first-hook | |
216 'eshell-complete-history-reference nil t)) | |
217 | |
43322
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
218 (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
|
219 (not eshell-non-interactive-p)) |
29876 | 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))) | |
260 | |
261 (make-local-variable 'eshell-history-size) | |
262 (or eshell-history-size | |
263 (setq eshell-history-size (getenv "HISTSIZE"))) | |
264 | |
265 (make-local-variable 'eshell-history-file-name) | |
266 (or eshell-history-file-name | |
267 (setq eshell-history-file-name (getenv "HISTFILE"))) | |
268 | |
269 (make-local-variable 'eshell-history-index) | |
270 (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
|
271 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
272 (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
|
273 (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
|
274 (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
|
275 (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
|
276 (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
|
277 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
278 (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
|
279 |
29876 | 280 (unless eshell-history-ring |
281 (setq eshell-history-ring (make-ring eshell-history-size))) | |
282 | |
283 (add-hook 'eshell-exit-hook 'eshell-write-history nil t) | |
284 | |
285 (add-hook 'kill-emacs-hook 'eshell-save-some-history) | |
286 | |
287 (make-local-variable 'eshell-input-filter-functions) | |
288 (add-hook 'eshell-input-filter-functions 'eshell-add-to-history nil t) | |
289 | |
290 (define-key eshell-command-map [(control ?l)] 'eshell-list-history) | |
291 (define-key eshell-command-map [(control ?x)] 'eshell-get-next-from-history)) | |
292 | |
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 | |
49266
f4d32be2d504
(eshell-save-history-on-exit): Renamed `eshell-ask-to-save-history'
John Wiegley <johnw@newartisans.com>
parents:
46852
diff
changeset
|
300 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
|
301 (or (eq eshell-save-history-on-exit t) |
29876 | 302 (y-or-n-p |
303 (format "Save input history for Eshell buffer `%s'? " | |
304 (buffer-name buf))))) | |
305 (eshell-write-history)))))) | |
306 | |
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)) | |
342 (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)) | |
354 | |
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))) | |
361 | |
362 (defun eshell-get-history (index &optional ring) | |
363 "Get an input line from the history ring." | |
31241 | 364 (ring-ref (or ring eshell-history-ring) index)) |
29876 | 365 |
43322
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
366 (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
|
367 "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
|
368 Input is entered into the input history ring, if the value of |
29876 | 369 variable `eshell-input-filter' returns non-nil when called on the |
370 input." | |
43322
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
371 (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
|
372 (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
|
373 (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
|
374 (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
|
375 (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
|
376 (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
|
377 (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
|
378 (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
|
379 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
380 (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
|
381 "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
|
382 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
|
383 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
|
384 command. |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
385 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
386 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
|
387 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
|
388 (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
|
389 (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
|
390 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
391 (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
|
392 "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
|
393 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
|
394 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
|
395 command." |
29876 | 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)))) | |
43322
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
399 (eshell-add-input-to-history input)))) |
29876 | 400 |
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. | |
407 | |
408 If the optional argument SILENT is non-nil, we say nothing about a | |
409 failure to read the history file. | |
410 | |
411 This function is useful for major mode commands and mode hooks. | |
412 | |
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))) | |
424 (t | |
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))) | |
31241 | 441 (ring-insert-at-beginning |
442 ring (subst-char-in-string ?\177 ?\n history)))) | |
29876 | 443 (setq count (1+ count)))) |
444 (setq eshell-history-ring ring | |
445 eshell-history-index nil)))))) | |
446 | |
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. | |
453 | |
454 Useful within process sentinels. | |
455 | |
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)) | |
466 (t | |
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)) | |
31241 | 474 (let ((start (point))) |
475 (insert (ring-ref ring index) ?\n) | |
476 (subst-char-in-region start (1- (point)) ?\n ?\177))) | |
29876 | 477 (eshell-with-private-file-modes |
478 (write-region (point-min) (point-max) file append | |
479 'no-message)))))))) | |
480 | |
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)))))))) | |
518 | |
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) | |
53871
dbe1ef05fa24
(eshell-hist-word-reference): Fix format
Andreas Schwab <schwab@suse.de>
parents:
52401
diff
changeset
|
527 (error "`%%' history word designator not yet implemented")))) |
29876 | 528 |
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)))) | |
566 | |
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)))))))) | |
584 | |
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))))))) | |
613 | |
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))))))) | |
654 | |
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)))) | |
678 (t | |
679 (error "Failed to parse event designator `%s'" event))))) | |
680 (and pos (cons pos end))))) | |
681 | |
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)))) | |
719 | |
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))))) | |
733 | |
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"))) | |
744 | |
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) | |
758 1) | |
759 (t | |
760 arg))) | |
761 | |
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))))) | |
773 | |
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))) | |
781 | |
782 (defun eshell-previous-input (arg) | |
783 "Cycle backwards through input history." | |
784 (interactive "*p") | |
785 (eshell-previous-matching-input "." arg)) | |
786 | |
787 (defun eshell-next-input (arg) | |
788 "Cycle forwards through input history." | |
789 (interactive "*p") | |
790 (eshell-previous-input (- arg))) | |
791 | |
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)))) | |
797 | |
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) | |
46852
6eb625bead4f
Removed eshell-under-cygwin-p, and all uses of it.
John Wiegley <johnw@newartisans.com>
parents:
46820
diff
changeset
|
809 (case-fold-search (eshell-under-windows-p)) |
29876 | 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.) | |
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) | |
37323
11b33211a908
(eshell-previous-matching-input): Don't display "History item" if the
John Wiegley <johnw@newartisans.com>
parents:
32526
diff
changeset
|
840 (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
|
841 (message "History item: %d" (- (ring-length eshell-history-ring) pos))) |
29876 | 842 ;; 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
|
843 (delete-region eshell-last-output-end (point)) |
29876 | 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.) | |
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))) | |
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.) | |
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)) | |
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.) | |
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))) | |
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)) | |
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 | |
52401 | 988 ;;; arch-tag: 1a847333-f864-4b96-9acd-b549d620b6c6 |
29876 | 989 ;;; em-hist.el ends here |