]> code.delx.au - gnu-emacs/blob - lisp/recentf.el
(recentf-keep): New option.
[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., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, 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" submenu is displayed
32 ;; in the "Files" menu. The recent files list is automatically saved
33 ;; across Emacs sessions. You can customize the number of recent
34 ;; files displayed, the location of the menu and others options (see
35 ;; the source code for details).
36
37 ;;; History:
38 ;;
39
40 ;;; Code:
41 (require 'easymenu)
42 (require 'wid-edit)
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 'function)
263 \f
264 ;;; Utilities
265 ;;
266 (defconst recentf-case-fold-search
267 (memq system-type '(vax-vms windows-nt cygwin))
268 "Non-nil if recentf searches and matches should ignore case.")
269
270 (defsubst recentf-string-equal (s1 s2)
271 "Return non-nil if strings S1 and S2 have identical contents.
272 Ignore case if `recentf-case-fold-search' is non-nil."
273 (if recentf-case-fold-search
274 (string-equal (downcase s1) (downcase s2))
275 (string-equal s1 s2)))
276
277 (defsubst recentf-string-lessp (s1 s2)
278 "Return non-nil if string S1 is less than S2 in lexicographic order.
279 Ignore case if `recentf-case-fold-search' is non-nil."
280 (if recentf-case-fold-search
281 (string-lessp (downcase s1) (downcase s2))
282 (string-lessp s1 s2)))
283
284 (defun recentf-string-member (elt list)
285 "Return non-nil if ELT is an element of LIST.
286 The value is actually the tail of LIST whose car is ELT.
287 ELT must be a string and LIST a list of strings.
288 Ignore case if `recentf-case-fold-search' is non-nil."
289 (while (and list (not (recentf-string-equal elt (car list))))
290 (setq list (cdr list)))
291 list)
292
293 (defsubst recentf-trunc-list (l n)
294 "Return from L the list of its first N elements."
295 (let (nl)
296 (while (and l (> n 0))
297 (setq nl (cons (car l) nl)
298 n (1- n)
299 l (cdr l)))
300 (nreverse nl)))
301
302 (defun recentf-dump-variable (variable &optional limit)
303 "Insert a \"(setq VARIABLE value)\" in the current buffer.
304 When the value of VARIABLE is a list, optional argument LIMIT
305 specifies a maximum number of elements to insert. By default insert
306 the full list."
307 (let ((value (symbol-value variable)))
308 (if (atom value)
309 (insert (format "\n(setq %S %S)\n" variable value))
310 (when (and (integerp limit) (> limit 0))
311 (setq value (recentf-trunc-list value limit)))
312 (insert (format "\n(setq %S\n '(" variable))
313 (dolist (e value)
314 (insert (format "\n %S" e)))
315 (insert "\n ))\n"))))
316
317 (defvar recentf-auto-cleanup-timer nil
318 "Timer used to automatically cleanup the recent list.
319 See also the option `recentf-auto-cleanup'.")
320
321 (defun recentf-auto-cleanup ()
322 "Automatic cleanup of the recent list."
323 (when (timerp recentf-auto-cleanup-timer)
324 (cancel-timer recentf-auto-cleanup-timer))
325 (when recentf-mode
326 (setq recentf-auto-cleanup-timer
327 (cond
328 ((eq 'mode recentf-auto-cleanup)
329 (recentf-cleanup)
330 nil)
331 ((numberp recentf-auto-cleanup)
332 (run-with-idle-timer
333 recentf-auto-cleanup t 'recentf-cleanup))
334 ((stringp recentf-auto-cleanup)
335 (run-at-time
336 recentf-auto-cleanup nil 'recentf-cleanup))))))
337 \f
338 ;;; File functions
339 ;;
340 (defsubst recentf-push (filename)
341 "Push FILENAME into the recent list, if it isn't there yet.
342 If it is there yet, move it at the beginning of the list.
343 If `recentf-case-fold-search' is non-nil, ignore case when comparing
344 filenames."
345 (let ((m (recentf-string-member filename recentf-list)))
346 (and m (setq recentf-list (delq (car m) recentf-list)))
347 (push filename recentf-list)))
348
349 (defsubst recentf-expand-file-name (name)
350 "Convert filename NAME to absolute, and canonicalize it.
351 See also the function `expand-file-name'.
352 If defined, call the function `recentf-filename-handler' to post
353 process the canonical name."
354 (let* ((filename (expand-file-name name)))
355 (or (and recentf-filename-handler
356 (funcall recentf-filename-handler filename))
357 filename)))
358
359 (defun recentf-include-p (filename)
360 "Return non-nil if FILENAME should be included in the recent list.
361 That is, if it doesn't match any of the `recentf-exclude' checks."
362 (let ((case-fold-search recentf-case-fold-search)
363 (checks recentf-exclude)
364 (keepit t))
365 (while (and checks keepit)
366 (setq keepit (condition-case nil
367 (not (if (stringp (car checks))
368 ;; A regexp
369 (string-match (car checks) filename)
370 ;; A predicate
371 (funcall (car checks) filename)))
372 (error nil))
373 checks (cdr checks)))
374 keepit))
375
376 (defun recentf-keep-p (filename)
377 "Return non-nil if FILENAME should be kept in the recent list.
378 That is, if it matches any of the `recentf-keep' checks."
379 (let* ((case-fold-search recentf-case-fold-search)
380 (checks recentf-keep)
381 (keepit (null checks)))
382 (while (and checks (not keepit))
383 (setq keepit (condition-case nil
384 (if (stringp (car checks))
385 ;; A regexp
386 (string-match (car checks) filename)
387 ;; A predicate
388 (funcall (car checks) filename))
389 (error nil))
390 checks (cdr checks)))
391 keepit))
392
393 (defsubst recentf-add-file (filename)
394 "Add or move FILENAME at the beginning of the recent list.
395 Does nothing if the name satisfies any of the `recentf-exclude'
396 regexps or predicates."
397 (setq filename (recentf-expand-file-name filename))
398 (when (recentf-include-p filename)
399 (recentf-push filename)))
400
401 (defsubst recentf-remove-if-non-kept (filename)
402 "Remove FILENAME from the recent list, if file is not kept.
403 Return non-nil if FILENAME has been removed."
404 (unless (recentf-keep-p filename)
405 (let ((m (recentf-string-member
406 (recentf-expand-file-name filename) recentf-list)))
407 (and m (setq recentf-list (delq (car m) recentf-list))))))
408
409 (defsubst recentf-directory-compare (f1 f2)
410 "Compare absolute filenames F1 and F2.
411 First compare directories, then filenames sans directory.
412 Return non-nil if F1 is less than F2."
413 (let ((d1 (file-name-directory f1))
414 (d2 (file-name-directory f2)))
415 (if (recentf-string-equal d1 d2)
416 (recentf-string-lessp (file-name-nondirectory f1)
417 (file-name-nondirectory f2))
418 (recentf-string-lessp d1 d2))))
419 \f
420 ;;; Menu building
421 ;;
422 (defvar recentf-menu-items-for-commands
423 (list ["Cleanup list"
424 recentf-cleanup
425 :help "Remove all excluded and non-kept files from the recent list"
426 :active t]
427 ["Edit list..."
428 recentf-edit-list
429 :help "Edit the files that are kept in the recent list"
430 :active t]
431 ["Save list now"
432 recentf-save-list
433 :help "Save the list of recently opened files now"
434 :active t]
435 ["Options..."
436 (customize-group "recentf")
437 :help "Customize recently opened files menu and options"
438 :active t]
439 )
440 "List of menu items for recentf commands.")
441
442 (defvar recentf-menu-filter-commands nil
443 "This variable can be used by menu filters to setup their own command menu.
444 If non-nil it must contain a list of valid menu-items to be appended
445 to the recent file list part of the menu. Before calling a menu
446 filter function this variable is reset to nil.")
447
448 (defsubst recentf-elements (n)
449 "Return a list of the first N elements of the recent list."
450 (recentf-trunc-list recentf-list n))
451
452 (defsubst recentf-make-menu-element (menu-item menu-value)
453 "Create a new menu-element.
454 A menu element is a pair (MENU-ITEM . MENU-VALUE), where MENU-ITEM is
455 the menu item string displayed. MENU-VALUE is the file to be open
456 when the corresponding MENU-ITEM is selected. Or it is a
457 pair (SUB-MENU-TITLE . MENU-ELEMENTS) where SUB-MENU-TITLE is a
458 sub-menu title and MENU-ELEMENTS is the list of menu elements in the
459 sub-menu."
460 (cons menu-item menu-value))
461
462 (defsubst recentf-menu-element-item (e)
463 "Return the item part of the menu-element E."
464 (car e))
465
466 (defsubst recentf-menu-element-value (e)
467 "Return the value part of the menu-element E."
468 (cdr e))
469
470 (defsubst recentf-set-menu-element-item (e item)
471 "Change the item part of menu-element E to ITEM."
472 (setcar e item))
473
474 (defsubst recentf-set-menu-element-value (e value)
475 "Change the value part of menu-element E to VALUE."
476 (setcdr e value))
477
478 (defsubst recentf-sub-menu-element-p (e)
479 "Return non-nil if menu-element E defines a sub-menu."
480 (consp (recentf-menu-element-value e)))
481
482 (defsubst recentf-make-default-menu-element (file)
483 "Make a new default menu element with FILE.
484 This a menu element (FILE . FILE)."
485 (recentf-make-menu-element file file))
486
487 (defsubst recentf-menu-elements (n)
488 "Return a list of the first N default menu elements from the recent list.
489 See also `recentf-make-default-menu-element'."
490 (mapcar 'recentf-make-default-menu-element
491 (recentf-elements n)))
492
493 (defun recentf-apply-menu-filter (filter l)
494 "Apply function FILTER to the list of menu-elements L.
495 It takes care of sub-menu elements in L and recursively apply FILTER
496 to them. It is guaranteed that FILTER receives only a list of single
497 menu-elements (no sub-menu)."
498 (if (and l (functionp filter))
499 (let ((case-fold-search recentf-case-fold-search)
500 elts others)
501 ;; split L into two sub-listes, one of sub-menus elements and
502 ;; another of single menu elements.
503 (dolist (elt l)
504 (if (recentf-sub-menu-element-p elt)
505 (push elt elts)
506 (push elt others)))
507 ;; Apply FILTER to single elements.
508 (when others
509 (setq others (funcall filter (nreverse others))))
510 ;; Apply FILTER to sub-menu elements.
511 (setq l nil)
512 (dolist (elt elts)
513 (recentf-set-menu-element-value
514 elt (recentf-apply-menu-filter
515 filter (recentf-menu-element-value elt)))
516 (push elt l))
517 ;; Return the new filtered menu element list.
518 (nconc l others))
519 l))
520
521 (defun recentf-make-menu-items ()
522 "Make menu items from the recent list."
523 (setq recentf-menu-filter-commands nil)
524 (let ((file-items
525 (mapcar 'recentf-make-menu-item
526 (recentf-apply-menu-filter
527 recentf-menu-filter
528 (recentf-menu-elements recentf-max-menu-items)))))
529 (append (or file-items (list ["No files" t
530 :help "No recent file to open"
531 :active nil]))
532 (and (< recentf-max-menu-items (length recentf-list))
533 (list ["More..." recentf-open-more-files
534 :help "Open files that are not in the menu"
535 :active t]))
536 (and recentf-menu-filter-commands
537 (cons "---"
538 recentf-menu-filter-commands))
539 (and recentf-menu-append-commands-flag
540 (cons "---"
541 recentf-menu-items-for-commands)))))
542
543 (defsubst recentf-make-menu-item (elt)
544 "Make a menu item from menu element ELT."
545 (let ((item (recentf-menu-element-item elt))
546 (value (recentf-menu-element-value elt)))
547 (if (recentf-sub-menu-element-p elt)
548 (cons item (mapcar 'recentf-make-menu-item value))
549 (vector item (list recentf-menu-action value)
550 :help (concat "Open " value)
551 :active t))))
552
553 (defsubst recentf-menu-bar ()
554 "Return the keymap of the global menu bar."
555 (lookup-key global-map [menu-bar]))
556
557 (defun recentf-clear-data ()
558 "Clear data used to build the recentf menu.
559 This force a rebuild of the menu."
560 (easy-menu-remove-item (recentf-menu-bar)
561 recentf-menu-path recentf-menu-title)
562 (setq recentf-data-cache nil))
563 \f
564 ;;; Predefined menu filters
565 ;;
566 (defsubst recentf-sort-ascending (l)
567 "Sort the list of menu elements L in ascending order.
568 The MENU-ITEM part of each menu element is compared."
569 (sort (copy-sequence l)
570 #'(lambda (e1 e2)
571 (recentf-string-lessp
572 (recentf-menu-element-item e1)
573 (recentf-menu-element-item e2)))))
574
575 (defsubst recentf-sort-descending (l)
576 "Sort the list of menu elements L in descending order.
577 The MENU-ITEM part of each menu element is compared."
578 (sort (copy-sequence l)
579 #'(lambda (e1 e2)
580 (recentf-string-lessp
581 (recentf-menu-element-item e2)
582 (recentf-menu-element-item e1)))))
583
584 (defsubst recentf-sort-basenames-ascending (l)
585 "Sort the list of menu elements L in ascending order.
586 Only filenames sans directory are compared."
587 (sort (copy-sequence l)
588 #'(lambda (e1 e2)
589 (recentf-string-lessp
590 (file-name-nondirectory (recentf-menu-element-value e1))
591 (file-name-nondirectory (recentf-menu-element-value e2))))))
592
593 (defsubst recentf-sort-basenames-descending (l)
594 "Sort the list of menu elements L in descending order.
595 Only filenames sans directory are compared."
596 (sort (copy-sequence l)
597 #'(lambda (e1 e2)
598 (recentf-string-lessp
599 (file-name-nondirectory (recentf-menu-element-value e2))
600 (file-name-nondirectory (recentf-menu-element-value e1))))))
601
602 (defsubst recentf-sort-directories-ascending (l)
603 "Sort the list of menu elements L in ascending order.
604 Compares directories then filenames to order the list."
605 (sort (copy-sequence l)
606 #'(lambda (e1 e2)
607 (recentf-directory-compare
608 (recentf-menu-element-value e1)
609 (recentf-menu-element-value e2)))))
610
611 (defsubst recentf-sort-directories-descending (l)
612 "Sort the list of menu elements L in descending order.
613 Compares directories then filenames to order the list."
614 (sort (copy-sequence l)
615 #'(lambda (e1 e2)
616 (recentf-directory-compare
617 (recentf-menu-element-value e2)
618 (recentf-menu-element-value e1)))))
619
620 (defun recentf-show-basenames (l &optional no-dir)
621 "Filter the list of menu elements L to show filenames sans directory.
622 When a filename is duplicated, it is appended a sequence number if
623 optional argument NO-DIR is non-nil, or its directory otherwise."
624 (let (filtered-names filtered-list full name counters sufx)
625 (dolist (elt l (nreverse filtered-list))
626 (setq full (recentf-menu-element-value elt)
627 name (file-name-nondirectory full))
628 (if (not (member name filtered-names))
629 (push name filtered-names)
630 (if no-dir
631 (if (setq sufx (assoc name counters))
632 (setcdr sufx (1+ (cdr sufx)))
633 (setq sufx 1)
634 (push (cons name sufx) counters))
635 (setq sufx (file-name-directory full)))
636 (setq name (format "%s(%s)" name sufx)))
637 (push (recentf-make-menu-element name full) filtered-list))))
638
639 (defsubst recentf-show-basenames-ascending (l)
640 "Filter the list of menu elements L to show filenames sans directory.
641 Filenames are sorted in ascending order.
642 This filter combines the `recentf-sort-basenames-ascending' and
643 `recentf-show-basenames' filters."
644 (recentf-show-basenames (recentf-sort-basenames-ascending l)))
645
646 (defsubst recentf-show-basenames-descending (l)
647 "Filter the list of menu elements L to show filenames sans directory.
648 Filenames are sorted in descending order.
649 This filter combines the `recentf-sort-basenames-descending' and
650 `recentf-show-basenames' filters."
651 (recentf-show-basenames (recentf-sort-basenames-descending l)))
652
653 (defun recentf-relative-filter (l)
654 "Filter the list of menu-elements L to show relative filenames.
655 Filenames are relative to the `default-directory'."
656 (mapcar #'(lambda (menu-element)
657 (let* ((ful (recentf-menu-element-value menu-element))
658 (rel (file-relative-name ful default-directory)))
659 (if (string-match "^\\.\\." rel)
660 menu-element
661 (recentf-make-menu-element rel ful))))
662 l))
663 \f
664 ;;; Rule based menu filters
665 ;;
666 (defcustom recentf-arrange-rules
667 '(
668 ("Elisp files (%d)" ".\\.el$")
669 ("Java files (%d)" ".\\.java$")
670 ("C/C++ files (%d)" "c\\(pp\\)?$")
671 )
672 "*List of rules used by `recentf-arrange-by-rule' to build sub-menus.
673 A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
674 displayed title of the sub-menu where a '%d' `format' pattern is
675 replaced by the number of items in the sub-menu. MATCHER is a regexp
676 or a list of regexps. Items matching one of the regular expressions in
677 MATCHER are added to the corresponding sub-menu."
678 :group 'recentf-filters
679 :type '(repeat (cons string (repeat regexp)))
680 :set 'recentf-menu-customization-changed)
681
682 (defcustom recentf-arrange-by-rule-others "Other files (%d)"
683 "*Title of the `recentf-arrange-by-rule' sub-menu.
684 This is for the menu where items that don't match any
685 `recentf-arrange-rules' are displayed. If nil these items are
686 displayed in the main recent files menu. A '%d' `format' pattern in
687 the title is replaced by the number of items in the sub-menu."
688 :group 'recentf-filters
689 :type '(choice (const :tag "Main menu" nil)
690 (string :tag "Title"))
691 :set 'recentf-menu-customization-changed)
692
693 (defcustom recentf-arrange-by-rules-min-items 0
694 "*Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
695 If the number of items in a sub-menu is less than this value the
696 corresponding sub-menu items are displayed in the main recent files
697 menu or in the `recentf-arrange-by-rule-others' sub-menu if
698 defined."
699 :group 'recentf-filters
700 :type 'number
701 :set 'recentf-menu-customization-changed)
702
703 (defcustom recentf-arrange-by-rule-subfilter nil
704 "*Function called by a rule based filter to filter sub-menu elements.
705 nil means no filter. See also `recentf-menu-filter'.
706 You can't use another rule based filter here."
707 :group 'recentf-filters
708 :type '(choice (const nil) function)
709 :set (lambda (variable value)
710 (when (memq value '(recentf-arrange-by-rule
711 recentf-arrange-by-mode
712 recentf-arrange-by-dir))
713 (error "Recursive use of a rule based filter"))
714 (recentf-menu-customization-changed variable value)))
715
716 (defun recentf-match-rule-p (matcher filename)
717 "Return non-nil if the rule specified by MATCHER match FILENAME.
718 See `recentf-arrange-rules' for details on MATCHER."
719 (if (stringp matcher)
720 (string-match matcher filename)
721 (while (and (consp matcher)
722 (not (string-match (car matcher) filename)))
723 (setq matcher (cdr matcher)))
724 matcher))
725
726 (defun recentf-arrange-by-rule (l)
727 "Filter the list of menu-elements L.
728 Arrange them in sub-menus following rules in `recentf-arrange-rules'."
729 (if (not recentf-arrange-rules)
730 l
731 (let ((menus (mapcar #'(lambda (r) (list (car r)))
732 recentf-arrange-rules))
733 menu others min file rules elts count)
734 (dolist (elt l)
735 (setq file (recentf-menu-element-value elt)
736 rules recentf-arrange-rules
737 elts menus
738 menu nil)
739 (while (and (not menu) rules)
740 (when (recentf-match-rule-p (cdar rules) file)
741 (setq menu (car elts))
742 (recentf-set-menu-element-value
743 menu (cons elt (recentf-menu-element-value menu))))
744 (setq rules (cdr rules)
745 elts (cdr elts)))
746 (unless menu
747 (push elt others)))
748
749 (setq l nil
750 min (if (natnump recentf-arrange-by-rules-min-items)
751 recentf-arrange-by-rules-min-items 0))
752 (dolist (menu menus)
753 (when (setq elts (recentf-menu-element-value menu))
754 (setq count (length elts))
755 (if (< count min)
756 (setq others (nconc elts others))
757 (recentf-set-menu-element-item
758 menu (format (recentf-menu-element-item menu) count))
759 (recentf-set-menu-element-value
760 menu (recentf-apply-menu-filter
761 recentf-arrange-by-rule-subfilter (nreverse elts)))
762 (push menu l))))
763
764 (if (and (stringp recentf-arrange-by-rule-others) others)
765 (nreverse
766 (cons
767 (recentf-make-menu-element
768 (format recentf-arrange-by-rule-others (length others))
769 (recentf-apply-menu-filter
770 recentf-arrange-by-rule-subfilter (nreverse others)))
771 l))
772 (nconc
773 (nreverse l)
774 (recentf-apply-menu-filter
775 recentf-arrange-by-rule-subfilter (nreverse others)))))
776 ))
777 \f
778 ;;; Predefined rule based menu filters
779 ;;
780 (defun recentf-build-mode-rules ()
781 "Convert `auto-mode-alist' to menu filter rules.
782 Rules obey `recentf-arrange-rules' format."
783 (let ((case-fold-search recentf-case-fold-search)
784 regexp rule-name rule rules)
785 (dolist (mode auto-mode-alist)
786 (setq regexp (car mode)
787 mode (cdr mode))
788 (when (symbolp mode)
789 (setq rule-name (symbol-name mode))
790 (if (string-match "\\(.*\\)-mode$" rule-name)
791 (setq rule-name (match-string 1 rule-name)))
792 (setq rule-name (concat rule-name " (%d)")
793 rule (assoc rule-name rules))
794 (if rule
795 (setcdr rule (cons regexp (cdr rule)))
796 (push (list rule-name regexp) rules))))
797 ;; It is important to preserve auto-mode-alist order
798 ;; to ensure the right file <-> mode association
799 (nreverse rules)))
800
801 (defun recentf-arrange-by-mode (l)
802 "Split the list of menu-elements L into sub-menus by major mode."
803 (let ((recentf-arrange-rules (recentf-build-mode-rules))
804 (recentf-arrange-by-rule-others "others (%d)"))
805 (recentf-arrange-by-rule l)))
806
807 (defun recentf-build-dir-rules (l)
808 "Convert directories in menu-elements L to menu filter rules.
809 Rules obey `recentf-arrange-rules' format."
810 (let (dirs)
811 (mapcar #'(lambda (e)
812 (let ((dir (file-name-directory
813 (recentf-menu-element-value e))))
814 (or (recentf-string-member dir dirs)
815 (push dir dirs))))
816 l)
817 (mapcar #'(lambda (d)
818 (cons (concat d " (%d)")
819 (concat "\\`" d)))
820 (nreverse (sort dirs 'recentf-string-lessp)))))
821
822 (defun recentf-file-name-nondir (l)
823 "Filter the list of menu-elements L to show filenames sans directory.
824 This simplified version of `recentf-show-basenames' does not handle
825 duplicates. It is used by `recentf-arrange-by-dir' as its
826 `recentf-arrange-by-rule-subfilter'."
827 (mapcar #'(lambda (e)
828 (recentf-make-menu-element
829 (file-name-nondirectory (recentf-menu-element-value e))
830 (recentf-menu-element-value e)))
831 l))
832
833 (defun recentf-arrange-by-dir (l)
834 "Split the list of menu-elements L into sub-menus by directory."
835 (let ((recentf-arrange-rules (recentf-build-dir-rules l))
836 (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
837 recentf-arrange-by-rule-others)
838 (nreverse (recentf-arrange-by-rule l))))
839 \f
840 ;;; Ring of menu filters
841 ;;
842 (defvar recentf-filter-changer-state nil
843 "Used by `recentf-filter-changer' to hold its state.")
844
845 (defcustom recentf-filter-changer-alist
846 '(
847 (recentf-arrange-by-mode . "*Files by Mode*")
848 (recentf-arrange-by-dir . "*Files by Directory*")
849 (recentf-arrange-by-rule . "*Files by User Rule*")
850 )
851 "*List of filters managed by `recentf-filter-changer'.
852 Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is
853 the filter function, and LABEL is the menu item displayed to select
854 that filter."
855 :group 'recentf-filters
856 :type '(repeat (cons function string))
857 :set (lambda (variable value)
858 (setq recentf-filter-changer-state nil)
859 (recentf-menu-customization-changed variable value)))
860
861 (defun recentf-filter-changer-goto-next ()
862 "Go to the next filter available.
863 See `recentf-filter-changer'."
864 (setq recentf-filter-changer-state (cdr recentf-filter-changer-state))
865 (recentf-clear-data))
866
867 (defsubst recentf-filter-changer-get-current ()
868 "Get the current filter available.
869 See `recentf-filter-changer'."
870 (unless recentf-filter-changer-state
871 (setq recentf-filter-changer-state recentf-filter-changer-alist))
872 (car recentf-filter-changer-state))
873
874 (defsubst recentf-filter-changer-get-next ()
875 "Get the next filter available.
876 See `recentf-filter-changer'."
877 ;; At this point the current filter is the first element of
878 ;; `recentf-filter-changer-state'.
879 (car (or (cdr recentf-filter-changer-state)
880 ;; There is no next element in
881 ;; `recentf-filter-changer-state', so loop back to the
882 ;; first element of `recentf-filter-changer-alist'.
883 recentf-filter-changer-alist)))
884
885 (defun recentf-filter-changer (l)
886 "Manage a ring of menu filters.
887 `recentf-filter-changer-alist' defines the filters in the ring.
888 Filtering of L is delegated to the current filter in the ring. A
889 filter menu item is displayed allowing to dynamically activate the
890 next filter in the ring. If the filter ring is empty, L is left
891 unchanged."
892 (let ((filter (recentf-filter-changer-get-current)))
893 (when filter
894 (setq l (recentf-apply-menu-filter (car filter) l)
895 filter (recentf-filter-changer-get-next))
896 (when filter
897 (setq recentf-menu-filter-commands
898 (list (vector (cdr filter)
899 '(recentf-filter-changer-goto-next)
900 t)))))
901 l))
902 \f
903 ;;; Common dialog stuff
904 ;;
905 (defun recentf-cancel-dialog (&rest ignore)
906 "Cancel the current dialog.
907 Used internally by recentf dialogs.
908 IGNORE arguments."
909 (interactive)
910 (kill-buffer (current-buffer))
911 (message "Dialog canceled"))
912
913 (defvar recentf-dialog-mode-map
914 (let ((km (make-sparse-keymap)))
915 (define-key km "q" 'recentf-cancel-dialog)
916 (define-key km [down-mouse-1] 'widget-button-click)
917 (set-keymap-parent km widget-keymap)
918 km)
919 "Keymap used in recentf dialogs.")
920
921 (defun recentf-dialog-mode ()
922 "Major mode of recentf dialogs.
923
924 \\{recentf-dialog-mode-map}"
925 (interactive)
926 (setq major-mode 'recentf-dialog-mode)
927 (setq mode-name "recentf-dialog")
928 (use-local-map recentf-dialog-mode-map))
929 \f
930 ;;; Hooks
931 ;;
932 (defun recentf-track-opened-file ()
933 "Insert the name of the file just opened or written into the recent list."
934 (and buffer-file-name
935 (recentf-add-file buffer-file-name))
936 ;; Must return nil because it is run from `write-file-functions'.
937 nil)
938
939 (defun recentf-track-closed-file ()
940 "Update the recent list when a buffer is killed.
941 That is, remove a non kept file from the recent list."
942 (and buffer-file-name
943 (recentf-remove-if-non-kept buffer-file-name)))
944
945 (defun recentf-update-menu ()
946 "Update the recentf menu from the current recent list."
947 (let ((cache (cons default-directory recentf-list)))
948 ;; Does nothing, if nothing has changed.
949 (unless (equal recentf-data-cache cache)
950 (setq recentf-data-cache cache)
951 (condition-case err
952 (easy-menu-add-item
953 (recentf-menu-bar) recentf-menu-path
954 (easy-menu-create-menu recentf-menu-title
955 (recentf-make-menu-items))
956 recentf-menu-before)
957 (error
958 (message "recentf update menu failed: %s"
959 (error-message-string err)))))))
960
961 (defconst recentf-used-hooks
962 '(
963 (find-file-hook recentf-track-opened-file)
964 (write-file-functions recentf-track-opened-file)
965 (kill-buffer-hook recentf-track-closed-file)
966 (menu-bar-update-hook recentf-update-menu)
967 (kill-emacs-hook recentf-save-list)
968 )
969 "Hooks used by recentf.")
970
971 (defsubst recentf-enabled-p ()
972 "Return non-nil if recentf mode is currently enabled."
973 (memq 'recentf-update-menu menu-bar-update-hook))
974 \f
975 ;;; Commands
976 ;;
977 (defvar recentf-edit-selected-items nil
978 "List of files to be deleted from the recent list.
979 Used internally by `recentf-edit-list'.")
980
981 (defun recentf-edit-list-action (widget &rest ignore)
982 "Checkbox WIDGET action that toogles a file selection.
983 Used internally by `recentf-edit-list'.
984 IGNORE other arguments."
985 (let ((value (widget-get widget ':tag)))
986 ;; if value is already in the selected items
987 (if (memq value recentf-edit-selected-items)
988 ;; then remove it
989 (progn
990 (setq recentf-edit-selected-items
991 (delq value recentf-edit-selected-items))
992 (message "%s removed from selection" value))
993 ;; else add it
994 (push value recentf-edit-selected-items)
995 (message "%s added to selection" value))))
996
997 (defun recentf-edit-list ()
998 "Show a dialog buffer to edit the recent list.
999 That is to select files to be deleted from the recent list."
1000 (interactive)
1001 (with-current-buffer
1002 (get-buffer-create (format "*%s - Edit list*" recentf-menu-title))
1003 (switch-to-buffer (current-buffer))
1004 ;; Cleanup buffer
1005 (kill-all-local-variables)
1006 (let ((inhibit-read-only t)
1007 (ol (overlay-lists)))
1008 (erase-buffer)
1009 ;; Delete all the overlays.
1010 (mapc 'delete-overlay (car ol))
1011 (mapc 'delete-overlay (cdr ol)))
1012 (setq recentf-edit-selected-items nil)
1013 ;; Insert the dialog header
1014 (widget-insert
1015 "\
1016 Select the files to be deleted from the recent list.\n\n\
1017 Click on Ok to update the list. \
1018 Click on Cancel or type \"q\" to quit.\n")
1019 ;; Insert the list of files as checkboxes
1020 (dolist (item recentf-list)
1021 (widget-create
1022 'checkbox
1023 :value nil ; unselected checkbox
1024 :format "\n %[%v%] %t"
1025 :tag item
1026 :notify 'recentf-edit-list-action))
1027 (widget-insert "\n\n")
1028 ;; Insert the Ok button
1029 (widget-create
1030 'push-button
1031 :notify (lambda (&rest ignore)
1032 (if recentf-edit-selected-items
1033 (let ((i 0))
1034 (kill-buffer (current-buffer))
1035 (dolist (e recentf-edit-selected-items)
1036 (setq recentf-list (delq e recentf-list)
1037 i (1+ i)))
1038 (message "%S file(s) removed from the list" i)
1039 (recentf-clear-data))
1040 (message "No file selected")))
1041 "Ok")
1042 (widget-insert " ")
1043 ;; Insert the Cancel button
1044 (widget-create
1045 'push-button
1046 :notify 'recentf-cancel-dialog
1047 "Cancel")
1048 (recentf-dialog-mode)
1049 (widget-setup)
1050 (goto-char (point-min))))
1051
1052 (defun recentf-open-files-action (widget &rest ignore)
1053 "Button WIDGET action that open a file.
1054 Used internally by `recentf-open-files'.
1055 IGNORE other arguments."
1056 (kill-buffer (current-buffer))
1057 (funcall recentf-menu-action (widget-value widget)))
1058
1059 (defvar recentf-open-files-item-shift ""
1060 "Amount of space to shift right sub-menu items.
1061 Used internally by `recentf-open-files'.")
1062
1063 (defun recentf-open-files-item (menu-element)
1064 "Insert an item widget for MENU-ELEMENT in the current dialog buffer.
1065 Used internally by `recentf-open-files'."
1066 (let ((item (car menu-element))
1067 (file (cdr menu-element)))
1068 (if (consp file) ; This is a sub-menu
1069 (let* ((shift recentf-open-files-item-shift)
1070 (recentf-open-files-item-shift (concat shift " ")))
1071 (widget-create
1072 'item
1073 :tag item
1074 :sample-face 'bold
1075 :format (concat shift "%{%t%}:\n"))
1076 (mapc 'recentf-open-files-item file)
1077 (widget-insert "\n"))
1078 (widget-create
1079 'push-button
1080 :button-face 'default
1081 :tag item
1082 :help-echo (concat "Open " file)
1083 :format (concat recentf-open-files-item-shift "%[%t%]")
1084 :notify 'recentf-open-files-action
1085 file)
1086 (widget-insert "\n"))))
1087
1088 (defun recentf-open-files (&optional files buffer-name)
1089 "Show a dialog buffer to open a recent file.
1090 If optional argument FILES is non-nil, it specifies the list of
1091 recently-opened files to choose from. It is the whole recent list
1092 otherwise.
1093 If optional argument BUFFER-NAME is non-nil, it specifies which buffer
1094 name to use for the interaction. It is \"*`recentf-menu-title'*\" by
1095 default."
1096 (interactive)
1097 (unless files
1098 (setq files recentf-list))
1099 (unless buffer-name
1100 (setq buffer-name (format "*%s*" recentf-menu-title)))
1101 (with-current-buffer (get-buffer-create buffer-name)
1102 (switch-to-buffer (current-buffer))
1103 ;; Cleanup buffer
1104 (kill-all-local-variables)
1105 (let ((inhibit-read-only t)
1106 (ol (overlay-lists)))
1107 (erase-buffer)
1108 ;; Delete all the overlays.
1109 (mapc 'delete-overlay (car ol))
1110 (mapc 'delete-overlay (cdr ol)))
1111 ;; Insert the dialog header
1112 (widget-insert "Click on a file to open it. ")
1113 (widget-insert "Click on Cancel or type \"q\" to quit.\n\n" )
1114 ;; Insert the list of files as buttons
1115 (let ((recentf-open-files-item-shift ""))
1116 (mapc 'recentf-open-files-item
1117 (recentf-apply-menu-filter
1118 recentf-menu-filter
1119 (mapcar 'recentf-make-default-menu-element files))))
1120 (widget-insert "\n")
1121 ;; Insert the Cancel button
1122 (widget-create
1123 'push-button
1124 :notify 'recentf-cancel-dialog
1125 "Cancel")
1126 (recentf-dialog-mode)
1127 (widget-setup)
1128 (goto-char (point-min))))
1129
1130 (defun recentf-open-more-files ()
1131 "Show a dialog buffer to open a recent file that is not in the menu."
1132 (interactive)
1133 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
1134 (format "*%s - More*" recentf-menu-title)))
1135
1136 (defconst recentf-save-file-header
1137 ";;; Automatically generated by `recentf' on %s.\n"
1138 "Header to be written into the `recentf-save-file'.")
1139
1140 (defun recentf-save-list ()
1141 "Save the recent list.
1142 Write data into the file specified by `recentf-save-file'."
1143 (interactive)
1144 (condition-case error
1145 (with-temp-buffer
1146 (erase-buffer)
1147 (insert (format recentf-save-file-header (current-time-string)))
1148 (recentf-dump-variable 'recentf-list recentf-max-saved-items)
1149 (recentf-dump-variable 'recentf-filter-changer-state)
1150 (write-file (expand-file-name recentf-save-file))
1151 nil)
1152 (error
1153 (warn "recentf mode: %s" (error-message-string error)))))
1154
1155 (defun recentf-load-list ()
1156 "Load a previously saved recent list.
1157 Read data from the file specified by `recentf-save-file'.
1158 When `recentf-initialize-file-name-history' is non-nil, initialize an
1159 empty `file-name-history' with the recent list."
1160 (interactive)
1161 (let ((file (expand-file-name recentf-save-file)))
1162 (when (file-readable-p file)
1163 (load-file file)
1164 (and recentf-initialize-file-name-history
1165 (not file-name-history)
1166 (setq file-name-history (mapcar 'abbreviate-file-name
1167 recentf-list))))))
1168
1169 (defun recentf-cleanup ()
1170 "Remove all non-kept and excluded files from the recent list."
1171 (interactive)
1172 (message "Cleaning up the recentf list...")
1173 (let ((n 0) newlist)
1174 (dolist (f recentf-list)
1175 (if (and (recentf-include-p f)
1176 (recentf-keep-p f))
1177 (push f newlist)
1178 (setq n (1+ n))
1179 (message "File %s removed from the recentf list" f)))
1180 (message "Cleaning up the recentf list...done (%d removed)" n)
1181 (setq recentf-list (nreverse newlist))))
1182
1183 ;;;###autoload
1184 (define-minor-mode recentf-mode
1185 "Toggle recentf mode.
1186 With prefix argument ARG, turn on if positive, otherwise off.
1187 Returns non-nil if the new state is enabled.
1188
1189 When recentf mode is enabled, it maintains a menu for visiting files
1190 that were operated on recently."
1191 :global t
1192 :group 'recentf
1193 (unless (and recentf-mode (recentf-enabled-p))
1194 (if recentf-mode
1195 (recentf-load-list)
1196 (recentf-save-list))
1197 (recentf-auto-cleanup)
1198 (recentf-clear-data)
1199 (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
1200 (dolist (hook recentf-used-hooks)
1201 (apply hook-setup hook)))
1202 (run-hooks 'recentf-mode-hook)
1203 (when (interactive-p)
1204 (message "Recentf mode %sabled" (if recentf-mode "en" "dis"))))
1205 recentf-mode)
1206
1207 (provide 'recentf)
1208
1209 (run-hooks 'recentf-load-hook)
1210
1211 ;;; arch-tag: 78f1eec9-0d16-4d19-a4eb-2e4529edb62a
1212 ;;; recentf.el ends here