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