1 ;;; recentf.el --- setup a menu of recently opened files
3 ;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Created: July 19 1999
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/>.
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:
41 (require 'tree-widget
)
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
))
56 "Maintain a menu of recently opened 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."
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'."
72 (defcustom recentf-save-file
(locate-user-emacs-file "recentf" ".recentf")
73 "File to save the recent list into."
77 :initialize
'custom-initialize-default
78 :set
(lambda (symbol value
)
79 (let ((oldvalue (eval symbol
)))
80 (custom-set-default symbol value
)
81 (and (not (equal value oldvalue
))
83 (recentf-load-list)))))
85 (defcustom recentf-save-file-modes
384 ;; 0600
86 "Mode bits of recentf save file, as an integer, or nil.
87 If non-nil, after writing `recentf-save-file', set its mode bits to
88 this value. By default give R/W access only to the user who owns that
89 file. See also the function `set-file-modes'."
91 :type
'(choice (const :tag
"Don't change" nil
)
94 (defcustom recentf-exclude nil
95 "List of regexps and predicates for filenames excluded from the recent list.
96 When a filename matches any of the regexps or satisfies any of the
97 predicates it is excluded from the recent list.
98 A predicate is a function that is passed a filename to check and that
99 must return non-nil to exclude it."
101 :type
'(repeat (choice regexp function
)))
103 (defun recentf-keep-default-predicate (file)
104 "Return non-nil if FILE should be kept in the recent list.
105 It handles the case of remote files as well."
107 ((file-remote-p file nil t
) (file-readable-p file
))
108 ((file-remote-p file
))
109 ((file-readable-p file
))))
111 (defcustom recentf-keep
112 '(recentf-keep-default-predicate)
113 "List of regexps and predicates for filenames kept in the recent list.
114 Regexps and predicates are tried in the specified order.
115 When nil all filenames are kept in the recent list.
116 When a filename matches any of the regexps or satisfies any of the
117 predicates it is kept in the recent list.
118 The default is to keep readable files. Remote files are checked
119 for readability only in case a connection is established to that
120 remote system, otherwise they are kept in the recent list without
121 checking their readability.
122 A predicate is a function that is passed a filename to check and that
123 must return non-nil to keep it."
125 :type
'(repeat (choice regexp function
)))
127 (defun recentf-menu-customization-changed (variable value
)
128 "Function called when the recentf menu customization has changed.
129 Set VARIABLE with VALUE, and force a rebuild of the recentf menu."
130 (if (and (featurep 'recentf
) (recentf-enabled-p))
132 ;; Unavailable until recentf has been loaded.
134 (set-default variable value
)
136 (set-default variable value
)))
138 (defcustom recentf-menu-title
"Open Recent"
139 "Name of the recentf menu."
142 :set
'recentf-menu-customization-changed
)
144 (defcustom recentf-menu-path
'("File")
145 "Path where to add the recentf menu.
146 If nil add it at top level (see also `easy-menu-add-item')."
148 :type
'(choice (const :tag
"Top Level" nil
)
149 (sexp :tag
"Menu Path"))
150 :set
'recentf-menu-customization-changed
)
152 (defcustom recentf-menu-before
"Open File..."
153 "Name of the menu before which the recentf menu will be added.
154 If nil add it at end of menu (see also `easy-menu-add-item')."
156 :type
'(choice (string :tag
"Name")
157 (const :tag
"Last" nil
))
158 :set
'recentf-menu-customization-changed
)
160 (defcustom recentf-menu-action
'find-file
161 "Function to invoke with a filename item of the recentf menu.
162 The default is to call `find-file' to edit the selected file."
166 (defcustom recentf-max-menu-items
10
167 "Maximum number of items in the recentf menu."
171 (defcustom recentf-menu-filter nil
172 "Function used to filter files displayed in the recentf menu.
173 A nil value means no filter. The following functions are predefined:
175 - `recentf-sort-ascending'
176 Sort menu items in ascending order.
177 - `recentf-sort-descending'
178 Sort menu items in descending order.
179 - `recentf-sort-basenames-ascending'
180 Sort menu items by filenames sans directory in ascending order.
181 - `recentf-sort-basenames-descending'
182 Sort menu items by filenames sans directory in descending order.
183 - `recentf-sort-directories-ascending'
184 Sort menu items by directories in ascending order.
185 - `recentf-sort-directories-descending'
186 Sort menu items by directories in descending order.
187 - `recentf-show-basenames'
188 Show filenames sans directory in menu items.
189 - `recentf-show-basenames-ascending'
190 Show filenames sans directory in ascending order.
191 - `recentf-show-basenames-descending'
192 Show filenames sans directory in descending order.
193 - `recentf-relative-filter'
194 Show filenames relative to `default-directory'.
195 - `recentf-arrange-by-rule'
196 Show sub-menus following user defined rules.
197 - `recentf-arrange-by-mode'
198 Show a sub-menu for each major mode.
199 - `recentf-arrange-by-dir'
200 Show a sub-menu for each directory.
201 - `recentf-filter-changer'
202 Manage a menu of filters.
204 The filter function is called with one argument, the list of menu
205 elements used to build the menu and must return a new list of menu
206 elements (see `recentf-make-menu-element' for menu element form)."
208 :type
'(radio (const nil
)
209 (function-item recentf-sort-ascending
)
210 (function-item recentf-sort-descending
)
211 (function-item recentf-sort-basenames-ascending
)
212 (function-item recentf-sort-basenames-descending
)
213 (function-item recentf-sort-directories-ascending
)
214 (function-item recentf-sort-directories-descending
)
215 (function-item recentf-show-basenames
)
216 (function-item recentf-show-basenames-ascending
)
217 (function-item recentf-show-basenames-descending
)
218 (function-item recentf-relative-filter
)
219 (function-item recentf-arrange-by-rule
)
220 (function-item recentf-arrange-by-mode
)
221 (function-item recentf-arrange-by-dir
)
222 (function-item recentf-filter-changer
)
225 (defcustom recentf-menu-open-all-flag nil
226 "Non-nil means to show an \"All...\" item in the menu.
227 This item will replace the \"More...\" item."
231 (define-obsolete-variable-alias 'recentf-menu-append-commands-p
232 'recentf-menu-append-commands-flag
235 (defcustom recentf-menu-append-commands-flag t
236 "Non-nil means to append command items to the menu."
240 (defcustom recentf-auto-cleanup
'mode
241 "Define when to automatically cleanup the recent list.
242 The following values can be set:
245 Cleanup when turning the mode on (default).
247 Never cleanup the list automatically.
249 Cleanup each time Emacs has been idle that number of seconds.
251 Cleanup at specified time string, for example at \"11:00pm\".
253 Setting this variable directly does not take effect;
256 See also the command `recentf-cleanup', that can be used to manually
259 :type
'(radio (const :tag
"When mode enabled"
263 (number :tag
"When idle that seconds"
265 (string :tag
"At time"
267 :set
(lambda (variable value
)
268 (set-default variable value
)
269 (when (featurep 'recentf
)
270 ;; Unavailable until recentf has been loaded.
271 (recentf-auto-cleanup))))
273 (defcustom recentf-initialize-file-name-history t
274 "Non-nil means to initialize `file-name-history' with the recent list.
275 If `file-name-history' is not empty, do nothing."
279 (defcustom recentf-load-hook nil
280 "Normal hook run at end of loading the `recentf' package."
284 (defcustom recentf-filename-handlers nil
285 "Functions to post process recent file names.
286 They are successively passed a file name to transform it."
289 (const :tag
"None" nil
)
290 (repeat :tag
"Functions"
292 (const file-truename
)
293 (const abbreviate-file-name
)
294 (function :tag
"Other function")))))
296 (defcustom recentf-show-file-shortcuts-flag t
297 "Whether to show ``[N]'' for the Nth item up to 10.
298 If non-nil, `recentf-open-files' will show labels for keys that can be
299 used as shortcuts to open the Nth file."
305 (defconst recentf-case-fold-search
306 (memq system-type
'(windows-nt cygwin
))
307 "Non-nil if recentf searches and matches should ignore case.")
309 (defsubst recentf-string-equal
(s1 s2
)
310 "Return non-nil if strings S1 and S2 have identical contents.
311 Ignore case if `recentf-case-fold-search' is non-nil."
312 (if recentf-case-fold-search
313 (string-equal (downcase s1
) (downcase s2
))
314 (string-equal s1 s2
)))
316 (defsubst recentf-string-lessp
(s1 s2
)
317 "Return non-nil if string S1 is less than S2 in lexicographic order.
318 Ignore case if `recentf-case-fold-search' is non-nil."
319 (if recentf-case-fold-search
320 (string-lessp (downcase s1
) (downcase s2
))
321 (string-lessp s1 s2
)))
323 (defun recentf-string-member (elt list
)
324 "Return non-nil if ELT is an element of LIST.
325 The value is actually the tail of LIST whose car is ELT.
326 ELT must be a string and LIST a list of strings.
327 Ignore case if `recentf-case-fold-search' is non-nil."
328 (while (and list
(not (recentf-string-equal elt
(car list
))))
329 (setq list
(cdr list
)))
332 (defsubst recentf-trunc-list
(l n
)
333 "Return from L the list of its first N elements."
335 (while (and l
(> n
0))
336 (setq nl
(cons (car l
) nl
)
341 (defun recentf-dump-variable (variable &optional limit
)
342 "Insert a \"(setq VARIABLE value)\" in the current buffer.
343 When the value of VARIABLE is a list, optional argument LIMIT
344 specifies a maximum number of elements to insert. By default insert
346 (let ((value (symbol-value variable
)))
348 (insert (format "\n(setq %S '%S)\n" variable value
))
349 (when (and (integerp limit
) (> limit
0))
350 (setq value
(recentf-trunc-list value limit
)))
351 (insert (format "\n(setq %S\n '(" variable
))
353 (insert (format "\n %S" e
)))
354 (insert "\n ))\n"))))
356 (defvar recentf-auto-cleanup-timer nil
357 "Timer used to automatically cleanup the recent list.
358 See also the option `recentf-auto-cleanup'.")
360 (defun recentf-auto-cleanup ()
361 "Automatic cleanup of the recent list."
362 (when (timerp recentf-auto-cleanup-timer
)
363 (cancel-timer recentf-auto-cleanup-timer
))
365 (setq recentf-auto-cleanup-timer
367 ((eq 'mode recentf-auto-cleanup
)
370 ((numberp recentf-auto-cleanup
)
372 recentf-auto-cleanup t
'recentf-cleanup
))
373 ((stringp recentf-auto-cleanup
)
375 recentf-auto-cleanup nil
'recentf-cleanup
))))))
379 (defsubst recentf-push
(filename)
380 "Push FILENAME into the recent list, if it isn't there yet.
381 If it is there yet, move it at the beginning of the list.
382 If `recentf-case-fold-search' is non-nil, ignore case when comparing
384 (let ((m (recentf-string-member filename recentf-list
)))
385 (and m
(setq recentf-list
(delq (car m
) recentf-list
)))
386 (push filename recentf-list
)))
388 (defun recentf-apply-filename-handlers (name)
389 "Apply `recentf-filename-handlers' to file NAME.
390 Return the transformed file name, or NAME if any handler failed, or
392 (or (condition-case nil
393 (let ((handlers recentf-filename-handlers
)
395 (while (and filename handlers
)
396 (setq filename
(funcall (car handlers
) filename
)
397 handlers
(cdr handlers
)))
402 (defsubst recentf-expand-file-name
(name)
403 "Convert file NAME to absolute, and canonicalize it.
404 NAME is first passed to the function `expand-file-name', then to
405 `recentf-filename-handlers' to post process it."
406 (recentf-apply-filename-handlers (expand-file-name name
)))
408 (defun recentf-include-p (filename)
409 "Return non-nil if FILENAME should be included in the recent list.
410 That is, if it doesn't match any of the `recentf-exclude' checks."
411 (let ((case-fold-search recentf-case-fold-search
)
412 (checks recentf-exclude
)
414 (while (and checks keepit
)
415 ;; If there was an error in a predicate, err on the side of
416 ;; keeping the file. (Bug#5843)
417 (setq keepit
(not (ignore-errors
418 (if (stringp (car checks
))
420 (string-match (car checks
) filename
)
422 (funcall (car checks
) filename
))))
423 checks
(cdr checks
)))
426 (defun recentf-keep-p (filename)
427 "Return non-nil if FILENAME should be kept in the recent list.
428 That is, if it matches any of the `recentf-keep' checks."
429 (let* ((case-fold-search recentf-case-fold-search
)
430 (checks recentf-keep
)
431 (keepit (null checks
)))
432 (while (and checks
(not keepit
))
433 (setq keepit
(condition-case nil
434 (if (stringp (car checks
))
436 (string-match (car checks
) filename
)
438 (funcall (car checks
) filename
))
440 checks
(cdr checks
)))
443 (defsubst recentf-add-file
(filename)
444 "Add or move FILENAME at the beginning of the recent list.
445 Does nothing if the name satisfies any of the `recentf-exclude'
446 regexps or predicates."
447 (setq filename
(recentf-expand-file-name filename
))
448 (when (recentf-include-p filename
)
449 (recentf-push filename
)))
451 (defsubst recentf-remove-if-non-kept
(filename)
452 "Remove FILENAME from the recent list, if file is not kept.
453 Return non-nil if FILENAME has been removed."
454 (unless (recentf-keep-p filename
)
455 (let ((m (recentf-string-member
456 (recentf-expand-file-name filename
) recentf-list
)))
457 (and m
(setq recentf-list
(delq (car m
) recentf-list
))))))
459 (defsubst recentf-directory-compare
(f1 f2
)
460 "Compare absolute filenames F1 and F2.
461 First compare directories, then filenames sans directory.
462 Return non-nil if F1 is less than F2."
463 (let ((d1 (file-name-directory f1
))
464 (d2 (file-name-directory f2
)))
465 (if (recentf-string-equal d1 d2
)
466 (recentf-string-lessp (file-name-nondirectory f1
)
467 (file-name-nondirectory f2
))
468 (recentf-string-lessp d1 d2
))))
472 (defsubst recentf-digit-shortcut-command-name
(n)
473 "Return a command name to open the Nth most recent file.
474 See also the command `recentf-open-most-recent-file'."
475 (intern (format "recentf-open-most-recent-file-%d" n
)))
477 (defvar recentf--shortcuts-keymap
478 (let ((km (make-sparse-keymap)))
479 (dolist (k '(0 9 8 7 6 5 4 3 2 1))
480 (let ((cmd (recentf-digit-shortcut-command-name k
)))
481 ;; Define a shortcut command.
485 (recentf-open-most-recent-file ,k
)))
486 ;; Bind it to a digit key.
487 (define-key km
(vector (+ k ?
0)) cmd
)))
489 "Digit shortcuts keymap.")
491 (defvar recentf-menu-items-for-commands
495 :help
"Remove duplicates, and obsoletes files from the recent list"
499 :help
"Manually remove files from the recent list"
503 :help
"Save the list of recently opened files now"
506 (customize-group "recentf")
507 :help
"Customize recently opened files menu and options"
510 "List of menu items for recentf commands.")
512 (defvar recentf-menu-filter-commands nil
513 "This variable can be used by menu filters to setup their own command menu.
514 If non-nil it must contain a list of valid menu-items to be appended
515 to the recent file list part of the menu. Before calling a menu
516 filter function this variable is reset to nil.")
518 (defsubst recentf-elements
(n)
519 "Return a list of the first N elements of the recent list."
520 (recentf-trunc-list recentf-list n
))
522 (defsubst recentf-make-menu-element
(menu-item menu-value
)
523 "Create a new menu-element.
524 A menu element is a pair (MENU-ITEM . MENU-VALUE), where MENU-ITEM is
525 the menu item string displayed. MENU-VALUE is the file to be open
526 when the corresponding MENU-ITEM is selected. Or it is a
527 pair (SUB-MENU-TITLE . MENU-ELEMENTS) where SUB-MENU-TITLE is a
528 sub-menu title and MENU-ELEMENTS is the list of menu elements in the
530 (cons menu-item menu-value
))
532 (defsubst recentf-menu-element-item
(e)
533 "Return the item part of the menu-element E."
536 (defsubst recentf-menu-element-value
(e)
537 "Return the value part of the menu-element E."
540 (defsubst recentf-set-menu-element-item
(e item
)
541 "Change the item part of menu-element E to ITEM."
544 (defsubst recentf-set-menu-element-value
(e value
)
545 "Change the value part of menu-element E to VALUE."
548 (defsubst recentf-sub-menu-element-p
(e)
549 "Return non-nil if menu-element E defines a sub-menu."
550 (consp (recentf-menu-element-value e
)))
552 (defsubst recentf-make-default-menu-element
(file)
553 "Make a new default menu element with FILE.
554 This a menu element (FILE . FILE)."
555 (recentf-make-menu-element file file
))
557 (defsubst recentf-menu-elements
(n)
558 "Return a list of the first N default menu elements from the recent list.
559 See also `recentf-make-default-menu-element'."
560 (mapcar 'recentf-make-default-menu-element
561 (recentf-elements n
)))
563 (defun recentf-apply-menu-filter (filter l
)
564 "Apply function FILTER to the list of menu-elements L.
565 It takes care of sub-menu elements in L and recursively apply FILTER
566 to them. It is guaranteed that FILTER receives only a list of single
567 menu-elements (no sub-menu)."
568 (if (and l
(functionp filter
))
569 (let ((case-fold-search recentf-case-fold-search
)
571 ;; split L into two sub-lists, one of sub-menus elements and
572 ;; another of single menu elements.
574 (if (recentf-sub-menu-element-p elt
)
577 ;; Apply FILTER to single elements.
579 (setq others
(funcall filter
(nreverse others
))))
580 ;; Apply FILTER to sub-menu elements.
583 (recentf-set-menu-element-value
584 elt
(recentf-apply-menu-filter
585 filter
(recentf-menu-element-value elt
)))
587 ;; Return the new filtered menu element list.
591 ;; Count the number of assigned menu shortcuts.
592 (defvar recentf-menu-shortcuts
)
594 (defun recentf-make-menu-items (&optional _menu
)
595 "Make menu items from the recent list.
596 This is a menu filter function which ignores the MENU argument."
597 (setq recentf-menu-filter-commands nil
)
598 (let* ((recentf-menu-shortcuts 0)
601 (mapcar 'recentf-make-menu-item
602 (recentf-apply-menu-filter
604 (recentf-menu-elements recentf-max-menu-items
)))
606 (message "recentf update menu failed: %s"
607 (error-message-string err
))))))
611 :help
"No recent file to open"
613 (if recentf-menu-open-all-flag
614 '(["All..." recentf-open-files
615 :help
"Open recent files through a dialog"
617 (and (< recentf-max-menu-items
(length recentf-list
))
618 '(["More..." recentf-open-more-files
619 :help
"Open files not in the menu through a dialog"
621 (and recentf-menu-filter-commands
'("---"))
622 recentf-menu-filter-commands
623 (and recentf-menu-items-for-commands
'("---"))
624 recentf-menu-items-for-commands
)))
626 (defun recentf-menu-value-shortcut (name)
627 "Return a shortcut digit for file NAME.
628 Return nil if file NAME is not one of the ten more recent."
630 (while (and (not k
) (< i
10))
631 (if (string-equal name
(nth i recentf-list
))
633 (setq recentf-menu-shortcuts
(1+ recentf-menu-shortcuts
))
634 (setq k
(%
(1+ i
) 10)))
638 (defun recentf-make-menu-item (elt)
639 "Make a menu item from menu element ELT."
640 (let ((item (recentf-menu-element-item elt
))
641 (value (recentf-menu-element-value elt
)))
642 (if (recentf-sub-menu-element-p elt
)
643 (cons item
(mapcar 'recentf-make-menu-item value
))
644 (let ((k (and (< recentf-menu-shortcuts
10)
645 (recentf-menu-value-shortcut value
))))
647 ;; If the file name is one of the ten more recent, use
648 ;; a digit shortcut command to open it, else use an
649 ;; anonymous command.
651 (recentf-digit-shortcut-command-name k
)
654 (,recentf-menu-action
,value
)))
655 :help
(concat "Open " value
)
658 (defsubst recentf-menu-bar
()
659 "Return the keymap of the global menu bar."
660 (lookup-key global-map
[menu-bar
]))
662 (defun recentf-show-menu ()
663 "Show the menu of recently opened files."
665 (recentf-menu-bar) recentf-menu-path
666 (list recentf-menu-title
:filter
'recentf-make-menu-items
)
667 recentf-menu-before
))
669 (defun recentf-hide-menu ()
670 "Hide the menu of recently opened files."
671 (easy-menu-remove-item (recentf-menu-bar) recentf-menu-path
674 ;;; Predefined menu filters
676 (defsubst recentf-sort-ascending
(l)
677 "Sort the list of menu elements L in ascending order.
678 The MENU-ITEM part of each menu element is compared."
679 (sort (copy-sequence l
)
681 (recentf-string-lessp
682 (recentf-menu-element-item e1
)
683 (recentf-menu-element-item e2
)))))
685 (defsubst recentf-sort-descending
(l)
686 "Sort the list of menu elements L in descending order.
687 The MENU-ITEM part of each menu element is compared."
688 (sort (copy-sequence l
)
690 (recentf-string-lessp
691 (recentf-menu-element-item e2
)
692 (recentf-menu-element-item e1
)))))
694 (defsubst recentf-sort-basenames-ascending
(l)
695 "Sort the list of menu elements L in ascending order.
696 Only filenames sans directory are compared."
697 (sort (copy-sequence l
)
699 (recentf-string-lessp
700 (file-name-nondirectory (recentf-menu-element-value e1
))
701 (file-name-nondirectory (recentf-menu-element-value e2
))))))
703 (defsubst recentf-sort-basenames-descending
(l)
704 "Sort the list of menu elements L in descending order.
705 Only filenames sans directory are compared."
706 (sort (copy-sequence l
)
708 (recentf-string-lessp
709 (file-name-nondirectory (recentf-menu-element-value e2
))
710 (file-name-nondirectory (recentf-menu-element-value e1
))))))
712 (defsubst recentf-sort-directories-ascending
(l)
713 "Sort the list of menu elements L in ascending order.
714 Compares directories then filenames to order the list."
715 (sort (copy-sequence l
)
717 (recentf-directory-compare
718 (recentf-menu-element-value e1
)
719 (recentf-menu-element-value e2
)))))
721 (defsubst recentf-sort-directories-descending
(l)
722 "Sort the list of menu elements L in descending order.
723 Compares directories then filenames to order the list."
724 (sort (copy-sequence l
)
726 (recentf-directory-compare
727 (recentf-menu-element-value e2
)
728 (recentf-menu-element-value e1
)))))
730 (defun recentf-show-basenames (l &optional no-dir
)
731 "Filter the list of menu elements L to show filenames sans directory.
732 When a filename is duplicated, it is appended a sequence number if
733 optional argument NO-DIR is non-nil, or its directory otherwise."
734 (let (filtered-names filtered-list full name counters sufx
)
735 (dolist (elt l
(nreverse filtered-list
))
736 (setq full
(recentf-menu-element-value elt
)
737 name
(file-name-nondirectory full
))
738 (if (not (member name filtered-names
))
739 (push name filtered-names
)
741 (if (setq sufx
(assoc name counters
))
742 (setcdr sufx
(1+ (cdr sufx
)))
744 (push (cons name sufx
) counters
))
745 (setq sufx
(file-name-directory full
)))
746 (setq name
(format "%s(%s)" name sufx
)))
747 (push (recentf-make-menu-element name full
) filtered-list
))))
749 (defsubst recentf-show-basenames-ascending
(l)
750 "Filter the list of menu elements L to show filenames sans directory.
751 Filenames are sorted in ascending order.
752 This filter combines the `recentf-sort-basenames-ascending' and
753 `recentf-show-basenames' filters."
754 (recentf-show-basenames (recentf-sort-basenames-ascending l
)))
756 (defsubst recentf-show-basenames-descending
(l)
757 "Filter the list of menu elements L to show filenames sans directory.
758 Filenames are sorted in descending order.
759 This filter combines the `recentf-sort-basenames-descending' and
760 `recentf-show-basenames' filters."
761 (recentf-show-basenames (recentf-sort-basenames-descending l
)))
763 (defun recentf-relative-filter (l)
764 "Filter the list of menu-elements L to show relative filenames.
765 Filenames are relative to the `default-directory'."
766 (mapcar #'(lambda (menu-element)
767 (let* ((ful (recentf-menu-element-value menu-element
))
768 (rel (file-relative-name ful default-directory
)))
769 (if (string-match "^\\.\\." rel
)
771 (recentf-make-menu-element rel ful
))))
774 ;;; Rule based menu filters
776 (defcustom recentf-arrange-rules
778 ("Elisp files (%d)" ".\\.el\\'")
779 ("Java files (%d)" ".\\.java\\'")
780 ("C/C++ files (%d)" "c\\(pp\\)?\\'")
782 "List of rules used by `recentf-arrange-by-rule' to build sub-menus.
783 A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
784 displayed title of the sub-menu where a '%d' `format' pattern is
785 replaced by the number of items in the sub-menu. MATCHER is a regexp
786 or a list of regexps. Items matching one of the regular expressions in
787 MATCHER are added to the corresponding sub-menu.
788 SUB-MENU-TITLE can be a function. It is passed every items that
789 matched the corresponding MATCHER, and it must return a
790 pair (SUB-MENU-TITLE . ITEM). SUB-MENU-TITLE is a computed sub-menu
791 title that can be another function. ITEM is the received item which
792 may have been modified to match another rule."
793 :group
'recentf-filters
794 :type
'(repeat (cons (choice string function
)
797 (defcustom recentf-arrange-by-rule-others
"Other files (%d)"
798 "Title of the `recentf-arrange-by-rule' sub-menu.
799 This is for the menu where items that don't match any
800 `recentf-arrange-rules' are displayed. If nil these items are
801 displayed in the main recent files menu. A '%d' `format' pattern in
802 the title is replaced by the number of items in the sub-menu."
803 :group
'recentf-filters
804 :type
'(choice (const :tag
"Main menu" nil
)
805 (string :tag
"Title")))
807 (defcustom recentf-arrange-by-rules-min-items
0
808 "Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
809 If the number of items in a sub-menu is less than this value the
810 corresponding sub-menu items are displayed in the main recent files
811 menu or in the `recentf-arrange-by-rule-others' sub-menu if
813 :group
'recentf-filters
816 (defcustom recentf-arrange-by-rule-subfilter nil
817 "Function called by a rule based filter to filter sub-menu elements.
818 A nil value means no filter. See also `recentf-menu-filter'.
819 You can't use another rule based filter here."
820 :group
'recentf-filters
821 :type
'(choice (const nil
) function
)
822 :set
(lambda (variable value
)
823 (when (memq value
'(recentf-arrange-by-rule
824 recentf-arrange-by-mode
825 recentf-arrange-by-dir
))
826 (error "Recursive use of a rule based filter"))
827 (set-default variable value
)))
829 (defun recentf-match-rule (file)
830 "Return the rule that match FILE."
831 (let ((rules recentf-arrange-rules
)
833 (while (and (not found
) rules
)
834 (setq match
(cdar rules
))
835 (when (stringp match
)
836 (setq match
(list match
)))
837 (while (and match
(not (string-match (car match
) file
)))
838 (setq match
(cdr match
)))
840 (setq found
(cons (caar rules
) file
))
841 (setq rules
(cdr rules
))))
844 (defun recentf-arrange-by-rule (l)
845 "Filter the list of menu-elements L.
846 Arrange them in sub-menus following rules in `recentf-arrange-rules'."
847 (when recentf-arrange-rules
848 (let (menus others menu file min count
)
849 ;; Put menu items into sub-menus as defined by rules.
851 (setq file
(recentf-menu-element-value elt
)
852 menu
(recentf-match-rule file
))
853 (while (functionp (car menu
))
854 (setq menu
(funcall (car menu
) (cdr menu
))))
855 (if (not (stringp (car menu
)))
857 (setq menu
(or (assoc (car menu
) menus
)
858 (car (push (list (car menu
)) menus
))))
859 (recentf-set-menu-element-value
860 menu
(cons elt
(recentf-menu-element-value menu
)))))
861 ;; Finalize each sub-menu:
862 ;; - truncate it depending on the value of
863 ;; `recentf-arrange-by-rules-min-items',
864 ;; - replace %d by the number of menu items,
865 ;; - apply `recentf-arrange-by-rule-subfilter' to menu items.
866 (setq min
(if (natnump recentf-arrange-by-rules-min-items
)
867 recentf-arrange-by-rules-min-items
0)
870 (setq menu
(recentf-menu-element-value elt
)
873 (setq others
(nconc menu others
))
874 (recentf-set-menu-element-item
875 elt
(format (recentf-menu-element-item elt
) count
))
876 (recentf-set-menu-element-value
877 elt
(recentf-apply-menu-filter
878 recentf-arrange-by-rule-subfilter
(nreverse menu
)))
880 ;; Add the menu items remaining in the `others' bin.
881 (when (setq others
(nreverse others
))
884 ;; Put items in an sub menu.
885 (if (stringp recentf-arrange-by-rule-others
)
887 (recentf-make-menu-element
888 (format recentf-arrange-by-rule-others
890 (recentf-apply-menu-filter
891 recentf-arrange-by-rule-subfilter others
)))
892 ;; Append items to the main menu.
893 (recentf-apply-menu-filter
894 recentf-arrange-by-rule-subfilter others
)))))))
897 ;;; Predefined rule based menu filters
899 (defun recentf-indirect-mode-rule (file)
900 "Apply a second level `auto-mode-alist' regexp to FILE."
901 (recentf-match-rule (substring file
0 (match-beginning 0))))
903 (defun recentf-build-mode-rules ()
904 "Convert `auto-mode-alist' to menu filter rules.
905 Rules obey `recentf-arrange-rules' format."
906 (let ((case-fold-search recentf-case-fold-search
)
907 regexp rule-name rule rules
)
908 (dolist (mode auto-mode-alist
)
909 (setq regexp
(car mode
)
913 ;; Build a special "strip suffix" rule from entries of the
914 ;; form (REGEXP FUNCTION NON-NIL). Notice that FUNCTION is
915 ;; ignored by the menu filter. So in some corner cases a
916 ;; wrong mode could be guessed.
917 ((and (consp mode
) (cadr mode
))
918 (setq rule-name
'recentf-indirect-mode-rule
))
919 ((and mode
(symbolp mode
))
920 (setq rule-name
(symbol-name mode
))
921 (if (string-match "\\(.*\\)-mode$" rule-name
)
922 (setq rule-name
(match-string 1 rule-name
)))
923 (setq rule-name
(concat rule-name
" (%d)"))))
924 (setq rule
(assoc rule-name rules
))
926 (setcdr rule
(cons regexp
(cdr rule
)))
927 (push (list rule-name regexp
) rules
))))
928 ;; It is important to preserve auto-mode-alist order
929 ;; to ensure the right file <-> mode association
932 (defun recentf-arrange-by-mode (l)
933 "Split the list of menu-elements L into sub-menus by major mode."
934 (let ((recentf-arrange-rules (recentf-build-mode-rules))
935 (recentf-arrange-by-rule-others "others (%d)"))
936 (recentf-arrange-by-rule l
)))
938 (defun recentf-file-name-nondir (l)
939 "Filter the list of menu-elements L to show filenames sans directory.
940 This simplified version of `recentf-show-basenames' does not handle
941 duplicates. It is used by `recentf-arrange-by-dir' as its
942 `recentf-arrange-by-rule-subfilter'."
943 (mapcar #'(lambda (e)
944 (recentf-make-menu-element
945 (file-name-nondirectory (recentf-menu-element-value e
))
946 (recentf-menu-element-value e
)))
949 (defun recentf-dir-rule (file)
950 "Return as a sub-menu, the directory FILE belongs to."
951 (cons (file-name-directory file
) file
))
953 (defun recentf-arrange-by-dir (l)
954 "Split the list of menu-elements L into sub-menus by directory."
955 (let ((recentf-arrange-rules '((recentf-dir-rule .
".*")))
956 (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir
)
957 recentf-arrange-by-rule-others
)
958 (recentf-arrange-by-rule l
)))
960 ;;; Menu of menu filters
962 (defvar recentf-filter-changer-current nil
963 "Current filter used by `recentf-filter-changer'.")
965 (defcustom recentf-filter-changer-alist
967 (recentf-arrange-by-mode .
"Grouped by Mode")
968 (recentf-arrange-by-dir .
"Grouped by Directory")
969 (recentf-arrange-by-rule .
"Grouped by Custom Rules")
971 "List of filters managed by `recentf-filter-changer'.
972 Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is
973 the filter function, and LABEL is the menu item displayed to select
975 :group
'recentf-filters
976 :type
'(repeat (cons function string
))
977 :set
(lambda (variable value
)
978 (setq recentf-filter-changer-current nil
)
979 (set-default variable value
)))
981 (defun recentf-filter-changer-select (filter)
982 "Select FILTER as the current menu filter.
983 See `recentf-filter-changer'."
984 (setq recentf-filter-changer-current filter
))
986 (defun recentf-filter-changer (l)
987 "Manage a sub-menu of menu filters.
988 `recentf-filter-changer-alist' defines the filters in the menu.
989 Filtering of L is delegated to the selected filter in the menu."
990 (unless recentf-filter-changer-current
991 (setq recentf-filter-changer-current
992 (caar recentf-filter-changer-alist
)))
993 (if (not recentf-filter-changer-current
)
995 (setq recentf-menu-filter-commands
1001 (setq recentf-filter-changer-current
',(car f
))
1003 :style radio
;;radio Don't work with GTK :-(
1004 :selected
(eq recentf-filter-changer-current
1008 recentf-filter-changer-alist
))))
1009 (recentf-apply-menu-filter recentf-filter-changer-current l
)))
1013 (defun recentf-track-opened-file ()
1014 "Insert the name of the file just opened or written into the recent list."
1015 (and buffer-file-name
1016 (recentf-add-file buffer-file-name
))
1017 ;; Must return nil because it is run from `write-file-functions'.
1020 (defun recentf-track-closed-file ()
1021 "Update the recent list when a buffer is killed.
1022 That is, remove a non kept file from the recent list."
1023 (and buffer-file-name
1024 (recentf-remove-if-non-kept buffer-file-name
)))
1026 (defconst recentf-used-hooks
1028 (find-file-hook recentf-track-opened-file
)
1029 (write-file-functions recentf-track-opened-file
)
1030 (kill-buffer-hook recentf-track-closed-file
)
1031 (kill-emacs-hook recentf-save-list
)
1033 "Hooks used by recentf.")
1038 ;;; Common dialog stuff
1040 (defun recentf-cancel-dialog (&rest _ignore
)
1041 "Cancel the current dialog.
1044 (kill-buffer (current-buffer))
1045 (message "Dialog canceled"))
1047 (defun recentf-dialog-goto-first (widget-type)
1048 "Move the cursor to the first WIDGET-TYPE in current dialog.
1049 Go to the beginning of buffer if not found."
1050 (goto-char (point-min))
1055 (if (eq widget-type
(widget-type (widget-at (point))))
1059 (goto-char (point-min)))))
1061 (defvar recentf-dialog-mode-map
1062 (let ((km (copy-keymap recentf--shortcuts-keymap
)))
1063 (set-keymap-parent km widget-keymap
)
1064 (define-key km
"q" 'recentf-cancel-dialog
)
1065 (define-key km
"n" 'next-line
)
1066 (define-key km
"p" 'previous-line
)
1067 (define-key km
[follow-link
] "\C-m")
1069 "Keymap used in recentf dialogs.")
1071 (define-derived-mode recentf-dialog-mode nil
"recentf-dialog"
1072 "Major mode of recentf dialogs.
1074 \\{recentf-dialog-mode-map}"
1077 (setq truncate-lines t
))
1079 (defmacro recentf-dialog
(name &rest forms
)
1080 "Show a dialog buffer with NAME, setup with FORMS."
1081 (declare (indent 1) (debug t
))
1082 `(with-current-buffer (get-buffer-create ,name
)
1084 (let ((inhibit-read-only t
)
1085 (ol (overlay-lists)))
1086 (mapc 'delete-overlay
(car ol
))
1087 (mapc 'delete-overlay
(cdr ol
))
1089 (recentf-dialog-mode)
1092 (switch-to-buffer (current-buffer))))
1094 ;;; Edit list dialog
1096 (defvar recentf-edit-list nil
)
1098 (defun recentf-edit-list-select (widget &rest _ignore
)
1099 "Toggle a file selection based on the checkbox WIDGET state.
1100 IGNORE other arguments."
1101 (let ((value (widget-get widget
:tag
))
1102 (check (widget-value widget
)))
1104 (add-to-list 'recentf-edit-list value
)
1105 (setq recentf-edit-list
(delq value recentf-edit-list
)))
1106 (message "%s %sselected" value
(if check
"" "un"))))
1108 (defun recentf-edit-list-validate (&rest _ignore
)
1109 "Process the recent list when the edit list dialog is committed.
1111 (if recentf-edit-list
1113 (dolist (e recentf-edit-list
)
1114 (setq recentf-list
(delq e recentf-list
)
1116 (kill-buffer (current-buffer))
1117 (message "%S file(s) removed from the list" i
))
1118 (message "No file selected")))
1120 (defun recentf-edit-list ()
1121 "Show a dialog to delete selected files from the recent list."
1123 (unless recentf-list
1124 (error "The list of recent files is empty"))
1125 (recentf-dialog (format "*%s - Edit list*" recentf-menu-title
)
1126 (set (make-local-variable 'recentf-edit-list
) nil
)
1128 "Click on OK to delete selected files from the recent list.
1129 Click on Cancel or type `q' to cancel.\n")
1130 ;; Insert the list of files as checkboxes
1131 (dolist (item recentf-list
)
1132 (widget-create 'checkbox
1133 :value nil
; unselected checkbox
1134 :format
"\n %[%v%] %t"
1136 :notify
'recentf-edit-list-select
))
1137 (widget-insert "\n\n")
1140 :notify
'recentf-edit-list-validate
1141 :help-echo
"Delete selected files from the recent list"
1146 :notify
'recentf-cancel-dialog
1148 (recentf-dialog-goto-first 'checkbox
)))
1150 ;;; Open file dialog
1152 (defun recentf-open-files-action (widget &rest _ignore
)
1153 "Open the file stored in WIDGET's value when notified.
1154 IGNORE other arguments."
1155 (kill-buffer (current-buffer))
1156 (funcall recentf-menu-action
(widget-value widget
)))
1158 ;; List of files associated to a digit shortcut key.
1159 (defvar recentf--files-with-key nil
)
1161 (defun recentf-show-digit-shortcut-filter (l)
1162 "Filter the list of menu-elements L to show digit shortcuts."
1166 (recentf-set-menu-element-item
1167 e
(format "[%d] %s" (% i
10) (recentf-menu-element-item e
))))
1170 (defun recentf-open-files-item (menu-element)
1171 "Return a widget to display MENU-ELEMENT in a dialog buffer."
1172 (if (consp (cdr menu-element
))
1173 ;; Represent a sub-menu with a tree widget
1177 :node
(item :tag
,(car menu-element
)
1179 :format
"%{%t%}:\n")
1180 ,@(mapcar 'recentf-open-files-item
1181 (cdr menu-element
)))
1182 ;; Represent a single file with a link widget
1183 `(link :tag
,(car menu-element
)
1186 :button-face default
1188 :help-echo
,(concat "Open " (cdr menu-element
))
1189 :action recentf-open-files-action
1190 ,(cdr menu-element
))))
1192 (defun recentf-open-files-items (files)
1193 "Return a list of widgets to display FILES in a dialog buffer."
1194 (set (make-local-variable 'recentf--files-with-key
)
1195 (recentf-trunc-list files
10))
1196 (mapcar 'recentf-open-files-item
1198 ;; When requested group the files with shortcuts together
1199 ;; at the top of the list.
1200 (when recentf-show-file-shortcuts-flag
1201 (setq files
(nthcdr 10 files
))
1202 (recentf-apply-menu-filter
1203 'recentf-show-digit-shortcut-filter
1204 (mapcar 'recentf-make-default-menu-element
1205 recentf--files-with-key
)))
1206 ;; Then the other files.
1207 (recentf-apply-menu-filter
1209 (mapcar 'recentf-make-default-menu-element
1212 (defun recentf-open-files (&optional files buffer-name
)
1213 "Show a dialog to open a recent file.
1214 If optional argument FILES is non-nil, it is a list of recently-opened
1215 files to choose from. It defaults to the whole recent list.
1216 If optional argument BUFFER-NAME is non-nil, it is a buffer name to
1217 use for the dialog. It defaults to \"*`recentf-menu-title'*\"."
1219 (unless (or files recentf-list
)
1220 (error "There is no recent file to open"))
1221 (recentf-dialog (or buffer-name
(format "*%s*" recentf-menu-title
))
1222 (widget-insert "Click on a file"
1223 (if recentf-show-file-shortcuts-flag
1224 ", or type the corresponding digit key,"
1227 "Click on Cancel or type `q' to cancel.\n")
1228 ;; Use a L&F that looks like the recentf menu.
1229 (tree-widget-set-theme "folder")
1230 (apply 'widget-create
1234 ,@(recentf-open-files-items (or files recentf-list
))))
1237 :notify
'recentf-cancel-dialog
1239 (recentf-dialog-goto-first 'link
)))
1241 (defun recentf-open-more-files ()
1242 "Show a dialog to open a recent file that is not in the menu."
1244 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list
)
1245 (format "*%s - More*" recentf-menu-title
)))
1247 (defun recentf-open-most-recent-file (&optional n
)
1248 "Open the Nth most recent file.
1249 Optional argument N must be a valid digit number. It defaults to 1.
1250 1 opens the most recent file, 2 the second most recent one, etc..
1251 0 opens the tenth most recent file."
1254 ((zerop n
) (setq n
10))
1255 ((and (> n
0) (< n
10)))
1256 ((error "Recent file number out of range [0-9], %d" n
)))
1257 (let ((file (nth (1- n
) (or recentf--files-with-key recentf-list
))))
1258 (unless file
(error "Not that many recent files"))
1259 ;; Close the open files dialog.
1260 (when recentf--files-with-key
1261 (kill-buffer (current-buffer)))
1262 (funcall recentf-menu-action file
)))
1264 ;;; Save/load/cleanup the recent list
1266 (defconst recentf-save-file-header
1267 ";;; Automatically generated by `recentf' on %s.\n"
1268 "Header to be written into the `recentf-save-file'.")
1270 (defconst recentf-save-file-coding-system
1271 (if (coding-system-p 'utf-8-emacs
)
1274 "Coding system of the file `recentf-save-file'.")
1276 (defun recentf-save-list ()
1277 "Save the recent list.
1278 Write data into the file specified by `recentf-save-file'."
1280 (condition-case error
1283 (set-buffer-file-coding-system recentf-save-file-coding-system
)
1284 (insert (format recentf-save-file-header
(current-time-string)))
1285 (recentf-dump-variable 'recentf-list recentf-max-saved-items
)
1286 (recentf-dump-variable 'recentf-filter-changer-current
)
1287 (insert "\n\f\n;; Local Variables:\n"
1288 (format ";; coding: %s\n" recentf-save-file-coding-system
)
1290 (write-file (expand-file-name recentf-save-file
))
1291 (when recentf-save-file-modes
1292 (set-file-modes recentf-save-file recentf-save-file-modes
))
1295 (warn "recentf mode: %s" (error-message-string error
)))))
1297 (defun recentf-load-list ()
1298 "Load a previously saved recent list.
1299 Read data from the file specified by `recentf-save-file'.
1300 When `recentf-initialize-file-name-history' is non-nil, initialize an
1301 empty `file-name-history' with the recent list."
1303 (let ((file (expand-file-name recentf-save-file
)))
1304 (when (file-readable-p file
)
1306 (and recentf-initialize-file-name-history
1307 (not file-name-history
)
1308 (setq file-name-history
(mapcar 'abbreviate-file-name
1311 (defun recentf-cleanup ()
1312 "Cleanup the recent list.
1313 That is, remove duplicates, non-kept, and excluded files."
1315 (message "Cleaning up the recentf list...")
1317 (ht (make-hash-table
1318 :size recentf-max-saved-items
1321 (dolist (f recentf-list
)
1322 (setq f
(recentf-expand-file-name f
)
1323 key
(if recentf-case-fold-search
(downcase f
) f
))
1324 (if (and (recentf-include-p f
)
1326 (not (gethash key ht
)))
1331 (message "File %s removed from the recentf list" f
)))
1332 (message "Cleaning up the recentf list...done (%d removed)" n
)
1333 (setq recentf-list
(nreverse newlist
))))
1337 (defvar recentf-mode-map
(make-sparse-keymap)
1338 "Keymap to use in recentf mode.")
1341 (define-minor-mode recentf-mode
1342 "Toggle \"Open Recent\" menu (Recentf mode).
1343 With a prefix argument ARG, enable Recentf mode if ARG is
1344 positive, and disable it otherwise. If called from Lisp, enable
1345 Recentf mode if ARG is omitted or nil.
1347 When Recentf mode is enabled, a \"Open Recent\" submenu is
1348 displayed in the \"File\" menu, containing a list of files that
1349 were operated on recently."
1352 :keymap recentf-mode-map
1353 (unless (and recentf-mode
(recentf-enabled-p))
1357 (recentf-show-menu))
1359 (recentf-save-list))
1360 (recentf-auto-cleanup)
1361 (let ((hook-setup (if recentf-mode
'add-hook
'remove-hook
)))
1362 (dolist (hook recentf-used-hooks
)
1363 (apply hook-setup hook
)))))
1365 (defun recentf-unload-function ()
1366 "Unload the recentf library."
1368 ;; continue standard unloading
1373 (run-hooks 'recentf-load-hook
)
1375 ;;; recentf.el ends here