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