recentf.el 49.7 KB
Newer Older
1
;;; recentf.el --- setup a menu of recently opened files
Dave Love's avatar
Dave Love committed
2

3
;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
Dave Love's avatar
Dave Love committed
5 6 7

;; Author: David Ponce <david@dponce.com>
;; Created: July 19 1999
8 9
;; Keywords: files

Dave Love's avatar
Dave Love committed
10 11
;; This file is part of GNU Emacs.

12 13 14 15
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Dave Love's avatar
Dave Love committed
16 17 18 19 20 21 22

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
23
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Dave Love's avatar
Dave Love committed
24 25 26 27

;;; Commentary:

;; This package maintains a menu for visiting files that were operated
28 29 30 31 32
;; on recently.  When enabled a new "Open Recent" sub menu is
;; displayed in the "Files" menu.  The recent files list is
;; automatically saved across Emacs sessions.  You can customize the
;; number of recent files displayed, the location of the menu and
;; others options (see the source code for details).
33 34

;;; History:
Dave Love's avatar
Dave Love committed
35 36 37 38
;;

;;; Code:
(require 'easymenu)
39
(require 'tree-widget)
40
(require 'timer)
Dave Love's avatar
Dave Love committed
41

42 43
;;; Internal data
;;
Dave Love's avatar
Dave Love committed
44 45 46
(defvar recentf-list nil
  "List of recently opened files.")

David Ponce's avatar
David Ponce committed
47 48 49
(defsubst recentf-enabled-p ()
  "Return non-nil if recentf mode is currently enabled."
  (memq 'recentf-save-list kill-emacs-hook))
50 51 52

;;; Customization
;;
Dave Love's avatar
Dave Love committed
53 54 55 56 57 58 59 60 61 62 63
(defgroup recentf nil
  "Maintain a menu of recently opened files."
  :version "21.1"
  :group 'files)

(defgroup recentf-filters nil
  "Group to customize recentf menu filters.
You should define the options of your own filters in this group."
  :group 'recentf)

(defcustom recentf-max-saved-items 20
64
  "Maximum number of items of the recent list that will be saved.
65
A nil value means to save the whole list.
66
See the command `recentf-save-list'."
Dave Love's avatar
Dave Love committed
67 68 69
  :group 'recentf
  :type 'integer)

70
(defcustom recentf-save-file (convert-standard-filename "~/.recentf")
71
  "File to save the recent list into."
Dave Love's avatar
Dave Love committed
72
  :group 'recentf
73 74 75 76 77 78 79 80
  :type 'file
  :initialize 'custom-initialize-default
  :set (lambda (symbol value)
         (let ((oldvalue (eval symbol)))
           (custom-set-default symbol value)
           (and (not (equal value oldvalue))
                recentf-mode
                (recentf-load-list)))))
Dave Love's avatar
Dave Love committed
81

82 83 84 85 86 87 88 89
(defcustom recentf-save-file-modes 384 ;; 0600
  "Mode bits of recentf save file, as an integer, or nil.
If non-nil, after writing `recentf-save-file', set its mode bits to
this value.  By default give R/W access only to the user who owns that
file.  See also the function `set-file-modes'."
  :group 'recentf
  :type '(choice (const :tag "Don't change" nil)
          integer))
90

Dave Love's avatar
Dave Love committed
91
(defcustom recentf-exclude nil
92
  "List of regexps and predicates for filenames excluded from the recent list.
Glenn Morris's avatar
Glenn Morris committed
93 94 95 96
When a filename matches any of the regexps or satisfies any of the
predicates it is excluded from the recent list.
A predicate is a function that is passed a filename to check and that
must return non-nil to exclude it."
Dave Love's avatar
Dave Love committed
97
  :group 'recentf
Glenn Morris's avatar
Glenn Morris committed
98
  :type '(repeat (choice regexp function)))
Dave Love's avatar
Dave Love committed
99

100 101 102 103
(defun recentf-keep-default-predicate (file)
  "Return non-nil if FILE should be kept in the recent list.
It handles the case of remote files as well."
  (cond
104
   ((file-remote-p file nil t) (file-readable-p file))
105 106 107
   ((file-remote-p file))
   ((file-readable-p file))))

David Ponce's avatar
David Ponce committed
108
(defcustom recentf-keep
109
  '(recentf-keep-default-predicate)
110
  "List of regexps and predicates for filenames kept in the recent list.
David Ponce's avatar
David Ponce committed
111 112 113 114
Regexps and predicates are tried in the specified order.
When nil all filenames are kept in the recent list.
When a filename matches any of the regexps or satisfies any of the
predicates it is kept in the recent list.
115 116 117 118
The default is to keep readable files.  Remote files are checked
for readability only in case a connection is established to that
remote system, otherwise they are kept in the recent list without
checking their readability.
David Ponce's avatar
David Ponce committed
119
A predicate is a function that is passed a filename to check and that
120
must return non-nil to keep it."
David Ponce's avatar
David Ponce committed
121 122 123
  :group 'recentf
  :type '(repeat (choice regexp function)))

124 125 126
(defun recentf-menu-customization-changed (variable value)
  "Function called when the recentf menu customization has changed.
Set VARIABLE with VALUE, and force a rebuild of the recentf menu."
David Ponce's avatar
David Ponce committed
127 128 129 130 131 132 133
  (if (and (featurep 'recentf) (recentf-enabled-p))
      (progn
        ;; Unavailable until recentf has been loaded.
        (recentf-hide-menu)
        (set-default variable value)
        (recentf-show-menu))
    (set-default variable value)))
134

Dave Love's avatar
Dave Love committed
135
(defcustom recentf-menu-title "Open Recent"
136
  "Name of the recentf menu."
Dave Love's avatar
Dave Love committed
137 138 139 140
  :group 'recentf
  :type 'string
  :set 'recentf-menu-customization-changed)

141
(defcustom recentf-menu-path '("File")
142
  "Path where to add the recentf menu.
143
If nil add it at top level (see also `easy-menu-add-item')."
Dave Love's avatar
Dave Love committed
144 145 146 147 148
  :group 'recentf
  :type '(choice (const :tag "Top Level" nil)
                 (sexp :tag "Menu Path"))
  :set 'recentf-menu-customization-changed)

149
(defcustom recentf-menu-before "Open File..."
150
  "Name of the menu before which the recentf menu will be added.
151
If nil add it at end of menu (see also `easy-menu-add-item')."
Dave Love's avatar
Dave Love committed
152 153 154 155 156
  :group 'recentf
  :type '(choice (string :tag "Name")
                 (const :tag "Last" nil))
  :set 'recentf-menu-customization-changed)

David Ponce's avatar
David Ponce committed
157
(defcustom recentf-menu-action 'find-file
158
  "Function to invoke with a filename item of the recentf menu.
David Ponce's avatar
David Ponce committed
159
The default is to call `find-file' to edit the selected file."
Dave Love's avatar
Dave Love committed
160
  :group 'recentf
David Ponce's avatar
David Ponce committed
161
  :type 'function)
Dave Love's avatar
Dave Love committed
162 163

(defcustom recentf-max-menu-items 10
164
  "Maximum number of items in the recentf menu."
Dave Love's avatar
Dave Love committed
165
  :group 'recentf
David Ponce's avatar
David Ponce committed
166
  :type 'integer)
Dave Love's avatar
Dave Love committed
167 168

(defcustom recentf-menu-filter nil
169
  "Function used to filter files displayed in the recentf menu.
170
A nil value means no filter.  The following functions are predefined:
Dave Love's avatar
Dave Love committed
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
- `recentf-sort-ascending'
    Sort menu items in ascending order.
- `recentf-sort-descending'
    Sort menu items in descending order.
- `recentf-sort-basenames-ascending'
    Sort menu items by filenames sans directory in ascending order.
- `recentf-sort-basenames-descending'
    Sort menu items by filenames sans directory in descending order.
- `recentf-sort-directories-ascending'
    Sort menu items by directories in ascending order.
- `recentf-sort-directories-descending'
    Sort menu items by directories in descending order.
- `recentf-show-basenames'
    Show filenames sans directory in menu items.
- `recentf-show-basenames-ascending'
    Show filenames sans directory in ascending order.
- `recentf-show-basenames-descending'
    Show filenames sans directory in descending order.
- `recentf-relative-filter'
    Show filenames relative to `default-directory'.
- `recentf-arrange-by-rule'
    Show sub-menus following user defined rules.
- `recentf-arrange-by-mode'
    Show a sub-menu for each major mode.
- `recentf-arrange-by-dir'
    Show a sub-menu for each directory.
- `recentf-filter-changer'
David Ponce's avatar
David Ponce committed
199
    Manage a menu of filters.
200 201 202 203

The filter function is called with one argument, the list of menu
elements used to build the menu and must return a new list of menu
elements (see `recentf-make-menu-element' for menu element form)."
Dave Love's avatar
Dave Love committed
204
  :group 'recentf
Juanma Barranquero's avatar
Juanma Barranquero committed
205
  :type '(radio (const nil)
206 207 208 209 210 211 212 213 214 215 216 217 218 219
                (function-item recentf-sort-ascending)
                (function-item recentf-sort-descending)
                (function-item recentf-sort-basenames-ascending)
                (function-item recentf-sort-basenames-descending)
                (function-item recentf-sort-directories-ascending)
                (function-item recentf-sort-directories-descending)
                (function-item recentf-show-basenames)
                (function-item recentf-show-basenames-ascending)
                (function-item recentf-show-basenames-descending)
                (function-item recentf-relative-filter)
                (function-item recentf-arrange-by-rule)
                (function-item recentf-arrange-by-mode)
                (function-item recentf-arrange-by-dir)
                (function-item recentf-filter-changer)
David Ponce's avatar
David Ponce committed
220
                function))
Dave Love's avatar
Dave Love committed
221

222
(defcustom recentf-menu-open-all-flag nil
223
  "Non-nil means to show an \"All...\" item in the menu.
224 225
This item will replace the \"More...\" item."
  :group 'recentf
David Ponce's avatar
David Ponce committed
226
  :type 'boolean)
227

228 229 230 231
(define-obsolete-variable-alias 'recentf-menu-append-commands-p
                                'recentf-menu-append-commands-flag
                                "22.1")

232
(defcustom recentf-menu-append-commands-flag t
233
  "Non-nil means to append command items to the menu."
Dave Love's avatar
Dave Love committed
234
  :group 'recentf
David Ponce's avatar
David Ponce committed
235
  :type 'boolean)
Dave Love's avatar
Dave Love committed
236

237
(defcustom recentf-auto-cleanup 'mode
238
  "Define when to automatically cleanup the recent list.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
The following values can be set:

- `mode'
    Cleanup when turning the mode on (default).
- `never'
    Never cleanup the list automatically.
- A number
    Cleanup each time Emacs has been idle that number of seconds.
- A time string
    Cleanup at specified time string, for example at \"11:00pm\".

Setting this variable directly does not take effect;
use \\[customize].

See also the command `recentf-cleanup', that can be used to manually
cleanup the list."
  :group 'recentf
  :type '(radio (const  :tag "When mode enabled"
                        :value mode)
                (const  :tag "Never"
                        :value never)
                (number :tag "When idle that seconds"
                        :value 300)
                (string :tag "At time"
                        :value "11:00pm"))
  :set (lambda (variable value)
         (set-default variable value)
         (when (featurep 'recentf)
           ;; Unavailable until recentf has been loaded.
           (recentf-auto-cleanup))))
Dave Love's avatar
Dave Love committed
269

270
(defcustom recentf-initialize-file-name-history t
271
  "Non-nil means to initialize `file-name-history' with the recent list.
272 273 274 275
If `file-name-history' is not empty, do nothing."
  :group 'recentf
  :type  'boolean)

Dave Love's avatar
Dave Love committed
276
(defcustom recentf-load-hook nil
277
   "Normal hook run at end of loading the `recentf' package."
Dave Love's avatar
Dave Love committed
278 279 280
  :group 'recentf
  :type 'hook)

281 282 283
(defcustom recentf-filename-handlers nil
  "Functions to post process recent file names.
They are successively passed a file name to transform it."
284
  :group 'recentf
285 286 287 288 289 290 291
  :type '(choice
          (const :tag "None" nil)
          (repeat :tag "Functions"
           (choice
            (const file-truename)
            (const abbreviate-file-name)
            (function :tag "Other function")))))
292 293 294 295 296 297 298

(defcustom recentf-show-file-shortcuts-flag t
  "Whether to show ``[N]'' for the Nth item up to 10.
If non-nil, `recentf-open-files' will show labels for keys that can be
used as shortcuts to open the Nth file."
  :group 'recentf
  :type 'boolean)
299 300 301

;;; Utilities
;;
Dave Love's avatar
Dave Love committed
302
(defconst recentf-case-fold-search
Dan Nicolaescu's avatar
Dan Nicolaescu committed
303
  (memq system-type '(windows-nt cygwin))
Dave Love's avatar
Dave Love committed
304 305
  "Non-nil if recentf searches and matches should ignore case.")

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
(defsubst recentf-string-equal (s1 s2)
  "Return non-nil if strings S1 and S2 have identical contents.
Ignore case if `recentf-case-fold-search' is non-nil."
  (if recentf-case-fold-search
      (string-equal (downcase s1) (downcase s2))
    (string-equal s1 s2)))

(defsubst recentf-string-lessp (s1 s2)
  "Return non-nil if string S1 is less than S2 in lexicographic order.
Ignore case if `recentf-case-fold-search' is non-nil."
  (if recentf-case-fold-search
      (string-lessp (downcase s1) (downcase s2))
    (string-lessp s1 s2)))

(defun recentf-string-member (elt list)
  "Return non-nil if ELT is an element of LIST.
The value is actually the tail of LIST whose car is ELT.
ELT must be a string and LIST a list of strings.
Ignore case if `recentf-case-fold-search' is non-nil."
  (while (and list (not (recentf-string-equal elt (car list))))
    (setq list (cdr list)))
  list)

(defsubst recentf-trunc-list (l n)
  "Return from L the list of its first N elements."
  (let (nl)
    (while (and l (> n 0))
      (setq nl (cons (car l) nl)
            n  (1- n)
            l  (cdr l)))
    (nreverse nl)))

(defun recentf-dump-variable (variable &optional limit)
  "Insert a \"(setq VARIABLE value)\" in the current buffer.
When the value of VARIABLE is a list, optional argument LIMIT
specifies a maximum number of elements to insert.  By default insert
the full list."
  (let ((value (symbol-value variable)))
    (if (atom value)
David Ponce's avatar
David Ponce committed
345
        (insert (format "\n(setq %S '%S)\n" variable value))
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
      (when (and (integerp limit) (> limit 0))
        (setq value (recentf-trunc-list value limit)))
      (insert (format "\n(setq %S\n      '(" variable))
      (dolist (e value)
        (insert (format "\n        %S" e)))
      (insert "\n        ))\n"))))

(defvar recentf-auto-cleanup-timer nil
  "Timer used to automatically cleanup the recent list.
See also the option `recentf-auto-cleanup'.")

(defun recentf-auto-cleanup ()
  "Automatic cleanup of the recent list."
  (when (timerp recentf-auto-cleanup-timer)
    (cancel-timer recentf-auto-cleanup-timer))
  (when recentf-mode
    (setq recentf-auto-cleanup-timer
          (cond
           ((eq 'mode recentf-auto-cleanup)
            (recentf-cleanup)
            nil)
           ((numberp recentf-auto-cleanup)
            (run-with-idle-timer
             recentf-auto-cleanup t 'recentf-cleanup))
           ((stringp recentf-auto-cleanup)
            (run-at-time
             recentf-auto-cleanup nil 'recentf-cleanup))))))

;;; File functions
;;
(defsubst recentf-push (filename)
  "Push FILENAME into the recent list, if it isn't there yet.
If it is there yet, move it at the beginning of the list.
If `recentf-case-fold-search' is non-nil, ignore case when comparing
filenames."
  (let ((m (recentf-string-member filename recentf-list)))
    (and m (setq recentf-list (delq (car m) recentf-list)))
    (push filename recentf-list)))

385 386 387 388 389 390 391 392 393 394 395 396 397 398
(defun recentf-apply-filename-handlers (name)
  "Apply `recentf-filename-handlers' to file NAME.
Return the transformed file name, or NAME if any handler failed, or
returned nil."
  (or (condition-case nil
          (let ((handlers recentf-filename-handlers)
                (filename name))
            (while (and filename handlers)
              (setq filename (funcall (car handlers) filename)
                    handlers (cdr handlers)))
            filename)
        (error nil))
      name))

399
(defsubst recentf-expand-file-name (name)
400 401 402 403
  "Convert file NAME to absolute, and canonicalize it.
NAME is first passed to the function `expand-file-name', then to
`recentf-filename-handlers' to post process it."
  (recentf-apply-filename-handlers (expand-file-name name)))
404

Dave Love's avatar
Dave Love committed
405
(defun recentf-include-p (filename)
Glenn Morris's avatar
Glenn Morris committed
406 407
  "Return non-nil if FILENAME should be included in the recent list.
That is, if it doesn't match any of the `recentf-exclude' checks."
Dave Love's avatar
Dave Love committed
408
  (let ((case-fold-search recentf-case-fold-search)
Glenn Morris's avatar
Glenn Morris committed
409
        (checks recentf-exclude)
David Ponce's avatar
David Ponce committed
410
        (keepit t))
Glenn Morris's avatar
Glenn Morris committed
411
    (while (and checks keepit)
David Ponce's avatar
David Ponce committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
      (setq keepit (condition-case nil
                       (not (if (stringp (car checks))
                                ;; A regexp
                                (string-match (car checks) filename)
                              ;; A predicate
                              (funcall (car checks) filename)))
                     (error nil))
            checks (cdr checks)))
    keepit))

(defun recentf-keep-p (filename)
  "Return non-nil if FILENAME should be kept in the recent list.
That is, if it matches any of the `recentf-keep' checks."
  (let* ((case-fold-search recentf-case-fold-search)
         (checks recentf-keep)
         (keepit (null checks)))
    (while (and checks (not keepit))
      (setq keepit (condition-case nil
                       (if (stringp (car checks))
                           ;; A regexp
                           (string-match (car checks) filename)
                         ;; A predicate
                         (funcall (car checks) filename))
                     (error nil))
            checks (cdr checks)))
Glenn Morris's avatar
Glenn Morris committed
437
    keepit))
Dave Love's avatar
Dave Love committed
438

439 440
(defsubst recentf-add-file (filename)
  "Add or move FILENAME at the beginning of the recent list.
David Ponce's avatar
David Ponce committed
441 442
Does nothing if the name satisfies any of the `recentf-exclude'
regexps or predicates."
443 444 445
  (setq filename (recentf-expand-file-name filename))
  (when (recentf-include-p filename)
    (recentf-push filename)))
Dave Love's avatar
Dave Love committed
446

David Ponce's avatar
David Ponce committed
447 448
(defsubst recentf-remove-if-non-kept (filename)
  "Remove FILENAME from the recent list, if file is not kept.
449
Return non-nil if FILENAME has been removed."
David Ponce's avatar
David Ponce committed
450
  (unless (recentf-keep-p filename)
451 452 453
    (let ((m (recentf-string-member
              (recentf-expand-file-name filename) recentf-list)))
      (and m (setq recentf-list (delq (car m) recentf-list))))))
Dave Love's avatar
Dave Love committed
454

455 456 457 458 459 460 461 462 463 464 465 466 467
(defsubst recentf-directory-compare (f1 f2)
  "Compare absolute filenames F1 and F2.
First compare directories, then filenames sans directory.
Return non-nil if F1 is less than F2."
  (let ((d1 (file-name-directory f1))
        (d2 (file-name-directory f2)))
    (if (recentf-string-equal d1 d2)
        (recentf-string-lessp (file-name-nondirectory f1)
                              (file-name-nondirectory f2))
      (recentf-string-lessp d1 d2))))

;;; Menu building
;;
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
(defsubst recentf-digit-shortcut-command-name (n)
  "Return a command name to open the Nth most recent file.
See also the command `recentf-open-most-recent-file'."
  (intern (format "recentf-open-most-recent-file-%d" n)))

(defvar recentf--shortcuts-keymap
  (let ((km (make-sparse-keymap)))
    (dolist (k '(0 9 8 7 6 5 4 3 2 1))
      (let ((cmd (recentf-digit-shortcut-command-name k)))
        ;; Define a shortcut command.
        (defalias cmd
          `(lambda ()
             (interactive)
             (recentf-open-most-recent-file ,k)))
        ;; Bind it to a digit key.
        (define-key km (vector (+ k ?0)) cmd)))
    km)
  "Digit shortcuts keymap.")

487
(defvar recentf-menu-items-for-commands
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
  (list
   ["Cleanup list"
    recentf-cleanup
    :help "Remove duplicates, and obsoletes files from the recent list"
    :active t]
   ["Edit list..."
    recentf-edit-list
    :help "Manually remove files from the recent list"
    :active t]
   ["Save list now"
    recentf-save-list
    :help "Save the list of recently opened files now"
    :active t]
   ["Options..."
    (customize-group "recentf")
    :help "Customize recently opened files menu and options"
    :active t]
   )
506
  "List of menu items for recentf commands.")
Dave Love's avatar
Dave Love committed
507

508 509 510 511 512 513 514 515
(defvar recentf-menu-filter-commands nil
  "This variable can be used by menu filters to setup their own command menu.
If non-nil it must contain a list of valid menu-items to be appended
to the recent file list part of the menu.  Before calling a menu
filter function this variable is reset to nil.")

(defsubst recentf-elements (n)
  "Return a list of the first N elements of the recent list."
Dave Love's avatar
Dave Love committed
516 517
  (recentf-trunc-list recentf-list n))

518
(defsubst recentf-make-menu-element (menu-item menu-value)
Dave Love's avatar
Dave Love committed
519
  "Create a new menu-element.
520 521 522 523 524 525
A menu element is a pair (MENU-ITEM . MENU-VALUE), where MENU-ITEM is
the menu item string displayed.  MENU-VALUE is the file to be open
when the corresponding MENU-ITEM is selected.  Or it is a
pair (SUB-MENU-TITLE . MENU-ELEMENTS) where SUB-MENU-TITLE is a
sub-menu title and MENU-ELEMENTS is the list of menu elements in the
sub-menu."
Dave Love's avatar
Dave Love committed
526 527
  (cons menu-item menu-value))

528
(defsubst recentf-menu-element-item (e)
Dave Love's avatar
Dave Love committed
529 530 531
  "Return the item part of the menu-element E."
  (car e))

532
(defsubst recentf-menu-element-value (e)
Dave Love's avatar
Dave Love committed
533 534 535
  "Return the value part of the menu-element E."
  (cdr e))

536
(defsubst recentf-set-menu-element-item (e item)
Dave Love's avatar
Dave Love committed
537 538 539
  "Change the item part of menu-element E to ITEM."
  (setcar e item))

540
(defsubst recentf-set-menu-element-value (e value)
Dave Love's avatar
Dave Love committed
541 542 543
  "Change the value part of menu-element E to VALUE."
  (setcdr e value))

544
(defsubst recentf-sub-menu-element-p (e)
Dave Love's avatar
Dave Love committed
545 546 547
  "Return non-nil if menu-element E defines a sub-menu."
  (consp (recentf-menu-element-value e)))

548 549 550 551
(defsubst recentf-make-default-menu-element (file)
  "Make a new default menu element with FILE.
This a menu element (FILE . FILE)."
  (recentf-make-menu-element file file))
Dave Love's avatar
Dave Love committed
552

553 554
(defsubst recentf-menu-elements (n)
  "Return a list of the first N default menu elements from the recent list.
Dave Love's avatar
Dave Love committed
555
See also `recentf-make-default-menu-element'."
Dave Love's avatar
Dave Love committed
556 557 558 559
  (mapcar 'recentf-make-default-menu-element
          (recentf-elements n)))

(defun recentf-apply-menu-filter (filter l)
Dave Love's avatar
Dave Love committed
560 561
  "Apply function FILTER to the list of menu-elements L.
It takes care of sub-menu elements in L and recursively apply FILTER
Dave Love's avatar
Dave Love committed
562
to them.  It is guaranteed that FILTER receives only a list of single
Dave Love's avatar
Dave Love committed
563
menu-elements (no sub-menu)."
564
  (if (and l (functionp filter))
Dave Love's avatar
Dave Love committed
565
      (let ((case-fold-search recentf-case-fold-search)
566 567 568 569 570 571 572 573 574 575 576 577 578
            elts others)
        ;; split L into two sub-listes, one of sub-menus elements and
        ;; another of single menu elements.
        (dolist (elt l)
          (if (recentf-sub-menu-element-p elt)
              (push elt elts)
            (push elt others)))
        ;; Apply FILTER to single elements.
        (when others
          (setq others (funcall filter (nreverse others))))
        ;; Apply FILTER to sub-menu elements.
        (setq l nil)
        (dolist (elt elts)
Dave Love's avatar
Dave Love committed
579
          (recentf-set-menu-element-value
580 581 582 583 584
           elt (recentf-apply-menu-filter
                filter (recentf-menu-element-value elt)))
          (push elt l))
        ;; Return the new filtered menu element list.
        (nconc l others))
Dave Love's avatar
Dave Love committed
585 586
    l))

587 588 589
;; Count the number of assigned menu shortcuts.
(defvar recentf-menu-shortcuts)

David Ponce's avatar
David Ponce committed
590 591 592
(defun recentf-make-menu-items (&optional menu)
  "Make menu items from the recent list.
This is a menu filter function which ignores the MENU argument."
Dave Love's avatar
Dave Love committed
593
  (setq recentf-menu-filter-commands nil)
594 595
  (let* ((recentf-menu-shortcuts 0)
         (file-items
David Ponce's avatar
David Ponce committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
          (condition-case err
              (mapcar 'recentf-make-menu-item
                      (recentf-apply-menu-filter
                       recentf-menu-filter
                       (recentf-menu-elements recentf-max-menu-items)))
            (error
             (message "recentf update menu failed: %s"
                      (error-message-string err))))))
    (append
     (or file-items
         '(["No files" t
            :help "No recent file to open"
            :active nil]))
     (if recentf-menu-open-all-flag
         '(["All..." recentf-open-files
            :help "Open recent files through a dialog"
            :active t])
       (and (< recentf-max-menu-items (length recentf-list))
            '(["More..." recentf-open-more-files
               :help "Open files not in the menu through a dialog"
               :active t])))
     (and recentf-menu-filter-commands '("---"))
     recentf-menu-filter-commands
     (and recentf-menu-items-for-commands '("---"))
     recentf-menu-items-for-commands)))
Dave Love's avatar
Dave Love committed
621

622
(defun recentf-menu-value-shortcut (name)
David Ponce's avatar
David Ponce committed
623
  "Return a shortcut digit for file NAME.
624 625 626 627 628 629 630 631 632 633 634
Return nil if file NAME is not one of the ten more recent."
  (let ((i 0) k)
    (while (and (not k) (< i 10))
      (if (string-equal name (nth i recentf-list))
          (progn
            (setq recentf-menu-shortcuts (1+ recentf-menu-shortcuts))
            (setq k (% (1+ i) 10)))
        (setq i (1+ i))))
    k))

(defun recentf-make-menu-item (elt)
635 636 637 638 639
  "Make a menu item from menu element ELT."
  (let ((item  (recentf-menu-element-item  elt))
        (value (recentf-menu-element-value elt)))
    (if (recentf-sub-menu-element-p elt)
        (cons item (mapcar 'recentf-make-menu-item value))
640 641 642 643 644 645 646 647 648 649 650 651 652
      (let ((k (and (< recentf-menu-shortcuts 10)
                    (recentf-menu-value-shortcut value))))
        (vector item
                ;; If the file name is one of the ten more recent, use
                ;; a digit shortcut command to open it, else use an
                ;; anonymous command.
                (if k
                    (recentf-digit-shortcut-command-name k)
                  `(lambda ()
                     (interactive)
                     (,recentf-menu-action ,value)))
                :help (concat "Open " value)
                :active t)))))
Dave Love's avatar
Dave Love committed
653

654 655 656 657
(defsubst recentf-menu-bar ()
  "Return the keymap of the global menu bar."
  (lookup-key global-map [menu-bar]))

David Ponce's avatar
David Ponce committed
658 659 660 661 662 663 664 665 666 667 668
(defun recentf-show-menu ()
  "Show the menu of recently opened files."
  (easy-menu-add-item
   (recentf-menu-bar) recentf-menu-path
   (list recentf-menu-title :filter 'recentf-make-menu-items)
   recentf-menu-before))

(defun recentf-hide-menu ()
  "Hide the menu of recently opened files."
  (easy-menu-remove-item (recentf-menu-bar) recentf-menu-path
                         recentf-menu-title))
669 670 671 672

;;; Predefined menu filters
;;
(defsubst recentf-sort-ascending (l)
Dave Love's avatar
Dave Love committed
673 674 675
  "Sort the list of menu elements L in ascending order.
The MENU-ITEM part of each menu element is compared."
  (sort (copy-sequence l)
676 677 678 679
        #'(lambda (e1 e2)
            (recentf-string-lessp
             (recentf-menu-element-item e1)
             (recentf-menu-element-item e2)))))
Dave Love's avatar
Dave Love committed
680

681
(defsubst recentf-sort-descending (l)
Dave Love's avatar
Dave Love committed
682 683 684
  "Sort the list of menu elements L in descending order.
The MENU-ITEM part of each menu element is compared."
  (sort (copy-sequence l)
685 686 687 688
        #'(lambda (e1 e2)
            (recentf-string-lessp
             (recentf-menu-element-item e2)
             (recentf-menu-element-item e1)))))
Dave Love's avatar
Dave Love committed
689

690
(defsubst recentf-sort-basenames-ascending (l)
Dave Love's avatar
Dave Love committed
691
  "Sort the list of menu elements L in ascending order.
692
Only filenames sans directory are compared."
Dave Love's avatar
Dave Love committed
693
  (sort (copy-sequence l)
694 695 696 697
        #'(lambda (e1 e2)
            (recentf-string-lessp
             (file-name-nondirectory (recentf-menu-element-value e1))
             (file-name-nondirectory (recentf-menu-element-value e2))))))
Dave Love's avatar
Dave Love committed
698

699
(defsubst recentf-sort-basenames-descending (l)
Dave Love's avatar
Dave Love committed
700
  "Sort the list of menu elements L in descending order.
701
Only filenames sans directory are compared."
Dave Love's avatar
Dave Love committed
702
  (sort (copy-sequence l)
703 704 705 706 707 708
        #'(lambda (e1 e2)
            (recentf-string-lessp
             (file-name-nondirectory (recentf-menu-element-value e2))
             (file-name-nondirectory (recentf-menu-element-value e1))))))

(defsubst recentf-sort-directories-ascending (l)
Dave Love's avatar
Dave Love committed
709 710 711
  "Sort the list of menu elements L in ascending order.
Compares directories then filenames to order the list."
  (sort (copy-sequence l)
712 713 714 715
        #'(lambda (e1 e2)
            (recentf-directory-compare
             (recentf-menu-element-value e1)
             (recentf-menu-element-value e2)))))
Dave Love's avatar
Dave Love committed
716

717
(defsubst recentf-sort-directories-descending (l)
Dave Love's avatar
Dave Love committed
718 719 720
  "Sort the list of menu elements L in descending order.
Compares directories then filenames to order the list."
  (sort (copy-sequence l)
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
        #'(lambda (e1 e2)
            (recentf-directory-compare
             (recentf-menu-element-value e2)
             (recentf-menu-element-value e1)))))

(defun recentf-show-basenames (l &optional no-dir)
  "Filter the list of menu elements L to show filenames sans directory.
When a filename is duplicated, it is appended a sequence number if
optional argument NO-DIR is non-nil, or its directory otherwise."
  (let (filtered-names filtered-list full name counters sufx)
    (dolist (elt l (nreverse filtered-list))
      (setq full (recentf-menu-element-value elt)
            name (file-name-nondirectory full))
      (if (not (member name filtered-names))
          (push name filtered-names)
        (if no-dir
            (if (setq sufx (assoc name counters))
                (setcdr sufx (1+ (cdr sufx)))
              (setq sufx 1)
              (push (cons name sufx) counters))
          (setq sufx (file-name-directory full)))
        (setq name (format "%s(%s)" name sufx)))
      (push (recentf-make-menu-element name full) filtered-list))))

(defsubst recentf-show-basenames-ascending (l)
  "Filter the list of menu elements L to show filenames sans directory.
Filenames are sorted in ascending order.
This filter combines the `recentf-sort-basenames-ascending' and
Dave Love's avatar
Dave Love committed
749
`recentf-show-basenames' filters."
Dave Love's avatar
Dave Love committed
750 751
  (recentf-show-basenames (recentf-sort-basenames-ascending l)))

752 753 754 755
(defsubst recentf-show-basenames-descending (l)
  "Filter the list of menu elements L to show filenames sans directory.
Filenames are sorted in descending order.
This filter combines the `recentf-sort-basenames-descending' and
Dave Love's avatar
Dave Love committed
756
`recentf-show-basenames' filters."
Dave Love's avatar
Dave Love committed
757 758 759
  (recentf-show-basenames (recentf-sort-basenames-descending l)))

(defun recentf-relative-filter (l)
760 761 762 763 764 765 766 767
  "Filter the list of menu-elements L to show relative filenames.
Filenames are relative to the `default-directory'."
  (mapcar #'(lambda (menu-element)
              (let* ((ful (recentf-menu-element-value menu-element))
                     (rel (file-relative-name ful default-directory)))
                (if (string-match "^\\.\\." rel)
                    menu-element
                  (recentf-make-menu-element rel ful))))
Dave Love's avatar
Dave Love committed
768
          l))
769 770 771

;;; Rule based menu filters
;;
Dave Love's avatar
Dave Love committed
772 773
(defcustom recentf-arrange-rules
  '(
David Ponce's avatar
David Ponce committed
774 775 776
    ("Elisp files (%d)" ".\\.el\\'")
    ("Java files (%d)"  ".\\.java\\'")
    ("C/C++ files (%d)" "c\\(pp\\)?\\'")
Dave Love's avatar
Dave Love committed
777
    )
778
  "List of rules used by `recentf-arrange-by-rule' to build sub-menus.
Dave Love's avatar
Dave Love committed
779
A rule is a pair (SUB-MENU-TITLE . MATCHER).  SUB-MENU-TITLE is the
Dave Love's avatar
Dave Love committed
780
displayed title of the sub-menu where a '%d' `format' pattern is
Dave Love's avatar
Dave Love committed
781 782
replaced by the number of items in the sub-menu.  MATCHER is a regexp
or a list of regexps.  Items matching one of the regular expressions in
David Ponce's avatar
David Ponce committed
783 784 785 786 787 788
MATCHER are added to the corresponding sub-menu.
SUB-MENU-TITLE can be a function.  It is passed every items that
matched the corresponding MATCHER, and it must return a
pair (SUB-MENU-TITLE . ITEM).  SUB-MENU-TITLE is a computed sub-menu
title that can be another function.  ITEM is the received item which
may have been modified to match another rule."
Dave Love's avatar
Dave Love committed
789
  :group 'recentf-filters
David Ponce's avatar
David Ponce committed
790 791
  :type '(repeat (cons (choice string function)
                       (repeat regexp))))
Dave Love's avatar
Dave Love committed
792 793

(defcustom recentf-arrange-by-rule-others "Other files (%d)"
794
  "Title of the `recentf-arrange-by-rule' sub-menu.
Dave Love's avatar
Dave Love committed
795 796 797 798
This is for the menu where items that don't match any
`recentf-arrange-rules' are displayed.  If nil these items are
displayed in the main recent files menu.  A '%d' `format' pattern in
the title is replaced by the number of items in the sub-menu."
Dave Love's avatar
Dave Love committed
799 800
  :group 'recentf-filters
  :type '(choice (const  :tag "Main menu" nil)
David Ponce's avatar
David Ponce committed
801
                 (string :tag "Title")))
Dave Love's avatar
Dave Love committed
802 803

(defcustom recentf-arrange-by-rules-min-items 0
804
  "Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
Dave Love's avatar
Dave Love committed
805 806 807 808 809
If the number of items in a sub-menu is less than this value the
corresponding sub-menu items are displayed in the main recent files
menu or in the `recentf-arrange-by-rule-others' sub-menu if
defined."
  :group 'recentf-filters
David Ponce's avatar
David Ponce committed
810
  :type 'number)
Dave Love's avatar
Dave Love committed
811 812

(defcustom recentf-arrange-by-rule-subfilter nil
813
  "Function called by a rule based filter to filter sub-menu elements.
814
A nil value means no filter.  See also `recentf-menu-filter'.
815
You can't use another rule based filter here."
Dave Love's avatar
Dave Love committed
816
  :group 'recentf-filters
Dave Love's avatar
Dave Love committed
817
  :type '(choice (const nil) function)
818 819 820 821 822
  :set (lambda (variable value)
         (when (memq value '(recentf-arrange-by-rule
                             recentf-arrange-by-mode
                             recentf-arrange-by-dir))
           (error "Recursive use of a rule based filter"))
David Ponce's avatar
David Ponce committed
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
         (set-default variable value)))

(defun recentf-match-rule (file)
  "Return the rule that match FILE."
  (let ((rules recentf-arrange-rules)
        match found)
    (while (and (not found) rules)
      (setq match (cdar rules))
      (when (stringp match)
        (setq match (list match)))
      (while (and match (not (string-match (car match) file)))
        (setq match (cdr match)))
      (if match
          (setq found (cons (caar rules) file))
        (setq rules (cdr rules))))
    found))
Dave Love's avatar
Dave Love committed
839 840

(defun recentf-arrange-by-rule (l)
Dave Love's avatar
Dave Love committed
841 842
  "Filter the list of menu-elements L.
Arrange them in sub-menus following rules in `recentf-arrange-rules'."
David Ponce's avatar
David Ponce committed
843 844
  (when recentf-arrange-rules
    (let (menus others menu file min count)
845
      ;; Put menu items into sub-menus as defined by rules.
846
      (dolist (elt l)
David Ponce's avatar
David Ponce committed
847 848 849 850 851 852 853 854 855 856 857
        (setq file (recentf-menu-element-value elt)
              menu (recentf-match-rule file))
        (while (functionp (car menu))
          (setq menu (funcall (car menu) (cdr menu))))
        (if (not (stringp (car menu)))
            (push elt others)
          (setq menu (or (assoc (car menu) menus)
                         (car (push (list (car menu)) menus))))
          (recentf-set-menu-element-value
           menu (cons elt (recentf-menu-element-value menu)))))
      ;; Finalize each sub-menu:
858 859 860 861 862 863
      ;; - truncate it depending on the value of
      ;;   `recentf-arrange-by-rules-min-items',
      ;; - replace %d by the number of menu items,
      ;; - apply `recentf-arrange-by-rule-subfilter' to menu items.
      (setq min (if (natnump recentf-arrange-by-rules-min-items)
                    recentf-arrange-by-rules-min-items 0)
David Ponce's avatar
David Ponce committed
864 865 866 867 868 869 870 871 872 873 874 875
            l nil)
      (dolist (elt menus)
        (setq menu (recentf-menu-element-value elt)
              count (length menu))
        (if (< count min)
            (setq others (nconc menu others))
          (recentf-set-menu-element-item
           elt (format (recentf-menu-element-item elt) count))
          (recentf-set-menu-element-value
           elt (recentf-apply-menu-filter
                recentf-arrange-by-rule-subfilter (nreverse menu)))
          (push elt l)))
876
      ;; Add the menu items remaining in the `others' bin.
David Ponce's avatar
David Ponce committed
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
      (when (setq others (nreverse others))
        (setq l (nconc
                 l
                 ;; Put items in an sub menu.
                 (if (stringp recentf-arrange-by-rule-others)
                     (list
                      (recentf-make-menu-element
                       (format recentf-arrange-by-rule-others
                               (length others))
                       (recentf-apply-menu-filter
                        recentf-arrange-by-rule-subfilter others)))
                   ;; Append items to the main menu.
                   (recentf-apply-menu-filter
                    recentf-arrange-by-rule-subfilter others)))))))
  l)
892 893 894

;;; Predefined rule based menu filters
;;
David Ponce's avatar
David Ponce committed
895 896 897 898
(defun recentf-indirect-mode-rule (file)
  "Apply a second level `auto-mode-alist' regexp to FILE."
  (recentf-match-rule (substring file 0 (match-beginning 0))))

Dave Love's avatar
Dave Love committed
899
(defun recentf-build-mode-rules ()
900 901
  "Convert `auto-mode-alist' to menu filter rules.
Rules obey `recentf-arrange-rules' format."
Dave Love's avatar
Dave Love committed
902
  (let ((case-fold-search recentf-case-fold-search)
903 904 905 906
        regexp rule-name rule rules)
    (dolist (mode auto-mode-alist)
      (setq regexp (car mode)
            mode   (cdr mode))
907 908 909 910 911 912 913
      (when mode
        (cond
         ;; Build a special "strip suffix" rule from entries of the
         ;; form (REGEXP FUNCTION NON-NIL).  Notice that FUNCTION is
         ;; ignored by the menu filter.  So in some corner cases a
         ;; wrong mode could be guessed.
         ((and (consp mode) (cadr mode))
David Ponce's avatar
David Ponce committed
914
          (setq rule-name 'recentf-indirect-mode-rule))
915 916 917 918 919 920
         ((and mode (symbolp mode))
          (setq rule-name (symbol-name mode))
          (if (string-match "\\(.*\\)-mode$" rule-name)
              (setq rule-name (match-string 1 rule-name)))
          (setq rule-name (concat rule-name " (%d)"))))
        (setq rule (assoc rule-name rules))
Dave Love's avatar
Dave Love committed
921 922
        (if rule
            (setcdr rule (cons regexp (cdr rule)))
923
          (push (list rule-name regexp) rules))))
Dave Love's avatar
Dave Love committed
924 925 926
    ;; It is important to preserve auto-mode-alist order
    ;; to ensure the right file <-> mode association
    (nreverse rules)))
Juanma Barranquero's avatar
Juanma Barranquero committed
927

Dave Love's avatar
Dave Love committed
928
(defun recentf-arrange-by-mode (l)
929
  "Split the list of menu-elements L into sub-menus by major mode."
Dave Love's avatar
Dave Love committed
930 931 932 933 934
  (let ((recentf-arrange-rules (recentf-build-mode-rules))
        (recentf-arrange-by-rule-others "others (%d)"))
    (recentf-arrange-by-rule l)))

(defun recentf-file-name-nondir (l)
935
  "Filter the list of menu-elements L to show filenames sans directory.
Dave Love's avatar
Dave Love committed
936 937
This simplified version of `recentf-show-basenames' does not handle
duplicates.  It is used by `recentf-arrange-by-dir' as its
Dave Love's avatar
Dave Love committed
938
`recentf-arrange-by-rule-subfilter'."
939 940 941 942
  (mapcar #'(lambda (e)
              (recentf-make-menu-element
               (file-name-nondirectory (recentf-menu-element-value e))
               (recentf-menu-element-value e)))
Dave Love's avatar
Dave Love committed
943 944
          l))

David Ponce's avatar
David Ponce committed
945 946 947 948
(defun recentf-dir-rule (file)
  "Return as a sub-menu, the directory FILE belongs to."
  (cons (file-name-directory file) file))

Dave Love's avatar
Dave Love committed
949
(defun recentf-arrange-by-dir (l)
950
  "Split the list of menu-elements L into sub-menus by directory."
David Ponce's avatar
David Ponce committed
951
  (let ((recentf-arrange-rules '((recentf-dir-rule . ".*")))
Dave Love's avatar
Dave Love committed
952 953
        (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
        recentf-arrange-by-rule-others)
David Ponce's avatar
David Ponce committed
954
    (recentf-arrange-by-rule l)))
955

David Ponce's avatar
David Ponce committed
956
;;; Menu of menu filters
957
;;
David Ponce's avatar
David Ponce committed
958 959
(defvar recentf-filter-changer-current nil
  "Current filter used by `recentf-filter-changer'.")
Dave Love's avatar
Dave Love committed
960 961 962

(defcustom recentf-filter-changer-alist
  '(
David Ponce's avatar
David Ponce committed
963 964 965
    (recentf-arrange-by-mode . "Grouped by Mode")
    (recentf-arrange-by-dir  . "Grouped by Directory")
    (recentf-arrange-by-rule . "Grouped by Custom Rules")
Dave Love's avatar
Dave Love committed
966
    )
967
  "List of filters managed by `recentf-filter-changer'.
968 969 970
Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is
the filter function, and LABEL is the menu item displayed to select
that filter."
Dave Love's avatar
Dave Love committed
971 972
  :group 'recentf-filters
  :type '(repeat (cons function string))
973
  :set (lambda (variable value)
David Ponce's avatar
David Ponce committed
974 975
         (setq recentf-filter-changer-current nil)
         (set-default variable value)))
976

David Ponce's avatar
David Ponce committed
977 978
(defun recentf-filter-changer-select (filter)
  "Select FILTER as the current menu filter.
979
See `recentf-filter-changer'."
David Ponce's avatar
David Ponce committed
980
  (setq recentf-filter-changer-current filter))
Juanma Barranquero's avatar
Juanma Barranquero committed
981

Dave Love's avatar
Dave Love committed
982
(defun recentf-filter-changer (l)
David Ponce's avatar
David Ponce committed
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
  "Manage a sub-menu of menu filters.
`recentf-filter-changer-alist' defines the filters in the menu.
Filtering of L is delegated to the selected filter in the menu."
  (unless recentf-filter-changer-current
    (setq recentf-filter-changer-current
          (caar recentf-filter-changer-alist)))
  (if (not recentf-filter-changer-current)
      l
    (setq recentf-menu-filter-commands
          (list
           `("Show files"
             ,@(mapcar
                #'(lambda (f)
                    `[,(cdr f)
                      (setq recentf-filter-changer-current ',(car f))
                      ;;:active t
                      :style radio ;;radio Don't work with GTK :-(
                      :selected (eq recentf-filter-changer-current
                                    ',(car f))
                      ;;:help ,(cdr f)
                      ])
                recentf-filter-changer-alist))))
    (recentf-apply-menu-filter recentf-filter-changer-current l)))
1006

David Ponce's avatar
David Ponce committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
;;; Hooks
;;
(defun recentf-track-opened-file ()
  "Insert the name of the file just opened or written into the recent list."
  (and buffer-file-name
       (recentf-add-file buffer-file-name))
  ;; Must return nil because it is run from `write-file-functions'.
  nil)

(defun recentf-track-closed-file ()
  "Update the recent list when a buffer is killed.
That is, remove a non kept file from the recent list."
  (and buffer-file-name
       (recentf-remove-if-non-kept buffer-file-name)))

(defconst recentf-used-hooks
  '(
    (find-file-hook       recentf-track-opened-file)
    (write-file-functions recentf-track-opened-file)
    (kill-buffer-hook     recentf-track-closed-file)
    (kill-emacs-hook      recentf-save-list)
    )
  "Hooks used by recentf.")

;;; Commands
;;

1034 1035
;;; Common dialog stuff
;;
Dave Love's avatar
Dave Love committed
1036
(defun recentf-cancel-dialog (&rest ignore)
Dave Love's avatar
Dave Love committed
1037
  "Cancel the current dialog.
1038
IGNORE arguments."
Dave Love's avatar
Dave Love committed
1039 1040
  (interactive)
  (kill-buffer (current-buffer))
1041
  (message "Dialog canceled"))
Dave Love's avatar
Dave Love committed
1042

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
(defun recentf-dialog-goto-first (widget-type)
  "Move the cursor to the first WIDGET-TYPE in current dialog.
Go to the beginning of buffer if not found."
  (goto-char (point-min))
  (condition-case nil
      (let (done)
        (widget-move 1)
        (while (not done)
          (if (eq widget-type (widget-type (widget-at (point))))
              (setq done t)
            (widget-move 1))))
1054 1055
    (error
     (goto-char (point-min)))))
1056

1057
(defvar recentf-dialog-mode-map
1058
  (let ((km (copy-keymap recentf--shortcuts-keymap)))
1059
    (set-keymap-parent km widget-keymap)
1060
    (define-key km "q" 'recentf-cancel-dialog)
David Ponce's avatar
David Ponce committed
1061
    (define-key km [follow-link] "\C-m")
1062 1063
    km)
  "Keymap used in recentf dialogs.")
Dave Love's avatar
Dave Love committed
1064

1065
(define-derived-mode recentf-dialog-mode nil "recentf-dialog"
1066
  "Major mode of recentf dialogs.
Dave Love's avatar
Dave Love committed
1067

1068
\\{recentf-dialog-mode-map}"
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
  :syntax-table nil
  :abbrev-table nil
  (setq truncate-lines t))

(defmacro recentf-dialog (name &rest forms)
  "Show a dialog buffer with NAME, setup with FORMS."
  (declare (indent 1) (debug t))
  `(with-current-buffer (get-buffer-create ,name)
    ;; Cleanup buffer
    (let ((inhibit-read-only t)
          (ol (overlay-lists)))
      (mapc 'delete-overlay (car ol))
      (mapc 'delete-overlay (cdr ol))
      (erase-buffer))
    (recentf-dialog-mode)
    ,@forms
    (widget-setup)
    (switch-to-buffer (current-buffer))))
1087

1088 1089 1090 1091 1092 1093
;;; Edit list dialog
;;
(defvar recentf-edit-list nil)

(defun recentf-edit-list-select (widget &rest ignore)
  "Toggle a file selection based on the checkbox WIDGET state.
1094
IGNORE other arguments."
1095 1096 1097 1098 1099 1100