Mercurial > emacs
annotate lisp/eshell/em-hist.el @ 64518:7b82147924c7
(org-table-formula-substitute-names, org-table-get-vertical-vector): Doc fixes.
(org-table-recalculate): Remove unused argument to `message'.
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Tue, 19 Jul 2005 23:37:03 +0000 |
parents | 54d5227ff298 |
children | 34bd8e434dd7 187d6a1f84f7 |
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 | |
64085 | 21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
22 ;; Boston, MA 02110-1301, USA. | |
29876 | 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 | |
64384
54d5227ff298
(eshell-rebind-keys-alist): Add defvar.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
207 (defvar eshell-rebind-keys-alist) |
54d5227ff298
(eshell-rebind-keys-alist): Add defvar.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
208 |
29876 | 209 ;;; Functions: |
210 | |
211 (defun eshell-hist-initialize () | |
212 "Initialize the history management code for one Eshell buffer." | |
213 (add-hook 'eshell-expand-input-functions | |
214 'eshell-expand-history-references nil t) | |
215 | |
216 (when (eshell-using-module 'eshell-cmpl) | |
217 (add-hook 'pcomplete-try-first-hook | |
218 'eshell-complete-history-reference nil t)) | |
219 | |
43322
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
220 (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
|
221 (not eshell-non-interactive-p)) |
64384
54d5227ff298
(eshell-rebind-keys-alist): Add defvar.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
222 (let ((rebind-alist eshell-rebind-keys-alist)) |
29876 | 223 (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
|
224 (setq eshell-rebind-keys-alist |
54d5227ff298
(eshell-rebind-keys-alist): Add defvar.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
225 (append rebind-alist eshell-hist-rebind-keys-alist)) |
29876 | 226 (set (make-local-variable 'search-invisible) t) |
227 (set (make-local-variable 'search-exit-option) t) | |
228 (add-hook 'isearch-mode-hook | |
229 (function | |
230 (lambda () | |
231 (if (>= (point) eshell-last-output-end) | |
232 (setq overriding-terminal-local-map | |
233 eshell-isearch-map)))) nil t) | |
234 (add-hook 'isearch-mode-end-hook | |
235 (function | |
236 (lambda () | |
237 (setq overriding-terminal-local-map nil))) nil t)) | |
238 (define-key eshell-mode-map [up] 'eshell-previous-matching-input-from-input) | |
239 (define-key eshell-mode-map [down] 'eshell-next-matching-input-from-input) | |
240 (define-key eshell-mode-map [(control up)] 'eshell-previous-input) | |
241 (define-key eshell-mode-map [(control down)] 'eshell-next-input) | |
242 (define-key eshell-mode-map [(meta ?r)] 'eshell-previous-matching-input) | |
243 (define-key eshell-mode-map [(meta ?s)] 'eshell-next-matching-input) | |
244 (define-key eshell-command-map [(meta ?r)] | |
245 'eshell-previous-matching-input-from-input) | |
246 (define-key eshell-command-map [(meta ?s)] | |
247 'eshell-next-matching-input-from-input) | |
248 (if eshell-hist-match-partial | |
249 (progn | |
250 (define-key eshell-mode-map [(meta ?p)] | |
251 'eshell-previous-matching-input-from-input) | |
252 (define-key eshell-mode-map [(meta ?n)] | |
253 'eshell-next-matching-input-from-input) | |
254 (define-key eshell-command-map [(meta ?p)] 'eshell-previous-input) | |
255 (define-key eshell-command-map [(meta ?n)] 'eshell-next-input)) | |
256 (define-key eshell-mode-map [(meta ?p)] 'eshell-previous-input) | |
257 (define-key eshell-mode-map [(meta ?n)] 'eshell-next-input) | |
258 (define-key eshell-command-map [(meta ?p)] | |
259 'eshell-previous-matching-input-from-input) | |
260 (define-key eshell-command-map [(meta ?n)] | |
261 'eshell-next-matching-input-from-input))) | |
262 | |
263 (make-local-variable 'eshell-history-size) | |
264 (or eshell-history-size | |
265 (setq eshell-history-size (getenv "HISTSIZE"))) | |
266 | |
267 (make-local-variable 'eshell-history-file-name) | |
268 (or eshell-history-file-name | |
269 (setq eshell-history-file-name (getenv "HISTFILE"))) | |
270 | |
271 (make-local-variable 'eshell-history-index) | |
272 (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
|
273 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
274 (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
|
275 (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
|
276 (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
|
277 (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
|
278 (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
|
279 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
280 (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
|
281 |
29876 | 282 (unless eshell-history-ring |
283 (setq eshell-history-ring (make-ring eshell-history-size))) | |
284 | |
285 (add-hook 'eshell-exit-hook 'eshell-write-history nil t) | |
286 | |
287 (add-hook 'kill-emacs-hook 'eshell-save-some-history) | |
288 | |
289 (make-local-variable 'eshell-input-filter-functions) | |
290 (add-hook 'eshell-input-filter-functions 'eshell-add-to-history nil t) | |
291 | |
292 (define-key eshell-command-map [(control ?l)] 'eshell-list-history) | |
293 (define-key eshell-command-map [(control ?x)] 'eshell-get-next-from-history)) | |
294 | |
295 (defun eshell-save-some-history () | |
296 "Save the history for any open Eshell buffers." | |
297 (eshell-for buf (buffer-list) | |
298 (if (buffer-live-p buf) | |
299 (with-current-buffer buf | |
300 (if (and eshell-mode | |
301 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
|
302 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
|
303 (or (eq eshell-save-history-on-exit t) |
29876 | 304 (y-or-n-p |
305 (format "Save input history for Eshell buffer `%s'? " | |
306 (buffer-name buf))))) | |
307 (eshell-write-history)))))) | |
308 | |
309 (defun eshell/history (&rest args) | |
310 "List in help buffer the buffer's input history." | |
311 (eshell-init-print-buffer) | |
312 (eshell-eval-using-options | |
313 "history" args | |
314 '((?r "read" nil read-history | |
315 "read from history file to current history list") | |
316 (?w "write" nil write-history | |
317 "write current history list to history file") | |
318 (?a "append" nil append-history | |
319 "append current history list to history file") | |
320 (?h "help" nil nil "display this usage message") | |
321 :usage "[n] [-rwa [filename]]" | |
322 :post-usage | |
323 "When Eshell is started, history is read from `eshell-history-file-name'. | |
324 This is also the location where history info will be saved by this command, | |
325 unless a different file is specified on the command line.") | |
326 (and (or (not (ring-p eshell-history-ring)) | |
327 (ring-empty-p eshell-history-ring)) | |
328 (error "No history")) | |
329 (let (length command file) | |
330 (when (and args (string-match "^[0-9]+$" (car args))) | |
331 (setq length (min (eshell-convert (car args)) | |
332 (ring-length eshell-history-ring)) | |
333 args (cdr args))) | |
334 (and length | |
335 (or read-history write-history append-history) | |
336 (error "history: extra arguments")) | |
337 (when (and args (stringp (car args))) | |
338 (setq file (car args) | |
339 args (cdr args))) | |
340 (cond | |
341 (read-history (eshell-read-history file)) | |
342 (write-history (eshell-write-history file)) | |
343 (append-history (eshell-write-history file t)) | |
344 (t | |
345 (let* ((history nil) | |
346 (index (1- (or length (ring-length eshell-history-ring)))) | |
347 (ref (- (ring-length eshell-history-ring) index))) | |
348 ;; We have to build up a list ourselves from the ring vector. | |
349 (while (>= index 0) | |
350 (eshell-buffered-print | |
351 (format "%5d %s\n" ref (eshell-get-history index))) | |
352 (setq index (1- index) | |
353 ref (1+ ref))))))) | |
354 (eshell-flush) | |
355 nil)) | |
356 | |
357 (defun eshell-put-history (input &optional ring at-beginning) | |
358 "Put a new input line into the history ring." | |
359 (unless ring (setq ring eshell-history-ring)) | |
360 (if at-beginning | |
361 (ring-insert-at-beginning ring input) | |
362 (ring-insert ring input))) | |
363 | |
364 (defun eshell-get-history (index &optional ring) | |
365 "Get an input line from the history ring." | |
31241 | 366 (ring-ref (or ring eshell-history-ring) index)) |
29876 | 367 |
43322
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
368 (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
|
369 "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
|
370 Input is entered into the input history ring, if the value of |
29876 | 371 variable `eshell-input-filter' returns non-nil when called on the |
372 input." | |
43322
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
373 (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
|
374 (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
|
375 (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
|
376 (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
|
377 (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
|
378 (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
|
379 (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
|
380 (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
|
381 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
382 (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
|
383 "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
|
384 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
|
385 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
|
386 command. |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
387 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
388 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
|
389 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
|
390 (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
|
391 (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
|
392 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
393 (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
|
394 "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
|
395 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
|
396 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
|
397 command." |
29876 | 398 (when (> (1- eshell-last-input-end) eshell-last-input-start) |
399 (let ((input (buffer-substring eshell-last-input-start | |
400 (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
|
401 (eshell-add-input-to-history input)))) |
29876 | 402 |
403 (defun eshell-read-history (&optional filename silent) | |
404 "Sets the buffer's `eshell-history-ring' from a history file. | |
405 The name of the file is given by the variable | |
406 `eshell-history-file-name'. The history ring is of size | |
407 `eshell-history-size', regardless of file size. If | |
408 `eshell-history-file-name' is nil this function does nothing. | |
409 | |
410 If the optional argument SILENT is non-nil, we say nothing about a | |
411 failure to read the history file. | |
412 | |
413 This function is useful for major mode commands and mode hooks. | |
414 | |
415 The structure of the history file should be one input command per | |
416 line, with the most recent command last. See also | |
417 `eshell-hist-ignoredups' and `eshell-write-history'." | |
418 (let ((file (or filename eshell-history-file-name))) | |
419 (cond | |
420 ((or (null file) | |
421 (equal file "")) | |
422 nil) | |
423 ((not (file-readable-p file)) | |
424 (or silent | |
425 (message "Cannot read history file %s" file))) | |
426 (t | |
427 (let* ((count 0) | |
428 (size eshell-history-size) | |
429 (ring (make-ring size)) | |
430 (ignore-dups eshell-hist-ignoredups)) | |
431 (with-temp-buffer | |
432 (insert-file-contents file) | |
433 ;; Save restriction in case file is already visited... | |
434 ;; Watch for those date stamps in history files! | |
435 (goto-char (point-max)) | |
436 (while (and (< count size) | |
437 (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$" | |
438 nil t)) | |
439 (let ((history (match-string 1))) | |
440 (if (or (null ignore-dups) | |
441 (ring-empty-p ring) | |
442 (not (string-equal (ring-ref ring 0) history))) | |
31241 | 443 (ring-insert-at-beginning |
444 ring (subst-char-in-string ?\177 ?\n history)))) | |
29876 | 445 (setq count (1+ count)))) |
446 (setq eshell-history-ring ring | |
447 eshell-history-index nil)))))) | |
448 | |
449 (defun eshell-write-history (&optional filename append) | |
450 "Writes the buffer's `eshell-history-ring' to a history file. | |
451 The name of the file is given by the variable | |
452 `eshell-history-file-name'. The original contents of the file are | |
453 lost if `eshell-history-ring' is not empty. If | |
454 `eshell-history-file-name' is nil this function does nothing. | |
455 | |
456 Useful within process sentinels. | |
457 | |
458 See also `eshell-read-history'." | |
459 (let ((file (or filename eshell-history-file-name))) | |
460 (cond | |
461 ((or (null file) | |
462 (equal file "") | |
463 (null eshell-history-ring) | |
464 (ring-empty-p eshell-history-ring)) | |
465 nil) | |
466 ((not (file-writable-p file)) | |
467 (message "Cannot write history file %s" file)) | |
468 (t | |
469 (let* ((ring eshell-history-ring) | |
470 (index (ring-length ring))) | |
471 ;; Write it all out into a buffer first. Much faster, but | |
472 ;; messier, than writing it one line at a time. | |
473 (with-temp-buffer | |
474 (while (> index 0) | |
475 (setq index (1- index)) | |
31241 | 476 (let ((start (point))) |
477 (insert (ring-ref ring index) ?\n) | |
478 (subst-char-in-region start (1- (point)) ?\n ?\177))) | |
29876 | 479 (eshell-with-private-file-modes |
480 (write-region (point-min) (point-max) file append | |
481 'no-message)))))))) | |
482 | |
483 (defun eshell-list-history () | |
484 "List in help buffer the buffer's input history." | |
485 (interactive) | |
486 (let (prefix prelen) | |
487 (save-excursion | |
488 (if (re-search-backward "!\\(.+\\)" (line-beginning-position) t) | |
489 (setq prefix (match-string 1) | |
490 prelen (length prefix)))) | |
491 (if (or (not (ring-p eshell-history-ring)) | |
492 (ring-empty-p eshell-history-ring)) | |
493 (message "No history") | |
494 (let ((history nil) | |
495 (history-buffer " *Input History*") | |
496 (index (1- (ring-length eshell-history-ring))) | |
497 (conf (current-window-configuration))) | |
498 ;; We have to build up a list ourselves from the ring vector. | |
499 (while (>= index 0) | |
500 (let ((hist (eshell-get-history index))) | |
501 (if (or (not prefix) | |
502 (and (>= (length hist) prelen) | |
503 (string= (substring hist 0 prelen) prefix))) | |
504 (setq history (cons hist history)))) | |
505 (setq index (1- index))) | |
506 ;; Change "completion" to "history reference" | |
507 ;; to make the display accurate. | |
508 (with-output-to-temp-buffer history-buffer | |
509 (display-completion-list history) | |
510 (set-buffer history-buffer) | |
511 (forward-line 3) | |
512 (while (search-backward "completion" nil 'move) | |
513 (replace-match "history reference"))) | |
514 (eshell-redisplay) | |
515 (message "Hit space to flush") | |
516 (let ((ch (read-event))) | |
517 (if (eq ch ?\ ) | |
518 (set-window-configuration conf) | |
519 (setq unread-command-events (list ch)))))))) | |
520 | |
521 (defun eshell-hist-word-reference (ref) | |
522 "Return the word designator index referred to by REF." | |
523 (cond | |
524 ((string-match "^[0-9]+$" ref) | |
525 (string-to-number ref)) | |
526 ((string= "^" ref) 1) | |
527 ((string= "$" ref) nil) | |
528 ((string= "%" ref) | |
53871
dbe1ef05fa24
(eshell-hist-word-reference): Fix format
Andreas Schwab <schwab@suse.de>
parents:
52401
diff
changeset
|
529 (error "`%%' history word designator not yet implemented")))) |
29876 | 530 |
531 (defun eshell-hist-parse-arguments (&optional silent b e) | |
532 "Parse current command arguments in a history-code-friendly way." | |
533 (let ((end (or e (point))) | |
534 (begin (or b (save-excursion (eshell-bol) (point)))) | |
535 (posb (list t)) | |
536 (pose (list t)) | |
537 (textargs (list t)) | |
538 hist args) | |
539 (unless (catch 'eshell-incomplete | |
540 (ignore | |
541 (setq args (eshell-parse-arguments begin end)))) | |
542 (save-excursion | |
543 (goto-char begin) | |
544 (while (< (point) end) | |
545 (if (get-text-property (point) 'arg-begin) | |
546 (nconc posb (list (point)))) | |
547 (if (get-text-property (point) 'arg-end) | |
548 (nconc pose | |
549 (list (if (= (1+ (point)) end) | |
550 (1+ (point)) | |
551 (point))))) | |
552 (forward-char)) | |
553 (setq posb (cdr posb) | |
554 pose (cdr pose)) | |
555 (assert (= (length posb) (length args))) | |
556 (assert (<= (length posb) (length pose)))) | |
557 (setq hist (buffer-substring-no-properties begin end)) | |
558 (let ((b posb) (e pose)) | |
559 (while b | |
560 (nconc textargs | |
561 (list (substring hist (- (car b) begin) | |
562 (- (car e) begin)))) | |
563 (setq b (cdr b) | |
564 e (cdr e)))) | |
565 (setq textargs (cdr textargs)) | |
566 (assert (= (length textargs) (length args))) | |
567 (list textargs posb pose)))) | |
568 | |
569 (defun eshell-expand-history-references (beg end) | |
570 "Parse and expand any history references in current input." | |
571 (let ((result (eshell-hist-parse-arguments t beg end))) | |
572 (when result | |
573 (let ((textargs (nreverse (nth 0 result))) | |
574 (posb (nreverse (nth 1 result))) | |
575 (pose (nreverse (nth 2 result)))) | |
576 (save-excursion | |
577 (while textargs | |
578 (let ((str (eshell-history-reference (car textargs)))) | |
579 (unless (eq str (car textargs)) | |
580 (goto-char (car posb)) | |
581 (insert-and-inherit str) | |
582 (delete-char (- (car pose) (car posb))))) | |
583 (setq textargs (cdr textargs) | |
584 posb (cdr posb) | |
585 pose (cdr pose)))))))) | |
586 | |
587 (defun eshell-complete-history-reference () | |
588 "Complete a history reference, by completing the event designator." | |
589 (let ((arg (pcomplete-actual-arg))) | |
590 (when (string-match "\\`![^:^$*%]*\\'" arg) | |
591 (setq pcomplete-stub (substring arg 1) | |
592 pcomplete-last-completion-raw t) | |
593 (throw 'pcomplete-completions | |
594 (let ((history nil) | |
595 (index (1- (ring-length eshell-history-ring))) | |
596 (stublen (length pcomplete-stub))) | |
597 ;; We have to build up a list ourselves from the ring | |
598 ;; vector. | |
599 (while (>= index 0) | |
600 (let ((hist (eshell-get-history index))) | |
601 (if (and (>= (length hist) stublen) | |
602 (string= (substring hist 0 stublen) | |
603 pcomplete-stub) | |
604 (string-match "^\\([^:^$*% \t\n]+\\)" hist)) | |
605 (setq history (cons (match-string 1 hist) | |
606 history)))) | |
607 (setq index (1- index))) | |
608 (let ((fhist (list t))) | |
609 ;; uniqify the list, but preserve the order | |
610 (while history | |
611 (unless (member (car history) fhist) | |
612 (nconc fhist (list (car history)))) | |
613 (setq history (cdr history))) | |
614 (cdr fhist))))))) | |
615 | |
616 (defun eshell-history-reference (reference) | |
617 "Expand directory stack REFERENCE. | |
618 The syntax used here was taken from the Bash info manual. | |
619 Returns the resultant reference, or the same string REFERENCE if none | |
620 matched." | |
621 ;; `^string1^string2^' | |
622 ;; Quick Substitution. Repeat the last command, replacing | |
623 ;; STRING1 with STRING2. Equivalent to `!!:s/string1/string2/' | |
624 (if (and (eshell-using-module 'eshell-pred) | |
625 (string-match "\\^\\([^^]+\\)\\^\\([^^]+\\)\\^?\\s-*$" | |
626 reference)) | |
627 (setq reference (format "!!:s/%s/%s/" | |
628 (match-string 1 reference) | |
629 (match-string 2 reference)))) | |
630 ;; `!' | |
631 ;; Start a history substitution, except when followed by a | |
632 ;; space, tab, the end of the line, = or (. | |
633 (if (not (string-match "^![^ \t\n=\(]" reference)) | |
634 reference | |
635 (setq eshell-history-index nil) | |
636 (let ((event (eshell-hist-parse-event-designator reference))) | |
637 (unless event | |
638 (error "Could not find history event `%s'" reference)) | |
639 (setq eshell-history-index (car event) | |
640 reference (substring reference (cdr event)) | |
641 event (eshell-get-history eshell-history-index)) | |
642 (if (not (string-match "^[:^$*%]" reference)) | |
643 event | |
644 (let ((word (eshell-hist-parse-word-designator | |
645 event reference))) | |
646 (unless word | |
647 (error "Unable to honor word designator `%s'" reference)) | |
648 (unless (string-match "^[:^$*%][[$^*%0-9-]" reference) | |
649 (setcdr word 0)) | |
650 (setq event (car word) | |
651 reference (substring reference (cdr word))) | |
652 (if (not (and (eshell-using-module 'eshell-pred) | |
653 (string-match "^:" reference))) | |
654 event | |
655 (eshell-hist-parse-modifier event reference))))))) | |
656 | |
657 (defun eshell-hist-parse-event-designator (reference) | |
658 "Parse a history event designator beginning in REFERENCE." | |
659 (let* ((index (string-match eshell-hist-event-designator reference)) | |
660 (end (and index (match-end 0)))) | |
661 (unless index | |
662 (error "Invalid history event designator `%s'" reference)) | |
663 (let* ((event (match-string 1 reference)) | |
664 (pos | |
665 (cond | |
666 ((string= event "!") (ring-length eshell-history-ring)) | |
667 ((string= event "#") (error "!# not yet implemented")) | |
668 ((string-match "^-?[0-9]+$" event) | |
669 (let ((num (string-to-number event))) | |
670 (if (>= num 0) | |
671 (- (ring-length eshell-history-ring) num) | |
672 (1- (abs num))))) | |
673 ((string-match "^\\(\\??\\)\\([^?]+\\)\\??$" event) | |
674 (let ((pref (if (> (length (match-string 1 event)) 0) | |
675 "" "^")) | |
676 (str (match-string 2 event))) | |
677 (save-match-data | |
678 (eshell-previous-matching-input-string-position | |
679 (concat pref (regexp-quote str)) 1)))) | |
680 (t | |
681 (error "Failed to parse event designator `%s'" event))))) | |
682 (and pos (cons pos end))))) | |
683 | |
684 (defun eshell-hist-parse-word-designator (hist reference) | |
685 "Parse a history word designator beginning for HIST in REFERENCE." | |
686 (let* ((index (string-match eshell-hist-word-designator reference)) | |
687 (end (and index (match-end 0)))) | |
688 (unless (memq (aref reference 0) '(?: ?^ ?$ ?* ?%)) | |
689 (error "Invalid history word designator `%s'" reference)) | |
690 (let ((nth (match-string 1 reference)) | |
691 (mth (match-string 2 reference)) | |
692 (here (point)) | |
693 textargs) | |
694 (insert hist) | |
695 (setq textargs (car (eshell-hist-parse-arguments nil here (point)))) | |
696 (delete-region here (point)) | |
697 (if (string= nth "*") | |
698 (if mth | |
699 (error "Invalid history word designator `%s'" | |
700 reference) | |
701 (setq nth 1 mth "-$"))) | |
702 (if (not mth) | |
703 (if nth | |
704 (setq mth nth) | |
705 (setq nth 0 mth "$")) | |
706 (if (string= mth "-") | |
707 (setq mth (- (length textargs) 2)) | |
708 (if (string= mth "*") | |
709 (setq mth "$") | |
710 (if (not (and (> (length mth) 1) | |
711 (eq (aref mth 0) ?-))) | |
712 (error "Invalid history word designator `%s'" | |
713 reference) | |
714 (setq mth (substring mth 1)))))) | |
715 (unless (numberp nth) | |
716 (setq nth (eshell-hist-word-reference nth))) | |
717 (unless (numberp mth) | |
718 (setq mth (eshell-hist-word-reference mth))) | |
719 (cons (mapconcat 'identity (eshell-sublist textargs nth mth) "") | |
720 end)))) | |
721 | |
722 (defun eshell-hist-parse-modifier (hist reference) | |
723 "Parse a history modifier beginning for HIST in REFERENCE." | |
724 (let ((here (point))) | |
725 (insert reference) | |
726 (prog1 | |
727 (save-restriction | |
728 (narrow-to-region here (point)) | |
729 (goto-char (point-min)) | |
730 (let ((modifiers (cdr (eshell-parse-modifiers)))) | |
731 (eshell-for mod modifiers | |
732 (setq hist (funcall mod hist))) | |
733 hist)) | |
734 (delete-region here (point))))) | |
735 | |
736 (defun eshell-get-next-from-history () | |
737 "After fetching a line from input history, this fetches the next. | |
738 In other words, this recalls the input line after the line you | |
739 recalled last. You can use this to repeat a sequence of input lines." | |
740 (interactive) | |
741 (if eshell-save-history-index | |
742 (progn | |
743 (setq eshell-history-index (1+ eshell-save-history-index)) | |
744 (eshell-next-input 1)) | |
745 (message "No previous history command"))) | |
746 | |
747 (defun eshell-search-arg (arg) | |
748 ;; First make sure there is a ring and that we are after the process | |
749 ;; mark | |
750 (if (and eshell-hist-move-to-end | |
751 (< (point) eshell-last-output-end)) | |
752 (goto-char eshell-last-output-end)) | |
753 (cond ((or (null eshell-history-ring) | |
754 (ring-empty-p eshell-history-ring)) | |
755 (error "Empty input ring")) | |
756 ((zerop arg) | |
757 ;; arg of zero resets search from beginning, and uses arg of | |
758 ;; 1 | |
759 (setq eshell-history-index nil) | |
760 1) | |
761 (t | |
762 arg))) | |
763 | |
764 (defun eshell-search-start (arg) | |
765 "Index to start a directional search, starting at `eshell-history-index'." | |
766 (if eshell-history-index | |
767 ;; If a search is running, offset by 1 in direction of arg | |
768 (mod (+ eshell-history-index (if (> arg 0) 1 -1)) | |
769 (ring-length eshell-history-ring)) | |
770 ;; For a new search, start from beginning or end, as appropriate | |
771 (if (>= arg 0) | |
772 0 ; First elt for forward search | |
773 ;; Last elt for backward search | |
774 (1- (ring-length eshell-history-ring))))) | |
775 | |
776 (defun eshell-previous-input-string (arg) | |
777 "Return the string ARG places along the input ring. | |
778 Moves relative to `eshell-history-index'." | |
779 (eshell-get-history (if eshell-history-index | |
780 (mod (+ arg eshell-history-index) | |
781 (ring-length eshell-history-ring)) | |
782 arg))) | |
783 | |
784 (defun eshell-previous-input (arg) | |
785 "Cycle backwards through input history." | |
786 (interactive "*p") | |
787 (eshell-previous-matching-input "." arg)) | |
788 | |
789 (defun eshell-next-input (arg) | |
790 "Cycle forwards through input history." | |
791 (interactive "*p") | |
792 (eshell-previous-input (- arg))) | |
793 | |
794 (defun eshell-previous-matching-input-string (regexp arg) | |
795 "Return the string matching REGEXP ARG places along the input ring. | |
796 Moves relative to `eshell-history-index'." | |
797 (let* ((pos (eshell-previous-matching-input-string-position regexp arg))) | |
798 (if pos (eshell-get-history pos)))) | |
799 | |
800 (defun eshell-previous-matching-input-string-position | |
801 (regexp arg &optional start) | |
802 "Return the index matching REGEXP ARG places along the input ring. | |
803 Moves relative to START, or `eshell-history-index'." | |
804 (if (or (not (ring-p eshell-history-ring)) | |
805 (ring-empty-p eshell-history-ring)) | |
806 (error "No history")) | |
807 (let* ((len (ring-length eshell-history-ring)) | |
808 (motion (if (> arg 0) 1 -1)) | |
809 (n (mod (- (or start (eshell-search-start arg)) motion) len)) | |
810 (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
|
811 (case-fold-search (eshell-under-windows-p)) |
29876 | 812 (prev nil)) |
813 ;; Do the whole search as many times as the argument says. | |
814 (while (and (/= arg 0) (not tried-each-ring-item)) | |
815 ;; Step once. | |
816 (setq prev n | |
817 n (mod (+ n motion) len)) | |
818 ;; If we haven't reached a match, step some more. | |
819 (while (and (< n len) (not tried-each-ring-item) | |
820 (not (string-match regexp (eshell-get-history n)))) | |
821 (setq n (mod (+ n motion) len) | |
822 ;; If we have gone all the way around in this search. | |
823 tried-each-ring-item (= n prev))) | |
824 (setq arg (if (> arg 0) (1- arg) (1+ arg)))) | |
825 ;; Now that we know which ring element to use, if we found it, | |
826 ;; return that. | |
827 (if (string-match regexp (eshell-get-history n)) | |
828 n))) | |
829 | |
830 (defun eshell-previous-matching-input (regexp arg) | |
831 "Search backwards through input history for match for REGEXP. | |
832 \(Previous history elements are earlier commands.) | |
833 With prefix argument N, search for Nth previous match. | |
834 If N is negative, find the next or Nth next match." | |
835 (interactive (eshell-regexp-arg "Previous input matching (regexp): ")) | |
836 (setq arg (eshell-search-arg arg)) | |
837 (let ((pos (eshell-previous-matching-input-string-position regexp arg))) | |
838 ;; Has a match been found? | |
839 (if (null pos) | |
840 (error "Not found") | |
841 (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
|
842 (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
|
843 (message "History item: %d" (- (ring-length eshell-history-ring) pos))) |
29876 | 844 ;; 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
|
845 (delete-region eshell-last-output-end (point)) |
29876 | 846 (insert-and-inherit (eshell-get-history pos))))) |
847 | |
848 (defun eshell-next-matching-input (regexp arg) | |
849 "Search forwards through input history for match for REGEXP. | |
850 \(Later history elements are more recent commands.) | |
851 With prefix argument N, search for Nth following match. | |
852 If N is negative, find the previous or Nth previous match." | |
853 (interactive (eshell-regexp-arg "Next input matching (regexp): ")) | |
854 (eshell-previous-matching-input regexp (- arg))) | |
855 | |
856 (defun eshell-previous-matching-input-from-input (arg) | |
857 "Search backwards through input history for match for current input. | |
858 \(Previous history elements are earlier commands.) | |
859 With prefix argument N, search for Nth previous match. | |
860 If N is negative, search forwards for the -Nth following match." | |
861 (interactive "p") | |
862 (if (not (memq last-command '(eshell-previous-matching-input-from-input | |
863 eshell-next-matching-input-from-input))) | |
864 ;; Starting a new search | |
865 (setq eshell-matching-input-from-input-string | |
866 (buffer-substring (save-excursion (eshell-bol) (point)) | |
867 (point)) | |
868 eshell-history-index nil)) | |
869 (eshell-previous-matching-input | |
870 (concat "^" (regexp-quote eshell-matching-input-from-input-string)) | |
871 arg)) | |
872 | |
873 (defun eshell-next-matching-input-from-input (arg) | |
874 "Search forwards through input history for match for current input. | |
875 \(Following history elements are more recent commands.) | |
876 With prefix argument N, search for Nth following match. | |
877 If N is negative, search backwards for the -Nth previous match." | |
878 (interactive "p") | |
879 (eshell-previous-matching-input-from-input (- arg))) | |
880 | |
881 (defun eshell-test-imatch () | |
882 "If isearch match good, put point at the beginning and return non-nil." | |
883 (if (get-text-property (point) 'history) | |
884 (progn (beginning-of-line) t) | |
885 (let ((before (point))) | |
886 (eshell-bol) | |
887 (if (and (not (bolp)) | |
888 (<= (point) before)) | |
889 t | |
890 (if isearch-forward | |
891 (progn | |
892 (end-of-line) | |
893 (forward-char)) | |
894 (beginning-of-line) | |
895 (backward-char)))))) | |
896 | |
897 (defun eshell-return-to-prompt () | |
898 "Once a search string matches, insert it at the end and go there." | |
899 (setq isearch-other-end nil) | |
900 (let ((found (eshell-test-imatch)) before) | |
901 (while (and (not found) | |
902 (setq before | |
903 (funcall (if isearch-forward | |
904 're-search-forward | |
905 're-search-backward) | |
906 isearch-string nil t))) | |
907 (setq found (eshell-test-imatch))) | |
908 (if (not found) | |
909 (progn | |
910 (goto-char eshell-last-output-end) | |
911 (delete-region (point) (point-max))) | |
912 (setq before (point)) | |
913 (let ((text (buffer-substring-no-properties | |
914 (point) (line-end-position))) | |
915 (orig (marker-position eshell-last-output-end))) | |
916 (goto-char eshell-last-output-end) | |
917 (delete-region (point) (point-max)) | |
918 (when (and text (> (length text) 0)) | |
919 (insert text) | |
920 (put-text-property (1- (point)) (point) | |
921 'last-search-pos before) | |
922 (set-marker eshell-last-output-end orig) | |
923 (goto-char eshell-last-output-end)))))) | |
924 | |
925 (defun eshell-prepare-for-search () | |
926 "Make sure the old history file is at the beginning of the buffer." | |
927 (unless (get-text-property (point-min) 'history) | |
928 (save-excursion | |
929 (goto-char (point-min)) | |
930 (let ((end (copy-marker (point) t))) | |
931 (insert-file-contents eshell-history-file-name) | |
932 (set-text-properties (point-min) end | |
933 '(history t invisible t)))))) | |
934 | |
935 (defun eshell-isearch-backward (&optional invert) | |
936 "Do incremental regexp search backward through past commands." | |
937 (interactive) | |
938 (let ((inhibit-read-only t) end) | |
939 (eshell-prepare-for-search) | |
940 (goto-char (point-max)) | |
941 (set-marker eshell-last-output-end (point)) | |
942 (delete-region (point) (point-max))) | |
943 (isearch-mode invert t 'eshell-return-to-prompt)) | |
944 | |
945 (defun eshell-isearch-repeat-backward (&optional invert) | |
946 "Do incremental regexp search backward through past commands." | |
947 (interactive) | |
948 (let ((old-pos (get-text-property (1- (point-max)) | |
949 'last-search-pos))) | |
950 (when old-pos | |
951 (goto-char old-pos) | |
952 (if invert | |
953 (end-of-line) | |
954 (backward-char))) | |
955 (setq isearch-forward invert) | |
956 (isearch-search-and-update))) | |
957 | |
958 (defun eshell-isearch-forward () | |
959 "Do incremental regexp search backward through past commands." | |
960 (interactive) | |
961 (eshell-isearch-backward t)) | |
962 | |
963 (defun eshell-isearch-repeat-forward () | |
964 "Do incremental regexp search backward through past commands." | |
965 (interactive) | |
966 (eshell-isearch-repeat-backward t)) | |
967 | |
968 (defun eshell-isearch-cancel () | |
969 (interactive) | |
970 (goto-char eshell-last-output-end) | |
971 (delete-region (point) (point-max)) | |
972 (call-interactively 'isearch-cancel)) | |
973 | |
974 (defun eshell-isearch-abort () | |
975 (interactive) | |
976 (goto-char eshell-last-output-end) | |
977 (delete-region (point) (point-max)) | |
978 (call-interactively 'isearch-abort)) | |
979 | |
980 (defun eshell-isearch-delete-char () | |
981 (interactive) | |
982 (save-excursion | |
983 (isearch-delete-char))) | |
984 | |
985 (defun eshell-isearch-return () | |
986 (interactive) | |
987 (isearch-done) | |
988 (eshell-send-input)) | |
989 | |
52401 | 990 ;;; arch-tag: 1a847333-f864-4b96-9acd-b549d620b6c6 |
29876 | 991 ;;; em-hist.el ends here |