1 ;;; mh-utils.el --- MH-E general utilities
3 ;; Copyright (C) 1993, 1995, 1997,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Bill Wohler <wohler@newt.com>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
42 (defun mh-search-from-end (char string)
43 "Return the position of last occurrence of CHAR in STRING.
44 If CHAR is not present in STRING then return nil. The function is
45 used in lieu of `search' in the CL package."
46 (loop for index from (1- (length string)) downto 0
47 when (equal (aref string index) char) return index
55 (defun mh-beginning-of-word (&optional n)
56 "Return position of the N th word backwards."
58 (let ((syntax-table (syntax-table)))
61 (mh-mail-abbrev-make-syntax-table)
62 (set-syntax-table mail-abbrev-syntax-table)
65 (set-syntax-table syntax-table))))
68 (defun mh-colors-available-p ()
69 "Check if colors are available in the Emacs being used."
71 (let ((color-cells (mh-display-color-cells)))
72 (and (numberp color-cells) (>= color-cells 8)))))
75 (defun mh-colors-in-use-p ()
76 "Check if colors are being used in the folder buffer."
77 (and mh-colors-available-flag font-lock-mode))
80 (defun mh-delete-line (lines)
81 "Delete the next LINES lines."
82 (delete-region (point) (progn (forward-line lines) (point))))
85 (defun mh-image-load-path (library image &optional path)
86 "Return a suitable search path for images of LIBRARY.
88 Images for LIBRARY are searched for in \"../../etc/images\" and
89 \"../etc/images\" relative to the files in \"lisp/LIBRARY\", in
90 `image-load-path', or in `load-path'.
92 This function returns value of `load-path' augmented with the
93 path to IMAGE. If PATH is given, it is used instead of
95 (unless library (error "No library specified"))
96 (unless image (error "No image specified"))
97 (let ((mh-image-directory))
99 ;; Try relative setting.
100 ((let (mh-library-name d1ei d2ei)
101 ;; First, find library in the load-path.
102 (setq mh-library-name (locate-library library))
103 (if (not mh-library-name)
104 (error "Cannot find library %s in load-path" library))
105 ;; And then set mh-image-directory relative to that.
108 d2ei (expand-file-name
109 (concat (file-name-directory mh-library-name)
112 d1ei (expand-file-name
113 (concat (file-name-directory mh-library-name)
115 (setq mh-image-directory
116 ;; Set it to nil if image is not found.
117 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
118 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
119 ;; Check for images in image-load-path or load-path.
122 ;; Images in image-load-path.
123 (mh-image-search-load-path image)
124 ;; Images in load-path.
125 (locate-library image)))
127 ;; Since the image might be in a nested directory
128 ;; (for example, mail/attach.pbm), adjust `mh-image-directory'
131 (setq dir (file-name-directory dir))
133 (while (setq parent (file-name-directory img))
134 (setq img (directory-file-name parent)
135 dir (expand-file-name "../" dir)))
136 (setq mh-image-directory dir))))))
138 (unless (file-exists-p mh-image-directory)
139 (error "Directory %s in mh-image-directory does not exist"
141 (unless (file-exists-p (expand-file-name image mh-image-directory))
142 (error "Directory %s in mh-image-directory does not contain image %s"
143 mh-image-directory image))
144 ;; Return augmented `image-load-path' or `load-path'.
145 (cond ((and path (symbolp path))
146 (nconc (list mh-image-directory)
147 (delete mh-image-directory
149 (copy-sequence (symbol-value path))
152 (nconc (list mh-image-directory)
153 (delete mh-image-directory
154 (copy-sequence load-path)))))))
157 (defun mh-make-local-vars (&rest pairs)
158 "Initialize local variables according to the variable-value PAIRS."
160 (set (make-local-variable (car pairs)) (car (cdr pairs)))
161 (setq pairs (cdr (cdr pairs)))))
164 (defun mh-mapc (function list)
165 "Apply FUNCTION to each element of LIST for side effects only."
167 (funcall function (car list))
168 (setq list (cdr list))))
170 (defvar mh-pick-regexp-chars ".*$["
171 "List of special characters in pick regular expressions.")
174 (defun mh-quote-pick-expr (pick-expr)
175 "Quote `mh-pick-regexp-chars' in PICK-EXPR.
176 PICK-EXPR is a list of strings. Return nil if PICK-EXPR is nil."
177 (let ((quoted-pick-expr))
178 (dolist (string pick-expr)
180 (not (string-equal string "")))
181 (loop for i from 0 to (1- (length mh-pick-regexp-chars)) do
182 (let ((s (string ?\\ (aref mh-pick-regexp-chars i))))
183 (setq string (mh-replace-regexp-in-string s s string t t))))
184 (setq quoted-pick-expr (append quoted-pick-expr (list string)))))
188 (defun mh-replace-string (old new)
189 "Replace all occurrences of OLD with NEW in the current buffer.
190 Ignores case when searching for OLD."
191 (goto-char (point-min))
192 (let ((case-fold-search t))
193 (while (search-forward old nil t)
194 (replace-match new t t))))
200 (defvar mh-logo-cache nil)
203 (defun mh-logo-display ()
204 "Modify mode line to display MH-E logo."
207 (mh-image-load-path "mh-e" "mh-logo.xpm" 'load-path))
209 (mh-image-load-path "mh-e" "mh-logo.xpm" 'image-load-path)))
212 `(display ,(or mh-logo-cache
214 (mh-funcall-if-exists
215 find-image '((:type xpm :ascent center
216 :file "mh-logo.xpm"))))))
217 (car mode-line-buffer-identification))))
219 (setq modeline-buffer-identification
221 (if mh-modeline-glyph
222 (cons modeline-buffer-id-left-extent mh-modeline-glyph)
223 (cons modeline-buffer-id-left-extent "XEmacs%N:"))
224 (cons modeline-buffer-id-right-extent " %17b")))))
230 (defvar mh-find-path-run nil
231 "Non-nil if `mh-find-path' has been run already.
232 Do not access this variable; `mh-find-path' already uses it to
233 avoid running more than once.")
236 (defun mh-find-path ()
237 "Set variables from user's MH profile.
239 This function sets `mh-user-path' from your \"Path:\" MH profile
240 component (but defaults to \"Mail\" if one isn't present),
241 `mh-draft-folder' from \"Draft-Folder:\", `mh-unseen-seq' from
242 \"Unseen-Sequence:\", `mh-previous-seq' from
243 \"Previous-Sequence:\", and `mh-inbox' from \"Inbox:\" (defaults
246 The hook `mh-find-path-hook' is run after these variables have
247 been set. This hook can be used the change the value of these
248 variables if you need to run with different values between MH and
250 (unless mh-find-path-run
252 (if (and (getenv "MH")
253 (not (file-readable-p (getenv "MH"))))
254 (error "MH environment variable contains unreadable file %s"
256 (if (null (mh-variants))
257 (error "Install MH and run install-mh before running MH-E"))
258 (let ((profile "~/.mh_profile"))
259 (if (not (file-readable-p profile))
260 (error "Run install-mh before running MH-E")))
262 (setq mh-user-path (mh-profile-component "Path"))
263 (if (not mh-user-path)
264 (setq mh-user-path "Mail"))
266 (file-name-as-directory
267 (expand-file-name mh-user-path (expand-file-name "~"))))
268 (mh-set-x-image-cache-directory (expand-file-name ".mhe-x-image-cache"
270 (setq mh-draft-folder (mh-profile-component "Draft-Folder"))
273 (if (not (mh-folder-name-p mh-draft-folder))
274 (setq mh-draft-folder (format "+%s" mh-draft-folder)))
275 (if (not (file-exists-p (mh-expand-file-name mh-draft-folder)))
277 "Draft folder \"%s\" not found; create it and try again"
278 (mh-expand-file-name mh-draft-folder)))))
279 (setq mh-inbox (mh-profile-component "Inbox"))
280 (cond ((not mh-inbox)
281 (setq mh-inbox "+inbox"))
282 ((not (mh-folder-name-p mh-inbox))
283 (setq mh-inbox (format "+%s" mh-inbox))))
284 (setq mh-unseen-seq (mh-profile-component "Unseen-Sequence"))
286 (setq mh-unseen-seq (intern mh-unseen-seq))
287 (setq mh-unseen-seq 'unseen)) ;old MH default?
288 (setq mh-previous-seq (mh-profile-component "Previous-Sequence"))
290 (setq mh-previous-seq (intern mh-previous-seq)))
291 (run-hooks 'mh-find-path-hook)
292 (mh-collect-folder-names)
293 (setq mh-find-path-run t)))
300 (defun mh-ephem-message (string)
301 "Display STRING in the minibuffer momentarily."
302 (message "%s" string)
306 (defvar mh-help-default nil
307 "Mode to use if messages are not present for the current mode.")
309 (defvar mh-help-messages nil
310 "Help messages for all modes.
311 This is an alist of alists. The primary key is a symbol
312 representing the mode; the value is described in `mh-set-help'.")
315 (defun mh-set-help (messages &optional default)
318 The MESSAGES are assumed to be an associative array. It is used
319 to show help for the most common commands in the current mode.
320 The key is a prefix char. The value is one or more strings which
321 are concatenated together and displayed in a help buffer if ? is
322 pressed after the prefix character. The special key nil is used
323 to display the non-prefixed commands.
325 The substitutions described in `substitute-command-keys' are performed as
328 If optional argument DEFAULT is non-nil, then these messages will
329 be used if help is asked for an unknown mode."
330 (add-to-list 'mh-help-messages (cons major-mode messages))
332 (setq mh-help-default major-mode)))
335 (defun mh-help (&optional help-messages)
336 "Display cheat sheet for the MH-E commands.
337 See `mh-set-help' for setting the help messages.
338 HELP-MESSAGES are used instead if given.
339 This is a list of one or more strings which are concatenated together
340 and displayed in a help buffer."
342 (let* ((help (or help-messages
343 (cdr (assoc nil (assoc major-mode mh-help-messages)))))
344 (text (substitute-command-keys (mapconcat 'identity help ""))))
352 (defun mh-prefix-help ()
353 "Display cheat sheet for the commands of the current prefix in minibuffer."
355 ;; We got here because the user pressed a "?", but he pressed a prefix key
356 ;; before that. Since the the key vector starts at index 0, the index of the
357 ;; last keystroke is length-1 and thus the second to last keystroke is at
358 ;; length-2. We use that information to obtain a suitable prefix character
359 ;; from the recent keys.
360 (let* ((keys (recent-keys))
361 (prefix-char (elt keys (- (length keys) 2)))
362 (help (cdr (assoc prefix-char (assoc major-mode mh-help-messages)))))
367 ;;; Message Number Utilities
370 (defun mh-coalesce-msg-list (messages)
371 "Given a list of MESSAGES, return a list of message number ranges.
372 This is the inverse of `mh-read-msg-list', which expands ranges.
373 Message lists passed to MH programs should be processed by this
374 function to avoid exceeding system command line argument limits."
375 (let ((msgs (sort (copy-sequence messages) 'mh-greaterp))
381 (if (or (not (numberp prev))
382 (not (equal (car msgs) (1- prev))))
383 (progn ;non-sequential, flush old range
384 (if (eq prev range-high)
385 (setq ranges (cons range-high ranges))
386 (setq ranges (cons (format "%s-%s" prev range-high) ranges)))
387 (setq range-high nil))))
389 (setq range-high (car msgs))) ;start new or first range
390 (setq prev (car msgs))
391 (setq msgs (cdr msgs)))
394 (defun mh-greaterp (msg1 msg2)
395 "Return the greater of two message indicators MSG1 and MSG2.
396 Strings are \"smaller\" than numbers.
397 Valid values are things like \"cur\", \"last\", 1, and 1820."
404 (string-lessp msg2 msg1))))
407 (defun mh-lessp (msg1 msg2)
408 "Return the lesser of two message indicators MSG1 and MSG2.
409 Strings are \"smaller\" than numbers.
410 Valid values are things like \"cur\", \"last\", 1, and 1820."
411 (not (mh-greaterp msg1 msg2)))
414 (defun mh-get-msg-num (error-if-no-message)
415 "Return the message number of the displayed message.
416 If the argument ERROR-IF-NO-MESSAGE is non-nil, then complain if
417 the cursor is not pointing to a message."
420 (cond ((looking-at (mh-scan-msg-number-regexp))
421 (string-to-number (buffer-substring (match-beginning 1)
424 (error "Cursor not pointing to message"))
427 (add-to-list 'debug-ignored-errors "^Cursor not pointing to message$")
431 ;;; Folder Cache and Access
433 (defvar mh-sub-folders-cache (make-hash-table :test #'equal))
434 (defvar mh-current-folder-name nil)
435 (defvar mh-flists-partial-line "")
436 (defvar mh-flists-process nil)
439 (defun mh-clear-sub-folders-cache ()
440 "Clear `mh-sub-folders-cache'."
441 (clrhash mh-sub-folders-cache))
443 ;; Initialize mh-sub-folders-cache...
444 (defun mh-collect-folder-names ()
445 "Collect folder names by running \"folders\"."
446 (unless mh-flists-process
447 (setq mh-flists-process
448 (mh-exec-cmd-daemon "folders" 'mh-collect-folder-names-filter
449 "-recurse" "-fast"))))
451 (defun mh-collect-folder-names-filter (process output)
453 PROCESS is the flists process that was run to collect folder
454 names and the function is called when OUTPUT is available."
456 (prevailing-match-data (match-data))
459 (while (setq line-end (string-match "\n" output position))
460 (setq folder (format "+%s%s"
461 mh-flists-partial-line
462 (substring output position line-end)))
463 (setq mh-flists-partial-line "")
464 (unless (equal (aref folder 1) ?.)
465 (mh-populate-sub-folders-cache folder))
466 (setq position (1+ line-end)))
467 (set-match-data prevailing-match-data))
468 (setq mh-flists-partial-line (substring output position))))
470 (defun mh-populate-sub-folders-cache (folder)
471 "Tell `mh-sub-folders-cache' about FOLDER."
472 (let* ((last-slash (mh-search-from-end ?/ folder))
473 (child1 (substring folder (1+ (or last-slash 0))))
474 (parent (and last-slash (substring folder 0 last-slash)))
475 (parent-slash (and parent (mh-search-from-end ?/ parent)))
476 (child2 (and parent (substring parent (1+ (or parent-slash 0)))))
477 (grand-parent (and parent-slash (substring parent 0 parent-slash)))
478 (cache-entry (gethash parent mh-sub-folders-cache)))
479 (unless (loop for x in cache-entry when (equal (car x) child1) return t
481 (push (list child1) cache-entry)
482 (setf (gethash parent mh-sub-folders-cache)
483 (sort cache-entry (lambda (x y) (string< (car x) (car y)))))
485 (loop for x in (gethash grand-parent mh-sub-folders-cache)
486 when (equal (car x) child2)
487 do (progn (setf (cdr x) t) (return)))))))
489 (defun mh-normalize-folder-name (folder &optional empty-string-okay
490 dont-remove-trailing-slash)
491 "Normalizes FOLDER name.
493 Makes sure that two '/' characters never occur next to each
494 other. Also all occurrences of \"..\" and \".\" are suitably
495 processed. So \"+inbox/../news\" will be normalized to \"+news\".
497 If optional argument EMPTY-STRING-OKAY is nil then a '+' is added
498 at the front if FOLDER lacks one. If non-nil and FOLDER is the
499 empty string then nothing is added.
501 If optional argument DONT-REMOVE-TRAILING-SLASH is non-nil then a
502 trailing '/' if present is retained (if present), otherwise it is
504 (when (stringp folder)
505 ;; Replace two or more consecutive '/' characters with a single '/'
506 (while (string-match "//" folder)
507 (setq folder (replace-match "/" nil t folder)))
508 (let* ((length (length folder))
509 (trailing-slash-present (and (> length 0)
510 (equal (aref folder (1- length)) ?/)))
511 (leading-slash-present (and (> length 0)
512 (equal (aref folder 0) ?/))))
513 (when (and (> length 0) (equal (aref folder 0) ?@)
514 (stringp mh-current-folder-name))
515 (setq folder (format "%s/%s/" mh-current-folder-name
516 (substring folder 1))))
517 ;; XXX: Purge empty strings from the list that split-string returns. In
518 ;; XEmacs, (split-string "+foo/" "/") returns ("+foo" "") while in GNU
519 ;; Emacs it returns ("+foo"). In the code it is assumed that the
520 ;; components list has no empty strings.
521 (let ((components (delete "" (split-string folder "/")))
523 ;; Remove .. and . from the pathname.
524 (dolist (component components)
525 (cond ((and (equal component "..") result)
527 ((equal component ".."))
528 ((equal component "."))
529 (t (push component result))))
531 (dolist (component result)
532 (setq folder (concat component "/" folder)))
533 ;; Remove trailing '/' if needed.
534 (unless (and trailing-slash-present dont-remove-trailing-slash)
535 (when (not (equal folder ""))
536 (setq folder (substring folder 0 (1- (length folder))))))
537 (when leading-slash-present
538 (setq folder (concat "/" folder)))))
539 (cond ((and empty-string-okay (equal folder "")))
540 ((equal folder "") (setq folder "+"))
541 ((not (equal (aref folder 0) ?+)) (setq folder (concat "+" folder)))))
544 (defmacro mh-children-p (folder)
545 "Return t if FOLDER from sub-folders cache has children.
546 The car of folder is the name, and the cdr is either t or some
547 sort of count that I do not understand. It's too small to be the
548 number of messages in the sub-folders and too large to be the
549 number of sub-folders. XXX"
555 (defun mh-folder-list (folder)
556 "Return FOLDER and its descendents.
557 FOLDER may have a + prefix. Returns a list of strings without the
558 + prefix. If FOLDER is nil, then all folders are considered. For
559 example, if your Mail directory only contains the folders +inbox,
560 +outbox, +lists, and +lists/mh-e, then
563 => (\"inbox\" \"lists\" \"lists/mh-e\" \"outbox\")
564 (mh-folder-list \"+lists\")
567 Respects the value of `mh-recursive-folders-flag'. If this flag
568 is nil, and the sub-folders have not been explicitly viewed, then
569 they will not be returned."
571 ;; Normalize folder. Strip leading +. Add trailing slash (done in
572 ;; two steps to avoid infinite loops when replacing "/*$" with "/"
573 ;; in XEmacs). If no folder is specified, ensure it is nil to
574 ;; ensure we get the top-level folders; otherwise mh-sub-folders
575 ;; returns all the files in / if given an empty string or +.
577 (setq folder (mh-replace-regexp-in-string "^\+" "" folder))
578 (setq folder (mh-replace-regexp-in-string "/+$" "" folder)))
579 ;; Add provided folder to list, unless all folders are asked for.
580 (unless (null folder)
581 (setq folder-list (list folder)))
582 (loop for f in (mh-sub-folders folder) do
585 (if (mh-children-p f)
586 (mh-folder-list (concat folder "/" (car f)))
587 (list (concat folder "/" (car f)))))))
591 (defun mh-sub-folders (folder &optional add-trailing-slash-flag)
592 "Find the subfolders of FOLDER.
593 The function avoids running folders unnecessarily by caching the
594 results of the actual folders call.
596 If optional argument ADD-TRAILING-SLASH-FLAG is non-nil then a
597 slash is added to each of the sub-folder names that may have
598 nested folders within them."
599 (let* ((folder (mh-normalize-folder-name folder))
600 (match (gethash folder mh-sub-folders-cache 'no-result))
601 (sub-folders (cond ((eq match 'no-result)
602 (setf (gethash folder mh-sub-folders-cache)
603 (mh-sub-folders-actual folder)))
605 (if add-trailing-slash-flag
606 (mapcar #'(lambda (x)
607 (if (cdr x) (cons (concat (car x) "/") (cdr x)) x))
611 (defun mh-sub-folders-actual (folder)
612 "Execute the command folders to return the sub-folders of FOLDER.
613 Filters out the folder names that start with \".\" so that
614 directories that aren't usually mail folders are hidden."
615 (let ((arg-list `(,(expand-file-name "folders" mh-progs)
616 nil (t nil) nil "-noheader" "-norecurse" "-nototal"
617 ,@(if (stringp folder) (list folder) ())))
619 (current-folder (concat
621 (call-process (expand-file-name "folder" mh-progs)
622 nil '(t nil) nil "-fast")
623 (buffer-substring (point-min) (1- (point-max))))
626 (apply #'call-process arg-list)
627 (goto-char (point-min))
628 (while (not (and (eolp) (bolp)))
629 (goto-char (mh-line-end-position))
630 (let ((start-pos (mh-line-beginning-position))
631 (has-pos (search-backward " has "
632 (mh-line-beginning-position) t)))
633 (when (integerp has-pos)
634 (while (equal (char-after has-pos) ? )
637 (while (equal (char-after start-pos) ? )
639 (let* ((name (buffer-substring start-pos has-pos))
640 (first-char (aref name 0))
641 (last-char (aref name (1- (length name)))))
642 (unless (member first-char '(?. ?# ?,))
643 (when (and (equal last-char ?+) (equal name current-folder))
644 (setq name (substring name 0 (1- (length name)))))
647 (search-forward "(others)" (mh-line-end-position) t))
650 (setq results (nreverse results))
651 (when (stringp folder)
652 (setq results (cdr results))
653 (let ((folder-name-len (length (format "%s/" (substring folder 1)))))
654 (setq results (mapcar (lambda (f)
655 (cons (substring (car f) folder-name-len)
661 (defun mh-remove-from-sub-folders-cache (folder)
662 "Remove FOLDER and its parent from `mh-sub-folders-cache'.
663 FOLDER should be unconditionally removed from the cache. Also the
664 last ancestor of FOLDER present in the cache must be removed as
667 To see why this is needed assume we have a folder +foo which has
668 a single sub-folder qux. Now we create the folder +foo/bar/baz.
669 Here we will need to invalidate the cached sub-folders of +foo,
670 otherwise completion on +foo won't tell us about the option
672 (remhash folder mh-sub-folders-cache)
673 (block ancestor-found
674 (let ((parent folder)
675 (one-ancestor-found nil)
677 (while (setq last-slash (mh-search-from-end ?/ parent))
678 (setq parent (substring parent 0 last-slash))
679 (unless (eq (gethash parent mh-sub-folders-cache 'none) 'none)
680 (remhash parent mh-sub-folders-cache)
681 (if one-ancestor-found
682 (return-from ancestor-found)
683 (setq one-ancestor-found t))))
684 (remhash nil mh-sub-folders-cache))))
691 (defun mh-folder-name-p (name)
692 "Return non-nil if NAME is the name of a folder.
693 A name (a string or symbol) can be a folder name if it begins
696 (eq (aref (symbol-name name) 0) ?+)
697 (and (> (length name) 0)
698 (eq (aref name 0) ?+))))
701 (defun mh-expand-file-name (filename &optional default)
702 "Expand FILENAME like `expand-file-name', but also handle MH folder names.
703 Any filename that starts with '+' is treated as a folder name.
704 See `expand-file-name' for description of DEFAULT."
705 (if (mh-folder-name-p filename)
706 (expand-file-name (substring filename 1) mh-user-path)
707 (expand-file-name filename default)))
709 (defvar mh-folder-hist nil)
712 (eval-when-compile (defvar mh-speed-flists-cache))
714 (defvar mh-allow-root-folder-flag nil
715 "Non-nil means \"+\" is an acceptable folder name.
716 This variable is used to communicate with
717 `mh-folder-completion-function'. That function can have exactly
718 three arguments so we bind this variable to t or nil.
720 This variable should never be set.")
722 (defvar mh-folder-completion-map (copy-keymap minibuffer-local-completion-map))
723 (define-key mh-folder-completion-map " " 'minibuffer-complete) ;Why???
725 (defvar mh-speed-flists-inhibit-flag nil)
728 (defun mh-speed-flists-active-p ()
729 "Check if speedbar is running with message counts enabled."
730 (and (featurep 'mh-speed)
731 (not mh-speed-flists-inhibit-flag)
732 (> (hash-table-count mh-speed-flists-cache) 0)))
735 (defun mh-folder-completion-function (name predicate flag)
736 "Programmable completion for folder names.
737 NAME is the partial folder name that has been input. PREDICATE if
738 non-nil is a function that is used to filter the possible choices
739 and FLAG determines whether the completion is over."
740 (let* ((orig-name name)
741 (name (mh-normalize-folder-name name nil t))
742 (last-slash (mh-search-from-end ?/ name))
743 (last-complete (if last-slash (substring name 0 last-slash) nil))
744 (remainder (cond (last-complete (substring name (1+ last-slash)))
745 ((and (> (length name) 0) (equal (aref name 0) ?+))
749 (let ((try-res (try-completion
752 (cons (if (not last-complete)
754 (concat last-complete "/" (car x)))
756 (mh-sub-folders last-complete t))
758 (cond ((eq try-res nil) nil)
759 ((and (eq try-res t) (equal name orig-name)) t)
760 ((eq try-res t) name)
764 remainder (mh-sub-folders last-complete t) predicate))
766 (let ((path (concat mh-user-path
767 (substring (mh-normalize-folder-name name) 1))))
768 (cond (mh-allow-root-folder-flag (file-exists-p path))
769 ((equal path mh-user-path) nil)
770 (t (file-exists-p path))))))))
775 (defvar completion-root-regexp)
776 (defvar minibuffer-completing-file-name)))
778 (defun mh-folder-completing-read (prompt default allow-root-folder-flag)
779 "Read folder name with PROMPT and default result DEFAULT.
780 If ALLOW-ROOT-FOLDER-FLAG is non-nil then \"+\" is allowed to be
781 a folder name corresponding to `mh-user-path'."
782 (mh-normalize-folder-name
783 (let ((minibuffer-completing-file-name t)
784 (completion-root-regexp "^[+/]")
785 (minibuffer-local-completion-map mh-folder-completion-map)
786 (mh-allow-root-folder-flag allow-root-folder-flag))
787 (completing-read prompt 'mh-folder-completion-function nil nil nil
788 'mh-folder-hist default))
792 (defun mh-prompt-for-folder (prompt default can-create
793 &optional default-string allow-root-folder-flag)
794 "Prompt for a folder name with PROMPT.
795 Returns the folder's name as a string. DEFAULT is used if the
796 folder exists and the user types return. If the CAN-CREATE flag
797 is t, then a folder is created if it doesn't already exist. If
798 optional argument DEFAULT-STRING is non-nil, use it in the prompt
799 instead of DEFAULT. If ALLOW-ROOT-FOLDER-FLAG is non-nil then the
800 function will accept the folder +, which means all folders when
804 (let* ((default-string (cond (default-string (format " (default %s)" default-string))
805 ((equal "" default) "")
806 (t (format " (default %s)" default))))
807 (prompt (format "%s folder%s: " prompt default-string))
808 (mh-current-folder-name mh-current-folder)
809 read-name folder-name)
810 (while (and (setq read-name (mh-folder-completing-read
811 prompt default allow-root-folder-flag))
814 (cond ((or (equal read-name "")
815 (and (equal read-name "+") (not allow-root-folder-flag)))
816 (setq read-name default))
817 ((not (mh-folder-name-p read-name))
818 (setq read-name (format "+%s" read-name))))
819 (if (or (not read-name) (equal "" read-name))
820 (error "No folder specified"))
821 (setq folder-name read-name)
822 (cond ((and (> (length folder-name) 0)
823 (eq (aref folder-name (1- (length folder-name))) ?/))
824 (setq folder-name (substring folder-name 0 -1))))
825 (let* ((last-slash (mh-search-from-end ?/ folder-name))
826 (parent (and last-slash (substring folder-name 0 last-slash)))
827 (child (if last-slash
828 (substring folder-name (1+ last-slash))
829 (substring folder-name 1))))
830 (unless (member child
831 (mapcar #'car (gethash parent mh-sub-folders-cache)))
832 (mh-remove-from-sub-folders-cache folder-name)))
834 (not (file-exists-p (mh-expand-file-name folder-name)))))
835 (cond ((and new-file-flag
838 (format "Folder %s does not exist. Create it? "
840 (message "Creating %s" folder-name)
841 (mh-exec-cmd-error nil "folder" folder-name)
842 (mh-remove-from-sub-folders-cache folder-name)
843 (when (boundp 'mh-speed-folder-map)
844 (mh-speed-add-folder folder-name))
845 (message "Creating %s...done" folder-name))
847 (error "Folder %s does not exist" folder-name))
848 ((not (file-directory-p (mh-expand-file-name folder-name)))
849 (error "%s is not a directory"
850 (mh-expand-file-name folder-name)))))
855 ;;; Message Utilities
857 ;; Functions that would ordinarily be in mh-letter.el that are needed
858 ;; by mh-show.el are found here in order to prevent the loading of
859 ;; mh-letter.el until a message is actually composed.
862 (defun mh-in-header-p ()
863 "Return non-nil if the point is in the header of a draft message."
864 (< (point) (mh-mail-header-end)))
867 (defun mh-extract-from-header-value ()
868 "Extract From: string from header."
870 (if (not (mh-goto-header-field "From:"))
872 (skip-chars-forward " \t")
873 (buffer-substring-no-properties
874 (point) (progn (mh-header-field-end)(point))))))
877 (defun mh-get-header-field (field)
878 "Find and return the body of FIELD in the mail header.
879 Returns the empty string if the field is not in the header of the
881 (if (mh-goto-header-field field)
883 (skip-chars-forward " \t") ;strip leading white space in body
884 (let ((start (point)))
885 (mh-header-field-end)
886 (buffer-substring-no-properties start (point))))
890 (defun mh-goto-header-field (field)
891 "Move to FIELD in the message header.
892 Move to the end of the FIELD name, which should end in a colon.
893 Returns t if found, nil if not."
894 (goto-char (point-min))
895 (let ((case-fold-search t)
896 (headers-end (save-excursion
897 (mh-goto-header-end 0)
899 (re-search-forward (format "^%s" field) headers-end t)))
902 (defun mh-goto-header-end (arg)
903 "Move the cursor ARG lines after the header."
904 (if (re-search-forward "^-*$" nil nil)
908 (defun mh-mail-header-end ()
909 "Substitute for `mail-header-end' that doesn't widen the buffer.
911 In MH-E we frequently need to find the end of headers in nested
912 messages, where the buffer has been narrowed. This function works
915 ;; XXX: The following replaces a call to rfc822-goto-eoh. Occasionally,
916 ;; mail headers that MH-E has to read contains lines of the form:
917 ;; From xxx@yyy Mon May 10 11:48:07 2004
918 ;; In this situation, rfc822-goto-eoh doesn't go to the end of the
919 ;; header. The replacement allows From_ lines in the mail header.
920 (goto-char (point-min))
921 (loop for p = (re-search-forward
922 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
923 do (cond ((null p) (return))
924 (t (goto-char (match-beginning 0))
925 (unless (looking-at "From ") (return))
930 (defun mh-header-field-beginning ()
931 "Move to the beginning of the current header field.
932 Handles RFC 822 continuation lines."
934 (while (looking-at "^[ \t]")
938 (defun mh-header-field-end ()
939 "Move to the end of the current header field.
940 Handles RFC 822 continuation lines."
942 (while (looking-at "^[ \t]")
944 (backward-char 1)) ;to end of previous line
947 (defun mh-letter-hide-all-skipped-fields ()
948 "Hide all skipped fields."
950 (goto-char (point-min))
952 (narrow-to-region (point) (mh-mail-header-end))
953 (while (re-search-forward mh-letter-header-field-regexp nil t)
954 (if (mh-letter-skipped-header-field-p (match-string 1))
955 (mh-letter-toggle-header-field-display -1)
956 (mh-letter-toggle-header-field-display 'long))
957 (beginning-of-line 2)))))
960 (defun mh-letter-skipped-header-field-p (field)
961 "Check if FIELD is to be skipped."
962 (let ((field (downcase field)))
963 (loop for x in mh-compose-skipped-header-fields
964 when (equal (downcase x) field) return t
965 finally return nil)))
967 (defvar mh-hidden-header-keymap
968 (let ((map (make-sparse-keymap)))
970 (define-key map [mouse-2] 'mh-letter-toggle-header-field-display-button))
972 (define-key map '(button2)
973 'mh-letter-toggle-header-field-display-button))
977 (defun mh-letter-toggle-header-field-display (arg)
978 "Toggle display of header field at point.
980 Use this command to display truncated header fields. This command
981 is a toggle so entering it again will hide the field. This
982 command takes a prefix argument ARG: if negative then the field
983 is hidden, if positive then the field is displayed."
984 (interactive (list nil))
985 (when (and (mh-in-header-p)
988 (re-search-backward mh-letter-header-field-regexp nil t)))
989 (let ((buffer-read-only nil)
990 (modified-flag (buffer-modified-p))
994 (setq end (1- (if (re-search-forward "^[^ \t]" nil t)
998 ;; Make it clickable...
999 (add-text-properties begin end `(keymap ,mh-hidden-header-keymap
1000 mouse-face highlight))
1002 (cond ((or (and (not arg)
1003 (text-property-any begin end 'invisible 'vanish))
1007 (> (mh-line-beginning-position 5) end)))
1008 (remove-text-properties begin end '(invisible nil))
1009 (search-forward ":" (mh-line-end-position) t)
1010 (mh-letter-skip-leading-whitespace-in-header-field))
1011 ;; XXX Redesign to make usable by user. Perhaps use a positive
1012 ;; numeric prefix to make that many lines visible.
1015 (mh-letter-truncate-header-field end)
1016 (beginning-of-line))
1018 (mh-letter-truncate-header-field end)
1019 (beginning-of-line)))
1020 (set-buffer-modified-p modified-flag)))))
1023 (defun mh-letter-skip-leading-whitespace-in-header-field ()
1024 "Skip leading whitespace in a header field.
1025 If the header field doesn't have at least one space after the
1026 colon then a space character is added."
1027 (let ((need-space t))
1028 (while (memq (char-after) '(?\t ?\ ))
1030 (setq need-space nil))
1031 (when need-space (insert " "))))
1033 (defun mh-letter-truncate-header-field (end)
1034 "Replace text from current line till END with an ellipsis.
1035 If the current line is too long truncate a part of it as well."
1036 (let ((max-len (min (window-width) 62)))
1037 (when (> (+ (current-column) 4) max-len)
1038 (backward-char (- (+ (current-column) 5) max-len)))
1039 (when (> end (point))
1040 (add-text-properties (point) end '(invisible vanish)))))
1043 (defun mh-signature-separator-p ()
1044 "Return non-nil if buffer includes \"^-- $\"."
1046 (goto-char (point-min))
1047 (re-search-forward mh-signature-separator-regexp nil t)))
1052 ;; indent-tabs-mode: nil
1053 ;; sentence-end-double-space: nil
1056 ;; arch-tag: 1af39fdf-f66f-4b06-9b48-18a7656c8e36
1057 ;;; mh-utils.el ends here