UseIswitchBuffer

Difference between revision 16 and current revision

Summary: Rollback to 2008-09-05 00:16 UTC

No diff available.

Keybindings

Something most IswitchBuffers users aren’t aware of is that you can hit C-k to kill the currently selected buffer.

Using Iswitch-Buffer Programmatically

 From: KinCho
 Subject: my-icompleting-read
 Newsgroups: gnu.emacs.sources
 Date: Tue, 09 Oct 2001 16:28:18 GMT

I used iswitchb for a while and really liked it. I began to explore to see if I can borrow iswitchb to make my scripts work like iswitchb as well. Well, it turned out iswitchb is coded in a way that makdes it really easy to borrow it to do regex-style completing-read:

(defun my-icompleting-read(prompt choices)
  (let ((iswitchb-make-buflist-hook
	 (lambda ()
	   (setq iswitchb-temp-buflist choices))))
    (iswitchb-read-buffer prompt)))

Another example. Two things. “nil t” to iswitchb-read-buffer requires a choice from the given list with no default. Using an flet is a way to work when there’s no hook variable. There is a hook variable.

(defvar interesting  (regexp-opt '(".c" ".h" "etc.")))
(defvar some-directory "/home/somewhere/etc/")

;; Making this more flexible is an exercise for the reader
(defun find-a-file (arg &optional non-selective)
  "Select files using substrings."
  (interactive "sFile: ")
  (let ((dir (expand-file-name some-directory))
        candidates)
    (flet ((file-match (file)
             (if non-selective
                 (string-match arg file)
                 (and
                  (string-match interesting file)
                  (string-match arg file)))))
      (setq candidates (delq nil (loop for file in (directory-files dir)
                                       collect (if (file-match file) file)))))
    (cond
      ((eq (length candidates) 1)
       (find-file (format "%s%s" dir (car candidates))))
      ((eq (length candidates) 0)
       (if non-selective
           (message "No such file!")
           (find-a-file arg t)))
      (t
       (flet ((iswitchb-make-buflist (default)
                (setq iswitchb-buflist candidates)))
         (find-file (format 
                     "%s%s" dir
                     (iswitchb-read-buffer "File: " nil t))))))))

I’m quite new to iswitchb. I’ve been after eliminating the need to confirm (with TAB or ret) the last left possibility.

I’ve started with this simple hack, which only displays the desired buffer, but in this way, i don’t need to look at the minibuffer (in some cases).

(defun iswitchb-post-command ()
  "Run after command in `iswitchb-buffer'."
  (iswitchb-exhibit)
  (if (= (length iswitchb-matches) 1)
      (display-buffer (car iswitchb-matches))))

another thing i like, is to have the freedom to decide to open the buffer in other-window/frame after i made the selection: Hence a new minibuffer exiting command:

(defun iswitchb-select-buffer-other-window ()
  "Select the buffer named by the prompt. But in another window."
  (interactive)
  (setq iswitchb-method 'otherwindow)
  (exit-minibuffer))

The functionality of iswitchb can also be used to provide a replacement for the usual behaviour of find-file. Instead of hitting Tab to bring up a buffer listing the possible file completions, a list of file completions is continuously updated in the minibuffer:

(defun exd-find-file ()
  "Use functionality from `iswitchb' as a replacement for `find-file'"
  (interactive)
  (find-file (exd-iswitchb-find-file "." (directory-files "."))))

(defun exd-iswitchb-find-file (dir file-list)
  "Use functionality from `iswitchb' to select a file for `find-file'.
If a directory is selected, enter that directory and generate a new
list from which to select a file."
  ;; sort the file list into directories first
  (setq file-list
        (sort file-list
              (lambda (elt-1 elt-2)
                (and (file-directory-p (concat dir "/" elt-1))
                     (not (file-directory-p (concat dir "/" elt-2)))))))
  ;; use iswitchb for minibuffer file list/completion magic
  (let* (resize-mini-windows
         (iswitchb-make-buflist-hook
          (lambda ()
            (setq iswitchb-temp-buflist file-list)))
         ;; get the selected file
         (selected-file (concat dir "/" (iswitchb-read-buffer
                                         (concat "Find File: "
                                                 (expand-file-name dir)
                                                 "/")))))
    ;; if the selected file is a directory, recurse, else return file
    (if (file-directory-p selected-file)
        (exd-iswitchb-find-file selected-file (directory-files selected-file))
      selected-file)))

See Also:

  • Icicles, and command ‘icicle-buffer’, which is similar to ‘iswitchb-buffer’ but provides some additional features. Also, Icicles treats all types of minibuffer input the same way: filenames, buffer names, commands, variables…everything. And it lets you use a regexp to match completions, if you like.
  • InteractivelyDoThings (ido), which implements regex selection for files, directory buffers etc…