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