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