(Fwrite_region): Use `visiting' rather than `visit'
[emacs.git] / lisp / recentf.el
blobc2c66961608802e3e6ba3edad7358b8599d8345c
1 ;; recentf.el --- setup a menu of recently opened files
3 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Created: July 19 1999
7 ;; Keywords: customization
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; This package maintains a menu for visiting files that were operated
29 ;; on recently. When enabled a new "Open Recent" submenu is displayed
30 ;; in the "Files" menu. The recent files list is automatically saved
31 ;; across Emacs sessions. You can customize the number of recent
32 ;; files displayed, the location of the menu and others options (see
33 ;; the source code for details). To install and use, put the file on
34 ;; your Emacs-Lisp load path and add the following into your ~/.emacs
35 ;; startup file:
37 ;; (require 'recentf)
38 ;; (recentf-mode 1)
40 ;;; Code:
42 (require 'easymenu)
43 (require 'wid-edit)
45 (defconst recentf-save-file-header
46 ";;; Automatically generated by `recentf' on %s.\n"
47 "Header to be written into the `recentf-save-file'.")
49 (defvar recentf-list nil
50 "List of recently opened files.")
52 (defvar recentf-update-menu-p t
53 "Non-nil if the recentf menu must be updated.")
55 (defvar recentf-initialized-p nil
56 "Non-nil if recentf already initialized.")
58 ;; IMPORTANT: This function must be defined before the following defcustoms
59 ;; because it is used in their :set clause. To avoid byte-compiler warnings
60 ;; the `symbol-value' function is used to access the `recentf-menu-path'
61 ;; and `recentf-menu-title' values.
62 (defun recentf-menu-customization-changed (sym val)
63 "Function called when menu customization has changed.
64 It removes the recentf menu and forces its complete redrawing."
65 (when recentf-initialized-p
66 (easy-menu-remove-item nil
67 (symbol-value 'recentf-menu-path)
68 (symbol-value 'recentf-menu-title))
69 (setq recentf-update-menu-p t))
70 (custom-set-default sym val))
72 (defgroup recentf nil
73 "Maintain a menu of recently opened files."
74 :version "21.1"
75 :group 'files)
77 (defgroup recentf-filters nil
78 "Group to customize recentf menu filters.
79 You should define the options of your own filters in this group."
80 :group 'recentf)
82 (defcustom recentf-max-saved-items 20
83 "*Maximum number of items saved to `recentf-save-file'."
84 :group 'recentf
85 :type 'integer)
87 (defcustom recentf-save-file (expand-file-name "~/.recentf")
88 "*File to save `recentf-list' into."
89 :group 'recentf
90 :type 'file)
92 (defcustom recentf-exclude nil
93 "*List of regexps for filenames excluded from `recentf-list'."
94 :group 'recentf
95 :type '(repeat regexp))
97 (defcustom recentf-menu-title "Open Recent"
98 "*Name of the recentf menu."
99 :group 'recentf
100 :type 'string
101 :set 'recentf-menu-customization-changed)
103 (defcustom recentf-menu-path '("files")
104 "*Path where to add the recentf menu.
105 If nil add it at top level (see also `easy-menu-change')."
106 :group 'recentf
107 :type '(choice (const :tag "Top Level" nil)
108 (sexp :tag "Menu Path"))
109 :set 'recentf-menu-customization-changed)
111 (defcustom recentf-menu-before "open-file"
112 "*Name of the menu before which the recentf menu will be added.
113 If nil add it at end of menu (see also `easy-menu-change')."
114 :group 'recentf
115 :type '(choice (string :tag "Name")
116 (const :tag "Last" nil))
117 :set 'recentf-menu-customization-changed)
119 (defcustom recentf-menu-action 'recentf-find-file
120 "*Function to invoke with a filename item of the recentf menu.
121 The default action `recentf-find-file' calls `find-file' to edit an
122 existing file. If the file does not exist or is not readable, it is
123 not edited and its name is removed from `recentf-list'. You can use
124 `find-file' instead to open non-existing files and keep them in the
125 list of recently opened files."
126 :group 'recentf
127 :type 'function
128 :set 'recentf-menu-customization-changed)
130 (defcustom recentf-max-menu-items 10
131 "*Maximum number of items in the recentf menu."
132 :group 'recentf
133 :type 'integer
134 :set 'recentf-menu-customization-changed)
136 (defcustom recentf-menu-filter nil
137 "*Function used to filter files displayed in the recentf menu.
138 Nil means no filter. The following functions are predefined:
140 - - `recentf-sort-ascending' to sort menu items in ascending order.
141 - - `recentf-sort-descending' to sort menu items in descending order.
142 - - `recentf-sort-basenames-ascending' to sort file names in descending order.
143 - - `recentf-sort-basenames-descending' to sort file names in descending order.
144 - - `recentf-sort-directories-ascending' to sort directories in ascending order.
145 - - `recentf-sort-directories-descending' to sort directories in descending order.
146 - - `recentf-show-basenames' to show file names (no directories) in menu items.
147 - - `recentf-show-basenames-ascending' to show file names in ascending order.
148 - - `recentf-show-basenames-descending' to show file names in descending order.
149 - - `recentf-relative-filter' to show file names relative to `default-directory'.
150 - - `recentf-arrange-by-rule' to show sub-menus following user defined rules.
151 - - `recentf-arrange-by-mode' to show a sub-menu for each major mode.
152 - - `recentf-arrange-by-dir' to show a sub-menu for each directory.
153 - - `recentf-filter-changer' to manage a ring of filters.
155 The filter function is called with one argument, the list of menu elements
156 used to build the menu and must return a new list of menu elements (see
157 `recentf-make-menu-element' for menu element form)."
158 :group 'recentf
159 :type 'function
160 :set 'recentf-menu-customization-changed)
162 (defcustom recentf-menu-append-commands-p t
163 "*If not-nil command items are appended to the menu."
164 :group 'recentf
165 :type 'boolean
166 :set 'recentf-menu-customization-changed)
168 (defcustom recentf-keep-non-readable-files-p nil
169 "*If nil (default), non-readable files are not kept in `recentf-list'."
170 :group 'recentf
171 :type 'boolean
172 :require 'recentf
173 :initialize 'custom-initialize-default
174 :set (lambda (sym val)
175 (if val
176 (remove-hook 'kill-buffer-hook 'recentf-remove-file-hook)
177 (add-hook 'kill-buffer-hook 'recentf-remove-file-hook))
178 (custom-set-default sym val)))
180 (defcustom recentf-load-hook nil
181 "*Normal hook run at end of loading the `recentf' package."
182 :group 'recentf
183 :type 'hook)
185 ;;;;
186 ;;;; Common functions
187 ;;;;
188 (defconst recentf-case-fold-search
189 (memq system-type '(vax-vms windows-nt))
190 "Non-nil if recentf searches and matches should ignore case.")
192 (defun recentf-include-p (filename)
193 "Return t if FILENAME match none of the `recentf-exclude' regexps."
194 (let ((case-fold-search recentf-case-fold-search)
195 (rl recentf-exclude))
196 (while (and rl (not (string-match (car rl) filename)))
197 (setq rl (cdr rl)))
198 (null rl)))
200 (defun recentf-add-file (filename)
201 "Add or move FILENAME at the beginning of `recentf-list'.
202 Does nothing if FILENAME matches one of the `recentf-exclude' regexps."
203 (let ((filename (expand-file-name filename)))
204 (when (recentf-include-p filename)
205 (setq recentf-list (cons filename (delete filename recentf-list)))
206 (setq recentf-update-menu-p t))))
208 (defun recentf-remove-if-non-readable (filename)
209 "Remove FILENAME from `recentf-list' if not readable."
210 (unless (file-readable-p filename)
211 (setq recentf-list (delete filename recentf-list))
212 (setq recentf-update-menu-p t)))
214 (defun recentf-find-file (filename)
215 "Edit file FILENAME using `find-file'.
216 If FILENAME is not readable it is removed from `recentf-list'."
217 (if (file-readable-p filename)
218 (find-file filename)
219 (progn
220 (message "File `%s' not found." filename)
221 (setq recentf-list (delete filename recentf-list))
222 (setq recentf-update-menu-p t))))
224 (defun recentf-trunc-list (l n)
225 "Return a list of the first N elements of L."
226 (let ((lh nil))
227 (while (and l (> n 0))
228 (setq lh (cons (car l) lh))
229 (setq n (1- n))
230 (setq l (cdr l)))
231 (nreverse lh)))
233 (defun recentf-elements (n)
234 "Return a list of the first N elements of `recentf-list'."
235 (recentf-trunc-list recentf-list n))
237 (defun recentf-make-menu-element (menu-item menu-value)
238 "Create a new menu-element.
240 A menu element is a pair (MENU-ITEM . MENU-VALUE) where:
242 - - MENU-ITEM is the menu item string displayed.
243 - - MENU-VALUE is the path used to open the file when the
244 corresponding MENU-ITEM is selected. Or it is
245 a pair (SUB-MENU-TITLE . MENU-ELEMENTS) where
246 SUB-MENU-TITLE is a sub-menu title and
247 MENU-ELEMENTS is the list of menu elements in
248 the sub-menu."
249 (cons menu-item menu-value))
251 (defun recentf-menu-element-item (e)
252 "Return the item part of the menu-element E."
253 (car e))
255 (defun recentf-menu-element-value (e)
256 "Return the value part of the menu-element E."
257 (cdr e))
259 (defun recentf-set-menu-element-item (e item)
260 "Change the item part of menu-element E to ITEM."
261 (setcar e item))
263 (defun recentf-set-menu-element-value (e value)
264 "Change the value part of menu-element E to VALUE."
265 (setcdr e value))
267 (defun recentf-sub-menu-element-p (e)
268 "Return non-nil if menu-element E defines a sub-menu."
269 (consp (recentf-menu-element-value e)))
271 (defun recentf-make-default-menu-element (file-path)
272 "Make a new default menu element (MENU-ITEM . MENU-VALUE).
273 Do so for the given recent file path FILE-PATH. MENU-ITEM and
274 MENU-VALUE are set to FILE-PATH. See also
275 `recentf-make-menu-element'."
276 (recentf-make-menu-element file-path file-path))
278 (defun recentf-menu-elements (n)
279 "Return a list of the first N default menu elements from `recentf-list'.
280 See also `recentf-make-default-menu-element'."
281 (mapcar 'recentf-make-default-menu-element
282 (recentf-elements n)))
284 (defun recentf-apply-menu-filter (filter l)
285 "Apply function FILTER to the list of menu-elements L.
286 It takes care of sub-menu elements in L and recursively apply FILTER
287 to them. It is guaranteed that FILTER receives only a list of single
288 menu-elements (no sub-menu)."
289 (if (and (functionp filter) l)
290 (let ((case-fold-search recentf-case-fold-search)
291 menu-element sub-menu-elements single-elements)
292 ;; split L in two sub-listes:
293 ;; one of sub-menus elements and
294 ;; one of single menu elements
295 (while l
296 (setq menu-element (car l))
297 (if (recentf-sub-menu-element-p menu-element)
298 (setq sub-menu-elements
299 (cons menu-element sub-menu-elements))
300 (setq single-elements
301 (cons menu-element single-elements)))
302 (setq l (cdr l)))
303 ;; apply FILTER to the list of single menu elements
304 (if single-elements
305 (setq single-elements (funcall filter
306 (nreverse single-elements))))
307 ;; apply FILTER to sub-menu menu element list
308 (setq l sub-menu-elements)
309 (setq sub-menu-elements nil)
310 (while l
311 (setq menu-element (car l))
312 (recentf-set-menu-element-value
313 menu-element
314 (recentf-apply-menu-filter
315 filter
316 (recentf-menu-element-value menu-element)))
317 (setq sub-menu-elements (cons menu-element sub-menu-elements))
318 (setq l (cdr l)))
319 ;; build and return the new filtered menu element list
320 (nconc sub-menu-elements single-elements))
323 (defvar recentf-menu-items-for-commands
324 (list ["Cleanup list" recentf-cleanup t]
325 ["Edit list..." recentf-edit-list t]
326 ["Save list now" recentf-save-list t]
327 (vector "Recentf Options..." '(customize-group "recentf") t))
328 "List of menu items for recentf commands.")
330 (defvar recentf-menu-filter-commands nil
331 "This variable can be used by menu filters to setup their own command menu.
333 If non-nil it must contain a list of valid menu-items to be appended
334 to the recent file list part of the menu. Before calling a menu
335 filter function this variable is reset to nil.")
337 (defun recentf-make-menu-items ()
338 "Make menu items from `recentf-list'."
339 (setq recentf-menu-filter-commands nil)
340 (let ((file-items
341 (mapcar 'recentf-make-menu-item
342 (recentf-apply-menu-filter
343 recentf-menu-filter
344 (recentf-menu-elements recentf-max-menu-items)))))
345 (append (or file-items (list ["No files" t nil]))
346 (and (< recentf-max-menu-items (length recentf-list))
347 (list ["More..." recentf-open-more-files t]))
348 (and recentf-menu-filter-commands
349 (cons "---"
350 recentf-menu-filter-commands))
351 (and recentf-menu-append-commands-p
352 (cons "---"
353 recentf-menu-items-for-commands)))))
355 (defun recentf-make-menu-item (menu-element)
356 "Make a menu item from MENU-ELEMENT (see `recentf-make-menu-element')."
357 (let ((menu-item (recentf-menu-element-item menu-element))
358 (menu-value (recentf-menu-element-value menu-element)))
359 (if (recentf-sub-menu-element-p menu-element)
360 (cons menu-item (mapcar 'recentf-make-menu-item menu-value))
361 (vector menu-item
362 (list recentf-menu-action menu-value)
363 t))))
365 ;;;;
366 ;;;; Predefined menu filter functions
367 ;;;;
369 (defun recentf-sort-ascending (l)
370 "Sort the list of menu elements L in ascending order.
371 The MENU-ITEM part of each menu element is compared."
372 (sort (copy-sequence l)
373 (function
374 (lambda (e1 e2)
375 (string-lessp (recentf-menu-element-item e1)
376 (recentf-menu-element-item e2))))))
378 (defun recentf-sort-descending (l)
379 "Sort the list of menu elements L in descending order.
380 The MENU-ITEM part of each menu element is compared."
381 (sort (copy-sequence l)
382 (function
383 (lambda (e1 e2)
384 (string-lessp (recentf-menu-element-item e2)
385 (recentf-menu-element-item e1))))))
387 (defun recentf-sort-basenames-ascending (l)
388 "Sort the list of menu elements L in ascending order.
389 Only file names (without directories) are compared."
390 (sort (copy-sequence l)
391 (function
392 (lambda (e1 e2)
393 (string-lessp
394 (file-name-nondirectory (recentf-menu-element-value e1))
395 (file-name-nondirectory (recentf-menu-element-value e2)))))))
397 (defun recentf-sort-basenames-descending (l)
398 "Sort the list of menu elements L in descending order.
399 Only file names (without directories) are compared."
400 (sort (copy-sequence l)
401 (function
402 (lambda (e1 e2)
403 (string-lessp
404 (file-name-nondirectory (recentf-menu-element-value e2))
405 (file-name-nondirectory (recentf-menu-element-value e1)))))))
407 (defun recentf-directory-compare (p1 p2)
408 "Compare directories then filenames in paths P1 and P2.
409 Return non-nil if P1 is less than P2."
410 (let ((d1 (file-name-directory p1))
411 (f1 (file-name-nondirectory p1))
412 (d2 (file-name-directory p2))
413 (f2 (file-name-nondirectory p2)))
414 (if (string= d1 d2)
415 (string-lessp f1 f2)
416 (string-lessp d1 d2))))
418 (defun recentf-sort-directories-ascending (l)
419 "Sort the list of menu elements L in ascending order.
420 Compares directories then filenames to order the list."
421 (sort (copy-sequence l)
422 (function
423 (lambda (e1 e2)
424 (recentf-directory-compare (recentf-menu-element-value e1)
425 (recentf-menu-element-value e2))))))
427 (defun recentf-sort-directories-descending (l)
428 "Sort the list of menu elements L in descending order.
429 Compares directories then filenames to order the list."
430 (sort (copy-sequence l)
431 (function
432 (lambda (e1 e2)
433 (recentf-directory-compare (recentf-menu-element-value e2)
434 (recentf-menu-element-value e1))))))
436 (defun recentf-show-basenames (l)
437 "Filter the list of menu elements L to show only file names (no directories)
438 in the menu. When file names are duplicated their directory component is added."
439 (let ((names (mapcar (function
440 (lambda (item)
441 (file-name-nondirectory
442 (recentf-menu-element-value item))))
444 (dirs (mapcar (function
445 (lambda (item)
446 (file-name-directory
447 (recentf-menu-element-value item))))
449 (pathes (mapcar 'recentf-menu-element-value l))
450 (pos -1)
451 item filtered-items filtered-list)
452 (while names
453 (setq item (car names))
454 (setq names (cdr names))
455 (setq pos (1+ pos))
456 (setq filtered-list
457 (cons (recentf-make-menu-element
458 (if (or (member item names) (member item filtered-items))
459 (concat item " (" (nth pos dirs) ")")
460 item)
461 (nth pos pathes))
462 filtered-list))
463 (setq filtered-items (cons item filtered-items)))
464 (nreverse filtered-list)))
466 (defun recentf-show-basenames-ascending (l)
467 "Filter the list of menu elements L.
468 Show only file names in the menu, sorted in ascending order. This
469 filter combines the `recentf-sort-basenames-ascending' and
470 `recentf-show-basenames' filters."
471 (recentf-show-basenames (recentf-sort-basenames-ascending l)))
473 (defun recentf-show-basenames-descending (l)
474 "Filter the list of menu elements L.
475 Show only file names in the menu, sorted in descending order. This
476 filter combines the `recentf-sort-basenames-descending' and
477 `recentf-show-basenames' filters."
478 (recentf-show-basenames (recentf-sort-basenames-descending l)))
480 (defun recentf-relative-filter (l)
481 "Filter the list of `recentf-menu-elements' L.
482 Show filenames relative to `default-directory'."
483 (setq recentf-update-menu-p t) ; force menu update
484 (mapcar (function
485 (lambda (menu-element)
486 (let* ((ful-path (recentf-menu-element-value menu-element))
487 (rel-path (file-relative-name ful-path)))
488 (if (string-match "^\\.\\." rel-path)
489 menu-element
490 (recentf-make-menu-element rel-path ful-path)))))
493 (defcustom recentf-arrange-rules
495 ("Elisp files (%d)" ".\\.el$")
496 ("Java files (%d)" ".\\.java$")
497 ("C/C++ files (%d)" "c\\(pp\\)?$")
499 "*List of rules used by `recentf-arrange-by-rule' to build sub-menus.
500 A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
501 displayed title of the sub-menu where a '%d' `format' pattern is
502 replaced by the number of items in the sub-menu. MATCHER is a regexp
503 or a list of regexps. Items matching one of the regular expressions in
504 MATCHER are added to the corresponding sub-menu."
505 :group 'recentf-filters
506 :type '(repeat (cons string (repeat regexp)))
507 :set 'recentf-menu-customization-changed)
509 (defcustom recentf-arrange-by-rule-others "Other files (%d)"
510 "*Title of the `recentf-arrange-by-rule' sub-menu.
511 This is for the menu where items that don't match any
512 `recentf-arrange-rules' are displayed. If nil these items are
513 displayed in the main recent files menu. A '%d' `format' pattern in
514 the title is replaced by the number of items in the sub-menu."
515 :group 'recentf-filters
516 :type '(choice (const :tag "Main menu" nil)
517 (string :tag "Title"))
518 :set 'recentf-menu-customization-changed)
520 (defcustom recentf-arrange-by-rules-min-items 0
521 "*Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
522 If the number of items in a sub-menu is less than this value the
523 corresponding sub-menu items are displayed in the main recent files
524 menu or in the `recentf-arrange-by-rule-others' sub-menu if
525 defined."
526 :group 'recentf-filters
527 :type 'number
528 :set 'recentf-menu-customization-changed)
530 (defcustom recentf-arrange-by-rule-subfilter nil
531 "*Function used by `recentf-arrange-by-rule' to filter sub-menu elements.
532 Nil means no filter. See also `recentf-menu-filter'. You can't use
533 `recentf-arrange-by-rule' itself here!"
534 :group 'recentf-filters
535 :type 'function
536 :set (lambda (sym val)
537 (if (eq val 'recentf-arrange-by-rule)
538 (error "Can't use `recentf-arrange-by-rule' itself here!")
539 (recentf-menu-customization-changed sym val))))
541 (defun recentf-match-rule-p (matcher file-path)
542 "Return non-nil if FILE-PATH match the rule specified by MATCHER.
543 See `recentf-arrange-rules' for details on MATCHER."
544 (if (stringp matcher)
545 (string-match matcher file-path)
546 (while (and (consp matcher)
547 (not (string-match (car matcher) file-path)))
548 (setq matcher (cdr matcher)))
549 matcher))
551 (defun recentf-arrange-by-rule (l)
552 "Filter the list of menu-elements L.
553 Arrange them in sub-menus following rules in `recentf-arrange-rules'."
554 (let ((sub-menus-number (length recentf-arrange-rules)))
555 (if (> sub-menus-number 0)
556 (let ((sub-menus (apply 'vector
557 (mapcar (function
558 (lambda (pair)
559 (list (car pair))))
560 recentf-arrange-rules)))
561 other-menu-elements index min-size)
562 (while l
563 (let* ((menu-element (car l))
564 (file-path (recentf-menu-element-value menu-element))
565 (rules recentf-arrange-rules)
566 (found nil))
567 (setq index 0)
568 (while (and (not found) rules)
569 (if (recentf-match-rule-p (cdar rules) file-path)
570 (let ((sub-menu (aref sub-menus index)))
571 (setq found t)
572 (recentf-set-menu-element-value
573 sub-menu
574 (cons menu-element (recentf-menu-element-value sub-menu)))
576 (setq index (1+ index))
577 (setq rules (cdr rules)))
578 (or found
579 (setq other-menu-elements
580 (cons menu-element other-menu-elements)))
581 (setq l (cdr l))))
582 (setq index 0)
583 (setq l nil)
584 (setq min-size (if (integerp recentf-arrange-by-rules-min-items)
585 (max 0 recentf-arrange-by-rules-min-items)
587 (while (< index sub-menus-number)
588 (let* ((sub-menu (aref sub-menus index))
589 (sub-menu-title (recentf-menu-element-item sub-menu))
590 (sub-menu-elements (recentf-menu-element-value sub-menu))
591 (sub-menu-length (length sub-menu-elements)))
592 (if (> sub-menu-length 0)
593 (cond
594 ((< sub-menu-length min-size)
595 (setq other-menu-elements
596 (nconc sub-menu-elements other-menu-elements)))
597 ((>= sub-menu-length min-size)
598 (recentf-set-menu-element-item
599 sub-menu
600 (format sub-menu-title sub-menu-length))
601 (recentf-set-menu-element-value
602 sub-menu
603 (recentf-apply-menu-filter
604 recentf-arrange-by-rule-subfilter
605 (nreverse sub-menu-elements)))
606 (setq l (cons sub-menu l)))))
607 (setq index (1+ index))))
608 (if (and (stringp recentf-arrange-by-rule-others)
609 other-menu-elements)
610 (setq l
611 (nreverse
612 (cons (recentf-make-menu-element
613 (format recentf-arrange-by-rule-others
614 (length other-menu-elements))
615 (recentf-apply-menu-filter
616 recentf-arrange-by-rule-subfilter
617 (nreverse other-menu-elements)))
618 l)))
619 (setq l (nconc (nreverse l)
620 (recentf-apply-menu-filter
621 recentf-arrange-by-rule-subfilter
622 (nreverse other-menu-elements)))))))
625 (defun recentf-build-mode-rules ()
626 "Convert `auto-mode-alist' to `recentf-arrange-rules' format."
627 (let ((case-fold-search recentf-case-fold-search)
628 (modes auto-mode-alist)
629 regexp mode rule-name rule rules)
630 (while modes
631 (setq regexp (caar modes))
632 (setq mode (cdar modes))
633 (when (symbolp mode)
634 (setq rule-name (symbol-name mode))
635 (if (string-match "\\(.*\\)-mode$" rule-name)
636 (setq rule-name (match-string 1 rule-name)))
637 (setq rule-name (concat rule-name " (%d)"))
638 (setq rule (assoc rule-name rules))
639 (if rule
640 (setcdr rule (cons regexp (cdr rule)))
641 (setq rules (cons (list rule-name regexp) rules))))
642 (setq modes (cdr modes)))
643 ;; It is important to preserve auto-mode-alist order
644 ;; to ensure the right file <-> mode association
645 (nreverse rules)))
647 (defun recentf-arrange-by-mode (l)
648 "Filter the list of menu-elements L to build sub-menus for each major mode."
649 (let ((recentf-arrange-rules (recentf-build-mode-rules))
650 (recentf-arrange-by-rule-others "others (%d)"))
651 (recentf-arrange-by-rule l)))
653 (defun recentf-build-dir-rules (l)
654 "Convert directories in menu-elements L to rules in `recentf-arrange-rules' format."
655 (let (dirs)
656 (mapcar (function
657 (lambda (e)
658 (let ((dir (file-name-directory
659 (recentf-menu-element-value e))))
660 (or (member dir dirs)
661 (setq dirs (cons dir dirs))))))
663 (mapcar (function
664 (lambda (d)
665 (cons (concat d " (%d)")
666 (concat "\\`" d))))
667 (nreverse (sort dirs 'string-lessp)))))
669 (defun recentf-file-name-nondir (l)
670 "Filter the list of menu-elements L to show only filenames.
671 This simplified version of `recentf-show-basenames' does not handle
672 duplicates. It is used by `recentf-arrange-by-dir' as its
673 `recentf-arrange-by-rule-subfilter'."
674 (mapcar (function
675 (lambda (e)
676 (recentf-make-menu-element
677 (file-name-nondirectory (recentf-menu-element-value e))
678 (recentf-menu-element-value e))))
681 (defun recentf-arrange-by-dir (l)
682 "Filter the list of menu-elements L to build sub-menus for each directory."
683 (let ((recentf-arrange-rules (recentf-build-dir-rules l))
684 (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
685 recentf-arrange-by-rule-others)
686 (nreverse (recentf-arrange-by-rule l))))
688 (defvar recentf-filter-changer-state nil
689 "Used by `recentf-filter-changer' to hold its state.")
691 (defcustom recentf-filter-changer-alist
693 (recentf-arrange-by-mode . "*Files by Mode*")
694 (recentf-arrange-by-dir . "*Files by Directory*")
695 (recentf-arrange-by-rule . "*Files by User Rule*")
697 "*List of filters managed by `recentf-filter-changer'.
698 Each filter is defined by a pair (FILTER-FUN . FILTER-LBL) where:
700 - - FILTER-FUN is the function that filters menu-elements
701 - - FILTER-LBL is the menu item used to activate the filter"
702 :group 'recentf-filters
703 :type '(repeat (cons function string))
704 :set (lambda (sym val)
705 (setq recentf-filter-changer-state nil)
706 (recentf-menu-customization-changed sym val)))
708 (defun recentf-filter-changer-goto-next ()
709 "Go to the next filter available (see `recentf-filter-changer')."
710 (and (consp recentf-filter-changer-state)
711 (setq recentf-filter-changer-state
712 (cdr recentf-filter-changer-state)))
713 (setq recentf-update-menu-p t))
715 (defun recentf-filter-changer-get-current ()
716 "Get the current filter available (see `recentf-filter-changer')."
717 (if (null recentf-filter-changer-state)
718 (setq recentf-filter-changer-state recentf-filter-changer-alist))
719 (and (consp recentf-filter-changer-state)
720 (car recentf-filter-changer-state)))
722 (defun recentf-filter-changer-get-next ()
723 "Get the next filter available (see `recentf-filter-changer')."
724 (let ((filters recentf-filter-changer-state))
725 (cond ((consp filters)
726 (setq filters (cdr filters))
727 (if (null filters)
728 (setq filters recentf-filter-changer-alist)))
730 (setq filters recentf-filter-changer-alist)
731 (if (consp filters)
732 (setq filters (cdr filters)))))
733 (if (consp filters)
734 (car filters))))
736 (defun recentf-filter-changer (l)
737 "Manage a ring of filters.
738 `recentf-filter-changer-alist' defines the filters in the ring.
739 Actual filtering of L is delegated to the current filter in the
740 ring. A filter menu item is displayed allowing to dynamically activate
741 the next filter in the ring. If the filter ring is empty L is left
742 unchanged."
743 (let ((current-filter-item (recentf-filter-changer-get-current))
744 (next-filter-item (recentf-filter-changer-get-next)))
745 (when current-filter-item
746 (setq l (recentf-apply-menu-filter (car current-filter-item) l))
747 (if next-filter-item
748 (setq recentf-menu-filter-commands
749 (list (vector (cdr next-filter-item)
750 '(recentf-filter-changer-goto-next)
751 t)))))
754 ;;;;
755 ;;;; Dialogs stuff
756 ;;;;
758 (defun recentf-cancel-dialog (&rest ignore)
759 "Cancel the current dialog.
760 Used by `recentf-edit-list' and `recentf-open-files' dialogs."
761 (interactive)
762 (kill-buffer (current-buffer))
763 (message "Dialog canceled."))
765 (defvar recentf-dialog-mode-map nil
766 "`recentf-dialog-mode' keymap.")
768 (if recentf-dialog-mode-map
770 (setq recentf-dialog-mode-map (make-sparse-keymap))
771 (define-key recentf-dialog-mode-map "q" 'recentf-cancel-dialog)
772 (define-key recentf-dialog-mode-map [down-mouse-1] 'widget-button-click)
773 (set-keymap-parent recentf-dialog-mode-map widget-keymap))
775 (defun recentf-dialog-mode ()
776 "Major mode used in recentf dialogs.
778 These are the special commands of `recentf-dialog-mode' mode:
779 q -- cancel this dialog."
780 (interactive)
781 (setq major-mode 'recentf-dialog-mode)
782 (setq mode-name "recentf-dialog")
783 (use-local-map recentf-dialog-mode-map))
785 ;;;;
786 ;;;; Hooks and Commands
787 ;;;;
789 (defun recentf-add-file-hook ()
790 "Insert the name of the file just opened or written into `recentf-list'."
791 (and buffer-file-name (recentf-add-file buffer-file-name))
792 nil)
794 (defun recentf-remove-file-hook ()
795 "When a buffer is killed remove a non readable file from `recentf-list'."
796 (and buffer-file-name (recentf-remove-if-non-readable buffer-file-name))
797 nil)
799 (defun recentf-update-menu-hook ()
800 "Update the recentf menu from the current `recentf-list'."
801 (when recentf-update-menu-p
802 (condition-case nil
803 (progn
804 (setq recentf-update-menu-p nil)
805 (easy-menu-change recentf-menu-path
806 recentf-menu-title
807 (recentf-make-menu-items)
808 recentf-menu-before))
809 (error nil))))
811 (defun recentf-dump-variable (variable &optional limit)
812 "Insert a \"(setq VARIABLE value)\" in the current buffer.
813 Optional argument LIMIT specifies a maximum length when VARIABLE value
814 is a list (default to the full list)."
815 (let ((value (symbol-value variable)))
816 (insert (format "(setq %S\n '(\n" variable))
817 (cond ((consp value)
818 (if (and (integerp limit) (> limit 0))
819 (setq value (recentf-trunc-list value limit)))
820 (mapcar (function
821 (lambda (e)
822 (insert (format " %S\n" e))))
823 value))
825 (insert (format " %S\n" value))))
826 (insert " ))\n")
829 ;;;###autoload
830 (defun recentf-save-list ()
831 "Save the current `recentf-list' to the file `recentf-save-file'."
832 (interactive)
833 (with-temp-buffer
834 (erase-buffer)
835 (insert (format recentf-save-file-header (current-time-string)))
836 (recentf-dump-variable 'recentf-list recentf-max-saved-items)
837 (recentf-dump-variable 'recentf-filter-changer-state)
838 (if (file-writable-p recentf-save-file)
839 (write-region (point-min) (point-max) recentf-save-file))
840 (kill-buffer (current-buffer)))
841 nil)
843 (defvar recentf-edit-selected-items nil
844 "Used by `recentf-edit-list'.
845 Holds list of files to be deleted from `recentf-list'.")
847 (defun recentf-edit-list-action (widget &rest ignore)
848 "Checkbox WIDGET action used by `recentf-edit-list' to select/unselect a file."
849 (let ((value (widget-get widget ':tag)))
850 ;; if value is already in the selected items
851 (if (memq value recentf-edit-selected-items)
852 ;; then remove it
853 (progn
854 (setq recentf-edit-selected-items
855 (delq value recentf-edit-selected-items))
856 (message "%s removed from selection." value))
857 ;; else add it
858 (progn
859 (setq recentf-edit-selected-items
860 (nconc (list value) recentf-edit-selected-items))
861 (message "%s added to selection." value)))))
863 ;;;###autoload
864 (defun recentf-edit-list ()
865 "Allow the user to edit the files that are kept in the recent list."
866 (interactive)
867 (with-current-buffer (get-buffer-create (concat "*" recentf-menu-title " - Edit list*"))
868 (switch-to-buffer (current-buffer))
869 (kill-all-local-variables)
870 (let ((inhibit-read-only t))
871 (erase-buffer))
872 (let ((all (overlay-lists)))
873 ;; Delete all the overlays.
874 (mapcar 'delete-overlay (car all))
875 (mapcar 'delete-overlay (cdr all)))
876 (setq recentf-edit-selected-items nil)
877 ;; Insert the dialog header
878 (widget-insert "Select the files to be deleted from the 'recentf-list'.\n\n")
879 (widget-insert "Click on Ok to update the list. ")
880 (widget-insert "Click on Cancel or type \"q\" to quit.\n")
881 ;; Insert the list of files as checkboxes
882 (mapcar (function
883 (lambda (item)
884 (widget-create 'checkbox
885 :value nil ; unselected checkbox
886 :format "\n %[%v%] %t"
887 :tag item
888 :notify 'recentf-edit-list-action)))
889 recentf-list)
890 (widget-insert "\n\n")
891 ;; Insert the Ok button
892 (widget-create 'push-button
893 :notify (lambda (&rest ignore)
894 (if recentf-edit-selected-items
895 (progn (kill-buffer (current-buffer))
896 (mapcar (function
897 (lambda (item)
898 (setq recentf-list
899 (delq item recentf-list))))
900 recentf-edit-selected-items)
901 (message "%S file(s) removed from the list"
902 (length recentf-edit-selected-items))
903 (setq recentf-update-menu-p t))
904 (message "No file selected.")))
905 "Ok")
906 (widget-insert " ")
907 ;; Insert the Cancel button
908 (widget-create 'push-button
909 :notify 'recentf-cancel-dialog
910 "Cancel")
911 (recentf-dialog-mode)
912 (widget-setup)
913 (goto-char (point-min))))
915 ;;;###autoload
916 (defun recentf-cleanup ()
917 "Remove all non-readable and excluded files from `recentf-list'."
918 (interactive)
919 (let ((count (length recentf-list)))
920 (setq recentf-list
921 (delq nil
922 (mapcar (function
923 (lambda (filename)
924 (and (file-readable-p filename)
925 (recentf-include-p filename)
926 filename)))
927 recentf-list)))
928 (setq count (- count (length recentf-list)))
929 (message "%s removed from the list"
930 (cond ((= count 0) "No file")
931 ((= count 1) "One file")
932 (t (format "%d files" count)))))
933 (setq recentf-update-menu-p t))
935 (defun recentf-open-files-action (widget &rest ignore)
936 "Button WIDGET action used by `recentf-open-files' to open a file."
937 (kill-buffer (current-buffer))
938 (funcall recentf-menu-action (widget-value widget)))
940 (defvar recentf-open-files-item-shift ""
941 "String used by `recentf-open-files' to shift right sub-menu items.")
943 (defun recentf-open-files-item (menu-element)
944 "Insert MENU-ELEMENT item in the current interaction buffer."
945 (let ((menu-item (car menu-element))
946 (file-path (cdr menu-element)))
947 (if (consp file-path) ; This is a sub-menu
948 (let* ((shift recentf-open-files-item-shift)
949 (recentf-open-files-item-shift (concat shift " ")))
950 (widget-create 'item
951 :tag menu-item
952 :sample-face 'bold
953 :format (concat shift "%{%t%}:\n"))
954 (mapcar 'recentf-open-files-item
955 file-path)
956 (widget-insert "\n"))
957 (widget-create 'push-button
958 :button-face 'default
959 :tag menu-item
960 :help-echo (concat "Open " file-path)
961 :format (concat recentf-open-files-item-shift "%[%t%]")
962 :notify 'recentf-open-files-action
963 file-path)
964 (widget-insert "\n"))))
966 ;;;###autoload
967 (defun recentf-open-files (&optional files buffer-name)
968 "Display buffer allowing user to choose a file from recently-opened list.
969 The optional argument FILES may be used to specify the list, otherwise
970 `recentf-list' is used. The optional argument BUFFER-NAME specifies
971 which buffer to use for the interaction."
972 (interactive)
973 (if (null files)
974 (setq files recentf-list))
975 (if (null buffer-name)
976 (setq buffer-name (concat "*" recentf-menu-title "*")))
977 (with-current-buffer (get-buffer-create buffer-name)
978 (switch-to-buffer (current-buffer))
979 (kill-all-local-variables)
980 (let ((inhibit-read-only t))
981 (erase-buffer))
982 (let ((all (overlay-lists)))
983 ;; Delete all the overlays.
984 (mapcar 'delete-overlay (car all))
985 (mapcar 'delete-overlay (cdr all)))
986 ;; Insert the dialog header
987 (widget-insert "Click on a file to open it. ")
988 (widget-insert "Click on Cancel or type \"q\" to quit.\n\n" )
989 ;; Insert the list of files as buttons
990 (let ((recentf-open-files-item-shift ""))
991 (mapcar 'recentf-open-files-item
992 (recentf-apply-menu-filter
993 recentf-menu-filter
994 (mapcar 'recentf-make-default-menu-element files))))
995 (widget-insert "\n")
996 ;; Insert the Cancel button
997 (widget-create 'push-button
998 :notify 'recentf-cancel-dialog
999 "Cancel")
1000 (recentf-dialog-mode)
1001 (widget-setup)
1002 (goto-char (point-min))))
1004 ;;;###autoload
1005 (defun recentf-open-more-files ()
1006 "Allow the user to open files that are not in the menu."
1007 (interactive)
1008 (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
1009 (concat "*" recentf-menu-title " - More*")))
1012 ;;; Note this definition must be at the end of the file, because
1013 ;;; `define-minor-mode' actually calls the mode-function if the
1014 ;;; associated variable is non-nil, which requires that all needed
1015 ;;; functions be already defined. [This is arguably a bug in d-m-m]
1016 ;;;###autoload
1017 (define-minor-mode recentf-mode
1018 "Toggle recentf mode.
1019 With prefix argument ARG, turn on if positive, otherwise off.
1020 Returns non-nil if the new state is enabled.
1022 When recentf mode is enabled, it maintains a menu for visiting files that
1023 were operated on recently."
1024 :global t
1025 :group 'recentf
1026 (if recentf-mode
1027 (unless recentf-initialized-p
1028 (setq recentf-initialized-p t)
1029 (if (file-readable-p recentf-save-file)
1030 (load-file recentf-save-file))
1031 (setq recentf-update-menu-p t)
1032 (add-hook 'find-file-hooks 'recentf-add-file-hook)
1033 (add-hook 'write-file-hooks 'recentf-add-file-hook)
1034 (add-hook 'menu-bar-update-hook 'recentf-update-menu-hook)
1035 (add-hook 'kill-emacs-hook 'recentf-save-list))
1036 (when recentf-initialized-p
1037 (setq recentf-initialized-p nil)
1038 (recentf-save-list)
1039 (easy-menu-remove-item nil recentf-menu-path recentf-menu-title)
1040 (remove-hook 'find-file-hooks 'recentf-add-file-hook)
1041 (remove-hook 'write-file-hooks 'recentf-add-file-hook)
1042 (remove-hook 'menu-bar-update-hook 'recentf-update-menu-hook)
1043 (remove-hook 'kill-emacs-hook 'recentf-save-list))))
1046 (provide 'recentf)
1048 (run-hooks 'recentf-load-hook)
1050 ;;; recentf.el ends here