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