(search_buffer): Give up BM search on case-fold-search
[emacs.git] / lisp / recentf.el
blobdee7a8d438d9e0aa248f9db8c291224074e05ea9
1 ;;; recentf.el --- setup a menu of recently opened files
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: David Ponce <david@dponce.com>
7 ;; Created: July 19 1999
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
14 ;; by the Free Software Foundation; either version 2, or (at your
15 ;; option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; This package maintains a menu for visiting files that were operated
30 ;; on recently. When enabled a new "Open Recent" sub menu is
31 ;; displayed in the "Files" menu. The recent files list is
32 ;; automatically saved across Emacs sessions. You can customize the
33 ;; number of recent files displayed, the location of the menu and
34 ;; others options (see the source code for details).
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 (defvar recentf-data-cache nil
50 "Cache of data used to build the recentf menu.
51 The menu is rebuilt when this data has changed.")
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 "~/.recentf"
73 "*File to save the recent list into."
74 :group 'recentf
75 :type 'file)
77 (defcustom recentf-save-file-modes 384 ;; 0600
78 "Mode bits of recentf save file, as an integer, or nil.
79 If non-nil, after writing `recentf-save-file', set its mode bits to
80 this value. By default give R/W access only to the user who owns that
81 file. See also the function `set-file-modes'."
82 :group 'recentf
83 :type '(choice (const :tag "Don't change" nil)
84 integer))
86 (defcustom recentf-exclude nil
87 "*List of regexps and predicates for filenames excluded from the recent list.
88 When a filename matches any of the regexps or satisfies any of the
89 predicates it is excluded from the recent list.
90 A predicate is a function that is passed a filename to check and that
91 must return non-nil to exclude it."
92 :group 'recentf
93 :type '(repeat (choice regexp function)))
95 (defcustom recentf-keep
96 '(file-readable-p)
97 "*List of regexps and predicates for filenames kept in the recent list.
98 Regexps and predicates are tried in the specified order.
99 When nil all filenames are kept in the recent list.
100 When a filename matches any of the regexps or satisfies any of the
101 predicates it is kept in the recent list.
102 The default is to keep readable files.
103 A predicate is a function that is passed a filename to check and that
104 must return non-nil to keep it. For example, you can add the
105 `file-remote-p' predicate in front of this list to keep remote file
106 names in the recent list without checking their readability through a
107 remote access."
108 :group 'recentf
109 :type '(repeat (choice regexp function)))
111 (defun recentf-menu-customization-changed (variable value)
112 "Function called when the recentf menu customization has changed.
113 Set VARIABLE with VALUE, and force a rebuild of the recentf menu."
114 (when (featurep 'recentf)
115 ;; Unavailable until recentf has been loaded.
116 (recentf-clear-data))
117 (set-default variable value))
119 (defcustom recentf-menu-title "Open Recent"
120 "*Name of the recentf menu."
121 :group 'recentf
122 :type 'string
123 :set 'recentf-menu-customization-changed)
125 (defcustom recentf-menu-path '("File")
126 "*Path where to add the recentf menu.
127 If nil add it at top level (see also `easy-menu-add-item')."
128 :group 'recentf
129 :type '(choice (const :tag "Top Level" nil)
130 (sexp :tag "Menu Path"))
131 :set 'recentf-menu-customization-changed)
133 (defcustom recentf-menu-before "Open File..."
134 "*Name of the menu before which the recentf menu will be added.
135 If nil add it at end of menu (see also `easy-menu-add-item')."
136 :group 'recentf
137 :type '(choice (string :tag "Name")
138 (const :tag "Last" nil))
139 :set 'recentf-menu-customization-changed)
141 (defcustom recentf-menu-action 'find-file
142 "*Function to invoke with a filename item of the recentf menu.
143 The default is to call `find-file' to edit the selected file."
144 :group 'recentf
145 :type 'function
146 :set 'recentf-menu-customization-changed)
148 (defcustom recentf-max-menu-items 10
149 "*Maximum number of items in the recentf menu."
150 :group 'recentf
151 :type 'integer
152 :set 'recentf-menu-customization-changed)
154 (defcustom recentf-menu-filter nil
155 "*Function used to filter files displayed in the recentf menu.
156 A nil value means no filter. The following functions are predefined:
158 - `recentf-sort-ascending'
159 Sort menu items in ascending order.
160 - `recentf-sort-descending'
161 Sort menu items in descending order.
162 - `recentf-sort-basenames-ascending'
163 Sort menu items by filenames sans directory in ascending order.
164 - `recentf-sort-basenames-descending'
165 Sort menu items by filenames sans directory in descending order.
166 - `recentf-sort-directories-ascending'
167 Sort menu items by directories in ascending order.
168 - `recentf-sort-directories-descending'
169 Sort menu items by directories in descending order.
170 - `recentf-show-basenames'
171 Show filenames sans directory in menu items.
172 - `recentf-show-basenames-ascending'
173 Show filenames sans directory in ascending order.
174 - `recentf-show-basenames-descending'
175 Show filenames sans directory in descending order.
176 - `recentf-relative-filter'
177 Show filenames relative to `default-directory'.
178 - `recentf-arrange-by-rule'
179 Show sub-menus following user defined rules.
180 - `recentf-arrange-by-mode'
181 Show a sub-menu for each major mode.
182 - `recentf-arrange-by-dir'
183 Show a sub-menu for each directory.
184 - `recentf-filter-changer'
185 Manage a ring of filters.
187 The filter function is called with one argument, the list of menu
188 elements used to build the menu and must return a new list of menu
189 elements (see `recentf-make-menu-element' for menu element form)."
190 :group 'recentf
191 :type '(radio (const nil)
192 (function-item recentf-sort-ascending)
193 (function-item recentf-sort-descending)
194 (function-item recentf-sort-basenames-ascending)
195 (function-item recentf-sort-basenames-descending)
196 (function-item recentf-sort-directories-ascending)
197 (function-item recentf-sort-directories-descending)
198 (function-item recentf-show-basenames)
199 (function-item recentf-show-basenames-ascending)
200 (function-item recentf-show-basenames-descending)
201 (function-item recentf-relative-filter)
202 (function-item recentf-arrange-by-rule)
203 (function-item recentf-arrange-by-mode)
204 (function-item recentf-arrange-by-dir)
205 (function-item recentf-filter-changer)
206 function)
207 :set 'recentf-menu-customization-changed)
209 (defcustom recentf-menu-open-all-flag nil
210 "*Non-nil means to show an \"All...\" item in the menu.
211 This item will replace the \"More...\" item."
212 :group 'recentf
213 :type 'boolean
214 :set 'recentf-menu-customization-changed)
216 (defcustom recentf-menu-append-commands-flag t
217 "*Non-nil means to append command items to the menu."
218 :group 'recentf
219 :type 'boolean
220 :set 'recentf-menu-customization-changed)
222 (define-obsolete-variable-alias 'recentf-menu-append-commands-p
223 'recentf-menu-append-commands-flag
224 "22.1")
226 (defcustom recentf-auto-cleanup 'mode
227 "*Define when to automatically cleanup the recent list.
228 The following values can be set:
230 - `mode'
231 Cleanup when turning the mode on (default).
232 - `never'
233 Never cleanup the list automatically.
234 - A number
235 Cleanup each time Emacs has been idle that number of seconds.
236 - A time string
237 Cleanup at specified time string, for example at \"11:00pm\".
239 Setting this variable directly does not take effect;
240 use \\[customize].
242 See also the command `recentf-cleanup', that can be used to manually
243 cleanup the list."
244 :group 'recentf
245 :type '(radio (const :tag "When mode enabled"
246 :value mode)
247 (const :tag "Never"
248 :value never)
249 (number :tag "When idle that seconds"
250 :value 300)
251 (string :tag "At time"
252 :value "11:00pm"))
253 :set (lambda (variable value)
254 (set-default variable value)
255 (when (featurep 'recentf)
256 ;; Unavailable until recentf has been loaded.
257 (recentf-auto-cleanup))))
259 (defcustom recentf-initialize-file-name-history t
260 "*Non-nil means to initialize `file-name-history' with the recent list.
261 If `file-name-history' is not empty, do nothing."
262 :group 'recentf
263 :type 'boolean)
265 (defcustom recentf-load-hook nil
266 "*Normal hook run at end of loading the `recentf' package."
267 :group 'recentf
268 :type 'hook)
270 (defcustom recentf-filename-handlers nil
271 "Functions to post process recent file names.
272 They are successively passed a file name to transform it."
273 :group 'recentf
274 :type '(choice
275 (const :tag "None" nil)
276 (repeat :tag "Functions"
277 (choice
278 (const file-truename)
279 (const abbreviate-file-name)
280 (function :tag "Other function")))))
282 (defcustom recentf-show-file-shortcuts-flag t
283 "Whether to show ``[N]'' for the Nth item up to 10.
284 If non-nil, `recentf-open-files' will show labels for keys that can be
285 used as shortcuts to open the Nth file."
286 :group 'recentf
287 :type 'boolean)
289 ;;; Utilities
291 (defconst recentf-case-fold-search
292 (memq system-type '(vax-vms windows-nt cygwin))
293 "Non-nil if recentf searches and matches should ignore case.")
295 (defsubst recentf-string-equal (s1 s2)
296 "Return non-nil if strings S1 and S2 have identical contents.
297 Ignore case if `recentf-case-fold-search' is non-nil."
298 (if recentf-case-fold-search
299 (string-equal (downcase s1) (downcase s2))
300 (string-equal s1 s2)))
302 (defsubst recentf-string-lessp (s1 s2)
303 "Return non-nil if string S1 is less than S2 in lexicographic order.
304 Ignore case if `recentf-case-fold-search' is non-nil."
305 (if recentf-case-fold-search
306 (string-lessp (downcase s1) (downcase s2))
307 (string-lessp s1 s2)))
309 (defun recentf-string-member (elt list)
310 "Return non-nil if ELT is an element of LIST.
311 The value is actually the tail of LIST whose car is ELT.
312 ELT must be a string and LIST a list of strings.
313 Ignore case if `recentf-case-fold-search' is non-nil."
314 (while (and list (not (recentf-string-equal elt (car list))))
315 (setq list (cdr list)))
316 list)
318 (defsubst recentf-trunc-list (l n)
319 "Return from L the list of its first N elements."
320 (let (nl)
321 (while (and l (> n 0))
322 (setq nl (cons (car l) nl)
323 n (1- n)
324 l (cdr l)))
325 (nreverse nl)))
327 (defun recentf-dump-variable (variable &optional limit)
328 "Insert a \"(setq VARIABLE value)\" in the current buffer.
329 When the value of VARIABLE is a list, optional argument LIMIT
330 specifies a maximum number of elements to insert. By default insert
331 the full list."
332 (let ((value (symbol-value variable)))
333 (if (atom value)
334 (insert (format "\n(setq %S %S)\n" variable value))
335 (when (and (integerp limit) (> limit 0))
336 (setq value (recentf-trunc-list value limit)))
337 (insert (format "\n(setq %S\n '(" variable))
338 (dolist (e value)
339 (insert (format "\n %S" e)))
340 (insert "\n ))\n"))))
342 (defvar recentf-auto-cleanup-timer nil
343 "Timer used to automatically cleanup the recent list.
344 See also the option `recentf-auto-cleanup'.")
346 (defun recentf-auto-cleanup ()
347 "Automatic cleanup of the recent list."
348 (when (timerp recentf-auto-cleanup-timer)
349 (cancel-timer recentf-auto-cleanup-timer))
350 (when recentf-mode
351 (setq recentf-auto-cleanup-timer
352 (cond
353 ((eq 'mode recentf-auto-cleanup)
354 (recentf-cleanup)
355 nil)
356 ((numberp recentf-auto-cleanup)
357 (run-with-idle-timer
358 recentf-auto-cleanup t 'recentf-cleanup))
359 ((stringp recentf-auto-cleanup)
360 (run-at-time
361 recentf-auto-cleanup nil 'recentf-cleanup))))))
363 ;;; File functions
365 (defsubst recentf-push (filename)
366 "Push FILENAME into the recent list, if it isn't there yet.
367 If it is there yet, move it at the beginning of the list.
368 If `recentf-case-fold-search' is non-nil, ignore case when comparing
369 filenames."
370 (let ((m (recentf-string-member filename recentf-list)))
371 (and m (setq recentf-list (delq (car m) recentf-list)))
372 (push filename recentf-list)))
374 (defun recentf-apply-filename-handlers (name)
375 "Apply `recentf-filename-handlers' to file NAME.
376 Return the transformed file name, or NAME if any handler failed, or
377 returned nil."
378 (or (condition-case nil
379 (let ((handlers recentf-filename-handlers)
380 (filename name))
381 (while (and filename handlers)
382 (setq filename (funcall (car handlers) filename)
383 handlers (cdr handlers)))
384 filename)
385 (error nil))
386 name))
388 (defsubst recentf-expand-file-name (name)
389 "Convert file NAME to absolute, and canonicalize it.
390 NAME is first passed to the function `expand-file-name', then to
391 `recentf-filename-handlers' to post process it."
392 (recentf-apply-filename-handlers (expand-file-name name)))
394 (defun recentf-include-p (filename)
395 "Return non-nil if FILENAME should be included in the recent list.
396 That is, if it doesn't match any of the `recentf-exclude' checks."
397 (let ((case-fold-search recentf-case-fold-search)
398 (checks recentf-exclude)
399 (keepit t))
400 (while (and checks keepit)
401 (setq keepit (condition-case nil
402 (not (if (stringp (car checks))
403 ;; A regexp
404 (string-match (car checks) filename)
405 ;; A predicate
406 (funcall (car checks) filename)))
407 (error nil))
408 checks (cdr checks)))
409 keepit))
411 (defun recentf-keep-p (filename)
412 "Return non-nil if FILENAME should be kept in the recent list.
413 That is, if it matches any of the `recentf-keep' checks."
414 (let* ((case-fold-search recentf-case-fold-search)
415 (checks recentf-keep)
416 (keepit (null checks)))
417 (while (and checks (not keepit))
418 (setq keepit (condition-case nil
419 (if (stringp (car checks))
420 ;; A regexp
421 (string-match (car checks) filename)
422 ;; A predicate
423 (funcall (car checks) filename))
424 (error nil))
425 checks (cdr checks)))
426 keepit))
428 (defsubst recentf-add-file (filename)
429 "Add or move FILENAME at the beginning of the recent list.
430 Does nothing if the name satisfies any of the `recentf-exclude'
431 regexps or predicates."
432 (setq filename (recentf-expand-file-name filename))
433 (when (recentf-include-p filename)
434 (recentf-push filename)))
436 (defsubst recentf-remove-if-non-kept (filename)
437 "Remove FILENAME from the recent list, if file is not kept.
438 Return non-nil if FILENAME has been removed."
439 (unless (recentf-keep-p filename)
440 (let ((m (recentf-string-member
441 (recentf-expand-file-name filename) recentf-list)))
442 (and m (setq recentf-list (delq (car m) recentf-list))))))
444 (defsubst recentf-directory-compare (f1 f2)
445 "Compare absolute filenames F1 and F2.
446 First compare directories, then filenames sans directory.
447 Return non-nil if F1 is less than F2."
448 (let ((d1 (file-name-directory f1))
449 (d2 (file-name-directory f2)))
450 (if (recentf-string-equal d1 d2)
451 (recentf-string-lessp (file-name-nondirectory f1)
452 (file-name-nondirectory f2))
453 (recentf-string-lessp d1 d2))))
455 ;;; Menu building
457 (defsubst recentf-digit-shortcut-command-name (n)
458 "Return a command name to open the Nth most recent file.
459 See also the command `recentf-open-most-recent-file'."
460 (intern (format "recentf-open-most-recent-file-%d" n)))
462 (defvar recentf--shortcuts-keymap
463 (let ((km (make-sparse-keymap)))
464 (dolist (k '(0 9 8 7 6 5 4 3 2 1))
465 (let ((cmd (recentf-digit-shortcut-command-name k)))
466 ;; Define a shortcut command.
467 (defalias cmd
468 `(lambda ()
469 (interactive)
470 (recentf-open-most-recent-file ,k)))
471 ;; Bind it to a digit key.
472 (define-key km (vector (+ k ?0)) cmd)))
474 "Digit shortcuts keymap.")
476 (defvar recentf-menu-items-for-commands
477 (list
478 ["Cleanup list"
479 recentf-cleanup
480 :help "Remove duplicates, and obsoletes files from the recent list"
481 :active t]
482 ["Edit list..."
483 recentf-edit-list
484 :help "Manually remove files from the recent list"
485 :active t]
486 ["Save list now"
487 recentf-save-list
488 :help "Save the list of recently opened files now"
489 :active t]
490 ["Options..."
491 (customize-group "recentf")
492 :help "Customize recently opened files menu and options"
493 :active t]
495 "List of menu items for recentf commands.")
497 (defvar recentf-menu-filter-commands nil
498 "This variable can be used by menu filters to setup their own command menu.
499 If non-nil it must contain a list of valid menu-items to be appended
500 to the recent file list part of the menu. Before calling a menu
501 filter function this variable is reset to nil.")
503 (defsubst recentf-elements (n)
504 "Return a list of the first N elements of the recent list."
505 (recentf-trunc-list recentf-list n))
507 (defsubst recentf-make-menu-element (menu-item menu-value)
508 "Create a new menu-element.
509 A menu element is a pair (MENU-ITEM . MENU-VALUE), where MENU-ITEM is
510 the menu item string displayed. MENU-VALUE is the file to be open
511 when the corresponding MENU-ITEM is selected. Or it is a
512 pair (SUB-MENU-TITLE . MENU-ELEMENTS) where SUB-MENU-TITLE is a
513 sub-menu title and MENU-ELEMENTS is the list of menu elements in the
514 sub-menu."
515 (cons menu-item menu-value))
517 (defsubst recentf-menu-element-item (e)
518 "Return the item part of the menu-element E."
519 (car e))
521 (defsubst recentf-menu-element-value (e)
522 "Return the value part of the menu-element E."
523 (cdr e))
525 (defsubst recentf-set-menu-element-item (e item)
526 "Change the item part of menu-element E to ITEM."
527 (setcar e item))
529 (defsubst recentf-set-menu-element-value (e value)
530 "Change the value part of menu-element E to VALUE."
531 (setcdr e value))
533 (defsubst recentf-sub-menu-element-p (e)
534 "Return non-nil if menu-element E defines a sub-menu."
535 (consp (recentf-menu-element-value e)))
537 (defsubst recentf-make-default-menu-element (file)
538 "Make a new default menu element with FILE.
539 This a menu element (FILE . FILE)."
540 (recentf-make-menu-element file file))
542 (defsubst recentf-menu-elements (n)
543 "Return a list of the first N default menu elements from the recent list.
544 See also `recentf-make-default-menu-element'."
545 (mapcar 'recentf-make-default-menu-element
546 (recentf-elements n)))
548 (defun recentf-apply-menu-filter (filter l)
549 "Apply function FILTER to the list of menu-elements L.
550 It takes care of sub-menu elements in L and recursively apply FILTER
551 to them. It is guaranteed that FILTER receives only a list of single
552 menu-elements (no sub-menu)."
553 (if (and l (functionp filter))
554 (let ((case-fold-search recentf-case-fold-search)
555 elts others)
556 ;; split L into two sub-listes, one of sub-menus elements and
557 ;; another of single menu elements.
558 (dolist (elt l)
559 (if (recentf-sub-menu-element-p elt)
560 (push elt elts)
561 (push elt others)))
562 ;; Apply FILTER to single elements.
563 (when others
564 (setq others (funcall filter (nreverse others))))
565 ;; Apply FILTER to sub-menu elements.
566 (setq l nil)
567 (dolist (elt elts)
568 (recentf-set-menu-element-value
569 elt (recentf-apply-menu-filter
570 filter (recentf-menu-element-value elt)))
571 (push elt l))
572 ;; Return the new filtered menu element list.
573 (nconc l others))
576 ;; Count the number of assigned menu shortcuts.
577 (defvar recentf-menu-shortcuts)
579 (defun recentf-make-menu-items ()
580 "Make menu items from the recent list."
581 (setq recentf-menu-filter-commands nil)
582 (let* ((recentf-menu-shortcuts 0)
583 (file-items
584 (mapcar 'recentf-make-menu-item
585 (recentf-apply-menu-filter
586 recentf-menu-filter
587 (recentf-menu-elements recentf-max-menu-items)))))
588 (append (or file-items (list ["No files" t
589 :help "No recent file to open"
590 :active nil]))
591 (if recentf-menu-open-all-flag
592 (list ["All..." recentf-open-files
593 :help "Open recent files through a dialog"
594 :active t])
595 (and (< recentf-max-menu-items (length recentf-list))
596 (list ["More..." recentf-open-more-files
597 :help "Open files not in the menu through a dialog"
598 :active t])))
599 (and recentf-menu-filter-commands
600 (cons "---"
601 recentf-menu-filter-commands))
602 (and recentf-menu-append-commands-flag
603 (cons "---"
604 recentf-menu-items-for-commands)))))
606 (defun recentf-menu-value-shortcut (name)
607 "Return a shorcut digit for file NAME.
608 Return nil if file NAME is not one of the ten more recent."
609 (let ((i 0) k)
610 (while (and (not k) (< i 10))
611 (if (string-equal name (nth i recentf-list))
612 (progn
613 (setq recentf-menu-shortcuts (1+ recentf-menu-shortcuts))
614 (setq k (% (1+ i) 10)))
615 (setq i (1+ i))))
618 (defun recentf-make-menu-item (elt)
619 "Make a menu item from menu element ELT."
620 (let ((item (recentf-menu-element-item elt))
621 (value (recentf-menu-element-value elt)))
622 (if (recentf-sub-menu-element-p elt)
623 (cons item (mapcar 'recentf-make-menu-item value))
624 (let ((k (and (< recentf-menu-shortcuts 10)
625 (recentf-menu-value-shortcut value))))
626 (vector item
627 ;; If the file name is one of the ten more recent, use
628 ;; a digit shortcut command to open it, else use an
629 ;; anonymous command.
630 (if k
631 (recentf-digit-shortcut-command-name k)
632 `(lambda ()
633 (interactive)
634 (,recentf-menu-action ,value)))
635 :help (concat "Open " value)
636 :active t)))))
638 (defsubst recentf-menu-bar ()
639 "Return the keymap of the global menu bar."
640 (lookup-key global-map [menu-bar]))
642 (defun recentf-clear-data ()
643 "Clear data used to build the recentf menu.
644 This forces a rebuild of the menu."
645 (easy-menu-remove-item (recentf-menu-bar)
646 recentf-menu-path recentf-menu-title)
647 (setq recentf-data-cache nil))
649 ;;; Predefined menu filters
651 (defsubst recentf-sort-ascending (l)
652 "Sort the list of menu elements L in ascending order.
653 The MENU-ITEM part of each menu element is compared."
654 (sort (copy-sequence l)
655 #'(lambda (e1 e2)
656 (recentf-string-lessp
657 (recentf-menu-element-item e1)
658 (recentf-menu-element-item e2)))))
660 (defsubst recentf-sort-descending (l)
661 "Sort the list of menu elements L in descending order.
662 The MENU-ITEM part of each menu element is compared."
663 (sort (copy-sequence l)
664 #'(lambda (e1 e2)
665 (recentf-string-lessp
666 (recentf-menu-element-item e2)
667 (recentf-menu-element-item e1)))))
669 (defsubst recentf-sort-basenames-ascending (l)
670 "Sort the list of menu elements L in ascending order.
671 Only filenames sans directory are compared."
672 (sort (copy-sequence l)
673 #'(lambda (e1 e2)
674 (recentf-string-lessp
675 (file-name-nondirectory (recentf-menu-element-value e1))
676 (file-name-nondirectory (recentf-menu-element-value e2))))))
678 (defsubst recentf-sort-basenames-descending (l)
679 "Sort the list of menu elements L in descending order.
680 Only filenames sans directory are compared."
681 (sort (copy-sequence l)
682 #'(lambda (e1 e2)
683 (recentf-string-lessp
684 (file-name-nondirectory (recentf-menu-element-value e2))
685 (file-name-nondirectory (recentf-menu-element-value e1))))))
687 (defsubst recentf-sort-directories-ascending (l)
688 "Sort the list of menu elements L in ascending order.
689 Compares directories then filenames to order the list."
690 (sort (copy-sequence l)
691 #'(lambda (e1 e2)
692 (recentf-directory-compare
693 (recentf-menu-element-value e1)
694 (recentf-menu-element-value e2)))))
696 (defsubst recentf-sort-directories-descending (l)
697 "Sort the list of menu elements L in descending order.
698 Compares directories then filenames to order the list."
699 (sort (copy-sequence l)
700 #'(lambda (e1 e2)
701 (recentf-directory-compare
702 (recentf-menu-element-value e2)
703 (recentf-menu-element-value e1)))))
705 (defun recentf-show-basenames (l &optional no-dir)
706 "Filter the list of menu elements L to show filenames sans directory.
707 When a filename is duplicated, it is appended a sequence number if
708 optional argument NO-DIR is non-nil, or its directory otherwise."
709 (let (filtered-names filtered-list full name counters sufx)
710 (dolist (elt l (nreverse filtered-list))
711 (setq full (recentf-menu-element-value elt)
712 name (file-name-nondirectory full))
713 (if (not (member name filtered-names))
714 (push name filtered-names)
715 (if no-dir
716 (if (setq sufx (assoc name counters))
717 (setcdr sufx (1+ (cdr sufx)))
718 (setq sufx 1)
719 (push (cons name sufx) counters))
720 (setq sufx (file-name-directory full)))
721 (setq name (format "%s(%s)" name sufx)))
722 (push (recentf-make-menu-element name full) filtered-list))))
724 (defsubst recentf-show-basenames-ascending (l)
725 "Filter the list of menu elements L to show filenames sans directory.
726 Filenames are sorted in ascending order.
727 This filter combines the `recentf-sort-basenames-ascending' and
728 `recentf-show-basenames' filters."
729 (recentf-show-basenames (recentf-sort-basenames-ascending l)))
731 (defsubst recentf-show-basenames-descending (l)
732 "Filter the list of menu elements L to show filenames sans directory.
733 Filenames are sorted in descending order.
734 This filter combines the `recentf-sort-basenames-descending' and
735 `recentf-show-basenames' filters."
736 (recentf-show-basenames (recentf-sort-basenames-descending l)))
738 (defun recentf-relative-filter (l)
739 "Filter the list of menu-elements L to show relative filenames.
740 Filenames are relative to the `default-directory'."
741 (mapcar #'(lambda (menu-element)
742 (let* ((ful (recentf-menu-element-value menu-element))
743 (rel (file-relative-name ful default-directory)))
744 (if (string-match "^\\.\\." rel)
745 menu-element
746 (recentf-make-menu-element rel ful))))
749 ;;; Rule based menu filters
751 (defcustom recentf-arrange-rules
753 ("Elisp files (%d)" ".\\.el$")
754 ("Java files (%d)" ".\\.java$")
755 ("C/C++ files (%d)" "c\\(pp\\)?$")
757 "*List of rules used by `recentf-arrange-by-rule' to build sub-menus.
758 A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
759 displayed title of the sub-menu where a '%d' `format' pattern is
760 replaced by the number of items in the sub-menu. MATCHER is a regexp
761 or a list of regexps. Items matching one of the regular expressions in
762 MATCHER are added to the corresponding sub-menu."
763 :group 'recentf-filters
764 :type '(repeat (cons string (repeat regexp)))
765 :set 'recentf-menu-customization-changed)
767 (defcustom recentf-arrange-by-rule-others "Other files (%d)"
768 "*Title of the `recentf-arrange-by-rule' sub-menu.
769 This is for the menu where items that don't match any
770 `recentf-arrange-rules' are displayed. If nil these items are
771 displayed in the main recent files menu. A '%d' `format' pattern in
772 the title is replaced by the number of items in the sub-menu."
773 :group 'recentf-filters
774 :type '(choice (const :tag "Main menu" nil)
775 (string :tag "Title"))
776 :set 'recentf-menu-customization-changed)
778 (defcustom recentf-arrange-by-rules-min-items 0
779 "*Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
780 If the number of items in a sub-menu is less than this value the
781 corresponding sub-menu items are displayed in the main recent files
782 menu or in the `recentf-arrange-by-rule-others' sub-menu if
783 defined."
784 :group 'recentf-filters
785 :type 'number
786 :set 'recentf-menu-customization-changed)
788 (defcustom recentf-arrange-by-rule-subfilter nil
789 "*Function called by a rule based filter to filter sub-menu elements.
790 A nil value means no filter. See also `recentf-menu-filter'.
791 You can't use another rule based filter here."
792 :group 'recentf-filters
793 :type '(choice (const nil) function)
794 :set (lambda (variable value)
795 (when (memq value '(recentf-arrange-by-rule
796 recentf-arrange-by-mode
797 recentf-arrange-by-dir))
798 (error "Recursive use of a rule based filter"))
799 (recentf-menu-customization-changed variable value)))
801 (defun recentf-match-rule-p (matcher filename)
802 "Return non-nil if the rule specified by MATCHER match FILENAME.
803 See `recentf-arrange-rules' for details on MATCHER."
804 (if (stringp matcher)
805 (string-match matcher filename)
806 (while (and (consp matcher)
807 (not (string-match (car matcher) filename)))
808 (setq matcher (cdr matcher)))
809 matcher))
811 (defun recentf-arrange-by-rule (l)
812 "Filter the list of menu-elements L.
813 Arrange them in sub-menus following rules in `recentf-arrange-rules'."
814 (if (not recentf-arrange-rules)
816 (let ((menus (mapcar #'(lambda (r) (list (car r)))
817 recentf-arrange-rules))
818 menu others min file rules elts count)
819 (dolist (elt l)
820 (setq file (recentf-menu-element-value elt)
821 rules recentf-arrange-rules
822 elts menus
823 menu nil)
824 (while (and (not menu) rules)
825 (when (recentf-match-rule-p (cdar rules) file)
826 (setq menu (car elts))
827 (recentf-set-menu-element-value
828 menu (cons elt (recentf-menu-element-value menu))))
829 (setq rules (cdr rules)
830 elts (cdr elts)))
831 (unless menu
832 (push elt others)))
834 (setq l nil
835 min (if (natnump recentf-arrange-by-rules-min-items)
836 recentf-arrange-by-rules-min-items 0))
837 (dolist (menu menus)
838 (when (setq elts (recentf-menu-element-value menu))
839 (setq count (length elts))
840 (if (< count min)
841 (setq others (nconc elts others))
842 (recentf-set-menu-element-item
843 menu (format (recentf-menu-element-item menu) count))
844 (recentf-set-menu-element-value
845 menu (recentf-apply-menu-filter
846 recentf-arrange-by-rule-subfilter (nreverse elts)))
847 (push menu l))))
849 (if (and (stringp recentf-arrange-by-rule-others) others)
850 (nreverse
851 (cons
852 (recentf-make-menu-element
853 (format recentf-arrange-by-rule-others (length others))
854 (recentf-apply-menu-filter
855 recentf-arrange-by-rule-subfilter (nreverse others)))
857 (nconc
858 (nreverse l)
859 (recentf-apply-menu-filter
860 recentf-arrange-by-rule-subfilter (nreverse others)))))
863 ;;; Predefined rule based menu filters
865 (defun recentf-build-mode-rules ()
866 "Convert `auto-mode-alist' to menu filter rules.
867 Rules obey `recentf-arrange-rules' format."
868 (let ((case-fold-search recentf-case-fold-search)
869 regexp rule-name rule rules)
870 (dolist (mode auto-mode-alist)
871 (setq regexp (car mode)
872 mode (cdr mode))
873 (when (symbolp mode)
874 (setq rule-name (symbol-name mode))
875 (if (string-match "\\(.*\\)-mode$" rule-name)
876 (setq rule-name (match-string 1 rule-name)))
877 (setq rule-name (concat rule-name " (%d)")
878 rule (assoc rule-name rules))
879 (if rule
880 (setcdr rule (cons regexp (cdr rule)))
881 (push (list rule-name regexp) rules))))
882 ;; It is important to preserve auto-mode-alist order
883 ;; to ensure the right file <-> mode association
884 (nreverse rules)))
886 (defun recentf-arrange-by-mode (l)
887 "Split the list of menu-elements L into sub-menus by major mode."
888 (let ((recentf-arrange-rules (recentf-build-mode-rules))
889 (recentf-arrange-by-rule-others "others (%d)"))
890 (recentf-arrange-by-rule l)))
892 (defun recentf-build-dir-rules (l)
893 "Convert directories in menu-elements L to menu filter rules.
894 Rules obey `recentf-arrange-rules' format."
895 (let (dirs)
896 (mapcar #'(lambda (e)
897 (let ((dir (file-name-directory
898 (recentf-menu-element-value e))))
899 (or (recentf-string-member dir dirs)
900 (push dir dirs))))
902 (mapcar #'(lambda (d)
903 (cons (concat d " (%d)")
904 (concat "\\`" d)))
905 (nreverse (sort dirs 'recentf-string-lessp)))))
907 (defun recentf-file-name-nondir (l)
908 "Filter the list of menu-elements L to show filenames sans directory.
909 This simplified version of `recentf-show-basenames' does not handle
910 duplicates. It is used by `recentf-arrange-by-dir' as its
911 `recentf-arrange-by-rule-subfilter'."
912 (mapcar #'(lambda (e)
913 (recentf-make-menu-element
914 (file-name-nondirectory (recentf-menu-element-value e))
915 (recentf-menu-element-value e)))
918 (defun recentf-arrange-by-dir (l)
919 "Split the list of menu-elements L into sub-menus by directory."
920 (let ((recentf-arrange-rules (recentf-build-dir-rules l))
921 (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
922 recentf-arrange-by-rule-others)
923 (nreverse (recentf-arrange-by-rule l))))
925 ;;; Ring of menu filters
927 (defvar recentf-filter-changer-state nil
928 "Used by `recentf-filter-changer' to hold its state.")
930 (defcustom recentf-filter-changer-alist
932 (recentf-arrange-by-mode . "*Files by Mode*")
933 (recentf-arrange-by-dir . "*Files by Directory*")
934 (recentf-arrange-by-rule . "*Files by User Rule*")
936 "*List of filters managed by `recentf-filter-changer'.
937 Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is
938 the filter function, and LABEL is the menu item displayed to select
939 that filter."
940 :group 'recentf-filters
941 :type '(repeat (cons function string))
942 :set (lambda (variable value)
943 (setq recentf-filter-changer-state nil)
944 (recentf-menu-customization-changed variable value)))
946 (defun recentf-filter-changer-goto-next ()
947 "Go to the next filter available.
948 See `recentf-filter-changer'."
949 (setq recentf-filter-changer-state (cdr recentf-filter-changer-state))
950 (recentf-clear-data))
952 (defsubst recentf-filter-changer-get-current ()
953 "Get the current filter available.
954 See `recentf-filter-changer'."
955 (unless recentf-filter-changer-state
956 (setq recentf-filter-changer-state recentf-filter-changer-alist))
957 (car recentf-filter-changer-state))
959 (defsubst recentf-filter-changer-get-next ()
960 "Get the next filter available.
961 See `recentf-filter-changer'."
962 ;; At this point the current filter is the first element of
963 ;; `recentf-filter-changer-state'.
964 (car (or (cdr recentf-filter-changer-state)
965 ;; There is no next element in
966 ;; `recentf-filter-changer-state', so loop back to the
967 ;; first element of `recentf-filter-changer-alist'.
968 recentf-filter-changer-alist)))
970 (defun recentf-filter-changer (l)
971 "Manage a ring of menu filters.
972 `recentf-filter-changer-alist' defines the filters in the ring.
973 Filtering of L is delegated to the current filter in the ring. A
974 filter menu item is displayed allowing to dynamically activate the
975 next filter in the ring. If the filter ring is empty, L is left
976 unchanged."
977 (let ((filter (recentf-filter-changer-get-current)))
978 (when filter
979 (setq l (recentf-apply-menu-filter (car filter) l)
980 filter (recentf-filter-changer-get-next))
981 (when filter
982 (setq recentf-menu-filter-commands
983 (list (vector (cdr filter)
984 '(recentf-filter-changer-goto-next)
985 t)))))
988 ;;; Common dialog stuff
990 (defun recentf-cancel-dialog (&rest ignore)
991 "Cancel the current dialog.
992 IGNORE arguments."
993 (interactive)
994 (kill-buffer (current-buffer))
995 (message "Dialog canceled"))
997 (defun recentf-dialog-goto-first (widget-type)
998 "Move the cursor to the first WIDGET-TYPE in current dialog.
999 Go to the beginning of buffer if not found."
1000 (goto-char (point-min))
1001 (condition-case nil
1002 (let (done)
1003 (widget-move 1)
1004 (while (not done)
1005 (if (eq widget-type (widget-type (widget-at (point))))
1006 (setq done t)
1007 (widget-move 1))))
1008 (goto-char (point-min))))
1010 (defvar recentf-dialog-mode-map
1011 (let ((km (copy-keymap recentf--shortcuts-keymap)))
1012 (set-keymap-parent km widget-keymap)
1013 (define-key km "q" 'recentf-cancel-dialog)
1014 (define-key km [down-mouse-1] 'widget-button-click)
1016 "Keymap used in recentf dialogs.")
1018 (define-derived-mode recentf-dialog-mode nil "recentf-dialog"
1019 "Major mode of recentf dialogs.
1021 \\{recentf-dialog-mode-map}"
1022 :syntax-table nil
1023 :abbrev-table nil
1024 (setq truncate-lines t))
1026 (defmacro recentf-dialog (name &rest forms)
1027 "Show a dialog buffer with NAME, setup with FORMS."
1028 (declare (indent 1) (debug t))
1029 `(with-current-buffer (get-buffer-create ,name)
1030 ;; Cleanup buffer
1031 (let ((inhibit-read-only t)
1032 (ol (overlay-lists)))
1033 (mapc 'delete-overlay (car ol))
1034 (mapc 'delete-overlay (cdr ol))
1035 (erase-buffer))
1036 (recentf-dialog-mode)
1037 ,@forms
1038 (widget-setup)
1039 (switch-to-buffer (current-buffer))))
1041 ;;; Hooks
1043 (defun recentf-track-opened-file ()
1044 "Insert the name of the file just opened or written into the recent list."
1045 (and buffer-file-name
1046 (recentf-add-file buffer-file-name))
1047 ;; Must return nil because it is run from `write-file-functions'.
1048 nil)
1050 (defun recentf-track-closed-file ()
1051 "Update the recent list when a buffer is killed.
1052 That is, remove a non kept file from the recent list."
1053 (and buffer-file-name
1054 (recentf-remove-if-non-kept buffer-file-name)))
1056 (defun recentf-update-menu ()
1057 "Update the recentf menu from the current recent list."
1058 (let ((cache (cons default-directory recentf-list)))
1059 ;; Does nothing, if nothing has changed.
1060 (unless (equal recentf-data-cache cache)
1061 (setq recentf-data-cache cache)
1062 (condition-case err
1063 (easy-menu-add-item
1064 (recentf-menu-bar) recentf-menu-path
1065 (easy-menu-create-menu recentf-menu-title
1066 (recentf-make-menu-items))
1067 recentf-menu-before)
1068 (error
1069 (message "recentf update menu failed: %s"
1070 (error-message-string err)))))))
1072 (defconst recentf-used-hooks
1074 (find-file-hook recentf-track-opened-file)
1075 (write-file-functions recentf-track-opened-file)
1076 (kill-buffer-hook recentf-track-closed-file)
1077 (menu-bar-update-hook recentf-update-menu)
1078 (kill-emacs-hook recentf-save-list)
1080 "Hooks used by recentf.")
1082 (defsubst recentf-enabled-p ()
1083 "Return non-nil if recentf mode is currently enabled."
1084 (memq 'recentf-update-menu menu-bar-update-hook))
1086 ;;; Commands
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 (recentf-clear-data))
1114 (message "No file selected")))
1116 (defun recentf-edit-list ()
1117 "Show a dialog to delete selected files from the recent list."
1118 (interactive)
1119 (recentf-dialog (format "*%s - Edit list*" recentf-menu-title)
1120 (set (make-local-variable 'recentf-edit-list) nil)
1121 (widget-insert
1122 "Click on OK to delete selected files from the recent list.
1123 Click on Cancel or type `q' to cancel.\n")
1124 ;; Insert the list of files as checkboxes
1125 (dolist (item recentf-list)
1126 (widget-create 'checkbox
1127 :value nil ; unselected checkbox
1128 :format "\n %[%v%] %t"
1129 :tag item
1130 :notify 'recentf-edit-list-select))
1131 (widget-insert "\n\n")
1132 (widget-create
1133 'push-button
1134 :notify 'recentf-edit-list-validate
1135 :help-echo "Delete selected files from the recent list"
1136 "Ok")
1137 (widget-insert " ")
1138 (widget-create
1139 'push-button
1140 :notify 'recentf-cancel-dialog
1141 "Cancel")
1142 (recentf-dialog-goto-first 'checkbox)))
1144 ;;; Open file dialog
1146 (defun recentf-open-files-action (widget &rest ignore)
1147 "Open the file stored in WIDGET's value when notified.
1148 IGNORE other arguments."
1149 (kill-buffer (current-buffer))
1150 (funcall recentf-menu-action (widget-value widget)))
1152 ;; List of files associated to a digit shortcut key.
1153 (defvar recentf--files-with-key nil)
1155 (defun recentf-show-digit-shortcut-filter (l)
1156 "Filter the list of menu-elements L to show digit shortcuts."
1157 (let ((i 0))
1158 (dolist (e l)
1159 (setq i (1+ i))
1160 (recentf-set-menu-element-item
1161 e (format "[%d] %s" (% i 10) (recentf-menu-element-item e))))
1164 (defun recentf-open-files-item (menu-element)
1165 "Return a widget to display MENU-ELEMENT in a dialog buffer."
1166 (if (consp (cdr menu-element))
1167 ;; Represent a sub-menu with a tree widget
1168 `(tree-widget
1169 :open t
1170 :match ignore
1171 :node (item :tag ,(car menu-element)
1172 :sample-face bold
1173 :format "%{%t%}:\n")
1174 ,@(mapcar 'recentf-open-files-item
1175 (cdr menu-element)))
1176 ;; Represent a single file with a link widget
1177 `(link :tag ,(car menu-element)
1178 :button-prefix ""
1179 :button-suffix ""
1180 :button-face default
1181 :format "%[%t%]\n"
1182 :help-echo ,(concat "Open " (cdr menu-element))
1183 :action recentf-open-files-action
1184 ,(cdr menu-element))))
1186 (defun recentf-open-files-items (files)
1187 "Return a list of widgets to display FILES in a dialog buffer."
1188 (set (make-local-variable 'recentf--files-with-key)
1189 (recentf-trunc-list files 10))
1190 (mapcar 'recentf-open-files-item
1191 (append
1192 ;; When requested group the files with shortcuts together
1193 ;; at the top of the list.
1194 (when recentf-show-file-shortcuts-flag
1195 (setq files (nthcdr 10 files))
1196 (recentf-apply-menu-filter
1197 'recentf-show-digit-shortcut-filter
1198 (mapcar 'recentf-make-default-menu-element
1199 recentf--files-with-key)))
1200 ;; Then the other files.
1201 (recentf-apply-menu-filter
1202 recentf-menu-filter
1203 (mapcar 'recentf-make-default-menu-element
1204 files)))))
1206 (defun recentf-open-files (&optional files buffer-name)
1207 "Show a dialog to open a recent file.
1208 If optional argument FILES is non-nil, it is a list of recently-opened
1209 files to choose from. It defaults to the whole recent list.
1210 If optional argument BUFFER-NAME is non-nil, it is a buffer name to
1211 use for the dialog. It defaults to \"*`recentf-menu-title'*\"."
1212 (interactive)
1213 (recentf-dialog (or buffer-name (format "*%s*" recentf-menu-title))
1214 (widget-insert "Click on a file"
1215 (if recentf-show-file-shortcuts-flag
1216 ", or type the corresponding digit key,"
1218 " to open it.\n"
1219 "Click on Cancel or type `q' to cancel.\n")
1220 ;; Use a L&F that looks like the recentf menu.
1221 (tree-widget-set-theme "folder")
1222 (apply 'widget-create
1223 `(group
1224 :indent 2
1225 :format "\n%v\n"
1226 ,@(recentf-open-files-items (or files recentf-list))))
1227 (widget-create
1228 'push-button
1229 :notify 'recentf-cancel-dialog
1230 "Cancel")
1231 (recentf-dialog-goto-first 'link)))
1233 (defun recentf-open-more-files ()
1234 "Show a dialog to open a recent file that is not in the menu."
1235 (interactive)
1236 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
1237 (format "*%s - More*" recentf-menu-title)))
1239 (defun recentf-open-most-recent-file (&optional n)
1240 "Open the Nth most recent file.
1241 Optional argument N must be a valid digit number. It defaults to 1.
1242 1 opens the most recent file, 2 the second most recent one, etc..
1243 0 opens the tenth most recent file."
1244 (interactive "p")
1245 (cond
1246 ((zerop n) (setq n 10))
1247 ((and (> n 0) (< n 10)))
1248 ((error "Recent file number out of range [0-9], %d" n)))
1249 (let ((file (nth (1- n) (or recentf--files-with-key recentf-list))))
1250 (unless file (error "Not that many recent files"))
1251 ;; Close the open files dialog.
1252 (when recentf--files-with-key
1253 (kill-buffer (current-buffer)))
1254 (funcall recentf-menu-action file)))
1256 ;;; Save/load/cleanup the recent list
1258 (defconst recentf-save-file-header
1259 ";;; Automatically generated by `recentf' on %s.\n"
1260 "Header to be written into the `recentf-save-file'.")
1262 (defconst recentf-save-file-coding-system
1263 (if (coding-system-p 'utf-8-emacs)
1264 'utf-8-emacs
1265 'emacs-mule)
1266 "Coding system of the file `recentf-save-file'.")
1268 (defun recentf-save-list ()
1269 "Save the recent list.
1270 Write data into the file specified by `recentf-save-file'."
1271 (interactive)
1272 (condition-case error
1273 (with-temp-buffer
1274 (erase-buffer)
1275 (set-buffer-file-coding-system recentf-save-file-coding-system)
1276 (insert (format recentf-save-file-header (current-time-string)))
1277 (recentf-dump-variable 'recentf-list recentf-max-saved-items)
1278 (recentf-dump-variable 'recentf-filter-changer-state)
1279 (insert "\n\f\n;;; Local Variables:\n"
1280 (format ";;; coding: %s\n" recentf-save-file-coding-system)
1281 ";;; End:\n")
1282 (write-file (expand-file-name recentf-save-file))
1283 (when recentf-save-file-modes
1284 (set-file-modes recentf-save-file recentf-save-file-modes))
1285 nil)
1286 (error
1287 (warn "recentf mode: %s" (error-message-string error)))))
1289 (defun recentf-load-list ()
1290 "Load a previously saved recent list.
1291 Read data from the file specified by `recentf-save-file'.
1292 When `recentf-initialize-file-name-history' is non-nil, initialize an
1293 empty `file-name-history' with the recent list."
1294 (interactive)
1295 (let ((file (expand-file-name recentf-save-file)))
1296 (when (file-readable-p file)
1297 (load-file file)
1298 (and recentf-initialize-file-name-history
1299 (not file-name-history)
1300 (setq file-name-history (mapcar 'abbreviate-file-name
1301 recentf-list))))))
1303 (defun recentf-cleanup ()
1304 "Cleanup the recent list.
1305 That is, remove duplicates, non-kept, and excluded files."
1306 (interactive)
1307 (message "Cleaning up the recentf list...")
1308 (let ((n 0) newlist)
1309 (dolist (f recentf-list)
1310 (setq f (recentf-expand-file-name f))
1311 (if (and (recentf-include-p f)
1312 (recentf-keep-p f)
1313 (not (recentf-string-member f newlist)))
1314 (push f newlist)
1315 (setq n (1+ n))
1316 (message "File %s removed from the recentf list" f)))
1317 (message "Cleaning up the recentf list...done (%d removed)" n)
1318 (setq recentf-list (nreverse newlist))))
1320 (defvar recentf-mode-map (make-sparse-keymap)
1321 "Keymap to use in recentf mode.")
1323 ;;;###autoload
1324 (define-minor-mode recentf-mode
1325 "Toggle recentf mode.
1326 With prefix argument ARG, turn on if positive, otherwise off.
1327 Returns non-nil if the new state is enabled.
1329 When recentf mode is enabled, it maintains a menu for visiting files
1330 that were operated on recently.
1332 \\{recentf-mode-map}"
1333 :global t
1334 :group 'recentf
1335 :keymap recentf-mode-map
1336 (unless (and recentf-mode (recentf-enabled-p))
1337 (if recentf-mode
1338 (recentf-load-list)
1339 (recentf-save-list))
1340 (recentf-auto-cleanup)
1341 (recentf-clear-data)
1342 (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
1343 (dolist (hook recentf-used-hooks)
1344 (apply hook-setup hook)))
1345 (run-hooks 'recentf-mode-hook)
1346 (when (interactive-p)
1347 (message "Recentf mode %sabled" (if recentf-mode "en" "dis"))))
1348 recentf-mode)
1350 (provide 'recentf)
1352 (run-hooks 'recentf-load-hook)
1354 ;; arch-tag: 78f1eec9-0d16-4d19-a4eb-2e4529edb62a
1355 ;;; recentf.el ends here