1 ;;; mh-speed.el --- MH-E speedbar support
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 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 3, 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., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Future versions should only use flists.
43 (defvar mh-speed-refresh-flag nil
)
44 (defvar mh-speed-last-selected-folder nil
)
45 (defvar mh-speed-folder-map
(make-hash-table :test
#'equal
))
46 (defvar mh-speed-flists-cache
(make-hash-table :test
#'equal
))
47 (defvar mh-speed-flists-process nil
)
48 (defvar mh-speed-flists-timer nil
)
49 (defvar mh-speed-partial-line
"")
55 (unless (member 'mh-speed-stealth-update
56 (cdr (assoc "files" speedbar-stealthy-function-list
)))
57 ;; Is changing constant lists in elisp safe?
58 (setq speedbar-stealthy-function-list
59 (copy-tree speedbar-stealthy-function-list
))
60 (push 'mh-speed-stealth-update
61 (cdr (assoc "files" speedbar-stealthy-function-list
))))
67 (defvar mh-folder-speedbar-menu-items
69 ["Visit Folder" mh-speed-view
71 (set-buffer speedbar-buffer
)
72 (get-text-property (mh-line-beginning-position) 'mh-folder
))]
73 ["Expand Nested Folders" mh-speed-expand-folder
74 (and (get-text-property (mh-line-beginning-position) 'mh-children-p
)
75 (not (get-text-property (mh-line-beginning-position) 'mh-expanded
)))]
76 ["Contract Nested Folders" mh-speed-contract-folder
77 (and (get-text-property (mh-line-beginning-position) 'mh-children-p
)
78 (get-text-property (mh-line-beginning-position) 'mh-expanded
))]
79 ["Refresh Speedbar" mh-speed-refresh t
])
80 "Extra menu items for speedbar.")
82 (defvar mh-show-speedbar-menu-items mh-folder-speedbar-menu-items
)
83 (defvar mh-letter-speedbar-menu-items mh-folder-speedbar-menu-items
)
89 (defvar mh-folder-speedbar-key-map
(speedbar-make-specialized-keymap)
90 "Specialized speedbar keymap for MH-E buffers.")
92 (gnus-define-keys mh-folder-speedbar-key-map
93 "+" mh-speed-expand-folder
94 "-" mh-speed-contract-folder
98 (defvar mh-show-speedbar-key-map mh-folder-speedbar-key-map
)
99 (defvar mh-letter-speedbar-key-map mh-folder-speedbar-key-map
)
103 ;;; Speedbar Commands
107 (defalias 'mh-speed-contract-folder
'mh-speed-toggle
)
109 (defalias 'mh-speed-expand-folder
'mh-speed-toggle
)
111 (defun mh-speed-refresh ()
112 "Regenerates the list of folders in the speedbar.
114 Run this command if you've added or deleted a folder, or want to
115 update the unseen message count before the next automatic
119 (mh-speed-invalidate-map ""))
121 (defun mh-speed-stealth-update (&optional force
)
123 With non-nil FORCE, the update is always carried out."
124 (cond ((save-excursion (set-buffer speedbar-buffer
)
125 (get-text-property (point-min) 'mh-level
))
126 ;; Execute this hook and *don't* run anything else
127 (mh-speed-update-current-folder force
)
129 ;; Otherwise on to your regular programming
132 (defun mh-speed-toggle (&rest args
)
133 "Toggle the display of child folders in the speedbar.
134 The optional ARGS from speedbar are ignored."
136 (declare (ignore args
))
138 (let ((parent (get-text-property (point) 'mh-folder
))
139 (kids-p (get-text-property (point) 'mh-children-p
))
140 (expanded (get-text-property (point) 'mh-expanded
))
141 (level (get-text-property (point) 'mh-level
))
144 (speedbar-with-writable
145 (cond ((not kids-p
) nil
)
148 (setq start-region
(point))
149 (while (and (get-text-property (point) 'mh-level
)
150 (> (get-text-property (point) 'mh-level
) level
))
151 (let ((folder (get-text-property (point) 'mh-folder
)))
152 (when (gethash folder mh-speed-folder-map
)
153 (set-marker (gethash folder mh-speed-folder-map
) nil
)
154 (remhash folder mh-speed-folder-map
)))
156 (delete-region start-region
(point))
158 (speedbar-change-expand-button-char ?
+)
160 (mh-line-beginning-position) (1+ (line-beginning-position))
164 (mh-speed-add-buttons parent
(1+ level
))
166 (speedbar-change-expand-button-char ?-
)
168 (mh-line-beginning-position) (1+ (line-beginning-position))
169 `(mh-expanded t
)))))))
171 (defun mh-speed-view (&rest args
)
172 "Visits the selected folder just as if you had used \\<mh-folder-mode-map>\\[mh-visit-folder].
173 The optional ARGS from speedbar are ignored."
175 (declare (ignore args
))
176 (let* ((folder (get-text-property (mh-line-beginning-position) 'mh-folder
))
177 (range (and (stringp folder
)
178 (mh-read-range "Scan" folder t nil nil
179 mh-interpret-number-as-range-flag
))))
180 (when (stringp folder
)
181 (speedbar-with-attached-buffer
182 (mh-visit-folder folder range
)
183 (delete-other-windows)))))
190 (defun mh-folder-speedbar-buttons (buffer)
191 "Interface function to create MH-E speedbar buffer.
192 BUFFER is the MH-E buffer for which the speedbar buffer is to be
194 (unless (get-text-property (point-min) 'mh-level
)
196 (clrhash mh-speed-folder-map
)
197 (speedbar-make-tag-line 'bracket ?
+ 'mh-speed-toggle nil
" " 'ignore nil
198 'mh-speedbar-folder
0)
200 (setf (gethash nil mh-speed-folder-map
)
201 (set-marker (or (gethash nil mh-speed-folder-map
) (make-marker))
202 (1+ (mh-line-beginning-position))))
204 (mh-line-beginning-position) (1+ (line-beginning-position))
205 `(mh-folder nil mh-expanded nil mh-children-p t mh-level
0))
206 (mh-speed-stealth-update t
)
207 (when (> mh-speed-update-interval
0)
208 (mh-speed-flists nil
))))
211 (defalias 'mh-show-speedbar-buttons
'mh-folder-speedbar-buttons
)
213 (defalias 'mh-letter-speedbar-buttons
'mh-folder-speedbar-buttons
)
215 (defmacro mh-speed-select-attached-frame
()
216 "Compatibility macro to handle speedbar versions 0.11a and 0.14beta4."
217 (cond ((fboundp 'dframe-select-attached-frame
)
218 '(dframe-select-attached-frame speedbar-frame
))
219 ((boundp 'speedbar-attached-frame
)
220 '(select-frame speedbar-attached-frame
))
221 (t (error "Installed speedbar version not supported by MH-E"))))
223 (defun mh-speed-update-current-folder (force)
224 "Update speedbar highlighting of the current folder.
225 The function tries to be smart so that work done is minimized.
226 The currently highlighted folder is cached and no highlighting
227 happens unless it changes.
228 Also highlighting is suspended while the speedbar frame is selected.
229 Otherwise you get the disconcerting behavior of folders popping open
230 on their own when you are trying to navigate around in the speedbar
233 The update is always carried out if FORCE is non-nil."
234 (let* ((lastf (selected-frame))
235 (newcf (save-excursion
236 (mh-speed-select-attached-frame)
237 (prog1 (mh-speed-extract-folder-name (buffer-name))
238 (select-frame lastf
))))
239 (lastb (current-buffer))
240 (case-fold-search t
))
242 (and mh-speed-refresh-flag
(not (eq lastf speedbar-frame
)))
244 (equal (substring newcf
0 1) "+")
245 (not (equal newcf mh-speed-last-selected-folder
))))
246 (setq mh-speed-refresh-flag nil
)
247 (select-frame speedbar-frame
)
248 (set-buffer speedbar-buffer
)
250 ;; Remove highlight from previous match...
251 (mh-speed-highlight mh-speed-last-selected-folder
'mh-speedbar-folder
)
253 ;; If we found a match highlight it...
254 (when (mh-speed-goto-folder newcf
)
255 (mh-speed-highlight newcf
'mh-speedbar-selected-folder
))
257 (setq mh-speed-last-selected-folder newcf
)
258 (speedbar-position-cursor-on-line)
259 (set-window-point (frame-first-window speedbar-frame
) (point))
261 (select-frame lastf
))
262 (when (eq lastf speedbar-frame
)
263 (setq mh-speed-refresh-flag t
))))
265 (defun mh-speed-highlight (folder face
)
266 "Set FOLDER to FACE."
268 (speedbar-with-writable
269 (goto-char (gethash folder mh-speed-folder-map
(point)))
271 (if (re-search-forward "([1-9][0-9]*/[0-9]+)" (mh-line-end-position) t
)
272 (setq face
(mh-speed-bold-face face
))
273 (setq face
(mh-speed-normal-face face
)))
275 (when (re-search-forward "\\[.\\] " (mh-line-end-position) t
)
276 (put-text-property (point) (mh-line-end-position) 'face face
)))))
278 (defun mh-speed-normal-face (face)
279 "Return normal face for given FACE."
280 (cond ((eq face
'mh-speedbar-folder-with-unseen-messages
)
282 ((eq face
'mh-speedbar-selected-folder-with-unseen-messages
)
283 'mh-speedbar-selected-folder
)
286 (defun mh-speed-bold-face (face)
287 "Return bold face for given FACE."
288 (cond ((eq face
'mh-speedbar-folder
)
289 'mh-speedbar-folder-with-unseen-messages
)
290 ((eq face
'mh-speedbar-selected-folder
)
291 'mh-speedbar-selected-folder-with-unseen-messages
)
294 (defun mh-speed-goto-folder (folder)
295 "Move point to line containing FOLDER.
296 The function will expand out parent folders of FOLDER if needed."
297 (let ((prefix folder
)
300 (while (and (not (gethash prefix mh-speed-folder-map
)) last-slash
)
301 (setq last-slash
(mh-search-from-end ?
/ prefix
))
302 (when (integerp last-slash
)
303 (push (substring prefix
(1+ last-slash
)) suffix-list
)
304 (setq prefix
(substring prefix
0 last-slash
))))
305 (let ((prefix-position (gethash prefix mh-speed-folder-map
)))
307 (goto-char prefix-position
)
308 (goto-char (point-min))
310 (unless (get-text-property (point) 'mh-expanded
)
312 (goto-char (gethash prefix mh-speed-folder-map
))))
314 ;; We always need atleast one toggle. We need two if the directory list
315 ;; is stale since a folder was added.
316 (when (equal prefix
(get-text-property (mh-line-beginning-position)
319 (unless (get-text-property (point) 'mh-expanded
)
321 (setq prefix
(format "%s/%s" prefix
(pop suffix-list
)))
322 (goto-char (gethash prefix mh-speed-folder-map
(point))))
324 (equal folder
(get-text-property (point) 'mh-folder
))))
326 (defun mh-speed-extract-folder-name (buffer)
327 "Given an MH-E BUFFER find the folder that should be highlighted.
328 Do the right thing for the different kinds of buffers that MH-E
332 (cond ((eq major-mode
'mh-folder-mode
)
334 ((eq major-mode
'mh-show-mode
)
335 (set-buffer mh-show-folder-buffer
)
337 ((eq major-mode
'mh-letter-mode
)
338 (when (string-match mh-user-path buffer-file-name
)
339 (let* ((rel-path (substring buffer-file-name
(match-end 0)))
340 (directory-end (mh-search-from-end ?
/ rel-path
)))
342 (format "+%s" (substring rel-path
0 directory-end
)))))))))
344 (defun mh-speed-add-buttons (folder level
)
345 "Add speedbar button for FOLDER which is at indented by LEVEL amount."
346 (let ((folder-list (mh-sub-folders folder
)))
349 (let* ((folder-name (format "%s%s%s" (or folder
"+")
350 (if folder
"/" "") (car f
)))
351 (counts (gethash folder-name mh-speed-flists-cache
)))
352 (speedbar-with-writable
353 (speedbar-make-tag-line
354 'bracket
(if (cdr f
) ?
+ ?
)
359 (format " (%s/%s)" (car counts
) (cdr counts
))
362 (if (and counts
(> (car counts
) 0))
363 'mh-speedbar-folder-with-unseen-messages
368 (setf (gethash folder-name mh-speed-folder-map
)
369 (set-marker (or (gethash folder-name mh-speed-folder-map
)
371 (1+ (mh-line-beginning-position))))
373 (mh-line-beginning-position) (1+ (mh-line-beginning-position))
374 `(mh-folder ,folder-name
376 mh-children-p
,(not (not (cdr f
)))
377 ,@(if counts
`(mh-count
378 (,(car counts
) .
,(cdr counts
))) ())
379 mh-level
,level
))))))
382 (defvar mh-speed-current-folder nil
)
383 (defvar mh-speed-flists-folder nil
)
385 (defmacro mh-process-kill-without-query
(process)
386 "PROCESS can be killed without query on Emacs exit.
387 Avoid using `process-kill-without-query' if possible since it is
389 (if (fboundp 'set-process-query-on-exit-flag
)
390 `(set-process-query-on-exit-flag ,process nil
)
391 `(process-kill-without-query ,process
)))
394 (defun mh-speed-flists (force &rest folders
)
395 "Execute flists -recurse and update message counts.
396 If FORCE is non-nil the timer is reset.
398 Any number of optional FOLDERS can be specified. If specified,
399 flists is run only for that one folder."
400 (interactive (list t
))
402 (when mh-speed-flists-timer
403 (mh-cancel-timer mh-speed-flists-timer
)
404 (setq mh-speed-flists-timer nil
))
405 (when (and (processp mh-speed-flists-process
)
406 (not (eq (process-status mh-speed-flists-process
) 'exit
)))
407 (set-process-filter mh-speed-flists-process t
)
408 (kill-process mh-speed-flists-process
)
409 (setq mh-speed-partial-line
"")
410 (setq mh-speed-flists-process nil
)))
411 (setq mh-speed-flists-folder folders
)
412 (unless mh-speed-flists-timer
413 (setq mh-speed-flists-timer
415 nil
(if (> mh-speed-update-interval
0)
416 mh-speed-update-interval
419 (unless (and (processp mh-speed-flists-process
)
420 (not (eq (process-status mh-speed-flists-process
)
422 (setq mh-speed-current-folder
424 (if mh-speed-flists-folder
425 (substring (car (reverse mh-speed-flists-folder
)) 1)
427 (call-process (expand-file-name "folder" mh-progs
)
428 nil
'(t nil
) nil
"-fast")
429 (buffer-substring (point-min) (1- (point-max)))))
431 (setq mh-speed-flists-process
432 (apply #'start-process
"*flists*" nil
433 (expand-file-name "flists" mh-progs
)
434 (if mh-speed-flists-folder
"-noall" "-all")
435 "-sequence" (symbol-name mh-unseen-seq
)
436 (or mh-speed-flists-folder
'("-recurse"))))
437 ;; Run flists on all folders the next time around...
438 (setq mh-speed-flists-folder nil
)
439 (mh-process-kill-without-query mh-speed-flists-process
)
440 (set-process-filter mh-speed-flists-process
441 'mh-speed-parse-flists-output
)))))))
443 ;; Copied from mh-make-folder-list-filter...
444 ;; XXX Refactor to use mh-make-folder-list-filer?
445 (defun mh-speed-parse-flists-output (process output
)
446 "Parse the incremental results from flists.
447 PROCESS is the flists process and OUTPUT is the results that must
449 (let ((prevailing-match-data (match-data))
451 line-end line folder unseen total
)
453 (while (setq line-end
(string-match "\n" output position
))
454 (setq line
(format "%s%s"
455 mh-speed-partial-line
456 (substring output position line-end
))
457 mh-speed-partial-line
"")
458 (multiple-value-setq (folder unseen total
)
459 (mh-parse-flist-output-line line mh-speed-current-folder
))
460 (when (and folder unseen total
461 (let ((old-pair (gethash folder mh-speed-flists-cache
)))
462 (or (not (equal (car old-pair
) unseen
))
463 (not (equal (cdr old-pair
) total
)))))
464 (setf (gethash folder mh-speed-flists-cache
) (cons unseen total
))
466 (when (buffer-live-p (get-buffer speedbar-buffer
))
467 (set-buffer speedbar-buffer
)
468 (speedbar-with-writable
469 (when (get-text-property (point-min) 'mh-level
)
470 (let ((pos (gethash folder mh-speed-folder-map
))
474 (goto-char (mh-line-beginning-position))
476 ((null (get-text-property (point) 'mh-count
))
477 (goto-char (mh-line-end-position))
478 (setq face
(get-text-property (1- (point)) 'face
))
479 (insert (format " (%s/%s)" unseen total
))
480 (mh-speed-highlight 'unknown face
)
481 (goto-char (mh-line-beginning-position))
482 (add-text-properties (point) (1+ (point))
483 `(mh-count (,unseen .
,total
))))
484 ((not (equal (get-text-property (point) 'mh-count
)
485 (cons unseen total
)))
486 (goto-char (mh-line-end-position))
487 (setq face
(get-text-property (1- (point)) 'face
))
488 (re-search-backward " " (mh-line-beginning-position) t
)
489 (delete-region (point) (mh-line-end-position))
490 (insert (format " (%s/%s)" unseen total
))
491 (mh-speed-highlight 'unknown face
)
492 (goto-char (mh-line-beginning-position))
495 `(mh-count (,unseen .
,total
))))))))))))
496 (setq position
(1+ line-end
)))
497 (set-match-data prevailing-match-data
))
498 (setq mh-speed-partial-line
(substring output position
))))
501 (defun mh-speed-invalidate-map (folder)
502 "Remove FOLDER from various optimization caches."
503 (interactive (list ""))
505 (set-buffer speedbar-buffer
)
506 (let* ((speedbar-update-flag nil
)
507 (last-slash (mh-search-from-end ?
/ folder
))
508 (parent (if last-slash
(substring folder
0 last-slash
) nil
))
509 (parent-position (gethash parent mh-speed-folder-map
))
511 (when parent-position
512 (let ((parent-kids (mh-sub-folders parent
)))
513 (cond ((null parent-kids
)
514 (setq parent-change ?
+))
515 ((and (null (cdr parent-kids
))
516 (equal (if last-slash
517 (substring folder
(1+ last-slash
))
518 (substring folder
1))
520 (setq parent-change ?
))))
521 (goto-char parent-position
)
522 (when (equal (get-text-property (mh-line-beginning-position) 'mh-folder
)
524 (when (get-text-property (mh-line-beginning-position) 'mh-expanded
)
527 (speedbar-with-writable
528 (mh-speedbar-change-expand-button-char parent-change
)
530 (mh-line-beginning-position) (1+ (mh-line-beginning-position))
531 `(mh-children-p ,(equal parent-change ?
+)))))
532 (mh-speed-highlight mh-speed-last-selected-folder
'mh-speedbar-folder
)
533 (setq mh-speed-last-selected-folder nil
)
534 (setq mh-speed-refresh-flag t
)))
535 (when (equal folder
"")
536 (mh-clear-sub-folders-cache)))))
538 ;; Make it slightly more general to allow for [ ] buttons to be
540 (defun mh-speedbar-change-expand-button-char (char)
541 "Change the expansion button character to CHAR for the current line."
544 (if (re-search-forward "\\[.\\]" (mh-line-end-position) t
)
545 (speedbar-with-writable
548 (insert-char char
1 t
)
549 (put-text-property (point) (1- (point)) 'invisible nil
)
550 ;; make sure we fix the image on the text here.
551 (mh-funcall-if-exists
552 speedbar-insert-image-button-maybe
(- (point) 2) 3)))))
555 (defun mh-speed-add-folder (folder)
556 "Add FOLDER since it is being created.
557 The function invalidates the latest ancestor that is present."
559 (set-buffer speedbar-buffer
)
560 (let ((speedbar-update-flag nil
)
561 (last-slash (mh-search-from-end ?
/ folder
))
566 (setq ancestor
(substring ancestor
0 last-slash
))
567 (setq ancestor-pos
(gethash ancestor mh-speed-folder-map
))
569 (return-from while-loop
))
570 (setq last-slash
(mh-search-from-end ?
/ ancestor
))))
571 (unless ancestor-pos
(setq ancestor nil
))
572 (goto-char (or ancestor-pos
(gethash nil mh-speed-folder-map
)))
573 (speedbar-with-writable
574 (mh-speedbar-change-expand-button-char ?
+)
576 (mh-line-beginning-position) (1+ (mh-line-beginning-position))
578 (when (get-text-property (mh-line-beginning-position) 'mh-expanded
)
580 (setq mh-speed-refresh-flag t
))))
585 ;; indent-tabs-mode: nil
586 ;; sentence-end-double-space: nil
589 ;; arch-tag: d38ddcd4-3c00-4e37-99bf-8b89dda7b32c
590 ;;; mh-speed.el ends here