(ibuffer-insert-filter-group): Fix spazz.
[emacs.git] / lisp / mh-e / mh-speed.el
blobca60b2f78401bc2b6f92afafc6bd7895c97a2286
1 ;;; mh-speed.el --- Speedbar interface for MH-E.
3 ;; Copyright (C) 2002 Free Software Foundation, Inc.
5 ;; Author: Satyaki Das <satyaki@theforce.stanford.edu>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
7 ;; Keywords: mail
8 ;; See: mh-e.el
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
28 ;; Future versions should only use flists.
30 ;; Speedbar support for MH-E package.
32 ;;; Change Log:
34 ;; $Id: mh-speed.el,v 1.37 2003/01/31 03:18:18 satyaki Exp $
36 ;;; Code:
38 ;; Requires
39 (require 'cl)
40 (require 'mh-e)
41 (require 'speedbar)
43 ;; Global variables
44 (defvar mh-speed-refresh-flag nil)
45 (defvar mh-speed-last-selected-folder nil)
46 (defvar mh-speed-folder-map (make-hash-table :test #'equal))
47 (defvar mh-speed-flists-cache (make-hash-table :test #'equal))
48 (defvar mh-speed-flists-process nil)
49 (defvar mh-speed-flists-timer nil)
50 (defvar mh-speed-partial-line "")
52 ;; Add our stealth update function
53 (unless (member 'mh-speed-stealth-update
54 (cdr (assoc "files" speedbar-stealthy-function-list)))
55 ;; Is changing constant lists in elisp safe?
56 (setq speedbar-stealthy-function-list
57 (copy-tree speedbar-stealthy-function-list))
58 (push 'mh-speed-stealth-update
59 (cdr (assoc "files" speedbar-stealthy-function-list))))
61 ;; Functions called by speedbar to initialize display...
62 ;;;###mh-autoload
63 (defun mh-folder-speedbar-buttons (buffer)
64 "Interface function to create MH-E speedbar buffer.
65 BUFFER is the MH-E buffer for which the speedbar buffer is to be created."
66 (unless (get-text-property (point-min) 'mh-level)
67 (erase-buffer)
68 (clrhash mh-speed-folder-map)
69 (speedbar-make-tag-line 'bracket ?+ 'mh-speed-toggle nil " " 'ignore nil
70 'mh-speedbar-folder-face 0)
71 (forward-line -1)
72 (setf (gethash nil mh-speed-folder-map)
73 (set-marker (make-marker) (1+ (line-beginning-position))))
74 (add-text-properties
75 (line-beginning-position) (1+ (line-beginning-position))
76 `(mh-folder nil mh-expanded nil mh-children-p t mh-level 0))
77 (mh-speed-stealth-update t)
78 (when mh-speed-run-flists-flag
79 (mh-speed-flists nil))))
81 ;;;###mh-autoload
82 (defalias 'mh-show-speedbar-buttons 'mh-folder-speedbar-buttons)
83 ;;;###mh-autoload
84 (defalias 'mh-letter-speedbar-buttons 'mh-folder-speedbar-buttons)
86 ;; Keymaps for speedbar...
87 (defvar mh-folder-speedbar-key-map (speedbar-make-specialized-keymap)
88 "Specialized speedbar keymap for MH-E buffers.")
89 (gnus-define-keys mh-folder-speedbar-key-map
90 "+" mh-speed-expand-folder
91 "-" mh-speed-contract-folder
92 "\r" mh-speed-view
93 "f" mh-speed-flists
94 "i" mh-speed-invalidate-map)
96 (defvar mh-show-speedbar-key-map mh-folder-speedbar-key-map)
97 (defvar mh-letter-speedbar-key-map mh-folder-speedbar-key-map)
99 ;; Menus for speedbar...
100 (defvar mh-folder-speedbar-menu-items
101 '(["Visit Folder" mh-speed-view
102 (save-excursion
103 (set-buffer speedbar-buffer)
104 (get-text-property (line-beginning-position) 'mh-folder))]
105 ["Expand nested folders" mh-speed-expand-folder
106 (and (get-text-property (line-beginning-position) 'mh-children-p)
107 (not (get-text-property (line-beginning-position) 'mh-expanded)))]
108 ["Contract nested folders" mh-speed-contract-folder
109 (and (get-text-property (line-beginning-position) 'mh-children-p)
110 (get-text-property (line-beginning-position) 'mh-expanded))]
111 ["Run Flists" mh-speed-flists t]
112 ["Invalidate cached folders" mh-speed-invalidate-map t])
113 "Extra menu items for speedbar.")
115 (defvar mh-show-speedbar-menu-items mh-folder-speedbar-menu-items)
116 (defvar mh-letter-speedbar-menu-items mh-folder-speedbar-menu-items)
118 (defmacro mh-speed-select-attached-frame ()
119 "Compatibility macro to handle speedbar versions 0.11a and 0.14beta4."
120 (cond ((fboundp 'dframe-select-attached-frame)
121 '(dframe-select-attached-frame speedbar-frame))
122 ((boundp 'speedbar-attached-frame)
123 '(select-frame speedbar-attached-frame))
124 (t (error "Installed speedbar version not supported by MH-E"))))
126 (defun mh-speed-update-current-folder (force)
127 "Update speedbar highlighting of the current folder.
128 The function tries to be smart so that work done is minimized. The currently
129 highlighted folder is cached and no highlighting happens unless it changes.
130 Also highlighting is suspended while the speedbar frame is selected.
131 Otherwise you get the disconcerting behavior of folders popping open on their
132 own when you are trying to navigate around in the speedbar buffer.
134 The update is always carried out if FORCE is non-nil."
135 (let* ((lastf (selected-frame))
136 (newcf (save-excursion
137 (mh-speed-select-attached-frame)
138 (prog1 (mh-speed-extract-folder-name (buffer-name))
139 (select-frame lastf))))
140 (lastb (current-buffer))
141 (case-fold-search t))
142 (when (or force
143 (and mh-speed-refresh-flag (not (eq lastf speedbar-frame)))
144 (and (stringp newcf)
145 (equal (substring newcf 0 1) "+")
146 (not (equal newcf mh-speed-last-selected-folder))))
147 (setq mh-speed-refresh-flag nil)
148 (select-frame speedbar-frame)
149 (set-buffer speedbar-buffer)
151 ;; Remove highlight from previous match...
152 (mh-speed-highlight mh-speed-last-selected-folder
153 'mh-speedbar-folder-face)
155 ;; If we found a match highlight it...
156 (when (mh-speed-goto-folder newcf)
157 (mh-speed-highlight newcf 'mh-speedbar-selected-folder-face))
159 (setq mh-speed-last-selected-folder newcf)
160 (speedbar-position-cursor-on-line)
161 (set-window-point (frame-first-window speedbar-frame) (point))
162 (set-buffer lastb)
163 (select-frame lastf))
164 (when (eq lastf speedbar-frame)
165 (setq mh-speed-refresh-flag t))))
167 (defun mh-speed-normal-face (face)
168 "Return normal face for given FACE."
169 (cond ((eq face 'mh-speedbar-folder-with-unseen-messages-face)
170 'mh-speedbar-folder-face)
171 ((eq face 'mh-speedbar-selected-folder-with-unseen-messages-face)
172 'mh-speedbar-selected-folder-face)
173 (t face)))
175 (defun mh-speed-bold-face (face)
176 "Return bold face for given FACE."
177 (cond ((eq face 'mh-speedbar-folder-face)
178 'mh-speedbar-folder-with-unseen-messages-face)
179 ((eq face 'mh-speedbar-selected-folder-face)
180 'mh-speedbar-selected-folder-with-unseen-messages-face)
181 (t face)))
183 (defun mh-speed-highlight (folder face)
184 "Set FOLDER to FACE."
185 (save-excursion
186 (speedbar-with-writable
187 (goto-char (gethash folder mh-speed-folder-map (point)))
188 (beginning-of-line)
189 (if (re-search-forward "([1-9][0-9]*/[0-9]+)" (line-end-position) t)
190 (setq face (mh-speed-bold-face face))
191 (setq face (mh-speed-normal-face face)))
192 (beginning-of-line)
193 (when (re-search-forward "\\[.\\] " (line-end-position) t)
194 (put-text-property (point) (line-end-position) 'face face)))))
196 (defun mh-speed-stealth-update (&optional force)
197 "Do stealth update.
198 With non-nil FORCE, the update is always carried out."
199 (cond ((save-excursion (set-buffer speedbar-buffer)
200 (get-text-property (point-min) 'mh-level))
201 ;; Execute this hook and *don't* run anything else
202 (mh-speed-update-current-folder force)
203 nil)
204 ;; Otherwise on to your regular programming
205 (t t)))
207 (defun mh-speed-goto-folder (folder)
208 "Move point to line containing FOLDER.
209 The function will expand out parent folders of FOLDER if needed."
210 (let ((prefix folder)
211 (suffix-list ())
212 (last-slash t))
213 (while (and (not (gethash prefix mh-speed-folder-map)) last-slash)
214 (setq last-slash (mh-search-from-end ?/ prefix))
215 (when (integerp last-slash)
216 (push (substring prefix (1+ last-slash)) suffix-list)
217 (setq prefix (substring prefix 0 last-slash))))
218 (let ((prefix-position (gethash prefix mh-speed-folder-map)))
219 (if prefix-position
220 (goto-char prefix-position)
221 (goto-char (point-min))
222 (mh-speed-toggle)
223 (unless (get-text-property (point) 'mh-expanded)
224 (mh-speed-toggle))
225 (goto-char (gethash prefix mh-speed-folder-map))))
226 (while suffix-list
227 ;; We always need atleast one toggle. We need two if the directory list
228 ;; is stale since a folder was added.
229 (when (equal prefix (get-text-property (line-beginning-position)
230 'mh-folder))
231 (mh-speed-toggle)
232 (unless (get-text-property (point) 'mh-expanded)
233 (mh-speed-toggle)))
234 (setq prefix (format "%s/%s" prefix (pop suffix-list)))
235 (goto-char (gethash prefix mh-speed-folder-map (point))))
236 (beginning-of-line)
237 (equal folder (get-text-property (point) 'mh-folder))))
239 (defun mh-speed-extract-folder-name (buffer)
240 "Given an MH-E BUFFER find the folder that should be highlighted.
241 Do the right thing for the different kinds of buffers that MH-E uses."
242 (save-excursion
243 (set-buffer buffer)
244 (cond ((eq major-mode 'mh-folder-mode)
245 mh-current-folder)
246 ((eq major-mode 'mh-show-mode)
247 (set-buffer mh-show-folder-buffer)
248 mh-current-folder)
249 ((eq major-mode 'mh-letter-mode)
250 (when (string-match mh-user-path buffer-file-name)
251 (let* ((rel-path (substring buffer-file-name (match-end 0)))
252 (directory-end (mh-search-from-end ?/ rel-path)))
253 (when directory-end
254 (format "+%s" (substring rel-path 0 directory-end)))))))))
256 (defun mh-speed-add-buttons (folder level)
257 "Add speedbar button for FOLDER which is at indented by LEVEL amount."
258 (let ((folder-list (mh-sub-folders folder)))
259 (mapc
260 (lambda (f)
261 (let* ((folder-name (format "%s%s%s" (or folder "+")
262 (if folder "/" "") (car f)))
263 (counts (gethash folder-name mh-speed-flists-cache)))
264 (speedbar-with-writable
265 (speedbar-make-tag-line
266 'bracket (if (cdr f) ?+ ? )
267 'mh-speed-toggle nil
268 (format "%s%s"
269 (car f)
270 (if counts
271 (format " (%s/%s)" (car counts) (cdr counts))
272 ""))
273 'mh-speed-view nil
274 (if (and counts (> (car counts) 0))
275 'mh-speedbar-folder-with-unseen-messages-face
276 'mh-speedbar-folder-face)
277 level)
278 (save-excursion
279 (forward-line -1)
280 (setf (gethash folder-name mh-speed-folder-map)
281 (set-marker (make-marker) (1+ (line-beginning-position))))
282 (add-text-properties
283 (line-beginning-position) (1+ (line-beginning-position))
284 `(mh-folder ,folder-name
285 mh-expanded nil
286 mh-children-p ,(not (not (cdr f)))
287 ,@(if counts `(mh-count
288 (,(car counts) . ,(cdr counts))) ())
289 mh-level ,level))))))
290 folder-list)))
292 ;;;###mh-autoload
293 (defun mh-speed-toggle (&rest args)
294 "Toggle the display of child folders.
295 The otional ARGS are ignored and there for compatibilty with speedbar."
296 (interactive)
297 (declare (ignore args))
298 (beginning-of-line)
299 (let ((parent (get-text-property (point) 'mh-folder))
300 (kids-p (get-text-property (point) 'mh-children-p))
301 (expanded (get-text-property (point) 'mh-expanded))
302 (level (get-text-property (point) 'mh-level))
303 (point (point))
304 start-region)
305 (speedbar-with-writable
306 (cond ((not kids-p) nil)
307 (expanded
308 (forward-line)
309 (setq start-region (point))
310 (while (and (get-text-property (point) 'mh-level)
311 (> (get-text-property (point) 'mh-level) level))
312 (remhash (get-text-property (point) 'mh-folder)
313 mh-speed-folder-map)
314 (forward-line))
315 (delete-region start-region (point))
316 (forward-line -1)
317 (speedbar-change-expand-button-char ?+)
318 (add-text-properties
319 (line-beginning-position) (1+ (line-beginning-position))
320 '(mh-expanded nil)))
322 (forward-line)
323 (mh-speed-add-buttons parent (1+ level))
324 (goto-char point)
325 (speedbar-change-expand-button-char ?-)
326 (add-text-properties
327 (line-beginning-position) (1+ (line-beginning-position))
328 `(mh-expanded t)))))))
330 (defalias 'mh-speed-expand-folder 'mh-speed-toggle)
331 (defalias 'mh-speed-contract-folder 'mh-speed-toggle)
333 ;;;###mh-autoload
334 (defun mh-speed-view (&rest args)
335 "View folder on current line.
336 Optional ARGS are ignored."
337 (interactive)
338 (declare (ignore args))
339 (let* ((folder (get-text-property (line-beginning-position) 'mh-folder))
340 (range (and (stringp folder) (mh-read-msg-range folder))))
341 (when (stringp folder)
342 (speedbar-with-attached-buffer
343 (mh-visit-folder folder range)
344 (delete-other-windows)))))
346 (defvar mh-speed-current-folder nil)
348 ;;;###mh-autoload
349 (defun mh-speed-flists (force)
350 "Execute flists -recurse and update message counts.
351 If FORCE is non-nil the timer is reset."
352 (interactive (list t))
353 (when force
354 (when (timerp mh-speed-flists-timer)
355 (cancel-timer mh-speed-flists-timer))
356 (setq mh-speed-flists-timer nil)
357 (when (and (processp mh-speed-flists-process)
358 (not (eq (process-status mh-speed-flists-process) 'exit)))
359 (kill-process mh-speed-flists-process)
360 (setq mh-speed-flists-process nil)))
361 (unless mh-speed-flists-timer
362 (setq mh-speed-flists-timer
363 (run-at-time
364 nil mh-speed-flists-interval
365 (lambda ()
366 (unless (and (processp mh-speed-flists-process)
367 (not (eq (process-status mh-speed-flists-process)
368 'exit)))
369 (setq mh-speed-current-folder
370 (concat
371 (with-temp-buffer
372 (call-process (expand-file-name "folder" mh-progs)
373 nil '(t nil) nil "-fast")
374 (buffer-substring (point-min) (1- (point-max))))
375 "+"))
376 (setq mh-speed-flists-process
377 (start-process "*flists*" nil
378 (expand-file-name "flists" mh-progs)
379 "-recurse"
380 "-sequence" (symbol-name mh-unseen-seq)))
381 (set-process-filter mh-speed-flists-process
382 'mh-speed-parse-flists-output)))))))
384 ;; Copied from mh-make-folder-list-filter...
385 (defun mh-speed-parse-flists-output (process output)
386 "Parse the incremental results from flists.
387 PROCESS is the flists process and OUTPUT is the results that must be handled
388 next."
389 (let ((prevailing-match-data (match-data))
390 (position 0)
391 line-end line folder unseen total)
392 (unwind-protect
393 (while (setq line-end (string-match "\n" output position))
394 (setq line (format "%s%s"
395 mh-speed-partial-line
396 (substring output position line-end))
397 mh-speed-partial-line "")
398 (multiple-value-setq (folder unseen total)
399 (mh-parse-flist-output-line line mh-speed-current-folder))
400 (when (and folder unseen total)
401 (setf (gethash folder mh-speed-flists-cache) (cons unseen total))
402 (save-excursion
403 (when (buffer-live-p (get-buffer speedbar-buffer))
404 (set-buffer speedbar-buffer)
405 (speedbar-with-writable
406 (when (get-text-property (point-min) 'mh-level)
407 (let ((pos (gethash folder mh-speed-folder-map))
408 face)
409 (when pos
410 (goto-char pos)
411 (goto-char (line-beginning-position))
412 (cond
413 ((null (get-text-property (point) 'mh-count))
414 (goto-char (line-end-position))
415 (setq face (get-text-property (1- (point)) 'face))
416 (insert (format " (%s/%s)" unseen total))
417 (mh-speed-highlight 'unknown face)
418 (goto-char (line-beginning-position))
419 (add-text-properties (point) (1+ (point))
420 `(mh-count (,unseen . ,total))))
421 ((not (equal (get-text-property (point) 'mh-count)
422 (cons unseen total)))
423 (goto-char (line-end-position))
424 (setq face (get-text-property (1- (point)) 'face))
425 (re-search-backward " " (line-beginning-position) t)
426 (delete-region (point) (line-end-position))
427 (insert (format " (%s/%s)" unseen total))
428 (mh-speed-highlight 'unknown face)
429 (goto-char (line-beginning-position))
430 (add-text-properties
431 (point) (1+ (point))
432 `(mh-count (,unseen . ,total))))))))))))
433 (setq position (1+ line-end)))
434 (set-match-data prevailing-match-data))
435 (setq mh-speed-partial-line (substring output position))))
437 ;;;###mh-autoload
438 (defun mh-speed-invalidate-map (folder)
439 "Remove FOLDER from various optimization caches."
440 (interactive (list ""))
441 (save-excursion
442 (set-buffer speedbar-buffer)
443 (let* ((speedbar-update-flag nil)
444 (last-slash (mh-search-from-end ?/ folder))
445 (parent (if last-slash (substring folder 0 last-slash) nil))
446 (parent-position (gethash parent mh-speed-folder-map))
447 (parent-change nil))
448 (when parent-position
449 (let ((parent-kids (mh-sub-folders parent)))
450 (cond ((null parent-kids)
451 (setq parent-change ?+))
452 ((and (null (cdr parent-kids))
453 (equal (if last-slash
454 (substring folder (1+ last-slash))
455 (substring folder 1))
456 (caar parent-kids)))
457 (setq parent-change ? ))))
458 (goto-char parent-position)
459 (when (equal (get-text-property (line-beginning-position) 'mh-folder)
460 parent)
461 (when (get-text-property (line-beginning-position) 'mh-expanded)
462 (mh-speed-toggle))
463 (when parent-change
464 (speedbar-with-writable
465 (mh-speedbar-change-expand-button-char parent-change)
466 (add-text-properties
467 (line-beginning-position) (1+ (line-beginning-position))
468 `(mh-children-p ,(equal parent-change ?+)))))
469 (mh-speed-highlight mh-speed-last-selected-folder
470 'mh-speedbar-folder-face)
471 (setq mh-speed-last-selected-folder nil)
472 (setq mh-speed-refresh-flag t)))
473 (when (equal folder "")
474 (clrhash mh-sub-folders-cache)))))
476 ;;;###mh-autoload
477 (defun mh-speed-add-folder (folder)
478 "Add FOLDER since it is being created.
479 The function invalidates the latest ancestor that is present."
480 (save-excursion
481 (set-buffer speedbar-buffer)
482 (let ((speedbar-update-flag nil)
483 (last-slash (mh-search-from-end ?/ folder))
484 (ancestor folder)
485 (ancestor-pos nil))
486 (block while-loop
487 (while last-slash
488 (setq ancestor (substring ancestor 0 last-slash))
489 (setq ancestor-pos (gethash ancestor mh-speed-folder-map))
490 (when ancestor-pos
491 (return-from while-loop))
492 (setq last-slash (mh-search-from-end ?/ ancestor))))
493 (unless ancestor-pos (setq ancestor nil))
494 (goto-char (or ancestor-pos (gethash nil mh-speed-folder-map)))
495 (speedbar-with-writable
496 (mh-speedbar-change-expand-button-char ?+)
497 (add-text-properties
498 (line-beginning-position) (1+ (line-beginning-position))
499 `(mh-children-p t)))
500 (when (get-text-property (line-beginning-position) 'mh-expanded)
501 (mh-speed-toggle))
502 (setq mh-speed-refresh-flag t))))
504 ;; Make it slightly more general to allow for [ ] buttons to be changed to
505 ;; [+].
506 (defun mh-speedbar-change-expand-button-char (char)
507 "Change the expansion button character to CHAR for the current line."
508 (save-excursion
509 (beginning-of-line)
510 (if (re-search-forward "\\[.\\]" (line-end-position) t)
511 (speedbar-with-writable
512 (backward-char 2)
513 (delete-char 1)
514 (insert-char char 1 t)
515 (put-text-property (point) (1- (point)) 'invisible nil)
516 ;; make sure we fix the image on the text here.
517 (speedbar-insert-image-button-maybe (- (point) 2) 3)))))
519 (provide 'mh-speed)
521 ;;; Local Variables:
522 ;;; indent-tabs-mode: nil
523 ;;; sentence-end-double-space: nil
524 ;;; End:
526 ;;; mh-speed.el ends here