1 ;;; recentf.el --- setup a menu of recently opened files
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: David Ponce <david@dponce.com>
7 ;; Created: July 19 1999
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published
14 ;; by the Free Software Foundation; either version 2, or (at your
15 ;; option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This package maintains a menu for visiting files that were operated
30 ;; on recently. When enabled a new "Open Recent" sub menu is
31 ;; displayed in the "Files" menu. The recent files list is
32 ;; automatically saved across Emacs sessions. You can customize the
33 ;; number of recent files displayed, the location of the menu and
34 ;; others options (see the source code for details).
41 (require 'tree-widget)
46 (defvar recentf-list nil
47 "List of recently opened files.")
49 (defvar recentf-data-cache nil
50 "Cache of data used to build the recentf menu.
51 The menu is rebuilt when this data has changed.")
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 "~/.recentf"
73 "*File to save the recent list into."
77 (defcustom recentf-save-file-modes 384 ;; 0600
78 "Mode bits of recentf save file, as an integer, or nil.
79 If non-nil, after writing `recentf-save-file', set its mode bits to
80 this value. By default give R/W access only to the user who owns that
81 file. See also the function `set-file-modes'."
83 :type '(choice (const :tag "Don't change" nil)
86 (defcustom recentf-exclude nil
87 "*List of regexps and predicates for filenames excluded from the recent list.
88 When a filename matches any of the regexps or satisfies any of the
89 predicates it is excluded from the recent list.
90 A predicate is a function that is passed a filename to check and that
91 must return non-nil to exclude it."
93 :type '(repeat (choice regexp function)))
95 (defcustom recentf-keep
97 "*List of regexps and predicates for filenames kept in the recent list.
98 Regexps and predicates are tried in the specified order.
99 When nil all filenames are kept in the recent list.
100 When a filename matches any of the regexps or satisfies any of the
101 predicates it is kept in the recent list.
102 The default is to keep readable files.
103 A predicate is a function that is passed a filename to check and that
104 must return non-nil to keep it. For example, you can add the
105 `file-remote-p' predicate in front of this list to keep remote file
106 names in the recent list without checking their readability through a
109 :type '(repeat (choice regexp function)))
111 (defun recentf-menu-customization-changed (variable value)
112 "Function called when the recentf menu customization has changed.
113 Set VARIABLE with VALUE, and force a rebuild of the recentf menu."
114 (when (featurep 'recentf)
115 ;; Unavailable until recentf has been loaded.
116 (recentf-clear-data))
117 (set-default variable value))
119 (defcustom recentf-menu-title "Open Recent"
120 "*Name of the recentf menu."
123 :set 'recentf-menu-customization-changed)
125 (defcustom recentf-menu-path '("File")
126 "*Path where to add the recentf menu.
127 If nil add it at top level (see also `easy-menu-add-item')."
129 :type '(choice (const :tag "Top Level" nil)
130 (sexp :tag "Menu Path"))
131 :set 'recentf-menu-customization-changed)
133 (defcustom recentf-menu-before "Open File..."
134 "*Name of the menu before which the recentf menu will be added.
135 If nil add it at end of menu (see also `easy-menu-add-item')."
137 :type '(choice (string :tag "Name")
138 (const :tag "Last" nil))
139 :set 'recentf-menu-customization-changed)
141 (defcustom recentf-menu-action 'find-file
142 "*Function to invoke with a filename item of the recentf menu.
143 The default is to call `find-file' to edit the selected file."
146 :set 'recentf-menu-customization-changed)
148 (defcustom recentf-max-menu-items 10
149 "*Maximum number of items in the recentf menu."
152 :set 'recentf-menu-customization-changed)
154 (defcustom recentf-menu-filter nil
155 "*Function used to filter files displayed in the recentf menu.
156 A nil value means no filter. The following functions are predefined:
158 - `recentf-sort-ascending'
159 Sort menu items in ascending order.
160 - `recentf-sort-descending'
161 Sort menu items in descending order.
162 - `recentf-sort-basenames-ascending'
163 Sort menu items by filenames sans directory in ascending order.
164 - `recentf-sort-basenames-descending'
165 Sort menu items by filenames sans directory in descending order.
166 - `recentf-sort-directories-ascending'
167 Sort menu items by directories in ascending order.
168 - `recentf-sort-directories-descending'
169 Sort menu items by directories in descending order.
170 - `recentf-show-basenames'
171 Show filenames sans directory in menu items.
172 - `recentf-show-basenames-ascending'
173 Show filenames sans directory in ascending order.
174 - `recentf-show-basenames-descending'
175 Show filenames sans directory in descending order.
176 - `recentf-relative-filter'
177 Show filenames relative to `default-directory'.
178 - `recentf-arrange-by-rule'
179 Show sub-menus following user defined rules.
180 - `recentf-arrange-by-mode'
181 Show a sub-menu for each major mode.
182 - `recentf-arrange-by-dir'
183 Show a sub-menu for each directory.
184 - `recentf-filter-changer'
185 Manage a ring of filters.
187 The filter function is called with one argument, the list of menu
188 elements used to build the menu and must return a new list of menu
189 elements (see `recentf-make-menu-element' for menu element form)."
191 :type '(radio (const nil)
192 (function-item recentf-sort-ascending)
193 (function-item recentf-sort-descending)
194 (function-item recentf-sort-basenames-ascending)
195 (function-item recentf-sort-basenames-descending)
196 (function-item recentf-sort-directories-ascending)
197 (function-item recentf-sort-directories-descending)
198 (function-item recentf-show-basenames)
199 (function-item recentf-show-basenames-ascending)
200 (function-item recentf-show-basenames-descending)
201 (function-item recentf-relative-filter)
202 (function-item recentf-arrange-by-rule)
203 (function-item recentf-arrange-by-mode)
204 (function-item recentf-arrange-by-dir)
205 (function-item recentf-filter-changer)
207 :set 'recentf-menu-customization-changed)
209 (defcustom recentf-menu-append-commands-flag t
210 "*Non-nil means to append command items to the menu."
213 :set 'recentf-menu-customization-changed)
215 (define-obsolete-variable-alias 'recentf-menu-append-commands-p
216 'recentf-menu-append-commands-flag
219 (defcustom recentf-auto-cleanup 'mode
220 "*Define when to automatically cleanup the recent list.
221 The following values can be set:
224 Cleanup when turning the mode on (default).
226 Never cleanup the list automatically.
228 Cleanup each time Emacs has been idle that number of seconds.
230 Cleanup at specified time string, for example at \"11:00pm\".
232 Setting this variable directly does not take effect;
235 See also the command `recentf-cleanup', that can be used to manually
238 :type '(radio (const :tag "When mode enabled"
242 (number :tag "When idle that seconds"
244 (string :tag "At time"
246 :set (lambda (variable value)
247 (set-default variable value)
248 (when (featurep 'recentf)
249 ;; Unavailable until recentf has been loaded.
250 (recentf-auto-cleanup))))
252 (defcustom recentf-initialize-file-name-history t
253 "*Non-nil means to initialize `file-name-history' with the recent list.
254 If `file-name-history' is not empty, do nothing."
258 (defcustom recentf-load-hook nil
259 "*Normal hook run at end of loading the `recentf' package."
263 (defcustom recentf-filename-handler nil
264 "Function to call to process filename handled by recentf.
265 It is passed a filename to give a chance to transform it.
266 If it returns nil, the filename is left unchanged."
268 :type '(choice (const :tag "None" nil)
269 (const abbreviate-file-name)
272 (defcustom recentf-show-file-shortcuts-flag t
273 "Whether to show ``[N]'' for the Nth item up to 10.
274 If non-nil, `recentf-open-files' will show labels for keys that can be
275 used as shortcuts to open the Nth file."
282 (defconst recentf-case-fold-search
283 (memq system-type '(vax-vms windows-nt cygwin))
284 "Non-nil if recentf searches and matches should ignore case.")
286 (defsubst recentf-string-equal (s1 s2)
287 "Return non-nil if strings S1 and S2 have identical contents.
288 Ignore case if `recentf-case-fold-search' is non-nil."
289 (if recentf-case-fold-search
290 (string-equal (downcase s1) (downcase s2))
291 (string-equal s1 s2)))
293 (defsubst recentf-string-lessp (s1 s2)
294 "Return non-nil if string S1 is less than S2 in lexicographic order.
295 Ignore case if `recentf-case-fold-search' is non-nil."
296 (if recentf-case-fold-search
297 (string-lessp (downcase s1) (downcase s2))
298 (string-lessp s1 s2)))
300 (defun recentf-string-member (elt list)
301 "Return non-nil if ELT is an element of LIST.
302 The value is actually the tail of LIST whose car is ELT.
303 ELT must be a string and LIST a list of strings.
304 Ignore case if `recentf-case-fold-search' is non-nil."
305 (while (and list (not (recentf-string-equal elt (car list))))
306 (setq list (cdr list)))
309 (defsubst recentf-trunc-list (l n)
310 "Return from L the list of its first N elements."
312 (while (and l (> n 0))
313 (setq nl (cons (car l) nl)
318 (defun recentf-dump-variable (variable &optional limit)
319 "Insert a \"(setq VARIABLE value)\" in the current buffer.
320 When the value of VARIABLE is a list, optional argument LIMIT
321 specifies a maximum number of elements to insert. By default insert
323 (let ((value (symbol-value variable)))
325 (insert (format "\n(setq %S %S)\n" variable value))
326 (when (and (integerp limit) (> limit 0))
327 (setq value (recentf-trunc-list value limit)))
328 (insert (format "\n(setq %S\n '(" variable))
330 (insert (format "\n %S" e)))
331 (insert "\n ))\n"))))
333 (defvar recentf-auto-cleanup-timer nil
334 "Timer used to automatically cleanup the recent list.
335 See also the option `recentf-auto-cleanup'.")
337 (defun recentf-auto-cleanup ()
338 "Automatic cleanup of the recent list."
339 (when (timerp recentf-auto-cleanup-timer)
340 (cancel-timer recentf-auto-cleanup-timer))
342 (setq recentf-auto-cleanup-timer
344 ((eq 'mode recentf-auto-cleanup)
347 ((numberp recentf-auto-cleanup)
349 recentf-auto-cleanup t 'recentf-cleanup))
350 ((stringp recentf-auto-cleanup)
352 recentf-auto-cleanup nil 'recentf-cleanup))))))
356 (defsubst recentf-push (filename)
357 "Push FILENAME into the recent list, if it isn't there yet.
358 If it is there yet, move it at the beginning of the list.
359 If `recentf-case-fold-search' is non-nil, ignore case when comparing
361 (let ((m (recentf-string-member filename recentf-list)))
362 (and m (setq recentf-list (delq (car m) recentf-list)))
363 (push filename recentf-list)))
365 (defsubst recentf-expand-file-name (name)
366 "Convert filename NAME to absolute, and canonicalize it.
367 See also the function `expand-file-name'.
368 If defined, call the function `recentf-filename-handler'
369 to post process the canonical name."
370 (let* ((filename (expand-file-name name)))
371 (or (and recentf-filename-handler
372 (funcall recentf-filename-handler filename))
375 (defun recentf-include-p (filename)
376 "Return non-nil if FILENAME should be included in the recent list.
377 That is, if it doesn't match any of the `recentf-exclude' checks."
378 (let ((case-fold-search recentf-case-fold-search)
379 (checks recentf-exclude)
381 (while (and checks keepit)
382 (setq keepit (condition-case nil
383 (not (if (stringp (car checks))
385 (string-match (car checks) filename)
387 (funcall (car checks) filename)))
389 checks (cdr checks)))
392 (defun recentf-keep-p (filename)
393 "Return non-nil if FILENAME should be kept in the recent list.
394 That is, if it matches any of the `recentf-keep' checks."
395 (let* ((case-fold-search recentf-case-fold-search)
396 (checks recentf-keep)
397 (keepit (null checks)))
398 (while (and checks (not keepit))
399 (setq keepit (condition-case nil
400 (if (stringp (car checks))
402 (string-match (car checks) filename)
404 (funcall (car checks) filename))
406 checks (cdr checks)))
409 (defsubst recentf-add-file (filename)
410 "Add or move FILENAME at the beginning of the recent list.
411 Does nothing if the name satisfies any of the `recentf-exclude'
412 regexps or predicates."
413 (setq filename (recentf-expand-file-name filename))
414 (when (recentf-include-p filename)
415 (recentf-push filename)))
417 (defsubst recentf-remove-if-non-kept (filename)
418 "Remove FILENAME from the recent list, if file is not kept.
419 Return non-nil if FILENAME has been removed."
420 (unless (recentf-keep-p filename)
421 (let ((m (recentf-string-member
422 (recentf-expand-file-name filename) recentf-list)))
423 (and m (setq recentf-list (delq (car m) recentf-list))))))
425 (defsubst recentf-directory-compare (f1 f2)
426 "Compare absolute filenames F1 and F2.
427 First compare directories, then filenames sans directory.
428 Return non-nil if F1 is less than F2."
429 (let ((d1 (file-name-directory f1))
430 (d2 (file-name-directory f2)))
431 (if (recentf-string-equal d1 d2)
432 (recentf-string-lessp (file-name-nondirectory f1)
433 (file-name-nondirectory f2))
434 (recentf-string-lessp d1 d2))))
438 (defvar recentf-menu-items-for-commands
439 (list ["Cleanup list"
441 :help "Remove all excluded and non-kept files from the recent list"
445 :help "Edit the files that are kept in the recent list"
449 :help "Save the list of recently opened files now"
452 (customize-group "recentf")
453 :help "Customize recently opened files menu and options"
456 "List of menu items for recentf commands.")
458 (defvar recentf-menu-filter-commands nil
459 "This variable can be used by menu filters to setup their own command menu.
460 If non-nil it must contain a list of valid menu-items to be appended
461 to the recent file list part of the menu. Before calling a menu
462 filter function this variable is reset to nil.")
464 (defsubst recentf-elements (n)
465 "Return a list of the first N elements of the recent list."
466 (recentf-trunc-list recentf-list n))
468 (defsubst recentf-make-menu-element (menu-item menu-value)
469 "Create a new menu-element.
470 A menu element is a pair (MENU-ITEM . MENU-VALUE), where MENU-ITEM is
471 the menu item string displayed. MENU-VALUE is the file to be open
472 when the corresponding MENU-ITEM is selected. Or it is a
473 pair (SUB-MENU-TITLE . MENU-ELEMENTS) where SUB-MENU-TITLE is a
474 sub-menu title and MENU-ELEMENTS is the list of menu elements in the
476 (cons menu-item menu-value))
478 (defsubst recentf-menu-element-item (e)
479 "Return the item part of the menu-element E."
482 (defsubst recentf-menu-element-value (e)
483 "Return the value part of the menu-element E."
486 (defsubst recentf-set-menu-element-item (e item)
487 "Change the item part of menu-element E to ITEM."
490 (defsubst recentf-set-menu-element-value (e value)
491 "Change the value part of menu-element E to VALUE."
494 (defsubst recentf-sub-menu-element-p (e)
495 "Return non-nil if menu-element E defines a sub-menu."
496 (consp (recentf-menu-element-value e)))
498 (defsubst recentf-make-default-menu-element (file)
499 "Make a new default menu element with FILE.
500 This a menu element (FILE . FILE)."
501 (recentf-make-menu-element file file))
503 (defsubst recentf-menu-elements (n)
504 "Return a list of the first N default menu elements from the recent list.
505 See also `recentf-make-default-menu-element'."
506 (mapcar 'recentf-make-default-menu-element
507 (recentf-elements n)))
509 (defun recentf-apply-menu-filter (filter l)
510 "Apply function FILTER to the list of menu-elements L.
511 It takes care of sub-menu elements in L and recursively apply FILTER
512 to them. It is guaranteed that FILTER receives only a list of single
513 menu-elements (no sub-menu)."
514 (if (and l (functionp filter))
515 (let ((case-fold-search recentf-case-fold-search)
517 ;; split L into two sub-listes, one of sub-menus elements and
518 ;; another of single menu elements.
520 (if (recentf-sub-menu-element-p elt)
523 ;; Apply FILTER to single elements.
525 (setq others (funcall filter (nreverse others))))
526 ;; Apply FILTER to sub-menu elements.
529 (recentf-set-menu-element-value
530 elt (recentf-apply-menu-filter
531 filter (recentf-menu-element-value elt)))
533 ;; Return the new filtered menu element list.
537 (defun recentf-make-menu-items ()
538 "Make menu items from the recent list."
539 (setq recentf-menu-filter-commands nil)
541 (mapcar 'recentf-make-menu-item
542 (recentf-apply-menu-filter
544 (recentf-menu-elements recentf-max-menu-items)))))
545 (append (or file-items (list ["No files" t
546 :help "No recent file to open"
548 (and (< recentf-max-menu-items (length recentf-list))
549 (list ["More..." recentf-open-more-files
550 :help "Open files that are not in the menu"
552 (and recentf-menu-filter-commands
554 recentf-menu-filter-commands))
555 (and recentf-menu-append-commands-flag
557 recentf-menu-items-for-commands)))))
559 (defsubst recentf-make-menu-item (elt)
560 "Make a menu item from menu element ELT."
561 (let ((item (recentf-menu-element-item elt))
562 (value (recentf-menu-element-value elt)))
563 (if (recentf-sub-menu-element-p elt)
564 (cons item (mapcar 'recentf-make-menu-item value))
565 (vector item (list recentf-menu-action value)
566 :help (concat "Open " value)
569 (defsubst recentf-menu-bar ()
570 "Return the keymap of the global menu bar."
571 (lookup-key global-map [menu-bar]))
573 (defun recentf-clear-data ()
574 "Clear data used to build the recentf menu.
575 This forces a rebuild of the menu."
576 (easy-menu-remove-item (recentf-menu-bar)
577 recentf-menu-path recentf-menu-title)
578 (setq recentf-data-cache nil))
580 ;;; Predefined menu filters
582 (defsubst recentf-sort-ascending (l)
583 "Sort the list of menu elements L in ascending order.
584 The MENU-ITEM part of each menu element is compared."
585 (sort (copy-sequence l)
587 (recentf-string-lessp
588 (recentf-menu-element-item e1)
589 (recentf-menu-element-item e2)))))
591 (defsubst recentf-sort-descending (l)
592 "Sort the list of menu elements L in descending order.
593 The MENU-ITEM part of each menu element is compared."
594 (sort (copy-sequence l)
596 (recentf-string-lessp
597 (recentf-menu-element-item e2)
598 (recentf-menu-element-item e1)))))
600 (defsubst recentf-sort-basenames-ascending (l)
601 "Sort the list of menu elements L in ascending order.
602 Only filenames sans directory are compared."
603 (sort (copy-sequence l)
605 (recentf-string-lessp
606 (file-name-nondirectory (recentf-menu-element-value e1))
607 (file-name-nondirectory (recentf-menu-element-value e2))))))
609 (defsubst recentf-sort-basenames-descending (l)
610 "Sort the list of menu elements L in descending order.
611 Only filenames sans directory are compared."
612 (sort (copy-sequence l)
614 (recentf-string-lessp
615 (file-name-nondirectory (recentf-menu-element-value e2))
616 (file-name-nondirectory (recentf-menu-element-value e1))))))
618 (defsubst recentf-sort-directories-ascending (l)
619 "Sort the list of menu elements L in ascending order.
620 Compares directories then filenames to order the list."
621 (sort (copy-sequence l)
623 (recentf-directory-compare
624 (recentf-menu-element-value e1)
625 (recentf-menu-element-value e2)))))
627 (defsubst recentf-sort-directories-descending (l)
628 "Sort the list of menu elements L in descending order.
629 Compares directories then filenames to order the list."
630 (sort (copy-sequence l)
632 (recentf-directory-compare
633 (recentf-menu-element-value e2)
634 (recentf-menu-element-value e1)))))
636 (defun recentf-show-basenames (l &optional no-dir)
637 "Filter the list of menu elements L to show filenames sans directory.
638 When a filename is duplicated, it is appended a sequence number if
639 optional argument NO-DIR is non-nil, or its directory otherwise."
640 (let (filtered-names filtered-list full name counters sufx)
641 (dolist (elt l (nreverse filtered-list))
642 (setq full (recentf-menu-element-value elt)
643 name (file-name-nondirectory full))
644 (if (not (member name filtered-names))
645 (push name filtered-names)
647 (if (setq sufx (assoc name counters))
648 (setcdr sufx (1+ (cdr sufx)))
650 (push (cons name sufx) counters))
651 (setq sufx (file-name-directory full)))
652 (setq name (format "%s(%s)" name sufx)))
653 (push (recentf-make-menu-element name full) filtered-list))))
655 (defsubst recentf-show-basenames-ascending (l)
656 "Filter the list of menu elements L to show filenames sans directory.
657 Filenames are sorted in ascending order.
658 This filter combines the `recentf-sort-basenames-ascending' and
659 `recentf-show-basenames' filters."
660 (recentf-show-basenames (recentf-sort-basenames-ascending l)))
662 (defsubst recentf-show-basenames-descending (l)
663 "Filter the list of menu elements L to show filenames sans directory.
664 Filenames are sorted in descending order.
665 This filter combines the `recentf-sort-basenames-descending' and
666 `recentf-show-basenames' filters."
667 (recentf-show-basenames (recentf-sort-basenames-descending l)))
669 (defun recentf-relative-filter (l)
670 "Filter the list of menu-elements L to show relative filenames.
671 Filenames are relative to the `default-directory'."
672 (mapcar #'(lambda (menu-element)
673 (let* ((ful (recentf-menu-element-value menu-element))
674 (rel (file-relative-name ful default-directory)))
675 (if (string-match "^\\.\\." rel)
677 (recentf-make-menu-element rel ful))))
680 ;;; Rule based menu filters
682 (defcustom recentf-arrange-rules
684 ("Elisp files (%d)" ".\\.el$")
685 ("Java files (%d)" ".\\.java$")
686 ("C/C++ files (%d)" "c\\(pp\\)?$")
688 "*List of rules used by `recentf-arrange-by-rule' to build sub-menus.
689 A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
690 displayed title of the sub-menu where a '%d' `format' pattern is
691 replaced by the number of items in the sub-menu. MATCHER is a regexp
692 or a list of regexps. Items matching one of the regular expressions in
693 MATCHER are added to the corresponding sub-menu."
694 :group 'recentf-filters
695 :type '(repeat (cons string (repeat regexp)))
696 :set 'recentf-menu-customization-changed)
698 (defcustom recentf-arrange-by-rule-others "Other files (%d)"
699 "*Title of the `recentf-arrange-by-rule' sub-menu.
700 This is for the menu where items that don't match any
701 `recentf-arrange-rules' are displayed. If nil these items are
702 displayed in the main recent files menu. A '%d' `format' pattern in
703 the title is replaced by the number of items in the sub-menu."
704 :group 'recentf-filters
705 :type '(choice (const :tag "Main menu" nil)
706 (string :tag "Title"))
707 :set 'recentf-menu-customization-changed)
709 (defcustom recentf-arrange-by-rules-min-items 0
710 "*Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
711 If the number of items in a sub-menu is less than this value the
712 corresponding sub-menu items are displayed in the main recent files
713 menu or in the `recentf-arrange-by-rule-others' sub-menu if
715 :group 'recentf-filters
717 :set 'recentf-menu-customization-changed)
719 (defcustom recentf-arrange-by-rule-subfilter nil
720 "*Function called by a rule based filter to filter sub-menu elements.
721 A nil value means no filter. See also `recentf-menu-filter'.
722 You can't use another rule based filter here."
723 :group 'recentf-filters
724 :type '(choice (const nil) function)
725 :set (lambda (variable value)
726 (when (memq value '(recentf-arrange-by-rule
727 recentf-arrange-by-mode
728 recentf-arrange-by-dir))
729 (error "Recursive use of a rule based filter"))
730 (recentf-menu-customization-changed variable value)))
732 (defun recentf-match-rule-p (matcher filename)
733 "Return non-nil if the rule specified by MATCHER match FILENAME.
734 See `recentf-arrange-rules' for details on MATCHER."
735 (if (stringp matcher)
736 (string-match matcher filename)
737 (while (and (consp matcher)
738 (not (string-match (car matcher) filename)))
739 (setq matcher (cdr matcher)))
742 (defun recentf-arrange-by-rule (l)
743 "Filter the list of menu-elements L.
744 Arrange them in sub-menus following rules in `recentf-arrange-rules'."
745 (if (not recentf-arrange-rules)
747 (let ((menus (mapcar #'(lambda (r) (list (car r)))
748 recentf-arrange-rules))
749 menu others min file rules elts count)
751 (setq file (recentf-menu-element-value elt)
752 rules recentf-arrange-rules
755 (while (and (not menu) rules)
756 (when (recentf-match-rule-p (cdar rules) file)
757 (setq menu (car elts))
758 (recentf-set-menu-element-value
759 menu (cons elt (recentf-menu-element-value menu))))
760 (setq rules (cdr rules)
766 min (if (natnump recentf-arrange-by-rules-min-items)
767 recentf-arrange-by-rules-min-items 0))
769 (when (setq elts (recentf-menu-element-value menu))
770 (setq count (length elts))
772 (setq others (nconc elts others))
773 (recentf-set-menu-element-item
774 menu (format (recentf-menu-element-item menu) count))
775 (recentf-set-menu-element-value
776 menu (recentf-apply-menu-filter
777 recentf-arrange-by-rule-subfilter (nreverse elts)))
780 (if (and (stringp recentf-arrange-by-rule-others) others)
783 (recentf-make-menu-element
784 (format recentf-arrange-by-rule-others (length others))
785 (recentf-apply-menu-filter
786 recentf-arrange-by-rule-subfilter (nreverse others)))
790 (recentf-apply-menu-filter
791 recentf-arrange-by-rule-subfilter (nreverse others)))))
794 ;;; Predefined rule based menu filters
796 (defun recentf-build-mode-rules ()
797 "Convert `auto-mode-alist' to menu filter rules.
798 Rules obey `recentf-arrange-rules' format."
799 (let ((case-fold-search recentf-case-fold-search)
800 regexp rule-name rule rules)
801 (dolist (mode auto-mode-alist)
802 (setq regexp (car mode)
805 (setq rule-name (symbol-name mode))
806 (if (string-match "\\(.*\\)-mode$" rule-name)
807 (setq rule-name (match-string 1 rule-name)))
808 (setq rule-name (concat rule-name " (%d)")
809 rule (assoc rule-name rules))
811 (setcdr rule (cons regexp (cdr rule)))
812 (push (list rule-name regexp) rules))))
813 ;; It is important to preserve auto-mode-alist order
814 ;; to ensure the right file <-> mode association
817 (defun recentf-arrange-by-mode (l)
818 "Split the list of menu-elements L into sub-menus by major mode."
819 (let ((recentf-arrange-rules (recentf-build-mode-rules))
820 (recentf-arrange-by-rule-others "others (%d)"))
821 (recentf-arrange-by-rule l)))
823 (defun recentf-build-dir-rules (l)
824 "Convert directories in menu-elements L to menu filter rules.
825 Rules obey `recentf-arrange-rules' format."
827 (mapcar #'(lambda (e)
828 (let ((dir (file-name-directory
829 (recentf-menu-element-value e))))
830 (or (recentf-string-member dir dirs)
833 (mapcar #'(lambda (d)
834 (cons (concat d " (%d)")
836 (nreverse (sort dirs 'recentf-string-lessp)))))
838 (defun recentf-file-name-nondir (l)
839 "Filter the list of menu-elements L to show filenames sans directory.
840 This simplified version of `recentf-show-basenames' does not handle
841 duplicates. It is used by `recentf-arrange-by-dir' as its
842 `recentf-arrange-by-rule-subfilter'."
843 (mapcar #'(lambda (e)
844 (recentf-make-menu-element
845 (file-name-nondirectory (recentf-menu-element-value e))
846 (recentf-menu-element-value e)))
849 (defun recentf-arrange-by-dir (l)
850 "Split the list of menu-elements L into sub-menus by directory."
851 (let ((recentf-arrange-rules (recentf-build-dir-rules l))
852 (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
853 recentf-arrange-by-rule-others)
854 (nreverse (recentf-arrange-by-rule l))))
856 ;;; Ring of menu filters
858 (defvar recentf-filter-changer-state nil
859 "Used by `recentf-filter-changer' to hold its state.")
861 (defcustom recentf-filter-changer-alist
863 (recentf-arrange-by-mode . "*Files by Mode*")
864 (recentf-arrange-by-dir . "*Files by Directory*")
865 (recentf-arrange-by-rule . "*Files by User Rule*")
867 "*List of filters managed by `recentf-filter-changer'.
868 Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is
869 the filter function, and LABEL is the menu item displayed to select
871 :group 'recentf-filters
872 :type '(repeat (cons function string))
873 :set (lambda (variable value)
874 (setq recentf-filter-changer-state nil)
875 (recentf-menu-customization-changed variable value)))
877 (defun recentf-filter-changer-goto-next ()
878 "Go to the next filter available.
879 See `recentf-filter-changer'."
880 (setq recentf-filter-changer-state (cdr recentf-filter-changer-state))
881 (recentf-clear-data))
883 (defsubst recentf-filter-changer-get-current ()
884 "Get the current filter available.
885 See `recentf-filter-changer'."
886 (unless recentf-filter-changer-state
887 (setq recentf-filter-changer-state recentf-filter-changer-alist))
888 (car recentf-filter-changer-state))
890 (defsubst recentf-filter-changer-get-next ()
891 "Get the next filter available.
892 See `recentf-filter-changer'."
893 ;; At this point the current filter is the first element of
894 ;; `recentf-filter-changer-state'.
895 (car (or (cdr recentf-filter-changer-state)
896 ;; There is no next element in
897 ;; `recentf-filter-changer-state', so loop back to the
898 ;; first element of `recentf-filter-changer-alist'.
899 recentf-filter-changer-alist)))
901 (defun recentf-filter-changer (l)
902 "Manage a ring of menu filters.
903 `recentf-filter-changer-alist' defines the filters in the ring.
904 Filtering of L is delegated to the current filter in the ring. A
905 filter menu item is displayed allowing to dynamically activate the
906 next filter in the ring. If the filter ring is empty, L is left
908 (let ((filter (recentf-filter-changer-get-current)))
910 (setq l (recentf-apply-menu-filter (car filter) l)
911 filter (recentf-filter-changer-get-next))
913 (setq recentf-menu-filter-commands
914 (list (vector (cdr filter)
915 '(recentf-filter-changer-goto-next)
919 ;;; Common dialog stuff
921 (defun recentf-cancel-dialog (&rest ignore)
922 "Cancel the current dialog.
925 (kill-buffer (current-buffer))
926 (message "Dialog canceled"))
928 (defun recentf-dialog-goto-first (widget-type)
929 "Move the cursor to the first WIDGET-TYPE in current dialog.
930 Go to the beginning of buffer if not found."
931 (goto-char (point-min))
936 (if (eq widget-type (widget-type (widget-at (point))))
939 (goto-char (point-min))))
941 (defvar recentf-dialog-mode-map
942 (let ((km (make-sparse-keymap)))
943 (set-keymap-parent km widget-keymap)
944 (define-key km "q" 'recentf-cancel-dialog)
945 (define-key km [down-mouse-1] 'widget-button-click)
946 ;; Keys in reverse order of appearence in help.
947 (dolist (k '("0" "9" "8" "7" "6" "5" "4" "3" "2" "1"))
948 (define-key km k 'recentf-open-file-with-key))
950 "Keymap used in recentf dialogs.")
952 (define-derived-mode recentf-dialog-mode nil "recentf-dialog"
953 "Major mode of recentf dialogs.
955 \\{recentf-dialog-mode-map}"
958 (setq truncate-lines t))
960 (defmacro recentf-dialog (name &rest forms)
961 "Show a dialog buffer with NAME, setup with FORMS."
962 (declare (indent 1) (debug t))
963 `(with-current-buffer (get-buffer-create ,name)
965 (let ((inhibit-read-only t)
966 (ol (overlay-lists)))
967 (mapc 'delete-overlay (car ol))
968 (mapc 'delete-overlay (cdr ol))
970 (recentf-dialog-mode)
973 (switch-to-buffer (current-buffer))))
977 (defun recentf-track-opened-file ()
978 "Insert the name of the file just opened or written into the recent list."
979 (and buffer-file-name
980 (recentf-add-file buffer-file-name))
981 ;; Must return nil because it is run from `write-file-functions'.
984 (defun recentf-track-closed-file ()
985 "Update the recent list when a buffer is killed.
986 That is, remove a non kept file from the recent list."
987 (and buffer-file-name
988 (recentf-remove-if-non-kept buffer-file-name)))
990 (defun recentf-update-menu ()
991 "Update the recentf menu from the current recent list."
992 (let ((cache (cons default-directory recentf-list)))
993 ;; Does nothing, if nothing has changed.
994 (unless (equal recentf-data-cache cache)
995 (setq recentf-data-cache cache)
998 (recentf-menu-bar) recentf-menu-path
999 (easy-menu-create-menu recentf-menu-title
1000 (recentf-make-menu-items))
1001 recentf-menu-before)
1003 (message "recentf update menu failed: %s"
1004 (error-message-string err)))))))
1006 (defconst recentf-used-hooks
1008 (find-file-hook recentf-track-opened-file)
1009 (write-file-functions recentf-track-opened-file)
1010 (kill-buffer-hook recentf-track-closed-file)
1011 (menu-bar-update-hook recentf-update-menu)
1012 (kill-emacs-hook recentf-save-list)
1014 "Hooks used by recentf.")
1016 (defsubst recentf-enabled-p ()
1017 "Return non-nil if recentf mode is currently enabled."
1018 (memq 'recentf-update-menu menu-bar-update-hook))
1023 ;;; Edit list dialog
1025 (defvar recentf-edit-list nil)
1027 (defun recentf-edit-list-select (widget &rest ignore)
1028 "Toggle a file selection based on the checkbox WIDGET state.
1029 IGNORE other arguments."
1030 (let ((value (widget-get widget :tag))
1031 (check (widget-value widget)))
1033 (add-to-list 'recentf-edit-list value)
1034 (setq recentf-edit-list (delq value recentf-edit-list)))
1035 (message "%s %sselected" value (if check "" "un"))))
1037 (defun recentf-edit-list-validate (&rest ignore)
1038 "Process the recent list when the edit list dialog is committed.
1040 (if recentf-edit-list
1042 (dolist (e recentf-edit-list)
1043 (setq recentf-list (delq e recentf-list)
1045 (kill-buffer (current-buffer))
1046 (message "%S file(s) removed from the list" i)
1047 (recentf-clear-data))
1048 (message "No file selected")))
1050 (defun recentf-edit-list ()
1051 "Show a dialog to delete selected files from the recent list."
1053 (recentf-dialog (format "*%s - Edit list*" recentf-menu-title)
1054 (set (make-local-variable 'recentf-edit-list) nil)
1056 "Click on OK to delete selected files from the recent list.
1057 Click on Cancel or type `q' to cancel.\n")
1058 ;; Insert the list of files as checkboxes
1059 (dolist (item recentf-list)
1060 (widget-create 'checkbox
1061 :value nil ; unselected checkbox
1062 :format "\n %[%v%] %t"
1064 :notify 'recentf-edit-list-select))
1065 (widget-insert "\n\n")
1068 :notify 'recentf-edit-list-validate
1069 :help-echo "Delete selected files from the recent list"
1074 :notify 'recentf-cancel-dialog
1076 (recentf-dialog-goto-first 'checkbox)))
1078 ;;; Open file dialog
1080 (defun recentf-open-files-action (widget &rest ignore)
1081 "Open the file stored in WIDGET's value when notified.
1082 IGNORE other arguments."
1083 (kill-buffer (current-buffer))
1084 (funcall recentf-menu-action (widget-value widget)))
1086 ;; List of files associated to a digit shortcut key.
1087 (defvar recentf--files-with-key nil)
1089 (defun recentf-show-digit-shortcut-filter (l)
1090 "Filter the list of menu-elements L to show digit shortcuts."
1094 (recentf-set-menu-element-item
1095 e (format "[%d] %s" (% i 10) (recentf-menu-element-item e))))
1098 (defun recentf-open-files-item (menu-element)
1099 "Return a widget to display MENU-ELEMENT in a dialog buffer."
1100 (if (consp (cdr menu-element))
1101 ;; Represent a sub-menu with a tree widget
1105 :node (item :tag ,(car menu-element)
1107 :format "%{%t%}:\n")
1108 ,@(mapcar 'recentf-open-files-item
1109 (cdr menu-element)))
1110 ;; Represent a single file with a link widget
1111 `(link :tag ,(car menu-element)
1114 :button-face default
1116 :help-echo ,(concat "Open " (cdr menu-element))
1117 :action recentf-open-files-action
1118 ,(cdr menu-element))))
1120 (defun recentf-open-files-items (files)
1121 "Return a list of widgets to display FILES in a dialog buffer."
1122 (set (make-local-variable 'recentf--files-with-key)
1123 (recentf-trunc-list files 10))
1124 (mapcar 'recentf-open-files-item
1126 ;; When requested group the files with shortcuts together
1127 ;; at the top of the list.
1128 (when recentf-show-file-shortcuts-flag
1129 (setq files (nthcdr 10 files))
1130 (recentf-apply-menu-filter
1131 'recentf-show-digit-shortcut-filter
1132 (mapcar 'recentf-make-default-menu-element
1133 recentf--files-with-key)))
1134 ;; Then the other files.
1135 (recentf-apply-menu-filter
1137 (mapcar 'recentf-make-default-menu-element
1140 (defun recentf-open-files (&optional files buffer-name)
1141 "Show a dialog to open a recent file.
1142 If optional argument FILES is non-nil, it is a list of recently-opened
1143 files to choose from. It defaults to the whole recent list.
1144 If optional argument BUFFER-NAME is non-nil, it is a buffer name to
1145 use for the dialog. It defaults to \"*`recentf-menu-title'*\"."
1147 (recentf-dialog (or buffer-name (format "*%s*" recentf-menu-title))
1148 (widget-insert "Click on a file"
1149 (if recentf-show-file-shortcuts-flag
1150 ", or type the corresponding digit key,"
1153 "Click on Cancel or type `q' to cancel.\n")
1154 ;; Use a L&F that looks like the recentf menu.
1155 (tree-widget-set-theme "folder")
1156 (apply 'widget-create
1160 ,@(recentf-open-files-items (or files recentf-list))))
1163 :notify 'recentf-cancel-dialog
1165 (recentf-dialog-goto-first 'link)))
1167 (defun recentf-open-file-with-key (n)
1168 "Open the recent file with the shortcut numeric key N.
1169 N must be a valid digit.
1170 `1' opens the first file, `2' the second file, ... `9' the ninth file.
1171 `0' opens the tenth file."
1174 (let ((n (string-to-number (this-command-keys))))
1177 ((and (> n 0) (< n 10)) n)
1178 ((error "Invalid digit key %d" n))))))
1179 (when recentf--files-with-key
1180 (let ((file (nth (1- n) recentf--files-with-key)))
1181 (unless file (error "Not that many recent files"))
1182 (kill-buffer (current-buffer))
1183 (funcall recentf-menu-action file))))
1185 (defun recentf-open-more-files ()
1186 "Show a dialog to open a recent file that is not in the menu."
1188 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
1189 (format "*%s - More*" recentf-menu-title)))
1191 ;;; Save/load/cleanup the recent list
1193 (defconst recentf-save-file-header
1194 ";;; Automatically generated by `recentf' on %s.\n"
1195 "Header to be written into the `recentf-save-file'.")
1197 (defconst recentf-save-file-coding-system
1198 (if (coding-system-p 'utf-8-emacs)
1201 "Coding system of the file `recentf-save-file'.")
1203 (defun recentf-save-list ()
1204 "Save the recent list.
1205 Write data into the file specified by `recentf-save-file'."
1207 (condition-case error
1210 (set-buffer-file-coding-system recentf-save-file-coding-system)
1211 (insert (format recentf-save-file-header (current-time-string)))
1212 (recentf-dump-variable 'recentf-list recentf-max-saved-items)
1213 (recentf-dump-variable 'recentf-filter-changer-state)
1214 (insert "\n
\f\n;;; Local Variables:\n"
1215 (format ";;; coding: %s\n" recentf-save-file-coding-system)
1217 (write-file (expand-file-name recentf-save-file))
1218 (when recentf-save-file-modes
1219 (set-file-modes recentf-save-file recentf-save-file-modes))
1222 (warn "recentf mode: %s" (error-message-string error)))))
1224 (defun recentf-load-list ()
1225 "Load a previously saved recent list.
1226 Read data from the file specified by `recentf-save-file'.
1227 When `recentf-initialize-file-name-history' is non-nil, initialize an
1228 empty `file-name-history' with the recent list."
1230 (let ((file (expand-file-name recentf-save-file)))
1231 (when (file-readable-p file)
1233 (and recentf-initialize-file-name-history
1234 (not file-name-history)
1235 (setq file-name-history (mapcar 'abbreviate-file-name
1238 (defun recentf-cleanup ()
1239 "Remove all non-kept and excluded files from the recent list."
1241 (message "Cleaning up the recentf list...")
1242 (let ((n 0) newlist)
1243 (dolist (f recentf-list)
1244 (if (and (recentf-include-p f)
1248 (message "File %s removed from the recentf list" f)))
1249 (message "Cleaning up the recentf list...done (%d removed)" n)
1250 (setq recentf-list (nreverse newlist))))
1253 (define-minor-mode recentf-mode
1254 "Toggle recentf mode.
1255 With prefix argument ARG, turn on if positive, otherwise off.
1256 Returns non-nil if the new state is enabled.
1258 When recentf mode is enabled, it maintains a menu for visiting files
1259 that were operated on recently."
1262 (unless (and recentf-mode (recentf-enabled-p))
1265 (recentf-save-list))
1266 (recentf-auto-cleanup)
1267 (recentf-clear-data)
1268 (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
1269 (dolist (hook recentf-used-hooks)
1270 (apply hook-setup hook)))
1271 (run-hooks 'recentf-mode-hook)
1272 (when (interactive-p)
1273 (message "Recentf mode %sabled" (if recentf-mode "en" "dis"))))
1278 (run-hooks 'recentf-load-hook)
1280 ;; arch-tag: 78f1eec9-0d16-4d19-a4eb-2e4529edb62a
1281 ;;; recentf.el ends here