(gnus-blocked-images): Clarify privacy implications
[emacs.git] / lisp / recentf.el
blobc3c4e4592221de0c769a214c052b0f11bd3a7839
1 ;;; recentf.el --- setup a menu of recently opened files
3 ;; Copyright (C) 1999-2018 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 <https://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 #o600
86 "Mode bits of recentf save file, as an integer, or nil.
87 If non-nil, after writing `recentf-save-file', set its mode bits to
88 this value. By default give R/W access only to the user who owns that
89 file. See also the function `set-file-modes'."
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 (defcustom recentf-menu-append-commands-flag t
232 "Non-nil means to append command items to the menu."
233 :group 'recentf
234 :type 'boolean)
236 (defcustom recentf-auto-cleanup 'mode
237 "Define when to automatically cleanup the recent list.
238 The following values can be set:
240 - `mode'
241 Cleanup when turning the mode on (default).
242 - `never'
243 Never cleanup the list automatically.
244 - A number
245 Cleanup each time Emacs has been idle that number of seconds.
246 - A time string
247 Cleanup at specified time string, for example at \"11:00pm\".
249 Setting this variable directly does not take effect;
250 use \\[customize].
252 See also the command `recentf-cleanup', that can be used to manually
253 cleanup the list."
254 :group 'recentf
255 :type '(radio (const :tag "When mode enabled"
256 :value mode)
257 (const :tag "Never"
258 :value never)
259 (number :tag "When idle that seconds"
260 :value 300)
261 (string :tag "At time"
262 :value "11:00pm"))
263 :set (lambda (variable value)
264 (set-default variable value)
265 (when (featurep 'recentf)
266 ;; Unavailable until recentf has been loaded.
267 (recentf-auto-cleanup))))
269 (defcustom recentf-initialize-file-name-history t
270 "Non-nil means to initialize `file-name-history' with the recent list.
271 If `file-name-history' is not empty, do nothing."
272 :group 'recentf
273 :type 'boolean)
275 (defcustom recentf-load-hook nil
276 "Normal hook run at end of loading the `recentf' package."
277 :group 'recentf
278 :type 'hook)
280 (defcustom recentf-filename-handlers nil
281 "Functions to post process recent file names.
282 They are successively passed a file name to transform it."
283 :group 'recentf
284 :type '(choice
285 (const :tag "None" nil)
286 (repeat :tag "Functions"
287 (choice
288 (const file-truename)
289 (const abbreviate-file-name)
290 (function :tag "Other function")))))
292 (defcustom recentf-show-file-shortcuts-flag t
293 "Whether to show \"[N]\" for the Nth item up to 10.
294 If non-nil, `recentf-open-files' will show labels for keys that can be
295 used as shortcuts to open the Nth file."
296 :group 'recentf
297 :type 'boolean)
299 ;;; Utilities
301 (defconst recentf-case-fold-search
302 (memq system-type '(windows-nt cygwin))
303 "Non-nil if recentf searches and matches should ignore case.")
305 (defsubst recentf-string-equal (s1 s2)
306 "Return non-nil if strings S1 and S2 have identical contents.
307 Ignore case if `recentf-case-fold-search' is non-nil."
308 (if recentf-case-fold-search
309 (string-equal (downcase s1) (downcase s2))
310 (string-equal s1 s2)))
312 (defsubst recentf-string-lessp (s1 s2)
313 "Return non-nil if string S1 is less than S2 in lexicographic order.
314 Ignore case if `recentf-case-fold-search' is non-nil."
315 (if recentf-case-fold-search
316 (string-lessp (downcase s1) (downcase s2))
317 (string-lessp s1 s2)))
319 (defun recentf-string-member (elt list)
320 "Return non-nil if ELT is an element of LIST.
321 The value is actually the tail of LIST whose car is ELT.
322 ELT must be a string and LIST a list of strings.
323 Ignore case if `recentf-case-fold-search' is non-nil."
324 (while (and list (not (recentf-string-equal elt (car list))))
325 (setq list (cdr list)))
326 list)
328 (defsubst recentf-trunc-list (l n)
329 "Return from L the list of its first N elements."
330 (let (nl)
331 (while (and l (> n 0))
332 (setq nl (cons (car l) nl)
333 n (1- n)
334 l (cdr l)))
335 (nreverse nl)))
337 (defun recentf-dump-variable (variable &optional limit)
338 "Insert a \"(setq VARIABLE value)\" in the current buffer.
339 When the value of VARIABLE is a list, optional argument LIMIT
340 specifies a maximum number of elements to insert. By default insert
341 the full list."
342 (let ((value (symbol-value variable)))
343 (if (atom value)
344 (insert (format "\n(setq %S '%S)\n" variable value))
345 (when (and (integerp limit) (> limit 0))
346 (setq value (recentf-trunc-list value limit)))
347 (insert (format "\n(setq %S\n '(" variable))
348 (dolist (e value)
349 (insert (format "\n %S" e)))
350 (insert "\n ))\n"))))
352 (defvar recentf-auto-cleanup-timer nil
353 "Timer used to automatically cleanup the recent list.
354 See also the option `recentf-auto-cleanup'.")
356 (defun recentf-auto-cleanup ()
357 "Automatic cleanup of the recent list."
358 (when (timerp recentf-auto-cleanup-timer)
359 (cancel-timer recentf-auto-cleanup-timer))
360 (when recentf-mode
361 (setq recentf-auto-cleanup-timer
362 (cond
363 ((eq 'mode recentf-auto-cleanup)
364 (recentf-cleanup)
365 nil)
366 ((numberp recentf-auto-cleanup)
367 (run-with-idle-timer
368 recentf-auto-cleanup t 'recentf-cleanup))
369 ((stringp recentf-auto-cleanup)
370 (run-at-time
371 recentf-auto-cleanup nil 'recentf-cleanup))))))
373 ;;; File functions
375 (defsubst recentf-push (filename)
376 "Push FILENAME into the recent list, if it isn't there yet.
377 If it is there yet, move it at the beginning of the list.
378 If `recentf-case-fold-search' is non-nil, ignore case when comparing
379 filenames."
380 (let ((m (recentf-string-member filename recentf-list)))
381 (and m (setq recentf-list (delq (car m) recentf-list)))
382 (push filename recentf-list)))
384 (defun recentf-apply-filename-handlers (name)
385 "Apply `recentf-filename-handlers' to file NAME.
386 Return the transformed file name, or NAME if any handler failed, or
387 returned nil."
388 (or (condition-case nil
389 (let ((handlers recentf-filename-handlers)
390 (filename name))
391 (while (and filename handlers)
392 (setq filename (funcall (car handlers) filename)
393 handlers (cdr handlers)))
394 filename)
395 (error nil))
396 name))
398 (defsubst recentf-expand-file-name (name)
399 "Convert file NAME to absolute, and canonicalize it.
400 NAME is first passed to the function `expand-file-name', then to
401 `recentf-filename-handlers' to post process it."
402 (recentf-apply-filename-handlers (expand-file-name name)))
404 (defun recentf-include-p (filename)
405 "Return non-nil if FILENAME should be included in the recent list.
406 That is, if it doesn't match any of the `recentf-exclude' checks."
407 (let ((case-fold-search recentf-case-fold-search)
408 (checks recentf-exclude)
409 (keepit t))
410 (while (and checks keepit)
411 ;; If there was an error in a predicate, err on the side of
412 ;; keeping the file. (Bug#5843)
413 (setq keepit (not (ignore-errors
414 (if (stringp (car checks))
415 ;; A regexp
416 (string-match (car checks) filename)
417 ;; A predicate
418 (funcall (car checks) filename))))
419 checks (cdr checks)))
420 keepit))
422 (defun recentf-keep-p (filename)
423 "Return non-nil if FILENAME should be kept in the recent list.
424 That is, if it matches any of the `recentf-keep' checks."
425 (let* ((case-fold-search recentf-case-fold-search)
426 (checks recentf-keep)
427 (keepit (null checks)))
428 (while (and checks (not keepit))
429 (setq keepit (condition-case nil
430 (if (stringp (car checks))
431 ;; A regexp
432 (string-match (car checks) filename)
433 ;; A predicate
434 (funcall (car checks) filename))
435 (error nil))
436 checks (cdr checks)))
437 keepit))
439 (defsubst recentf-add-file (filename)
440 "Add or move FILENAME at the beginning of the recent list.
441 Does nothing if the name satisfies any of the `recentf-exclude'
442 regexps or predicates."
443 (setq filename (recentf-expand-file-name filename))
444 (when (recentf-include-p filename)
445 (recentf-push filename)))
447 (defsubst recentf-remove-if-non-kept (filename)
448 "Remove FILENAME from the recent list, if file is not kept.
449 Return non-nil if FILENAME has been removed."
450 (unless (recentf-keep-p filename)
451 (let ((m (recentf-string-member
452 (recentf-expand-file-name filename) recentf-list)))
453 (and m (setq recentf-list (delq (car m) recentf-list))))))
455 (defsubst recentf-directory-compare (f1 f2)
456 "Compare absolute filenames F1 and F2.
457 First compare directories, then filenames sans directory.
458 Return non-nil if F1 is less than F2."
459 (let ((d1 (file-name-directory f1))
460 (d2 (file-name-directory f2)))
461 (if (recentf-string-equal d1 d2)
462 (recentf-string-lessp (file-name-nondirectory f1)
463 (file-name-nondirectory f2))
464 (recentf-string-lessp d1 d2))))
466 ;;; Menu building
468 (defsubst recentf-digit-shortcut-command-name (n)
469 "Return a command name to open the Nth most recent file.
470 See also the command `recentf-open-most-recent-file'."
471 (intern (format "recentf-open-most-recent-file-%d" n)))
473 (defvar recentf--shortcuts-keymap
474 (let ((km (make-sparse-keymap)))
475 (dolist (k '(0 9 8 7 6 5 4 3 2 1))
476 (let ((cmd (recentf-digit-shortcut-command-name k)))
477 ;; Define a shortcut command.
478 (defalias cmd
479 `(lambda ()
480 (interactive)
481 (recentf-open-most-recent-file ,k)))
482 ;; Bind it to a digit key.
483 (define-key km (vector (+ k ?0)) cmd)))
485 "Digit shortcuts keymap.")
487 (defvar recentf-menu-items-for-commands
488 (list
489 ["Cleanup list"
490 recentf-cleanup
491 :help "Remove duplicates, and obsoletes files from the recent list"
492 :active t]
493 ["Edit list..."
494 recentf-edit-list
495 :help "Manually remove files from the recent list"
496 :active t]
497 ["Save list now"
498 recentf-save-list
499 :help "Save the list of recently opened files now"
500 :active t]
501 ["Options..."
502 (customize-group "recentf")
503 :help "Customize recently opened files menu and options"
504 :active t]
506 "List of menu items for recentf commands.")
508 (defvar recentf-menu-filter-commands nil
509 "This variable can be used by menu filters to setup their own command menu.
510 If non-nil it must contain a list of valid menu-items to be appended
511 to the recent file list part of the menu. Before calling a menu
512 filter function this variable is reset to nil.")
514 (defsubst recentf-elements (n)
515 "Return a list of the first N elements of the recent list."
516 (recentf-trunc-list recentf-list n))
518 (defsubst recentf-make-menu-element (menu-item menu-value)
519 "Create a new menu-element.
520 A menu element is a pair (MENU-ITEM . MENU-VALUE), where MENU-ITEM is
521 the menu item string displayed. MENU-VALUE is the file to be open
522 when the corresponding MENU-ITEM is selected. Or it is a
523 pair (SUB-MENU-TITLE . MENU-ELEMENTS) where SUB-MENU-TITLE is a
524 sub-menu title and MENU-ELEMENTS is the list of menu elements in the
525 sub-menu."
526 (cons menu-item menu-value))
528 (defsubst recentf-menu-element-item (e)
529 "Return the item part of the menu-element E."
530 (car e))
532 (defsubst recentf-menu-element-value (e)
533 "Return the value part of the menu-element E."
534 (cdr e))
536 (defsubst recentf-set-menu-element-item (e item)
537 "Change the item part of menu-element E to ITEM."
538 (setcar e item))
540 (defsubst recentf-set-menu-element-value (e value)
541 "Change the value part of menu-element E to VALUE."
542 (setcdr e value))
544 (defsubst recentf-sub-menu-element-p (e)
545 "Return non-nil if menu-element E defines a sub-menu."
546 (consp (recentf-menu-element-value e)))
548 (defsubst recentf-make-default-menu-element (file)
549 "Make a new default menu element with FILE.
550 This a menu element (FILE . FILE)."
551 (recentf-make-menu-element file file))
553 (defsubst recentf-menu-elements (n)
554 "Return a list of the first N default menu elements from the recent list.
555 See also `recentf-make-default-menu-element'."
556 (mapcar 'recentf-make-default-menu-element
557 (recentf-elements n)))
559 (defun recentf-apply-menu-filter (filter l)
560 "Apply function FILTER to the list of menu-elements L.
561 It takes care of sub-menu elements in L and recursively apply FILTER
562 to them. It is guaranteed that FILTER receives only a list of single
563 menu-elements (no sub-menu)."
564 (if (and l (functionp filter))
565 (let ((case-fold-search recentf-case-fold-search)
566 elts others)
567 ;; split L into two sub-lists, one of sub-menus elements and
568 ;; another of single menu elements.
569 (dolist (elt l)
570 (if (recentf-sub-menu-element-p elt)
571 (push elt elts)
572 (push elt others)))
573 ;; Apply FILTER to single elements.
574 (when others
575 (setq others (funcall filter (nreverse others))))
576 ;; Apply FILTER to sub-menu elements.
577 (setq l nil)
578 (dolist (elt elts)
579 (recentf-set-menu-element-value
580 elt (recentf-apply-menu-filter
581 filter (recentf-menu-element-value elt)))
582 (push elt l))
583 ;; Return the new filtered menu element list.
584 (nconc l others))
587 ;; Count the number of assigned menu shortcuts.
588 (defvar recentf-menu-shortcuts)
590 (defun recentf-make-menu-items (&optional _menu)
591 "Make menu items from the recent list.
592 This is a menu filter function which ignores the MENU argument."
593 (setq recentf-menu-filter-commands nil)
594 (let* ((recentf-menu-shortcuts 0)
595 (file-items
596 (condition-case err
597 (mapcar 'recentf-make-menu-item
598 (recentf-apply-menu-filter
599 recentf-menu-filter
600 (recentf-menu-elements recentf-max-menu-items)))
601 (error
602 (message "recentf update menu failed: %s"
603 (error-message-string err))))))
604 (append
605 (or file-items
606 '(["No files" t
607 :help "No recent file to open"
608 :active nil]))
609 (if recentf-menu-open-all-flag
610 '(["All..." recentf-open-files
611 :help "Open recent files through a dialog"
612 :active t])
613 (and (< recentf-max-menu-items (length recentf-list))
614 '(["More..." recentf-open-more-files
615 :help "Open files not in the menu through a dialog"
616 :active t])))
617 (and recentf-menu-filter-commands '("---"))
618 recentf-menu-filter-commands
619 (and recentf-menu-items-for-commands '("---"))
620 recentf-menu-items-for-commands)))
622 (defun recentf-menu-value-shortcut (name)
623 "Return a shortcut digit for file NAME.
624 Return nil if file NAME is not one of the ten more recent."
625 (let ((i 0) k)
626 (while (and (not k) (< i 10))
627 (if (string-equal name (nth i recentf-list))
628 (progn
629 (setq recentf-menu-shortcuts (1+ recentf-menu-shortcuts))
630 (setq k (% (1+ i) 10)))
631 (setq i (1+ i))))
634 (defun recentf-make-menu-item (elt)
635 "Make a menu item from menu element ELT."
636 (let ((item (recentf-menu-element-item elt))
637 (value (recentf-menu-element-value elt)))
638 (if (recentf-sub-menu-element-p elt)
639 (cons item (mapcar 'recentf-make-menu-item value))
640 (let ((k (and (< recentf-menu-shortcuts 10)
641 (recentf-menu-value-shortcut value))))
642 (vector item
643 ;; If the file name is one of the ten more recent, use
644 ;; a digit shortcut command to open it, else use an
645 ;; anonymous command.
646 (if k
647 (recentf-digit-shortcut-command-name k)
648 `(lambda ()
649 (interactive)
650 (,recentf-menu-action ,value)))
651 :help (concat "Open " value)
652 :active t)))))
654 (defsubst recentf-menu-bar ()
655 "Return the keymap of the global menu bar."
656 (lookup-key global-map [menu-bar]))
658 (defun recentf-show-menu ()
659 "Show the menu of recently opened files."
660 (easy-menu-add-item
661 (recentf-menu-bar) recentf-menu-path
662 (list recentf-menu-title :filter 'recentf-make-menu-items)
663 recentf-menu-before))
665 (defun recentf-hide-menu ()
666 "Hide the menu of recently opened files."
667 (easy-menu-remove-item (recentf-menu-bar) recentf-menu-path
668 recentf-menu-title))
670 ;;; Predefined menu filters
672 (defsubst recentf-sort-ascending (l)
673 "Sort the list of menu elements L in ascending order.
674 The MENU-ITEM part of each menu element is compared."
675 (sort (copy-sequence l)
676 #'(lambda (e1 e2)
677 (recentf-string-lessp
678 (recentf-menu-element-item e1)
679 (recentf-menu-element-item e2)))))
681 (defsubst recentf-sort-descending (l)
682 "Sort the list of menu elements L in descending order.
683 The MENU-ITEM part of each menu element is compared."
684 (sort (copy-sequence l)
685 #'(lambda (e1 e2)
686 (recentf-string-lessp
687 (recentf-menu-element-item e2)
688 (recentf-menu-element-item e1)))))
690 (defsubst recentf-sort-basenames-ascending (l)
691 "Sort the list of menu elements L in ascending order.
692 Only filenames sans directory are compared."
693 (sort (copy-sequence l)
694 #'(lambda (e1 e2)
695 (recentf-string-lessp
696 (file-name-nondirectory (recentf-menu-element-value e1))
697 (file-name-nondirectory (recentf-menu-element-value e2))))))
699 (defsubst recentf-sort-basenames-descending (l)
700 "Sort the list of menu elements L in descending order.
701 Only filenames sans directory are compared."
702 (sort (copy-sequence l)
703 #'(lambda (e1 e2)
704 (recentf-string-lessp
705 (file-name-nondirectory (recentf-menu-element-value e2))
706 (file-name-nondirectory (recentf-menu-element-value e1))))))
708 (defsubst recentf-sort-directories-ascending (l)
709 "Sort the list of menu elements L in ascending order.
710 Compares directories then filenames to order the list."
711 (sort (copy-sequence l)
712 #'(lambda (e1 e2)
713 (recentf-directory-compare
714 (recentf-menu-element-value e1)
715 (recentf-menu-element-value e2)))))
717 (defsubst recentf-sort-directories-descending (l)
718 "Sort the list of menu elements L in descending order.
719 Compares directories then filenames to order the list."
720 (sort (copy-sequence l)
721 #'(lambda (e1 e2)
722 (recentf-directory-compare
723 (recentf-menu-element-value e2)
724 (recentf-menu-element-value e1)))))
726 (defun recentf-show-basenames (l &optional no-dir)
727 "Filter the list of menu elements L to show filenames sans directory.
728 When a filename is duplicated, it is appended a sequence number if
729 optional argument NO-DIR is non-nil, or its directory otherwise."
730 (let (filtered-names filtered-list full name counters sufx)
731 (dolist (elt l (nreverse filtered-list))
732 (setq full (recentf-menu-element-value elt)
733 name (file-name-nondirectory full))
734 (if (not (member name filtered-names))
735 (push name filtered-names)
736 (if no-dir
737 (if (setq sufx (assoc name counters))
738 (setcdr sufx (1+ (cdr sufx)))
739 (setq sufx 1)
740 (push (cons name sufx) counters))
741 (setq sufx (file-name-directory full)))
742 (setq name (format "%s(%s)" name sufx)))
743 (push (recentf-make-menu-element name full) filtered-list))))
745 (defsubst recentf-show-basenames-ascending (l)
746 "Filter the list of menu elements L to show filenames sans directory.
747 Filenames are sorted in ascending order.
748 This filter combines the `recentf-sort-basenames-ascending' and
749 `recentf-show-basenames' filters."
750 (recentf-show-basenames (recentf-sort-basenames-ascending l)))
752 (defsubst recentf-show-basenames-descending (l)
753 "Filter the list of menu elements L to show filenames sans directory.
754 Filenames are sorted in descending order.
755 This filter combines the `recentf-sort-basenames-descending' and
756 `recentf-show-basenames' filters."
757 (recentf-show-basenames (recentf-sort-basenames-descending l)))
759 (defun recentf-relative-filter (l)
760 "Filter the list of menu-elements L to show relative filenames.
761 Filenames are relative to the `default-directory'."
762 (mapcar #'(lambda (menu-element)
763 (let* ((ful (recentf-menu-element-value menu-element))
764 (rel (file-relative-name ful default-directory)))
765 (if (string-match "^\\.\\." rel)
766 menu-element
767 (recentf-make-menu-element rel ful))))
770 ;;; Rule based menu filters
772 (defcustom recentf-arrange-rules
774 ("Elisp files (%d)" ".\\.el\\'")
775 ("Java files (%d)" ".\\.java\\'")
776 ("C/C++ files (%d)" "c\\(pp\\)?\\'")
778 "List of rules used by `recentf-arrange-by-rule' to build sub-menus.
779 A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
780 displayed title of the sub-menu where a `%d' `format' pattern is
781 replaced by the number of items in the sub-menu. MATCHER is a regexp
782 or a list of regexps. Items matching one of the regular expressions in
783 MATCHER are added to the corresponding sub-menu.
784 SUB-MENU-TITLE can be a function. It is passed every items that
785 matched the corresponding MATCHER, and it must return a
786 pair (SUB-MENU-TITLE . ITEM). SUB-MENU-TITLE is a computed sub-menu
787 title that can be another function. ITEM is the received item which
788 may have been modified to match another rule."
789 :group 'recentf-filters
790 :type '(repeat (cons (choice string function)
791 (repeat regexp))))
793 (defcustom recentf-arrange-by-rule-others "Other files (%d)"
794 "Title of the `recentf-arrange-by-rule' sub-menu.
795 This is for the menu where items that don't match any
796 `recentf-arrange-rules' are displayed. If nil these items are
797 displayed in the main recent files menu. A `%d' `format' pattern in
798 the title is replaced by the number of items in the sub-menu."
799 :group 'recentf-filters
800 :type '(choice (const :tag "Main menu" nil)
801 (string :tag "Title")))
803 (defcustom recentf-arrange-by-rules-min-items 0
804 "Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
805 If the number of items in a sub-menu is less than this value the
806 corresponding sub-menu items are displayed in the main recent files
807 menu or in the `recentf-arrange-by-rule-others' sub-menu if
808 defined."
809 :group 'recentf-filters
810 :type 'number)
812 (defcustom recentf-arrange-by-rule-subfilter nil
813 "Function called by a rule based filter to filter sub-menu elements.
814 A nil value means no filter. See also `recentf-menu-filter'.
815 You can't use another rule based filter here."
816 :group 'recentf-filters
817 :type '(choice (const nil) function)
818 :set (lambda (variable value)
819 (when (memq value '(recentf-arrange-by-rule
820 recentf-arrange-by-mode
821 recentf-arrange-by-dir))
822 (error "Recursive use of a rule based filter"))
823 (set-default variable value)))
825 (defun recentf-match-rule (file)
826 "Return the rule that match FILE."
827 (let ((rules recentf-arrange-rules)
828 match found)
829 (while (and (not found) rules)
830 (setq match (cdar rules))
831 (when (stringp match)
832 (setq match (list match)))
833 (while (and match (not (string-match (car match) file)))
834 (setq match (cdr match)))
835 (if match
836 (setq found (cons (caar rules) file))
837 (setq rules (cdr rules))))
838 found))
840 (defun recentf-arrange-by-rule (l)
841 "Filter the list of menu-elements L.
842 Arrange them in sub-menus following rules in `recentf-arrange-rules'."
843 (when recentf-arrange-rules
844 (let (menus others menu file min count)
845 ;; Put menu items into sub-menus as defined by rules.
846 (dolist (elt l)
847 (setq file (recentf-menu-element-value elt)
848 menu (recentf-match-rule file))
849 (while (functionp (car menu))
850 (setq menu (funcall (car menu) (cdr menu))))
851 (if (not (stringp (car menu)))
852 (push elt others)
853 (setq menu (or (assoc (car menu) menus)
854 (car (push (list (car menu)) menus))))
855 (recentf-set-menu-element-value
856 menu (cons elt (recentf-menu-element-value menu)))))
857 ;; Finalize each sub-menu:
858 ;; - truncate it depending on the value of
859 ;; `recentf-arrange-by-rules-min-items',
860 ;; - replace %d by the number of menu items,
861 ;; - apply `recentf-arrange-by-rule-subfilter' to menu items.
862 (setq min (if (natnump recentf-arrange-by-rules-min-items)
863 recentf-arrange-by-rules-min-items 0)
864 l nil)
865 (dolist (elt menus)
866 (setq menu (recentf-menu-element-value elt)
867 count (length menu))
868 (if (< count min)
869 (setq others (nconc menu others))
870 (recentf-set-menu-element-item
871 elt (format (recentf-menu-element-item elt) count))
872 (recentf-set-menu-element-value
873 elt (recentf-apply-menu-filter
874 recentf-arrange-by-rule-subfilter (nreverse menu)))
875 (push elt l)))
876 ;; Add the menu items remaining in the `others' bin.
877 (when (setq others (nreverse others))
878 (setq l (nconc
880 ;; Put items in an sub menu.
881 (if (stringp recentf-arrange-by-rule-others)
882 (list
883 (recentf-make-menu-element
884 (format recentf-arrange-by-rule-others
885 (length others))
886 (recentf-apply-menu-filter
887 recentf-arrange-by-rule-subfilter others)))
888 ;; Append items to the main menu.
889 (recentf-apply-menu-filter
890 recentf-arrange-by-rule-subfilter others)))))))
893 ;;; Predefined rule based menu filters
895 (defun recentf-indirect-mode-rule (file)
896 "Apply a second level `auto-mode-alist' regexp to FILE."
897 (recentf-match-rule (substring file 0 (match-beginning 0))))
899 (defun recentf-build-mode-rules ()
900 "Convert `auto-mode-alist' to menu filter rules.
901 Rules obey `recentf-arrange-rules' format."
902 (let ((case-fold-search recentf-case-fold-search)
903 regexp rule-name rule rules)
904 (dolist (mode auto-mode-alist)
905 (setq regexp (car mode)
906 mode (cdr mode))
907 (when mode
908 (cond
909 ;; Build a special "strip suffix" rule from entries of the
910 ;; form (REGEXP FUNCTION NON-NIL). Notice that FUNCTION is
911 ;; ignored by the menu filter. So in some corner cases a
912 ;; wrong mode could be guessed.
913 ((and (consp mode) (cadr mode))
914 (setq rule-name 'recentf-indirect-mode-rule))
915 ((and mode (symbolp mode))
916 (setq rule-name (symbol-name mode))
917 (if (string-match "\\(.*\\)-mode$" rule-name)
918 (setq rule-name (match-string 1 rule-name)))
919 (setq rule-name (concat rule-name " (%d)"))))
920 (setq rule (assoc rule-name rules))
921 (if rule
922 (setcdr rule (cons regexp (cdr rule)))
923 (push (list rule-name regexp) rules))))
924 ;; It is important to preserve auto-mode-alist order
925 ;; to ensure the right file <-> mode association
926 (nreverse rules)))
928 (defun recentf-arrange-by-mode (l)
929 "Split the list of menu-elements L into sub-menus by major mode."
930 (let ((recentf-arrange-rules (recentf-build-mode-rules))
931 (recentf-arrange-by-rule-others "others (%d)"))
932 (recentf-arrange-by-rule l)))
934 (defun recentf-file-name-nondir (l)
935 "Filter the list of menu-elements L to show filenames sans directory.
936 This simplified version of `recentf-show-basenames' does not handle
937 duplicates. It is used by `recentf-arrange-by-dir' as its
938 `recentf-arrange-by-rule-subfilter'."
939 (mapcar #'(lambda (e)
940 (recentf-make-menu-element
941 (file-name-nondirectory (recentf-menu-element-value e))
942 (recentf-menu-element-value e)))
945 (defun recentf-dir-rule (file)
946 "Return as a sub-menu, the directory FILE belongs to."
947 (cons (file-name-directory file) file))
949 (defun recentf-arrange-by-dir (l)
950 "Split the list of menu-elements L into sub-menus by directory."
951 (let ((recentf-arrange-rules '((recentf-dir-rule . ".*")))
952 (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
953 recentf-arrange-by-rule-others)
954 (recentf-arrange-by-rule l)))
956 ;;; Menu of menu filters
958 (defvar recentf-filter-changer-current nil
959 "Current filter used by `recentf-filter-changer'.")
961 (defcustom recentf-filter-changer-alist
963 (recentf-arrange-by-mode . "Grouped by Mode")
964 (recentf-arrange-by-dir . "Grouped by Directory")
965 (recentf-arrange-by-rule . "Grouped by Custom Rules")
967 "List of filters managed by `recentf-filter-changer'.
968 Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is
969 the filter function, and LABEL is the menu item displayed to select
970 that filter."
971 :group 'recentf-filters
972 :type '(repeat (cons function string))
973 :set (lambda (variable value)
974 (setq recentf-filter-changer-current nil)
975 (set-default variable value)))
977 (defun recentf-filter-changer-select (filter)
978 "Select FILTER as the current menu filter.
979 See `recentf-filter-changer'."
980 (setq recentf-filter-changer-current filter))
982 (defun recentf-filter-changer (l)
983 "Manage a sub-menu of menu filters.
984 `recentf-filter-changer-alist' defines the filters in the menu.
985 Filtering of L is delegated to the selected filter in the menu."
986 (unless recentf-filter-changer-current
987 (setq recentf-filter-changer-current
988 (caar recentf-filter-changer-alist)))
989 (if (not recentf-filter-changer-current)
991 (setq recentf-menu-filter-commands
992 (list
993 `("Show files"
994 ,@(mapcar
995 #'(lambda (f)
996 `[,(cdr f)
997 (setq recentf-filter-changer-current ',(car f))
998 ;;:active t
999 :style radio ;;radio Don't work with GTK :-(
1000 :selected (eq recentf-filter-changer-current
1001 ',(car f))
1002 ;;:help ,(cdr f)
1004 recentf-filter-changer-alist))))
1005 (recentf-apply-menu-filter recentf-filter-changer-current l)))
1007 ;;; Hooks
1009 (defun recentf-track-opened-file ()
1010 "Insert the name of the file just opened or written into the recent list."
1011 (and buffer-file-name
1012 (recentf-add-file buffer-file-name))
1013 ;; Must return nil because it is run from `write-file-functions'.
1014 nil)
1016 (defun recentf-track-closed-file ()
1017 "Update the recent list when a buffer is killed.
1018 That is, remove a non kept file from the recent list."
1019 (and buffer-file-name
1020 (recentf-remove-if-non-kept buffer-file-name)))
1022 (defconst recentf-used-hooks
1024 (find-file-hook recentf-track-opened-file)
1025 (write-file-functions recentf-track-opened-file)
1026 (kill-buffer-hook recentf-track-closed-file)
1027 (kill-emacs-hook recentf-save-list)
1029 "Hooks used by recentf.")
1031 ;;; Commands
1034 ;;; Common dialog stuff
1036 (defun recentf-cancel-dialog (&rest _ignore)
1037 "Cancel the current dialog.
1038 IGNORE arguments."
1039 (interactive)
1040 (kill-buffer (current-buffer))
1041 (message "Dialog canceled"))
1043 (defun recentf-dialog-goto-first (widget-type)
1044 "Move the cursor to the first WIDGET-TYPE in current dialog.
1045 Go to the beginning of buffer if not found."
1046 (goto-char (point-min))
1047 (condition-case nil
1048 (let (done)
1049 (widget-move 1)
1050 (while (not done)
1051 (if (eq widget-type (widget-type (widget-at (point))))
1052 (setq done t)
1053 (widget-move 1))))
1054 (error
1055 (goto-char (point-min)))))
1057 (defvar recentf-dialog-mode-map
1058 (let ((km (copy-keymap recentf--shortcuts-keymap)))
1059 (set-keymap-parent km widget-keymap)
1060 (define-key km "q" 'recentf-cancel-dialog)
1061 (define-key km "n" 'next-line)
1062 (define-key km "p" 'previous-line)
1064 "Keymap used in recentf dialogs.")
1066 (define-derived-mode recentf-dialog-mode nil "recentf-dialog"
1067 "Major mode of recentf dialogs.
1069 \\{recentf-dialog-mode-map}"
1070 :syntax-table nil
1071 :abbrev-table nil
1072 (setq truncate-lines t))
1074 (defmacro recentf-dialog (name &rest forms)
1075 "Show a dialog buffer with NAME, setup with FORMS."
1076 (declare (indent 1) (debug t))
1077 `(with-current-buffer (get-buffer-create ,name)
1078 ;; Cleanup buffer
1079 (let ((inhibit-read-only t)
1080 (ol (overlay-lists)))
1081 (mapc 'delete-overlay (car ol))
1082 (mapc 'delete-overlay (cdr ol))
1083 (erase-buffer))
1084 (recentf-dialog-mode)
1085 ,@forms
1086 (widget-setup)
1087 (switch-to-buffer (current-buffer))))
1089 ;;; Edit list dialog
1091 (defvar recentf-edit-list nil)
1093 (defun recentf-edit-list-select (widget &rest _ignore)
1094 "Toggle a file selection based on the checkbox WIDGET state.
1095 IGNORE other arguments."
1096 (let ((value (widget-get widget :tag))
1097 (check (widget-value widget)))
1098 (if check
1099 (add-to-list 'recentf-edit-list value)
1100 (setq recentf-edit-list (delq value recentf-edit-list)))
1101 (message "%s %sselected" value (if check "" "un"))))
1103 (defun recentf-edit-list-validate (&rest _ignore)
1104 "Process the recent list when the edit list dialog is committed.
1105 IGNORE arguments."
1106 (if recentf-edit-list
1107 (let ((i 0))
1108 (dolist (e recentf-edit-list)
1109 (setq recentf-list (delq e recentf-list)
1110 i (1+ i)))
1111 (kill-buffer (current-buffer))
1112 (message "%S file(s) removed from the list" i))
1113 (message "No file selected")))
1115 (defun recentf-edit-list ()
1116 "Show a dialog to delete selected files from the recent list."
1117 (interactive)
1118 (unless recentf-list
1119 (error "The list of recent files is empty"))
1120 (recentf-dialog (format "*%s - Edit list*" recentf-menu-title)
1121 (set (make-local-variable 'recentf-edit-list) nil)
1122 (widget-insert
1123 (format-message
1124 "Click on OK to delete selected files from the recent list.
1125 Click on Cancel or type `q' to cancel.\n"))
1126 ;; Insert the list of files as checkboxes
1127 (dolist (item recentf-list)
1128 (widget-create 'checkbox
1129 :value nil ; unselected checkbox
1130 :format "\n %[%v%] %t"
1131 :tag item
1132 :notify 'recentf-edit-list-select))
1133 (widget-insert "\n\n")
1134 (widget-create
1135 'push-button
1136 :notify 'recentf-edit-list-validate
1137 :help-echo "Delete selected files from the recent list"
1138 "Ok")
1139 (widget-insert " ")
1140 (widget-create
1141 'push-button
1142 :notify 'recentf-cancel-dialog
1143 "Cancel")
1144 (recentf-dialog-goto-first 'checkbox)))
1146 ;;; Open file dialog
1148 (defun recentf-open-files-action (widget &rest _ignore)
1149 "Open the file stored in WIDGET's value when notified.
1150 IGNORE other arguments."
1151 (kill-buffer (current-buffer))
1152 (funcall recentf-menu-action (widget-value widget)))
1154 ;; List of files associated to a digit shortcut key.
1155 (defvar recentf--files-with-key nil)
1157 (defun recentf-show-digit-shortcut-filter (l)
1158 "Filter the list of menu-elements L to show digit shortcuts."
1159 (let ((i 0))
1160 (dolist (e l)
1161 (setq i (1+ i))
1162 (recentf-set-menu-element-item
1163 e (format "[%d] %s" (% i 10) (recentf-menu-element-item e))))
1166 (defun recentf-open-files-item (menu-element)
1167 "Return a widget to display MENU-ELEMENT in a dialog buffer."
1168 (if (consp (cdr menu-element))
1169 ;; Represent a sub-menu with a tree widget
1170 `(tree-widget
1171 :open t
1172 :match ignore
1173 :node (item :tag ,(car menu-element)
1174 :sample-face bold
1175 :format "%{%t%}:\n")
1176 ,@(mapcar 'recentf-open-files-item
1177 (cdr menu-element)))
1178 ;; Represent a single file with a link widget
1179 `(link :tag ,(car menu-element)
1180 :button-prefix ""
1181 :button-suffix ""
1182 :button-face default
1183 :format "%[%t\n%]"
1184 :help-echo ,(concat "Open " (cdr menu-element))
1185 :action recentf-open-files-action
1186 ;; Override the (problematic) follow-link property of the
1187 ;; `link' widget (bug#22434).
1188 :follow-link nil
1189 ,(cdr menu-element))))
1191 (defun recentf-open-files-items (files)
1192 "Return a list of widgets to display FILES in a dialog buffer."
1193 (set (make-local-variable 'recentf--files-with-key)
1194 (recentf-trunc-list files 10))
1195 (mapcar 'recentf-open-files-item
1196 (append
1197 ;; When requested group the files with shortcuts together
1198 ;; at the top of the list.
1199 (when recentf-show-file-shortcuts-flag
1200 (setq files (nthcdr 10 files))
1201 (recentf-apply-menu-filter
1202 'recentf-show-digit-shortcut-filter
1203 (mapcar 'recentf-make-default-menu-element
1204 recentf--files-with-key)))
1205 ;; Then the other files.
1206 (recentf-apply-menu-filter
1207 recentf-menu-filter
1208 (mapcar 'recentf-make-default-menu-element
1209 files)))))
1211 (defun recentf-open-files (&optional files buffer-name)
1212 "Show a dialog to open a recent file.
1213 If optional argument FILES is non-nil, it is a list of recently-opened
1214 files to choose from. It defaults to the whole recent list.
1215 If optional argument BUFFER-NAME is non-nil, it is a buffer name to
1216 use for the dialog. It defaults to \"*`recentf-menu-title'*\"."
1217 (interactive)
1218 (unless (or files recentf-list)
1219 (error "There is no recent file to open"))
1220 (recentf-dialog (or buffer-name (format "*%s*" recentf-menu-title))
1221 (widget-insert "Click on a file"
1222 (if recentf-show-file-shortcuts-flag
1223 ", or type the corresponding digit key,"
1225 " to open it.\n"
1226 (format-message "Click on Cancel or type `q' to cancel.\n"))
1227 ;; Use a L&F that looks like the recentf menu.
1228 (tree-widget-set-theme "folder")
1229 (apply 'widget-create
1230 `(group
1231 :indent 2
1232 :format "\n%v\n"
1233 ,@(recentf-open-files-items (or files recentf-list))))
1234 (widget-create
1235 'push-button
1236 :notify 'recentf-cancel-dialog
1237 "Cancel")
1238 (recentf-dialog-goto-first 'link)))
1240 (defun recentf-open-more-files ()
1241 "Show a dialog to open a recent file that is not in the menu."
1242 (interactive)
1243 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
1244 (format "*%s - More*" recentf-menu-title)))
1246 (defun recentf-open-most-recent-file (&optional n)
1247 "Open the Nth most recent file.
1248 Optional argument N must be a valid digit number. It defaults to 1.
1249 1 opens the most recent file, 2 the second most recent one, etc..
1250 0 opens the tenth most recent file."
1251 (interactive "p")
1252 (cond
1253 ((zerop n) (setq n 10))
1254 ((and (> n 0) (< n 10)))
1255 ((error "Recent file number out of range [0-9], %d" n)))
1256 (let ((file (nth (1- n) (or recentf--files-with-key recentf-list))))
1257 (unless file (error "Not that many recent files"))
1258 ;; Close the open files dialog.
1259 (when recentf--files-with-key
1260 (kill-buffer (current-buffer)))
1261 (funcall recentf-menu-action file)))
1263 ;;; Save/load/cleanup the recent list
1265 (defconst recentf-save-file-header
1266 ";;; Automatically generated by `recentf' on %s.\n"
1267 "Header to be written into the `recentf-save-file'.")
1269 (defconst recentf-save-file-coding-system
1270 (if (coding-system-p 'utf-8-emacs)
1271 'utf-8-emacs
1272 'emacs-mule)
1273 "Coding system of the file `recentf-save-file'.")
1275 (defun recentf-save-list ()
1276 "Save the recent list.
1277 Write data into the file specified by `recentf-save-file'."
1278 (interactive)
1279 (condition-case error
1280 (with-temp-buffer
1281 (erase-buffer)
1282 (set-buffer-file-coding-system recentf-save-file-coding-system)
1283 (insert (format-message recentf-save-file-header
1284 (current-time-string)))
1285 (recentf-dump-variable 'recentf-list recentf-max-saved-items)
1286 (recentf-dump-variable 'recentf-filter-changer-current)
1287 (insert "\n\f\n;; Local Variables:\n"
1288 (format ";; coding: %s\n" recentf-save-file-coding-system)
1289 ";; End:\n")
1290 (write-file (expand-file-name recentf-save-file))
1291 (when recentf-save-file-modes
1292 (set-file-modes recentf-save-file recentf-save-file-modes))
1293 nil)
1294 (error
1295 (warn "recentf mode: %s" (error-message-string error)))))
1297 (defun recentf-load-list ()
1298 "Load a previously saved recent list.
1299 Read data from the file specified by `recentf-save-file'.
1300 When `recentf-initialize-file-name-history' is non-nil, initialize an
1301 empty `file-name-history' with the recent list."
1302 (interactive)
1303 (let ((file (expand-file-name recentf-save-file))
1304 ;; We do not want Tramp asking for passwords.
1305 (non-essential t))
1306 (when (file-readable-p file)
1307 (load-file file)
1308 (and recentf-initialize-file-name-history
1309 (not file-name-history)
1310 (setq file-name-history (mapcar 'abbreviate-file-name
1311 recentf-list))))))
1313 (defun recentf-cleanup ()
1314 "Cleanup the recent list.
1315 That is, remove duplicates, non-kept, and excluded files."
1316 (interactive)
1317 (message "Cleaning up the recentf list...")
1318 (let ((n 0)
1319 (ht (make-hash-table
1320 :size recentf-max-saved-items
1321 :test 'equal))
1322 newlist key)
1323 (dolist (f recentf-list)
1324 (setq f (recentf-expand-file-name f)
1325 key (if recentf-case-fold-search (downcase f) f))
1326 (if (and (recentf-include-p f)
1327 (recentf-keep-p f)
1328 (not (gethash key ht)))
1329 (progn
1330 (push f newlist)
1331 (puthash key t ht))
1332 (setq n (1+ n))
1333 (message "File %s removed from the recentf list" f)))
1334 (message "Cleaning up the recentf list...done (%d removed)" n)
1335 (setq recentf-list (nreverse newlist))))
1337 ;;; The minor mode
1339 (defvar recentf-mode-map (make-sparse-keymap)
1340 "Keymap to use in recentf mode.")
1342 ;;;###autoload
1343 (define-minor-mode recentf-mode
1344 "Toggle \"Open Recent\" menu (Recentf mode).
1345 With a prefix argument ARG, enable Recentf mode if ARG is
1346 positive, and disable it otherwise. If called from Lisp, enable
1347 Recentf mode if ARG is omitted or nil.
1349 When Recentf mode is enabled, a \"Open Recent\" submenu is
1350 displayed in the \"File\" menu, containing a list of files that
1351 were operated on recently."
1352 :global t
1353 :group 'recentf
1354 :keymap recentf-mode-map
1355 (unless (and recentf-mode (recentf-enabled-p))
1356 (if recentf-mode
1357 (progn
1358 (recentf-load-list)
1359 (recentf-show-menu))
1360 (recentf-hide-menu)
1361 (recentf-save-list))
1362 (recentf-auto-cleanup)
1363 (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
1364 (dolist (hook recentf-used-hooks)
1365 (apply hook-setup hook)))))
1367 (defun recentf-unload-function ()
1368 "Unload the recentf library."
1369 (recentf-mode -1)
1370 ;; continue standard unloading
1371 nil)
1373 (provide 'recentf)
1375 (run-hooks 'recentf-load-hook)
1377 ;;; recentf.el ends here