annotate lisp/comint/history.el @ 58:8b0bdfdf0cf0 r19-16-pre4

Import from CVS: tag r19-16-pre4
author cvs
date Mon, 13 Aug 2007 08:58:37 +0200
parents 0293115a14e9
children 131b0175ea99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 0
diff changeset
1 ;;; history.el --- Generic history stuff
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 0
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
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 4
diff changeset
18 ;; along with XEmacs; see the file COPYING. If not, write to the
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 4
diff changeset
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 4
diff changeset
20 ;; Boston, MA 02111-1307, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
4
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 0
diff changeset
22 ;;; Synched up with: Not in FSF
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 0
diff changeset
23
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 0
diff changeset
24 ;;; Commentary:
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 0
diff changeset
25
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;; suggested generic history stuff -- tale
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;; This is intended to provided easy access to a list of elements
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; being kept as a history ring.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;; 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
32 ;; 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
33 ;; to interact with these functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;; For example, a typical application of this is in interactive processes,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;; 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
37 ;; to the process so that they are easily retrieved for re-inspection or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;; re-use. Using process "foo" to illustrate:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;; Variable foo-history will be the list. foo-history-index would be the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;; 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
42 ;; 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
43 ;; user-variable which controls how many items are allowed to exist.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;; The following functions could interactive with the list; foo-mark
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ;; in these examples trackes the end of output from foo-process.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;; (defun foo-history-previous (arg) ;; Suggested binding: C-c C-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ;; "Retrieve the previous command sent to the foo process.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;; 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
51 ;; (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ;; (history-fetch 'foo-history 'foo-history-index (or arg 'previous)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;; foo-mark (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 ;; foo-history-next would look practically the same, but substituting "next"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 ;; for "previous". Suggested binding: C-c C-n
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 ;; (defun foo-history-clear () ;; Suggested binding: C-c C-u
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;; "Clear the input region for the foo-process and reset history location."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 ;; (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 ;; (delete-region foo-mark (goto-char (point-max))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 ;; To get the history on the stack, an extremely minimal function would look
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 ;; something like this, probably bound to RET:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 ;; (defun foo-send ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 ;; "Send a command to foo-process."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 ;; (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 ;; (let ((str (buffer-substring foo-mark (goto-char (point-max)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 ;; (insert ?\C-j)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 ;; (setq foo-history-index -1) ; reset the index
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 ;; (set-marker foo-mark (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 ;; (send-string foo-process str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 ;; (history-add 'foo-history str foo-history-size)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 ;; ToDo: history-isearch
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77
4
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 0
diff changeset
78 ;;; Code:
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 0
diff changeset
79
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (provide 'history)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (defvar history-last-search ""
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 "The last regexp used by history-search which resulted in a match.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (defun history-add (list item size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 "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
87 LIST should be the name of the list."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (set list (append (list item) (eval list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (let ((elist (eval list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (if (> (length elist) size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (setcdr (nthcdr (1- size) elist) nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (defun history-fetch (list index dir &optional beg end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 "Retrieve an entry from LIST, working from INDEX in direction DIR.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 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
96 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
97 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
98 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
99 nil is equivalent to ``next''.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 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
102 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
103 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
104 before the entry is inserted."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (let (str (eind (eval index)) (elist (eval list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 ((numberp dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (setq str (nth dir elist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (if str (set index dir) (message "No entry %d in %s." dir list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 ((or (not dir) (eq dir 'next))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (if (= eind -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (message "No next entry in %s." list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (set index (1- eind))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (setq str (if (zerop eind) "" (nth (1- eind) elist)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (if (>= (1+ eind) (length elist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (message "No previous entry in %s." list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (set index (1+ eind))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (setq str (nth (1+ eind) elist)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (if (not (and (integer-or-marker-p beg) str)) ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (if (integer-or-marker-p end) (delete-region beg end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (insert str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (goto-char beg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 str))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (defun history-search (list index dir regexp &optional beg end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 "In history LIST, starting at INDEX and working in direction DIR, find REGEXP.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 LIST and INDEX should be their respective symbol names. DIR nil or 'forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 means to search from the current index toward the most recent history entry.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 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
131 not checked in either case.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 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
134 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
135 then the region will be deleted between BEG and END."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (let* ((forw (or (not dir) (eq dir 'forward))) str found
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (eind (eval index))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (elist (eval list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (slist (if forw
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (nthcdr (- (length elist) eind) (reverse elist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (nthcdr (1+ eind) elist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (while (and (not found) slist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (if (string-match regexp (car slist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (setq found (car slist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 history-last-search regexp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (setq eind (+ (if forw -1 1) eind)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 slist (cdr slist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (if (not found)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (error "\"%s\" not found %s in %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 regexp (if forw "forward" "backward") list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (set index eind)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (if (not (integer-or-marker-p beg)) ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (if (integer-or-marker-p end) (delete-region beg end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (insert found)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (goto-char beg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 found))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (defun history-menu (list buffer &optional notemp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 "Show the history kept by LIST in BUFFER.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 This function will use ``with-output-to-temp-buffer'' unless optional third
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 argument NOTEMP is non-nil."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (let ((pop-up-windows t) (line 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (mapconcat (function (lambda (item)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (setq line (1+ line))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (format (format "%%%dd: %%s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (int-to-string (length list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 line item)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 list "\n")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (if notemp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (insert menu)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (display-buffer buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (with-output-to-temp-buffer buffer (princ menu)))))
4
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 0
diff changeset
175
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 0
diff changeset
176 ;;; history.el ends here