Add new function dom-remove-node
[emacs.git] / lisp / recentf.el
blobdc9489752fb56f6cf96e3db081548a1f7f95064f
1 ;;; recentf.el --- setup a menu of recently opened files
3 ;; Copyright (C) 1999-2016 Free Software Foundation, Inc.
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Created: July 19 1999
7 ;; Keywords: files
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This package maintains a menu for visiting files that were operated
27 ;; on recently. When enabled a new "Open Recent" sub menu is
28 ;; displayed in the "File" menu. The recent files list is
29 ;; automatically saved across Emacs sessions. You can customize the
30 ;; number of recent files displayed, the location of the menu and
31 ;; others options (see the source code for details).
33 ;; To enable this package, add the following to your .emacs:
34 ;; (recentf-mode 1)
36 ;;; History:
39 ;;; Code:
40 (require 'easymenu)
41 (require 'tree-widget)
42 (require 'timer)
44 ;;; Internal data
46 (defvar recentf-list nil
47 "List of recently opened files.")
49 (defsubst recentf-enabled-p ()
50 "Return non-nil if recentf mode is currently enabled."
51 (memq 'recentf-save-list kill-emacs-hook))
53 ;;; Customization
55 (defgroup recentf nil
56 "Maintain a menu of recently opened files."
57 :version "21.1"
58 :group 'files)
60 (defgroup recentf-filters nil
61 "Group to customize recentf menu filters.
62 You should define the options of your own filters in this group."
63 :group 'recentf)
65 (defcustom recentf-max-saved-items 20
66 "Maximum number of items of the recent list that will be saved.
67 A nil value means to save the whole list.
68 See the command `recentf-save-list'."
69 :group 'recentf
70 :type 'integer)
72 (defcustom recentf-save-file (locate-user-emacs-file "recentf" ".recentf")
73 "File to save the recent list into."
74 :group 'recentf
75 :version "24.4"
76 :type 'file
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))
82 recentf-mode
83 (recentf-load-list)))))
85 (defcustom recentf-save-file-modes 384 ;; 0600
86 "Mode bits of recentf save file, as an integer, or nil.
87 If non-nil, after writing `recentf-save-file', set its mode bits to
88 this value. By default give R/W access only to the user who owns that
89 file. See also the function `set-file-modes'."
90 :group 'recentf
91 :type '(choice (const :tag "Don't change" nil)
92 integer))
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."
100 :group 'recentf
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."
106 (cond
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."
124 :group 'recentf
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))
131 (progn
132 ;; Unavailable until recentf has been loaded.
133 (recentf-hide-menu)
134 (set-default variable value)
135 (recentf-show-menu))
136 (set-default variable value)))
138 (defcustom recentf-menu-title "Open Recent"
139 "Name of the recentf menu."
140 :group 'recentf
141 :type 'string
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')."
147 :group 'recentf
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')."
155 :group 'recentf
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."
163 :group 'recentf
164 :type 'function)
166 (defcustom recentf-max-menu-items 10
167 "Maximum number of items in the recentf menu."
168 :group 'recentf
169 :type 'integer)
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)."
207 :group 'recentf
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)
223 function))
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."
228 :group 'recentf
229 :type 'boolean)
231 (define-obsolete-variable-alias 'recentf-menu-append-commands-p
232 'recentf-menu-append-commands-flag
233 "22.1")
235 (defcustom recentf-menu-append-commands-flag t
236 "Non-nil means to append command items to the menu."
237 :group 'recentf
238 :type 'boolean)
240 (defcustom recentf-auto-cleanup 'mode
241 "Define when to automatically cleanup the recent list.
242 The following values can be set:
244 - `mode'
245 Cleanup when turning the mode on (default).
246 - `never'
247 Never cleanup the list automatically.
248 - A number
249 Cleanup each time Emacs has been idle that number of seconds.
250 - A time string
251 Cleanup at specified time string, for example at \"11:00pm\".
253 Setting this variable directly does not take effect;
254 use \\[customize].
256 See also the command `recentf-cleanup', that can be used to manually
257 cleanup the list."
258 :group 'recentf
259 :type '(radio (const :tag "When mode enabled"
260 :value mode)
261 (const :tag "Never"
262 :value never)
263 (number :tag "When idle that seconds"
264 :value 300)
265 (string :tag "At time"
266 :value "11:00pm"))
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."
276 :group 'recentf
277 :type 'boolean)
279 (defcustom recentf-load-hook nil
280 "Normal hook run at end of loading the `recentf' package."
281 :group 'recentf
282 :type 'hook)
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."
287 :group 'recentf
288 :type '(choice
289 (const :tag "None" nil)
290 (repeat :tag "Functions"
291 (choice
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."
300 :group 'recentf
301 :type 'boolean)
303 ;;; Utilities
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)))
330 list)
332 (defsubst recentf-trunc-list (l n)
333 "Return from L the list of its first N elements."
334 (let (nl)
335 (while (and l (> n 0))
336 (setq nl (cons (car l) nl)
337 n (1- n)
338 l (cdr l)))
339 (nreverse 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
345 the full list."
346 (let ((value (symbol-value variable)))
347 (if (atom value)
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))
352 (dolist (e value)
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))
364 (when recentf-mode
365 (setq recentf-auto-cleanup-timer
366 (cond
367 ((eq 'mode recentf-auto-cleanup)
368 (recentf-cleanup)
369 nil)
370 ((numberp recentf-auto-cleanup)
371 (run-with-idle-timer
372 recentf-auto-cleanup t 'recentf-cleanup))
373 ((stringp recentf-auto-cleanup)
374 (run-at-time
375 recentf-auto-cleanup nil 'recentf-cleanup))))))
377 ;;; File functions
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
383 filenames."
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
391 returned nil."
392 (or (condition-case nil
393 (let ((handlers recentf-filename-handlers)
394 (filename name))
395 (while (and filename handlers)
396 (setq filename (funcall (car handlers) filename)
397 handlers (cdr handlers)))
398 filename)
399 (error nil))
400 name))
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)
413 (keepit t))
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))
419 ;; A regexp
420 (string-match (car checks) filename)
421 ;; A predicate
422 (funcall (car checks) filename))))
423 checks (cdr checks)))
424 keepit))
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))
435 ;; A regexp
436 (string-match (car checks) filename)
437 ;; A predicate
438 (funcall (car checks) filename))
439 (error nil))
440 checks (cdr checks)))
441 keepit))
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))))
470 ;;; Menu building
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.
482 (defalias cmd
483 `(lambda ()
484 (interactive)
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
492 (list
493 ["Cleanup list"
494 recentf-cleanup
495 :help "Remove duplicates, and obsoletes files from the recent list"
496 :active t]
497 ["Edit list..."
498 recentf-edit-list
499 :help "Manually remove files from the recent list"
500 :active t]
501 ["Save list now"
502 recentf-save-list
503 :help "Save the list of recently opened files now"
504 :active t]
505 ["Options..."
506 (customize-group "recentf")
507 :help "Customize recently opened files menu and options"
508 :active t]
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
529 sub-menu."
530 (cons menu-item menu-value))
532 (defsubst recentf-menu-element-item (e)
533 "Return the item part of the menu-element E."
534 (car e))
536 (defsubst recentf-menu-element-value (e)
537 "Return the value part of the menu-element E."
538 (cdr e))
540 (defsubst recentf-set-menu-element-item (e item)
541 "Change the item part of menu-element E to ITEM."
542 (setcar e item))
544 (defsubst recentf-set-menu-element-value (e value)
545 "Change the value part of menu-element E to VALUE."
546 (setcdr e 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)
570 elts others)
571 ;; split L into two sub-lists, one of sub-menus elements and
572 ;; another of single menu elements.
573 (dolist (elt l)
574 (if (recentf-sub-menu-element-p elt)
575 (push elt elts)
576 (push elt others)))
577 ;; Apply FILTER to single elements.
578 (when others
579 (setq others (funcall filter (nreverse others))))
580 ;; Apply FILTER to sub-menu elements.
581 (setq l nil)
582 (dolist (elt elts)
583 (recentf-set-menu-element-value
584 elt (recentf-apply-menu-filter
585 filter (recentf-menu-element-value elt)))
586 (push elt l))
587 ;; Return the new filtered menu element list.
588 (nconc l others))
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)
599 (file-items
600 (condition-case err
601 (mapcar 'recentf-make-menu-item
602 (recentf-apply-menu-filter
603 recentf-menu-filter
604 (recentf-menu-elements recentf-max-menu-items)))
605 (error
606 (message "recentf update menu failed: %s"
607 (error-message-string err))))))
608 (append
609 (or file-items
610 '(["No files" t
611 :help "No recent file to open"
612 :active nil]))
613 (if recentf-menu-open-all-flag
614 '(["All..." recentf-open-files
615 :help "Open recent files through a dialog"
616 :active t])
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"
620 :active t])))
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."
629 (let ((i 0) k)
630 (while (and (not k) (< i 10))
631 (if (string-equal name (nth i recentf-list))
632 (progn
633 (setq recentf-menu-shortcuts (1+ recentf-menu-shortcuts))
634 (setq k (% (1+ i) 10)))
635 (setq i (1+ i))))
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))))
646 (vector item
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.
650 (if k
651 (recentf-digit-shortcut-command-name k)
652 `(lambda ()
653 (interactive)
654 (,recentf-menu-action ,value)))
655 :help (concat "Open " value)
656 :active t)))))
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."
664 (easy-menu-add-item
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
672 recentf-menu-title))
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)
680 #'(lambda (e1 e2)
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)
689 #'(lambda (e1 e2)
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)
698 #'(lambda (e1 e2)
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)
707 #'(lambda (e1 e2)
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)
716 #'(lambda (e1 e2)
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)
725 #'(lambda (e1 e2)
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)
740 (if no-dir
741 (if (setq sufx (assoc name counters))
742 (setcdr sufx (1+ (cdr sufx)))
743 (setq sufx 1)
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)
770 menu-element
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)
795 (repeat regexp))))
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
812 defined."
813 :group 'recentf-filters
814 :type 'number)
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)
832 match found)
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)))
839 (if match
840 (setq found (cons (caar rules) file))
841 (setq rules (cdr rules))))
842 found))
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.
850 (dolist (elt l)
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)))
856 (push elt others)
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)
868 l nil)
869 (dolist (elt menus)
870 (setq menu (recentf-menu-element-value elt)
871 count (length menu))
872 (if (< count min)
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)))
879 (push elt l)))
880 ;; Add the menu items remaining in the `others' bin.
881 (when (setq others (nreverse others))
882 (setq l (nconc
884 ;; Put items in an sub menu.
885 (if (stringp recentf-arrange-by-rule-others)
886 (list
887 (recentf-make-menu-element
888 (format recentf-arrange-by-rule-others
889 (length 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)
910 mode (cdr mode))
911 (when mode
912 (cond
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))
925 (if rule
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
930 (nreverse rules)))
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
974 that filter."
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
996 (list
997 `("Show files"
998 ,@(mapcar
999 #'(lambda (f)
1000 `[,(cdr f)
1001 (setq recentf-filter-changer-current ',(car f))
1002 ;;:active t
1003 :style radio ;;radio Don't work with GTK :-(
1004 :selected (eq recentf-filter-changer-current
1005 ',(car f))
1006 ;;:help ,(cdr f)
1008 recentf-filter-changer-alist))))
1009 (recentf-apply-menu-filter recentf-filter-changer-current l)))
1011 ;;; Hooks
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'.
1018 nil)
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.")
1035 ;;; Commands
1038 ;;; Common dialog stuff
1040 (defun recentf-cancel-dialog (&rest _ignore)
1041 "Cancel the current dialog.
1042 IGNORE arguments."
1043 (interactive)
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))
1051 (condition-case nil
1052 (let (done)
1053 (widget-move 1)
1054 (while (not done)
1055 (if (eq widget-type (widget-type (widget-at (point))))
1056 (setq done t)
1057 (widget-move 1))))
1058 (error
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}"
1074 :syntax-table nil
1075 :abbrev-table nil
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)
1082 ;; Cleanup buffer
1083 (let ((inhibit-read-only t)
1084 (ol (overlay-lists)))
1085 (mapc 'delete-overlay (car ol))
1086 (mapc 'delete-overlay (cdr ol))
1087 (erase-buffer))
1088 (recentf-dialog-mode)
1089 ,@forms
1090 (widget-setup)
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)))
1102 (if check
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.
1109 IGNORE arguments."
1110 (if recentf-edit-list
1111 (let ((i 0))
1112 (dolist (e recentf-edit-list)
1113 (setq recentf-list (delq e recentf-list)
1114 i (1+ i)))
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."
1121 (interactive)
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)
1126 (widget-insert
1127 "Click on OK to delete selected files from the recent list.
1128 Click on Cancel or type `q' to cancel.\n")
1129 ;; Insert the list of files as checkboxes
1130 (dolist (item recentf-list)
1131 (widget-create 'checkbox
1132 :value nil ; unselected checkbox
1133 :format "\n %[%v%] %t"
1134 :tag item
1135 :notify 'recentf-edit-list-select))
1136 (widget-insert "\n\n")
1137 (widget-create
1138 'push-button
1139 :notify 'recentf-edit-list-validate
1140 :help-echo "Delete selected files from the recent list"
1141 "Ok")
1142 (widget-insert " ")
1143 (widget-create
1144 'push-button
1145 :notify 'recentf-cancel-dialog
1146 "Cancel")
1147 (recentf-dialog-goto-first 'checkbox)))
1149 ;;; Open file dialog
1151 (defun recentf-open-files-action (widget &rest _ignore)
1152 "Open the file stored in WIDGET's value when notified.
1153 IGNORE other arguments."
1154 (kill-buffer (current-buffer))
1155 (funcall recentf-menu-action (widget-value widget)))
1157 ;; List of files associated to a digit shortcut key.
1158 (defvar recentf--files-with-key nil)
1160 (defun recentf-show-digit-shortcut-filter (l)
1161 "Filter the list of menu-elements L to show digit shortcuts."
1162 (let ((i 0))
1163 (dolist (e l)
1164 (setq i (1+ i))
1165 (recentf-set-menu-element-item
1166 e (format "[%d] %s" (% i 10) (recentf-menu-element-item e))))
1169 (defun recentf-open-files-item (menu-element)
1170 "Return a widget to display MENU-ELEMENT in a dialog buffer."
1171 (if (consp (cdr menu-element))
1172 ;; Represent a sub-menu with a tree widget
1173 `(tree-widget
1174 :open t
1175 :match ignore
1176 :node (item :tag ,(car menu-element)
1177 :sample-face bold
1178 :format "%{%t%}:\n")
1179 ,@(mapcar 'recentf-open-files-item
1180 (cdr menu-element)))
1181 ;; Represent a single file with a link widget
1182 `(link :tag ,(car menu-element)
1183 :button-prefix ""
1184 :button-suffix ""
1185 :button-face default
1186 :format "%[%t\n%]"
1187 :help-echo ,(concat "Open " (cdr menu-element))
1188 :action recentf-open-files-action
1189 ;; Override the (problematic) follow-link property of the
1190 ;; `link' widget (bug#22434).
1191 :follow-link nil
1192 ,(cdr menu-element))))
1194 (defun recentf-open-files-items (files)
1195 "Return a list of widgets to display FILES in a dialog buffer."
1196 (set (make-local-variable 'recentf--files-with-key)
1197 (recentf-trunc-list files 10))
1198 (mapcar 'recentf-open-files-item
1199 (append
1200 ;; When requested group the files with shortcuts together
1201 ;; at the top of the list.
1202 (when recentf-show-file-shortcuts-flag
1203 (setq files (nthcdr 10 files))
1204 (recentf-apply-menu-filter
1205 'recentf-show-digit-shortcut-filter
1206 (mapcar 'recentf-make-default-menu-element
1207 recentf--files-with-key)))
1208 ;; Then the other files.
1209 (recentf-apply-menu-filter
1210 recentf-menu-filter
1211 (mapcar 'recentf-make-default-menu-element
1212 files)))))
1214 (defun recentf-open-files (&optional files buffer-name)
1215 "Show a dialog to open a recent file.
1216 If optional argument FILES is non-nil, it is a list of recently-opened
1217 files to choose from. It defaults to the whole recent list.
1218 If optional argument BUFFER-NAME is non-nil, it is a buffer name to
1219 use for the dialog. It defaults to \"*`recentf-menu-title'*\"."
1220 (interactive)
1221 (unless (or files recentf-list)
1222 (error "There is no recent file to open"))
1223 (recentf-dialog (or buffer-name (format "*%s*" recentf-menu-title))
1224 (widget-insert "Click on a file"
1225 (if recentf-show-file-shortcuts-flag
1226 ", or type the corresponding digit key,"
1228 " to open it.\n"
1229 (format-message "Click on Cancel or type `q' to cancel.\n"))
1230 ;; Use a L&F that looks like the recentf menu.
1231 (tree-widget-set-theme "folder")
1232 (apply 'widget-create
1233 `(group
1234 :indent 2
1235 :format "\n%v\n"
1236 ,@(recentf-open-files-items (or files recentf-list))))
1237 (widget-create
1238 'push-button
1239 :notify 'recentf-cancel-dialog
1240 "Cancel")
1241 (recentf-dialog-goto-first 'link)))
1243 (defun recentf-open-more-files ()
1244 "Show a dialog to open a recent file that is not in the menu."
1245 (interactive)
1246 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
1247 (format "*%s - More*" recentf-menu-title)))
1249 (defun recentf-open-most-recent-file (&optional n)
1250 "Open the Nth most recent file.
1251 Optional argument N must be a valid digit number. It defaults to 1.
1252 1 opens the most recent file, 2 the second most recent one, etc..
1253 0 opens the tenth most recent file."
1254 (interactive "p")
1255 (cond
1256 ((zerop n) (setq n 10))
1257 ((and (> n 0) (< n 10)))
1258 ((error "Recent file number out of range [0-9], %d" n)))
1259 (let ((file (nth (1- n) (or recentf--files-with-key recentf-list))))
1260 (unless file (error "Not that many recent files"))
1261 ;; Close the open files dialog.
1262 (when recentf--files-with-key
1263 (kill-buffer (current-buffer)))
1264 (funcall recentf-menu-action file)))
1266 ;;; Save/load/cleanup the recent list
1268 (defconst recentf-save-file-header
1269 ";;; Automatically generated by `recentf' on %s.\n"
1270 "Header to be written into the `recentf-save-file'.")
1272 (defconst recentf-save-file-coding-system
1273 (if (coding-system-p 'utf-8-emacs)
1274 'utf-8-emacs
1275 'emacs-mule)
1276 "Coding system of the file `recentf-save-file'.")
1278 (defun recentf-save-list ()
1279 "Save the recent list.
1280 Write data into the file specified by `recentf-save-file'."
1281 (interactive)
1282 (condition-case error
1283 (with-temp-buffer
1284 (erase-buffer)
1285 (set-buffer-file-coding-system recentf-save-file-coding-system)
1286 (insert (format-message recentf-save-file-header
1287 (current-time-string)))
1288 (recentf-dump-variable 'recentf-list recentf-max-saved-items)
1289 (recentf-dump-variable 'recentf-filter-changer-current)
1290 (insert "\n\f\n;; Local Variables:\n"
1291 (format ";; coding: %s\n" recentf-save-file-coding-system)
1292 ";; End:\n")
1293 (write-file (expand-file-name recentf-save-file))
1294 (when recentf-save-file-modes
1295 (set-file-modes recentf-save-file recentf-save-file-modes))
1296 nil)
1297 (error
1298 (warn "recentf mode: %s" (error-message-string error)))))
1300 (defun recentf-load-list ()
1301 "Load a previously saved recent list.
1302 Read data from the file specified by `recentf-save-file'.
1303 When `recentf-initialize-file-name-history' is non-nil, initialize an
1304 empty `file-name-history' with the recent list."
1305 (interactive)
1306 (let ((file (expand-file-name recentf-save-file)))
1307 (when (file-readable-p file)
1308 (load-file file)
1309 (and recentf-initialize-file-name-history
1310 (not file-name-history)
1311 (setq file-name-history (mapcar 'abbreviate-file-name
1312 recentf-list))))))
1314 (defun recentf-cleanup ()
1315 "Cleanup the recent list.
1316 That is, remove duplicates, non-kept, and excluded files."
1317 (interactive)
1318 (message "Cleaning up the recentf list...")
1319 (let ((n 0)
1320 (ht (make-hash-table
1321 :size recentf-max-saved-items
1322 :test 'equal))
1323 newlist key)
1324 (dolist (f recentf-list)
1325 (setq f (recentf-expand-file-name f)
1326 key (if recentf-case-fold-search (downcase f) f))
1327 (if (and (recentf-include-p f)
1328 (recentf-keep-p f)
1329 (not (gethash key ht)))
1330 (progn
1331 (push f newlist)
1332 (puthash key t ht))
1333 (setq n (1+ n))
1334 (message "File %s removed from the recentf list" f)))
1335 (message "Cleaning up the recentf list...done (%d removed)" n)
1336 (setq recentf-list (nreverse newlist))))
1338 ;;; The minor mode
1340 (defvar recentf-mode-map (make-sparse-keymap)
1341 "Keymap to use in recentf mode.")
1343 ;;;###autoload
1344 (define-minor-mode recentf-mode
1345 "Toggle \"Open Recent\" menu (Recentf mode).
1346 With a prefix argument ARG, enable Recentf mode if ARG is
1347 positive, and disable it otherwise. If called from Lisp, enable
1348 Recentf mode if ARG is omitted or nil.
1350 When Recentf mode is enabled, a \"Open Recent\" submenu is
1351 displayed in the \"File\" menu, containing a list of files that
1352 were operated on recently."
1353 :global t
1354 :group 'recentf
1355 :keymap recentf-mode-map
1356 (unless (and recentf-mode (recentf-enabled-p))
1357 (if recentf-mode
1358 (progn
1359 (recentf-load-list)
1360 (recentf-show-menu))
1361 (recentf-hide-menu)
1362 (recentf-save-list))
1363 (recentf-auto-cleanup)
1364 (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
1365 (dolist (hook recentf-used-hooks)
1366 (apply hook-setup hook)))))
1368 (defun recentf-unload-function ()
1369 "Unload the recentf library."
1370 (recentf-mode -1)
1371 ;; continue standard unloading
1372 nil)
1374 (provide 'recentf)
1376 (run-hooks 'recentf-load-hook)
1378 ;;; recentf.el ends here