(PC-do-complete-and-exit): use minibuffer-prompt-end to
[emacs.git] / lisp / recentf.el
blobdad6d53638eb5634e39bdb4dacb6f565dabe3493
1 ;; recentf.el --- setup a menu of recently opened files
3 ;; Copyright (C) 1999 Free Software Foundation, Inc.
5 ;; Author: David Ponce <david.ponce@wanadoo.fr>
6 ;; Created: July 19 1999
7 ;; Keywords: customization
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
28 ;;; Code:
30 (require 'easymenu)
32 (defconst recentf-save-file-header
33 ";;; Automatically generated by `recentf' on %s.\n"
34 "Header to be written into the `recentf-save-file'.")
36 (defvar recentf-list nil
37 "List of recently opened files.")
39 (defvar recentf-update-menu-p t
40 "Non-nil if the recentf menu must be updated.")
42 (defvar recentf-initialized-p nil
43 "Non-nil if recentf already initialized.")
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))
59 (defgroup recentf nil
60 "Maintain a menu of recently opened files."
61 :group 'files)
63 (defcustom recentf-max-saved-items 20
64 "*Maximum number of items saved to `recentf-save-file'."
65 :group 'recentf
66 :type 'integer)
68 (defcustom recentf-save-file (expand-file-name "~/.recentf")
69 "*File to save `recentf-list' into."
70 :group 'recentf
71 :type 'file)
73 (defcustom recentf-exclude nil
74 "*List of regexps for filenames excluded from `recentf-list'."
75 :group 'recentf
76 :type '(repeat regexp))
78 (defcustom recentf-menu-title "Open Recent"
79 "*Name of the recentf menu."
80 :group 'recentf
81 :type 'string
82 :set 'recentf-menu-customization-changed)
84 (defcustom recentf-menu-path '("files")
85 "*Path where to add the recentf menu.
86 If nil add it at top-level (see also `easy-menu-change')."
87 :group 'recentf
88 :type '(choice (const :tag "Top Level" nil)
89 (sexp :tag "Menu Path"))
90 :set 'recentf-menu-customization-changed)
92 (defcustom recentf-menu-before "open-file"
93 "*Name of the menu before which the recentf menu will be added.
94 If nil add it at end of menu (see also `easy-menu-change')."
95 :group 'recentf
96 :type '(choice (string :tag "Name")
97 (const :tag "Last" nil))
98 :set 'recentf-menu-customization-changed)
100 (defcustom recentf-menu-action 'recentf-find-file
101 "*Function to invoke with a filename item of the recentf menu.
102 The default action `recentf-find-file' calls `find-file' to edit an
103 existing file. If the file does not exist or is not readable, it is
104 not edited and its name is removed from `recentf-list'. You can use
105 `find-file' instead to open non-existing files and keep them is the
106 list of recently opened files."
107 :group 'recentf
108 :type 'function
109 :set 'recentf-menu-customization-changed)
111 (defcustom recentf-max-menu-items 10
112 "*Maximum number of items in the recentf menu."
113 :group 'recentf
114 :type 'integer
115 :set 'recentf-menu-customization-changed)
117 (defcustom recentf-menu-filter nil
118 "*Function used to filter files displayed in the recentf menu.
119 Nil means no filter. The following functions are predefined:
121 - - `recentf-sort-ascending' to sort menu items in ascending order.
122 - - `recentf-sort-descending' to sort menu items in descending order.
124 The filter function is called with one argument, the list of filenames to be
125 displayed in the menu and must return a new list of filenames."
126 :group 'recentf
127 :type 'function
128 :set 'recentf-menu-customization-changed)
130 (defcustom recentf-menu-append-commands-p t
131 "*If not-nil command items are appended to the menu."
132 :group 'recentf
133 :type 'boolean
134 :set 'recentf-menu-customization-changed)
136 (defcustom recentf-keep-non-readable-files-p nil
137 "*If nil (default), non-readable files are not kept in `recentf-list'."
138 :group 'recentf
139 :type 'boolean
140 :set '(lambda (sym val)
141 (if val
142 (remove-hook kill-buffer-hook recentf-remove-file-hook)
143 (add-hook kill-buffer-hook recentf-remove-file-hook))
144 (custom-set-default sym val)))
146 (defcustom recentf-mode nil
147 "Toggle recentf mode.
148 When recentf mode is enabled, it maintains a menu for visiting files that
149 were operated on recently.
150 Setting this variable directly does not take effect;
151 use either \\[customize] or the function `recentf-mode'."
152 :set (lambda (symbol value)
153 (recentf-mode (or value 0)))
154 :initialize 'custom-initialize-default
155 :type 'boolean
156 :group 'recentf
157 :require 'recentf)
159 (defcustom recentf-load-hook nil
160 "*Normal hook run at end of loading the `recentf' package."
161 :group 'recentf
162 :type 'hook)
164 ;;;###autoload
165 (defun recentf-mode (&optional arg)
166 "Toggle recentf mode.
167 With prefix ARG, turn recentf mode on if and only if ARG is positive.
168 Returns the new status of recentf mode (non-nil means on).
170 When recentf mode is enabled, it maintains a menu for visiting files that
171 were operated on recently."
172 (interactive "P")
173 (when window-system
174 (let ((on-p (if arg
175 (> (prefix-numeric-value arg) 0)
176 (not recentf-mode))))
177 (if on-p
178 (unless recentf-initialized-p
179 (setq recentf-initialized-p t)
180 (if (file-readable-p recentf-save-file)
181 (load-file recentf-save-file))
182 (setq recentf-update-menu-p t)
183 (add-hook 'find-file-hooks 'recentf-add-file-hook)
184 (add-hook 'write-file-hooks 'recentf-add-file-hook)
185 ;; (add-hook 'activate-menubar-hook 'recentf-update-menu-hook)
186 (add-hook 'menu-bar-update-hook 'recentf-update-menu-hook)
187 (add-hook 'kill-emacs-hook 'recentf-save-list))
188 (when recentf-initialized-p
189 (setq recentf-initialized-p nil)
190 (recentf-save-list)
191 (easy-menu-remove-item nil recentf-menu-path recentf-menu-title)
192 (remove-hook 'find-file-hooks 'recentf-add-file-hook)
193 (remove-hook 'write-file-hooks 'recentf-add-file-hook)
194 ;; (remove-hook 'activate-menubar-hook 'recentf-update-menu-hook)
195 (remove-hook 'menu-bar-update-hook 'recentf-update-menu-hook)
196 (remove-hook 'kill-emacs-hook 'recentf-save-list)))
197 (setq recentf-mode on-p))))
199 (defun recentf-add-file-hook ()
200 "Insert the name of the file just opened or written into `recentf-list'."
201 (and buffer-file-name (recentf-add-file buffer-file-name))
202 nil)
204 (defun recentf-remove-file-hook ()
205 "When a buffer is killed remove a non readable file from `recentf-list'."
206 (and buffer-file-name (recentf-remove-if-non-readable buffer-file-name))
207 nil)
209 (defun recentf-update-menu-hook ()
210 "Update the recentf menu from the current `recentf-list'."
211 (when recentf-update-menu-p
212 (condition-case nil
213 (progn
214 (easy-menu-change recentf-menu-path
215 recentf-menu-title
216 (recentf-make-menu-items)
217 recentf-menu-before)
218 (setq recentf-update-menu-p nil))
219 (error nil))))
221 ;;;###autoload
222 (defun recentf-save-list ()
223 "Save the current `recentf-list' to the file `recentf-save-file'."
224 (interactive)
225 (let ((saved-list (recentf-elements recentf-max-saved-items)))
226 (with-temp-buffer
227 (erase-buffer)
228 (insert (format recentf-save-file-header (current-time-string)))
229 (insert "(setq recentf-list\n '(\n")
230 (mapcar '(lambda (e)
231 (insert (format " %S\n" e)))
232 saved-list)
233 (insert " ))")
234 (if (file-writable-p recentf-save-file)
235 (write-region (point-min) (point-max) recentf-save-file))
236 (kill-buffer (current-buffer))))
237 nil)
239 ;;;###autoload
240 (defun recentf-cleanup ()
241 "Remove all non-readable files from `recentf-list'."
242 (interactive)
243 (setq recentf-list (delq nil (mapcar '(lambda (f)
244 (and (file-readable-p f) f))
245 recentf-list)))
246 (setq recentf-update-menu-p t))
248 (defvar recentf-menu-items-for-commands
249 (list ["Cleanup list" recentf-cleanup t]
250 ["Save list now" recentf-save-list t]
251 (vector (format "Recentf Options...")
252 '(customize-group "recentf") t))
253 "List of menu items for recentf commands.")
255 (defun recentf-make-menu-items ()
256 "Make menu items from `recentf-list'."
257 (let ((file-items
258 (mapcar '(lambda (entry)
259 (vector entry (list recentf-menu-action entry) t))
260 (funcall (or recentf-menu-filter 'identity)
261 (recentf-elements recentf-max-menu-items)))))
262 (append (or file-items (list ["No files" t nil]))
263 (and recentf-menu-append-commands-p
264 (cons ["---" nil nil]
265 recentf-menu-items-for-commands)))))
267 (defun recentf-add-file (filename)
268 "Add or move FILENAME at the beginning of `recentf-list'.
269 Does nothing if FILENAME matches one of the `recentf-exclude' regexps."
270 (when (recentf-include-p filename)
271 (setq recentf-list (cons filename (delete filename recentf-list)))
272 (setq recentf-update-menu-p t)))
274 (defun recentf-remove-if-non-readable (filename)
275 "Remove FILENAME from `recentf-list' if not readable."
276 (unless (file-readable-p filename)
277 (setq recentf-list (delete filename recentf-list))
278 (setq recentf-update-menu-p t)))
280 (defun recentf-find-file (filename)
281 "Edit file FILENAME using `find-file'.
282 If FILENAME is not readable it is removed from `recentf-list'."
283 (if (file-readable-p filename)
284 (find-file filename)
285 (progn
286 (message "File `%s' not found." filename)
287 (setq recentf-list (delete filename recentf-list))
288 (setq recentf-update-menu-p t))))
290 (defun recentf-include-p (filename)
291 "Return t if FILENAME matches none of the `recentf-exclude' regexps."
292 (let ((rl recentf-exclude))
293 (while (and rl (not (string-match (car rl) filename)))
294 (setq rl (cdr rl)))
295 (null rl)))
297 (defun recentf-elements (n)
298 "Return a list of the first N elements of `recentf-list'."
299 (let ((lh nil) (l recentf-list))
300 (while (and l (> n 0))
301 (setq lh (cons (car l) lh))
302 (setq n (1- n))
303 (setq l (cdr l)))
304 (nreverse lh)))
306 (defun recentf-sort-ascending (l)
307 "Sort the list of strings L in ascending order."
308 (sort l '(lambda (e1 e2) (string-lessp e1 e2))))
310 (defun recentf-sort-descending (l)
311 "Sort the list of strings L in descending order."
312 (sort l '(lambda (e1 e2) (string-lessp e2 e1))))
314 (provide 'recentf)
316 (run-hooks 'recentf-load-hook)
318 ;;; recentf.el ends here.