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