Spelling fixes.
[emacs.git] / lisp / recentf.el
blob2dac870afd5637529a0fbee797e5f301b9466118
1 ;;; recentf.el --- setup a menu of recently opened files
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Created: July 19 1999
7 ;; Keywords: files
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This package maintains a menu for visiting files that were operated
27 ;; on recently. When enabled a new "Open Recent" sub menu is
28 ;; displayed in the "File" menu. The recent files list is
29 ;; automatically saved across Emacs sessions. You can customize the
30 ;; number of recent files displayed, the location of the menu and
31 ;; others options (see the source code for details).
33 ;; To enable this package, add the following to your .emacs:
34 ;; (recentf-mode 1)
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 (convert-standard-filename "~/.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 (define-obsolete-variable-alias 'recentf-menu-append-commands-p
231 'recentf-menu-append-commands-flag
232 "22.1")
234 (defcustom recentf-menu-append-commands-flag t
235 "Non-nil means to append command items to the menu."
236 :group 'recentf
237 :type 'boolean)
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 '(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 ;; If there was an error in a predicate, err on the side of
415 ;; keeping the file. (Bug#5843)
416 (setq keepit (not (ignore-errors
417 (if (stringp (car checks))
418 ;; A regexp
419 (string-match (car checks) filename)
420 ;; A predicate
421 (funcall (car checks) filename))))
422 checks (cdr checks)))
423 keepit))
425 (defun recentf-keep-p (filename)
426 "Return non-nil if FILENAME should be kept in the recent list.
427 That is, if it matches any of the `recentf-keep' checks."
428 (let* ((case-fold-search recentf-case-fold-search)
429 (checks recentf-keep)
430 (keepit (null checks)))
431 (while (and checks (not keepit))
432 (setq keepit (condition-case nil
433 (if (stringp (car checks))
434 ;; A regexp
435 (string-match (car checks) filename)
436 ;; A predicate
437 (funcall (car checks) filename))
438 (error nil))
439 checks (cdr checks)))
440 keepit))
442 (defsubst recentf-add-file (filename)
443 "Add or move FILENAME at the beginning of the recent list.
444 Does nothing if the name satisfies any of the `recentf-exclude'
445 regexps or predicates."
446 (setq filename (recentf-expand-file-name filename))
447 (when (recentf-include-p filename)
448 (recentf-push filename)))
450 (defsubst recentf-remove-if-non-kept (filename)
451 "Remove FILENAME from the recent list, if file is not kept.
452 Return non-nil if FILENAME has been removed."
453 (unless (recentf-keep-p filename)
454 (let ((m (recentf-string-member
455 (recentf-expand-file-name filename) recentf-list)))
456 (and m (setq recentf-list (delq (car m) recentf-list))))))
458 (defsubst recentf-directory-compare (f1 f2)
459 "Compare absolute filenames F1 and F2.
460 First compare directories, then filenames sans directory.
461 Return non-nil if F1 is less than F2."
462 (let ((d1 (file-name-directory f1))
463 (d2 (file-name-directory f2)))
464 (if (recentf-string-equal d1 d2)
465 (recentf-string-lessp (file-name-nondirectory f1)
466 (file-name-nondirectory f2))
467 (recentf-string-lessp d1 d2))))
469 ;;; Menu building
471 (defsubst recentf-digit-shortcut-command-name (n)
472 "Return a command name to open the Nth most recent file.
473 See also the command `recentf-open-most-recent-file'."
474 (intern (format "recentf-open-most-recent-file-%d" n)))
476 (defvar recentf--shortcuts-keymap
477 (let ((km (make-sparse-keymap)))
478 (dolist (k '(0 9 8 7 6 5 4 3 2 1))
479 (let ((cmd (recentf-digit-shortcut-command-name k)))
480 ;; Define a shortcut command.
481 (defalias cmd
482 `(lambda ()
483 (interactive)
484 (recentf-open-most-recent-file ,k)))
485 ;; Bind it to a digit key.
486 (define-key km (vector (+ k ?0)) cmd)))
488 "Digit shortcuts keymap.")
490 (defvar recentf-menu-items-for-commands
491 (list
492 ["Cleanup list"
493 recentf-cleanup
494 :help "Remove duplicates, and obsoletes files from the recent list"
495 :active t]
496 ["Edit list..."
497 recentf-edit-list
498 :help "Manually remove files from the recent list"
499 :active t]
500 ["Save list now"
501 recentf-save-list
502 :help "Save the list of recently opened files now"
503 :active t]
504 ["Options..."
505 (customize-group "recentf")
506 :help "Customize recently opened files menu and options"
507 :active t]
509 "List of menu items for recentf commands.")
511 (defvar recentf-menu-filter-commands nil
512 "This variable can be used by menu filters to setup their own command menu.
513 If non-nil it must contain a list of valid menu-items to be appended
514 to the recent file list part of the menu. Before calling a menu
515 filter function this variable is reset to nil.")
517 (defsubst recentf-elements (n)
518 "Return a list of the first N elements of the recent list."
519 (recentf-trunc-list recentf-list n))
521 (defsubst recentf-make-menu-element (menu-item menu-value)
522 "Create a new menu-element.
523 A menu element is a pair (MENU-ITEM . MENU-VALUE), where MENU-ITEM is
524 the menu item string displayed. MENU-VALUE is the file to be open
525 when the corresponding MENU-ITEM is selected. Or it is a
526 pair (SUB-MENU-TITLE . MENU-ELEMENTS) where SUB-MENU-TITLE is a
527 sub-menu title and MENU-ELEMENTS is the list of menu elements in the
528 sub-menu."
529 (cons menu-item menu-value))
531 (defsubst recentf-menu-element-item (e)
532 "Return the item part of the menu-element E."
533 (car e))
535 (defsubst recentf-menu-element-value (e)
536 "Return the value part of the menu-element E."
537 (cdr e))
539 (defsubst recentf-set-menu-element-item (e item)
540 "Change the item part of menu-element E to ITEM."
541 (setcar e item))
543 (defsubst recentf-set-menu-element-value (e value)
544 "Change the value part of menu-element E to VALUE."
545 (setcdr e value))
547 (defsubst recentf-sub-menu-element-p (e)
548 "Return non-nil if menu-element E defines a sub-menu."
549 (consp (recentf-menu-element-value e)))
551 (defsubst recentf-make-default-menu-element (file)
552 "Make a new default menu element with FILE.
553 This a menu element (FILE . FILE)."
554 (recentf-make-menu-element file file))
556 (defsubst recentf-menu-elements (n)
557 "Return a list of the first N default menu elements from the recent list.
558 See also `recentf-make-default-menu-element'."
559 (mapcar 'recentf-make-default-menu-element
560 (recentf-elements n)))
562 (defun recentf-apply-menu-filter (filter l)
563 "Apply function FILTER to the list of menu-elements L.
564 It takes care of sub-menu elements in L and recursively apply FILTER
565 to them. It is guaranteed that FILTER receives only a list of single
566 menu-elements (no sub-menu)."
567 (if (and l (functionp filter))
568 (let ((case-fold-search recentf-case-fold-search)
569 elts others)
570 ;; split L into two sub-lists, one of sub-menus elements and
571 ;; another of single menu elements.
572 (dolist (elt l)
573 (if (recentf-sub-menu-element-p elt)
574 (push elt elts)
575 (push elt others)))
576 ;; Apply FILTER to single elements.
577 (when others
578 (setq others (funcall filter (nreverse others))))
579 ;; Apply FILTER to sub-menu elements.
580 (setq l nil)
581 (dolist (elt elts)
582 (recentf-set-menu-element-value
583 elt (recentf-apply-menu-filter
584 filter (recentf-menu-element-value elt)))
585 (push elt l))
586 ;; Return the new filtered menu element list.
587 (nconc l others))
590 ;; Count the number of assigned menu shortcuts.
591 (defvar recentf-menu-shortcuts)
593 (defun recentf-make-menu-items (&optional _menu)
594 "Make menu items from the recent list.
595 This is a menu filter function which ignores the MENU argument."
596 (setq recentf-menu-filter-commands nil)
597 (let* ((recentf-menu-shortcuts 0)
598 (file-items
599 (condition-case err
600 (mapcar 'recentf-make-menu-item
601 (recentf-apply-menu-filter
602 recentf-menu-filter
603 (recentf-menu-elements recentf-max-menu-items)))
604 (error
605 (message "recentf update menu failed: %s"
606 (error-message-string err))))))
607 (append
608 (or file-items
609 '(["No files" t
610 :help "No recent file to open"
611 :active nil]))
612 (if recentf-menu-open-all-flag
613 '(["All..." recentf-open-files
614 :help "Open recent files through a dialog"
615 :active t])
616 (and (< recentf-max-menu-items (length recentf-list))
617 '(["More..." recentf-open-more-files
618 :help "Open files not in the menu through a dialog"
619 :active t])))
620 (and recentf-menu-filter-commands '("---"))
621 recentf-menu-filter-commands
622 (and recentf-menu-items-for-commands '("---"))
623 recentf-menu-items-for-commands)))
625 (defun recentf-menu-value-shortcut (name)
626 "Return a shortcut digit for file NAME.
627 Return nil if file NAME is not one of the ten more recent."
628 (let ((i 0) k)
629 (while (and (not k) (< i 10))
630 (if (string-equal name (nth i recentf-list))
631 (progn
632 (setq recentf-menu-shortcuts (1+ recentf-menu-shortcuts))
633 (setq k (% (1+ i) 10)))
634 (setq i (1+ i))))
637 (defun recentf-make-menu-item (elt)
638 "Make a menu item from menu element ELT."
639 (let ((item (recentf-menu-element-item elt))
640 (value (recentf-menu-element-value elt)))
641 (if (recentf-sub-menu-element-p elt)
642 (cons item (mapcar 'recentf-make-menu-item value))
643 (let ((k (and (< recentf-menu-shortcuts 10)
644 (recentf-menu-value-shortcut value))))
645 (vector item
646 ;; If the file name is one of the ten more recent, use
647 ;; a digit shortcut command to open it, else use an
648 ;; anonymous command.
649 (if k
650 (recentf-digit-shortcut-command-name k)
651 `(lambda ()
652 (interactive)
653 (,recentf-menu-action ,value)))
654 :help (concat "Open " value)
655 :active t)))))
657 (defsubst recentf-menu-bar ()
658 "Return the keymap of the global menu bar."
659 (lookup-key global-map [menu-bar]))
661 (defun recentf-show-menu ()
662 "Show the menu of recently opened files."
663 (easy-menu-add-item
664 (recentf-menu-bar) recentf-menu-path
665 (list recentf-menu-title :filter 'recentf-make-menu-items)
666 recentf-menu-before))
668 (defun recentf-hide-menu ()
669 "Hide the menu of recently opened files."
670 (easy-menu-remove-item (recentf-menu-bar) recentf-menu-path
671 recentf-menu-title))
673 ;;; Predefined menu filters
675 (defsubst recentf-sort-ascending (l)
676 "Sort the list of menu elements L in ascending order.
677 The MENU-ITEM part of each menu element is compared."
678 (sort (copy-sequence l)
679 #'(lambda (e1 e2)
680 (recentf-string-lessp
681 (recentf-menu-element-item e1)
682 (recentf-menu-element-item e2)))))
684 (defsubst recentf-sort-descending (l)
685 "Sort the list of menu elements L in descending order.
686 The MENU-ITEM part of each menu element is compared."
687 (sort (copy-sequence l)
688 #'(lambda (e1 e2)
689 (recentf-string-lessp
690 (recentf-menu-element-item e2)
691 (recentf-menu-element-item e1)))))
693 (defsubst recentf-sort-basenames-ascending (l)
694 "Sort the list of menu elements L in ascending order.
695 Only filenames sans directory are compared."
696 (sort (copy-sequence l)
697 #'(lambda (e1 e2)
698 (recentf-string-lessp
699 (file-name-nondirectory (recentf-menu-element-value e1))
700 (file-name-nondirectory (recentf-menu-element-value e2))))))
702 (defsubst recentf-sort-basenames-descending (l)
703 "Sort the list of menu elements L in descending order.
704 Only filenames sans directory are compared."
705 (sort (copy-sequence l)
706 #'(lambda (e1 e2)
707 (recentf-string-lessp
708 (file-name-nondirectory (recentf-menu-element-value e2))
709 (file-name-nondirectory (recentf-menu-element-value e1))))))
711 (defsubst recentf-sort-directories-ascending (l)
712 "Sort the list of menu elements L in ascending order.
713 Compares directories then filenames to order the list."
714 (sort (copy-sequence l)
715 #'(lambda (e1 e2)
716 (recentf-directory-compare
717 (recentf-menu-element-value e1)
718 (recentf-menu-element-value e2)))))
720 (defsubst recentf-sort-directories-descending (l)
721 "Sort the list of menu elements L in descending order.
722 Compares directories then filenames to order the list."
723 (sort (copy-sequence l)
724 #'(lambda (e1 e2)
725 (recentf-directory-compare
726 (recentf-menu-element-value e2)
727 (recentf-menu-element-value e1)))))
729 (defun recentf-show-basenames (l &optional no-dir)
730 "Filter the list of menu elements L to show filenames sans directory.
731 When a filename is duplicated, it is appended a sequence number if
732 optional argument NO-DIR is non-nil, or its directory otherwise."
733 (let (filtered-names filtered-list full name counters sufx)
734 (dolist (elt l (nreverse filtered-list))
735 (setq full (recentf-menu-element-value elt)
736 name (file-name-nondirectory full))
737 (if (not (member name filtered-names))
738 (push name filtered-names)
739 (if no-dir
740 (if (setq sufx (assoc name counters))
741 (setcdr sufx (1+ (cdr sufx)))
742 (setq sufx 1)
743 (push (cons name sufx) counters))
744 (setq sufx (file-name-directory full)))
745 (setq name (format "%s(%s)" name sufx)))
746 (push (recentf-make-menu-element name full) filtered-list))))
748 (defsubst recentf-show-basenames-ascending (l)
749 "Filter the list of menu elements L to show filenames sans directory.
750 Filenames are sorted in ascending order.
751 This filter combines the `recentf-sort-basenames-ascending' and
752 `recentf-show-basenames' filters."
753 (recentf-show-basenames (recentf-sort-basenames-ascending l)))
755 (defsubst recentf-show-basenames-descending (l)
756 "Filter the list of menu elements L to show filenames sans directory.
757 Filenames are sorted in descending order.
758 This filter combines the `recentf-sort-basenames-descending' and
759 `recentf-show-basenames' filters."
760 (recentf-show-basenames (recentf-sort-basenames-descending l)))
762 (defun recentf-relative-filter (l)
763 "Filter the list of menu-elements L to show relative filenames.
764 Filenames are relative to the `default-directory'."
765 (mapcar #'(lambda (menu-element)
766 (let* ((ful (recentf-menu-element-value menu-element))
767 (rel (file-relative-name ful default-directory)))
768 (if (string-match "^\\.\\." rel)
769 menu-element
770 (recentf-make-menu-element rel ful))))
773 ;;; Rule based menu filters
775 (defcustom recentf-arrange-rules
777 ("Elisp files (%d)" ".\\.el\\'")
778 ("Java files (%d)" ".\\.java\\'")
779 ("C/C++ files (%d)" "c\\(pp\\)?\\'")
781 "List of rules used by `recentf-arrange-by-rule' to build sub-menus.
782 A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
783 displayed title of the sub-menu where a '%d' `format' pattern is
784 replaced by the number of items in the sub-menu. MATCHER is a regexp
785 or a list of regexps. Items matching one of the regular expressions in
786 MATCHER are added to the corresponding sub-menu.
787 SUB-MENU-TITLE can be a function. It is passed every items that
788 matched the corresponding MATCHER, and it must return a
789 pair (SUB-MENU-TITLE . ITEM). SUB-MENU-TITLE is a computed sub-menu
790 title that can be another function. ITEM is the received item which
791 may have been modified to match another rule."
792 :group 'recentf-filters
793 :type '(repeat (cons (choice string function)
794 (repeat regexp))))
796 (defcustom recentf-arrange-by-rule-others "Other files (%d)"
797 "Title of the `recentf-arrange-by-rule' sub-menu.
798 This is for the menu where items that don't match any
799 `recentf-arrange-rules' are displayed. If nil these items are
800 displayed in the main recent files menu. A '%d' `format' pattern in
801 the title is replaced by the number of items in the sub-menu."
802 :group 'recentf-filters
803 :type '(choice (const :tag "Main menu" nil)
804 (string :tag "Title")))
806 (defcustom recentf-arrange-by-rules-min-items 0
807 "Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
808 If the number of items in a sub-menu is less than this value the
809 corresponding sub-menu items are displayed in the main recent files
810 menu or in the `recentf-arrange-by-rule-others' sub-menu if
811 defined."
812 :group 'recentf-filters
813 :type 'number)
815 (defcustom recentf-arrange-by-rule-subfilter nil
816 "Function called by a rule based filter to filter sub-menu elements.
817 A nil value means no filter. See also `recentf-menu-filter'.
818 You can't use another rule based filter here."
819 :group 'recentf-filters
820 :type '(choice (const nil) function)
821 :set (lambda (variable value)
822 (when (memq value '(recentf-arrange-by-rule
823 recentf-arrange-by-mode
824 recentf-arrange-by-dir))
825 (error "Recursive use of a rule based filter"))
826 (set-default variable value)))
828 (defun recentf-match-rule (file)
829 "Return the rule that match FILE."
830 (let ((rules recentf-arrange-rules)
831 match found)
832 (while (and (not found) rules)
833 (setq match (cdar rules))
834 (when (stringp match)
835 (setq match (list match)))
836 (while (and match (not (string-match (car match) file)))
837 (setq match (cdr match)))
838 (if match
839 (setq found (cons (caar rules) file))
840 (setq rules (cdr rules))))
841 found))
843 (defun recentf-arrange-by-rule (l)
844 "Filter the list of menu-elements L.
845 Arrange them in sub-menus following rules in `recentf-arrange-rules'."
846 (when recentf-arrange-rules
847 (let (menus others menu file min count)
848 ;; Put menu items into sub-menus as defined by rules.
849 (dolist (elt l)
850 (setq file (recentf-menu-element-value elt)
851 menu (recentf-match-rule file))
852 (while (functionp (car menu))
853 (setq menu (funcall (car menu) (cdr menu))))
854 (if (not (stringp (car menu)))
855 (push elt others)
856 (setq menu (or (assoc (car menu) menus)
857 (car (push (list (car menu)) menus))))
858 (recentf-set-menu-element-value
859 menu (cons elt (recentf-menu-element-value menu)))))
860 ;; Finalize each sub-menu:
861 ;; - truncate it depending on the value of
862 ;; `recentf-arrange-by-rules-min-items',
863 ;; - replace %d by the number of menu items,
864 ;; - apply `recentf-arrange-by-rule-subfilter' to menu items.
865 (setq min (if (natnump recentf-arrange-by-rules-min-items)
866 recentf-arrange-by-rules-min-items 0)
867 l nil)
868 (dolist (elt menus)
869 (setq menu (recentf-menu-element-value elt)
870 count (length menu))
871 (if (< count min)
872 (setq others (nconc menu others))
873 (recentf-set-menu-element-item
874 elt (format (recentf-menu-element-item elt) count))
875 (recentf-set-menu-element-value
876 elt (recentf-apply-menu-filter
877 recentf-arrange-by-rule-subfilter (nreverse menu)))
878 (push elt l)))
879 ;; Add the menu items remaining in the `others' bin.
880 (when (setq others (nreverse others))
881 (setq l (nconc
883 ;; Put items in an sub menu.
884 (if (stringp recentf-arrange-by-rule-others)
885 (list
886 (recentf-make-menu-element
887 (format recentf-arrange-by-rule-others
888 (length others))
889 (recentf-apply-menu-filter
890 recentf-arrange-by-rule-subfilter others)))
891 ;; Append items to the main menu.
892 (recentf-apply-menu-filter
893 recentf-arrange-by-rule-subfilter others)))))))
896 ;;; Predefined rule based menu filters
898 (defun recentf-indirect-mode-rule (file)
899 "Apply a second level `auto-mode-alist' regexp to FILE."
900 (recentf-match-rule (substring file 0 (match-beginning 0))))
902 (defun recentf-build-mode-rules ()
903 "Convert `auto-mode-alist' to menu filter rules.
904 Rules obey `recentf-arrange-rules' format."
905 (let ((case-fold-search recentf-case-fold-search)
906 regexp rule-name rule rules)
907 (dolist (mode auto-mode-alist)
908 (setq regexp (car mode)
909 mode (cdr mode))
910 (when mode
911 (cond
912 ;; Build a special "strip suffix" rule from entries of the
913 ;; form (REGEXP FUNCTION NON-NIL). Notice that FUNCTION is
914 ;; ignored by the menu filter. So in some corner cases a
915 ;; wrong mode could be guessed.
916 ((and (consp mode) (cadr mode))
917 (setq rule-name 'recentf-indirect-mode-rule))
918 ((and mode (symbolp mode))
919 (setq rule-name (symbol-name mode))
920 (if (string-match "\\(.*\\)-mode$" rule-name)
921 (setq rule-name (match-string 1 rule-name)))
922 (setq rule-name (concat rule-name " (%d)"))))
923 (setq rule (assoc rule-name rules))
924 (if rule
925 (setcdr rule (cons regexp (cdr rule)))
926 (push (list rule-name regexp) rules))))
927 ;; It is important to preserve auto-mode-alist order
928 ;; to ensure the right file <-> mode association
929 (nreverse rules)))
931 (defun recentf-arrange-by-mode (l)
932 "Split the list of menu-elements L into sub-menus by major mode."
933 (let ((recentf-arrange-rules (recentf-build-mode-rules))
934 (recentf-arrange-by-rule-others "others (%d)"))
935 (recentf-arrange-by-rule l)))
937 (defun recentf-file-name-nondir (l)
938 "Filter the list of menu-elements L to show filenames sans directory.
939 This simplified version of `recentf-show-basenames' does not handle
940 duplicates. It is used by `recentf-arrange-by-dir' as its
941 `recentf-arrange-by-rule-subfilter'."
942 (mapcar #'(lambda (e)
943 (recentf-make-menu-element
944 (file-name-nondirectory (recentf-menu-element-value e))
945 (recentf-menu-element-value e)))
948 (defun recentf-dir-rule (file)
949 "Return as a sub-menu, the directory FILE belongs to."
950 (cons (file-name-directory file) file))
952 (defun recentf-arrange-by-dir (l)
953 "Split the list of menu-elements L into sub-menus by directory."
954 (let ((recentf-arrange-rules '((recentf-dir-rule . ".*")))
955 (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
956 recentf-arrange-by-rule-others)
957 (recentf-arrange-by-rule l)))
959 ;;; Menu of menu filters
961 (defvar recentf-filter-changer-current nil
962 "Current filter used by `recentf-filter-changer'.")
964 (defcustom recentf-filter-changer-alist
966 (recentf-arrange-by-mode . "Grouped by Mode")
967 (recentf-arrange-by-dir . "Grouped by Directory")
968 (recentf-arrange-by-rule . "Grouped by Custom Rules")
970 "List of filters managed by `recentf-filter-changer'.
971 Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is
972 the filter function, and LABEL is the menu item displayed to select
973 that filter."
974 :group 'recentf-filters
975 :type '(repeat (cons function string))
976 :set (lambda (variable value)
977 (setq recentf-filter-changer-current nil)
978 (set-default variable value)))
980 (defun recentf-filter-changer-select (filter)
981 "Select FILTER as the current menu filter.
982 See `recentf-filter-changer'."
983 (setq recentf-filter-changer-current filter))
985 (defun recentf-filter-changer (l)
986 "Manage a sub-menu of menu filters.
987 `recentf-filter-changer-alist' defines the filters in the menu.
988 Filtering of L is delegated to the selected filter in the menu."
989 (unless recentf-filter-changer-current
990 (setq recentf-filter-changer-current
991 (caar recentf-filter-changer-alist)))
992 (if (not recentf-filter-changer-current)
994 (setq recentf-menu-filter-commands
995 (list
996 `("Show files"
997 ,@(mapcar
998 #'(lambda (f)
999 `[,(cdr f)
1000 (setq recentf-filter-changer-current ',(car f))
1001 ;;:active t
1002 :style radio ;;radio Don't work with GTK :-(
1003 :selected (eq recentf-filter-changer-current
1004 ',(car f))
1005 ;;:help ,(cdr f)
1007 recentf-filter-changer-alist))))
1008 (recentf-apply-menu-filter recentf-filter-changer-current l)))
1010 ;;; Hooks
1012 (defun recentf-track-opened-file ()
1013 "Insert the name of the file just opened or written into the recent list."
1014 (and buffer-file-name
1015 (recentf-add-file buffer-file-name))
1016 ;; Must return nil because it is run from `write-file-functions'.
1017 nil)
1019 (defun recentf-track-closed-file ()
1020 "Update the recent list when a buffer is killed.
1021 That is, remove a non kept file from the recent list."
1022 (and buffer-file-name
1023 (recentf-remove-if-non-kept buffer-file-name)))
1025 (defconst recentf-used-hooks
1027 (find-file-hook recentf-track-opened-file)
1028 (write-file-functions recentf-track-opened-file)
1029 (kill-buffer-hook recentf-track-closed-file)
1030 (kill-emacs-hook recentf-save-list)
1032 "Hooks used by recentf.")
1034 ;;; Commands
1037 ;;; Common dialog stuff
1039 (defun recentf-cancel-dialog (&rest _ignore)
1040 "Cancel the current dialog.
1041 IGNORE arguments."
1042 (interactive)
1043 (kill-buffer (current-buffer))
1044 (message "Dialog canceled"))
1046 (defun recentf-dialog-goto-first (widget-type)
1047 "Move the cursor to the first WIDGET-TYPE in current dialog.
1048 Go to the beginning of buffer if not found."
1049 (goto-char (point-min))
1050 (condition-case nil
1051 (let (done)
1052 (widget-move 1)
1053 (while (not done)
1054 (if (eq widget-type (widget-type (widget-at (point))))
1055 (setq done t)
1056 (widget-move 1))))
1057 (error
1058 (goto-char (point-min)))))
1060 (defvar recentf-dialog-mode-map
1061 (let ((km (copy-keymap recentf--shortcuts-keymap)))
1062 (set-keymap-parent km widget-keymap)
1063 (define-key km "q" 'recentf-cancel-dialog)
1064 (define-key km [follow-link] "\C-m")
1066 "Keymap used in recentf dialogs.")
1068 (define-derived-mode recentf-dialog-mode nil "recentf-dialog"
1069 "Major mode of recentf dialogs.
1071 \\{recentf-dialog-mode-map}"
1072 :syntax-table nil
1073 :abbrev-table nil
1074 (setq truncate-lines t))
1076 (defmacro recentf-dialog (name &rest forms)
1077 "Show a dialog buffer with NAME, setup with FORMS."
1078 (declare (indent 1) (debug t))
1079 `(with-current-buffer (get-buffer-create ,name)
1080 ;; Cleanup buffer
1081 (let ((inhibit-read-only t)
1082 (ol (overlay-lists)))
1083 (mapc 'delete-overlay (car ol))
1084 (mapc 'delete-overlay (cdr ol))
1085 (erase-buffer))
1086 (recentf-dialog-mode)
1087 ,@forms
1088 (widget-setup)
1089 (switch-to-buffer (current-buffer))))
1091 ;;; Edit list dialog
1093 (defvar recentf-edit-list nil)
1095 (defun recentf-edit-list-select (widget &rest _ignore)
1096 "Toggle a file selection based on the checkbox WIDGET state.
1097 IGNORE other arguments."
1098 (let ((value (widget-get widget :tag))
1099 (check (widget-value widget)))
1100 (if check
1101 (add-to-list 'recentf-edit-list value)
1102 (setq recentf-edit-list (delq value recentf-edit-list)))
1103 (message "%s %sselected" value (if check "" "un"))))
1105 (defun recentf-edit-list-validate (&rest _ignore)
1106 "Process the recent list when the edit list dialog is committed.
1107 IGNORE arguments."
1108 (if recentf-edit-list
1109 (let ((i 0))
1110 (dolist (e recentf-edit-list)
1111 (setq recentf-list (delq e recentf-list)
1112 i (1+ i)))
1113 (kill-buffer (current-buffer))
1114 (message "%S file(s) removed from the list" i))
1115 (message "No file selected")))
1117 (defun recentf-edit-list ()
1118 "Show a dialog to delete selected files from the recent list."
1119 (interactive)
1120 (unless recentf-list
1121 (error "The list of recent files is empty"))
1122 (recentf-dialog (format "*%s - Edit list*" recentf-menu-title)
1123 (set (make-local-variable 'recentf-edit-list) nil)
1124 (widget-insert
1125 "Click on OK to delete selected files from the recent list.
1126 Click on Cancel or type `q' to cancel.\n")
1127 ;; Insert the list of files as checkboxes
1128 (dolist (item recentf-list)
1129 (widget-create 'checkbox
1130 :value nil ; unselected checkbox
1131 :format "\n %[%v%] %t"
1132 :tag item
1133 :notify 'recentf-edit-list-select))
1134 (widget-insert "\n\n")
1135 (widget-create
1136 'push-button
1137 :notify 'recentf-edit-list-validate
1138 :help-echo "Delete selected files from the recent list"
1139 "Ok")
1140 (widget-insert " ")
1141 (widget-create
1142 'push-button
1143 :notify 'recentf-cancel-dialog
1144 "Cancel")
1145 (recentf-dialog-goto-first 'checkbox)))
1147 ;;; Open file dialog
1149 (defun recentf-open-files-action (widget &rest _ignore)
1150 "Open the file stored in WIDGET's value when notified.
1151 IGNORE other arguments."
1152 (kill-buffer (current-buffer))
1153 (funcall recentf-menu-action (widget-value widget)))
1155 ;; List of files associated to a digit shortcut key.
1156 (defvar recentf--files-with-key nil)
1158 (defun recentf-show-digit-shortcut-filter (l)
1159 "Filter the list of menu-elements L to show digit shortcuts."
1160 (let ((i 0))
1161 (dolist (e l)
1162 (setq i (1+ i))
1163 (recentf-set-menu-element-item
1164 e (format "[%d] %s" (% i 10) (recentf-menu-element-item e))))
1167 (defun recentf-open-files-item (menu-element)
1168 "Return a widget to display MENU-ELEMENT in a dialog buffer."
1169 (if (consp (cdr menu-element))
1170 ;; Represent a sub-menu with a tree widget
1171 `(tree-widget
1172 :open t
1173 :match ignore
1174 :node (item :tag ,(car menu-element)
1175 :sample-face bold
1176 :format "%{%t%}:\n")
1177 ,@(mapcar 'recentf-open-files-item
1178 (cdr menu-element)))
1179 ;; Represent a single file with a link widget
1180 `(link :tag ,(car menu-element)
1181 :button-prefix ""
1182 :button-suffix ""
1183 :button-face default
1184 :format "%[%t\n%]"
1185 :help-echo ,(concat "Open " (cdr menu-element))
1186 :action recentf-open-files-action
1187 ,(cdr menu-element))))
1189 (defun recentf-open-files-items (files)
1190 "Return a list of widgets to display FILES in a dialog buffer."
1191 (set (make-local-variable 'recentf--files-with-key)
1192 (recentf-trunc-list files 10))
1193 (mapcar 'recentf-open-files-item
1194 (append
1195 ;; When requested group the files with shortcuts together
1196 ;; at the top of the list.
1197 (when recentf-show-file-shortcuts-flag
1198 (setq files (nthcdr 10 files))
1199 (recentf-apply-menu-filter
1200 'recentf-show-digit-shortcut-filter
1201 (mapcar 'recentf-make-default-menu-element
1202 recentf--files-with-key)))
1203 ;; Then the other files.
1204 (recentf-apply-menu-filter
1205 recentf-menu-filter
1206 (mapcar 'recentf-make-default-menu-element
1207 files)))))
1209 (defun recentf-open-files (&optional files buffer-name)
1210 "Show a dialog to open a recent file.
1211 If optional argument FILES is non-nil, it is a list of recently-opened
1212 files to choose from. It defaults to the whole recent list.
1213 If optional argument BUFFER-NAME is non-nil, it is a buffer name to
1214 use for the dialog. It defaults to \"*`recentf-menu-title'*\"."
1215 (interactive)
1216 (unless (or files recentf-list)
1217 (error "There is no recent file to open"))
1218 (recentf-dialog (or buffer-name (format "*%s*" recentf-menu-title))
1219 (widget-insert "Click on a file"
1220 (if recentf-show-file-shortcuts-flag
1221 ", or type the corresponding digit key,"
1223 " to open it.\n"
1224 "Click on Cancel or type `q' to cancel.\n")
1225 ;; Use a L&F that looks like the recentf menu.
1226 (tree-widget-set-theme "folder")
1227 (apply 'widget-create
1228 `(group
1229 :indent 2
1230 :format "\n%v\n"
1231 ,@(recentf-open-files-items (or files recentf-list))))
1232 (widget-create
1233 'push-button
1234 :notify 'recentf-cancel-dialog
1235 "Cancel")
1236 (recentf-dialog-goto-first 'link)))
1238 (defun recentf-open-more-files ()
1239 "Show a dialog to open a recent file that is not in the menu."
1240 (interactive)
1241 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
1242 (format "*%s - More*" recentf-menu-title)))
1244 (defun recentf-open-most-recent-file (&optional n)
1245 "Open the Nth most recent file.
1246 Optional argument N must be a valid digit number. It defaults to 1.
1247 1 opens the most recent file, 2 the second most recent one, etc..
1248 0 opens the tenth most recent file."
1249 (interactive "p")
1250 (cond
1251 ((zerop n) (setq n 10))
1252 ((and (> n 0) (< n 10)))
1253 ((error "Recent file number out of range [0-9], %d" n)))
1254 (let ((file (nth (1- n) (or recentf--files-with-key recentf-list))))
1255 (unless file (error "Not that many recent files"))
1256 ;; Close the open files dialog.
1257 (when recentf--files-with-key
1258 (kill-buffer (current-buffer)))
1259 (funcall recentf-menu-action file)))
1261 ;;; Save/load/cleanup the recent list
1263 (defconst recentf-save-file-header
1264 ";;; Automatically generated by `recentf' on %s.\n"
1265 "Header to be written into the `recentf-save-file'.")
1267 (defconst recentf-save-file-coding-system
1268 (if (coding-system-p 'utf-8-emacs)
1269 'utf-8-emacs
1270 'emacs-mule)
1271 "Coding system of the file `recentf-save-file'.")
1273 (defun recentf-save-list ()
1274 "Save the recent list.
1275 Write data into the file specified by `recentf-save-file'."
1276 (interactive)
1277 (condition-case error
1278 (with-temp-buffer
1279 (erase-buffer)
1280 (set-buffer-file-coding-system recentf-save-file-coding-system)
1281 (insert (format recentf-save-file-header (current-time-string)))
1282 (recentf-dump-variable 'recentf-list recentf-max-saved-items)
1283 (recentf-dump-variable 'recentf-filter-changer-current)
1284 (insert "\n\f\n;; Local Variables:\n"
1285 (format ";; coding: %s\n" recentf-save-file-coding-system)
1286 ";; End:\n")
1287 (write-file (expand-file-name recentf-save-file))
1288 (when recentf-save-file-modes
1289 (set-file-modes recentf-save-file recentf-save-file-modes))
1290 nil)
1291 (error
1292 (warn "recentf mode: %s" (error-message-string error)))))
1294 (defun recentf-load-list ()
1295 "Load a previously saved recent list.
1296 Read data from the file specified by `recentf-save-file'.
1297 When `recentf-initialize-file-name-history' is non-nil, initialize an
1298 empty `file-name-history' with the recent list."
1299 (interactive)
1300 (let ((file (expand-file-name recentf-save-file)))
1301 (when (file-readable-p file)
1302 (load-file file)
1303 (and recentf-initialize-file-name-history
1304 (not file-name-history)
1305 (setq file-name-history (mapcar 'abbreviate-file-name
1306 recentf-list))))))
1308 (defun recentf-cleanup ()
1309 "Cleanup the recent list.
1310 That is, remove duplicates, non-kept, and excluded files."
1311 (interactive)
1312 (message "Cleaning up the recentf list...")
1313 (let ((n 0)
1314 (ht (make-hash-table
1315 :size recentf-max-saved-items
1316 :test 'equal))
1317 newlist key)
1318 (dolist (f recentf-list)
1319 (setq f (recentf-expand-file-name f)
1320 key (if recentf-case-fold-search (downcase f) f))
1321 (if (and (recentf-include-p f)
1322 (recentf-keep-p f)
1323 (not (gethash key ht)))
1324 (progn
1325 (push f newlist)
1326 (puthash key t ht))
1327 (setq n (1+ n))
1328 (message "File %s removed from the recentf list" f)))
1329 (message "Cleaning up the recentf list...done (%d removed)" n)
1330 (setq recentf-list (nreverse newlist))))
1332 ;;; The minor mode
1334 (defvar recentf-mode-map (make-sparse-keymap)
1335 "Keymap to use in recentf mode.")
1337 ;;;###autoload
1338 (define-minor-mode recentf-mode
1339 "Toggle \"Open Recent\" menu (Recentf mode).
1340 With a prefix argument ARG, enable Recentf mode if ARG is
1341 positive, and disable it otherwise. If called from Lisp, enable
1342 Recentf mode if ARG is omitted or nil.
1344 When Recentf mode is enabled, a \"Open Recent\" submenu is
1345 displayed in the \"File\" menu, containing a list of files that
1346 were operated on recently."
1347 :global t
1348 :group 'recentf
1349 :keymap recentf-mode-map
1350 (unless (and recentf-mode (recentf-enabled-p))
1351 (if recentf-mode
1352 (progn
1353 (recentf-load-list)
1354 (recentf-show-menu))
1355 (recentf-hide-menu)
1356 (recentf-save-list))
1357 (recentf-auto-cleanup)
1358 (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
1359 (dolist (hook recentf-used-hooks)
1360 (apply hook-setup hook)))))
1362 (defun recentf-unload-function ()
1363 "Unload the recentf library."
1364 (recentf-mode -1)
1365 ;; continue standard unloading
1366 nil)
1368 (provide 'recentf)
1370 (run-hooks 'recentf-load-hook)
1372 ;;; recentf.el ends here