(diff-default-read-only): Change default.
[emacs.git] / lisp / recentf.el
blobf2700fcf21045664659fb385e6b91ae7f0b3fa11
1 ;;; recentf.el --- setup a menu of recently opened files
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003
4 ;; Free Software Foundation, Inc.
6 ;; Author: David Ponce <david@dponce.com>
7 ;; Created: July 19 1999
8 ;; Maintainer: FSF
9 ;; Keywords: files
11 (defconst recentf-version "$Revision: 1.27 $")
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published
17 ;; by the Free Software Foundation; either version 2, or (at your
18 ;; option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
30 ;;; Commentary:
32 ;; This package maintains a menu for visiting files that were operated
33 ;; on recently. When enabled a new "Open Recent" submenu is displayed
34 ;; in the "Files" menu. The recent files list is automatically saved
35 ;; across Emacs sessions. You can customize the number of recent
36 ;; files displayed, the location of the menu and others options (see
37 ;; the source code for details).
39 ;;; History:
42 ;;; Code:
43 (require 'easymenu)
44 (require 'wid-edit)
45 (require 'timer)
47 ;;; Internal data
49 (defvar recentf-list nil
50 "List of recently opened files.")
52 (defvar recentf-data-cache nil
53 "Cache of data used to build the recentf menu.
54 The menu is rebuilt when this data has changed.")
56 ;;; Customization
58 (defgroup recentf nil
59 "Maintain a menu of recently opened files."
60 :version "21.1"
61 :group 'files)
63 (defgroup recentf-filters nil
64 "Group to customize recentf menu filters.
65 You should define the options of your own filters in this group."
66 :group 'recentf)
68 (defcustom recentf-max-saved-items 20
69 "*Maximum number of items of the recent list that will be saved.
70 nil means to save the whole list.
71 See the command `recentf-save-list'."
72 :group 'recentf
73 :type 'integer)
75 (defcustom recentf-save-file "~/.recentf"
76 "*File to save the recent list into."
77 :group 'recentf
78 :type 'file)
80 (defcustom recentf-exclude nil
81 "*List of regexps and predicates for filenames excluded from the recent list.
82 When a filename matches any of the regexps or satisfies any of the
83 predicates it is excluded from the recent list.
84 A predicate is a function that is passed a filename to check and that
85 must return non-nil to exclude it."
86 :group 'recentf
87 :type '(repeat (choice regexp function)))
89 (defun recentf-menu-customization-changed (variable value)
90 "Function called when the recentf menu customization has changed.
91 Set VARIABLE with VALUE, and force a rebuild of the recentf menu."
92 (when (featurep 'recentf)
93 ;; Unavailable until recentf has been loaded.
94 (recentf-clear-data))
95 (set-default variable value))
97 (defcustom recentf-menu-title "Open Recent"
98 "*Name of the recentf menu."
99 :group 'recentf
100 :type 'string
101 :set 'recentf-menu-customization-changed)
103 (defcustom recentf-menu-path '("files")
104 "*Path where to add the recentf menu.
105 If nil add it at top level (see also `easy-menu-add-item')."
106 :group 'recentf
107 :type '(choice (const :tag "Top Level" nil)
108 (sexp :tag "Menu Path"))
109 :set 'recentf-menu-customization-changed)
111 (defcustom recentf-menu-before "Open File..."
112 "*Name of the menu before which the recentf menu will be added.
113 If nil add it at end of menu (see also `easy-menu-add-item')."
114 :group 'recentf
115 :type '(choice (string :tag "Name")
116 (const :tag "Last" nil))
117 :set 'recentf-menu-customization-changed)
119 (defcustom recentf-menu-action 'recentf-find-file
120 "*Function to invoke with a filename item of the recentf menu.
121 The default is to call `recentf-find-file' to edit the selected file."
122 :group 'recentf
123 :type 'function
124 :set 'recentf-menu-customization-changed)
126 (defcustom recentf-max-menu-items 10
127 "*Maximum number of items in the recentf menu."
128 :group 'recentf
129 :type 'integer
130 :set 'recentf-menu-customization-changed)
132 (defcustom recentf-menu-filter nil
133 "*Function used to filter files displayed in the recentf menu.
134 nil means no filter. The following functions are predefined:
136 - `recentf-sort-ascending'
137 Sort menu items in ascending order.
138 - `recentf-sort-descending'
139 Sort menu items in descending order.
140 - `recentf-sort-basenames-ascending'
141 Sort menu items by filenames sans directory in ascending order.
142 - `recentf-sort-basenames-descending'
143 Sort menu items by filenames sans directory in descending order.
144 - `recentf-sort-directories-ascending'
145 Sort menu items by directories in ascending order.
146 - `recentf-sort-directories-descending'
147 Sort menu items by directories in descending order.
148 - `recentf-show-basenames'
149 Show filenames sans directory in menu items.
150 - `recentf-show-basenames-ascending'
151 Show filenames sans directory in ascending order.
152 - `recentf-show-basenames-descending'
153 Show filenames sans directory in descending order.
154 - `recentf-relative-filter'
155 Show filenames relative to `default-directory'.
156 - `recentf-arrange-by-rule'
157 Show sub-menus following user defined rules.
158 - `recentf-arrange-by-mode'
159 Show a sub-menu for each major mode.
160 - `recentf-arrange-by-dir'
161 Show a sub-menu for each directory.
162 - `recentf-filter-changer'
163 Manage a ring of filters.
165 The filter function is called with one argument, the list of menu
166 elements used to build the menu and must return a new list of menu
167 elements (see `recentf-make-menu-element' for menu element form)."
168 :group 'recentf
169 :type '(radio (const nil)
170 (function-item recentf-sort-ascending)
171 (function-item recentf-sort-descending)
172 (function-item recentf-sort-basenames-ascending)
173 (function-item recentf-sort-basenames-descending)
174 (function-item recentf-sort-directories-ascending)
175 (function-item recentf-sort-directories-descending)
176 (function-item recentf-show-basenames)
177 (function-item recentf-show-basenames-ascending)
178 (function-item recentf-show-basenames-descending)
179 (function-item recentf-relative-filter)
180 (function-item recentf-arrange-by-rule)
181 (function-item recentf-arrange-by-mode)
182 (function-item recentf-arrange-by-dir)
183 (function-item recentf-filter-changer)
184 function)
185 :set 'recentf-menu-customization-changed)
187 (defcustom recentf-menu-append-commands-flag t
188 "*non-nil means to append command items to the menu."
189 :group 'recentf
190 :type 'boolean
191 :set 'recentf-menu-customization-changed)
193 (defvaralias 'recentf-menu-append-commands-p
194 'recentf-menu-append-commands-flag)
195 (make-obsolete-variable 'recentf-menu-append-commands-p
196 'recentf-menu-append-commands-flag
197 "21.4")
199 (defcustom recentf-keep-non-readable-files-flag nil
200 "*non-nil means to keep non readable files in the recent list."
201 :group 'recentf
202 :type 'boolean)
204 (defvaralias 'recentf-keep-non-readable-files-p
205 'recentf-keep-non-readable-files-flag)
206 (make-obsolete-variable 'recentf-keep-non-readable-files-p
207 'recentf-keep-non-readable-files-flag
208 "21.4")
210 (defcustom recentf-auto-cleanup 'mode
211 "*Define when to automatically cleanup the recent list.
212 The following values can be set:
214 - `mode'
215 Cleanup when turning the mode on (default).
216 - `never'
217 Never cleanup the list automatically.
218 - A number
219 Cleanup each time Emacs has been idle that number of seconds.
220 - A time string
221 Cleanup at specified time string, for example at \"11:00pm\".
223 Setting this variable directly does not take effect;
224 use \\[customize].
226 See also the command `recentf-cleanup', that can be used to manually
227 cleanup the list."
228 :group 'recentf
229 :type '(radio (const :tag "When mode enabled"
230 :value mode)
231 (const :tag "Never"
232 :value never)
233 (number :tag "When idle that seconds"
234 :value 300)
235 (string :tag "At time"
236 :value "11:00pm"))
237 :set (lambda (variable value)
238 (set-default variable value)
239 (when (featurep 'recentf)
240 ;; Unavailable until recentf has been loaded.
241 (recentf-auto-cleanup))))
243 (defcustom recentf-initialize-file-name-history t
244 "*non-nil means to initialize `file-name-history' with the recent list.
245 If `file-name-history' is not empty, do nothing."
246 :group 'recentf
247 :type 'boolean)
249 (defcustom recentf-load-hook nil
250 "*Normal hook run at end of loading the `recentf' package."
251 :group 'recentf
252 :type 'hook)
254 (defcustom recentf-filename-handler nil
255 "Function to call to process filename handled by recentf.
256 It is passed a filename to give a chance to transform it.
257 If it returns nil, the filename is left unchanged."
258 :group 'recentf
259 :type 'function)
261 ;;; Utilities
263 (defconst recentf-case-fold-search
264 (memq system-type '(vax-vms windows-nt cygwin))
265 "Non-nil if recentf searches and matches should ignore case.")
267 (defsubst recentf-string-equal (s1 s2)
268 "Return non-nil if strings S1 and S2 have identical contents.
269 Ignore case if `recentf-case-fold-search' is non-nil."
270 (if recentf-case-fold-search
271 (string-equal (downcase s1) (downcase s2))
272 (string-equal s1 s2)))
274 (defsubst recentf-string-lessp (s1 s2)
275 "Return non-nil if string S1 is less than S2 in lexicographic order.
276 Ignore case if `recentf-case-fold-search' is non-nil."
277 (if recentf-case-fold-search
278 (string-lessp (downcase s1) (downcase s2))
279 (string-lessp s1 s2)))
281 (defun recentf-string-member (elt list)
282 "Return non-nil if ELT is an element of LIST.
283 The value is actually the tail of LIST whose car is ELT.
284 ELT must be a string and LIST a list of strings.
285 Ignore case if `recentf-case-fold-search' is non-nil."
286 (while (and list (not (recentf-string-equal elt (car list))))
287 (setq list (cdr list)))
288 list)
290 (defsubst recentf-trunc-list (l n)
291 "Return from L the list of its first N elements."
292 (let (nl)
293 (while (and l (> n 0))
294 (setq nl (cons (car l) nl)
295 n (1- n)
296 l (cdr l)))
297 (nreverse nl)))
299 (defun recentf-dump-variable (variable &optional limit)
300 "Insert a \"(setq VARIABLE value)\" in the current buffer.
301 When the value of VARIABLE is a list, optional argument LIMIT
302 specifies a maximum number of elements to insert. By default insert
303 the full list."
304 (let ((value (symbol-value variable)))
305 (if (atom value)
306 (insert (format "\n(setq %S %S)\n" variable value))
307 (when (and (integerp limit) (> limit 0))
308 (setq value (recentf-trunc-list value limit)))
309 (insert (format "\n(setq %S\n '(" variable))
310 (dolist (e value)
311 (insert (format "\n %S" e)))
312 (insert "\n ))\n"))))
314 (defvar recentf-auto-cleanup-timer nil
315 "Timer used to automatically cleanup the recent list.
316 See also the option `recentf-auto-cleanup'.")
318 (defun recentf-auto-cleanup ()
319 "Automatic cleanup of the recent list."
320 (when (timerp recentf-auto-cleanup-timer)
321 (cancel-timer recentf-auto-cleanup-timer))
322 (when recentf-mode
323 (setq recentf-auto-cleanup-timer
324 (cond
325 ((eq 'mode recentf-auto-cleanup)
326 (recentf-cleanup)
327 nil)
328 ((numberp recentf-auto-cleanup)
329 (run-with-idle-timer
330 recentf-auto-cleanup t 'recentf-cleanup))
331 ((stringp recentf-auto-cleanup)
332 (run-at-time
333 recentf-auto-cleanup nil 'recentf-cleanup))))))
335 ;;; File functions
337 (defsubst recentf-push (filename)
338 "Push FILENAME into the recent list, if it isn't there yet.
339 If it is there yet, move it at the beginning of the list.
340 If `recentf-case-fold-search' is non-nil, ignore case when comparing
341 filenames."
342 (let ((m (recentf-string-member filename recentf-list)))
343 (and m (setq recentf-list (delq (car m) recentf-list)))
344 (push filename recentf-list)))
346 (defsubst recentf-expand-file-name (name)
347 "Convert filename NAME to absolute, and canonicalize it.
348 See also the function `expand-file-name'.
349 If defined, call the function `recentf-filename-handler' to post
350 process the canonical name."
351 (let* ((filename (expand-file-name name)))
352 (or (and recentf-filename-handler
353 (funcall recentf-filename-handler filename))
354 filename)))
356 (defsubst recentf-file-readable-p (filename)
357 "Return t if file FILENAME exists and you can read it.
358 Like the function `file-readable-p' but return nil on error."
359 (condition-case nil
360 (file-readable-p filename)
361 (error nil)))
363 (defun recentf-include-p (filename)
364 "Return non-nil if FILENAME should be included in the recent list.
365 That is, if it doesn't match any of the `recentf-exclude' checks."
366 (let ((case-fold-search recentf-case-fold-search)
367 (checks recentf-exclude)
368 (keepit t)
369 check)
370 (while (and checks keepit)
371 (setq check (car checks)
372 checks (cdr checks)
373 keepit (not (if (stringp check)
374 ;; A regexp
375 (string-match check filename)
376 ;; A predicate
377 (funcall check filename)))))
378 keepit))
380 (defsubst recentf-add-file (filename)
381 "Add or move FILENAME at the beginning of the recent list.
382 Does nothing if the name satisfies any of the `recentf-exclude' regexps or
383 predicates."
384 (setq filename (recentf-expand-file-name filename))
385 (when (recentf-include-p filename)
386 (recentf-push filename)))
388 (defsubst recentf-remove-if-non-readable (filename)
389 "Remove FILENAME from the recent list, if file is not readable.
390 Return non-nil if FILENAME has been removed."
391 (unless (recentf-file-readable-p filename)
392 (let ((m (recentf-string-member
393 (recentf-expand-file-name filename) recentf-list)))
394 (and m (setq recentf-list (delq (car m) recentf-list))))))
396 (defun recentf-find-file (filename)
397 "Edit file FILENAME using `find-file'.
398 If the file does not exist or is non readable, and
399 `recentf-keep-non-readable-files-flag' is nil, it is not edited and
400 its name is removed from the recent list."
401 (if (and (not recentf-keep-non-readable-files-flag)
402 (recentf-remove-if-non-readable filename))
403 (message "File `%s' not found" filename)
404 (find-file filename)))
406 (defsubst recentf-directory-compare (f1 f2)
407 "Compare absolute filenames F1 and F2.
408 First compare directories, then filenames sans directory.
409 Return non-nil if F1 is less than F2."
410 (let ((d1 (file-name-directory f1))
411 (d2 (file-name-directory f2)))
412 (if (recentf-string-equal d1 d2)
413 (recentf-string-lessp (file-name-nondirectory f1)
414 (file-name-nondirectory f2))
415 (recentf-string-lessp d1 d2))))
417 ;;; Menu building
419 (defvar recentf-menu-items-for-commands
420 (list ["Cleanup list"
421 recentf-cleanup
422 :help "Remove all non-readable and excluded files from the recent list"
423 :active t]
424 ["Edit list..."
425 recentf-edit-list
426 :help "Edit the files that are kept in the recent list"
427 :active t]
428 ["Save list now"
429 recentf-save-list
430 :help "Save the list of recently opened files now"
431 :active t]
432 ["Options..."
433 (customize-group "recentf")
434 :help "Customize recently opened files menu and options"
435 :active t]
437 "List of menu items for recentf commands.")
439 (defvar recentf-menu-filter-commands nil
440 "This variable can be used by menu filters to setup their own command menu.
441 If non-nil it must contain a list of valid menu-items to be appended
442 to the recent file list part of the menu. Before calling a menu
443 filter function this variable is reset to nil.")
445 (defsubst recentf-elements (n)
446 "Return a list of the first N elements of the recent list."
447 (recentf-trunc-list recentf-list n))
449 (defsubst recentf-make-menu-element (menu-item menu-value)
450 "Create a new menu-element.
451 A menu element is a pair (MENU-ITEM . MENU-VALUE), where MENU-ITEM is
452 the menu item string displayed. MENU-VALUE is the file to be open
453 when the corresponding MENU-ITEM is selected. Or it is a
454 pair (SUB-MENU-TITLE . MENU-ELEMENTS) where SUB-MENU-TITLE is a
455 sub-menu title and MENU-ELEMENTS is the list of menu elements in the
456 sub-menu."
457 (cons menu-item menu-value))
459 (defsubst recentf-menu-element-item (e)
460 "Return the item part of the menu-element E."
461 (car e))
463 (defsubst recentf-menu-element-value (e)
464 "Return the value part of the menu-element E."
465 (cdr e))
467 (defsubst recentf-set-menu-element-item (e item)
468 "Change the item part of menu-element E to ITEM."
469 (setcar e item))
471 (defsubst recentf-set-menu-element-value (e value)
472 "Change the value part of menu-element E to VALUE."
473 (setcdr e value))
475 (defsubst recentf-sub-menu-element-p (e)
476 "Return non-nil if menu-element E defines a sub-menu."
477 (consp (recentf-menu-element-value e)))
479 (defsubst recentf-make-default-menu-element (file)
480 "Make a new default menu element with FILE.
481 This a menu element (FILE . FILE)."
482 (recentf-make-menu-element file file))
484 (defsubst recentf-menu-elements (n)
485 "Return a list of the first N default menu elements from the recent list.
486 See also `recentf-make-default-menu-element'."
487 (mapcar 'recentf-make-default-menu-element
488 (recentf-elements n)))
490 (defun recentf-apply-menu-filter (filter l)
491 "Apply function FILTER to the list of menu-elements L.
492 It takes care of sub-menu elements in L and recursively apply FILTER
493 to them. It is guaranteed that FILTER receives only a list of single
494 menu-elements (no sub-menu)."
495 (if (and l (functionp filter))
496 (let ((case-fold-search recentf-case-fold-search)
497 elts others)
498 ;; split L into two sub-listes, one of sub-menus elements and
499 ;; another of single menu elements.
500 (dolist (elt l)
501 (if (recentf-sub-menu-element-p elt)
502 (push elt elts)
503 (push elt others)))
504 ;; Apply FILTER to single elements.
505 (when others
506 (setq others (funcall filter (nreverse others))))
507 ;; Apply FILTER to sub-menu elements.
508 (setq l nil)
509 (dolist (elt elts)
510 (recentf-set-menu-element-value
511 elt (recentf-apply-menu-filter
512 filter (recentf-menu-element-value elt)))
513 (push elt l))
514 ;; Return the new filtered menu element list.
515 (nconc l others))
518 (defun recentf-make-menu-items ()
519 "Make menu items from the recent list."
520 (setq recentf-menu-filter-commands nil)
521 (let ((file-items
522 (mapcar 'recentf-make-menu-item
523 (recentf-apply-menu-filter
524 recentf-menu-filter
525 (recentf-menu-elements recentf-max-menu-items)))))
526 (append (or file-items (list ["No files" t
527 :help "No recent file to open"
528 :active nil]))
529 (and (< recentf-max-menu-items (length recentf-list))
530 (list ["More..." recentf-open-more-files
531 :help "Open files that are not in the menu"
532 :active t]))
533 (and recentf-menu-filter-commands
534 (cons "---"
535 recentf-menu-filter-commands))
536 (and recentf-menu-append-commands-flag
537 (cons "---"
538 recentf-menu-items-for-commands)))))
540 (defsubst recentf-make-menu-item (elt)
541 "Make a menu item from menu element ELT."
542 (let ((item (recentf-menu-element-item elt))
543 (value (recentf-menu-element-value elt)))
544 (if (recentf-sub-menu-element-p elt)
545 (cons item (mapcar 'recentf-make-menu-item value))
546 (vector item (list recentf-menu-action value)
547 :help (concat "Open " value)
548 :active t))))
550 (defsubst recentf-menu-bar ()
551 "Return the keymap of the global menu bar."
552 (lookup-key global-map [menu-bar]))
554 (defun recentf-clear-data ()
555 "Clear data used to build the recentf menu.
556 This force a rebuild of the menu."
557 (easy-menu-remove-item (recentf-menu-bar)
558 recentf-menu-path recentf-menu-title)
559 (setq recentf-data-cache nil))
561 ;;; Predefined menu filters
563 (defsubst recentf-sort-ascending (l)
564 "Sort the list of menu elements L in ascending order.
565 The MENU-ITEM part of each menu element is compared."
566 (sort (copy-sequence l)
567 #'(lambda (e1 e2)
568 (recentf-string-lessp
569 (recentf-menu-element-item e1)
570 (recentf-menu-element-item e2)))))
572 (defsubst recentf-sort-descending (l)
573 "Sort the list of menu elements L in descending order.
574 The MENU-ITEM part of each menu element is compared."
575 (sort (copy-sequence l)
576 #'(lambda (e1 e2)
577 (recentf-string-lessp
578 (recentf-menu-element-item e2)
579 (recentf-menu-element-item e1)))))
581 (defsubst recentf-sort-basenames-ascending (l)
582 "Sort the list of menu elements L in ascending order.
583 Only filenames sans directory are compared."
584 (sort (copy-sequence l)
585 #'(lambda (e1 e2)
586 (recentf-string-lessp
587 (file-name-nondirectory (recentf-menu-element-value e1))
588 (file-name-nondirectory (recentf-menu-element-value e2))))))
590 (defsubst recentf-sort-basenames-descending (l)
591 "Sort the list of menu elements L in descending order.
592 Only filenames sans directory are compared."
593 (sort (copy-sequence l)
594 #'(lambda (e1 e2)
595 (recentf-string-lessp
596 (file-name-nondirectory (recentf-menu-element-value e2))
597 (file-name-nondirectory (recentf-menu-element-value e1))))))
599 (defsubst recentf-sort-directories-ascending (l)
600 "Sort the list of menu elements L in ascending order.
601 Compares directories then filenames to order the list."
602 (sort (copy-sequence l)
603 #'(lambda (e1 e2)
604 (recentf-directory-compare
605 (recentf-menu-element-value e1)
606 (recentf-menu-element-value e2)))))
608 (defsubst recentf-sort-directories-descending (l)
609 "Sort the list of menu elements L in descending order.
610 Compares directories then filenames to order the list."
611 (sort (copy-sequence l)
612 #'(lambda (e1 e2)
613 (recentf-directory-compare
614 (recentf-menu-element-value e2)
615 (recentf-menu-element-value e1)))))
617 (defun recentf-show-basenames (l &optional no-dir)
618 "Filter the list of menu elements L to show filenames sans directory.
619 When a filename is duplicated, it is appended a sequence number if
620 optional argument NO-DIR is non-nil, or its directory otherwise."
621 (let (filtered-names filtered-list full name counters sufx)
622 (dolist (elt l (nreverse filtered-list))
623 (setq full (recentf-menu-element-value elt)
624 name (file-name-nondirectory full))
625 (if (not (member name filtered-names))
626 (push name filtered-names)
627 (if no-dir
628 (if (setq sufx (assoc name counters))
629 (setcdr sufx (1+ (cdr sufx)))
630 (setq sufx 1)
631 (push (cons name sufx) counters))
632 (setq sufx (file-name-directory full)))
633 (setq name (format "%s(%s)" name sufx)))
634 (push (recentf-make-menu-element name full) filtered-list))))
636 (defsubst recentf-show-basenames-ascending (l)
637 "Filter the list of menu elements L to show filenames sans directory.
638 Filenames are sorted in ascending order.
639 This filter combines the `recentf-sort-basenames-ascending' and
640 `recentf-show-basenames' filters."
641 (recentf-show-basenames (recentf-sort-basenames-ascending l)))
643 (defsubst recentf-show-basenames-descending (l)
644 "Filter the list of menu elements L to show filenames sans directory.
645 Filenames are sorted in descending order.
646 This filter combines the `recentf-sort-basenames-descending' and
647 `recentf-show-basenames' filters."
648 (recentf-show-basenames (recentf-sort-basenames-descending l)))
650 (defun recentf-relative-filter (l)
651 "Filter the list of menu-elements L to show relative filenames.
652 Filenames are relative to the `default-directory'."
653 (mapcar #'(lambda (menu-element)
654 (let* ((ful (recentf-menu-element-value menu-element))
655 (rel (file-relative-name ful default-directory)))
656 (if (string-match "^\\.\\." rel)
657 menu-element
658 (recentf-make-menu-element rel ful))))
661 ;;; Rule based menu filters
663 (defcustom recentf-arrange-rules
665 ("Elisp files (%d)" ".\\.el$")
666 ("Java files (%d)" ".\\.java$")
667 ("C/C++ files (%d)" "c\\(pp\\)?$")
669 "*List of rules used by `recentf-arrange-by-rule' to build sub-menus.
670 A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
671 displayed title of the sub-menu where a '%d' `format' pattern is
672 replaced by the number of items in the sub-menu. MATCHER is a regexp
673 or a list of regexps. Items matching one of the regular expressions in
674 MATCHER are added to the corresponding sub-menu."
675 :group 'recentf-filters
676 :type '(repeat (cons string (repeat regexp)))
677 :set 'recentf-menu-customization-changed)
679 (defcustom recentf-arrange-by-rule-others "Other files (%d)"
680 "*Title of the `recentf-arrange-by-rule' sub-menu.
681 This is for the menu where items that don't match any
682 `recentf-arrange-rules' are displayed. If nil these items are
683 displayed in the main recent files menu. A '%d' `format' pattern in
684 the title is replaced by the number of items in the sub-menu."
685 :group 'recentf-filters
686 :type '(choice (const :tag "Main menu" nil)
687 (string :tag "Title"))
688 :set 'recentf-menu-customization-changed)
690 (defcustom recentf-arrange-by-rules-min-items 0
691 "*Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
692 If the number of items in a sub-menu is less than this value the
693 corresponding sub-menu items are displayed in the main recent files
694 menu or in the `recentf-arrange-by-rule-others' sub-menu if
695 defined."
696 :group 'recentf-filters
697 :type 'number
698 :set 'recentf-menu-customization-changed)
700 (defcustom recentf-arrange-by-rule-subfilter nil
701 "*Function called by a rule based filter to filter sub-menu elements.
702 nil means no filter. See also `recentf-menu-filter'.
703 You can't use another rule based filter here."
704 :group 'recentf-filters
705 :type '(choice (const nil) function)
706 :set (lambda (variable value)
707 (when (memq value '(recentf-arrange-by-rule
708 recentf-arrange-by-mode
709 recentf-arrange-by-dir))
710 (error "Recursive use of a rule based filter"))
711 (recentf-menu-customization-changed variable value)))
713 (defun recentf-match-rule-p (matcher filename)
714 "Return non-nil if the rule specified by MATCHER match FILENAME.
715 See `recentf-arrange-rules' for details on MATCHER."
716 (if (stringp matcher)
717 (string-match matcher filename)
718 (while (and (consp matcher)
719 (not (string-match (car matcher) filename)))
720 (setq matcher (cdr matcher)))
721 matcher))
723 (defun recentf-arrange-by-rule (l)
724 "Filter the list of menu-elements L.
725 Arrange them in sub-menus following rules in `recentf-arrange-rules'."
726 (if (not recentf-arrange-rules)
728 (let ((menus (mapcar #'(lambda (r) (list (car r)))
729 recentf-arrange-rules))
730 menu others min file rules elts count)
731 (dolist (elt l)
732 (setq file (recentf-menu-element-value elt)
733 rules recentf-arrange-rules
734 elts menus
735 menu nil)
736 (while (and (not menu) rules)
737 (when (recentf-match-rule-p (cdar rules) file)
738 (setq menu (car elts))
739 (recentf-set-menu-element-value
740 menu (cons elt (recentf-menu-element-value menu))))
741 (setq rules (cdr rules)
742 elts (cdr elts)))
743 (unless menu
744 (push elt others)))
746 (setq l nil
747 min (if (natnump recentf-arrange-by-rules-min-items)
748 recentf-arrange-by-rules-min-items 0))
749 (dolist (menu menus)
750 (when (setq elts (recentf-menu-element-value menu))
751 (setq count (length elts))
752 (if (< count min)
753 (setq others (nconc elts others))
754 (recentf-set-menu-element-item
755 menu (format (recentf-menu-element-item menu) count))
756 (recentf-set-menu-element-value
757 menu (recentf-apply-menu-filter
758 recentf-arrange-by-rule-subfilter (nreverse elts)))
759 (push menu l))))
761 (if (and (stringp recentf-arrange-by-rule-others) others)
762 (nreverse
763 (cons
764 (recentf-make-menu-element
765 (format recentf-arrange-by-rule-others (length others))
766 (recentf-apply-menu-filter
767 recentf-arrange-by-rule-subfilter (nreverse others)))
769 (nconc
770 (nreverse l)
771 (recentf-apply-menu-filter
772 recentf-arrange-by-rule-subfilter (nreverse others)))))
775 ;;; Predefined rule based menu filters
777 (defun recentf-build-mode-rules ()
778 "Convert `auto-mode-alist' to menu filter rules.
779 Rules obey `recentf-arrange-rules' format."
780 (let ((case-fold-search recentf-case-fold-search)
781 regexp rule-name rule rules)
782 (dolist (mode auto-mode-alist)
783 (setq regexp (car mode)
784 mode (cdr mode))
785 (when (symbolp mode)
786 (setq rule-name (symbol-name mode))
787 (if (string-match "\\(.*\\)-mode$" rule-name)
788 (setq rule-name (match-string 1 rule-name)))
789 (setq rule-name (concat rule-name " (%d)")
790 rule (assoc rule-name rules))
791 (if rule
792 (setcdr rule (cons regexp (cdr rule)))
793 (push (list rule-name regexp) rules))))
794 ;; It is important to preserve auto-mode-alist order
795 ;; to ensure the right file <-> mode association
796 (nreverse rules)))
798 (defun recentf-arrange-by-mode (l)
799 "Split the list of menu-elements L into sub-menus by major mode."
800 (let ((recentf-arrange-rules (recentf-build-mode-rules))
801 (recentf-arrange-by-rule-others "others (%d)"))
802 (recentf-arrange-by-rule l)))
804 (defun recentf-build-dir-rules (l)
805 "Convert directories in menu-elements L to menu filter rules.
806 Rules obey `recentf-arrange-rules' format."
807 (let (dirs)
808 (mapcar #'(lambda (e)
809 (let ((dir (file-name-directory
810 (recentf-menu-element-value e))))
811 (or (recentf-string-member dir dirs)
812 (push dir dirs))))
814 (mapcar #'(lambda (d)
815 (cons (concat d " (%d)")
816 (concat "\\`" d)))
817 (nreverse (sort dirs 'recentf-string-lessp)))))
819 (defun recentf-file-name-nondir (l)
820 "Filter the list of menu-elements L to show filenames sans directory.
821 This simplified version of `recentf-show-basenames' does not handle
822 duplicates. It is used by `recentf-arrange-by-dir' as its
823 `recentf-arrange-by-rule-subfilter'."
824 (mapcar #'(lambda (e)
825 (recentf-make-menu-element
826 (file-name-nondirectory (recentf-menu-element-value e))
827 (recentf-menu-element-value e)))
830 (defun recentf-arrange-by-dir (l)
831 "Split the list of menu-elements L into sub-menus by directory."
832 (let ((recentf-arrange-rules (recentf-build-dir-rules l))
833 (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
834 recentf-arrange-by-rule-others)
835 (nreverse (recentf-arrange-by-rule l))))
837 ;;; Ring of menu filters
839 (defvar recentf-filter-changer-state nil
840 "Used by `recentf-filter-changer' to hold its state.")
842 (defcustom recentf-filter-changer-alist
844 (recentf-arrange-by-mode . "*Files by Mode*")
845 (recentf-arrange-by-dir . "*Files by Directory*")
846 (recentf-arrange-by-rule . "*Files by User Rule*")
848 "*List of filters managed by `recentf-filter-changer'.
849 Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is
850 the filter function, and LABEL is the menu item displayed to select
851 that filter."
852 :group 'recentf-filters
853 :type '(repeat (cons function string))
854 :set (lambda (variable value)
855 (setq recentf-filter-changer-state nil)
856 (recentf-menu-customization-changed variable value)))
858 (defun recentf-filter-changer-goto-next ()
859 "Go to the next filter available.
860 See `recentf-filter-changer'."
861 (setq recentf-filter-changer-state (cdr recentf-filter-changer-state))
862 (recentf-clear-data))
864 (defsubst recentf-filter-changer-get-current ()
865 "Get the current filter available.
866 See `recentf-filter-changer'."
867 (unless recentf-filter-changer-state
868 (setq recentf-filter-changer-state recentf-filter-changer-alist))
869 (car recentf-filter-changer-state))
871 (defsubst recentf-filter-changer-get-next ()
872 "Get the next filter available.
873 See `recentf-filter-changer'."
874 ;; At this point the current filter is the first element of
875 ;; `recentf-filter-changer-state'.
876 (car (or (cdr recentf-filter-changer-state)
877 ;; There is no next element in
878 ;; `recentf-filter-changer-state', so loop back to the
879 ;; first element of `recentf-filter-changer-alist'.
880 recentf-filter-changer-alist)))
882 (defun recentf-filter-changer (l)
883 "Manage a ring of menu filters.
884 `recentf-filter-changer-alist' defines the filters in the ring.
885 Filtering of L is delegated to the current filter in the ring. A
886 filter menu item is displayed allowing to dynamically activate the
887 next filter in the ring. If the filter ring is empty, L is left
888 unchanged."
889 (let ((filter (recentf-filter-changer-get-current)))
890 (when filter
891 (setq l (recentf-apply-menu-filter (car filter) l)
892 filter (recentf-filter-changer-get-next))
893 (when filter
894 (setq recentf-menu-filter-commands
895 (list (vector (cdr filter)
896 '(recentf-filter-changer-goto-next)
897 t)))))
900 ;;; Common dialog stuff
902 (defun recentf-cancel-dialog (&rest ignore)
903 "Cancel the current dialog.
904 Used internally by recentf dialogs.
905 IGNORE arguments."
906 (interactive)
907 (kill-buffer (current-buffer))
908 (message "Dialog canceled"))
910 (defvar recentf-dialog-mode-map
911 (let ((km (make-sparse-keymap)))
912 (define-key km "q" 'recentf-cancel-dialog)
913 (define-key km [down-mouse-1] 'widget-button-click)
914 (set-keymap-parent km widget-keymap)
916 "Keymap used in recentf dialogs.")
918 (defun recentf-dialog-mode ()
919 "Major mode of recentf dialogs.
921 \\{recentf-dialog-mode-map}"
922 (interactive)
923 (setq major-mode 'recentf-dialog-mode)
924 (setq mode-name "recentf-dialog")
925 (use-local-map recentf-dialog-mode-map))
927 ;;; Hooks
929 (defun recentf-track-opened-file ()
930 "Insert the name of the file just opened or written into the recent list."
931 (and buffer-file-name
932 (recentf-add-file buffer-file-name))
933 ;; Must return nil because it is run from `write-file-functions'.
934 nil)
936 (defun recentf-track-closed-file ()
937 "Update the recent list when a buffer is killed.
938 That is, remove a non readable file from the recent list, if
939 `recentf-keep-non-readable-files-flag' is nil."
940 (and buffer-file-name
941 (not recentf-keep-non-readable-files-flag)
942 (recentf-remove-if-non-readable buffer-file-name)))
944 (defun recentf-update-menu ()
945 "Update the recentf menu from the current recent list."
946 (let ((cache (cons default-directory recentf-list)))
947 ;; Does nothing, if nothing has changed.
948 (unless (equal recentf-data-cache cache)
949 (setq recentf-data-cache cache)
950 (condition-case err
951 (easy-menu-add-item
952 (recentf-menu-bar) recentf-menu-path
953 (easy-menu-create-menu recentf-menu-title
954 (recentf-make-menu-items))
955 recentf-menu-before)
956 (error
957 (message "recentf update menu failed: %s"
958 (error-message-string err)))))))
960 (defconst recentf-used-hooks
962 (find-file-hook recentf-track-opened-file)
963 (write-file-functions recentf-track-opened-file)
964 (kill-buffer-hook recentf-track-closed-file)
965 (menu-bar-update-hook recentf-update-menu)
966 (kill-emacs-hook recentf-save-list)
968 "Hooks used by recentf.")
970 (defsubst recentf-enabled-p ()
971 "Return non-nil if recentf mode is currently enabled."
972 (memq 'recentf-update-menu menu-bar-update-hook))
974 ;;; Commands
976 (defvar recentf-edit-selected-items nil
977 "List of files to be deleted from the recent list.
978 Used internally by `recentf-edit-list'.")
980 (defun recentf-edit-list-action (widget &rest ignore)
981 "Checkbox WIDGET action that toogles a file selection.
982 Used internally by `recentf-edit-list'.
983 IGNORE other arguments."
984 (let ((value (widget-get widget ':tag)))
985 ;; if value is already in the selected items
986 (if (memq value recentf-edit-selected-items)
987 ;; then remove it
988 (progn
989 (setq recentf-edit-selected-items
990 (delq value recentf-edit-selected-items))
991 (message "%s removed from selection" value))
992 ;; else add it
993 (push value recentf-edit-selected-items)
994 (message "%s added to selection" value))))
996 (defun recentf-edit-list ()
997 "Show a dialog buffer to edit the recent list.
998 That is to select files to be deleted from the recent list."
999 (interactive)
1000 (with-current-buffer
1001 (get-buffer-create (format "*%s - Edit list*" recentf-menu-title))
1002 (switch-to-buffer (current-buffer))
1003 ;; Cleanup buffer
1004 (kill-all-local-variables)
1005 (let ((inhibit-read-only t)
1006 (ol (overlay-lists)))
1007 (erase-buffer)
1008 ;; Delete all the overlays.
1009 (mapc 'delete-overlay (car ol))
1010 (mapc 'delete-overlay (cdr ol)))
1011 (setq recentf-edit-selected-items nil)
1012 ;; Insert the dialog header
1013 (widget-insert
1015 Select the files to be deleted from the recent list.\n\n\
1016 Click on Ok to update the list. \
1017 Click on Cancel or type \"q\" to quit.\n")
1018 ;; Insert the list of files as checkboxes
1019 (dolist (item recentf-list)
1020 (widget-create
1021 'checkbox
1022 :value nil ; unselected checkbox
1023 :format "\n %[%v%] %t"
1024 :tag item
1025 :notify 'recentf-edit-list-action))
1026 (widget-insert "\n\n")
1027 ;; Insert the Ok button
1028 (widget-create
1029 'push-button
1030 :notify (lambda (&rest ignore)
1031 (if recentf-edit-selected-items
1032 (let ((i 0))
1033 (kill-buffer (current-buffer))
1034 (dolist (e recentf-edit-selected-items)
1035 (setq recentf-list (delq e recentf-list)
1036 i (1+ i)))
1037 (message "%S file(s) removed from the list" i))
1038 (message "No file selected")))
1039 "Ok")
1040 (widget-insert " ")
1041 ;; Insert the Cancel button
1042 (widget-create
1043 'push-button
1044 :notify 'recentf-cancel-dialog
1045 "Cancel")
1046 (recentf-dialog-mode)
1047 (widget-setup)
1048 (goto-char (point-min))))
1050 (defun recentf-open-files-action (widget &rest ignore)
1051 "Button WIDGET action that open a file.
1052 Used internally by `recentf-open-files'.
1053 IGNORE other arguments."
1054 (kill-buffer (current-buffer))
1055 (funcall recentf-menu-action (widget-value widget)))
1057 (defvar recentf-open-files-item-shift ""
1058 "Amount of space to shift right sub-menu items.
1059 Used internally by `recentf-open-files'.")
1061 (defun recentf-open-files-item (menu-element)
1062 "Insert an item widget for MENU-ELEMENT in the current dialog buffer.
1063 Used internally by `recentf-open-files'."
1064 (let ((item (car menu-element))
1065 (file (cdr menu-element)))
1066 (if (consp file) ; This is a sub-menu
1067 (let* ((shift recentf-open-files-item-shift)
1068 (recentf-open-files-item-shift (concat shift " ")))
1069 (widget-create
1070 'item
1071 :tag item
1072 :sample-face 'bold
1073 :format (concat shift "%{%t%}:\n"))
1074 (mapc 'recentf-open-files-item file)
1075 (widget-insert "\n"))
1076 (widget-create
1077 'push-button
1078 :button-face 'default
1079 :tag item
1080 :help-echo (concat "Open " file)
1081 :format (concat recentf-open-files-item-shift "%[%t%]")
1082 :notify 'recentf-open-files-action
1083 file)
1084 (widget-insert "\n"))))
1086 (defun recentf-open-files (&optional files buffer-name)
1087 "Show a dialog buffer to open a recent file.
1088 If optional argument FILES is non-nil, it specifies the list of
1089 recently-opened files to choose from. It is the whole recent list
1090 otherwise.
1091 If optional argument BUFFER-NAME is non-nil, it specifies which buffer
1092 name to use for the interaction. It is \"*`recentf-menu-title'*\" by
1093 default."
1094 (interactive)
1095 (unless files
1096 (setq files recentf-list))
1097 (unless buffer-name
1098 (setq buffer-name (format "*%s*" recentf-menu-title)))
1099 (with-current-buffer (get-buffer-create buffer-name)
1100 (switch-to-buffer (current-buffer))
1101 ;; Cleanup buffer
1102 (kill-all-local-variables)
1103 (let ((inhibit-read-only t)
1104 (ol (overlay-lists)))
1105 (erase-buffer)
1106 ;; Delete all the overlays.
1107 (mapc 'delete-overlay (car ol))
1108 (mapc 'delete-overlay (cdr ol)))
1109 ;; Insert the dialog header
1110 (widget-insert "Click on a file to open it. ")
1111 (widget-insert "Click on Cancel or type \"q\" to quit.\n\n" )
1112 ;; Insert the list of files as buttons
1113 (let ((recentf-open-files-item-shift ""))
1114 (mapc 'recentf-open-files-item
1115 (recentf-apply-menu-filter
1116 recentf-menu-filter
1117 (mapcar 'recentf-make-default-menu-element files))))
1118 (widget-insert "\n")
1119 ;; Insert the Cancel button
1120 (widget-create
1121 'push-button
1122 :notify 'recentf-cancel-dialog
1123 "Cancel")
1124 (recentf-dialog-mode)
1125 (widget-setup)
1126 (goto-char (point-min))))
1128 (defun recentf-open-more-files ()
1129 "Show a dialog buffer to open a recent file that is not in the menu."
1130 (interactive)
1131 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
1132 (format "*%s - More*" recentf-menu-title)))
1134 (defconst recentf-save-file-header
1135 ";;; Automatically generated by `recentf' on %s.\n"
1136 "Header to be written into the `recentf-save-file'.")
1138 (defun recentf-save-list ()
1139 "Save the recent list.
1140 Write data into the file specified by `recentf-save-file'."
1141 (interactive)
1142 (with-temp-buffer
1143 (erase-buffer)
1144 (insert (format recentf-save-file-header (current-time-string)))
1145 (recentf-dump-variable 'recentf-list recentf-max-saved-items)
1146 (recentf-dump-variable 'recentf-filter-changer-state)
1147 (write-file (expand-file-name recentf-save-file))
1148 nil))
1150 (defun recentf-load-list ()
1151 "Load a previously saved recent list.
1152 Read data from the file specified by `recentf-save-file'.
1153 When `recentf-initialize-file-name-history' is non-nil, initialize an
1154 empty `file-name-history' with the recent list."
1155 (interactive)
1156 (let ((file (expand-file-name recentf-save-file)))
1157 (when (file-readable-p file)
1158 (load-file file)
1159 (and recentf-initialize-file-name-history
1160 (not file-name-history)
1161 (setq file-name-history (mapcar 'abbreviate-file-name
1162 recentf-list))))))
1164 (defun recentf-cleanup ()
1165 "Remove all excluded or non-readable files from the recent list."
1166 (interactive)
1167 (message "Cleaning up the recentf list...")
1168 (let (newlist)
1169 (dolist (f recentf-list)
1170 (if (and (recentf-include-p f) (recentf-file-readable-p f))
1171 (push f newlist)
1172 (message "File %s removed from the recentf list" f)))
1173 (setq recentf-list (nreverse newlist))
1174 (message "Cleaning up the recentf list...done")))
1176 ;;;###autoload
1177 (define-minor-mode recentf-mode
1178 "Toggle recentf mode.
1179 With prefix argument ARG, turn on if positive, otherwise off.
1180 Returns non-nil if the new state is enabled.
1182 When recentf mode is enabled, it maintains a menu for visiting files
1183 that were operated on recently."
1184 :global t
1185 :group 'recentf
1186 (unless (and recentf-mode (recentf-enabled-p))
1187 (if recentf-mode
1188 (recentf-load-list)
1189 (recentf-save-list))
1190 (recentf-auto-cleanup)
1191 (recentf-clear-data)
1192 (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
1193 (dolist (hook recentf-used-hooks)
1194 (apply hook-setup hook)))
1195 (run-hooks 'recentf-mode-hook)
1196 (when (interactive-p)
1197 (message "Recentf mode %sabled" (if recentf-mode "en" "dis"))))
1198 recentf-mode)
1200 (provide 'recentf)
1202 (run-hooks 'recentf-load-hook)
1204 ;;; arch-tag: 78f1eec9-0d16-4d19-a4eb-2e4529edb62a
1205 ;;; recentf.el ends here