Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / recentf.el
index b14997d..589e5eb 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, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: David Ponce <david@dponce.com>
 ;; Created: July 19 1999
@@ -9,10 +9,10 @@
 
 ;; This file is part of GNU Emacs.
 
-;; 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 2, or (at your
-;; option) any later version.
+;; 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.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -20,9 +20,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -46,9 +44,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
 ;;
@@ -63,16 +61,23 @@ You should define the options of your own filters in this group."
   :group 'recentf)
 
 (defcustom recentf-max-saved-items 20
-  "*Maximum number of items of the recent list that will be saved.
+  "Maximum number of items of the recent list that will be saved.
 A nil value means to save the whole list.
 See the command `recentf-save-list'."
   :group 'recentf
   :type 'integer)
 
 (defcustom recentf-save-file "~/.recentf"
-  "*File to save the recent list into."
+  "File to save the recent list into."
   :group 'recentf
-  :type 'file)
+  :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)))))
 
 (defcustom recentf-save-file-modes 384 ;; 0600
   "Mode bits of recentf save file, as an integer, or nil.
@@ -82,9 +87,9 @@ file.  See also the function `set-file-modes'."
   :group 'recentf
   :type '(choice (const :tag "Don't change" nil)
           integer))
-  
+
 (defcustom recentf-exclude nil
-  "*List of regexps and predicates for filenames excluded from the recent list.
+  "List of regexps and predicates for filenames excluded from the recent list.
 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
@@ -92,38 +97,49 @@ must return non-nil to exclude it."
   :group 'recentf
   :type '(repeat (choice regexp function)))
 
+(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
+   ((file-remote-p file nil t) (file-readable-p file))
+   ((file-remote-p file))
+   ((file-readable-p file))))
+
 (defcustom recentf-keep
-  '(file-readable-p)
-  "*List of regexps and predicates for filenames kept in the recent list.
+  '(recentf-keep-default-predicate)
+  "List of regexps and predicates for filenames kept in the recent list.
 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.
-The default is to keep readable files.
+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.
 A predicate is a function that is passed a filename to check and that
-must return non-nil to keep it.  For example, you can add the
-`file-remote-p' predicate in front of this list to keep remote file
-names in the recent list without checking their readability through a
-remote access."
+must return non-nil to keep it."
   :group 'recentf
   :type '(repeat (choice regexp function)))
 
 (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."
+  "Name of the recentf menu."
   :group 'recentf
   :type 'string
   :set 'recentf-menu-customization-changed)
 
 (defcustom recentf-menu-path '("File")
-  "*Path where to add the recentf menu.
+  "Path where to add the recentf menu.
 If nil add it at top level (see also `easy-menu-add-item')."
   :group 'recentf
   :type '(choice (const :tag "Top Level" nil)
@@ -131,7 +147,7 @@ If nil add it at top level (see also `easy-menu-add-item')."
   :set 'recentf-menu-customization-changed)
 
 (defcustom recentf-menu-before "Open File..."
-  "*Name of the menu before which the recentf menu will be added.
+  "Name of the menu before which the recentf menu will be added.
 If nil add it at end of menu (see also `easy-menu-add-item')."
   :group 'recentf
   :type '(choice (string :tag "Name")
@@ -139,20 +155,18 @@ If nil add it at end of menu (see also `easy-menu-add-item')."
   :set 'recentf-menu-customization-changed)
 
 (defcustom recentf-menu-action 'find-file
-  "*Function to invoke with a filename item of the recentf menu.
+  "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."
+  "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.
+  "Function used to filter files displayed in the recentf menu.
 A nil value means no filter.  The following functions are predefined:
 
 - `recentf-sort-ascending'
@@ -182,7 +196,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,28 +217,25 @@ 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.
+  "Non-nil means to show an \"All...\" item in the menu.
 This item will replace the \"More...\" item."
   :group 'recentf
-  :type 'boolean
-  :set 'recentf-menu-customization-changed)
-
-(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
                                 "22.1")
 
+(defcustom recentf-menu-append-commands-flag t
+  "Non-nil means to append command items to the menu."
+  :group 'recentf
+  :type 'boolean)
+
 (defcustom recentf-auto-cleanup 'mode
-  "*Define when to automatically cleanup the recent list.
+  "Define when to automatically cleanup the recent list.
 The following values can be set:
 
 - `mode'
@@ -257,13 +268,13 @@ cleanup the list."
            (recentf-auto-cleanup))))
 
 (defcustom recentf-initialize-file-name-history t
-  "*Non-nil means to initialize `file-name-history' with the recent list.
+  "Non-nil means to initialize `file-name-history' with the recent list.
 If `file-name-history' is not empty, do nothing."
   :group 'recentf
   :type  'boolean)
 
 (defcustom recentf-load-hook nil
-   "*Normal hook run at end of loading the `recentf' package."
+   "Normal hook run at end of loading the `recentf' package."
   :group 'recentf
   :type 'hook)
 
@@ -331,7 +342,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))
@@ -576,35 +587,40 @@ menu-elements (no sub-menu)."
 ;; Count the number of assigned menu shortcuts.
 (defvar recentf-menu-shortcuts)
 
-(defun recentf-make-menu-items ()
-  "Make menu items from the recent list."
+(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* ((recentf-menu-shortcuts 0)
          (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]))
-            (if recentf-menu-open-all-flag
-                (list ["All..." recentf-open-files
-                       :help "Open recent files through a dialog"
-                       :active t])
-              (and (< recentf-max-menu-items (length recentf-list))
-                   (list ["More..." recentf-open-more-files
-                          :help "Open files not in the menu through a dialog"
-                          :active t])))
-            (and recentf-menu-filter-commands
-                 (cons "---"
-                       recentf-menu-filter-commands))
-            (and recentf-menu-append-commands-flag
-                 (cons "---"
-                       recentf-menu-items-for-commands)))))
+          (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 shorcut digit for file 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))
@@ -639,12 +655,17 @@ Return nil if file NAME is not one of the ten more recent."
   "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
 ;;
@@ -750,43 +771,46 @@ 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.
+  "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.
+  "Title of the `recentf-arrange-by-rule' sub-menu.
 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."
   :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.
+  "Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
 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
-  :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.
+  "Function called by a rule based filter to filter sub-menu elements.
 A nil value means no filter.  See also `recentf-menu-filter'.
 You can't use another rule based filter here."
   :group 'recentf-filters
@@ -796,72 +820,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."
@@ -870,12 +904,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))))
@@ -889,21 +931,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
@@ -915,75 +942,67 @@ 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'.
+  "List of filters managed by `recentf-filter-changer'.
 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."
   :group 'recentf-filters
   :type '(repeat (cons function string))
   :set (lambda (variable value)
-         (setq recentf-filter-changer-state nil)
-         (recentf-menu-customization-changed variable value)))
-
-(defun recentf-filter-changer-goto-next ()
-  "Go to the next filter available.
-See `recentf-filter-changer'."
-  (setq recentf-filter-changer-state (cdr recentf-filter-changer-state))
-  (recentf-clear-data))
-
-(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))
+         (setq recentf-filter-changer-current nil)
+         (set-default variable value)))
 
-(defsubst recentf-filter-changer-get-next ()
-  "Get the next filter available.
+(defun recentf-filter-changer-select (filter)
+  "Select FILTER as the current menu filter.
 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)))
+  (setq recentf-filter-changer-current filter))
 
 (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))
+  "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
 ;;
@@ -1000,35 +1019,14 @@ 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
 ;;
@@ -1053,7 +1051,8 @@ 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 (copy-keymap recentf--shortcuts-keymap)))
@@ -1109,13 +1108,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
@@ -1178,7 +1178,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))))
@@ -1210,6 +1210,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
@@ -1275,10 +1277,10 @@ 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)
-        (insert "\n\f\n;;; Local Variables:\n"
-                (format ";;; coding: %s\n" recentf-save-file-coding-system)
-                ";;; End:\n")
+        (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")
         (write-file (expand-file-name recentf-save-file))
         (when recentf-save-file-modes
           (set-file-modes recentf-save-file recentf-save-file-modes))
@@ -1329,18 +1331,18 @@ 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.
-
-\\{recentf-mode-map}"
+that were operated on recently."
   :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)))