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