changeset 65894:e0cf1ed38749

(recentf-menu-open-all-flag): New option. (recentf-digit-shortcut-command-name): New function. (recentf--shortcuts-keymap): New variable. (recentf-menu-shortcuts): New variable. (recentf-make-menu-items): Initialize it. Replace the "More..." menu item by "All...", if `recentf-menu-open-all-flag' is non-nil. (recentf-menu-value-shortcut): New function. (recentf-make-menu-item): Use it. No more in-lined. (recentf-dialog-mode-map): Base on `recentf--shortcuts-keymap'. (recentf-open-most-recent-file): Rename from `recentf-open-file-with-key'. Don't depend on key binding. (recentf-mode-map): New variable. (recentf-mode): Use it.
author David Ponce <david@dponce.com>
date Fri, 07 Oct 2005 07:52:31 +0000
parents ae7e19399e2d
children 1083f8e63cc0
files lisp/recentf.el
diffstat 1 files changed, 95 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/recentf.el	Fri Oct 07 07:40:35 2005 +0000
+++ b/lisp/recentf.el	Fri Oct 07 07:52:31 2005 +0000
@@ -206,6 +206,13 @@
                 function)
   :set 'recentf-menu-customization-changed)
 
+(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
+  :set 'recentf-menu-customization-changed)
+
 (defcustom recentf-menu-append-commands-flag t
   "*Non-nil means to append command items to the menu."
   :group 'recentf
@@ -278,7 +285,6 @@
 used as shortcuts to open the Nth file."
   :group 'recentf
   :type 'boolean)
-
 
 ;;; Utilities
 ;;
@@ -448,6 +454,25 @@
 
 ;;; 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,21 +573,29 @@
         (nconc l others))
     l))
 
+;; Count the number of assigned menu shortcuts.
+(defvar recentf-menu-shortcuts)
+
 (defun recentf-make-menu-items ()
   "Make menu items from the recent list."
   (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)))))
+  (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]))
-            (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]))
+            (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))
@@ -570,15 +603,37 @@
                  (cons "---"
                        recentf-menu-items-for-commands)))))
 
-(defsubst recentf-make-menu-item (elt)
+(defun recentf-menu-value-shortcut (name)
+  "Return a shorcut 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."
@@ -953,13 +1008,10 @@
     (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))
     km)
   "Keymap used in recentf dialogs.")
 
@@ -1081,7 +1133,7 @@
      'push-button
      :notify 'recentf-edit-list-validate
      :help-echo "Delete selected files from the recent list"
-      "Ok")
+     "Ok")
     (widget-insert " ")
     (widget-create
      'push-button
@@ -1178,30 +1230,29 @@
      "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)))
+
 ;;; Save/load/cleanup the recent list
 ;;
 (defconst recentf-save-file-header
@@ -1266,6 +1317,9 @@
     (message "Cleaning up the recentf list...done (%d removed)" n)
     (setq recentf-list (nreverse newlist))))
 
+(defvar recentf-mode-map (make-sparse-keymap)
+  "Keymap to use in recentf mode.")
+
 ;;;###autoload
 (define-minor-mode recentf-mode
   "Toggle recentf mode.
@@ -1273,9 +1327,12 @@
 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)