gnus-article-html: Decode contents by charset.
[emacs.git] / lisp / recentf.el
blob817434bdd555d18a280b5cdcda1db830e9fd9280
1 ;;; recentf.el --- setup a menu of recently opened files
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: David Ponce <david@dponce.com>
7 ;; Created: July 19 1999
8 ;; Keywords: files
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This package maintains a menu for visiting files that were operated
28 ;; on recently. When enabled a new "Open Recent" sub menu is
29 ;; displayed in the "File" menu. The recent files list is
30 ;; automatically saved across Emacs sessions. You can customize the
31 ;; number of recent files displayed, the location of the menu and
32 ;; others options (see the source code for details).
34 ;; To enable this package, add the following to your .emacs:
35 ;; (recentf-mode 1)
37 ;;; History:
40 ;;; Code:
41 (require 'easymenu)
42 (require 'tree-widget)
43 (require 'timer)
45 ;;; Internal data
47 (defvar recentf-list nil
48 "List of recently opened files.")
50 (defsubst recentf-enabled-p ()
51 "Return non-nil if recentf mode is currently enabled."
52 (memq 'recentf-save-list kill-emacs-hook))
54 ;;; Customization
56 (defgroup recentf nil
57 "Maintain a menu of recently opened files."
58 :version "21.1"
59 :group 'files)
61 (defgroup recentf-filters nil
62 "Group to customize recentf menu filters.
63 You should define the options of your own filters in this group."
64 :group 'recentf)
66 (defcustom recentf-max-saved-items 20
67 "Maximum number of items of the recent list that will be saved.
68 A nil value means to save the whole list.
69 See the command `recentf-save-list'."
70 :group 'recentf
71 :type 'integer)
73 (defcustom recentf-save-file (convert-standard-filename "~/.recentf")
74 "File to save the recent list into."
75 :group 'recentf
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 (setq keepit (condition-case nil
416 (not (if (stringp (car checks))
417 ;; A regexp
418 (string-match (car checks) filename)
419 ;; A predicate
420 (funcall (car checks) filename)))
421 (error nil))
422 checks (cdr checks)))
423 keepit))
425 (defun recentf-keep-p (filename)
426 "Return non-nil if FILENAME should be kept in the recent list.
427 That is, if it matches any of the `recentf-keep' checks."
428 (let* ((case-fold-search recentf-case-fold-search)
429 (checks recentf-keep)
430 (keepit (null checks)))
431 (while (and checks (not keepit))
432 (setq keepit (condition-case nil
433 (if (stringp (car checks))
434 ;; A regexp
435 (string-match (car checks) filename)
436 ;; A predicate
437 (funcall (car checks) filename))
438 (error nil))
439 checks (cdr checks)))
440 keepit))
442 (defsubst recentf-add-file (filename)
443 "Add or move FILENAME at the beginning of the recent list.
444 Does nothing if the name satisfies any of the `recentf-exclude'
445 regexps or predicates."
446 (setq filename (recentf-expand-file-name filename))
447 (when (recentf-include-p filename)
448 (recentf-push filename)))
450 (defsubst recentf-remove-if-non-kept (filename)
451 "Remove FILENAME from the recent list, if file is not kept.
452 Return non-nil if FILENAME has been removed."
453 (unless (recentf-keep-p filename)
454 (let ((m (recentf-string-member
455 (recentf-expand-file-name filename) recentf-list)))
456 (and m (setq recentf-list (delq (car m) recentf-list))))))
458 (defsubst recentf-directory-compare (f1 f2)
459 "Compare absolute filenames F1 and F2.
460 First compare directories, then filenames sans directory.
461 Return non-nil if F1 is less than F2."
462 (let ((d1 (file-name-directory f1))
463 (d2 (file-name-directory f2)))
464 (if (recentf-string-equal d1 d2)
465 (recentf-string-lessp (file-name-nondirectory f1)
466 (file-name-nondirectory f2))
467 (recentf-string-lessp d1 d2))))
469 ;;; Menu building
471 (defsubst recentf-digit-shortcut-command-name (n)
472 "Return a command name to open the Nth most recent file.
473 See also the command `recentf-open-most-recent-file'."
474 (intern (format "recentf-open-most-recent-file-%d" n)))
476 (defvar recentf--shortcuts-keymap
477 (let ((km (make-sparse-keymap)))
478 (dolist (k '(0 9 8 7 6 5 4 3 2 1))
479 (let ((cmd (recentf-digit-shortcut-command-name k)))
480 ;; Define a shortcut command.
481 (defalias cmd
482 `(lambda ()
483 (interactive)
484 (recentf-open-most-recent-file ,k)))
485 ;; Bind it to a digit key.
486 (define-key km (vector (+ k ?0)) cmd)))
488 "Digit shortcuts keymap.")
490 (defvar recentf-menu-items-for-commands
491 (list
492 ["Cleanup list"
493 recentf-cleanup
494 :help "Remove duplicates, and obsoletes files from the recent list"
495 :active t]
496 ["Edit list..."
497 recentf-edit-list
498 :help "Manually remove files from the recent list"
499 :active t]
500 ["Save list now"
501 recentf-save-list
502 :help "Save the list of recently opened files now"
503 :active t]
504 ["Options..."
505 (customize-group "recentf")
506 :help "Customize recently opened files menu and options"
507 :active t]
509 "List of menu items for recentf commands.")
511 (defvar recentf-menu-filter-commands nil
512 "This variable can be used by menu filters to setup their own command menu.
513 If non-nil it must contain a list of valid menu-items to be appended
514 to the recent file list part of the menu. Before calling a menu
515 filter function this variable is reset to nil.")
517 (defsubst recentf-elements (n)
518 "Return a list of the first N elements of the recent list."
519 (recentf-trunc-list recentf-list n))
521 (defsubst recentf-make-menu-element (menu-item menu-value)
522 "Create a new menu-element.
523 A menu element is a pair (MENU-ITEM . MENU-VALUE), where MENU-ITEM is
524 the menu item string displayed. MENU-VALUE is the file to be open
525 when the corresponding MENU-ITEM is selected. Or it is a
526 pair (SUB-MENU-TITLE . MENU-ELEMENTS) where SUB-MENU-TITLE is a
527 sub-menu title and MENU-ELEMENTS is the list of menu elements in the
528 sub-menu."
529 (cons menu-item menu-value))
531 (defsubst recentf-menu-element-item (e)
532 "Return the item part of the menu-element E."
533 (car e))
535 (defsubst recentf-menu-element-value (e)
536 "Return the value part of the menu-element E."
537 (cdr e))
539 (defsubst recentf-set-menu-element-item (e item)
540 "Change the item part of menu-element E to ITEM."
541 (setcar e item))
543 (defsubst recentf-set-menu-element-value (e value)
544 "Change the value part of menu-element E to VALUE."
545 (setcdr e value))
547 (defsubst recentf-sub-menu-element-p (e)
548 "Return non-nil if menu-element E defines a sub-menu."
549 (consp (recentf-menu-element-value e)))
551 (defsubst recentf-make-default-menu-element (file)
552 "Make a new default menu element with FILE.
553 This a menu element (FILE . FILE)."
554 (recentf-make-menu-element file file))
556 (defsubst recentf-menu-elements (n)
557 "Return a list of the first N default menu elements from the recent list.
558 See also `recentf-make-default-menu-element'."
559 (mapcar 'recentf-make-default-menu-element
560 (recentf-elements n)))
562 (defun recentf-apply-menu-filter (filter l)
563 "Apply function FILTER to the list of menu-elements L.
564 It takes care of sub-menu elements in L and recursively apply FILTER
565 to them. It is guaranteed that FILTER receives only a list of single
566 menu-elements (no sub-menu)."
567 (if (and l (functionp filter))
568 (let ((case-fold-search recentf-case-fold-search)
569 elts others)
570 ;; split L into two sub-listes, one of sub-menus elements and
571 ;; another of single menu elements.
572 (dolist (elt l)
573 (if (recentf-sub-menu-element-p elt)
574 (push elt elts)
575 (push elt others)))
576 ;; Apply FILTER to single elements.
577 (when others
578 (setq others (funcall filter (nreverse others))))
579 ;; Apply FILTER to sub-menu elements.
580 (setq l nil)
581 (dolist (elt elts)
582 (recentf-set-menu-element-value
583 elt (recentf-apply-menu-filter
584 filter (recentf-menu-element-value elt)))
585 (push elt l))
586 ;; Return the new filtered menu element list.
587 (nconc l others))
590 ;; Count the number of assigned menu shortcuts.
591 (defvar recentf-menu-shortcuts)
593 (defun recentf-make-menu-items (&optional menu)
594 "Make menu items from the recent list.
595 This is a menu filter function which ignores the MENU argument."
596 (setq recentf-menu-filter-commands nil)
597 (let* ((recentf-menu-shortcuts 0)
598 (file-items
599 (condition-case err
600 (mapcar 'recentf-make-menu-item
601 (recentf-apply-menu-filter
602 recentf-menu-filter
603 (recentf-menu-elements recentf-max-menu-items)))
604 (error
605 (message "recentf update menu failed: %s"
606 (error-message-string err))))))
607 (append
608 (or file-items
609 '(["No files" t
610 :help "No recent file to open"
611 :active nil]))
612 (if recentf-menu-open-all-flag
613 '(["All..." recentf-open-files
614 :help "Open recent files through a dialog"
615 :active t])
616 (and (< recentf-max-menu-items (length recentf-list))
617 '(["More..." recentf-open-more-files
618 :help "Open files not in the menu through a dialog"
619 :active t])))
620 (and recentf-menu-filter-commands '("---"))
621 recentf-menu-filter-commands
622 (and recentf-menu-items-for-commands '("---"))
623 recentf-menu-items-for-commands)))
625 (defun recentf-menu-value-shortcut (name)
626 "Return a shortcut digit for file NAME.
627 Return nil if file NAME is not one of the ten more recent."
628 (let ((i 0) k)
629 (while (and (not k) (< i 10))
630 (if (string-equal name (nth i recentf-list))
631 (progn
632 (setq recentf-menu-shortcuts (1+ recentf-menu-shortcuts))
633 (setq k (% (1+ i) 10)))
634 (setq i (1+ i))))
637 (defun recentf-make-menu-item (elt)
638 "Make a menu item from menu element ELT."
639 (let ((item (recentf-menu-element-item elt))
640 (value (recentf-menu-element-value elt)))
641 (if (recentf-sub-menu-element-p elt)
642 (cons item (mapcar 'recentf-make-menu-item value))
643 (let ((k (and (< recentf-menu-shortcuts 10)
644 (recentf-menu-value-shortcut value))))
645 (vector item
646 ;; If the file name is one of the ten more recent, use
647 ;; a digit shortcut command to open it, else use an
648 ;; anonymous command.
649 (if k
650 (recentf-digit-shortcut-command-name k)
651 `(lambda ()
652 (interactive)
653 (,recentf-menu-action ,value)))
654 :help (concat "Open " value)
655 :active t)))))
657 (defsubst recentf-menu-bar ()
658 "Return the keymap of the global menu bar."
659 (lookup-key global-map [menu-bar]))
661 (defun recentf-show-menu ()
662 "Show the menu of recently opened files."
663 (easy-menu-add-item
664 (recentf-menu-bar) recentf-menu-path
665 (list recentf-menu-title :filter 'recentf-make-menu-items)
666 recentf-menu-before))
668 (defun recentf-hide-menu ()
669 "Hide the menu of recently opened files."
670 (easy-menu-remove-item (recentf-menu-bar) recentf-menu-path
671 recentf-menu-title))
673 ;;; Predefined menu filters
675 (defsubst recentf-sort-ascending (l)
676 "Sort the list of menu elements L in ascending order.
677 The MENU-ITEM part of each menu element is compared."
678 (sort (copy-sequence l)
679 #'(lambda (e1 e2)
680 (recentf-string-lessp
681 (recentf-menu-element-item e1)
682 (recentf-menu-element-item e2)))))
684 (defsubst recentf-sort-descending (l)
685 "Sort the list of menu elements L in descending order.
686 The MENU-ITEM part of each menu element is compared."
687 (sort (copy-sequence l)
688 #'(lambda (e1 e2)
689 (recentf-string-lessp
690 (recentf-menu-element-item e2)
691 (recentf-menu-element-item e1)))))
693 (defsubst recentf-sort-basenames-ascending (l)
694 "Sort the list of menu elements L in ascending order.
695 Only filenames sans directory are compared."
696 (sort (copy-sequence l)
697 #'(lambda (e1 e2)
698 (recentf-string-lessp
699 (file-name-nondirectory (recentf-menu-element-value e1))
700 (file-name-nondirectory (recentf-menu-element-value e2))))))
702 (defsubst recentf-sort-basenames-descending (l)
703 "Sort the list of menu elements L in descending order.
704 Only filenames sans directory are compared."
705 (sort (copy-sequence l)
706 #'(lambda (e1 e2)
707 (recentf-string-lessp
708 (file-name-nondirectory (recentf-menu-element-value e2))
709 (file-name-nondirectory (recentf-menu-element-value e1))))))
711 (defsubst recentf-sort-directories-ascending (l)
712 "Sort the list of menu elements L in ascending order.
713 Compares directories then filenames to order the list."
714 (sort (copy-sequence l)
715 #'(lambda (e1 e2)
716 (recentf-directory-compare
717 (recentf-menu-element-value e1)
718 (recentf-menu-element-value e2)))))
720 (defsubst recentf-sort-directories-descending (l)
721 "Sort the list of menu elements L in descending order.
722 Compares directories then filenames to order the list."
723 (sort (copy-sequence l)
724 #'(lambda (e1 e2)
725 (recentf-directory-compare
726 (recentf-menu-element-value e2)
727 (recentf-menu-element-value e1)))))
729 (defun recentf-show-basenames (l &optional no-dir)
730 "Filter the list of menu elements L to show filenames sans directory.
731 When a filename is duplicated, it is appended a sequence number if
732 optional argument NO-DIR is non-nil, or its directory otherwise."
733 (let (filtered-names filtered-list full name counters sufx)
734 (dolist (elt l (nreverse filtered-list))
735 (setq full (recentf-menu-element-value elt)
736 name (file-name-nondirectory full))
737 (if (not (member name filtered-names))
738 (push name filtered-names)
739 (if no-dir
740 (if (setq sufx (assoc name counters))
741 (setcdr sufx (1+ (cdr sufx)))
742 (setq sufx 1)
743 (push (cons name sufx) counters))
744 (setq sufx (file-name-directory full)))
745 (setq name (format "%s(%s)" name sufx)))
746 (push (recentf-make-menu-element name full) filtered-list))))
748 (defsubst recentf-show-basenames-ascending (l)
749 "Filter the list of menu elements L to show filenames sans directory.
750 Filenames are sorted in ascending order.
751 This filter combines the `recentf-sort-basenames-ascending' and
752 `recentf-show-basenames' filters."
753 (recentf-show-basenames (recentf-sort-basenames-ascending l)))
755 (defsubst recentf-show-basenames-descending (l)
756 "Filter the list of menu elements L to show filenames sans directory.
757 Filenames are sorted in descending order.
758 This filter combines the `recentf-sort-basenames-descending' and
759 `recentf-show-basenames' filters."
760 (recentf-show-basenames (recentf-sort-basenames-descending l)))
762 (defun recentf-relative-filter (l)
763 "Filter the list of menu-elements L to show relative filenames.
764 Filenames are relative to the `default-directory'."
765 (mapcar #'(lambda (menu-element)
766 (let* ((ful (recentf-menu-element-value menu-element))
767 (rel (file-relative-name ful default-directory)))
768 (if (string-match "^\\.\\." rel)
769 menu-element
770 (recentf-make-menu-element rel ful))))
773 ;;; Rule based menu filters
775 (defcustom recentf-arrange-rules
777 ("Elisp files (%d)" ".\\.el\\'")
778 ("Java files (%d)" ".\\.java\\'")
779 ("C/C++ files (%d)" "c\\(pp\\)?\\'")
781 "List of rules used by `recentf-arrange-by-rule' to build sub-menus.
782 A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
783 displayed title of the sub-menu where a '%d' `format' pattern is
784 replaced by the number of items in the sub-menu. MATCHER is a regexp
785 or a list of regexps. Items matching one of the regular expressions in
786 MATCHER are added to the corresponding sub-menu.
787 SUB-MENU-TITLE can be a function. It is passed every items that
788 matched the corresponding MATCHER, and it must return a
789 pair (SUB-MENU-TITLE . ITEM). SUB-MENU-TITLE is a computed sub-menu
790 title that can be another function. ITEM is the received item which
791 may have been modified to match another rule."
792 :group 'recentf-filters
793 :type '(repeat (cons (choice string function)
794 (repeat regexp))))
796 (defcustom recentf-arrange-by-rule-others "Other files (%d)"
797 "Title of the `recentf-arrange-by-rule' sub-menu.
798 This is for the menu where items that don't match any
799 `recentf-arrange-rules' are displayed. If nil these items are
800 displayed in the main recent files menu. A '%d' `format' pattern in
801 the title is replaced by the number of items in the sub-menu."
802 :group 'recentf-filters
803 :type '(choice (const :tag "Main menu" nil)
804 (string :tag "Title")))
806 (defcustom recentf-arrange-by-rules-min-items 0
807 "Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
808 If the number of items in a sub-menu is less than this value the
809 corresponding sub-menu items are displayed in the main recent files
810 menu or in the `recentf-arrange-by-rule-others' sub-menu if
811 defined."
812 :group 'recentf-filters
813 :type 'number)
815 (defcustom recentf-arrange-by-rule-subfilter nil
816 "Function called by a rule based filter to filter sub-menu elements.
817 A nil value means no filter. See also `recentf-menu-filter'.
818 You can't use another rule based filter here."
819 :group 'recentf-filters
820 :type '(choice (const nil) function)
821 :set (lambda (variable value)
822 (when (memq value '(recentf-arrange-by-rule
823 recentf-arrange-by-mode
824 recentf-arrange-by-dir))
825 (error "Recursive use of a rule based filter"))
826 (set-default variable value)))
828 (defun recentf-match-rule (file)
829 "Return the rule that match FILE."
830 (let ((rules recentf-arrange-rules)
831 match found)
832 (while (and (not found) rules)
833 (setq match (cdar rules))
834 (when (stringp match)
835 (setq match (list match)))
836 (while (and match (not (string-match (car match) file)))
837 (setq match (cdr match)))
838 (if match
839 (setq found (cons (caar rules) file))
840 (setq rules (cdr rules))))
841 found))
843 (defun recentf-arrange-by-rule (l)
844 "Filter the list of menu-elements L.
845 Arrange them in sub-menus following rules in `recentf-arrange-rules'."
846 (when recentf-arrange-rules
847 (let (menus others menu file min count)
848 ;; Put menu items into sub-menus as defined by rules.
849 (dolist (elt l)
850 (setq file (recentf-menu-element-value elt)
851 menu (recentf-match-rule file))
852 (while (functionp (car menu))
853 (setq menu (funcall (car menu) (cdr menu))))
854 (if (not (stringp (car menu)))
855 (push elt others)
856 (setq menu (or (assoc (car menu) menus)
857 (car (push (list (car menu)) menus))))
858 (recentf-set-menu-element-value
859 menu (cons elt (recentf-menu-element-value menu)))))
860 ;; Finalize each sub-menu:
861 ;; - truncate it depending on the value of
862 ;; `recentf-arrange-by-rules-min-items',
863 ;; - replace %d by the number of menu items,
864 ;; - apply `recentf-arrange-by-rule-subfilter' to menu items.
865 (setq min (if (natnump recentf-arrange-by-rules-min-items)
866 recentf-arrange-by-rules-min-items 0)
867 l nil)
868 (dolist (elt menus)
869 (setq menu (recentf-menu-element-value elt)
870 count (length menu))
871 (if (< count min)
872 (setq others (nconc menu others))
873 (recentf-set-menu-element-item
874 elt (format (recentf-menu-element-item elt) count))
875 (recentf-set-menu-element-value
876 elt (recentf-apply-menu-filter
877 recentf-arrange-by-rule-subfilter (nreverse menu)))
878 (push elt l)))
879 ;; Add the menu items remaining in the `others' bin.
880 (when (setq others (nreverse others))
881 (setq l (nconc
883 ;; Put items in an sub menu.
884 (if (stringp recentf-arrange-by-rule-others)
885 (list
886 (recentf-make-menu-element
887 (format recentf-arrange-by-rule-others
888 (length others))
889 (recentf-apply-menu-filter
890 recentf-arrange-by-rule-subfilter others)))
891 ;; Append items to the main menu.
892 (recentf-apply-menu-filter
893 recentf-arrange-by-rule-subfilter others)))))))
896 ;;; Predefined rule based menu filters
898 (defun recentf-indirect-mode-rule (file)
899 "Apply a second level `auto-mode-alist' regexp to FILE."
900 (recentf-match-rule (substring file 0 (match-beginning 0))))
902 (defun recentf-build-mode-rules ()
903 "Convert `auto-mode-alist' to menu filter rules.
904 Rules obey `recentf-arrange-rules' format."
905 (let ((case-fold-search recentf-case-fold-search)
906 regexp rule-name rule rules)
907 (dolist (mode auto-mode-alist)
908 (setq regexp (car mode)
909 mode (cdr mode))
910 (when mode
911 (cond
912 ;; Build a special "strip suffix" rule from entries of the
913 ;; form (REGEXP FUNCTION NON-NIL). Notice that FUNCTION is
914 ;; ignored by the menu filter. So in some corner cases a
915 ;; wrong mode could be guessed.
916 ((and (consp mode) (cadr mode))
917 (setq rule-name 'recentf-indirect-mode-rule))
918 ((and mode (symbolp mode))
919 (setq rule-name (symbol-name mode))
920 (if (string-match "\\(.*\\)-mode$" rule-name)
921 (setq rule-name (match-string 1 rule-name)))
922 (setq rule-name (concat rule-name " (%d)"))))
923 (setq rule (assoc rule-name rules))
924 (if rule
925 (setcdr rule (cons regexp (cdr rule)))
926 (push (list rule-name regexp) rules))))
927 ;; It is important to preserve auto-mode-alist order
928 ;; to ensure the right file <-> mode association
929 (nreverse rules)))
931 (defun recentf-arrange-by-mode (l)
932 "Split the list of menu-elements L into sub-menus by major mode."
933 (let ((recentf-arrange-rules (recentf-build-mode-rules))
934 (recentf-arrange-by-rule-others "others (%d)"))
935 (recentf-arrange-by-rule l)))
937 (defun recentf-file-name-nondir (l)
938 "Filter the list of menu-elements L to show filenames sans directory.
939 This simplified version of `recentf-show-basenames' does not handle
940 duplicates. It is used by `recentf-arrange-by-dir' as its
941 `recentf-arrange-by-rule-subfilter'."
942 (mapcar #'(lambda (e)
943 (recentf-make-menu-element
944 (file-name-nondirectory (recentf-menu-element-value e))
945 (recentf-menu-element-value e)))
948 (defun recentf-dir-rule (file)
949 "Return as a sub-menu, the directory FILE belongs to."
950 (cons (file-name-directory file) file))
952 (defun recentf-arrange-by-dir (l)
953 "Split the list of menu-elements L into sub-menus by directory."
954 (let ((recentf-arrange-rules '((recentf-dir-rule . ".*")))
955 (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
956 recentf-arrange-by-rule-others)
957 (recentf-arrange-by-rule l)))
959 ;;; Menu of menu filters
961 (defvar recentf-filter-changer-current nil
962 "Current filter used by `recentf-filter-changer'.")
964 (defcustom recentf-filter-changer-alist
966 (recentf-arrange-by-mode . "Grouped by Mode")
967 (recentf-arrange-by-dir . "Grouped by Directory")
968 (recentf-arrange-by-rule . "Grouped by Custom Rules")
970 "List of filters managed by `recentf-filter-changer'.
971 Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is
972 the filter function, and LABEL is the menu item displayed to select
973 that filter."
974 :group 'recentf-filters
975 :type '(repeat (cons function string))
976 :set (lambda (variable value)
977 (setq recentf-filter-changer-current nil)
978 (set-default variable value)))
980 (defun recentf-filter-changer-select (filter)
981 "Select FILTER as the current menu filter.
982 See `recentf-filter-changer'."
983 (setq recentf-filter-changer-current filter))
985 (defun recentf-filter-changer (l)
986 "Manage a sub-menu of menu filters.
987 `recentf-filter-changer-alist' defines the filters in the menu.
988 Filtering of L is delegated to the selected filter in the menu."
989 (unless recentf-filter-changer-current
990 (setq recentf-filter-changer-current
991 (caar recentf-filter-changer-alist)))
992 (if (not recentf-filter-changer-current)
994 (setq recentf-menu-filter-commands
995 (list
996 `("Show files"
997 ,@(mapcar
998 #'(lambda (f)
999 `[,(cdr f)
1000 (setq recentf-filter-changer-current ',(car f))
1001 ;;:active t
1002 :style radio ;;radio Don't work with GTK :-(
1003 :selected (eq recentf-filter-changer-current
1004 ',(car f))
1005 ;;:help ,(cdr f)
1007 recentf-filter-changer-alist))))
1008 (recentf-apply-menu-filter recentf-filter-changer-current l)))
1010 ;;; Hooks
1012 (defun recentf-track-opened-file ()
1013 "Insert the name of the file just opened or written into the recent list."
1014 (and buffer-file-name
1015 (recentf-add-file buffer-file-name))
1016 ;; Must return nil because it is run from `write-file-functions'.
1017 nil)
1019 (defun recentf-track-closed-file ()
1020 "Update the recent list when a buffer is killed.
1021 That is, remove a non kept file from the recent list."
1022 (and buffer-file-name
1023 (recentf-remove-if-non-kept buffer-file-name)))
1025 (defconst recentf-used-hooks
1027 (find-file-hook recentf-track-opened-file)
1028 (write-file-functions recentf-track-opened-file)
1029 (kill-buffer-hook recentf-track-closed-file)
1030 (kill-emacs-hook recentf-save-list)
1032 "Hooks used by recentf.")
1034 ;;; Commands
1037 ;;; Common dialog stuff
1039 (defun recentf-cancel-dialog (&rest ignore)
1040 "Cancel the current dialog.
1041 IGNORE arguments."
1042 (interactive)
1043 (kill-buffer (current-buffer))
1044 (message "Dialog canceled"))
1046 (defun recentf-dialog-goto-first (widget-type)
1047 "Move the cursor to the first WIDGET-TYPE in current dialog.
1048 Go to the beginning of buffer if not found."
1049 (goto-char (point-min))
1050 (condition-case nil
1051 (let (done)
1052 (widget-move 1)
1053 (while (not done)
1054 (if (eq widget-type (widget-type (widget-at (point))))
1055 (setq done t)
1056 (widget-move 1))))
1057 (error
1058 (goto-char (point-min)))))
1060 (defvar recentf-dialog-mode-map
1061 (let ((km (copy-keymap recentf--shortcuts-keymap)))
1062 (set-keymap-parent km widget-keymap)
1063 (define-key km "q" 'recentf-cancel-dialog)
1064 (define-key km [follow-link] "\C-m")
1066 "Keymap used in recentf dialogs.")
1068 (define-derived-mode recentf-dialog-mode nil "recentf-dialog"
1069 "Major mode of recentf dialogs.
1071 \\{recentf-dialog-mode-map}"
1072 :syntax-table nil
1073 :abbrev-table nil
1074 (setq truncate-lines t))
1076 (defmacro recentf-dialog (name &rest forms)
1077 "Show a dialog buffer with NAME, setup with FORMS."
1078 (declare (indent 1) (debug t))
1079 `(with-current-buffer (get-buffer-create ,name)
1080 ;; Cleanup buffer
1081 (let ((inhibit-read-only t)
1082 (ol (overlay-lists)))
1083 (mapc 'delete-overlay (car ol))
1084 (mapc 'delete-overlay (cdr ol))
1085 (erase-buffer))
1086 (recentf-dialog-mode)
1087 ,@forms
1088 (widget-setup)
1089 (switch-to-buffer (current-buffer))))
1091 ;;; Edit list dialog
1093 (defvar recentf-edit-list nil)
1095 (defun recentf-edit-list-select (widget &rest ignore)
1096 "Toggle a file selection based on the checkbox WIDGET state.
1097 IGNORE other arguments."
1098 (let ((value (widget-get widget :tag))
1099 (check (widget-value widget)))
1100 (if check
1101 (add-to-list 'recentf-edit-list value)
1102 (setq recentf-edit-list (delq value recentf-edit-list)))
1103 (message "%s %sselected" value (if check "" "un"))))
1105 (defun recentf-edit-list-validate (&rest ignore)
1106 "Process the recent list when the edit list dialog is committed.
1107 IGNORE arguments."
1108 (if recentf-edit-list
1109 (let ((i 0))
1110 (dolist (e recentf-edit-list)
1111 (setq recentf-list (delq e recentf-list)
1112 i (1+ i)))
1113 (kill-buffer (current-buffer))
1114 (message "%S file(s) removed from the list" i))
1115 (message "No file selected")))
1117 (defun recentf-edit-list ()
1118 "Show a dialog to delete selected files from the recent list."
1119 (interactive)
1120 (unless recentf-list
1121 (error "The list of recent files is empty"))
1122 (recentf-dialog (format "*%s - Edit list*" recentf-menu-title)
1123 (set (make-local-variable 'recentf-edit-list) nil)
1124 (widget-insert
1125 "Click on OK to delete selected files from the recent list.
1126 Click on Cancel or type `q' to cancel.\n")
1127 ;; Insert the list of files as checkboxes
1128 (dolist (item recentf-list)
1129 (widget-create 'checkbox
1130 :value nil ; unselected checkbox
1131 :format "\n %[%v%] %t"
1132 :tag item
1133 :notify 'recentf-edit-list-select))
1134 (widget-insert "\n\n")
1135 (widget-create
1136 'push-button
1137 :notify 'recentf-edit-list-validate
1138 :help-echo "Delete selected files from the recent list"
1139 "Ok")
1140 (widget-insert " ")
1141 (widget-create
1142 'push-button
1143 :notify 'recentf-cancel-dialog
1144 "Cancel")
1145 (recentf-dialog-goto-first 'checkbox)))
1147 ;;; Open file dialog
1149 (defun recentf-open-files-action (widget &rest ignore)
1150 "Open the file stored in WIDGET's value when notified.
1151 IGNORE other arguments."
1152 (kill-buffer (current-buffer))
1153 (funcall recentf-menu-action (widget-value widget)))
1155 ;; List of files associated to a digit shortcut key.
1156 (defvar recentf--files-with-key nil)
1158 (defun recentf-show-digit-shortcut-filter (l)
1159 "Filter the list of menu-elements L to show digit shortcuts."
1160 (let ((i 0))
1161 (dolist (e l)
1162 (setq i (1+ i))
1163 (recentf-set-menu-element-item
1164 e (format "[%d] %s" (% i 10) (recentf-menu-element-item e))))
1167 (defun recentf-open-files-item (menu-element)
1168 "Return a widget to display MENU-ELEMENT in a dialog buffer."
1169 (if (consp (cdr menu-element))
1170 ;; Represent a sub-menu with a tree widget
1171 `(tree-widget
1172 :open t
1173 :match ignore
1174 :node (item :tag ,(car menu-element)
1175 :sample-face bold
1176 :format "%{%t%}:\n")
1177 ,@(mapcar 'recentf-open-files-item
1178 (cdr menu-element)))
1179 ;; Represent a single file with a link widget
1180 `(link :tag ,(car menu-element)
1181 :button-prefix ""
1182 :button-suffix ""
1183 :button-face default
1184 :format "%[%t\n%]"
1185 :help-echo ,(concat "Open " (cdr menu-element))
1186 :action recentf-open-files-action
1187 ,(cdr menu-element))))
1189 (defun recentf-open-files-items (files)
1190 "Return a list of widgets to display FILES in a dialog buffer."
1191 (set (make-local-variable 'recentf--files-with-key)
1192 (recentf-trunc-list files 10))
1193 (mapcar 'recentf-open-files-item
1194 (append
1195 ;; When requested group the files with shortcuts together
1196 ;; at the top of the list.
1197 (when recentf-show-file-shortcuts-flag
1198 (setq files (nthcdr 10 files))
1199 (recentf-apply-menu-filter
1200 'recentf-show-digit-shortcut-filter
1201 (mapcar 'recentf-make-default-menu-element
1202 recentf--files-with-key)))
1203 ;; Then the other files.
1204 (recentf-apply-menu-filter
1205 recentf-menu-filter
1206 (mapcar 'recentf-make-default-menu-element
1207 files)))))
1209 (defun recentf-open-files (&optional files buffer-name)
1210 "Show a dialog to open a recent file.
1211 If optional argument FILES is non-nil, it is a list of recently-opened
1212 files to choose from. It defaults to the whole recent list.
1213 If optional argument BUFFER-NAME is non-nil, it is a buffer name to
1214 use for the dialog. It defaults to \"*`recentf-menu-title'*\"."
1215 (interactive)
1216 (unless (or files recentf-list)
1217 (error "There is no recent file to open"))
1218 (recentf-dialog (or buffer-name (format "*%s*" recentf-menu-title))
1219 (widget-insert "Click on a file"
1220 (if recentf-show-file-shortcuts-flag
1221 ", or type the corresponding digit key,"
1223 " to open it.\n"
1224 "Click on Cancel or type `q' to cancel.\n")
1225 ;; Use a L&F that looks like the recentf menu.
1226 (tree-widget-set-theme "folder")
1227 (apply 'widget-create
1228 `(group
1229 :indent 2
1230 :format "\n%v\n"
1231 ,@(recentf-open-files-items (or files recentf-list))))
1232 (widget-create
1233 'push-button
1234 :notify 'recentf-cancel-dialog
1235 "Cancel")
1236 (recentf-dialog-goto-first 'link)))
1238 (defun recentf-open-more-files ()
1239 "Show a dialog to open a recent file that is not in the menu."
1240 (interactive)
1241 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
1242 (format "*%s - More*" recentf-menu-title)))
1244 (defun recentf-open-most-recent-file (&optional n)
1245 "Open the Nth most recent file.
1246 Optional argument N must be a valid digit number. It defaults to 1.
1247 1 opens the most recent file, 2 the second most recent one, etc..
1248 0 opens the tenth most recent file."
1249 (interactive "p")
1250 (cond
1251 ((zerop n) (setq n 10))
1252 ((and (> n 0) (< n 10)))
1253 ((error "Recent file number out of range [0-9], %d" n)))
1254 (let ((file (nth (1- n) (or recentf--files-with-key recentf-list))))
1255 (unless file (error "Not that many recent files"))
1256 ;; Close the open files dialog.
1257 (when recentf--files-with-key
1258 (kill-buffer (current-buffer)))
1259 (funcall recentf-menu-action file)))
1261 ;;; Save/load/cleanup the recent list
1263 (defconst recentf-save-file-header
1264 ";;; Automatically generated by `recentf' on %s.\n"
1265 "Header to be written into the `recentf-save-file'.")
1267 (defconst recentf-save-file-coding-system
1268 (if (coding-system-p 'utf-8-emacs)
1269 'utf-8-emacs
1270 'emacs-mule)
1271 "Coding system of the file `recentf-save-file'.")
1273 (defun recentf-save-list ()
1274 "Save the recent list.
1275 Write data into the file specified by `recentf-save-file'."
1276 (interactive)
1277 (condition-case error
1278 (with-temp-buffer
1279 (erase-buffer)
1280 (set-buffer-file-coding-system recentf-save-file-coding-system)
1281 (insert (format recentf-save-file-header (current-time-string)))
1282 (recentf-dump-variable 'recentf-list recentf-max-saved-items)
1283 (recentf-dump-variable 'recentf-filter-changer-current)
1284 (insert "\n\f\n;; Local Variables:\n"
1285 (format ";; coding: %s\n" recentf-save-file-coding-system)
1286 ";; End:\n")
1287 (write-file (expand-file-name recentf-save-file))
1288 (when recentf-save-file-modes
1289 (set-file-modes recentf-save-file recentf-save-file-modes))
1290 nil)
1291 (error
1292 (warn "recentf mode: %s" (error-message-string error)))))
1294 (defun recentf-load-list ()
1295 "Load a previously saved recent list.
1296 Read data from the file specified by `recentf-save-file'.
1297 When `recentf-initialize-file-name-history' is non-nil, initialize an
1298 empty `file-name-history' with the recent list."
1299 (interactive)
1300 (let ((file (expand-file-name recentf-save-file)))
1301 (when (file-readable-p file)
1302 (load-file file)
1303 (and recentf-initialize-file-name-history
1304 (not file-name-history)
1305 (setq file-name-history (mapcar 'abbreviate-file-name
1306 recentf-list))))))
1308 (defun recentf-cleanup ()
1309 "Cleanup the recent list.
1310 That is, remove duplicates, non-kept, and excluded files."
1311 (interactive)
1312 (message "Cleaning up the recentf list...")
1313 (let ((n 0)
1314 (ht (make-hash-table
1315 :size recentf-max-saved-items
1316 :test 'equal))
1317 newlist key)
1318 (dolist (f recentf-list)
1319 (setq f (recentf-expand-file-name f)
1320 key (if recentf-case-fold-search (downcase f) f))
1321 (if (and (recentf-include-p f)
1322 (recentf-keep-p f)
1323 (not (gethash key ht)))
1324 (progn
1325 (push f newlist)
1326 (puthash key t ht))
1327 (setq n (1+ n))
1328 (message "File %s removed from the recentf list" f)))
1329 (message "Cleaning up the recentf list...done (%d removed)" n)
1330 (setq recentf-list (nreverse newlist))))
1332 ;;; The minor mode
1334 (defvar recentf-mode-map (make-sparse-keymap)
1335 "Keymap to use in recentf mode.")
1337 ;;;###autoload
1338 (define-minor-mode recentf-mode
1339 "Toggle recentf mode.
1340 With prefix argument ARG, turn on if positive, otherwise off.
1341 Returns non-nil if the new state is enabled.
1343 When recentf mode is enabled, it maintains a menu for visiting files
1344 that were operated on recently."
1345 :global t
1346 :group 'recentf
1347 :keymap recentf-mode-map
1348 (unless (and recentf-mode (recentf-enabled-p))
1349 (if recentf-mode
1350 (progn
1351 (recentf-load-list)
1352 (recentf-show-menu))
1353 (recentf-hide-menu)
1354 (recentf-save-list))
1355 (recentf-auto-cleanup)
1356 (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
1357 (dolist (hook recentf-used-hooks)
1358 (apply hook-setup hook)))
1359 (run-hooks 'recentf-mode-hook)
1360 (when (called-interactively-p 'interactive)
1361 (message "Recentf mode %sabled" (if recentf-mode "en" "dis"))))
1362 recentf-mode)
1364 (defun recentf-unload-function ()
1365 "Unload the recentf library."
1366 (recentf-mode -1)
1367 ;; continue standard unloading
1368 nil)
1370 (provide 'recentf)
1372 (run-hooks 'recentf-load-hook)
1374 ;; arch-tag: 78f1eec9-0d16-4d19-a4eb-2e4529edb62a
1375 ;;; recentf.el ends here