annotate lisp/comint/history.el @ 0:376386a54a3c r19-14

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