]> code.delx.au - gnu-emacs/blob - lisp/recentf.el
(face-font-family-alternatives): Add arial to helv.
[gnu-emacs] / lisp / recentf.el
1 ;; recentf.el --- setup a menu of recently opened files
2
3 ;; Copyright (C) 1999 Free Software Foundation, Inc.
4
5 ;; Author: David Ponce <david.ponce@wanadoo.fr>
6 ;; Created: July 19 1999
7 ;; Keywords: customization
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (require 'easymenu)
31
32 (defconst recentf-save-file-header
33 ";;; Automatically generated by `recentf' on %s.\n"
34 "Header to be written into the `recentf-save-file'.")
35
36 (defvar recentf-list nil
37 "List of recently opened files.")
38
39 (defvar recentf-update-menu-p t
40 "Non-nil if the recentf menu must be updated.")
41
42 (defvar recentf-initialized-p nil
43 "Non-nil if recentf already initialized.")
44
45 ;; IMPORTANT: This function must be defined before the following defcustoms
46 ;; because it is used in their :set clause. To avoid byte-compiler warnings
47 ;; the `symbol-value' function is used to access the `recentf-menu-path'
48 ;; and `recentf-menu-title' values.
49 (defun recentf-menu-customization-changed (sym val)
50 "Function called when menu customization has changed.
51 It removes the recentf menu and forces its complete redrawing."
52 (when recentf-initialized-p
53 (easy-menu-remove-item nil
54 (symbol-value 'recentf-menu-path)
55 (symbol-value 'recentf-menu-title))
56 (setq recentf-update-menu-p t))
57 (custom-set-default sym val))
58
59 (defgroup recentf nil
60 "Maintain a menu of recently opened files."
61 :version "21.1"
62 :group 'files)
63
64 (defcustom recentf-max-saved-items 20
65 "*Maximum number of items saved to `recentf-save-file'."
66 :group 'recentf
67 :type 'integer)
68
69 (defcustom recentf-save-file (expand-file-name "~/.recentf")
70 "*File to save `recentf-list' into."
71 :group 'recentf
72 :type 'file)
73
74 (defcustom recentf-exclude nil
75 "*List of regexps for filenames excluded from `recentf-list'."
76 :group 'recentf
77 :type '(repeat regexp))
78
79 (defcustom recentf-menu-title "Open Recent"
80 "*Name of the recentf menu."
81 :group 'recentf
82 :type 'string
83 :set 'recentf-menu-customization-changed)
84
85 (defcustom recentf-menu-path '("files")
86 "*Path where to add the recentf menu.
87 If nil add it at top-level (see also `easy-menu-change')."
88 :group 'recentf
89 :type '(choice (const :tag "Top Level" nil)
90 (sexp :tag "Menu Path"))
91 :set 'recentf-menu-customization-changed)
92
93 (defcustom recentf-menu-before "open-file"
94 "*Name of the menu before which the recentf menu will be added.
95 If nil add it at end of menu (see also `easy-menu-change')."
96 :group 'recentf
97 :type '(choice (string :tag "Name")
98 (const :tag "Last" nil))
99 :set 'recentf-menu-customization-changed)
100
101 (defcustom recentf-menu-action 'recentf-find-file
102 "*Function to invoke with a filename item of the recentf menu.
103 The default action `recentf-find-file' calls `find-file' to edit an
104 existing file. If the file does not exist or is not readable, it is
105 not edited and its name is removed from `recentf-list'. You can use
106 `find-file' instead to open non-existing files and keep them is the
107 list of recently opened files."
108 :group 'recentf
109 :type 'function
110 :set 'recentf-menu-customization-changed)
111
112 (defcustom recentf-max-menu-items 10
113 "*Maximum number of items in the recentf menu."
114 :group 'recentf
115 :type 'integer
116 :set 'recentf-menu-customization-changed)
117
118 (defcustom recentf-menu-filter nil
119 "*Function used to filter files displayed in the recentf menu.
120 Nil means no filter. The following functions are predefined:
121
122 - - `recentf-sort-ascending' to sort menu items in ascending order.
123 - - `recentf-sort-descending' to sort menu items in descending order.
124
125 The filter function is called with one argument, the list of filenames to be
126 displayed in the menu and must return a new list of filenames."
127 :group 'recentf
128 :type 'function
129 :set 'recentf-menu-customization-changed)
130
131 (defcustom recentf-menu-append-commands-p t
132 "*If not-nil command items are appended to the menu."
133 :group 'recentf
134 :type 'boolean
135 :set 'recentf-menu-customization-changed)
136
137 (defcustom recentf-keep-non-readable-files-p nil
138 "*If nil (default), non-readable files are not kept in `recentf-list'."
139 :group 'recentf
140 :type 'boolean
141 :set '(lambda (sym val)
142 (if val
143 (remove-hook kill-buffer-hook recentf-remove-file-hook)
144 (add-hook kill-buffer-hook recentf-remove-file-hook))
145 (custom-set-default sym val)))
146
147 (defcustom recentf-mode nil
148 "Toggle recentf mode.
149 When recentf mode is enabled, it maintains a menu for visiting files that
150 were operated on recently.
151 Setting this variable directly does not take effect;
152 use either \\[customize] or the function `recentf-mode'."
153 :set (lambda (symbol value)
154 (recentf-mode (or value 0)))
155 :initialize 'custom-initialize-default
156 :type 'boolean
157 :group 'recentf
158 :require 'recentf)
159
160 (defcustom recentf-load-hook nil
161 "*Normal hook run at end of loading the `recentf' package."
162 :group 'recentf
163 :type 'hook)
164
165 ;;;###autoload
166 (defun recentf-mode (&optional arg)
167 "Toggle recentf mode.
168 With prefix ARG, turn recentf mode on if and only if ARG is positive.
169 Returns the new status of recentf mode (non-nil means on).
170
171 When recentf mode is enabled, it maintains a menu for visiting files that
172 were operated on recently."
173 (interactive "P")
174 (when window-system
175 (let ((on-p (if arg
176 (> (prefix-numeric-value arg) 0)
177 (not recentf-mode))))
178 (if on-p
179 (unless recentf-initialized-p
180 (setq recentf-initialized-p t)
181 (if (file-readable-p recentf-save-file)
182 (load-file recentf-save-file))
183 (setq recentf-update-menu-p t)
184 (add-hook 'find-file-hooks 'recentf-add-file-hook)
185 (add-hook 'write-file-hooks 'recentf-add-file-hook)
186 ;; (add-hook 'activate-menubar-hook 'recentf-update-menu-hook)
187 (add-hook 'menu-bar-update-hook 'recentf-update-menu-hook)
188 (add-hook 'kill-emacs-hook 'recentf-save-list))
189 (when recentf-initialized-p
190 (setq recentf-initialized-p nil)
191 (recentf-save-list)
192 (easy-menu-remove-item nil recentf-menu-path recentf-menu-title)
193 (remove-hook 'find-file-hooks 'recentf-add-file-hook)
194 (remove-hook 'write-file-hooks 'recentf-add-file-hook)
195 ;; (remove-hook 'activate-menubar-hook 'recentf-update-menu-hook)
196 (remove-hook 'menu-bar-update-hook 'recentf-update-menu-hook)
197 (remove-hook 'kill-emacs-hook 'recentf-save-list)))
198 (setq recentf-mode on-p))))
199
200 (defun recentf-add-file-hook ()
201 "Insert the name of the file just opened or written into `recentf-list'."
202 (and buffer-file-name (recentf-add-file buffer-file-name))
203 nil)
204
205 (defun recentf-remove-file-hook ()
206 "When a buffer is killed remove a non readable file from `recentf-list'."
207 (and buffer-file-name (recentf-remove-if-non-readable buffer-file-name))
208 nil)
209
210 (defun recentf-update-menu-hook ()
211 "Update the recentf menu from the current `recentf-list'."
212 (when recentf-update-menu-p
213 (condition-case nil
214 (progn
215 (easy-menu-change recentf-menu-path
216 recentf-menu-title
217 (recentf-make-menu-items)
218 recentf-menu-before)
219 (setq recentf-update-menu-p nil))
220 (error nil))))
221
222 ;;;###autoload
223 (defun recentf-save-list ()
224 "Save the current `recentf-list' to the file `recentf-save-file'."
225 (interactive)
226 (let ((saved-list (recentf-elements recentf-max-saved-items)))
227 (with-temp-buffer
228 (erase-buffer)
229 (insert (format recentf-save-file-header (current-time-string)))
230 (insert "(setq recentf-list\n '(\n")
231 (mapcar '(lambda (e)
232 (insert (format " %S\n" e)))
233 saved-list)
234 (insert " ))")
235 (if (file-writable-p recentf-save-file)
236 (write-region (point-min) (point-max) recentf-save-file))
237 (kill-buffer (current-buffer))))
238 nil)
239
240 ;;;###autoload
241 (defun recentf-cleanup ()
242 "Remove all non-readable files from `recentf-list'."
243 (interactive)
244 (setq recentf-list (delq nil (mapcar '(lambda (f)
245 (and (file-readable-p f) f))
246 recentf-list)))
247 (setq recentf-update-menu-p t))
248
249 (defvar recentf-menu-items-for-commands
250 (list ["Cleanup list" recentf-cleanup t]
251 ["Save list now" recentf-save-list t]
252 (vector (format "Recentf Options...")
253 '(customize-group "recentf") t))
254 "List of menu items for recentf commands.")
255
256 (defun recentf-make-menu-items ()
257 "Make menu items from `recentf-list'."
258 (let ((file-items
259 (mapcar '(lambda (entry)
260 (vector entry (list recentf-menu-action entry) t))
261 (funcall (or recentf-menu-filter 'identity)
262 (recentf-elements recentf-max-menu-items)))))
263 (append (or file-items (list ["No files" t nil]))
264 (and recentf-menu-append-commands-p
265 (cons ["---" nil nil]
266 recentf-menu-items-for-commands)))))
267
268 (defun recentf-add-file (filename)
269 "Add or move FILENAME at the beginning of `recentf-list'.
270 Does nothing if FILENAME matches one of the `recentf-exclude' regexps."
271 (when (recentf-include-p filename)
272 (setq recentf-list (cons filename (delete filename recentf-list)))
273 (setq recentf-update-menu-p t)))
274
275 (defun recentf-remove-if-non-readable (filename)
276 "Remove FILENAME from `recentf-list' if not readable."
277 (unless (file-readable-p filename)
278 (setq recentf-list (delete filename recentf-list))
279 (setq recentf-update-menu-p t)))
280
281 (defun recentf-find-file (filename)
282 "Edit file FILENAME using `find-file'.
283 If FILENAME is not readable it is removed from `recentf-list'."
284 (if (file-readable-p filename)
285 (find-file filename)
286 (progn
287 (message "File `%s' not found." filename)
288 (setq recentf-list (delete filename recentf-list))
289 (setq recentf-update-menu-p t))))
290
291 (defun recentf-include-p (filename)
292 "Return t if FILENAME matches none of the `recentf-exclude' regexps."
293 (let ((rl recentf-exclude))
294 (while (and rl (not (string-match (car rl) filename)))
295 (setq rl (cdr rl)))
296 (null rl)))
297
298 (defun recentf-elements (n)
299 "Return a list of the first N elements of `recentf-list'."
300 (let ((lh nil) (l recentf-list))
301 (while (and l (> n 0))
302 (setq lh (cons (car l) lh))
303 (setq n (1- n))
304 (setq l (cdr l)))
305 (nreverse lh)))
306
307 (defun recentf-sort-ascending (l)
308 "Sort the list of strings L in ascending order."
309 (sort l '(lambda (e1 e2) (string-lessp e1 e2))))
310
311 (defun recentf-sort-descending (l)
312 "Sort the list of strings L in descending order."
313 (sort l '(lambda (e1 e2) (string-lessp e2 e1))))
314
315 (provide 'recentf)
316
317 (run-hooks 'recentf-load-hook)
318
319 ;;; recentf.el ends here.