(byte-compile-warning-types): Add interactive-only.
[emacs.git] / lisp / mh-e / mh-speed.el
blob2617a941de16788603313688d7a333814b85010d
1 ;;; mh-speed.el --- Speedbar interface for MH-E.
3 ;; Copyright (C) 2002, 2003, 2004 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 ;;; Code:
36 ;; Requires
37 (eval-when-compile (require 'mh-acros))
38 (mh-require-cl)
39 (require 'mh-e)
40 (require 'speedbar)
41 (require 'timer)
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 (or (gethash nil mh-speed-folder-map) (make-marker))
74 (1+ (line-beginning-position))))
75 (add-text-properties
76 (line-beginning-position) (1+ (line-beginning-position))
77 `(mh-folder nil mh-expanded nil mh-children-p t mh-level 0))
78 (mh-speed-stealth-update t)
79 (when mh-speed-run-flists-flag
80 (mh-speed-flists nil))))
82 ;;;###mh-autoload
83 (defalias 'mh-show-speedbar-buttons 'mh-folder-speedbar-buttons)
84 ;;;###mh-autoload
85 (defalias 'mh-letter-speedbar-buttons 'mh-folder-speedbar-buttons)
87 ;; Keymaps for speedbar...
88 (defvar mh-folder-speedbar-key-map (speedbar-make-specialized-keymap)
89 "Specialized speedbar keymap for MH-E buffers.")
90 (gnus-define-keys mh-folder-speedbar-key-map
91 "+" mh-speed-expand-folder
92 "-" mh-speed-contract-folder
93 "\r" mh-speed-view
94 "r" mh-speed-refresh)
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 '("--"
102 ["Visit Folder" mh-speed-view
103 (save-excursion
104 (set-buffer speedbar-buffer)
105 (get-text-property (line-beginning-position) 'mh-folder))]
106 ["Expand Nested Folders" mh-speed-expand-folder
107 (and (get-text-property (line-beginning-position) 'mh-children-p)
108 (not (get-text-property (line-beginning-position) 'mh-expanded)))]
109 ["Contract Nested Folders" mh-speed-contract-folder
110 (and (get-text-property (line-beginning-position) 'mh-children-p)
111 (get-text-property (line-beginning-position) 'mh-expanded))]
112 ["Refresh Speedbar" mh-speed-refresh 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 (or (gethash folder-name mh-speed-folder-map)
282 (make-marker))
283 (1+ (line-beginning-position))))
284 (add-text-properties
285 (line-beginning-position) (1+ (line-beginning-position))
286 `(mh-folder ,folder-name
287 mh-expanded nil
288 mh-children-p ,(not (not (cdr f)))
289 ,@(if counts `(mh-count
290 (,(car counts) . ,(cdr counts))) ())
291 mh-level ,level))))))
292 folder-list)))
294 ;;;###mh-autoload
295 (defun mh-speed-toggle (&rest args)
296 "Toggle the display of child folders.
297 The otional ARGS are ignored and there for compatibilty with speedbar."
298 (interactive)
299 (declare (ignore args))
300 (beginning-of-line)
301 (let ((parent (get-text-property (point) 'mh-folder))
302 (kids-p (get-text-property (point) 'mh-children-p))
303 (expanded (get-text-property (point) 'mh-expanded))
304 (level (get-text-property (point) 'mh-level))
305 (point (point))
306 start-region)
307 (speedbar-with-writable
308 (cond ((not kids-p) nil)
309 (expanded
310 (forward-line)
311 (setq start-region (point))
312 (while (and (get-text-property (point) 'mh-level)
313 (> (get-text-property (point) 'mh-level) level))
314 (let ((folder (get-text-property (point) 'mh-folder)))
315 (when (gethash folder mh-speed-folder-map)
316 (set-marker (gethash folder mh-speed-folder-map) nil)
317 (remhash folder mh-speed-folder-map)))
318 (forward-line))
319 (delete-region start-region (point))
320 (forward-line -1)
321 (speedbar-change-expand-button-char ?+)
322 (add-text-properties
323 (line-beginning-position) (1+ (line-beginning-position))
324 '(mh-expanded nil)))
326 (forward-line)
327 (mh-speed-add-buttons parent (1+ level))
328 (goto-char point)
329 (speedbar-change-expand-button-char ?-)
330 (add-text-properties
331 (line-beginning-position) (1+ (line-beginning-position))
332 `(mh-expanded t)))))))
334 (defalias 'mh-speed-expand-folder 'mh-speed-toggle)
335 (defalias 'mh-speed-contract-folder 'mh-speed-toggle)
337 ;;;###mh-autoload
338 (defun mh-speed-view (&rest args)
339 "View folder on current line.
340 Optional ARGS are ignored."
341 (interactive)
342 (declare (ignore args))
343 (let* ((folder (get-text-property (line-beginning-position) 'mh-folder))
344 (range (and (stringp folder)
345 (mh-read-range "Scan" folder t nil nil
346 mh-interpret-number-as-range-flag))))
347 (when (stringp folder)
348 (speedbar-with-attached-buffer
349 (mh-visit-folder folder range)
350 (delete-other-windows)))))
352 (defvar mh-speed-current-folder nil)
353 (defvar mh-speed-flists-folder nil)
355 (defmacro mh-process-kill-without-query (process)
356 "PROCESS can be killed without query on Emacs exit.
357 Avoid using `process-kill-without-query' if possible since it is now
358 obsolete."
359 (if (fboundp 'set-process-query-on-exit-flag)
360 `(set-process-query-on-exit-flag ,process nil)
361 `(process-kill-without-query ,process)))
363 ;;;###mh-autoload
364 (defun mh-speed-flists (force &rest folders)
365 "Execute flists -recurse and update message counts.
366 If FORCE is non-nil the timer is reset.
368 Any number of optional FOLDERS can be specified. If specified, flists is run
369 only for that one folder."
370 (interactive (list t))
371 (when force
372 (when mh-speed-flists-timer
373 (cancel-timer mh-speed-flists-timer)
374 (setq mh-speed-flists-timer nil))
375 (when (and (processp mh-speed-flists-process)
376 (not (eq (process-status mh-speed-flists-process) 'exit)))
377 (set-process-filter mh-speed-flists-process t)
378 (kill-process mh-speed-flists-process)
379 (setq mh-speed-partial-line "")
380 (setq mh-speed-flists-process nil)))
381 (setq mh-speed-flists-folder folders)
382 (unless mh-speed-flists-timer
383 (setq mh-speed-flists-timer
384 (run-at-time
385 nil (and mh-speed-run-flists-flag mh-speed-flists-interval)
386 (lambda ()
387 (unless (and (processp mh-speed-flists-process)
388 (not (eq (process-status mh-speed-flists-process)
389 'exit)))
390 (setq mh-speed-current-folder
391 (concat
392 (if mh-speed-flists-folder
393 (substring (car (reverse mh-speed-flists-folder)) 1)
394 (with-temp-buffer
395 (call-process (expand-file-name "folder" mh-progs)
396 nil '(t nil) nil "-fast")
397 (buffer-substring (point-min) (1- (point-max)))))
398 "+"))
399 (setq mh-speed-flists-process
400 (apply #'start-process "*flists*" nil
401 (expand-file-name "flists" mh-progs)
402 (if mh-speed-flists-folder "-noall" "-all")
403 "-sequence" (symbol-name mh-unseen-seq)
404 (or mh-speed-flists-folder '("-recurse"))))
405 ;; Run flists on all folders the next time around...
406 (setq mh-speed-flists-folder nil)
407 (mh-process-kill-without-query mh-speed-flists-process)
408 (set-process-filter mh-speed-flists-process
409 'mh-speed-parse-flists-output)))))))
411 ;; Copied from mh-make-folder-list-filter...
412 (defun mh-speed-parse-flists-output (process output)
413 "Parse the incremental results from flists.
414 PROCESS is the flists process and OUTPUT is the results that must be handled
415 next."
416 (let ((prevailing-match-data (match-data))
417 (position 0)
418 line-end line folder unseen total)
419 (unwind-protect
420 (while (setq line-end (string-match "\n" output position))
421 (setq line (format "%s%s"
422 mh-speed-partial-line
423 (substring output position line-end))
424 mh-speed-partial-line "")
425 (multiple-value-setq (folder unseen total)
426 (mh-parse-flist-output-line line mh-speed-current-folder))
427 (when (and folder unseen total
428 (let ((old-pair (gethash folder mh-speed-flists-cache)))
429 (or (not (equal (car old-pair) unseen))
430 (not (equal (cdr old-pair) total)))))
431 (setf (gethash folder mh-speed-flists-cache) (cons unseen total))
432 (save-excursion
433 (when (buffer-live-p (get-buffer speedbar-buffer))
434 (set-buffer speedbar-buffer)
435 (speedbar-with-writable
436 (when (get-text-property (point-min) 'mh-level)
437 (let ((pos (gethash folder mh-speed-folder-map))
438 face)
439 (when pos
440 (goto-char pos)
441 (goto-char (line-beginning-position))
442 (cond
443 ((null (get-text-property (point) 'mh-count))
444 (goto-char (line-end-position))
445 (setq face (get-text-property (1- (point)) 'face))
446 (insert (format " (%s/%s)" unseen total))
447 (mh-speed-highlight 'unknown face)
448 (goto-char (line-beginning-position))
449 (add-text-properties (point) (1+ (point))
450 `(mh-count (,unseen . ,total))))
451 ((not (equal (get-text-property (point) 'mh-count)
452 (cons unseen total)))
453 (goto-char (line-end-position))
454 (setq face (get-text-property (1- (point)) 'face))
455 (re-search-backward " " (line-beginning-position) t)
456 (delete-region (point) (line-end-position))
457 (insert (format " (%s/%s)" unseen total))
458 (mh-speed-highlight 'unknown face)
459 (goto-char (line-beginning-position))
460 (add-text-properties
461 (point) (1+ (point))
462 `(mh-count (,unseen . ,total))))))))))))
463 (setq position (1+ line-end)))
464 (set-match-data prevailing-match-data))
465 (setq mh-speed-partial-line (substring output position))))
467 ;;;###mh-autoload
468 (defun mh-speed-invalidate-map (folder)
469 "Remove FOLDER from various optimization caches."
470 (interactive (list ""))
471 (save-excursion
472 (set-buffer speedbar-buffer)
473 (let* ((speedbar-update-flag nil)
474 (last-slash (mh-search-from-end ?/ folder))
475 (parent (if last-slash (substring folder 0 last-slash) nil))
476 (parent-position (gethash parent mh-speed-folder-map))
477 (parent-change nil))
478 (when parent-position
479 (let ((parent-kids (mh-sub-folders parent)))
480 (cond ((null parent-kids)
481 (setq parent-change ?+))
482 ((and (null (cdr parent-kids))
483 (equal (if last-slash
484 (substring folder (1+ last-slash))
485 (substring folder 1))
486 (caar parent-kids)))
487 (setq parent-change ? ))))
488 (goto-char parent-position)
489 (when (equal (get-text-property (line-beginning-position) 'mh-folder)
490 parent)
491 (when (get-text-property (line-beginning-position) 'mh-expanded)
492 (mh-speed-toggle))
493 (when parent-change
494 (speedbar-with-writable
495 (mh-speedbar-change-expand-button-char parent-change)
496 (add-text-properties
497 (line-beginning-position) (1+ (line-beginning-position))
498 `(mh-children-p ,(equal parent-change ?+)))))
499 (mh-speed-highlight mh-speed-last-selected-folder
500 'mh-speedbar-folder-face)
501 (setq mh-speed-last-selected-folder nil)
502 (setq mh-speed-refresh-flag t)))
503 (when (equal folder "")
504 (clrhash mh-sub-folders-cache)))))
506 (defun mh-speed-refresh ()
507 "Refresh the speedbar.
508 Use this function to refresh the speedbar if folders have been added or
509 deleted or message ranges have been updated outside of MH-E."
510 (interactive)
511 (mh-speed-flists t)
512 (mh-speed-invalidate-map ""))
514 ;;;###mh-autoload
515 (defun mh-speed-add-folder (folder)
516 "Add FOLDER since it is being created.
517 The function invalidates the latest ancestor that is present."
518 (save-excursion
519 (set-buffer speedbar-buffer)
520 (let ((speedbar-update-flag nil)
521 (last-slash (mh-search-from-end ?/ folder))
522 (ancestor folder)
523 (ancestor-pos nil))
524 (block while-loop
525 (while last-slash
526 (setq ancestor (substring ancestor 0 last-slash))
527 (setq ancestor-pos (gethash ancestor mh-speed-folder-map))
528 (when ancestor-pos
529 (return-from while-loop))
530 (setq last-slash (mh-search-from-end ?/ ancestor))))
531 (unless ancestor-pos (setq ancestor nil))
532 (goto-char (or ancestor-pos (gethash nil mh-speed-folder-map)))
533 (speedbar-with-writable
534 (mh-speedbar-change-expand-button-char ?+)
535 (add-text-properties
536 (line-beginning-position) (1+ (line-beginning-position))
537 `(mh-children-p t)))
538 (when (get-text-property (line-beginning-position) 'mh-expanded)
539 (mh-speed-toggle))
540 (setq mh-speed-refresh-flag t))))
542 ;; Make it slightly more general to allow for [ ] buttons to be changed to
543 ;; [+].
544 (defun mh-speedbar-change-expand-button-char (char)
545 "Change the expansion button character to CHAR for the current line."
546 (save-excursion
547 (beginning-of-line)
548 (if (re-search-forward "\\[.\\]" (line-end-position) t)
549 (speedbar-with-writable
550 (backward-char 2)
551 (delete-char 1)
552 (insert-char char 1 t)
553 (put-text-property (point) (1- (point)) 'invisible nil)
554 ;; make sure we fix the image on the text here.
555 (mh-funcall-if-exists
556 speedbar-insert-image-button-maybe (- (point) 2) 3)))))
558 (provide 'mh-speed)
560 ;;; Local Variables:
561 ;;; indent-tabs-mode: nil
562 ;;; sentence-end-double-space: nil
563 ;;; End:
565 ;;; arch-tag: d38ddcd4-3c00-4e37-99bf-8b89dda7b32c
566 ;;; mh-speed.el ends here