1 ;; recentf.el --- setup a menu of recently opened files
3 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Created: July 19 1999
7 ;; Keywords: customization
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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This package maintains a menu for visiting files that were operated
29 ;; on recently. When enabled a new "Open Recent" submenu is displayed
30 ;; in the "Files" menu. The recent files list is automatically saved
31 ;; across Emacs sessions. You can customize the number of recent
32 ;; files displayed, the location of the menu and others options (see
33 ;; the source code for details). To install and use, put the file on
34 ;; your Emacs-Lisp load path and add the following into your ~/.emacs
45 (defconst recentf-save-file-header
46 ";;; Automatically generated by `recentf' on %s.\n"
47 "Header to be written into the `recentf-save-file'.")
49 (defvar recentf-list nil
50 "List of recently opened files.")
52 (defvar recentf-update-menu-p t
53 "Non-nil if the recentf menu must be updated.")
55 (defvar recentf-initialized-p nil
56 "Non-nil if recentf already initialized.")
58 ;; IMPORTANT: This function must be defined before the following defcustoms
59 ;; because it is used in their :set clause. To avoid byte-compiler warnings
60 ;; the `symbol-value' function is used to access the `recentf-menu-path'
61 ;; and `recentf-menu-title' values.
62 (defun recentf-menu-customization-changed (sym val
)
63 "Function called when menu customization has changed.
64 It removes the recentf menu and forces its complete redrawing."
65 (when recentf-initialized-p
66 (easy-menu-remove-item nil
67 (symbol-value 'recentf-menu-path
)
68 (symbol-value 'recentf-menu-title
))
69 (setq recentf-update-menu-p t
))
70 (custom-set-default sym val
))
73 "Maintain a menu of recently opened files."
77 (defgroup recentf-filters nil
78 "Group to customize recentf menu filters.
79 You should define the options of your own filters in this group."
82 (defcustom recentf-max-saved-items
20
83 "*Maximum number of items saved to `recentf-save-file'."
87 (defcustom recentf-save-file
(expand-file-name "~/.recentf")
88 "*File to save `recentf-list' into."
92 (defcustom recentf-exclude nil
93 "*List of regexps for filenames excluded from `recentf-list'."
95 :type
'(repeat regexp
))
97 (defcustom recentf-menu-title
"Open Recent"
98 "*Name of the recentf menu."
101 :set
'recentf-menu-customization-changed
)
103 (defcustom recentf-menu-path
'("files")
104 "*Path where to add the recentf menu.
105 If nil add it at top-level (see also `easy-menu-change')."
107 :type
'(choice (const :tag
"Top Level" nil
)
108 (sexp :tag
"Menu Path"))
109 :set
'recentf-menu-customization-changed
)
111 (defcustom recentf-menu-before
"open-file"
112 "*Name of the menu before which the recentf menu will be added.
113 If nil add it at end of menu (see also `easy-menu-change')."
115 :type
'(choice (string :tag
"Name")
116 (const :tag
"Last" nil
))
117 :set
'recentf-menu-customization-changed
)
119 (defcustom recentf-menu-action
'recentf-find-file
120 "*Function to invoke with a filename item of the recentf menu.
121 The default action `recentf-find-file' calls `find-file' to edit an
122 existing file. If the file does not exist or is not readable, it is
123 not edited and its name is removed from `recentf-list'. You can use
124 `find-file' instead to open non-existing files and keep them in the
125 list of recently opened files."
128 :set
'recentf-menu-customization-changed
)
130 (defcustom recentf-max-menu-items
10
131 "*Maximum number of items in the recentf menu."
134 :set
'recentf-menu-customization-changed
)
136 (defcustom recentf-menu-filter nil
137 "*Function used to filter files displayed in the recentf menu.
138 Nil means no filter. The following functions are predefined:
140 - - `recentf-sort-ascending' to sort menu items in ascending order.
141 - - `recentf-sort-descending' to sort menu items in descending order.
142 - - `recentf-sort-basenames-ascending' to sort file names in descending order.
143 - - `recentf-sort-basenames-descending' to sort file names in descending order.
144 - - `recentf-sort-directories-ascending' to sort directories in ascending order.
145 - - `recentf-sort-directories-descending' to sort directories in descending order.
146 - - `recentf-show-basenames' to show file names (no directories) in menu items.
147 - - `recentf-show-basenames-ascending' to show file names in ascending order.
148 - - `recentf-show-basenames-descending' to show file names in descending order.
149 - - `recentf-relative-filter' to show file names relative to `default-directory'.
150 - - `recentf-arrange-by-rule' to show sub-menus following user defined rules.
151 - - `recentf-arrange-by-mode' to show a sub-menu for each major mode.
152 - - `recentf-arrange-by-dir' to show a sub-menu for each directory.
153 - - `recentf-filter-changer' to manage a ring of filters.
155 The filter function is called with one argument, the list of menu elements
156 used to build the menu and must return a new list of menu elements (see
157 `recentf-make-menu-element' for menu element form)."
160 :set
'recentf-menu-customization-changed
)
162 (defcustom recentf-menu-append-commands-p t
163 "*If not-nil command items are appended to the menu."
166 :set
'recentf-menu-customization-changed
)
168 (defcustom recentf-keep-non-readable-files-p nil
169 "*If nil (default), non-readable files are not kept in `recentf-list'."
173 :initialize
'custom-initialize-default
174 :set
(lambda (sym val
)
176 (remove-hook 'kill-buffer-hook
'recentf-remove-file-hook
)
177 (add-hook 'kill-buffer-hook
'recentf-remove-file-hook
))
178 (custom-set-default sym val
)))
180 (defcustom recentf-mode nil
181 "Toggle recentf mode.
182 When recentf mode is enabled, it maintains a menu for visiting files that
183 were operated on recently.
184 Setting this variable directly does not take effect;
185 use either \\[customize] or the function `recentf-mode'."
186 :set
(lambda (symbol value
)
187 (recentf-mode (or value
0)))
188 :initialize
'custom-initialize-default
193 (defcustom recentf-load-hook nil
194 "*Normal hook run at end of loading the `recentf' package."
201 (defconst recentf-case-fold-search
202 (memq system-type
'(vax-vms windows-nt
))
203 "Non-nil if recentf searches and matches should ignore case.")
205 (defun recentf-include-p (filename)
206 "Return t if FILENAME matches none of the `recentf-exclude' regexps."
207 (let ((case-fold-search recentf-case-fold-search
)
208 (rl recentf-exclude
))
209 (while (and rl
(not (string-match (car rl
) filename
)))
213 (defun recentf-add-file (filename)
214 "Add or move FILENAME at the beginning of `recentf-list'.
215 Does nothing if FILENAME matches one of the `recentf-exclude' regexps."
216 (let ((filename (expand-file-name filename
)))
217 (when (recentf-include-p filename
)
218 (setq recentf-list
(cons filename
(delete filename recentf-list
)))
219 (setq recentf-update-menu-p t
))))
221 (defun recentf-remove-if-non-readable (filename)
222 "Remove FILENAME from `recentf-list' if not readable."
223 (unless (file-readable-p filename
)
224 (setq recentf-list
(delete filename recentf-list
))
225 (setq recentf-update-menu-p t
)))
227 (defun recentf-find-file (filename)
228 "Edit file FILENAME using `find-file'.
229 If FILENAME is not readable it is removed from `recentf-list'."
230 (if (file-readable-p filename
)
233 (message "File `%s' not found." filename
)
234 (setq recentf-list
(delete filename recentf-list
))
235 (setq recentf-update-menu-p t
))))
237 (defun recentf-trunc-list (l n
)
238 "Return a list of the first N elements of L."
240 (while (and l
(> n
0))
241 (setq lh
(cons (car l
) lh
))
246 (defun recentf-elements (n)
247 "Return a list of the first N elements of `recentf-list'."
248 (recentf-trunc-list recentf-list n
))
250 (defun recentf-make-menu-element (menu-item menu-value
)
251 "Create a new menu-element.
253 A menu element is a pair (MENU-ITEM . MENU-VALUE) where:
255 - - MENU-ITEM is the menu item string displayed.
256 - - MENU-VALUE is the path used to open the file when the
257 corresponding MENU-ITEM is selected. Or it is
258 a pair (SUB-MENU-TITLE . MENU-ELEMENTS) where
259 SUB-MENU-TITLE is a sub-menu title and
260 MENU-ELEMENTS is the list of menu elements in
262 (cons menu-item menu-value
))
264 (defun recentf-menu-element-item (e)
265 "Return the item part of the menu-element E."
268 (defun recentf-menu-element-value (e)
269 "Return the value part of the menu-element E."
272 (defun recentf-set-menu-element-item (e item
)
273 "Change the item part of menu-element E to ITEM."
276 (defun recentf-set-menu-element-value (e value
)
277 "Change the value part of menu-element E to VALUE."
280 (defun recentf-sub-menu-element-p (e)
281 "Return non-nil if menu-element E defines a sub-menu."
282 (consp (recentf-menu-element-value e
)))
284 (defun recentf-make-default-menu-element (file-path)
285 "Make a new default menu element (MENU-ITEM . MENU-VALUE) for the
286 given recent file path FILE-PATH. MENU-ITEM and MENU-VALUE are set to
287 FILE-PATH. See also `recentf-make-menu-element'."
288 (recentf-make-menu-element file-path file-path
))
290 (defun recentf-menu-elements (n)
291 "Return a list of the first N default menu elements from
292 `recentf-list'. See also `recentf-make-default-menu-element'."
293 (mapcar 'recentf-make-default-menu-element
294 (recentf-elements n
)))
296 (defun recentf-apply-menu-filter (filter l
)
297 "Convenient funtion to apply the function FILTER to the list of
298 menu-elements L. It takes care of sub-menu elements in L and
299 recursively apply FILTER to them. It is guarantee than FILTER receives
300 only a list of single menu-elements (no sub-menu)."
301 (if (and (functionp filter
) l
)
302 (let ((case-fold-search recentf-case-fold-search
)
303 menu-element sub-menu-elements single-elements
)
304 ;; split L in two sub-listes:
305 ;; one of sub-menus elements and
306 ;; one of single menu elements
308 (setq menu-element
(car l
))
309 (if (recentf-sub-menu-element-p menu-element
)
310 (setq sub-menu-elements
311 (cons menu-element sub-menu-elements
))
312 (setq single-elements
313 (cons menu-element single-elements
)))
315 ;; apply FILTER to the list of single menu elements
317 (setq single-elements
(funcall filter
318 (nreverse single-elements
))))
319 ;; apply FILTER to sub-menu menu element list
320 (setq l sub-menu-elements
)
321 (setq sub-menu-elements nil
)
323 (setq menu-element
(car l
))
324 (recentf-set-menu-element-value
326 (recentf-apply-menu-filter
328 (recentf-menu-element-value menu-element
)))
329 (setq sub-menu-elements
(cons menu-element sub-menu-elements
))
331 ;; build and return the new filtered menu element list
332 (nconc sub-menu-elements single-elements
))
335 (defvar recentf-menu-items-for-commands
336 (list ["Cleanup list" recentf-cleanup t
]
337 ["Edit list..." recentf-edit-list t
]
338 ["Save list now" recentf-save-list t
]
339 (vector "Recentf Options..." '(customize-group "recentf") t
))
340 "List of menu items for recentf commands.")
342 (defvar recentf-menu-filter-commands nil
343 "This variable can be used by menu filters to setup their own command menu.
345 If non-nil it must contain a list of valid menu-items to be appended
346 to the recent file list part of the menu. Before calling a menu
347 filter function this variable is reset to nil.")
349 (defun recentf-make-menu-items ()
350 "Make menu items from `recentf-list'."
351 (setq recentf-menu-filter-commands nil
)
353 (mapcar 'recentf-make-menu-item
354 (recentf-apply-menu-filter
356 (recentf-menu-elements recentf-max-menu-items
)))))
357 (append (or file-items
(list ["No files" t nil
]))
358 (and (< recentf-max-menu-items
(length recentf-list
))
359 (list ["More..." recentf-open-more-files t
]))
360 (and recentf-menu-filter-commands
362 recentf-menu-filter-commands
))
363 (and recentf-menu-append-commands-p
365 recentf-menu-items-for-commands
)))))
367 (defun recentf-make-menu-item (menu-element)
368 "Make a menu item from a menu element (see `recentf-make-menu-element')."
369 (let ((menu-item (recentf-menu-element-item menu-element
))
370 (menu-value (recentf-menu-element-value menu-element
)))
371 (if (recentf-sub-menu-element-p menu-element
)
372 (cons menu-item
(mapcar 'recentf-make-menu-item menu-value
))
374 (list recentf-menu-action menu-value
)
378 ;;; Predefined menu filter functions
381 (defun recentf-sort-ascending (l)
382 "Sort the list of menu elements L in ascending order.
383 The MENU-ITEM part of each menu element is compared."
384 (sort (copy-sequence l
)
387 (string-lessp (recentf-menu-element-item e1
)
388 (recentf-menu-element-item e2
))))))
390 (defun recentf-sort-descending (l)
391 "Sort the list of menu elements L in descending order.
392 The MENU-ITEM part of each menu element is compared."
393 (sort (copy-sequence l
)
396 (string-lessp (recentf-menu-element-item e2
)
397 (recentf-menu-element-item e1
))))))
399 (defun recentf-sort-basenames-ascending (l)
400 "Sort the list of menu elements L in ascending order.
401 Only file names (without directories) are compared."
402 (sort (copy-sequence l
)
406 (file-name-nondirectory (recentf-menu-element-value e1
))
407 (file-name-nondirectory (recentf-menu-element-value e2
)))))))
409 (defun recentf-sort-basenames-descending (l)
410 "Sort the list of menu elements L in descending order.
411 Only file names (without directories) are compared."
412 (sort (copy-sequence l
)
416 (file-name-nondirectory (recentf-menu-element-value e2
))
417 (file-name-nondirectory (recentf-menu-element-value e1
)))))))
419 (defun recentf-directory-compare (p1 p2
)
420 "Compare directories then filenames in pathes P1 and P2 and return
421 non-nil if P1 is less than P2."
422 (let ((d1 (file-name-directory p1
))
423 (f1 (file-name-nondirectory p1
))
424 (d2 (file-name-directory p2
))
425 (f2 (file-name-nondirectory p2
)))
428 (string-lessp d1 d2
))))
430 (defun recentf-sort-directories-ascending (l)
431 "Sort the list of menu elements L in ascending order.
432 Compares directories then filenames to order the list."
433 (sort (copy-sequence l
)
436 (recentf-directory-compare (recentf-menu-element-value e1
)
437 (recentf-menu-element-value e2
))))))
439 (defun recentf-sort-directories-descending (l)
440 "Sort the list of menu elements L in descending order.
441 Compares directories then filenames to order the list."
442 (sort (copy-sequence l
)
445 (recentf-directory-compare (recentf-menu-element-value e2
)
446 (recentf-menu-element-value e1
))))))
448 (defun recentf-show-basenames (l)
449 "Filter the list of menu elements L to show only file names (no directories)
450 in the menu. When file names are duplicated their directory component is added."
451 (let ((names (mapcar (function
453 (file-name-nondirectory
454 (recentf-menu-element-value item
))))
456 (dirs (mapcar (function
459 (recentf-menu-element-value item
))))
461 (pathes (mapcar 'recentf-menu-element-value l
))
463 item filtered-items filtered-list
)
465 (setq item
(car names
))
466 (setq names
(cdr names
))
469 (cons (recentf-make-menu-element
470 (if (or (member item names
) (member item filtered-items
))
471 (concat item
" (" (nth pos dirs
) ")")
475 (setq filtered-items
(cons item filtered-items
)))
476 (nreverse filtered-list
)))
478 (defun recentf-show-basenames-ascending (l)
479 "Filter the list of menu elements L to show only file names in the menu,
480 sorted in ascending order. This filter combines the `recentf-sort-basenames-ascending'
481 and `recentf-show-basenames' filters."
482 (recentf-show-basenames (recentf-sort-basenames-ascending l
)))
484 (defun recentf-show-basenames-descending (l)
485 "Filter the list of menu elements L to show only file names in the menu,
486 sorted in descending order. This filter combines the `recentf-sort-basenames-descending'
487 and `recentf-show-basenames' filters."
488 (recentf-show-basenames (recentf-sort-basenames-descending l
)))
490 (defun recentf-relative-filter (l)
491 "Filter the list of `recentf-menu-elements' L to show filenames
492 relative to `default-directory'."
493 (setq recentf-update-menu-p t
) ; force menu update
495 (lambda (menu-element)
496 (let* ((ful-path (recentf-menu-element-value menu-element
))
497 (rel-path (file-relative-name ful-path
)))
498 (if (string-match "^\\.\\." rel-path
)
500 (recentf-make-menu-element rel-path ful-path
)))))
503 (defcustom recentf-arrange-rules
505 ("Elisp files (%d)" ".\\.el$")
506 ("Java files (%d)" ".\\.java$")
507 ("C/C++ files (%d)" "c\\(pp\\)?$")
509 "*List of rules used by `recentf-arrange-by-rule' to build sub-menus.
510 A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
511 displayed title of the sub-menu where a '%d' `format' pattern is
512 replaced by the number of items in the sub-menu. MATCHER is a regexp
513 or a list of regexps. Items matching one of the regular expressions in
514 MATCHER are added to the corresponding sub-menu."
515 :group
'recentf-filters
516 :type
'(repeat (cons string
(repeat regexp
)))
517 :set
'recentf-menu-customization-changed
)
519 (defcustom recentf-arrange-by-rule-others
"Other files (%d)"
520 "*Title of the `recentf-arrange-by-rule' sub-menu where items that
521 don't match any `recentf-arrange-rules' are displayed. If nil
522 these items are displayed in the main recent files menu. A '%d'
523 `format' pattern in the title is replaced by the number of items in
525 :group
'recentf-filters
526 :type
'(choice (const :tag
"Main menu" nil
)
527 (string :tag
"Title"))
528 :set
'recentf-menu-customization-changed
)
530 (defcustom recentf-arrange-by-rules-min-items
0
531 "*Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
532 If the number of items in a sub-menu is less than this value the
533 corresponding sub-menu items are displayed in the main recent files
534 menu or in the `recentf-arrange-by-rule-others' sub-menu if
536 :group
'recentf-filters
538 :set
'recentf-menu-customization-changed
)
540 (defcustom recentf-arrange-by-rule-subfilter nil
541 "*Function used by `recentf-arrange-by-rule' to filter sub-menu elements.
542 Nil means no filter. See also `recentf-menu-filter'. You can't use
543 `recentf-arrange-by-rule' itself here!"
544 :group
'recentf-filters
546 :set
(lambda (sym val
)
547 (if (eq val
'recentf-arrange-by-rule
)
548 (error "Can't use `recentf-arrange-by-rule' itself here!")
549 (recentf-menu-customization-changed sym val
))))
551 (defun recentf-match-rule-p (matcher file-path
)
552 "Return non-nil if FILE-PATH match the rule specified by MATCHER.
553 See `recentf-arrange-rules' for details on MATCHER."
554 (if (stringp matcher
)
555 (string-match matcher file-path
)
556 (while (and (consp matcher
)
557 (not (string-match (car matcher
) file-path
)))
558 (setq matcher
(cdr matcher
)))
561 (defun recentf-arrange-by-rule (l)
562 "Filter the list of menu-elements L to arrange them in sub-menus
563 following rules in `recentf-arrange-rules'."
564 (let ((sub-menus-number (length recentf-arrange-rules
)))
565 (if (> sub-menus-number
0)
566 (let ((sub-menus (apply 'vector
570 recentf-arrange-rules
)))
571 other-menu-elements index min-size
)
573 (let* ((menu-element (car l
))
574 (file-path (recentf-menu-element-value menu-element
))
575 (rules recentf-arrange-rules
)
578 (while (and (not found
) rules
)
579 (if (recentf-match-rule-p (cdar rules
) file-path
)
580 (let ((sub-menu (aref sub-menus index
)))
582 (recentf-set-menu-element-value
584 (cons menu-element
(recentf-menu-element-value sub-menu
)))
586 (setq index
(1+ index
))
587 (setq rules
(cdr rules
)))
589 (setq other-menu-elements
590 (cons menu-element other-menu-elements
)))
594 (setq min-size
(if (integerp recentf-arrange-by-rules-min-items
)
595 (max 0 recentf-arrange-by-rules-min-items
)
597 (while (< index sub-menus-number
)
598 (let* ((sub-menu (aref sub-menus index
))
599 (sub-menu-title (recentf-menu-element-item sub-menu
))
600 (sub-menu-elements (recentf-menu-element-value sub-menu
))
601 (sub-menu-length (length sub-menu-elements
)))
602 (if (> sub-menu-length
0)
604 ((< sub-menu-length min-size
)
605 (setq other-menu-elements
606 (nconc sub-menu-elements other-menu-elements
)))
607 ((>= sub-menu-length min-size
)
608 (recentf-set-menu-element-item
610 (format sub-menu-title sub-menu-length
))
611 (recentf-set-menu-element-value
613 (recentf-apply-menu-filter
614 recentf-arrange-by-rule-subfilter
615 (nreverse sub-menu-elements
)))
616 (setq l
(cons sub-menu l
)))))
617 (setq index
(1+ index
))))
618 (if (and (stringp recentf-arrange-by-rule-others
)
622 (cons (recentf-make-menu-element
623 (format recentf-arrange-by-rule-others
624 (length other-menu-elements
))
625 (recentf-apply-menu-filter
626 recentf-arrange-by-rule-subfilter
627 (nreverse other-menu-elements
)))
629 (setq l
(nconc (nreverse l
)
630 (recentf-apply-menu-filter
631 recentf-arrange-by-rule-subfilter
632 (nreverse other-menu-elements
)))))))
635 (defun recentf-build-mode-rules ()
636 "Convert `auto-mode-alist' to `recentf-arrange-rules' format."
637 (let ((case-fold-search recentf-case-fold-search
)
638 (modes auto-mode-alist
)
639 regexp mode rule-name rule rules
)
641 (setq regexp
(caar modes
))
642 (setq mode
(cdar modes
))
644 (setq rule-name
(symbol-name mode
))
645 (if (string-match "\\(.*\\)-mode$" rule-name
)
646 (setq rule-name
(match-string 1 rule-name
)))
647 (setq rule-name
(concat rule-name
" (%d)"))
648 (setq rule
(assoc rule-name rules
))
650 (setcdr rule
(cons regexp
(cdr rule
)))
651 (setq rules
(cons (list rule-name regexp
) rules
))))
652 (setq modes
(cdr modes
)))
653 ;; It is important to preserve auto-mode-alist order
654 ;; to ensure the right file <-> mode association
657 (defun recentf-arrange-by-mode (l)
658 "Filter the list of menu-elements L to build sub-menus for each
660 (let ((recentf-arrange-rules (recentf-build-mode-rules))
661 (recentf-arrange-by-rule-others "others (%d)"))
662 (recentf-arrange-by-rule l
)))
664 (defun recentf-build-dir-rules (l)
665 "Convert directories in the list of menu-elements L to rules in
666 `recentf-arrange-rules' format."
670 (let ((dir (file-name-directory
671 (recentf-menu-element-value e
))))
672 (or (member dir dirs
)
673 (setq dirs
(cons dir dirs
))))))
677 (cons (concat d
" (%d)")
679 (nreverse (sort dirs
'string-lessp
)))))
681 (defun recentf-file-name-nondir (l)
682 "Filter the list of menu-elements L to show only filenames. This
683 simplified version of `recentf-show-basenames' do not handle
684 duplicates. It is used by `recentf-arrange-by-dir' as its
685 `recentf-arrange-by-rule-subfilter'."
688 (recentf-make-menu-element
689 (file-name-nondirectory (recentf-menu-element-value e
))
690 (recentf-menu-element-value e
))))
693 (defun recentf-arrange-by-dir (l)
694 "Filter the list of menu-elements L to build sub-menus for each
696 (let ((recentf-arrange-rules (recentf-build-dir-rules l
))
697 (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir
)
698 recentf-arrange-by-rule-others
)
699 (nreverse (recentf-arrange-by-rule l
))))
701 (defvar recentf-filter-changer-state nil
702 "Used by `recentf-filter-changer' to hold its state.")
704 (defcustom recentf-filter-changer-alist
706 (recentf-arrange-by-mode .
"*Files by Mode*")
707 (recentf-arrange-by-dir .
"*Files by Directory*")
708 (recentf-arrange-by-rule .
"*Files by User Rule*")
710 "*List of filters managed by `recentf-filter-changer'.
711 Each filter is defined by a pair (FILTER-FUN . FILTER-LBL) where:
713 - - FILTER-FUN is the function that filters menu-elements
714 - - FILTER-LBL is the menu item used to activate the filter"
715 :group
'recentf-filters
716 :type
'(repeat (cons function string
))
717 :set
(lambda (sym val
)
718 (setq recentf-filter-changer-state nil
)
719 (recentf-menu-customization-changed sym val
)))
721 (defun recentf-filter-changer-goto-next ()
722 "Go to the next filter available (see `recentf-filter-changer')."
723 (and (consp recentf-filter-changer-state
)
724 (setq recentf-filter-changer-state
725 (cdr recentf-filter-changer-state
)))
726 (setq recentf-update-menu-p t
))
728 (defun recentf-filter-changer-get-current ()
729 "Get the current filter available (see `recentf-filter-changer')."
730 (if (null recentf-filter-changer-state
)
731 (setq recentf-filter-changer-state recentf-filter-changer-alist
))
732 (and (consp recentf-filter-changer-state
)
733 (car recentf-filter-changer-state
)))
735 (defun recentf-filter-changer-get-next ()
736 "Get the next filter available (see `recentf-filter-changer')."
737 (let ((filters recentf-filter-changer-state
))
738 (cond ((consp filters
)
739 (setq filters
(cdr filters
))
741 (setq filters recentf-filter-changer-alist
)))
743 (setq filters recentf-filter-changer-alist
)
745 (setq filters
(cdr filters
)))))
749 (defun recentf-filter-changer (l)
750 "Manage a ring of filters. `recentf-filter-changer-alist' defines
751 the filters in the ring. Actual filtering of L is delegated to the
752 current filter in the ring. A filter menu item is displayed allowing
753 to dynamically activate the next filter in the ring. If the filter
754 ring is empty L is left unchanged."
755 (let ((current-filter-item (recentf-filter-changer-get-current))
756 (next-filter-item (recentf-filter-changer-get-next)))
757 (when current-filter-item
758 (setq l
(recentf-apply-menu-filter (car current-filter-item
) l
))
760 (setq recentf-menu-filter-commands
761 (list (vector (cdr next-filter-item
)
762 '(recentf-filter-changer-goto-next)
770 (defun recentf-cancel-dialog (&rest ignore
)
771 "Cancel the current dialog. Used by `recentf-edit-list' and
772 `recentf-open-files' dialogs."
774 (kill-buffer (current-buffer))
775 (message "Dialog canceled."))
777 (defvar recentf-dialog-mode-map nil
778 "`recentf-dialog-mode' keymap.")
780 (if recentf-dialog-mode-map
782 (setq recentf-dialog-mode-map
(make-sparse-keymap))
783 (define-key recentf-dialog-mode-map
"q" 'recentf-cancel-dialog
)
784 (set-keymap-parent recentf-dialog-mode-map widget-keymap
))
786 (defun recentf-dialog-mode ()
787 "Major mode used in recentf dialogs.
789 These are the special commands of recentf-dialog-mode mode:
790 q -- cancel this dialog."
792 (setq major-mode
'recentf-dialog-mode
)
793 (setq mode-name
"recentf-dialog")
794 (use-local-map recentf-dialog-mode-map
))
797 ;;; Hooks and Commands
800 (defun recentf-add-file-hook ()
801 "Insert the name of the file just opened or written into `recentf-list'."
802 (and buffer-file-name
(recentf-add-file buffer-file-name
))
805 (defun recentf-remove-file-hook ()
806 "When a buffer is killed remove a non readable file from `recentf-list'."
807 (and buffer-file-name
(recentf-remove-if-non-readable buffer-file-name
))
810 (defun recentf-update-menu-hook ()
811 "Update the recentf menu from the current `recentf-list'."
812 (when recentf-update-menu-p
815 (setq recentf-update-menu-p nil
)
816 (easy-menu-change recentf-menu-path
818 (recentf-make-menu-items)
819 recentf-menu-before
))
822 (defun recentf-dump-variable (variable &optional limit
)
823 "Insert a \"(setq VARIABLE value)\" in the current buffer. Optional
824 argument LIMIT specifies a maximum length when VARIABLE value is a
825 list (default to the full list)."
826 (let ((value (symbol-value variable
)))
827 (insert (format "(setq %S\n '(\n" variable
))
829 (if (and (integerp limit
) (> limit
0))
830 (setq value
(recentf-trunc-list value limit
)))
833 (insert (format " %S\n" e
))))
836 (insert (format " %S\n" value
))))
841 (defun recentf-save-list ()
842 "Save the current `recentf-list' to the file `recentf-save-file'."
846 (insert (format recentf-save-file-header
(current-time-string)))
847 (recentf-dump-variable 'recentf-list recentf-max-saved-items
)
848 (recentf-dump-variable 'recentf-filter-changer-state
)
849 (if (file-writable-p recentf-save-file
)
850 (write-region (point-min) (point-max) recentf-save-file
))
851 (kill-buffer (current-buffer)))
854 (defvar recentf-edit-selected-items nil
855 "Used by `recentf-edit-list' to hold the list of files to be deleted
856 from `recentf-list'.")
858 (defun recentf-edit-list-action (widget &rest ignore
)
859 "Checkbox widget action used by `recentf-edit-list' to select/unselect a file."
860 (let ((value (widget-get widget
':tag
)))
861 ;; if value is already in the selected items
862 (if (memq value recentf-edit-selected-items
)
865 (setq recentf-edit-selected-items
866 (delq value recentf-edit-selected-items
))
867 (message "%s removed from selection." value
))
870 (setq recentf-edit-selected-items
871 (nconc (list value
) recentf-edit-selected-items
))
872 (message "%s added to selection." value
)))))
875 (defun recentf-edit-list ()
876 "Allow the user to edit the files that are kept in the recent list."
878 (with-current-buffer (get-buffer-create (concat "*" recentf-menu-title
" - Edit list*"))
879 (switch-to-buffer (current-buffer))
880 (kill-all-local-variables)
881 (let ((inhibit-read-only t
))
883 (let ((all (overlay-lists)))
884 ;; Delete all the overlays.
885 (mapcar 'delete-overlay
(car all
))
886 (mapcar 'delete-overlay
(cdr all
)))
887 (setq recentf-edit-selected-items nil
)
888 ;; Insert the dialog header
889 (widget-insert "Select the files to be deleted from the 'recentf-list'.\n\n")
890 (widget-insert "Click on Ok to update the list. ")
891 (widget-insert "Click on Cancel or type \"q\" to quit.\n")
892 ;; Insert the list of files as checkboxes
895 (widget-create 'checkbox
896 :value nil
; unselected checkbox
897 :format
"\n %[%v%] %t"
899 :notify
'recentf-edit-list-action
)))
901 (widget-insert "\n\n")
902 ;; Insert the Ok button
903 (widget-create 'push-button
904 :notify
(lambda (&rest ignore
)
905 (if recentf-edit-selected-items
906 (progn (kill-buffer (current-buffer))
910 (delq item recentf-list
))))
911 recentf-edit-selected-items
)
912 (message "%S file(s) removed from the list"
913 (length recentf-edit-selected-items
))
914 (setq recentf-update-menu-p t
))
915 (message "No file selected.")))
918 ;; Insert the Cancel button
919 (widget-create 'push-button
920 :notify
'recentf-cancel-dialog
922 (recentf-dialog-mode)
924 (goto-char (point-min))))
927 (defun recentf-cleanup ()
928 "Remove all non-readable and excluded files from `recentf-list'."
930 (let ((count (length recentf-list
)))
935 (and (file-readable-p filename
)
936 (recentf-include-p filename
)
939 (setq count
(- count
(length recentf-list
)))
940 (message "%s removed from the list"
941 (cond ((= count
0) "No file")
942 ((= count
1) "One file")
943 (t (format "%d files" count
)))))
944 (setq recentf-update-menu-p t
))
946 (defun recentf-open-files-action (widget &rest ignore
)
947 "Button widget action used by `recentf-open-files' to open a file."
948 (kill-buffer (current-buffer))
949 (funcall recentf-menu-action
(widget-value widget
)))
951 (defvar recentf-open-files-item-shift
""
952 "String used by `recentf-open-files' to shift right sub-menu
955 (defun recentf-open-files-item (menu-element)
956 "Function called by `recentf-open-files' to insert a menu-element
957 item in the current interaction buffer."
958 (let ((menu-item (car menu-element
))
959 (file-path (cdr menu-element
)))
960 (if (consp file-path
) ; This is a sub-menu
961 (let* ((shift recentf-open-files-item-shift
)
962 (recentf-open-files-item-shift (concat shift
" ")))
966 :format
(concat shift
"%{%t%}:\n"))
967 (mapcar 'recentf-open-files-item
969 (widget-insert "\n"))
970 (widget-create 'push-button
971 :button-face
'default
973 :help-echo
(concat "Open " file-path
)
974 :format
(concat recentf-open-files-item-shift
"%[%t%]")
975 :notify
'recentf-open-files-action
977 (widget-insert "\n"))))
980 (defun recentf-open-files (&optional files buffer-name
)
981 "Open a buffer that allows the user to choose a file to open from
982 the list of recently opened files. The optional argument FILES may be
983 used to specify the list, otherwise recentf-list is used. The optional
984 argument BUFFER-NAME specifies which buffer to use for the interaction."
987 (setq files recentf-list
))
988 (if (null buffer-name
)
989 (setq buffer-name
(concat "*" recentf-menu-title
"*")))
990 (with-current-buffer (get-buffer-create buffer-name
)
991 (switch-to-buffer (current-buffer))
992 (kill-all-local-variables)
993 (let ((inhibit-read-only t
))
995 (let ((all (overlay-lists)))
996 ;; Delete all the overlays.
997 (mapcar 'delete-overlay
(car all
))
998 (mapcar 'delete-overlay
(cdr all
)))
999 ;; Insert the dialog header
1000 (widget-insert "Click on a file to open it. ")
1001 (widget-insert "Click on Cancel or type \"q\" to quit.\n\n" )
1002 ;; Insert the list of files as buttons
1003 (let ((recentf-open-files-item-shift ""))
1004 (mapcar 'recentf-open-files-item
1005 (recentf-apply-menu-filter
1007 (mapcar 'recentf-make-default-menu-element files
))))
1008 (widget-insert "\n")
1009 ;; Insert the Cancel button
1010 (widget-create 'push-button
1011 :notify
'recentf-cancel-dialog
1013 (recentf-dialog-mode)
1015 (goto-char (point-min))))
1018 (defun recentf-open-more-files ()
1019 "Allow the user to open files that are not in the menu."
1021 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list
)
1022 (concat "*" recentf-menu-title
" - More*")))
1025 (defun recentf-mode (&optional arg
)
1026 "Toggle recentf mode.
1027 With prefix ARG, turn recentf mode on if and only if ARG is positive.
1028 Returns the new status of recentf mode (non-nil means on).
1030 When recentf mode is enabled, it maintains a menu for visiting files that
1031 were operated on recently."
1034 (> (prefix-numeric-value arg
) 0)
1035 (not recentf-mode
))))
1037 (unless recentf-initialized-p
1038 (setq recentf-initialized-p t
)
1039 (if (file-readable-p recentf-save-file
)
1040 (load-file recentf-save-file
))
1041 (setq recentf-update-menu-p t
)
1042 (add-hook 'find-file-hooks
'recentf-add-file-hook
)
1043 (add-hook 'write-file-hooks
'recentf-add-file-hook
)
1044 (add-hook 'menu-bar-update-hook
'recentf-update-menu-hook
)
1045 (add-hook 'kill-emacs-hook
'recentf-save-list
))
1046 (when recentf-initialized-p
1047 (setq recentf-initialized-p nil
)
1049 (easy-menu-remove-item nil recentf-menu-path recentf-menu-title
)
1050 (remove-hook 'find-file-hooks
'recentf-add-file-hook
)
1051 (remove-hook 'write-file-hooks
'recentf-add-file-hook
)
1052 (remove-hook 'menu-bar-update-hook
'recentf-update-menu-hook
)
1053 (remove-hook 'kill-emacs-hook
'recentf-save-list
)))
1054 (setq recentf-mode on-p
)))
1058 (run-hooks 'recentf-load-hook
)
1060 ;;; recentf.el ends here.