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