* cedet/semantic/db-file.el (semanticdb-default-save-directory):
[bpt/emacs.git] / lisp / recentf.el
CommitLineData
e8af40ee 1;;; recentf.el --- setup a menu of recently opened files
bc66a9a9 2
0d30b337 3;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
ae940284 4;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
bc66a9a9
DL
5
6;; Author: David Ponce <david@dponce.com>
7;; Created: July 19 1999
be9e7056
JB
8;; Keywords: files
9
bc66a9a9
DL
10;; This file is part of GNU Emacs.
11
eb3fa2cf
GM
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
bc66a9a9
DL
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
bc66a9a9
DL
24
25;;; Commentary:
26
27;; This package maintains a menu for visiting files that were operated
7b2ab969 28;; on recently. When enabled a new "Open Recent" sub menu is
fc8b3711 29;; displayed in the "File" menu. The recent files list is
7b2ab969
DP
30;; automatically saved across Emacs sessions. You can customize the
31;; number of recent files displayed, the location of the menu and
32;; others options (see the source code for details).
be9e7056 33
fc8b3711
SE
34;; To enable this package, add the following to your .emacs:
35;; (recentf-mode 1)
36
be9e7056 37;;; History:
bc66a9a9 38;;
bc66a9a9
DL
39
40;;; Code:
bc66a9a9 41(require 'easymenu)
7b2ab969 42(require 'tree-widget)
be9e7056 43(require 'timer)
bc66a9a9 44
be9e7056
JB
45;;; Internal data
46;;
bc66a9a9
DL
47(defvar recentf-list nil
48 "List of recently opened files.")
49
52d2876f
DP
50(defsubst recentf-enabled-p ()
51 "Return non-nil if recentf mode is currently enabled."
52 (memq 'recentf-save-list kill-emacs-hook))
be9e7056
JB
53\f
54;;; Customization
55;;
bc66a9a9
DL
56(defgroup recentf nil
57 "Maintain a menu of recently opened files."
58 :version "21.1"
59 :group 'files)
60
61(defgroup recentf-filters nil
62 "Group to customize recentf menu filters.
63You should define the options of your own filters in this group."
64 :group 'recentf)
65
66(defcustom recentf-max-saved-items 20
2aa7c4d5 67 "Maximum number of items of the recent list that will be saved.
8154a06e 68A nil value means to save the whole list.
be9e7056 69See the command `recentf-save-list'."
bc66a9a9
DL
70 :group 'recentf
71 :type 'integer)
72
1bfda128 73(defcustom recentf-save-file (convert-standard-filename "~/.recentf")
2aa7c4d5 74 "File to save the recent list into."
bc66a9a9 75 :group 'recentf
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)
eafc2b27
DP
415 (setq keepit (condition-case nil
416 (not (if (stringp (car checks))
417 ;; A regexp
418 (string-match (car checks) filename)
419 ;; A predicate
420 (funcall (car checks) filename)))
421 (error nil))
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
52d2876f
DP
593(defun recentf-make-menu-items (&optional menu)
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;;
bc66a9a9 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
1095(defun recentf-edit-list-select (widget &rest ignore)
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
1105(defun recentf-edit-list-validate (&rest ignore)
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;;
bc66a9a9 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
bc66a9a9 1339 "Toggle recentf mode.
a30ccae6
MB
1340With prefix argument ARG, turn on if positive, otherwise off.
1341Returns non-nil if the new state is enabled.
bc66a9a9 1342
be9e7056 1343When recentf mode is enabled, it maintains a menu for visiting files
851370b0 1344that were operated on recently."
a30ccae6
MB
1345 :global t
1346 :group 'recentf
4e8cb311 1347 :keymap recentf-mode-map
be9e7056
JB
1348 (unless (and recentf-mode (recentf-enabled-p))
1349 (if recentf-mode
52d2876f
DP
1350 (progn
1351 (recentf-load-list)
1352 (recentf-show-menu))
1353 (recentf-hide-menu)
be9e7056
JB
1354 (recentf-save-list))
1355 (recentf-auto-cleanup)
be9e7056
JB
1356 (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
1357 (dolist (hook recentf-used-hooks)
1358 (apply hook-setup hook)))
1359 (run-hooks 'recentf-mode-hook)
32226619 1360 (when (called-interactively-p 'interactive)
be9e7056
JB
1361 (message "Recentf mode %sabled" (if recentf-mode "en" "dis"))))
1362 recentf-mode)
bc66a9a9
DL
1363
1364(provide 'recentf)
1365
1366(run-hooks 'recentf-load-hook)
8dde0e95 1367\f
7b2ab969 1368;; arch-tag: 78f1eec9-0d16-4d19-a4eb-2e4529edb62a
0a1280b2 1369;;; recentf.el ends here