annotate lisp/comint/history.el @ 80:1ce6082ce73f r20-0b90

Import from CVS: tag r20-0b90
author cvs
date Mon, 13 Aug 2007 09:06:37 +0200
parents b9518feda344
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
1 ;;; history.el --- Generic history stuff
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
2
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Copyright (C) 1989 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; You should have received a copy of the GNU General Public License
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
18 ;; along with XEmacs; see the file COPYING. If not, write to the Free
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
19 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
20
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
21 ;;; Synched up with: Not in FSF
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
22
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
23 ;;; Commentary:
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;; suggested generic history stuff -- tale
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;; This is intended to provided easy access to a list of elements
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;; being kept as a history ring.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;; To use, variables for a list and the index to it need to be kept, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;; a limit to how large the list can grow. Short wrappers can than be provided
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;; to interact with these functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;; For example, a typical application of this is in interactive processes,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;; like shell or gdb. A history can be kept of commands that are sent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;; to the process so that they are easily retrieved for re-inspection or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;; re-use. Using process "foo" to illustrate:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;; Variable foo-history will be the list. foo-history-index would be the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;; pointer to the current item within the list; it is based with 0 being
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;; the most recent element added to the list. foo-history-size can be a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;; user-variable which controls how many items are allowed to exist.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;; The following functions could interactive with the list; foo-mark
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;; in these examples trackes the end of output from foo-process.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ;; (defun foo-history-previous (arg) ;; Suggested binding: C-c C-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;; "Retrieve the previous command sent to the foo process.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ;; ARG means to select that message out of the list (0 is the first)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;; (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ;; (history-fetch 'foo-history 'foo-history-index (or arg 'previous)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ;; foo-mark (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ;; foo-history-next would look practically the same, but substituting "next"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 ;; for "previous". Suggested binding: C-c C-n
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 ;; (defun foo-history-clear () ;; Suggested binding: C-c C-u
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 ;; "Clear the input region for the foo-process and reset history location."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;; (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 ;; (delete-region foo-mark (goto-char (point-max))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 ;; To get the history on the stack, an extremely minimal function would look
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 ;; something like this, probably bound to RET:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 ;; (defun foo-send ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 ;; "Send a command to foo-process."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 ;; (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 ;; (let ((str (buffer-substring foo-mark (goto-char (point-max)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 ;; (insert ?\C-j)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 ;; (setq foo-history-index -1) ; reset the index
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 ;; (set-marker foo-mark (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 ;; (send-string foo-process str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 ;; (history-add 'foo-history str foo-history-size)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 ;; ToDo: history-isearch
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
77 ;;; Code:
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
78
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (provide 'history)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (defvar history-last-search ""
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 "The last regexp used by history-search which resulted in a match.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (defun history-add (list item size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 "At the head of LIST append ITEM. Limit the length of LIST to SIZE elements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 LIST should be the name of the list."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (set list (append (list item) (eval list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (let ((elist (eval list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (if (> (length elist) size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (setcdr (nthcdr (1- size) elist) nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (defun history-fetch (list index dir &optional beg end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 "Retrieve an entry from LIST, working from INDEX in direction DIR.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 LIST should be the name of the list, for message purposes. INDEX should be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 the name of the variable used to index the list, so it can be maintained.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 DIR non-nil means to use previous entry, unless it is the symbol ``next''
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 to get the next entry or a number to get an absolute reference. DIR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 nil is equivalent to ``next''.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 If optional numeric argument BEG is preset, it is taken as the point to insert
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 the entry in the current buffer, leaving point at the start of the entry.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 If followed by a numeric END, the region between BEG and END will be deleted
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 before the entry is inserted."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (let (str (eind (eval index)) (elist (eval list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 ((numberp dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (setq str (nth dir elist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (if str (set index dir) (message "No entry %d in %s." dir list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 ((or (not dir) (eq dir 'next))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (if (= eind -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (message "No next entry in %s." list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (set index (1- eind))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (setq str (if (zerop eind) "" (nth (1- eind) elist)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (if (>= (1+ eind) (length elist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (message "No previous entry in %s." list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (set index (1+ eind))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (setq str (nth (1+ eind) elist)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (if (not (and (integer-or-marker-p beg) str)) ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (if (integer-or-marker-p end) (delete-region beg end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (insert str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (goto-char beg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 str))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (defun history-search (list index dir regexp &optional beg end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 "In history LIST, starting at INDEX and working in direction DIR, find REGEXP.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 LIST and INDEX should be their respective symbol names. DIR nil or 'forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 means to search from the current index toward the most recent history entry.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 DIR non-nil means to search toward the oldest entry. The current entry is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 not checked in either case.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 If an entry is found and optional numeric argument BEG exists then the entry
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 will be inserted there and point left at BEG. If numeric END also exists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 then the region will be deleted between BEG and END."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (let* ((forw (or (not dir) (eq dir 'forward))) str found
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (eind (eval index))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (elist (eval list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (slist (if forw
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (nthcdr (- (length elist) eind) (reverse elist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (nthcdr (1+ eind) elist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (while (and (not found) slist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (if (string-match regexp (car slist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (setq found (car slist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 history-last-search regexp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (setq eind (+ (if forw -1 1) eind)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 slist (cdr slist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (if (not found)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (error "\"%s\" not found %s in %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 regexp (if forw "forward" "backward") list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (set index eind)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (if (not (integer-or-marker-p beg)) ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (if (integer-or-marker-p end) (delete-region beg end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (insert found)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (goto-char beg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 found))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (defun history-menu (list buffer &optional notemp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 "Show the history kept by LIST in BUFFER.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 This function will use ``with-output-to-temp-buffer'' unless optional third
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 argument NOTEMP is non-nil."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 (let ((pop-up-windows t) (line 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (mapconcat (function (lambda (item)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (setq line (1+ line))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (format (format "%%%dd: %%s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (int-to-string (length list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 line item)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 list "\n")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (if notemp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (insert menu)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (display-buffer buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (with-output-to-temp-buffer buffer (princ menu)))))
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
174
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
175 ;;; history.el ends here