From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: John Wiegley Newsgroups: gmane.emacs.devel Subject: Patch for "virtual buffers" in iswitchb Date: Sun, 18 Apr 2004 04:13:37 -0700 Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1082287636 4773 80.91.224.253 (18 Apr 2004 11:27:16 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 18 Apr 2004 11:27:16 +0000 (UTC) Cc: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Sun Apr 18 13:27:09 2004 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1BFAS5-00032p-00 for ; Sun, 18 Apr 2004 13:27:09 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1BFAS4-0000Lx-00 for ; Sun, 18 Apr 2004 13:27:09 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.30) id 1BFAQn-0007Tb-DA for emacs-devel@quimby.gnus.org; Sun, 18 Apr 2004 07:25:49 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.30) id 1BFANg-0005il-Gk for emacs-devel@gnu.org; Sun, 18 Apr 2004 07:22:36 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.30) id 1BFAFn-0002Vr-1C for emacs-devel@gnu.org; Sun, 18 Apr 2004 07:14:58 -0400 Original-Received: from [204.251.10.90] (helo=pop.directnic.com) by monty-python.gnu.org with smtp (Exim 4.30) id 1BFAFZ-0002PG-G3 for emacs-devel@gnu.org; Sun, 18 Apr 2004 07:14:13 -0400 Original-Received: by pop.directnic.com (iris/0.153:492879); 18 Apr 2004 11:14:10 +0000 X-Iris-Host: 1148748291/adsl-68-120-130-3.dsl.snfc21.pacbell.net Original-Received: from adsl-68-120-130-3.dsl.snfc21.pacbell.net (EHLO Majnun.local) (68.120.130.3) by pop.directnic.com (iris/0.153:492879/relay) with ESMTP for ; 18 Apr 2004 11:14:10 +0000 Original-Received: by Majnun.local (Postfix, from userid 501) id ACDF62278A1; Sun, 18 Apr 2004 04:13:38 -0700 (PDT) Original-To: Stephen Eglen User-Agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (darwin) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.4 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:21840 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:21840 Haven't signed my general papers yet, and would like some review on this anyway: The following patch adds a feature to iswitchb (off by default) that I call "virtual buffers". Basically, when a buffer visiting a file is killed, iswitchb remembers the name and location of that file. If the user then types a search string and fails to match a living buffer, iswitchb will consult the list of remembered ones, and allow the user to match against those names. Fontification is changed to reflect that these are "virtual" matches. If a virtual buffer is selected, the buffer is resurrected by visiting the last known path associated with that buffer name. The motivation for this patch is that many of the files I open using C-x C-f are being reopened. Combined with a module like session.el, virtual buffers allow my work environment to become more predictive. John Index: iswitchb.el =================================================================== RCS file: /cvsroot/emacs/emacs/lisp/iswitchb.el,v retrieving revision 1.48 diff -w -U3 -r1.48 iswitchb.el --- iswitchb.el 18 Apr 2004 10:31:29 -0000 1.48 +++ iswitchb.el 18 Apr 2004 11:08:53 -0000 @@ -305,6 +305,20 @@ :type 'integer :group 'iswitchb) +(defcustom iswitchb-use-virtual-buffers nil + "*If non-nil, refer to past buffers when none match." + :type 'boolean + :set (function + (lambda (sym value) + (if value + (add-hook 'kill-buffer-hook 'iswitchb-remember-file) + (remove-hook 'kill-buffer-hook 'iswitchb-remember-file)) + (set sym value))) + :group 'iswitchb) + +(defvar iswitchb-virtual-buffer-alist nil) +(defvar iswitchb-virtual-buffers nil) + (defcustom iswitchb-cannot-complete-hook 'iswitchb-completion-help "*Hook run when `iswitchb-complete' can't complete any more. The most useful values are `iswitchb-completion-help', which pops up a @@ -613,7 +627,17 @@ (if (and (not (eq iswitchb-exit 'usefirst)) (get-buffer iswitchb-final-text)) ;; This happens for example if the buffer was chosen with the mouse. - (setq iswitchb-matches (list iswitchb-final-text))) + (setq iswitchb-matches (list iswitchb-final-text) + iswitchb-virtual-buffers nil)) + + (if (and iswitchb-use-virtual-buffers + (not (iswitchb-existing-buffer-p)) + iswitchb-virtual-buffers) + (let ((virt (assoc (car iswitchb-virtual-buffers) + iswitchb-virtual-buffer-alist))) + (find-file-noselect (cdr virt)) + (setq iswitchb-matches (list (car virt)) + iswitchb-virtual-buffers nil))) ;; Handling the require-match must be done in a better way. (if (and require-match (not (iswitchb-existing-buffer-p))) @@ -833,7 +857,8 @@ (setq iswitchb-matches (let* ((buflist iswitchb-buflist)) (iswitchb-get-matched-buffers iswitchb-text iswitchb-regexp - buflist))))) + buflist)) + iswitchb-virtual-buffers nil))) (defun iswitchb-get-matched-buffers (regexp &optional string-format buffer-list) @@ -1192,6 +1217,25 @@ contents (not minibuffer-completion-confirm))))))) +(defun iswitchb-remember-file () + (if (and buffer-file-name + (file-exists-p buffer-file-name)) + (let* ((name (file-name-nondirectory buffer-file-name)) + (virt (assoc name iswitchb-virtual-buffer-alist))) + (if virt + (setcdr virt buffer-file-name) + (setq iswitchb-virtual-buffer-alist + (cons (cons name buffer-file-name) + iswitchb-virtual-buffer-alist)))))) + +(defun iswitchb-virtual-buffer-predicate (entry) + (let ((name (car entry))) + (and (or (and iswitchb-regexp (string-match iswitchb-text name)) + (and (null iswitchb-regexp) + (string-match (regexp-quote iswitchb-text) name))) + (not (iswitchb-ignore-buffername-p name)) + (file-exists-p (cdr entry))))) + (defun iswitchb-output-completion (com) (if (= (length com) most-len) ;; Most is one exact match, @@ -1225,6 +1273,23 @@ first) (setq comps (cons first (cdr comps))))) + ;; 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. + (if (and iswitchb-use-virtual-buffers (null comps) + iswitchb-virtual-buffer-alist) + (progn + (setq iswitchb-virtual-buffers + (all-completions "" iswitchb-virtual-buffer-alist + 'iswitchb-virtual-buffer-predicate) + comps iswitchb-virtual-buffers) + (let ((comp comps)) + (while comp + (put-text-property 0 (length (car comp)) + 'face 'font-lock-builtin-face + (car comp)) + (setq comp (cdr comp)))))) + (cond ((null comps) (format " %sNo match%s" open-bracket-determined close-bracket-determined))