Bug fixes.
[org-mode.git] / CONTRIB / lisp / org-iswitchb.el
blob93413b42b30c0caf4b39dc634f5acb3b4e130fb2
1 ;;; org-iswitchb.el --- use iswitchb to select Org buffer
2 ;;
3 ;; Copyright 2007 2008 Bastien Guerry
4 ;;
5 ;; Author: bzg AT altern DOT org
6 ;; Version: 0.1
7 ;; Keywords: Org buffer
8 ;; URL: http://www.cognition.ens.fr/~guerry/u/org-iswitchb.el
9 ;;
10 ;; This file is NOT part of GNU Emacs.
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; if not, write to the Free Software
24 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;; Commentary:
28 ;; Put this file into your load-path and the following into your ~/.emacs:
29 ;; (require 'org-iswitchb)
31 ;;; Code:
33 (defun org-iswitchb (&optional arg)
34 "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
35 With a prefix argument, restrict available to files.
36 With two prefix arguments, restrict available buffers to agenda files.
38 Due to some yet unresolved reason, global function
39 `iswitchb-mode' needs to be active for this function to work."
40 (interactive "P")
41 (eval-when-compile
42 (require 'iswitchb))
43 (let ((enabled iswitchb-mode) blist)
44 (or enabled (iswitchb-mode 1))
45 (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
46 ((equal arg '(16)) (org-buffer-list 'agenda))
47 (t (org-buffer-list))))
48 (unwind-protect
49 (let ((iswitchb-make-buflist-hook
50 (lambda ()
51 (setq iswitchb-temp-buflist
52 (mapcar 'buffer-name blist)))))
53 (switch-to-buffer
54 (iswitchb-read-buffer
55 "Switch-to: " nil t))
56 (or enabled (iswitchb-mode -1))))))
58 (defun org-buffer-list (&optional predicate tmp)
59 "Return a list of Org buffers.
60 PREDICATE can be either 'export, 'files or 'agenda.
62 'export restrict the list to Export buffers.
63 'files restrict the list to buffers visiting Org files.
64 'agenda restrict the list to buffers visiting agenda files.
66 If TMP is non-nil, don't include temporary buffers."
67 (let (filter blist)
68 (setq filter
69 (cond ((eq predicate 'files) "\.org$")
70 ((eq predicate 'export) "\*Org .*Export")
71 (t "\*Org \\|\.org$")))
72 (setq blist
73 (mapcar
74 (lambda(b)
75 (let ((bname (buffer-name b))
76 (bfile (buffer-file-name b)))
77 (if (and (string-match filter bname)
78 (if (eq predicate 'agenda)
79 (member bfile
80 (mapcar (lambda(f) (file-truename f))
81 org-agenda-files)) t)
82 (if tmp (not (string-match "tmp" bname)) t)) b)))
83 (buffer-list)))
84 (delete nil blist)))
86 (provide 'org-iswitchb)
88 ;;; User Options, Variables
90 ;;; org-iswitchb.el ends here