]> code.delx.au - gnu-emacs/blob - lisp/recentf.el
* term/rxvt.el (rxvt-register-default-colors): Add support for 88
[gnu-emacs] / lisp / recentf.el
1 ;;; recentf.el --- setup a menu of recently opened files
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
5
6 ;; Author: David Ponce <david@dponce.com>
7 ;; Created: July 19 1999
8 ;; Keywords: files
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published
14 ;; by the Free Software Foundation; either version 2, or (at your
15 ;; option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This package maintains a menu for visiting files that were operated
30 ;; on recently. When enabled a new "Open Recent" sub menu is
31 ;; displayed in the "Files" menu. The recent files list is
32 ;; automatically saved across Emacs sessions. You can customize the
33 ;; number of recent files displayed, the location of the menu and
34 ;; others options (see the source code for details).
35
36 ;;; History:
37 ;;
38
39 ;;; Code:
40 (require 'easymenu)
41 (require 'tree-widget)
42 (require 'timer)
43
44 ;;; Internal data
45 ;;
46 (defvar recentf-list nil
47 "List of recently opened files.")
48
49 (defvar recentf-data-cache nil
50 "Cache of data used to build the recentf menu.
51 The menu is rebuilt when this data has changed.")
52 \f
53 ;;; Customization
54 ;;
55 (defgroup recentf nil
56 "Maintain a menu of recently opened files."
57 :version "21.1"
58 :group 'files)
59
60 (defgroup recentf-filters nil
61 "Group to customize recentf menu filters.
62 You should define the options of your own filters in this group."
63 :group 'recentf)
64
65 (defcustom recentf-max-saved-items 20
66 "*Maximum number of items of the recent list that will be saved.
67 A nil value means to save the whole list.
68 See the command `recentf-save-list'."
69 :group 'recentf
70 :type 'integer)
71
72 (defcustom recentf-save-file "~/.recentf"
73 "*File to save the recent list into."
74 :group 'recentf
75 :type 'file)
76
77 (defcustom recentf-save-file-modes 384 ;; 0600
78 "Mode bits of recentf save file, as an integer, or nil.
79 If non-nil, after writing `recentf-save-file', set its mode bits to
80 this value. By default give R/W access only to the user who owns that
81 file. See also the function `set-file-modes'."
82 :group 'recentf
83 :type '(choice (const :tag "Don't change" nil)
84 integer))
85
86 (defcustom recentf-exclude nil
87 "*List of regexps and predicates for filenames excluded from the recent list.
88 When a filename matches any of the regexps or satisfies any of the
89 predicates it is excluded from the recent list.
90 A predicate is a function that is passed a filename to check and that
91 must return non-nil to exclude it."
92 :group 'recentf
93 :type '(repeat (choice regexp function)))
94
95 (defcustom recentf-keep
96 '(file-readable-p)
97 "*List of regexps and predicates for filenames kept in the recent list.
98 Regexps and predicates are tried in the specified order.
99 When nil all filenames are kept in the recent list.
100 When a filename matches any of the regexps or satisfies any of the
101 predicates it is kept in the recent list.
102 The default is to keep readable files.
103 A predicate is a function that is passed a filename to check and that
104 must return non-nil to keep it. For example, you can add the
105 `file-remote-p' predicate in front of this list to keep remote file
106 names in the recent list without checking their readability through a
107 remote access."
108 :group 'recentf
109 :type '(repeat (choice regexp function)))
110
111 (defun recentf-menu-customization-changed (variable value)
112 "Function called when the recentf menu customization has changed.
113 Set VARIABLE with VALUE, and force a rebuild of the recentf menu."
114 (when (featurep 'recentf)
115 ;; Unavailable until recentf has been loaded.
116 (recentf-clear-data))
117 (set-default variable value))
118
119 (defcustom recentf-menu-title "Open Recent"
120 "*Name of the recentf menu."
121 :group 'recentf
122 :type 'string
123 :set 'recentf-menu-customization-changed)
124
125 (defcustom recentf-menu-path '("File")
126 "*Path where to add the recentf menu.
127 If nil add it at top level (see also `easy-menu-add-item')."
128 :group 'recentf
129 :type '(choice (const :tag "Top Level" nil)
130 (sexp :tag "Menu Path"))
131 :set 'recentf-menu-customization-changed)
132
133 (defcustom recentf-menu-before "Open File..."
134 "*Name of the menu before which the recentf menu will be added.
135 If nil add it at end of menu (see also `easy-menu-add-item')."
136 :group 'recentf
137 :type '(choice (string :tag "Name")
138 (const :tag "Last" nil))
139 :set 'recentf-menu-customization-changed)
140
141 (defcustom recentf-menu-action 'find-file
142 "*Function to invoke with a filename item of the recentf menu.
143 The default is to call `find-file' to edit the selected file."
144 :group 'recentf
145 :type 'function
146 :set 'recentf-menu-customization-changed)
147
148 (defcustom recentf-max-menu-items 10
149 "*Maximum number of items in the recentf menu."
150 :group 'recentf
151 :type 'integer
152 :set 'recentf-menu-customization-changed)
153
154 (defcustom recentf-menu-filter nil
155 "*Function used to filter files displayed in the recentf menu.
156 A nil value means no filter. The following functions are predefined:
157
158 - `recentf-sort-ascending'
159 Sort menu items in ascending order.
160 - `recentf-sort-descending'
161 Sort menu items in descending order.
162 - `recentf-sort-basenames-ascending'
163 Sort menu items by filenames sans directory in ascending order.
164 - `recentf-sort-basenames-descending'
165 Sort menu items by filenames sans directory in descending order.
166 - `recentf-sort-directories-ascending'
167 Sort menu items by directories in ascending order.
168 - `recentf-sort-directories-descending'
169 Sort menu items by directories in descending order.
170 - `recentf-show-basenames'
171 Show filenames sans directory in menu items.
172 - `recentf-show-basenames-ascending'
173 Show filenames sans directory in ascending order.
174 - `recentf-show-basenames-descending'
175 Show filenames sans directory in descending order.
176 - `recentf-relative-filter'
177 Show filenames relative to `default-directory'.
178 - `recentf-arrange-by-rule'
179 Show sub-menus following user defined rules.
180 - `recentf-arrange-by-mode'
181 Show a sub-menu for each major mode.
182 - `recentf-arrange-by-dir'
183 Show a sub-menu for each directory.
184 - `recentf-filter-changer'
185 Manage a ring of filters.
186
187 The filter function is called with one argument, the list of menu
188 elements used to build the menu and must return a new list of menu
189 elements (see `recentf-make-menu-element' for menu element form)."
190 :group 'recentf
191 :type '(radio (const nil)
192 (function-item recentf-sort-ascending)
193 (function-item recentf-sort-descending)
194 (function-item recentf-sort-basenames-ascending)
195 (function-item recentf-sort-basenames-descending)
196 (function-item recentf-sort-directories-ascending)
197 (function-item recentf-sort-directories-descending)
198 (function-item recentf-show-basenames)
199 (function-item recentf-show-basenames-ascending)
200 (function-item recentf-show-basenames-descending)
201 (function-item recentf-relative-filter)
202 (function-item recentf-arrange-by-rule)
203 (function-item recentf-arrange-by-mode)
204 (function-item recentf-arrange-by-dir)
205 (function-item recentf-filter-changer)
206 function)
207 :set 'recentf-menu-customization-changed)
208
209 (defcustom recentf-menu-append-commands-flag t
210 "*Non-nil means to append command items to the menu."
211 :group 'recentf
212 :type 'boolean
213 :set 'recentf-menu-customization-changed)
214
215 (define-obsolete-variable-alias 'recentf-menu-append-commands-p
216 'recentf-menu-append-commands-flag
217 "22.1")
218
219 (defcustom recentf-auto-cleanup 'mode
220 "*Define when to automatically cleanup the recent list.
221 The following values can be set:
222
223 - `mode'
224 Cleanup when turning the mode on (default).
225 - `never'
226 Never cleanup the list automatically.
227 - A number
228 Cleanup each time Emacs has been idle that number of seconds.
229 - A time string
230 Cleanup at specified time string, for example at \"11:00pm\".
231
232 Setting this variable directly does not take effect;
233 use \\[customize].
234
235 See also the command `recentf-cleanup', that can be used to manually
236 cleanup the list."
237 :group 'recentf
238 :type '(radio (const :tag "When mode enabled"
239 :value mode)
240 (const :tag "Never"
241 :value never)
242 (number :tag "When idle that seconds"
243 :value 300)
244 (string :tag "At time"
245 :value "11:00pm"))
246 :set (lambda (variable value)
247 (set-default variable value)
248 (when (featurep 'recentf)
249 ;; Unavailable until recentf has been loaded.
250 (recentf-auto-cleanup))))
251
252 (defcustom recentf-initialize-file-name-history t
253 "*Non-nil means to initialize `file-name-history' with the recent list.
254 If `file-name-history' is not empty, do nothing."
255 :group 'recentf
256 :type 'boolean)
257
258 (defcustom recentf-load-hook nil
259 "*Normal hook run at end of loading the `recentf' package."
260 :group 'recentf
261 :type 'hook)
262
263 (defcustom recentf-filename-handler nil
264 "Function to call to process filename handled by recentf.
265 It is passed a filename to give a chance to transform it.
266 If it returns nil, the filename is left unchanged."
267 :group 'recentf
268 :type '(choice (const :tag "None" nil)
269 (const abbreviate-file-name)
270 function))
271
272 (defcustom recentf-show-file-shortcuts-flag t
273 "Whether to show ``[N]'' for the Nth item up to 10.
274 If non-nil, `recentf-open-files' will show labels for keys that can be
275 used as shortcuts to open the Nth file."
276 :group 'recentf
277 :type 'boolean)
278
279 \f
280 ;;; Utilities
281 ;;
282 (defconst recentf-case-fold-search
283 (memq system-type '(vax-vms windows-nt cygwin))
284 "Non-nil if recentf searches and matches should ignore case.")
285
286 (defsubst recentf-string-equal (s1 s2)
287 "Return non-nil if strings S1 and S2 have identical contents.
288 Ignore case if `recentf-case-fold-search' is non-nil."
289 (if recentf-case-fold-search
290 (string-equal (downcase s1) (downcase s2))
291 (string-equal s1 s2)))
292
293 (defsubst recentf-string-lessp (s1 s2)
294 "Return non-nil if string S1 is less than S2 in lexicographic order.
295 Ignore case if `recentf-case-fold-search' is non-nil."
296 (if recentf-case-fold-search
297 (string-lessp (downcase s1) (downcase s2))
298 (string-lessp s1 s2)))
299
300 (defun recentf-string-member (elt list)
301 "Return non-nil if ELT is an element of LIST.
302 The value is actually the tail of LIST whose car is ELT.
303 ELT must be a string and LIST a list of strings.
304 Ignore case if `recentf-case-fold-search' is non-nil."
305 (while (and list (not (recentf-string-equal elt (car list))))
306 (setq list (cdr list)))
307 list)
308
309 (defsubst recentf-trunc-list (l n)
310 "Return from L the list of its first N elements."
311 (let (nl)
312 (while (and l (> n 0))
313 (setq nl (cons (car l) nl)
314 n (1- n)
315 l (cdr l)))
316 (nreverse nl)))
317
318 (defun recentf-dump-variable (variable &optional limit)
319 "Insert a \"(setq VARIABLE value)\" in the current buffer.
320 When the value of VARIABLE is a list, optional argument LIMIT
321 specifies a maximum number of elements to insert. By default insert
322 the full list."
323 (let ((value (symbol-value variable)))
324 (if (atom value)
325 (insert (format "\n(setq %S %S)\n" variable value))
326 (when (and (integerp limit) (> limit 0))
327 (setq value (recentf-trunc-list value limit)))
328 (insert (format "\n(setq %S\n '(" variable))
329 (dolist (e value)
330 (insert (format "\n %S" e)))
331 (insert "\n ))\n"))))
332
333 (defvar recentf-auto-cleanup-timer nil
334 "Timer used to automatically cleanup the recent list.
335 See also the option `recentf-auto-cleanup'.")
336
337 (defun recentf-auto-cleanup ()
338 "Automatic cleanup of the recent list."
339 (when (timerp recentf-auto-cleanup-timer)
340 (cancel-timer recentf-auto-cleanup-timer))
341 (when recentf-mode
342 (setq recentf-auto-cleanup-timer
343 (cond
344 ((eq 'mode recentf-auto-cleanup)
345 (recentf-cleanup)
346 nil)
347 ((numberp recentf-auto-cleanup)
348 (run-with-idle-timer
349 recentf-auto-cleanup t 'recentf-cleanup))
350 ((stringp recentf-auto-cleanup)
351 (run-at-time
352 recentf-auto-cleanup nil 'recentf-cleanup))))))
353 \f
354 ;;; File functions
355 ;;
356 (defsubst recentf-push (filename)
357 "Push FILENAME into the recent list, if it isn't there yet.
358 If it is there yet, move it at the beginning of the list.
359 If `recentf-case-fold-search' is non-nil, ignore case when comparing
360 filenames."
361 (let ((m (recentf-string-member filename recentf-list)))
362 (and m (setq recentf-list (delq (car m) recentf-list)))
363 (push filename recentf-list)))
364
365 (defsubst recentf-expand-file-name (name)
366 "Convert filename NAME to absolute, and canonicalize it.
367 See also the function `expand-file-name'.
368 If defined, call the function `recentf-filename-handler'
369 to post process the canonical name."
370 (let* ((filename (expand-file-name name)))
371 (or (and recentf-filename-handler
372 (funcall recentf-filename-handler filename))
373 filename)))
374
375 (defun recentf-include-p (filename)
376 "Return non-nil if FILENAME should be included in the recent list.
377 That is, if it doesn't match any of the `recentf-exclude' checks."
378 (let ((case-fold-search recentf-case-fold-search)
379 (checks recentf-exclude)
380 (keepit t))
381 (while (and checks keepit)
382 (setq keepit (condition-case nil
383 (not (if (stringp (car checks))
384 ;; A regexp
385 (string-match (car checks) filename)
386 ;; A predicate
387 (funcall (car checks) filename)))
388 (error nil))
389 checks (cdr checks)))
390 keepit))
391
392 (defun recentf-keep-p (filename)
393 "Return non-nil if FILENAME should be kept in the recent list.
394 That is, if it matches any of the `recentf-keep' checks."
395 (let* ((case-fold-search recentf-case-fold-search)
396 (checks recentf-keep)
397 (keepit (null checks)))
398 (while (and checks (not keepit))
399 (setq keepit (condition-case nil
400 (if (stringp (car checks))
401 ;; A regexp
402 (string-match (car checks) filename)
403 ;; A predicate
404 (funcall (car checks) filename))
405 (error nil))
406 checks (cdr checks)))
407 keepit))
408
409 (defsubst recentf-add-file (filename)
410 "Add or move FILENAME at the beginning of the recent list.
411 Does nothing if the name satisfies any of the `recentf-exclude'
412 regexps or predicates."
413 (setq filename (recentf-expand-file-name filename))
414 (when (recentf-include-p filename)
415 (recentf-push filename)))
416
417 (defsubst recentf-remove-if-non-kept (filename)
418 "Remove FILENAME from the recent list, if file is not kept.
419 Return non-nil if FILENAME has been removed."
420 (unless (recentf-keep-p filename)
421 (let ((m (recentf-string-member
422 (recentf-expand-file-name filename) recentf-list)))
423 (and m (setq recentf-list (delq (car m) recentf-list))))))
424
425 (defsubst recentf-directory-compare (f1 f2)
426 "Compare absolute filenames F1 and F2.
427 First compare directories, then filenames sans directory.
428 Return non-nil if F1 is less than F2."
429 (let ((d1 (file-name-directory f1))
430 (d2 (file-name-directory f2)))
431 (if (recentf-string-equal d1 d2)
432 (recentf-string-lessp (file-name-nondirectory f1)
433 (file-name-nondirectory f2))
434 (recentf-string-lessp d1 d2))))
435 \f
436 ;;; Menu building
437 ;;
438 (defvar recentf-menu-items-for-commands
439 (list ["Cleanup list"
440 recentf-cleanup
441 :help "Remove all excluded and non-kept files from the recent list"
442 :active t]
443 ["Edit list..."
444 recentf-edit-list
445 :help "Edit the files that are kept in the recent list"
446 :active t]
447 ["Save list now"
448 recentf-save-list
449 :help "Save the list of recently opened files now"
450 :active t]
451 ["Options..."
452 (customize-group "recentf")
453 :help "Customize recently opened files menu and options"
454 :active t]
455 )
456 "List of menu items for recentf commands.")
457
458 (defvar recentf-menu-filter-commands nil
459 "This variable can be used by menu filters to setup their own command menu.
460 If non-nil it must contain a list of valid menu-items to be appended
461 to the recent file list part of the menu. Before calling a menu
462 filter function this variable is reset to nil.")
463
464 (defsubst recentf-elements (n)
465 "Return a list of the first N elements of the recent list."
466 (recentf-trunc-list recentf-list n))
467
468 (defsubst recentf-make-menu-element (menu-item menu-value)
469 "Create a new menu-element.
470 A menu element is a pair (MENU-ITEM . MENU-VALUE), where MENU-ITEM is
471 the menu item string displayed. MENU-VALUE is the file to be open
472 when the corresponding MENU-ITEM is selected. Or it is a
473 pair (SUB-MENU-TITLE . MENU-ELEMENTS) where SUB-MENU-TITLE is a
474 sub-menu title and MENU-ELEMENTS is the list of menu elements in the
475 sub-menu."
476 (cons menu-item menu-value))
477
478 (defsubst recentf-menu-element-item (e)
479 "Return the item part of the menu-element E."
480 (car e))
481
482 (defsubst recentf-menu-element-value (e)
483 "Return the value part of the menu-element E."
484 (cdr e))
485
486 (defsubst recentf-set-menu-element-item (e item)
487 "Change the item part of menu-element E to ITEM."
488 (setcar e item))
489
490 (defsubst recentf-set-menu-element-value (e value)
491 "Change the value part of menu-element E to VALUE."
492 (setcdr e value))
493
494 (defsubst recentf-sub-menu-element-p (e)
495 "Return non-nil if menu-element E defines a sub-menu."
496 (consp (recentf-menu-element-value e)))
497
498 (defsubst recentf-make-default-menu-element (file)
499 "Make a new default menu element with FILE.
500 This a menu element (FILE . FILE)."
501 (recentf-make-menu-element file file))
502
503 (defsubst recentf-menu-elements (n)
504 "Return a list of the first N default menu elements from the recent list.
505 See also `recentf-make-default-menu-element'."
506 (mapcar 'recentf-make-default-menu-element
507 (recentf-elements n)))
508
509 (defun recentf-apply-menu-filter (filter l)
510 "Apply function FILTER to the list of menu-elements L.
511 It takes care of sub-menu elements in L and recursively apply FILTER
512 to them. It is guaranteed that FILTER receives only a list of single
513 menu-elements (no sub-menu)."
514 (if (and l (functionp filter))
515 (let ((case-fold-search recentf-case-fold-search)
516 elts others)
517 ;; split L into two sub-listes, one of sub-menus elements and
518 ;; another of single menu elements.
519 (dolist (elt l)
520 (if (recentf-sub-menu-element-p elt)
521 (push elt elts)
522 (push elt others)))
523 ;; Apply FILTER to single elements.
524 (when others
525 (setq others (funcall filter (nreverse others))))
526 ;; Apply FILTER to sub-menu elements.
527 (setq l nil)
528 (dolist (elt elts)
529 (recentf-set-menu-element-value
530 elt (recentf-apply-menu-filter
531 filter (recentf-menu-element-value elt)))
532 (push elt l))
533 ;; Return the new filtered menu element list.
534 (nconc l others))
535 l))
536
537 (defun recentf-make-menu-items ()
538 "Make menu items from the recent list."
539 (setq recentf-menu-filter-commands nil)
540 (let ((file-items
541 (mapcar 'recentf-make-menu-item
542 (recentf-apply-menu-filter
543 recentf-menu-filter
544 (recentf-menu-elements recentf-max-menu-items)))))
545 (append (or file-items (list ["No files" t
546 :help "No recent file to open"
547 :active nil]))
548 (and (< recentf-max-menu-items (length recentf-list))
549 (list ["More..." recentf-open-more-files
550 :help "Open files that are not in the menu"
551 :active t]))
552 (and recentf-menu-filter-commands
553 (cons "---"
554 recentf-menu-filter-commands))
555 (and recentf-menu-append-commands-flag
556 (cons "---"
557 recentf-menu-items-for-commands)))))
558
559 (defsubst recentf-make-menu-item (elt)
560 "Make a menu item from menu element ELT."
561 (let ((item (recentf-menu-element-item elt))
562 (value (recentf-menu-element-value elt)))
563 (if (recentf-sub-menu-element-p elt)
564 (cons item (mapcar 'recentf-make-menu-item value))
565 (vector item (list recentf-menu-action value)
566 :help (concat "Open " value)
567 :active t))))
568
569 (defsubst recentf-menu-bar ()
570 "Return the keymap of the global menu bar."
571 (lookup-key global-map [menu-bar]))
572
573 (defun recentf-clear-data ()
574 "Clear data used to build the recentf menu.
575 This forces a rebuild of the menu."
576 (easy-menu-remove-item (recentf-menu-bar)
577 recentf-menu-path recentf-menu-title)
578 (setq recentf-data-cache nil))
579 \f
580 ;;; Predefined menu filters
581 ;;
582 (defsubst recentf-sort-ascending (l)
583 "Sort the list of menu elements L in ascending order.
584 The MENU-ITEM part of each menu element is compared."
585 (sort (copy-sequence l)
586 #'(lambda (e1 e2)
587 (recentf-string-lessp
588 (recentf-menu-element-item e1)
589 (recentf-menu-element-item e2)))))
590
591 (defsubst recentf-sort-descending (l)
592 "Sort the list of menu elements L in descending order.
593 The MENU-ITEM part of each menu element is compared."
594 (sort (copy-sequence l)
595 #'(lambda (e1 e2)
596 (recentf-string-lessp
597 (recentf-menu-element-item e2)
598 (recentf-menu-element-item e1)))))
599
600 (defsubst recentf-sort-basenames-ascending (l)
601 "Sort the list of menu elements L in ascending order.
602 Only filenames sans directory are compared."
603 (sort (copy-sequence l)
604 #'(lambda (e1 e2)
605 (recentf-string-lessp
606 (file-name-nondirectory (recentf-menu-element-value e1))
607 (file-name-nondirectory (recentf-menu-element-value e2))))))
608
609 (defsubst recentf-sort-basenames-descending (l)
610 "Sort the list of menu elements L in descending order.
611 Only filenames sans directory are compared."
612 (sort (copy-sequence l)
613 #'(lambda (e1 e2)
614 (recentf-string-lessp
615 (file-name-nondirectory (recentf-menu-element-value e2))
616 (file-name-nondirectory (recentf-menu-element-value e1))))))
617
618 (defsubst recentf-sort-directories-ascending (l)
619 "Sort the list of menu elements L in ascending order.
620 Compares directories then filenames to order the list."
621 (sort (copy-sequence l)
622 #'(lambda (e1 e2)
623 (recentf-directory-compare
624 (recentf-menu-element-value e1)
625 (recentf-menu-element-value e2)))))
626
627 (defsubst recentf-sort-directories-descending (l)
628 "Sort the list of menu elements L in descending order.
629 Compares directories then filenames to order the list."
630 (sort (copy-sequence l)
631 #'(lambda (e1 e2)
632 (recentf-directory-compare
633 (recentf-menu-element-value e2)
634 (recentf-menu-element-value e1)))))
635
636 (defun recentf-show-basenames (l &optional no-dir)
637 "Filter the list of menu elements L to show filenames sans directory.
638 When a filename is duplicated, it is appended a sequence number if
639 optional argument NO-DIR is non-nil, or its directory otherwise."
640 (let (filtered-names filtered-list full name counters sufx)
641 (dolist (elt l (nreverse filtered-list))
642 (setq full (recentf-menu-element-value elt)
643 name (file-name-nondirectory full))
644 (if (not (member name filtered-names))
645 (push name filtered-names)
646 (if no-dir
647 (if (setq sufx (assoc name counters))
648 (setcdr sufx (1+ (cdr sufx)))
649 (setq sufx 1)
650 (push (cons name sufx) counters))
651 (setq sufx (file-name-directory full)))
652 (setq name (format "%s(%s)" name sufx)))
653 (push (recentf-make-menu-element name full) filtered-list))))
654
655 (defsubst recentf-show-basenames-ascending (l)
656 "Filter the list of menu elements L to show filenames sans directory.
657 Filenames are sorted in ascending order.
658 This filter combines the `recentf-sort-basenames-ascending' and
659 `recentf-show-basenames' filters."
660 (recentf-show-basenames (recentf-sort-basenames-ascending l)))
661
662 (defsubst recentf-show-basenames-descending (l)
663 "Filter the list of menu elements L to show filenames sans directory.
664 Filenames are sorted in descending order.
665 This filter combines the `recentf-sort-basenames-descending' and
666 `recentf-show-basenames' filters."
667 (recentf-show-basenames (recentf-sort-basenames-descending l)))
668
669 (defun recentf-relative-filter (l)
670 "Filter the list of menu-elements L to show relative filenames.
671 Filenames are relative to the `default-directory'."
672 (mapcar #'(lambda (menu-element)
673 (let* ((ful (recentf-menu-element-value menu-element))
674 (rel (file-relative-name ful default-directory)))
675 (if (string-match "^\\.\\." rel)
676 menu-element
677 (recentf-make-menu-element rel ful))))
678 l))
679 \f
680 ;;; Rule based menu filters
681 ;;
682 (defcustom recentf-arrange-rules
683 '(
684 ("Elisp files (%d)" ".\\.el$")
685 ("Java files (%d)" ".\\.java$")
686 ("C/C++ files (%d)" "c\\(pp\\)?$")
687 )
688 "*List of rules used by `recentf-arrange-by-rule' to build sub-menus.
689 A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
690 displayed title of the sub-menu where a '%d' `format' pattern is
691 replaced by the number of items in the sub-menu. MATCHER is a regexp
692 or a list of regexps. Items matching one of the regular expressions in
693 MATCHER are added to the corresponding sub-menu."
694 :group 'recentf-filters
695 :type '(repeat (cons string (repeat regexp)))
696 :set 'recentf-menu-customization-changed)
697
698 (defcustom recentf-arrange-by-rule-others "Other files (%d)"
699 "*Title of the `recentf-arrange-by-rule' sub-menu.
700 This is for the menu where items that don't match any
701 `recentf-arrange-rules' are displayed. If nil these items are
702 displayed in the main recent files menu. A '%d' `format' pattern in
703 the title is replaced by the number of items in the sub-menu."
704 :group 'recentf-filters
705 :type '(choice (const :tag "Main menu" nil)
706 (string :tag "Title"))
707 :set 'recentf-menu-customization-changed)
708
709 (defcustom recentf-arrange-by-rules-min-items 0
710 "*Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
711 If the number of items in a sub-menu is less than this value the
712 corresponding sub-menu items are displayed in the main recent files
713 menu or in the `recentf-arrange-by-rule-others' sub-menu if
714 defined."
715 :group 'recentf-filters
716 :type 'number
717 :set 'recentf-menu-customization-changed)
718
719 (defcustom recentf-arrange-by-rule-subfilter nil
720 "*Function called by a rule based filter to filter sub-menu elements.
721 A nil value means no filter. See also `recentf-menu-filter'.
722 You can't use another rule based filter here."
723 :group 'recentf-filters
724 :type '(choice (const nil) function)
725 :set (lambda (variable value)
726 (when (memq value '(recentf-arrange-by-rule
727 recentf-arrange-by-mode
728 recentf-arrange-by-dir))
729 (error "Recursive use of a rule based filter"))
730 (recentf-menu-customization-changed variable value)))
731
732 (defun recentf-match-rule-p (matcher filename)
733 "Return non-nil if the rule specified by MATCHER match FILENAME.
734 See `recentf-arrange-rules' for details on MATCHER."
735 (if (stringp matcher)
736 (string-match matcher filename)
737 (while (and (consp matcher)
738 (not (string-match (car matcher) filename)))
739 (setq matcher (cdr matcher)))
740 matcher))
741
742 (defun recentf-arrange-by-rule (l)
743 "Filter the list of menu-elements L.
744 Arrange them in sub-menus following rules in `recentf-arrange-rules'."
745 (if (not recentf-arrange-rules)
746 l
747 (let ((menus (mapcar #'(lambda (r) (list (car r)))
748 recentf-arrange-rules))
749 menu others min file rules elts count)
750 (dolist (elt l)
751 (setq file (recentf-menu-element-value elt)
752 rules recentf-arrange-rules
753 elts menus
754 menu nil)
755 (while (and (not menu) rules)
756 (when (recentf-match-rule-p (cdar rules) file)
757 (setq menu (car elts))
758 (recentf-set-menu-element-value
759 menu (cons elt (recentf-menu-element-value menu))))
760 (setq rules (cdr rules)
761 elts (cdr elts)))
762 (unless menu
763 (push elt others)))
764
765 (setq l nil
766 min (if (natnump recentf-arrange-by-rules-min-items)
767 recentf-arrange-by-rules-min-items 0))
768 (dolist (menu menus)
769 (when (setq elts (recentf-menu-element-value menu))
770 (setq count (length elts))
771 (if (< count min)
772 (setq others (nconc elts others))
773 (recentf-set-menu-element-item
774 menu (format (recentf-menu-element-item menu) count))
775 (recentf-set-menu-element-value
776 menu (recentf-apply-menu-filter
777 recentf-arrange-by-rule-subfilter (nreverse elts)))
778 (push menu l))))
779
780 (if (and (stringp recentf-arrange-by-rule-others) others)
781 (nreverse
782 (cons
783 (recentf-make-menu-element
784 (format recentf-arrange-by-rule-others (length others))
785 (recentf-apply-menu-filter
786 recentf-arrange-by-rule-subfilter (nreverse others)))
787 l))
788 (nconc
789 (nreverse l)
790 (recentf-apply-menu-filter
791 recentf-arrange-by-rule-subfilter (nreverse others)))))
792 ))
793 \f
794 ;;; Predefined rule based menu filters
795 ;;
796 (defun recentf-build-mode-rules ()
797 "Convert `auto-mode-alist' to menu filter rules.
798 Rules obey `recentf-arrange-rules' format."
799 (let ((case-fold-search recentf-case-fold-search)
800 regexp rule-name rule rules)
801 (dolist (mode auto-mode-alist)
802 (setq regexp (car mode)
803 mode (cdr mode))
804 (when (symbolp mode)
805 (setq rule-name (symbol-name mode))
806 (if (string-match "\\(.*\\)-mode$" rule-name)
807 (setq rule-name (match-string 1 rule-name)))
808 (setq rule-name (concat rule-name " (%d)")
809 rule (assoc rule-name rules))
810 (if rule
811 (setcdr rule (cons regexp (cdr rule)))
812 (push (list rule-name regexp) rules))))
813 ;; It is important to preserve auto-mode-alist order
814 ;; to ensure the right file <-> mode association
815 (nreverse rules)))
816
817 (defun recentf-arrange-by-mode (l)
818 "Split the list of menu-elements L into sub-menus by major mode."
819 (let ((recentf-arrange-rules (recentf-build-mode-rules))
820 (recentf-arrange-by-rule-others "others (%d)"))
821 (recentf-arrange-by-rule l)))
822
823 (defun recentf-build-dir-rules (l)
824 "Convert directories in menu-elements L to menu filter rules.
825 Rules obey `recentf-arrange-rules' format."
826 (let (dirs)
827 (mapcar #'(lambda (e)
828 (let ((dir (file-name-directory
829 (recentf-menu-element-value e))))
830 (or (recentf-string-member dir dirs)
831 (push dir dirs))))
832 l)
833 (mapcar #'(lambda (d)
834 (cons (concat d " (%d)")
835 (concat "\\`" d)))
836 (nreverse (sort dirs 'recentf-string-lessp)))))
837
838 (defun recentf-file-name-nondir (l)
839 "Filter the list of menu-elements L to show filenames sans directory.
840 This simplified version of `recentf-show-basenames' does not handle
841 duplicates. It is used by `recentf-arrange-by-dir' as its
842 `recentf-arrange-by-rule-subfilter'."
843 (mapcar #'(lambda (e)
844 (recentf-make-menu-element
845 (file-name-nondirectory (recentf-menu-element-value e))
846 (recentf-menu-element-value e)))
847 l))
848
849 (defun recentf-arrange-by-dir (l)
850 "Split the list of menu-elements L into sub-menus by directory."
851 (let ((recentf-arrange-rules (recentf-build-dir-rules l))
852 (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
853 recentf-arrange-by-rule-others)
854 (nreverse (recentf-arrange-by-rule l))))
855 \f
856 ;;; Ring of menu filters
857 ;;
858 (defvar recentf-filter-changer-state nil
859 "Used by `recentf-filter-changer' to hold its state.")
860
861 (defcustom recentf-filter-changer-alist
862 '(
863 (recentf-arrange-by-mode . "*Files by Mode*")
864 (recentf-arrange-by-dir . "*Files by Directory*")
865 (recentf-arrange-by-rule . "*Files by User Rule*")
866 )
867 "*List of filters managed by `recentf-filter-changer'.
868 Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is
869 the filter function, and LABEL is the menu item displayed to select
870 that filter."
871 :group 'recentf-filters
872 :type '(repeat (cons function string))
873 :set (lambda (variable value)
874 (setq recentf-filter-changer-state nil)
875 (recentf-menu-customization-changed variable value)))
876
877 (defun recentf-filter-changer-goto-next ()
878 "Go to the next filter available.
879 See `recentf-filter-changer'."
880 (setq recentf-filter-changer-state (cdr recentf-filter-changer-state))
881 (recentf-clear-data))
882
883 (defsubst recentf-filter-changer-get-current ()
884 "Get the current filter available.
885 See `recentf-filter-changer'."
886 (unless recentf-filter-changer-state
887 (setq recentf-filter-changer-state recentf-filter-changer-alist))
888 (car recentf-filter-changer-state))
889
890 (defsubst recentf-filter-changer-get-next ()
891 "Get the next filter available.
892 See `recentf-filter-changer'."
893 ;; At this point the current filter is the first element of
894 ;; `recentf-filter-changer-state'.
895 (car (or (cdr recentf-filter-changer-state)
896 ;; There is no next element in
897 ;; `recentf-filter-changer-state', so loop back to the
898 ;; first element of `recentf-filter-changer-alist'.
899 recentf-filter-changer-alist)))
900
901 (defun recentf-filter-changer (l)
902 "Manage a ring of menu filters.
903 `recentf-filter-changer-alist' defines the filters in the ring.
904 Filtering of L is delegated to the current filter in the ring. A
905 filter menu item is displayed allowing to dynamically activate the
906 next filter in the ring. If the filter ring is empty, L is left
907 unchanged."
908 (let ((filter (recentf-filter-changer-get-current)))
909 (when filter
910 (setq l (recentf-apply-menu-filter (car filter) l)
911 filter (recentf-filter-changer-get-next))
912 (when filter
913 (setq recentf-menu-filter-commands
914 (list (vector (cdr filter)
915 '(recentf-filter-changer-goto-next)
916 t)))))
917 l))
918 \f
919 ;;; Common dialog stuff
920 ;;
921 (defun recentf-cancel-dialog (&rest ignore)
922 "Cancel the current dialog.
923 IGNORE arguments."
924 (interactive)
925 (kill-buffer (current-buffer))
926 (message "Dialog canceled"))
927
928 (defun recentf-dialog-goto-first (widget-type)
929 "Move the cursor to the first WIDGET-TYPE in current dialog.
930 Go to the beginning of buffer if not found."
931 (goto-char (point-min))
932 (condition-case nil
933 (let (done)
934 (widget-move 1)
935 (while (not done)
936 (if (eq widget-type (widget-type (widget-at (point))))
937 (setq done t)
938 (widget-move 1))))
939 (goto-char (point-min))))
940
941 (defvar recentf-dialog-mode-map
942 (let ((km (make-sparse-keymap)))
943 (set-keymap-parent km widget-keymap)
944 (define-key km "q" 'recentf-cancel-dialog)
945 (define-key km [down-mouse-1] 'widget-button-click)
946 ;; Keys in reverse order of appearence in help.
947 (dolist (k '("0" "9" "8" "7" "6" "5" "4" "3" "2" "1"))
948 (define-key km k 'recentf-open-file-with-key))
949 km)
950 "Keymap used in recentf dialogs.")
951
952 (define-derived-mode recentf-dialog-mode nil "recentf-dialog"
953 "Major mode of recentf dialogs.
954
955 \\{recentf-dialog-mode-map}"
956 :syntax-table nil
957 :abbrev-table nil
958 (setq truncate-lines t))
959
960 (defmacro recentf-dialog (name &rest forms)
961 "Show a dialog buffer with NAME, setup with FORMS."
962 (declare (indent 1) (debug t))
963 `(with-current-buffer (get-buffer-create ,name)
964 ;; Cleanup buffer
965 (let ((inhibit-read-only t)
966 (ol (overlay-lists)))
967 (mapc 'delete-overlay (car ol))
968 (mapc 'delete-overlay (cdr ol))
969 (erase-buffer))
970 (recentf-dialog-mode)
971 ,@forms
972 (widget-setup)
973 (switch-to-buffer (current-buffer))))
974 \f
975 ;;; Hooks
976 ;;
977 (defun recentf-track-opened-file ()
978 "Insert the name of the file just opened or written into the recent list."
979 (and buffer-file-name
980 (recentf-add-file buffer-file-name))
981 ;; Must return nil because it is run from `write-file-functions'.
982 nil)
983
984 (defun recentf-track-closed-file ()
985 "Update the recent list when a buffer is killed.
986 That is, remove a non kept file from the recent list."
987 (and buffer-file-name
988 (recentf-remove-if-non-kept buffer-file-name)))
989
990 (defun recentf-update-menu ()
991 "Update the recentf menu from the current recent list."
992 (let ((cache (cons default-directory recentf-list)))
993 ;; Does nothing, if nothing has changed.
994 (unless (equal recentf-data-cache cache)
995 (setq recentf-data-cache cache)
996 (condition-case err
997 (easy-menu-add-item
998 (recentf-menu-bar) recentf-menu-path
999 (easy-menu-create-menu recentf-menu-title
1000 (recentf-make-menu-items))
1001 recentf-menu-before)
1002 (error
1003 (message "recentf update menu failed: %s"
1004 (error-message-string err)))))))
1005
1006 (defconst recentf-used-hooks
1007 '(
1008 (find-file-hook recentf-track-opened-file)
1009 (write-file-functions recentf-track-opened-file)
1010 (kill-buffer-hook recentf-track-closed-file)
1011 (menu-bar-update-hook recentf-update-menu)
1012 (kill-emacs-hook recentf-save-list)
1013 )
1014 "Hooks used by recentf.")
1015
1016 (defsubst recentf-enabled-p ()
1017 "Return non-nil if recentf mode is currently enabled."
1018 (memq 'recentf-update-menu menu-bar-update-hook))
1019 \f
1020 ;;; Commands
1021 ;;
1022
1023 ;;; Edit list dialog
1024 ;;
1025 (defvar recentf-edit-list nil)
1026
1027 (defun recentf-edit-list-select (widget &rest ignore)
1028 "Toggle a file selection based on the checkbox WIDGET state.
1029 IGNORE other arguments."
1030 (let ((value (widget-get widget :tag))
1031 (check (widget-value widget)))
1032 (if check
1033 (add-to-list 'recentf-edit-list value)
1034 (setq recentf-edit-list (delq value recentf-edit-list)))
1035 (message "%s %sselected" value (if check "" "un"))))
1036
1037 (defun recentf-edit-list-validate (&rest ignore)
1038 "Process the recent list when the edit list dialog is committed.
1039 IGNORE arguments."
1040 (if recentf-edit-list
1041 (let ((i 0))
1042 (dolist (e recentf-edit-list)
1043 (setq recentf-list (delq e recentf-list)
1044 i (1+ i)))
1045 (kill-buffer (current-buffer))
1046 (message "%S file(s) removed from the list" i)
1047 (recentf-clear-data))
1048 (message "No file selected")))
1049
1050 (defun recentf-edit-list ()
1051 "Show a dialog to delete selected files from the recent list."
1052 (interactive)
1053 (recentf-dialog (format "*%s - Edit list*" recentf-menu-title)
1054 (set (make-local-variable 'recentf-edit-list) nil)
1055 (widget-insert
1056 "Click on OK to delete selected files from the recent list.
1057 Click on Cancel or type `q' to cancel.\n")
1058 ;; Insert the list of files as checkboxes
1059 (dolist (item recentf-list)
1060 (widget-create 'checkbox
1061 :value nil ; unselected checkbox
1062 :format "\n %[%v%] %t"
1063 :tag item
1064 :notify 'recentf-edit-list-select))
1065 (widget-insert "\n\n")
1066 (widget-create
1067 'push-button
1068 :notify 'recentf-edit-list-validate
1069 :help-echo "Delete selected files from the recent list"
1070 "Ok")
1071 (widget-insert " ")
1072 (widget-create
1073 'push-button
1074 :notify 'recentf-cancel-dialog
1075 "Cancel")
1076 (recentf-dialog-goto-first 'checkbox)))
1077
1078 ;;; Open file dialog
1079 ;;
1080 (defun recentf-open-files-action (widget &rest ignore)
1081 "Open the file stored in WIDGET's value when notified.
1082 IGNORE other arguments."
1083 (kill-buffer (current-buffer))
1084 (funcall recentf-menu-action (widget-value widget)))
1085
1086 ;; List of files associated to a digit shortcut key.
1087 (defvar recentf--files-with-key nil)
1088
1089 (defun recentf-show-digit-shortcut-filter (l)
1090 "Filter the list of menu-elements L to show digit shortcuts."
1091 (let ((i 0))
1092 (dolist (e l)
1093 (setq i (1+ i))
1094 (recentf-set-menu-element-item
1095 e (format "[%d] %s" (% i 10) (recentf-menu-element-item e))))
1096 l))
1097
1098 (defun recentf-open-files-item (menu-element)
1099 "Return a widget to display MENU-ELEMENT in a dialog buffer."
1100 (if (consp (cdr menu-element))
1101 ;; Represent a sub-menu with a tree widget
1102 `(tree-widget
1103 :open t
1104 :match ignore
1105 :node (item :tag ,(car menu-element)
1106 :sample-face bold
1107 :format "%{%t%}:\n")
1108 ,@(mapcar 'recentf-open-files-item
1109 (cdr menu-element)))
1110 ;; Represent a single file with a link widget
1111 `(link :tag ,(car menu-element)
1112 :button-prefix ""
1113 :button-suffix ""
1114 :button-face default
1115 :format "%[%t%]\n"
1116 :help-echo ,(concat "Open " (cdr menu-element))
1117 :action recentf-open-files-action
1118 ,(cdr menu-element))))
1119
1120 (defun recentf-open-files-items (files)
1121 "Return a list of widgets to display FILES in a dialog buffer."
1122 (set (make-local-variable 'recentf--files-with-key)
1123 (recentf-trunc-list files 10))
1124 (mapcar 'recentf-open-files-item
1125 (append
1126 ;; When requested group the files with shortcuts together
1127 ;; at the top of the list.
1128 (when recentf-show-file-shortcuts-flag
1129 (setq files (nthcdr 10 files))
1130 (recentf-apply-menu-filter
1131 'recentf-show-digit-shortcut-filter
1132 (mapcar 'recentf-make-default-menu-element
1133 recentf--files-with-key)))
1134 ;; Then the other files.
1135 (recentf-apply-menu-filter
1136 recentf-menu-filter
1137 (mapcar 'recentf-make-default-menu-element
1138 files)))))
1139
1140 (defun recentf-open-files (&optional files buffer-name)
1141 "Show a dialog to open a recent file.
1142 If optional argument FILES is non-nil, it is a list of recently-opened
1143 files to choose from. It defaults to the whole recent list.
1144 If optional argument BUFFER-NAME is non-nil, it is a buffer name to
1145 use for the dialog. It defaults to \"*`recentf-menu-title'*\"."
1146 (interactive)
1147 (recentf-dialog (or buffer-name (format "*%s*" recentf-menu-title))
1148 (widget-insert "Click on a file"
1149 (if recentf-show-file-shortcuts-flag
1150 ", or type the corresponding digit key,"
1151 "")
1152 " to open it.\n"
1153 "Click on Cancel or type `q' to cancel.\n")
1154 ;; Use a L&F that looks like the recentf menu.
1155 (tree-widget-set-theme "folder")
1156 (apply 'widget-create
1157 `(group
1158 :indent 2
1159 :format "\n%v\n"
1160 ,@(recentf-open-files-items (or files recentf-list))))
1161 (widget-create
1162 'push-button
1163 :notify 'recentf-cancel-dialog
1164 "Cancel")
1165 (recentf-dialog-goto-first 'link)))
1166
1167 (defun recentf-open-file-with-key (n)
1168 "Open the recent file with the shortcut numeric key N.
1169 N must be a valid digit.
1170 `1' opens the first file, `2' the second file, ... `9' the ninth file.
1171 `0' opens the tenth file."
1172 (interactive
1173 (list
1174 (let ((n (string-to-number (this-command-keys))))
1175 (cond
1176 ((zerop n) 10)
1177 ((and (> n 0) (< n 10)) n)
1178 ((error "Invalid digit key %d" n))))))
1179 (when recentf--files-with-key
1180 (let ((file (nth (1- n) recentf--files-with-key)))
1181 (unless file (error "Not that many recent files"))
1182 (kill-buffer (current-buffer))
1183 (funcall recentf-menu-action file))))
1184
1185 (defun recentf-open-more-files ()
1186 "Show a dialog to open a recent file that is not in the menu."
1187 (interactive)
1188 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
1189 (format "*%s - More*" recentf-menu-title)))
1190
1191 ;;; Save/load/cleanup the recent list
1192 ;;
1193 (defconst recentf-save-file-header
1194 ";;; Automatically generated by `recentf' on %s.\n"
1195 "Header to be written into the `recentf-save-file'.")
1196
1197 (defconst recentf-save-file-coding-system
1198 (if (coding-system-p 'utf-8-emacs)
1199 'utf-8-emacs
1200 'emacs-mule)
1201 "Coding system of the file `recentf-save-file'.")
1202
1203 (defun recentf-save-list ()
1204 "Save the recent list.
1205 Write data into the file specified by `recentf-save-file'."
1206 (interactive)
1207 (condition-case error
1208 (with-temp-buffer
1209 (erase-buffer)
1210 (set-buffer-file-coding-system recentf-save-file-coding-system)
1211 (insert (format recentf-save-file-header (current-time-string)))
1212 (recentf-dump-variable 'recentf-list recentf-max-saved-items)
1213 (recentf-dump-variable 'recentf-filter-changer-state)
1214 (insert "\n\f\n;;; Local Variables:\n"
1215 (format ";;; coding: %s\n" recentf-save-file-coding-system)
1216 ";;; End:\n")
1217 (write-file (expand-file-name recentf-save-file))
1218 (when recentf-save-file-modes
1219 (set-file-modes recentf-save-file recentf-save-file-modes))
1220 nil)
1221 (error
1222 (warn "recentf mode: %s" (error-message-string error)))))
1223
1224 (defun recentf-load-list ()
1225 "Load a previously saved recent list.
1226 Read data from the file specified by `recentf-save-file'.
1227 When `recentf-initialize-file-name-history' is non-nil, initialize an
1228 empty `file-name-history' with the recent list."
1229 (interactive)
1230 (let ((file (expand-file-name recentf-save-file)))
1231 (when (file-readable-p file)
1232 (load-file file)
1233 (and recentf-initialize-file-name-history
1234 (not file-name-history)
1235 (setq file-name-history (mapcar 'abbreviate-file-name
1236 recentf-list))))))
1237
1238 (defun recentf-cleanup ()
1239 "Remove all non-kept and excluded files from the recent list."
1240 (interactive)
1241 (message "Cleaning up the recentf list...")
1242 (let ((n 0) newlist)
1243 (dolist (f recentf-list)
1244 (if (and (recentf-include-p f)
1245 (recentf-keep-p f))
1246 (push f newlist)
1247 (setq n (1+ n))
1248 (message "File %s removed from the recentf list" f)))
1249 (message "Cleaning up the recentf list...done (%d removed)" n)
1250 (setq recentf-list (nreverse newlist))))
1251
1252 ;;;###autoload
1253 (define-minor-mode recentf-mode
1254 "Toggle recentf mode.
1255 With prefix argument ARG, turn on if positive, otherwise off.
1256 Returns non-nil if the new state is enabled.
1257
1258 When recentf mode is enabled, it maintains a menu for visiting files
1259 that were operated on recently."
1260 :global t
1261 :group 'recentf
1262 (unless (and recentf-mode (recentf-enabled-p))
1263 (if recentf-mode
1264 (recentf-load-list)
1265 (recentf-save-list))
1266 (recentf-auto-cleanup)
1267 (recentf-clear-data)
1268 (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
1269 (dolist (hook recentf-used-hooks)
1270 (apply hook-setup hook)))
1271 (run-hooks 'recentf-mode-hook)
1272 (when (interactive-p)
1273 (message "Recentf mode %sabled" (if recentf-mode "en" "dis"))))
1274 recentf-mode)
1275
1276 (provide 'recentf)
1277
1278 (run-hooks 'recentf-load-hook)
1279 \f
1280 ;; arch-tag: 78f1eec9-0d16-4d19-a4eb-2e4529edb62a
1281 ;;; recentf.el ends here