1 ;;; mh-speed.el --- MH-E speedbar support
3 ;; Copyright (C) 2002-2017 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 of the License, or
15 ;; (at your option) 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. If not, see <https://www.gnu.org/licenses/>.
27 ;; Future versions should only use flists.
41 (defvar mh-speed-refresh-flag nil
)
42 (defvar mh-speed-last-selected-folder nil
)
43 (defvar mh-speed-folder-map
(make-hash-table :test
#'equal
))
44 (defvar mh-speed-flists-cache
(make-hash-table :test
#'equal
))
45 (defvar mh-speed-flists-process nil
)
46 (defvar mh-speed-flists-timer nil
)
47 (defvar mh-speed-partial-line
"")
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
))))
65 (defvar mh-folder-speedbar-menu-items
67 ["Visit Folder" mh-speed-view
68 (with-current-buffer speedbar-buffer
69 (get-text-property (mh-line-beginning-position) 'mh-folder
))]
70 ["Expand Nested Folders" mh-speed-expand-folder
71 (and (get-text-property (mh-line-beginning-position) 'mh-children-p
)
72 (not (get-text-property (mh-line-beginning-position) 'mh-expanded
)))]
73 ["Contract Nested Folders" mh-speed-contract-folder
74 (and (get-text-property (mh-line-beginning-position) 'mh-children-p
)
75 (get-text-property (mh-line-beginning-position) 'mh-expanded
))]
76 ["Refresh Speedbar" mh-speed-refresh t
])
77 "Extra menu items for speedbar.")
79 (defvar mh-show-speedbar-menu-items mh-folder-speedbar-menu-items
)
80 (defvar mh-letter-speedbar-menu-items mh-folder-speedbar-menu-items
)
86 (defvar mh-folder-speedbar-key-map
(speedbar-make-specialized-keymap)
87 "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
95 (defvar mh-show-speedbar-key-map mh-folder-speedbar-key-map
)
96 (defvar mh-letter-speedbar-key-map mh-folder-speedbar-key-map
)
100 ;;; Speedbar Commands
104 (defalias 'mh-speed-contract-folder
'mh-speed-toggle
)
106 (defalias 'mh-speed-expand-folder
'mh-speed-toggle
)
108 (defun mh-speed-refresh ()
109 "Regenerates the list of folders in the speedbar.
111 Run this command if you've added or deleted a folder, or want to
112 update the unseen message count before the next automatic
116 (mh-speed-invalidate-map ""))
118 (defun mh-speed-stealth-update (&optional force
)
120 With non-nil FORCE, the update is always carried out."
121 (cond ((with-current-buffer speedbar-buffer
122 (get-text-property (point-min) 'mh-level
))
123 ;; Execute this hook and *don't* run anything else
124 (mh-speed-update-current-folder force
)
126 ;; Otherwise on to your regular programming
129 (defun mh-speed-toggle (&rest ignored
)
130 "Toggle the display of child folders in the speedbar.
131 The optional arguments from speedbar are IGNORED."
133 (declare (ignore args
))
135 (let ((parent (get-text-property (point) 'mh-folder
))
136 (kids-p (get-text-property (point) 'mh-children-p
))
137 (expanded (get-text-property (point) 'mh-expanded
))
138 (level (get-text-property (point) 'mh-level
))
141 (speedbar-with-writable
142 (cond ((not kids-p
) nil
)
145 (setq start-region
(point))
146 (while (and (get-text-property (point) 'mh-level
)
147 (> (get-text-property (point) 'mh-level
) level
))
148 (let ((folder (get-text-property (point) 'mh-folder
)))
149 (when (gethash folder mh-speed-folder-map
)
150 (set-marker (gethash folder mh-speed-folder-map
) nil
)
151 (remhash folder mh-speed-folder-map
)))
153 (delete-region start-region
(point))
155 (speedbar-change-expand-button-char ?
+)
157 (mh-line-beginning-position) (1+ (line-beginning-position))
161 (mh-speed-add-buttons parent
(1+ level
))
163 (speedbar-change-expand-button-char ?-
)
165 (mh-line-beginning-position) (1+ (line-beginning-position))
166 `(mh-expanded t
)))))))
168 (defun mh-speed-view (&rest ignored
)
169 "Visits the selected folder just as if you had used \\<mh-folder-mode-map>\\[mh-visit-folder].
170 The optional arguments from speedbar are IGNORED."
172 (declare (ignore args
))
173 (let* ((folder (get-text-property (mh-line-beginning-position) 'mh-folder
))
174 (range (and (stringp folder
)
175 (mh-read-range "Scan" folder t nil nil
176 mh-interpret-number-as-range-flag
))))
177 (when (stringp folder
)
178 (dframe-with-attached-buffer
179 (mh-visit-folder folder range
)
180 (delete-other-windows)))))
187 (defun mh-folder-speedbar-buttons (buffer)
188 "Interface function to create MH-E speedbar buffer.
189 BUFFER is the MH-E buffer for which the speedbar buffer is to be
191 (unless (get-text-property (point-min) 'mh-level
)
193 (clrhash mh-speed-folder-map
)
194 (speedbar-make-tag-line 'bracket ?
+ 'mh-speed-toggle nil
" " 'ignore nil
195 'mh-speedbar-folder
0)
197 (setf (gethash nil mh-speed-folder-map
)
198 (set-marker (or (gethash nil mh-speed-folder-map
) (make-marker))
199 (1+ (mh-line-beginning-position))))
201 (mh-line-beginning-position) (1+ (line-beginning-position))
202 `(mh-folder nil mh-expanded nil mh-children-p t mh-level
0))
203 (mh-speed-stealth-update t
)
204 (when (> mh-speed-update-interval
0)
205 (mh-speed-flists nil
))))
208 (defalias 'mh-show-speedbar-buttons
'mh-folder-speedbar-buttons
)
210 (defalias 'mh-letter-speedbar-buttons
'mh-folder-speedbar-buttons
)
212 (defmacro mh-speed-select-attached-frame
()
213 "Compatibility macro to handle speedbar versions 0.11a and 0.14beta4."
214 (cond ((fboundp 'dframe-select-attached-frame
)
215 '(dframe-select-attached-frame speedbar-frame
))
216 ((boundp 'speedbar-attached-frame
)
217 '(select-frame speedbar-attached-frame
))
218 (t (error "Installed speedbar version not supported by MH-E"))))
220 (defun mh-speed-update-current-folder (force)
221 "Update speedbar highlighting of the current folder.
222 The function tries to be smart so that work done is minimized.
223 The currently highlighted folder is cached and no highlighting
224 happens unless it changes.
225 Also highlighting is suspended while the speedbar frame is selected.
226 Otherwise you get the disconcerting behavior of folders popping open
227 on their own when you are trying to navigate around in the speedbar
230 The update is always carried out if FORCE is non-nil."
231 (let* ((lastf (selected-frame))
232 (newcf (save-excursion
233 (mh-speed-select-attached-frame)
234 (prog1 (mh-speed-extract-folder-name (buffer-name))
235 (select-frame lastf
))))
236 (lastb (current-buffer))
237 (case-fold-search t
))
239 (and mh-speed-refresh-flag
(not (eq lastf speedbar-frame
)))
241 (equal (substring newcf
0 1) "+")
242 (not (equal newcf mh-speed-last-selected-folder
))))
243 (setq mh-speed-refresh-flag nil
)
244 (select-frame speedbar-frame
)
245 (set-buffer speedbar-buffer
)
247 ;; Remove highlight from previous match...
248 (mh-speed-highlight mh-speed-last-selected-folder
'mh-speedbar-folder
)
250 ;; If we found a match highlight it...
251 (when (mh-speed-goto-folder newcf
)
252 (mh-speed-highlight newcf
'mh-speedbar-selected-folder
))
254 (setq mh-speed-last-selected-folder newcf
)
255 (speedbar-position-cursor-on-line)
256 (set-window-point (frame-first-window speedbar-frame
) (point))
258 (select-frame lastf
))
259 (when (eq lastf speedbar-frame
)
260 (setq mh-speed-refresh-flag t
))))
262 (defun mh-speed-highlight (folder face
)
263 "Set FOLDER to FACE."
265 (speedbar-with-writable
266 (goto-char (gethash folder mh-speed-folder-map
(point)))
268 (if (re-search-forward "([1-9][0-9]*/[0-9]+)" (mh-line-end-position) t
)
269 (setq face
(mh-speed-bold-face face
))
270 (setq face
(mh-speed-normal-face face
)))
272 (when (re-search-forward "\\[.\\] " (mh-line-end-position) t
)
273 (put-text-property (point) (mh-line-end-position) 'face face
)))))
275 (defun mh-speed-normal-face (face)
276 "Return normal face for given FACE."
277 (cond ((eq face
'mh-speedbar-folder-with-unseen-messages
)
279 ((eq face
'mh-speedbar-selected-folder-with-unseen-messages
)
280 'mh-speedbar-selected-folder
)
283 (defun mh-speed-bold-face (face)
284 "Return bold face for given FACE."
285 (cond ((eq face
'mh-speedbar-folder
)
286 'mh-speedbar-folder-with-unseen-messages
)
287 ((eq face
'mh-speedbar-selected-folder
)
288 'mh-speedbar-selected-folder-with-unseen-messages
)
291 (defun mh-speed-goto-folder (folder)
292 "Move point to line containing FOLDER.
293 The function will expand out parent folders of FOLDER if needed."
294 (let ((prefix folder
)
297 (while (and (not (gethash prefix mh-speed-folder-map
)) last-slash
)
298 (setq last-slash
(mh-search-from-end ?
/ prefix
))
299 (when (integerp last-slash
)
300 (push (substring prefix
(1+ last-slash
)) suffix-list
)
301 (setq prefix
(substring prefix
0 last-slash
))))
302 (let ((prefix-position (gethash prefix mh-speed-folder-map
)))
304 (goto-char prefix-position
)
305 (goto-char (point-min))
307 (unless (get-text-property (point) 'mh-expanded
)
309 (goto-char (gethash prefix mh-speed-folder-map
))))
311 ;; We always need atleast one toggle. We need two if the directory list
312 ;; is stale since a folder was added.
313 (when (equal prefix
(get-text-property (mh-line-beginning-position)
316 (unless (get-text-property (point) 'mh-expanded
)
318 (setq prefix
(format "%s/%s" prefix
(pop suffix-list
)))
319 (goto-char (gethash prefix mh-speed-folder-map
(point))))
321 (equal folder
(get-text-property (point) 'mh-folder
))))
323 (defun mh-speed-extract-folder-name (buffer)
324 "Given an MH-E BUFFER find the folder that should be highlighted.
325 Do the right thing for the different kinds of buffers that MH-E
327 (with-current-buffer buffer
328 (cond ((eq major-mode
'mh-folder-mode
)
330 ((eq major-mode
'mh-show-mode
)
331 (set-buffer mh-show-folder-buffer
)
333 ((eq major-mode
'mh-letter-mode
)
334 (when (string-match mh-user-path buffer-file-name
)
335 (let* ((rel-path (substring buffer-file-name
(match-end 0)))
336 (directory-end (mh-search-from-end ?
/ rel-path
)))
338 (format "+%s" (substring rel-path
0 directory-end
)))))))))
340 (defun mh-speed-add-buttons (folder level
)
341 "Add speedbar button for FOLDER which is at indented by LEVEL amount."
342 (let ((folder-list (mh-sub-folders folder
)))
345 (let* ((folder-name (format "%s%s%s" (or folder
"+")
346 (if folder
"/" "") (car f
)))
347 (counts (gethash folder-name mh-speed-flists-cache
)))
348 (speedbar-with-writable
349 (speedbar-make-tag-line
350 'bracket
(if (cdr f
) ?
+ ?
)
355 (format " (%s/%s)" (car counts
) (cdr counts
))
358 (if (and counts
(> (car counts
) 0))
359 'mh-speedbar-folder-with-unseen-messages
364 (setf (gethash folder-name mh-speed-folder-map
)
365 (set-marker (or (gethash folder-name mh-speed-folder-map
)
367 (1+ (mh-line-beginning-position))))
369 (mh-line-beginning-position) (1+ (mh-line-beginning-position))
370 `(mh-folder ,folder-name
372 mh-children-p
,(not (not (cdr f
)))
373 ,@(if counts
`(mh-count
374 (,(car counts
) .
,(cdr counts
))) ())
375 mh-level
,level
))))))
378 (defvar mh-speed-current-folder nil
)
379 (defvar mh-speed-flists-folder nil
)
381 (defmacro mh-process-kill-without-query
(process)
382 "PROCESS can be killed without query on Emacs exit.
383 Avoid using `process-kill-without-query' if possible since it is
385 (if (fboundp 'set-process-query-on-exit-flag
)
386 `(set-process-query-on-exit-flag ,process nil
)
387 `(process-kill-without-query ,process
)))
390 (defun mh-speed-flists (force &rest folders
)
391 "Execute flists -recurse and update message counts.
392 If FORCE is non-nil the timer is reset.
394 Any number of optional FOLDERS can be specified. If specified,
395 flists is run only for that one folder."
396 (interactive (list t
))
398 (when mh-speed-flists-timer
399 (mh-cancel-timer mh-speed-flists-timer
)
400 (setq mh-speed-flists-timer nil
))
401 (when (and (processp mh-speed-flists-process
)
402 (not (eq (process-status mh-speed-flists-process
) 'exit
)))
403 (set-process-filter mh-speed-flists-process t
)
404 (kill-process mh-speed-flists-process
)
405 (setq mh-speed-partial-line
"")
406 (setq mh-speed-flists-process nil
)))
407 (setq mh-speed-flists-folder folders
)
408 (unless mh-speed-flists-timer
409 (setq mh-speed-flists-timer
411 nil
(if (> mh-speed-update-interval
0)
412 mh-speed-update-interval
415 (unless (and (processp mh-speed-flists-process
)
416 (not (eq (process-status mh-speed-flists-process
)
418 (setq mh-speed-current-folder
420 (if mh-speed-flists-folder
421 (substring (car (reverse mh-speed-flists-folder
)) 1)
423 (call-process (expand-file-name "folder" mh-progs
)
424 nil
'(t nil
) nil
"-fast")
425 (buffer-substring (point-min) (1- (point-max)))))
427 (setq mh-speed-flists-process
428 (apply #'start-process
"*flists*" nil
429 (expand-file-name "flists" mh-progs
)
430 (if mh-speed-flists-folder
"-noall" "-all")
431 "-sequence" (symbol-name mh-unseen-seq
)
432 (or mh-speed-flists-folder
'("-recurse"))))
433 ;; Run flists on all folders the next time around...
434 (setq mh-speed-flists-folder nil
)
435 (mh-process-kill-without-query mh-speed-flists-process
)
436 (set-process-filter mh-speed-flists-process
437 'mh-speed-parse-flists-output
)))))))
439 ;; Copied from mh-make-folder-list-filter...
440 ;; XXX Refactor to use mh-make-folder-list-filer?
441 (defun mh-speed-parse-flists-output (process output
)
442 "Parse the incremental results from flists.
443 PROCESS is the flists process and OUTPUT is the results that must
445 (let ((prevailing-match-data (match-data))
447 line-end line folder unseen total
)
449 (while (setq line-end
(string-match "\n" output position
))
450 (setq line
(format "%s%s"
451 mh-speed-partial-line
452 (substring output position line-end
))
453 mh-speed-partial-line
"")
454 (multiple-value-setq (folder unseen total
)
456 (mh-parse-flist-output-line line mh-speed-current-folder
)))
457 (when (and folder unseen total
458 (let ((old-pair (gethash folder mh-speed-flists-cache
)))
459 (or (not (equal (car old-pair
) unseen
))
460 (not (equal (cdr old-pair
) total
)))))
461 (setf (gethash folder mh-speed-flists-cache
) (cons unseen total
))
462 (when (buffer-live-p (get-buffer speedbar-buffer
))
463 (with-current-buffer speedbar-buffer
464 (speedbar-with-writable
465 (when (get-text-property (point-min) 'mh-level
)
466 (let ((pos (gethash folder mh-speed-folder-map
))
470 (goto-char (mh-line-beginning-position))
472 ((null (get-text-property (point) 'mh-count
))
473 (goto-char (mh-line-end-position))
474 (setq face
(get-text-property (1- (point)) 'face
))
475 (insert (format " (%s/%s)" unseen total
))
476 (mh-speed-highlight 'unknown face
)
477 (goto-char (mh-line-beginning-position))
478 (add-text-properties (point) (1+ (point))
479 `(mh-count (,unseen .
,total
))))
480 ((not (equal (get-text-property (point) 'mh-count
)
481 (cons unseen total
)))
482 (goto-char (mh-line-end-position))
483 (setq face
(get-text-property (1- (point)) 'face
))
484 (re-search-backward " " (mh-line-beginning-position) t
)
485 (delete-region (point) (mh-line-end-position))
486 (insert (format " (%s/%s)" unseen total
))
487 (mh-speed-highlight 'unknown face
)
488 (goto-char (mh-line-beginning-position))
491 `(mh-count (,unseen .
,total
))))))))))))
492 (setq position
(1+ line-end
)))
493 (set-match-data prevailing-match-data
))
494 (setq mh-speed-partial-line
(substring output position
))))
497 (defun mh-speed-invalidate-map (folder)
498 "Remove FOLDER from various optimization caches."
499 (interactive (list ""))
500 (with-current-buffer speedbar-buffer
501 (let* ((speedbar-update-flag nil
)
502 (last-slash (mh-search-from-end ?
/ folder
))
503 (parent (if last-slash
(substring folder
0 last-slash
) nil
))
504 (parent-position (gethash parent mh-speed-folder-map
))
506 (when parent-position
507 (let ((parent-kids (mh-sub-folders parent
)))
508 (cond ((null parent-kids
)
509 (setq parent-change ?
+))
510 ((and (null (cdr parent-kids
))
511 (equal (if last-slash
512 (substring folder
(1+ last-slash
))
513 (substring folder
1))
515 (setq parent-change ?
))))
516 (goto-char parent-position
)
517 (when (equal (get-text-property (mh-line-beginning-position) 'mh-folder
)
519 (when (get-text-property (mh-line-beginning-position) 'mh-expanded
)
522 (speedbar-with-writable
523 (mh-speedbar-change-expand-button-char parent-change
)
525 (mh-line-beginning-position) (1+ (mh-line-beginning-position))
526 `(mh-children-p ,(equal parent-change ?
+)))))
527 (mh-speed-highlight mh-speed-last-selected-folder
'mh-speedbar-folder
)
528 (setq mh-speed-last-selected-folder nil
)
529 (setq mh-speed-refresh-flag t
)))
530 (when (equal folder
"")
531 (mh-clear-sub-folders-cache)))))
533 ;; Make it slightly more general to allow for [ ] buttons to be
535 (defun mh-speedbar-change-expand-button-char (char)
536 "Change the expansion button character to CHAR for the current line."
539 (if (re-search-forward "\\[.\\]" (mh-line-end-position) t
)
540 (speedbar-with-writable
543 (insert-char char
1 t
)
544 (put-text-property (point) (1- (point)) 'invisible nil
)
545 ;; make sure we fix the image on the text here.
546 (mh-funcall-if-exists
547 speedbar-insert-image-button-maybe
(- (point) 2) 3)))))
550 (defun mh-speed-add-folder (folder)
551 "Add FOLDER since it is being created.
552 The function invalidates the latest ancestor that is present."
553 (with-current-buffer speedbar-buffer
554 (let ((speedbar-update-flag nil
)
555 (last-slash (mh-search-from-end ?
/ folder
))
560 (setq ancestor
(substring ancestor
0 last-slash
))
561 (setq ancestor-pos
(gethash ancestor mh-speed-folder-map
))
563 (return-from while-loop
))
564 (setq last-slash
(mh-search-from-end ?
/ ancestor
))))
565 (unless ancestor-pos
(setq ancestor nil
))
566 (goto-char (or ancestor-pos
(gethash nil mh-speed-folder-map
)))
567 (speedbar-with-writable
568 (mh-speedbar-change-expand-button-char ?
+)
570 (mh-line-beginning-position) (1+ (mh-line-beginning-position))
572 (when (get-text-property (mh-line-beginning-position) 'mh-expanded
)
574 (setq mh-speed-refresh-flag t
))))
579 ;; indent-tabs-mode: nil
580 ;; sentence-end-double-space: nil
583 ;;; mh-speed.el ends here