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>
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)
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.
28 ;; Future versions should only use flists.
30 ;; Speedbar support for MH-E package.
34 ;; $Id: mh-speed.el,v 1.37 2003/01/31 03:18:18 satyaki Exp $
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...
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
)
68 (clrhash mh-speed-folder-map
)
69 (speedbar-make-tag-line 'bracket ?
+ 'mh-speed-toggle nil
" " 'ignore nil
70 'mh-speedbar-folder-face
0)
72 (setf (gethash nil mh-speed-folder-map
)
73 (set-marker (make-marker) (1+ (line-beginning-position))))
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
))))
82 (defalias 'mh-show-speedbar-buttons
'mh-folder-speedbar-buttons
)
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
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
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
))
143 (and mh-speed-refresh-flag
(not (eq lastf speedbar-frame
)))
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))
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
)
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
)
183 (defun mh-speed-highlight (folder face
)
184 "Set FOLDER to FACE."
186 (speedbar-with-writable
187 (goto-char (gethash folder mh-speed-folder-map
(point)))
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
)))
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
)
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
)
204 ;; Otherwise on to your regular programming
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
)
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
)))
220 (goto-char prefix-position
)
221 (goto-char (point-min))
223 (unless (get-text-property (point) 'mh-expanded
)
225 (goto-char (gethash prefix mh-speed-folder-map
))))
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)
232 (unless (get-text-property (point) 'mh-expanded
)
234 (setq prefix
(format "%s/%s" prefix
(pop suffix-list
)))
235 (goto-char (gethash prefix mh-speed-folder-map
(point))))
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."
244 (cond ((eq major-mode
'mh-folder-mode
)
246 ((eq major-mode
'mh-show-mode
)
247 (set-buffer mh-show-folder-buffer
)
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
)))
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
)))
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
) ?
+ ?
)
271 (format " (%s/%s)" (car counts
) (cdr counts
))
274 (if (and counts
(> (car counts
) 0))
275 'mh-speedbar-folder-with-unseen-messages-face
276 'mh-speedbar-folder-face
)
280 (setf (gethash folder-name mh-speed-folder-map
)
281 (set-marker (make-marker) (1+ (line-beginning-position))))
283 (line-beginning-position) (1+ (line-beginning-position))
284 `(mh-folder ,folder-name
286 mh-children-p
,(not (not (cdr f
)))
287 ,@(if counts
`(mh-count
288 (,(car counts
) .
,(cdr counts
))) ())
289 mh-level
,level
))))))
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."
297 (declare (ignore args
))
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
))
305 (speedbar-with-writable
306 (cond ((not kids-p
) nil
)
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
)
315 (delete-region start-region
(point))
317 (speedbar-change-expand-button-char ?
+)
319 (line-beginning-position) (1+ (line-beginning-position))
323 (mh-speed-add-buttons parent
(1+ level
))
325 (speedbar-change-expand-button-char ?-
)
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
)
334 (defun mh-speed-view (&rest args
)
335 "View folder on current line.
336 Optional ARGS are ignored."
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
)
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
))
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
364 nil mh-speed-flists-interval
366 (unless (and (processp mh-speed-flists-process
)
367 (not (eq (process-status mh-speed-flists-process
)
369 (setq mh-speed-current-folder
372 (call-process (expand-file-name "folder" mh-progs
)
373 nil
'(t nil
) nil
"-fast")
374 (buffer-substring (point-min) (1- (point-max))))
376 (setq mh-speed-flists-process
377 (start-process "*flists*" nil
378 (expand-file-name "flists" mh-progs
)
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
389 (let ((prevailing-match-data (match-data))
391 line-end line folder unseen total
)
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
))
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
))
411 (goto-char (line-beginning-position))
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))
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
))))
438 (defun mh-speed-invalidate-map (folder)
439 "Remove FOLDER from various optimization caches."
440 (interactive (list ""))
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
))
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))
457 (setq parent-change ?
))))
458 (goto-char parent-position
)
459 (when (equal (get-text-property (line-beginning-position) 'mh-folder
)
461 (when (get-text-property (line-beginning-position) 'mh-expanded
)
464 (speedbar-with-writable
465 (mh-speedbar-change-expand-button-char parent-change
)
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
)))))
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."
481 (set-buffer speedbar-buffer
)
482 (let ((speedbar-update-flag nil
)
483 (last-slash (mh-search-from-end ?
/ folder
))
488 (setq ancestor
(substring ancestor
0 last-slash
))
489 (setq ancestor-pos
(gethash ancestor mh-speed-folder-map
))
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 ?
+)
498 (line-beginning-position) (1+ (line-beginning-position))
500 (when (get-text-property (line-beginning-position) 'mh-expanded
)
502 (setq mh-speed-refresh-flag t
))))
504 ;; Make it slightly more general to allow for [ ] buttons to be changed to
506 (defun mh-speedbar-change-expand-button-char (char)
507 "Change the expansion button character to CHAR for the current line."
510 (if (re-search-forward "\\[.\\]" (line-end-position) t
)
511 (speedbar-with-writable
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)))))
522 ;;; indent-tabs-mode: nil
523 ;;; sentence-end-double-space: nil
526 ;;; mh-speed.el ends here