]> code.delx.au - gnu-emacs/blobdiff - lisp/recentf.el
(normal-splash-screen, fancy-splash-screens-1): Add a reference to the Lisp
[gnu-emacs] / lisp / recentf.el
index a2392fb852c900b7decaa4649c96de73352d30c5..02e7e712399dc5da2e657d9e5fb46199f0d88097 100644 (file)
@@ -1,7 +1,7 @@
 ;;; recentf.el --- setup a menu of recently opened files
 
 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
-;;   2005 Free Software Foundation, Inc.
+;;   2005, 2006 Free Software Foundation, Inc.
 
 ;; Author: David Ponce <david@dponce.com>
 ;; Created: July 19 1999
@@ -46,9 +46,9 @@
 (defvar recentf-list nil
   "List of recently opened files.")
 
-(defvar recentf-data-cache nil
-  "Cache of data used to build the recentf menu.
-The menu is rebuilt when this data has changed.")
+(defsubst recentf-enabled-p ()
+  "Return non-nil if recentf mode is currently enabled."
+  (memq 'recentf-save-list kill-emacs-hook))
 \f
 ;;; Customization
 ;;
@@ -111,10 +111,13 @@ remote access."
 (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."
-  (when (featurep 'recentf)
-    ;; Unavailable until recentf has been loaded.
-    (recentf-clear-data))
-  (set-default variable value))
+  (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)))
 
 (defcustom recentf-menu-title "Open Recent"
   "*Name of the recentf menu."
@@ -142,14 +145,12 @@ If nil add it at end of menu (see also `easy-menu-add-item')."
   "*Function to invoke with a filename item of the recentf menu.
 The default is to call `find-file' to edit the selected file."
   :group 'recentf
-  :type 'function
-  :set 'recentf-menu-customization-changed)
+  :type 'function)
 
 (defcustom recentf-max-menu-items 10
   "*Maximum number of items in the recentf menu."
   :group 'recentf
-  :type 'integer
-  :set 'recentf-menu-customization-changed)
+  :type 'integer)
 
 (defcustom recentf-menu-filter nil
   "*Function used to filter files displayed in the recentf menu.
@@ -182,7 +183,7 @@ A nil value means no filter.  The following functions are predefined:
 - `recentf-arrange-by-dir'
     Show a sub-menu for each directory.
 - `recentf-filter-changer'
-    Manage a ring of filters.
+    Manage a menu of filters.
 
 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
@@ -203,14 +204,18 @@ elements (see `recentf-make-menu-element' for menu element form)."
                 (function-item recentf-arrange-by-mode)
                 (function-item recentf-arrange-by-dir)
                 (function-item recentf-filter-changer)
-                function)
-  :set 'recentf-menu-customization-changed)
+                function))
+
+(defcustom recentf-menu-open-all-flag nil
+  "*Non-nil means to show an \"All...\" item in the menu.
+This item will replace the \"More...\" item."
+  :group 'recentf
+  :type 'boolean)
 
 (defcustom recentf-menu-append-commands-flag t
   "*Non-nil means to append command items to the menu."
   :group 'recentf
-  :type 'boolean
-  :set 'recentf-menu-customization-changed)
+  :type 'boolean)
 
 (define-obsolete-variable-alias 'recentf-menu-append-commands-p
                                 'recentf-menu-append-commands-flag
@@ -278,7 +283,6 @@ 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)
-
 \f
 ;;; Utilities
 ;;
@@ -325,7 +329,7 @@ specifies a maximum number of elements to insert.  By default insert
 the full list."
   (let ((value (symbol-value variable)))
     (if (atom value)
-        (insert (format "\n(setq %S %S)\n" variable value))
+        (insert (format "\n(setq %S '%S)\n" variable value))
       (when (and (integerp limit) (> limit 0))
         (setq value (recentf-trunc-list value limit)))
       (insert (format "\n(setq %S\n      '(" variable))
@@ -448,6 +452,25 @@ Return non-nil if F1 is less than F2."
 \f
 ;;; Menu building
 ;;
+(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.")
+
 (defvar recentf-menu-items-for-commands
   (list
    ["Cleanup list"
@@ -548,48 +571,88 @@ menu-elements (no sub-menu)."
         (nconc l others))
     l))
 
-(defun recentf-make-menu-items ()
-  "Make menu items from the recent list."
+;; Count the number of assigned menu shortcuts.
+(defvar recentf-menu-shortcuts)
+
+(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."
   (setq recentf-menu-filter-commands nil)
-  (let ((file-items
-         (mapcar 'recentf-make-menu-item
-                 (recentf-apply-menu-filter
-                  recentf-menu-filter
-                  (recentf-menu-elements recentf-max-menu-items)))))
-    (append (or file-items (list ["No files" t
-                                  :help "No recent file to open"
-                                  :active nil]))
-            (and (< recentf-max-menu-items (length recentf-list))
-                 (list ["More..." recentf-open-more-files
-                        :help "Open files that are not in the menu"
-                        :active t]))
-            (and recentf-menu-filter-commands
-                 (cons "---"
-                       recentf-menu-filter-commands))
-            (and recentf-menu-append-commands-flag
-                 (cons "---"
-                       recentf-menu-items-for-commands)))))
-
-(defsubst recentf-make-menu-item (elt)
+  (let* ((recentf-menu-shortcuts 0)
+         (file-items
+          (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)))
+
+(defun recentf-menu-value-shortcut (name)
+  "Return a shortcut digit for file NAME.
+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)
   "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))
-      (vector item (list recentf-menu-action value)
-              :help (concat "Open " value)
-              :active t))))
+      (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)))))
 
 (defsubst recentf-menu-bar ()
   "Return the keymap of the global menu bar."
   (lookup-key global-map [menu-bar]))
 
-(defun recentf-clear-data ()
-  "Clear data used to build the recentf menu.
-This forces a rebuild of the menu."
-  (easy-menu-remove-item (recentf-menu-bar)
-                         recentf-menu-path recentf-menu-title)
-  (setq recentf-data-cache nil))
+(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))
 \f
 ;;; Predefined menu filters
 ;;
@@ -695,19 +758,24 @@ Filenames are relative to the `default-directory'."
 ;;
 (defcustom recentf-arrange-rules
   '(
-    ("Elisp files (%d)" ".\\.el$")
-    ("Java files (%d)"  ".\\.java$")
-    ("C/C++ files (%d)" "c\\(pp\\)?$")
+    ("Elisp files (%d)" ".\\.el\\'")
+    ("Java files (%d)"  ".\\.java\\'")
+    ("C/C++ files (%d)" "c\\(pp\\)?\\'")
     )
   "*List of rules used by `recentf-arrange-by-rule' to build sub-menus.
 A rule is a pair (SUB-MENU-TITLE . MATCHER).  SUB-MENU-TITLE is the
 displayed title of the sub-menu where a '%d' `format' pattern is
 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
-MATCHER are added to the corresponding sub-menu."
+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."
   :group 'recentf-filters
-  :type '(repeat (cons string (repeat regexp)))
-  :set 'recentf-menu-customization-changed)
+  :type '(repeat (cons (choice string function)
+                       (repeat regexp))))
 
 (defcustom recentf-arrange-by-rule-others "Other files (%d)"
   "*Title of the `recentf-arrange-by-rule' sub-menu.
@@ -717,8 +785,7 @@ 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."
   :group 'recentf-filters
   :type '(choice (const  :tag "Main menu" nil)
-                 (string :tag "Title"))
-  :set 'recentf-menu-customization-changed)
+                 (string :tag "Title")))
 
 (defcustom recentf-arrange-by-rules-min-items 0
   "*Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
@@ -727,8 +794,7 @@ 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
-  :type 'number
-  :set 'recentf-menu-customization-changed)
+  :type 'number)
 
 (defcustom recentf-arrange-by-rule-subfilter nil
   "*Function called by a rule based filter to filter sub-menu elements.
@@ -741,72 +807,82 @@ You can't use another rule based filter here."
                              recentf-arrange-by-mode
                              recentf-arrange-by-dir))
            (error "Recursive use of a rule based filter"))
-         (recentf-menu-customization-changed variable value)))
-
-(defun recentf-match-rule-p (matcher filename)
-  "Return non-nil if the rule specified by MATCHER match FILENAME.
-See `recentf-arrange-rules' for details on MATCHER."
-  (if (stringp matcher)
-      (string-match matcher filename)
-    (while (and (consp matcher)
-                (not (string-match (car matcher) filename)))
-      (setq matcher (cdr matcher)))
-    matcher))
+         (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))
 
 (defun recentf-arrange-by-rule (l)
   "Filter the list of menu-elements L.
 Arrange them in sub-menus following rules in `recentf-arrange-rules'."
-  (if (not recentf-arrange-rules)
-      l
-    (let ((menus (mapcar #'(lambda (r) (list (car r)))
-                         recentf-arrange-rules))
-          menu others min file rules elts count)
+  (when recentf-arrange-rules
+    (let (menus others menu file min count)
+      ;; Put menu items into sub-menus as defined by rules.
       (dolist (elt l)
-        (setq file  (recentf-menu-element-value elt)
-              rules recentf-arrange-rules
-              elts  menus
-              menu  nil)
-        (while (and (not menu) rules)
-          (when (recentf-match-rule-p (cdar rules) file)
-            (setq menu (car elts))
-            (recentf-set-menu-element-value
-             menu (cons elt (recentf-menu-element-value menu))))
-          (setq rules (cdr rules)
-                elts  (cdr elts)))
-        (unless menu
-          (push elt others)))
-
-      (setq l nil
-            min (if (natnump recentf-arrange-by-rules-min-items)
-                    recentf-arrange-by-rules-min-items 0))
-      (dolist (menu menus)
-        (when (setq elts (recentf-menu-element-value menu))
-          (setq count (length elts))
-          (if (< count min)
-              (setq others (nconc elts others))
-            (recentf-set-menu-element-item
-             menu (format (recentf-menu-element-item menu) count))
-            (recentf-set-menu-element-value
-             menu (recentf-apply-menu-filter
-                   recentf-arrange-by-rule-subfilter (nreverse elts)))
-            (push menu l))))
-
-      (if (and (stringp recentf-arrange-by-rule-others) others)
-          (nreverse
-           (cons
-            (recentf-make-menu-element
-             (format recentf-arrange-by-rule-others (length others))
-             (recentf-apply-menu-filter
-              recentf-arrange-by-rule-subfilter (nreverse others)))
-            l))
-        (nconc
-         (nreverse l)
-         (recentf-apply-menu-filter
-          recentf-arrange-by-rule-subfilter (nreverse others)))))
-    ))
+        (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:
+      ;; - 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)
+            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)))
+      ;; Add the menu items remaining in the `others' bin.
+      (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)
 \f
 ;;; Predefined rule based menu filters
 ;;
+(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))))
+
 (defun recentf-build-mode-rules ()
   "Convert `auto-mode-alist' to menu filter rules.
 Rules obey `recentf-arrange-rules' format."
@@ -815,12 +891,20 @@ Rules obey `recentf-arrange-rules' format."
     (dolist (mode auto-mode-alist)
       (setq regexp (car mode)
             mode   (cdr mode))
-      (when (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)")
-              rule (assoc rule-name rules))
+      (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))
+          (setq rule-name 'recentf-indirect-mode-rule))
+         ((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))
         (if rule
             (setcdr rule (cons regexp (cdr rule)))
           (push (list rule-name regexp) rules))))
@@ -834,21 +918,6 @@ Rules obey `recentf-arrange-rules' format."
         (recentf-arrange-by-rule-others "others (%d)"))
     (recentf-arrange-by-rule l)))
 
-(defun recentf-build-dir-rules (l)
-  "Convert directories in menu-elements L to menu filter rules.
-Rules obey `recentf-arrange-rules' format."
-  (let (dirs)
-    (mapcar #'(lambda (e)
-                (let ((dir (file-name-directory
-                            (recentf-menu-element-value e))))
-                  (or (recentf-string-member dir dirs)
-                      (push dir dirs))))
-            l)
-    (mapcar #'(lambda (d)
-                (cons (concat d " (%d)")
-                      (concat "\\`" d)))
-            (nreverse (sort dirs 'recentf-string-lessp)))))
-
 (defun recentf-file-name-nondir (l)
   "Filter the list of menu-elements L to show filenames sans directory.
 This simplified version of `recentf-show-basenames' does not handle
@@ -860,23 +929,27 @@ duplicates.  It is used by `recentf-arrange-by-dir' as its
                (recentf-menu-element-value e)))
           l))
 
+(defun recentf-dir-rule (file)
+  "Return as a sub-menu, the directory FILE belongs to."
+  (cons (file-name-directory file) file))
+
 (defun recentf-arrange-by-dir (l)
   "Split the list of menu-elements L into sub-menus by directory."
-  (let ((recentf-arrange-rules (recentf-build-dir-rules l))
+  (let ((recentf-arrange-rules '((recentf-dir-rule . ".*")))
         (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
         recentf-arrange-by-rule-others)
-    (nreverse (recentf-arrange-by-rule l))))
+    (recentf-arrange-by-rule l)))
 \f
-;;; Ring of menu filters
+;;; Menu of menu filters
 ;;
-(defvar recentf-filter-changer-state nil
-  "Used by `recentf-filter-changer' to hold its state.")
+(defvar recentf-filter-changer-current nil
+  "Current filter used by `recentf-filter-changer'.")
 
 (defcustom recentf-filter-changer-alist
   '(
-    (recentf-arrange-by-mode . "*Files by Mode*")
-    (recentf-arrange-by-dir  . "*Files by Directory*")
-    (recentf-arrange-by-rule . "*Files by User Rule*")
+    (recentf-arrange-by-mode . "Grouped by Mode")
+    (recentf-arrange-by-dir  . "Grouped by Directory")
+    (recentf-arrange-by-rule . "Grouped by Custom Rules")
     )
   "*List of filters managed by `recentf-filter-changer'.
 Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is
@@ -885,51 +958,66 @@ that filter."
   :group 'recentf-filters
   :type '(repeat (cons function string))
   :set (lambda (variable value)
-         (setq recentf-filter-changer-state nil)
-         (recentf-menu-customization-changed variable value)))
+         (setq recentf-filter-changer-current nil)
+         (set-default variable value)))
 
-(defun recentf-filter-changer-goto-next ()
-  "Go to the next filter available.
+(defun recentf-filter-changer-select (filter)
+  "Select FILTER as the current menu filter.
 See `recentf-filter-changer'."
-  (setq recentf-filter-changer-state (cdr recentf-filter-changer-state))
-  (recentf-clear-data))
+  (setq recentf-filter-changer-current filter))
 
-(defsubst recentf-filter-changer-get-current ()
-  "Get the current filter available.
-See `recentf-filter-changer'."
-  (unless recentf-filter-changer-state
-    (setq recentf-filter-changer-state recentf-filter-changer-alist))
-  (car recentf-filter-changer-state))
+(defun recentf-filter-changer (l)
+  "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)))
+\f
+;;; 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)
 
-(defsubst recentf-filter-changer-get-next ()
-  "Get the next filter available.
-See `recentf-filter-changer'."
-  ;; At this point the current filter is the first element of
-  ;; `recentf-filter-changer-state'.
-  (car (or (cdr recentf-filter-changer-state)
-           ;; There is no next element in
-           ;; `recentf-filter-changer-state', so loop back to the
-           ;; first element of `recentf-filter-changer-alist'.
-           recentf-filter-changer-alist)))
+(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)))
 
-(defun recentf-filter-changer (l)
-  "Manage a ring of menu filters.
-`recentf-filter-changer-alist' defines the filters in the ring.
-Filtering of L is delegated to the current filter in the ring.  A
-filter menu item is displayed allowing to dynamically activate the
-next filter in the ring.  If the filter ring is empty, L is left
-unchanged."
-  (let ((filter (recentf-filter-changer-get-current)))
-    (when filter
-      (setq l (recentf-apply-menu-filter (car filter) l)
-            filter (recentf-filter-changer-get-next))
-      (when filter
-        (setq recentf-menu-filter-commands
-              (list (vector (cdr filter)
-                            '(recentf-filter-changer-goto-next)
-                            t)))))
-    l))
+(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.")
 \f
+;;; Commands
+;;
+
 ;;; Common dialog stuff
 ;;
 (defun recentf-cancel-dialog (&rest ignore)
@@ -950,16 +1038,14 @@ Go to the beginning of buffer if not found."
           (if (eq widget-type (widget-type (widget-at (point))))
               (setq done t)
             (widget-move 1))))
-    (goto-char (point-min))))
+    (error
+     (goto-char (point-min)))))
 
 (defvar recentf-dialog-mode-map
-  (let ((km (make-sparse-keymap)))
+  (let ((km (copy-keymap recentf--shortcuts-keymap)))
     (set-keymap-parent km widget-keymap)
     (define-key km "q" 'recentf-cancel-dialog)
-    (define-key km [down-mouse-1] 'widget-button-click)
-    ;; Keys in reverse order of appearence in help.
-    (dolist (k '("0" "9" "8" "7" "6" "5" "4" "3" "2" "1"))
-      (define-key km k 'recentf-open-file-with-key))
+    (define-key km [follow-link] "\C-m")
     km)
   "Keymap used in recentf dialogs.")
 
@@ -986,54 +1072,6 @@ Go to the beginning of buffer if not found."
     (widget-setup)
     (switch-to-buffer (current-buffer))))
 \f
-;;; 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)))
-
-(defun recentf-update-menu ()
-  "Update the recentf menu from the current recent list."
-  (let ((cache (cons default-directory recentf-list)))
-    ;; Does nothing, if nothing has changed.
-    (unless (equal recentf-data-cache cache)
-      (setq recentf-data-cache cache)
-      (condition-case err
-          (easy-menu-add-item
-           (recentf-menu-bar) recentf-menu-path
-           (easy-menu-create-menu recentf-menu-title
-                                  (recentf-make-menu-items))
-           recentf-menu-before)
-        (error
-         (message "recentf update menu failed: %s"
-                  (error-message-string err)))))))
-
-(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)
-    (menu-bar-update-hook recentf-update-menu)
-    (kill-emacs-hook      recentf-save-list)
-    )
-  "Hooks used by recentf.")
-
-(defsubst recentf-enabled-p ()
-  "Return non-nil if recentf mode is currently enabled."
-  (memq 'recentf-update-menu menu-bar-update-hook))
-\f
-;;; Commands
-;;
-
 ;;; Edit list dialog
 ;;
 (defvar recentf-edit-list nil)
@@ -1057,13 +1095,14 @@ IGNORE arguments."
           (setq recentf-list (delq e recentf-list)
                 i (1+ i)))
         (kill-buffer (current-buffer))
-        (message "%S file(s) removed from the list" i)
-        (recentf-clear-data))
+        (message "%S file(s) removed from the list" i))
     (message "No file selected")))
 
 (defun recentf-edit-list ()
   "Show a dialog to delete selected files from the recent list."
   (interactive)
+  (unless recentf-list
+    (error "The list of recent files is empty"))
   (recentf-dialog (format "*%s - Edit list*" recentf-menu-title)
     (set (make-local-variable 'recentf-edit-list) nil)
     (widget-insert
@@ -1081,14 +1120,14 @@ Click on Cancel or type `q' to cancel.\n")
      'push-button
      :notify 'recentf-edit-list-validate
      :help-echo "Delete selected files from the recent list"
-      "Ok")
+     "Ok")
     (widget-insert " ")
     (widget-create
      'push-button
      :notify 'recentf-cancel-dialog
      "Cancel")
     (recentf-dialog-goto-first 'checkbox)))
-
+\f
 ;;; Open file dialog
 ;;
 (defun recentf-open-files-action (widget &rest ignore)
@@ -1126,7 +1165,7 @@ IGNORE other arguments."
            :button-prefix ""
            :button-suffix ""
            :button-face default
-           :format "%[%t%]\n"
+           :format "%[%t\n%]"
            :help-echo ,(concat "Open " (cdr menu-element))
            :action recentf-open-files-action
            ,(cdr menu-element))))
@@ -1158,6 +1197,8 @@ files to choose from.  It defaults to the whole recent list.
 If optional argument BUFFER-NAME is non-nil, it is a buffer name to
 use for the dialog.  It defaults to \"*`recentf-menu-title'*\"."
   (interactive)
+  (unless (or files recentf-list)
+    (error "There is no recent file to open"))
   (recentf-dialog (or buffer-name (format "*%s*" recentf-menu-title))
     (widget-insert "Click on a file"
                    (if recentf-show-file-shortcuts-flag
@@ -1178,30 +1219,29 @@ use for the dialog.  It defaults to \"*`recentf-menu-title'*\"."
      "Cancel")
     (recentf-dialog-goto-first 'link)))
 
-(defun recentf-open-file-with-key (n)
-  "Open the recent file with the shortcut numeric key N.
-N must be a valid digit.
-`1' opens the first file, `2' the second file, ... `9' the ninth file.
-`0' opens the tenth file."
-  (interactive
-   (list
-    (let ((n (string-to-number (this-command-keys))))
-      (cond
-       ((zerop n) 10)
-       ((and (> n 0) (< n 10)) n)
-       ((error "Invalid digit key %d" n))))))
-  (when recentf--files-with-key
-    (let ((file (nth (1- n) recentf--files-with-key)))
-      (unless file (error "Not that many recent files"))
-      (kill-buffer (current-buffer))
-      (funcall recentf-menu-action file))))
-
 (defun recentf-open-more-files ()
   "Show a dialog to open a recent file that is not in the menu."
   (interactive)
   (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
                       (format "*%s - More*" recentf-menu-title)))
 
+(defun recentf-open-most-recent-file (&optional n)
+  "Open the Nth most recent file.
+Optional argument N must be a valid digit number.  It defaults to 1.
+1 opens the most recent file, 2 the second most recent one, etc..
+0 opens the tenth most recent file."
+  (interactive "p")
+  (cond
+   ((zerop n) (setq n 10))
+   ((and (> n 0) (< n 10)))
+   ((error "Recent file number out of range [0-9], %d" n)))
+  (let ((file (nth (1- n) (or recentf--files-with-key recentf-list))))
+    (unless file (error "Not that many recent files"))
+    ;; Close the open files dialog.
+    (when recentf--files-with-key
+      (kill-buffer (current-buffer)))
+    (funcall recentf-menu-action file)))
+\f
 ;;; Save/load/cleanup the recent list
 ;;
 (defconst recentf-save-file-header
@@ -1224,7 +1264,7 @@ Write data into the file specified by `recentf-save-file'."
         (set-buffer-file-coding-system recentf-save-file-coding-system)
         (insert (format recentf-save-file-header (current-time-string)))
         (recentf-dump-variable 'recentf-list recentf-max-saved-items)
-        (recentf-dump-variable 'recentf-filter-changer-state)
+        (recentf-dump-variable 'recentf-filter-changer-current)
         (insert "\n\f\n;;; Local Variables:\n"
                 (format ";;; coding: %s\n" recentf-save-file-coding-system)
                 ";;; End:\n")
@@ -1265,6 +1305,11 @@ That is, remove duplicates, non-kept, and excluded files."
         (message "File %s removed from the recentf list" f)))
     (message "Cleaning up the recentf list...done (%d removed)" n)
     (setq recentf-list (nreverse newlist))))
+\f
+;;; The minor mode
+;;
+(defvar recentf-mode-map (make-sparse-keymap)
+  "Keymap to use in recentf mode.")
 
 ;;;###autoload
 (define-minor-mode recentf-mode
@@ -1273,15 +1318,20 @@ With prefix argument ARG, turn on if positive, otherwise off.
 Returns non-nil if the new state is enabled.
 
 When recentf mode is enabled, it maintains a menu for visiting files
-that were operated on recently."
+that were operated on recently.
+
+\\{recentf-mode-map}"
   :global t
   :group 'recentf
+  :keymap recentf-mode-map
   (unless (and recentf-mode (recentf-enabled-p))
     (if recentf-mode
-        (recentf-load-list)
+        (progn
+          (recentf-load-list)
+          (recentf-show-menu))
+      (recentf-hide-menu)
       (recentf-save-list))
     (recentf-auto-cleanup)
-    (recentf-clear-data)
     (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
       (dolist (hook recentf-used-hooks)
         (apply hook-setup hook)))