1 ;;; mh-funcs.el --- MH-E functions not everyone will use right away
3 ;; Copyright (C) 1993, 1995, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Bill Wohler <wohler@newt.com>
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.
29 ;; Internal support for MH-E package.
30 ;; Putting these functions in a separate file lets MH-E start up faster,
31 ;; since less Lisp code needs to be loaded all at once.
35 ;; $Id: mh-funcs.el,v 1.2 2003/02/03 20:55:30 wohler Exp $
43 (defvar mh-sortm-args nil
44 "Extra arguments to have \\[mh-sort-folder] pass to the \"sortm\" command.
45 The arguments are passed to sortm if \\[mh-sort-folder] is given a
46 prefix argument. Normally default arguments to sortm are specified in the
48 For example, '(\"-nolimit\" \"-textfield\" \"subject\") is a useful setting.")
50 (defvar mh-note-copied
"C"
51 "String whose first character is used to notate copied messages.")
53 (defvar mh-note-printed
"P"
54 "String whose first character is used to notate printed messages.")
59 (defun mh-burst-digest ()
60 "Burst apart the current message, which should be a digest.
61 The message is replaced by its table of contents and the messages from the
62 digest are inserted into the folder after that message."
64 (let ((digest (mh-get-msg-num t
)))
65 (mh-process-or-undo-commands mh-current-folder
)
66 (mh-set-folder-modified-p t
) ; lock folder while bursting
67 (message "Bursting digest...")
68 (mh-exec-cmd "burst" mh-current-folder digest
"-inplace")
69 (with-mh-folder-updating (t)
71 (delete-region (point) (point-max)))
72 (mh-regenerate-headers (format "%d-last" digest
) t
)
74 (message "Bursting digest...done")))
77 (defun mh-copy-msg (msg-or-seq folder
)
78 "Copy the specified MSG-OR-SEQ to another FOLDER without deleting them.
79 Default is the displayed message. If optional prefix argument is provided,
80 then prompt for the message sequence."
81 (interactive (list (cond
83 (cons (region-beginning) (region-end)))
85 (mh-read-seq-default "Copy" t
))
87 (cons (line-beginning-position) (line-end-position))))
88 (mh-prompt-for-folder "Copy to" "" t
)))
89 (let ((msg-list (cond ((numberp msg-or-seq
) (list msg-or-seq
))
90 ((symbolp msg-or-seq
) (mh-seq-to-msgs msg-or-seq
))
91 ((and (consp msg-or-seq
) (numberp (car msg-or-seq
))
92 (numberp (cdr msg-or-seq
)))
94 (mh-iterate-on-messages-in-region msg
95 (car msg-or-seq
) (cdr msg-or-seq
)
96 (mh-notate nil mh-note-copied mh-cmd-note
)
100 (mh-exec-cmd "refile" (mh-coalesce-msg-list msg-list
)
101 "-link" "-src" mh-current-folder folder
)
102 (cond ((numberp msg-or-seq
)
103 (mh-notate msg-or-seq mh-note-copied mh-cmd-note
))
104 ((symbolp msg-or-seq
)
105 (mh-notate-seq msg-or-seq mh-note-copied mh-cmd-note
)))))
108 (defun mh-kill-folder ()
109 "Remove the current folder and all included messages.
110 Removes all of the messages (files) within the specified current folder,
111 and then removes the folder (directory) itself."
113 (if (or mh-index-data
114 (yes-or-no-p (format "Remove folder %s (and all included messages)?"
116 (let ((folder mh-current-folder
)
117 (window-config mh-previous-window-config
))
118 (mh-set-folder-modified-p t
) ; lock folder to kill it
119 (mh-exec-cmd-daemon "rmf" 'mh-rmf-daemon folder
)
120 (when (boundp 'mh-speed-folder-map
)
121 (mh-speed-invalidate-map folder
))
122 (mh-remove-from-sub-folders-cache folder
)
123 (mh-set-folder-modified-p nil
) ; so kill-buffer doesn't complain
124 (if (and mh-show-buffer
(get-buffer mh-show-buffer
))
125 (kill-buffer mh-show-buffer
))
126 (if (get-buffer folder
)
127 (kill-buffer folder
))
129 (set-window-configuration window-config
))
130 (message "Folder %s removed" folder
))
131 (message "Folder not removed")))
133 (defun mh-rmf-daemon (process output
)
134 "The rmf PROCESS puts OUTPUT in temporary buffer.
135 Display the results only if something went wrong."
136 (set-buffer (get-buffer-create mh-temp-buffer
))
137 (insert-before-markers output
)
138 (when (save-excursion
139 (beginning-of-buffer)
140 (re-search-forward "^rmf: " (point-max) t
))
141 (display-buffer mh-temp-buffer
)))
143 ;; Avoid compiler warning...
144 (defvar view-exit-action
)
147 (defun mh-list-folders ()
150 (let ((temp-buffer mh-folders-buffer
))
151 (with-output-to-temp-buffer temp-buffer
153 (set-buffer temp-buffer
)
155 (message "Listing folders...")
156 (mh-exec-cmd-output "folders" t
(if mh-recursive-folders-flag
159 (goto-char (point-min))
161 (setq view-exit-action
'kill-buffer
)
162 (message "Listing folders...done")))))
165 (defun mh-pack-folder (range)
166 "Renumber the messages of a folder to be 1..n.
167 First, offer to execute any outstanding commands for the current folder. If
168 optional prefix argument provided, prompt for the RANGE of messages to display
169 after packing. Otherwise, show the entire folder."
170 (interactive (list (if current-prefix-arg
171 (mh-read-msg-range mh-current-folder t
)
173 (let ((threaded-flag (memq 'unthread mh-view-ops
)))
174 (mh-pack-folder-1 range
)
177 (mh-index-update-maps mh-current-folder
))
178 (cond (threaded-flag (mh-toggle-threads))
179 (mh-index-data (mh-index-insert-folder-headers))))
180 (message "Packing folder...done"))
182 (defun mh-pack-folder-1 (range)
183 "Close and pack the current folder.
184 Display the given RANGE of messages after packing. If RANGE is nil, show the
186 (mh-process-or-undo-commands mh-current-folder
)
187 (message "Packing folder...")
188 (mh-set-folder-modified-p t
) ; lock folder while packing
190 (mh-exec-cmd-quiet t
"folder" mh-current-folder
"-pack"
191 "-norecurse" "-fast"))
192 (mh-reset-threads-and-narrowing)
193 (mh-regenerate-headers range
))
196 (defun mh-pipe-msg (command include-headers
)
197 "Pipe the current message through the given shell COMMAND.
198 If INCLUDE-HEADERS (prefix argument) is provided, send the entire message.
199 Otherwise just send the message's body without the headers."
201 (list (read-string "Shell command on message: ") current-prefix-arg
))
202 (let ((msg-file-to-pipe (mh-msg-filename (mh-get-msg-num t
)))
203 (message-directory default-directory
))
205 (set-buffer (get-buffer-create mh-temp-buffer
))
207 (insert-file-contents msg-file-to-pipe
)
208 (goto-char (point-min))
209 (if (not include-headers
) (search-forward "\n\n"))
210 (let ((default-directory message-directory
))
211 (shell-command-on-region (point) (point-max) command nil
)))))
214 (defun mh-page-digest ()
215 "Advance displayed message to next digested message."
217 (mh-in-show-buffer (mh-show-buffer)
218 ;; Go to top of screen (in case user moved point).
219 (move-to-window-line 0)
220 (let ((case-fold-search nil
))
221 ;; Search for blank line and then for From:
222 (or (and (search-forward "\n\n" nil t
)
223 (re-search-forward "^From:" nil t
))
224 (error "No more messages in digest")))
225 ;; Go back to previous blank line, then forward to the first non-blank.
226 (search-backward "\n\n" nil t
)
231 (defun mh-page-digest-backwards ()
232 "Back up displayed message to previous digested message."
234 (mh-in-show-buffer (mh-show-buffer)
235 ;; Go to top of screen (in case user moved point).
236 (move-to-window-line 0)
237 (let ((case-fold-search nil
))
239 (or (and (search-backward "\n\n" nil t
)
240 (re-search-backward "^From:" nil t
))
241 (error "No previous message in digest")))
242 ;; Go back to previous blank line, then forward to the first non-blank.
243 (if (search-backward "\n\n" nil t
)
248 (defun mh-print-msg (msg-or-seq)
249 "Print MSG-OR-SEQ (default: displayed message) on printer.
250 If optional prefix argument provided, then prompt for the message sequence.
251 The variable `mh-lpr-command-format' is used to generate the print command.
252 The messages are formatted by mhl. See the variable `mhl-formfile'."
253 (interactive (list (if current-prefix-arg
254 (reverse (mh-seq-to-msgs
255 (mh-read-seq-default "Print" t
)))
256 (mh-get-msg-num t
))))
257 (if (numberp msg-or-seq
)
258 (message "Printing message...")
259 (message "Printing sequence..."))
261 (if (numberp msg-or-seq
)
262 (format "%s -nobell -clear %s %s | %s"
263 (expand-file-name "mhl" mh-lib-progs
)
264 (mh-msg-filename msg-or-seq
)
265 (if (stringp mhl-formfile
)
266 (format "-form %s" mhl-formfile
)
268 (format mh-lpr-command-format
269 (if (numberp msg-or-seq
)
270 (format "%s/%d" mh-current-folder
272 (format "Sequence from %s" mh-current-folder
))))
273 (format "(scan -clear %s ; %s -nobell -clear %s %s) | %s"
274 (mapconcat (function (lambda (msg) msg
)) msg-or-seq
" ")
275 (expand-file-name "mhl" mh-lib-progs
)
276 (if (stringp mhl-formfile
)
277 (format "-form %s" mhl-formfile
)
279 (mh-msg-filenames msg-or-seq
)
280 (format mh-lpr-command-format
281 (if (numberp msg-or-seq
)
282 (format "%s/%d" mh-current-folder
284 (format "Sequence from %s"
285 mh-current-folder
)))))))
286 (if mh-print-background-flag
287 (mh-exec-cmd-daemon shell-file-name nil
"-c" print-command
)
288 (call-process shell-file-name nil nil nil
"-c" print-command
))
289 (if (numberp msg-or-seq
)
290 (mh-notate msg-or-seq mh-note-printed mh-cmd-note
)
291 (mh-notate-seq msg-or-seq mh-note-printed mh-cmd-note
))
292 (mh-add-msgs-to-seq msg-or-seq
'printed t
)
293 (if (numberp msg-or-seq
)
294 (message "Printing message...done")
295 (message "Printing sequence...done"))))
297 (defun mh-msg-filenames (msgs &optional folder
)
298 "Return a list of file names for MSGS in FOLDER (default current folder)."
299 (mapconcat (function (lambda (msg) (mh-msg-filename msg folder
))) msgs
" "))
302 (defun mh-sort-folder (&optional extra-args
)
303 "Sort the messages in the current folder by date.
304 Calls the MH program sortm to do the work.
305 The arguments in the list `mh-sortm-args' are passed to sortm if the optional
306 argument EXTRA-ARGS is given."
308 (mh-process-or-undo-commands mh-current-folder
)
309 (setq mh-next-direction
'forward
)
310 (mh-set-folder-modified-p t
) ; lock folder while sorting
311 (message "Sorting folder...")
312 (let ((threaded-flag (memq 'unthread mh-view-ops
)))
313 (mh-exec-cmd "sortm" mh-current-folder
(if extra-args mh-sortm-args
))
315 (mh-index-update-maps mh-current-folder
))
316 (message "Sorting folder...done")
317 (mh-reset-threads-and-narrowing)
318 (mh-scan-folder mh-current-folder
"all")
319 (cond (threaded-flag (mh-toggle-threads))
320 (mh-index-data (mh-index-insert-folder-headers)))))
323 (defun mh-undo-folder (&rest ignore
)
324 "Undo all pending deletes and refiles in current folder.
325 Argument IGNORE is deprecated."
327 (cond ((or mh-do-not-confirm-flag
328 (yes-or-no-p "Undo all commands in folder? "))
329 (setq mh-delete-list nil
332 mh-next-direction
'forward
)
333 (with-mh-folder-updating (nil)
334 (mh-unmark-all-headers t
)))
336 (message "Commands not undone.")
340 (defun mh-store-msg (directory)
341 "Store the file(s) contained in the current message into DIRECTORY.
342 The message can contain a shar file or uuencoded file.
343 Default directory is the last directory used, or initially the value of
344 `mh-store-default-directory' or the current directory."
345 (interactive (list (let ((udir (or mh-store-default-directory
347 (read-file-name "Store message in directory: "
349 (let ((msg-file-to-store (mh-msg-filename (mh-get-msg-num t
))))
351 (set-buffer (get-buffer-create mh-temp-buffer
))
353 (insert-file-contents msg-file-to-store
)
354 (mh-store-buffer directory
))))
357 (defun mh-store-buffer (directory)
358 "Store the file(s) contained in the current buffer into DIRECTORY.
359 The buffer can contain a shar file or uuencoded file.
360 Default directory is the last directory used, or initially the value of
361 `mh-store-default-directory' or the current directory."
362 (interactive (list (let ((udir (or mh-store-default-directory
364 (read-file-name "Store buffer in directory: "
366 (let ((store-directory (expand-file-name directory
))
367 (sh-start (save-excursion
368 (goto-char (point-min))
369 (if (re-search-forward
370 "^#![ \t]*/bin/sh\\|^#\\|^: " nil t
)
372 ;; The "cut here" pattern was removed from above
373 ;; because it seemed to hurt more than help.
374 ;; But keep this to make it easier to put it back.
375 (if (looking-at "^[^a-z0-9\"]*cut here\\b")
378 (if (looking-at "^[#:]....+\n\\( ?\n\\)?end$")
379 nil
;most likely end of a uuencode
381 (log-buffer (get-buffer-create "*Store Output*"))
383 (uudecode-filename "(unknown filename)"))
386 (goto-char (point-min))
387 (if (re-search-forward "^begin [0-7]+ " nil t
)
388 (setq uudecode-filename
389 (buffer-substring (point)
390 (progn (end-of-line) (point)))))))
392 (set-buffer log-buffer
)
394 (if (not (file-directory-p store-directory
))
396 (insert "mkdir " directory
"\n")
397 (call-process "mkdir" nil log-buffer t store-directory
)))
398 (insert "cd " directory
"\n")
399 (setq mh-store-default-directory directory
)
402 (setq command
"uudecode")
403 (insert uudecode-filename
" being uudecoded...\n"))))
404 (set-window-start (display-buffer log-buffer
) 0) ;watch progress
406 (let ((default-directory (file-name-as-directory store-directory
)))
407 (setq value
(call-process-region sh-start
(point-max) command
409 (set-buffer log-buffer
)
410 (mh-handle-process-error command value
))
411 (insert "\n(mh-store finished)\n")))
417 (defun mh-ephem-message (string)
418 "Display STRING in the minibuffer momentarily."
419 (message "%s" string
)
425 "Display cheat sheet for the MH-Folder commands in minibuffer."
428 (substitute-command-keys
429 (mapconcat 'identity
(cdr (assoc nil mh-help-messages
)) ""))))
432 (defun mh-prefix-help ()
433 "Display cheat sheet for the commands of the current prefix in minibuffer."
435 ;; We got here because the user pressed a `?', but he pressed a prefix key
436 ;; before that. Since the the key vector starts at index 0, the index of the
437 ;; last keystroke is length-1 and thus the second to last keystroke is at
438 ;; length-2. We use that information to obtain a suitable prefix character
439 ;; from the recent keys.
440 (let* ((keys (recent-keys))
441 (prefix-char (elt keys
(- (length keys
) 2))))
443 (substitute-command-keys
444 (mapconcat 'identity
(cdr (assoc prefix-char mh-help-messages
)) "")))))
449 ;;; indent-tabs-mode: nil
450 ;;; sentence-end-double-space: nil
453 ;;; mh-funcs.el ends here