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