changeset 107777:13c077500eb3

2010-04-04 John Wiegley <jwiegley@gmail.com> * ido.el (ido-use-virtual-buffers): New variable to indicate whether "virtual buffer" support is enabled for IDO. Essentially it works as follows: Say you are visiting a file and the buffer gets cleaned up by mignight.el. Later, you want to switch to that buffer, but find it's no longer open. With virtual buffers enabled, the buffer name stays in the buffer list (using the ido-virtual face, and always at the end), and if you select it, it opens the file back up again. This allows you to think less about whether recently opened files are still open or not. Most of the time you can quit Emacs, restart, and then switch to a file buffer that was previously open as if it still were. NOTE: This feature has been present in iswitchb for several years now, and I'm porting the same logic to IDO. (ido-virtual): Face used to indicate virtual buffers in the list. (ido-buffer-internal): If a buffer is chosen, and no such buffer exists, but a virtual buffer of that name does (which would be why it was in the list), recreate the buffer by reopening the file. (ido-make-buffer-list): If virtual buffers are being used, call `ido-add-virtual-buffers-to-list' before the make list hook. (ido-virtual-buffers): New variable which contains a copy of the current contents of the `recentf-list', albeit pared down for the sake of speed, and with proper faces applied. (ido-add-virtual-buffers-to-list): Using the `recentf-list', create a list of "virtual buffers" to present to the user in addition to the currently open set. Note that this logic could get rather slow if that list is too large. With the default `recentf-max-saved-items' of 200, there is little speed penalty.
author jwiegley@gmail.com
date Sun, 04 Apr 2010 02:55:19 -0400
parents cdab6ddd22d0
children a91f626531f7
files lisp/ChangeLog lisp/ido.el
diffstat 2 files changed, 85 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/ChangeLog	Sat Apr 03 16:10:14 2010 -0400
+++ b/lisp/ChangeLog	Sun Apr 04 02:55:19 2010 -0400
@@ -1,3 +1,33 @@
+2010-04-04  John Wiegley  <jwiegley@gmail.com>
+
+	* ido.el (ido-use-virtual-buffers): New variable to indicate
+	whether "virtual buffer" support is enabled for IDO.  Essentially
+	it works as follows: Say you are visiting a file and the buffer
+	gets cleaned up by mignight.el.  Later, you want to switch to that
+	buffer, but find it's no longer open.  With virtual buffers
+	enabled, the buffer name stays in the buffer list (using the
+	ido-virtual face, and always at the end), and if you select it, it
+	opens the file back up again.  This allows you to think less about
+	whether recently opened files are still open or not.  Most of the
+	time you can quit Emacs, restart, and then switch to a file buffer
+	that was previously open as if it still were.  NOTE: This feature
+	has been present in iswitchb for several years now, and I'm
+	porting the same logic to IDO.
+	(ido-virtual): Face used to indicate virtual buffers in the list.
+	(ido-buffer-internal): If a buffer is chosen, and no such buffer
+	exists, but a virtual buffer of that name does (which would be why
+	it was in the list), recreate the buffer by reopening the file.
+	(ido-make-buffer-list): If virtual buffers are being used, call
+	`ido-add-virtual-buffers-to-list' before the make list hook.
+	(ido-virtual-buffers): New variable which contains a copy of the
+	current contents of the `recentf-list', albeit pared down for the
+	sake of speed, and with proper faces applied.
+	(ido-add-virtual-buffers-to-list): Using the `recentf-list',
+	create a list of "virtual buffers" to present to the user in
+	addition to the currently open set.  Note that this logic could
+	get rather slow if that list is too large.  With the default
+	`recentf-max-saved-items' of 200, there is little speed penalty.
+
 2010-04-03  Stefan Monnier  <monnier@iro.umontreal.ca>
 
 	* font-lock.el: Require CL when compiling.
--- a/lisp/ido.el	Sat Apr 03 16:10:14 2010 -0400
+++ b/lisp/ido.el	Sun Apr 04 02:55:19 2010 -0400
@@ -774,6 +774,13 @@
   :type '(repeat string)
   :group 'ido)
 
+(defcustom ido-use-virtual-buffers nil
+  "If non-nil, refer to past buffers as well as existing ones.
+This feature relies upon the `recentf' package, which will be
+enabled if this variable is configured to a non-nil value."
+  :type 'boolean
+  :group 'ido)
+
 (defcustom ido-use-faces t
   "Non-nil means use ido faces to highlighting first match, only match and
 subdirs in the alternatives."
@@ -798,6 +805,10 @@
   "Face used by ido for highlighting subdirs in the alternatives."
   :group 'ido)
 
+(defface ido-virtual '((t (:inherit font-lock-builtin-face)))
+  "Face used by ido for matching virtual buffer names."
+  :group 'ido)
+
 (defface ido-indicator  '((((min-colors 88) (class color))
 				(:foreground "yellow1"
 				 :background "red1"
@@ -2155,7 +2166,8 @@
 	   (ido-directory-too-big nil)
 	   (require-match (confirm-nonexistent-file-or-buffer))
 	   (buf (ido-read-internal 'buffer (or prompt "Buffer: ") 'ido-buffer-history default
-				   require-match initial)))
+				   require-match initial))
+	   filename)
 
       ;; Choose the buffer name: either the text typed in, or the head
       ;; of the list of matches
@@ -2191,6 +2203,16 @@
 		 (point))))
 	  (ido-visit-buffer buf method t)))
 
+       ;; check for a virtual buffer reference
+       ((and ido-use-virtual-buffers ido-virtual-buffers
+	     (setq filename (assoc buf ido-virtual-buffers)))
+	(ido-visit-buffer (find-file-noselect (cdr filename)) method t))
+
+       ((and (eq ido-create-new-buffer 'prompt)
+	     (null require-match)
+	     (not (y-or-n-p (format "No buffer matching `%s', create one? " buf))))
+	nil)
+
        ;; buffer doesn't exist
        ((and (eq ido-create-new-buffer 'never)
 	     (null require-match))
@@ -3350,9 +3372,41 @@
 		(delete default ido-temp-list))
 	  (setq ido-temp-list
 		(cons default ido-temp-list))))
+    (if ido-use-virtual-buffers
+	(ido-add-virtual-buffers-to-list))
     (run-hooks 'ido-make-buffer-list-hook)
     ido-temp-list))
 
+(defvar ido-virtual-buffers nil)
+
+(defun ido-add-virtual-buffers-to-list ()
+  "Add recently visited files, and bookmark files, to the buffer list.
+This is to make them appear as if they were \"virtual buffers\"."
+  ;; If no buffers matched, and virtual buffers are being used, then
+  ;; consult the list of past visited files, to see if we can find
+  ;; the file which the user might thought was still open.
+  (setq ido-virtual-buffers nil)
+  (let ((head recentf-list) name)
+    (while head
+      (if (and (setq name (file-name-nondirectory (car head)))
+	       (null (get-file-buffer (car head)))
+	       (not (assoc name ido-virtual-buffers))
+	       (not (ido-ignore-item-p name ido-ignore-buffers))
+	       ;;(file-exists-p (car head))
+	       )
+	  (setq ido-virtual-buffers
+		(cons (cons name (car head)) ido-virtual-buffers)))
+      (setq head (cdr head))))
+  (when ido-virtual-buffers
+    (if ido-use-faces
+	(dolist (comp ido-virtual-buffers)
+	  (put-text-property 0 (length (car comp))
+			     'face 'ido-virtual
+			     (car comp))))
+    (setq ido-temp-list
+	  (nconc ido-temp-list
+		 (nreverse (mapcar #'car ido-virtual-buffers))))))
+
 (defun ido-make-choice-list (default)
   ;; Return the current list of choices.
   ;; If DEFAULT is non-nil, and corresponds to an element of choices,