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>
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.
37 (eval-when-compile (require 'mh-acros
))
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 (or (gethash nil mh-speed-folder-map
) (make-marker))
74 (1+ (line-beginning-position))))
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
))))
83 (defalias 'mh-show-speedbar-buttons
'mh-folder-speedbar-buttons
)
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
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
102 ["Visit Folder" mh-speed-view
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
))
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 (or (gethash folder-name mh-speed-folder-map
)
283 (1+ (line-beginning-position))))
285 (line-beginning-position) (1+ (line-beginning-position))
286 `(mh-folder ,folder-name
288 mh-children-p
,(not (not (cdr f
)))
289 ,@(if counts
`(mh-count
290 (,(car counts
) .
,(cdr counts
))) ())
291 mh-level
,level
))))))
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."
299 (declare (ignore args
))
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
))
307 (speedbar-with-writable
308 (cond ((not kids-p
) nil
)
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
)))
319 (delete-region start-region
(point))
321 (speedbar-change-expand-button-char ?
+)
323 (line-beginning-position) (1+ (line-beginning-position))
327 (mh-speed-add-buttons parent
(1+ level
))
329 (speedbar-change-expand-button-char ?-
)
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
)
338 (defun mh-speed-view (&rest args
)
339 "View folder on current line.
340 Optional ARGS are ignored."
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
359 (if (fboundp 'set-process-query-on-exit-flag
)
360 `(set-process-query-on-exit-flag ,process nil
)
361 `(process-kill-without-query ,process
)))
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
))
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
385 nil
(and mh-speed-run-flists-flag mh-speed-flists-interval
)
387 (unless (and (processp mh-speed-flists-process
)
388 (not (eq (process-status mh-speed-flists-process
)
390 (setq mh-speed-current-folder
392 (if mh-speed-flists-folder
393 (substring (car (reverse mh-speed-flists-folder
)) 1)
395 (call-process (expand-file-name "folder" mh-progs
)
396 nil
'(t nil
) nil
"-fast")
397 (buffer-substring (point-min) (1- (point-max)))))
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
416 (let ((prevailing-match-data (match-data))
418 line-end line folder unseen total
)
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
))
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
))
441 (goto-char (line-beginning-position))
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))
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
))))
468 (defun mh-speed-invalidate-map (folder)
469 "Remove FOLDER from various optimization caches."
470 (interactive (list ""))
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
))
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))
487 (setq parent-change ?
))))
488 (goto-char parent-position
)
489 (when (equal (get-text-property (line-beginning-position) 'mh-folder
)
491 (when (get-text-property (line-beginning-position) 'mh-expanded
)
494 (speedbar-with-writable
495 (mh-speedbar-change-expand-button-char parent-change
)
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."
512 (mh-speed-invalidate-map ""))
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."
519 (set-buffer speedbar-buffer
)
520 (let ((speedbar-update-flag nil
)
521 (last-slash (mh-search-from-end ?
/ folder
))
526 (setq ancestor
(substring ancestor
0 last-slash
))
527 (setq ancestor-pos
(gethash ancestor mh-speed-folder-map
))
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 ?
+)
536 (line-beginning-position) (1+ (line-beginning-position))
538 (when (get-text-property (line-beginning-position) 'mh-expanded
)
540 (setq mh-speed-refresh-flag t
))))
542 ;; Make it slightly more general to allow for [ ] buttons to be changed to
544 (defun mh-speedbar-change-expand-button-char (char)
545 "Change the expansion button character to CHAR for the current line."
548 (if (re-search-forward "\\[.\\]" (line-end-position) t
)
549 (speedbar-with-writable
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)))))
561 ;;; indent-tabs-mode: nil
562 ;;; sentence-end-double-space: nil
565 ;;; arch-tag: d38ddcd4-3c00-4e37-99bf-8b89dda7b32c
566 ;;; mh-speed.el ends here