1 ;;; recentf.el --- setup a menu of recently opened files
3 ;; Copyright (C) 1999-2017 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 <https://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
#o600
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
)
1068 "Keymap used in recentf dialogs.")
1070 (define-derived-mode recentf-dialog-mode nil
"recentf-dialog"
1071 "Major mode of recentf dialogs.
1073 \\{recentf-dialog-mode-map}"
1076 (setq truncate-lines t
))
1078 (defmacro recentf-dialog
(name &rest forms
)
1079 "Show a dialog buffer with NAME, setup with FORMS."
1080 (declare (indent 1) (debug t
))
1081 `(with-current-buffer (get-buffer-create ,name
)
1083 (let ((inhibit-read-only t
)
1084 (ol (overlay-lists)))
1085 (mapc 'delete-overlay
(car ol
))
1086 (mapc 'delete-overlay
(cdr ol
))
1088 (recentf-dialog-mode)
1091 (switch-to-buffer (current-buffer))))
1093 ;;; Edit list dialog
1095 (defvar recentf-edit-list nil
)
1097 (defun recentf-edit-list-select (widget &rest _ignore
)
1098 "Toggle a file selection based on the checkbox WIDGET state.
1099 IGNORE other arguments."
1100 (let ((value (widget-get widget
:tag
))
1101 (check (widget-value widget
)))
1103 (add-to-list 'recentf-edit-list value
)
1104 (setq recentf-edit-list
(delq value recentf-edit-list
)))
1105 (message "%s %sselected" value
(if check
"" "un"))))
1107 (defun recentf-edit-list-validate (&rest _ignore
)
1108 "Process the recent list when the edit list dialog is committed.
1110 (if recentf-edit-list
1112 (dolist (e recentf-edit-list
)
1113 (setq recentf-list
(delq e recentf-list
)
1115 (kill-buffer (current-buffer))
1116 (message "%S file(s) removed from the list" i
))
1117 (message "No file selected")))
1119 (defun recentf-edit-list ()
1120 "Show a dialog to delete selected files from the recent list."
1122 (unless recentf-list
1123 (error "The list of recent files is empty"))
1124 (recentf-dialog (format "*%s - Edit list*" recentf-menu-title
)
1125 (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 ;; Override the (problematic) follow-link property of the
1191 ;; `link' widget (bug#22434).
1193 ,(cdr menu-element
))))
1195 (defun recentf-open-files-items (files)
1196 "Return a list of widgets to display FILES in a dialog buffer."
1197 (set (make-local-variable 'recentf--files-with-key
)
1198 (recentf-trunc-list files
10))
1199 (mapcar 'recentf-open-files-item
1201 ;; When requested group the files with shortcuts together
1202 ;; at the top of the list.
1203 (when recentf-show-file-shortcuts-flag
1204 (setq files
(nthcdr 10 files
))
1205 (recentf-apply-menu-filter
1206 'recentf-show-digit-shortcut-filter
1207 (mapcar 'recentf-make-default-menu-element
1208 recentf--files-with-key
)))
1209 ;; Then the other files.
1210 (recentf-apply-menu-filter
1212 (mapcar 'recentf-make-default-menu-element
1215 (defun recentf-open-files (&optional files buffer-name
)
1216 "Show a dialog to open a recent file.
1217 If optional argument FILES is non-nil, it is a list of recently-opened
1218 files to choose from. It defaults to the whole recent list.
1219 If optional argument BUFFER-NAME is non-nil, it is a buffer name to
1220 use for the dialog. It defaults to \"*`recentf-menu-title'*\"."
1222 (unless (or files recentf-list
)
1223 (error "There is no recent file to open"))
1224 (recentf-dialog (or buffer-name
(format "*%s*" recentf-menu-title
))
1225 (widget-insert "Click on a file"
1226 (if recentf-show-file-shortcuts-flag
1227 ", or type the corresponding digit key,"
1230 (format-message "Click on Cancel or type `q' to cancel.\n"))
1231 ;; Use a L&F that looks like the recentf menu.
1232 (tree-widget-set-theme "folder")
1233 (apply 'widget-create
1237 ,@(recentf-open-files-items (or files recentf-list
))))
1240 :notify
'recentf-cancel-dialog
1242 (recentf-dialog-goto-first 'link
)))
1244 (defun recentf-open-more-files ()
1245 "Show a dialog to open a recent file that is not in the menu."
1247 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list
)
1248 (format "*%s - More*" recentf-menu-title
)))
1250 (defun recentf-open-most-recent-file (&optional n
)
1251 "Open the Nth most recent file.
1252 Optional argument N must be a valid digit number. It defaults to 1.
1253 1 opens the most recent file, 2 the second most recent one, etc..
1254 0 opens the tenth most recent file."
1257 ((zerop n
) (setq n
10))
1258 ((and (> n
0) (< n
10)))
1259 ((error "Recent file number out of range [0-9], %d" n
)))
1260 (let ((file (nth (1- n
) (or recentf--files-with-key recentf-list
))))
1261 (unless file
(error "Not that many recent files"))
1262 ;; Close the open files dialog.
1263 (when recentf--files-with-key
1264 (kill-buffer (current-buffer)))
1265 (funcall recentf-menu-action file
)))
1267 ;;; Save/load/cleanup the recent list
1269 (defconst recentf-save-file-header
1270 ";;; Automatically generated by `recentf' on %s.\n"
1271 "Header to be written into the `recentf-save-file'.")
1273 (defconst recentf-save-file-coding-system
1274 (if (coding-system-p 'utf-8-emacs
)
1277 "Coding system of the file `recentf-save-file'.")
1279 (defun recentf-save-list ()
1280 "Save the recent list.
1281 Write data into the file specified by `recentf-save-file'."
1283 (condition-case error
1286 (set-buffer-file-coding-system recentf-save-file-coding-system
)
1287 (insert (format-message recentf-save-file-header
1288 (current-time-string)))
1289 (recentf-dump-variable 'recentf-list recentf-max-saved-items
)
1290 (recentf-dump-variable 'recentf-filter-changer-current
)
1291 (insert "\n\f\n;; Local Variables:\n"
1292 (format ";; coding: %s\n" recentf-save-file-coding-system
)
1294 (write-file (expand-file-name recentf-save-file
))
1295 (when recentf-save-file-modes
1296 (set-file-modes recentf-save-file recentf-save-file-modes
))
1299 (warn "recentf mode: %s" (error-message-string error
)))))
1301 (defun recentf-load-list ()
1302 "Load a previously saved recent list.
1303 Read data from the file specified by `recentf-save-file'.
1304 When `recentf-initialize-file-name-history' is non-nil, initialize an
1305 empty `file-name-history' with the recent list."
1307 (let ((file (expand-file-name recentf-save-file
))
1308 ;; We do not want Tramp asking for passwords.
1310 (when (file-readable-p file
)
1312 (and recentf-initialize-file-name-history
1313 (not file-name-history
)
1314 (setq file-name-history
(mapcar 'abbreviate-file-name
1317 (defun recentf-cleanup ()
1318 "Cleanup the recent list.
1319 That is, remove duplicates, non-kept, and excluded files."
1321 (message "Cleaning up the recentf list...")
1323 (ht (make-hash-table
1324 :size recentf-max-saved-items
1327 (dolist (f recentf-list
)
1328 (setq f
(recentf-expand-file-name f
)
1329 key
(if recentf-case-fold-search
(downcase f
) f
))
1330 (if (and (recentf-include-p f
)
1332 (not (gethash key ht
)))
1337 (message "File %s removed from the recentf list" f
)))
1338 (message "Cleaning up the recentf list...done (%d removed)" n
)
1339 (setq recentf-list
(nreverse newlist
))))
1343 (defvar recentf-mode-map
(make-sparse-keymap)
1344 "Keymap to use in recentf mode.")
1347 (define-minor-mode recentf-mode
1348 "Toggle \"Open Recent\" menu (Recentf mode).
1349 With a prefix argument ARG, enable Recentf mode if ARG is
1350 positive, and disable it otherwise. If called from Lisp, enable
1351 Recentf mode if ARG is omitted or nil.
1353 When Recentf mode is enabled, a \"Open Recent\" submenu is
1354 displayed in the \"File\" menu, containing a list of files that
1355 were operated on recently."
1358 :keymap recentf-mode-map
1359 (unless (and recentf-mode
(recentf-enabled-p))
1363 (recentf-show-menu))
1365 (recentf-save-list))
1366 (recentf-auto-cleanup)
1367 (let ((hook-setup (if recentf-mode
'add-hook
'remove-hook
)))
1368 (dolist (hook recentf-used-hooks
)
1369 (apply hook-setup hook
)))))
1371 (defun recentf-unload-function ()
1372 "Unload the recentf library."
1374 ;; continue standard unloading
1379 (run-hooks 'recentf-load-hook
)
1381 ;;; recentf.el ends here