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