1 ;;; mh-funcs.el --- MH-E functions not everyone will use right away
3 ;; Copyright (C) 1993, 1995, 2001, 02, 2003 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.
41 (defvar mh-sortm-args nil
42 "Extra arguments to have \\[mh-sort-folder] pass to the \"sortm\" command.
43 The arguments are passed to sortm if \\[mh-sort-folder] is given a
44 prefix argument. Normally default arguments to sortm are specified in the
46 For example, '(\"-nolimit\" \"-textfield\" \"subject\") is a useful setting.")
48 (defvar mh-note-copied
"C"
49 "String whose first character is used to notate copied messages.")
51 (defvar mh-note-printed
"P"
52 "String whose first character is used to notate printed messages.")
57 (defun mh-burst-digest ()
58 "Burst apart the current message, which should be a digest.
59 The message is replaced by its table of contents and the messages from the
60 digest are inserted into the folder after that message."
62 (let ((digest (mh-get-msg-num t
)))
63 (mh-process-or-undo-commands mh-current-folder
)
64 (mh-set-folder-modified-p t
) ; lock folder while bursting
65 (message "Bursting digest...")
66 (mh-exec-cmd "burst" mh-current-folder digest
"-inplace")
67 (with-mh-folder-updating (t)
69 (delete-region (point) (point-max)))
70 (mh-regenerate-headers (format "%d-last" digest
) t
)
72 (message "Bursting digest...done")))
75 (defun mh-copy-msg (msg-or-seq folder
)
76 "Copy the specified MSG-OR-SEQ to another FOLDER without deleting them.
77 Default is the displayed message.
78 If optional prefix argument is provided, then prompt for the message sequence.
79 If variable `transient-mark-mode' is non-nil and the mark is active, then the
80 selected region is copied.
81 In a program, MSG-OR-SEQ can be a message number, a list of message numbers, a
82 region in a cons cell, or a sequence."
83 (interactive (list (mh-interactive-msg-or-seq "Copy")
84 (mh-prompt-for-folder "Copy to" "" t
)))
85 (let ((msg-list (let ((result ()))
86 (mh-iterate-on-msg-or-seq msg msg-or-seq
87 (mh-notate nil mh-note-copied mh-cmd-note
)
90 (mh-exec-cmd "refile" (mh-coalesce-msg-list msg-list
)
91 "-link" "-src" mh-current-folder folder
)))
94 (defun mh-kill-folder ()
95 "Remove the current folder and all included messages.
96 Removes all of the messages (files) within the specified current folder,
97 and then removes the folder (directory) itself."
100 (yes-or-no-p (format "Remove folder %s (and all included messages)? "
102 (let ((folder mh-current-folder
)
103 (window-config mh-previous-window-config
))
104 (mh-set-folder-modified-p t
) ; lock folder to kill it
105 (mh-exec-cmd-daemon "rmf" 'mh-rmf-daemon folder
)
106 (when (boundp 'mh-speed-folder-map
)
107 (mh-speed-invalidate-map folder
))
108 (mh-remove-from-sub-folders-cache folder
)
109 (mh-set-folder-modified-p nil
) ; so kill-buffer doesn't complain
110 (if (and mh-show-buffer
(get-buffer mh-show-buffer
))
111 (kill-buffer mh-show-buffer
))
112 (if (get-buffer folder
)
113 (kill-buffer folder
))
115 (set-window-configuration window-config
))
116 (message "Folder %s removed" folder
))
117 (message "Folder not removed")))
119 (defun mh-rmf-daemon (process output
)
120 "The rmf PROCESS puts OUTPUT in temporary buffer.
121 Display the results only if something went wrong."
122 (set-buffer (get-buffer-create mh-temp-buffer
))
123 (insert-before-markers output
)
124 (when (save-excursion
125 (beginning-of-buffer)
126 (re-search-forward "^rmf: " (point-max) t
))
127 (display-buffer mh-temp-buffer
)))
129 ;; Avoid compiler warning...
130 (defvar view-exit-action
)
133 (defun mh-list-folders ()
136 (let ((temp-buffer mh-folders-buffer
))
137 (with-output-to-temp-buffer temp-buffer
139 (set-buffer temp-buffer
)
141 (message "Listing folders...")
142 (mh-exec-cmd-output "folders" t
(if mh-recursive-folders-flag
145 (goto-char (point-min))
147 (setq view-exit-action
'kill-buffer
)
148 (message "Listing folders...done")))))
151 (defun mh-pack-folder (range)
152 "Renumber the messages of a folder to be 1..n.
153 First, offer to execute any outstanding commands for the current folder. If
154 optional prefix argument provided, prompt for the RANGE of messages to display
155 after packing. Otherwise, show the entire folder."
156 (interactive (list (if current-prefix-arg
157 (mh-read-msg-range mh-current-folder t
)
159 (let ((threaded-flag (memq 'unthread mh-view-ops
)))
160 (mh-pack-folder-1 range
)
163 (mh-index-update-maps mh-current-folder
))
164 (cond (threaded-flag (mh-toggle-threads))
165 (mh-index-data (mh-index-insert-folder-headers))))
166 (message "Packing folder...done"))
168 (defun mh-pack-folder-1 (range)
169 "Close and pack the current folder.
170 Display the given RANGE of messages after packing. If RANGE is nil, show the
172 (mh-process-or-undo-commands mh-current-folder
)
173 (message "Packing folder...")
174 (mh-set-folder-modified-p t
) ; lock folder while packing
176 (mh-exec-cmd-quiet t
"folder" mh-current-folder
"-pack"
177 "-norecurse" "-fast"))
178 (mh-reset-threads-and-narrowing)
179 (mh-regenerate-headers range
))
182 (defun mh-pipe-msg (command include-headers
)
183 "Pipe the current message through the given shell COMMAND.
184 If INCLUDE-HEADERS (prefix argument) is provided, send the entire message.
185 Otherwise just send the message's body without the headers."
187 (list (read-string "Shell command on message: ") current-prefix-arg
))
188 (let ((msg-file-to-pipe (mh-msg-filename (mh-get-msg-num t
)))
189 (message-directory default-directory
))
191 (set-buffer (get-buffer-create mh-temp-buffer
))
193 (insert-file-contents msg-file-to-pipe
)
194 (goto-char (point-min))
195 (if (not include-headers
) (search-forward "\n\n"))
196 (let ((default-directory message-directory
))
197 (shell-command-on-region (point) (point-max) command nil
)))))
200 (defun mh-page-digest ()
201 "Advance displayed message to next digested message."
203 (mh-in-show-buffer (mh-show-buffer)
204 ;; Go to top of screen (in case user moved point).
205 (move-to-window-line 0)
206 (let ((case-fold-search nil
))
207 ;; Search for blank line and then for From:
208 (or (and (search-forward "\n\n" nil t
)
209 (re-search-forward "^From:" nil t
))
210 (error "No more messages in digest")))
211 ;; Go back to previous blank line, then forward to the first non-blank.
212 (search-backward "\n\n" nil t
)
217 (defun mh-page-digest-backwards ()
218 "Back up displayed message to previous digested message."
220 (mh-in-show-buffer (mh-show-buffer)
221 ;; Go to top of screen (in case user moved point).
222 (move-to-window-line 0)
223 (let ((case-fold-search nil
))
225 (or (and (search-backward "\n\n" nil t
)
226 (re-search-backward "^From:" nil t
))
227 (error "No previous message in digest")))
228 ;; Go back to previous blank line, then forward to the first non-blank.
229 (if (search-backward "\n\n" nil t
)
234 (defun mh-print-msg (msg-or-seq)
235 "Print MSG-OR-SEQ on printer.
236 Default is the displayed message.
237 If optional prefix argument is provided, then prompt for the message sequence.
238 If variable `transient-mark-mode' is non-nil and the mark is active, then the
239 selected region is printed.
240 In a program, MSG-OR-SEQ can be a message number, a list of message numbers, a
241 region in a cons cell, or a sequence.
243 The variable `mh-lpr-command-format' is used to generate the print command.
244 The messages are formatted by mhl. See the variable `mhl-formfile'."
245 (interactive (list (mh-interactive-msg-or-seq "Print")))
246 (message "Printing...")
248 ;; Gather message numbers and add them to "printed" sequence.
249 (mh-iterate-on-msg-or-seq msg msg-or-seq
250 (mh-add-msgs-to-seq msg
'printed t
)
251 (mh-notate nil mh-note-printed mh-cmd-note
)
253 (setq msgs
(nreverse msgs
))
254 ;; Print scan listing if we have more than one message.
255 (if (> (length msgs
) 1)
257 (mapconcat 'identity
(mh-list-to-string
258 (mh-coalesce-msg-list msgs
)) " "))
260 (format mh-lpr-command-format
261 (cond ((listp msg-or-seq
)
262 (format "Folder: %s, Messages: %s"
263 mh-current-folder msgs-string
))
264 ((symbolp msg-or-seq
)
265 (format "Folder: %s, Sequence: %s"
266 mh-current-folder msg-or-seq
)))))
268 (format "scan %s | %s" msgs-string lpr-command
)))
269 (if mh-print-background-flag
270 (mh-exec-cmd-daemon shell-file-name nil
"-c" scan-command
)
271 (call-process shell-file-name nil nil nil
"-c" scan-command
))))
272 ;; Print the messages
274 (let* ((mhl-command (format "%s %s %s"
275 (expand-file-name "mhl" mh-lib-progs
)
277 (format " -form %s" mhl-formfile
)
279 (mh-msg-filename msg
)))
281 (format mh-lpr-command-format
282 (format "%s/%s" mh-current-folder msg
)))
284 (format "%s | %s" mhl-command lpr-command
)))
285 (if mh-print-background-flag
286 (mh-exec-cmd-daemon shell-file-name nil
"-c" print-command
)
287 (call-process shell-file-name nil nil nil
"-c" print-command
)))))
288 (message "Printing...done"))
291 (defun mh-sort-folder (&optional extra-args
)
292 "Sort the messages in the current folder by date.
293 Calls the MH program sortm to do the work.
294 The arguments in the list `mh-sortm-args' are passed to sortm if the optional
295 argument EXTRA-ARGS is given."
297 (mh-process-or-undo-commands mh-current-folder
)
298 (setq mh-next-direction
'forward
)
299 (mh-set-folder-modified-p t
) ; lock folder while sorting
300 (message "Sorting folder...")
301 (let ((threaded-flag (memq 'unthread mh-view-ops
)))
302 (mh-exec-cmd "sortm" mh-current-folder
(if extra-args mh-sortm-args
))
304 (mh-index-update-maps mh-current-folder
))
305 (message "Sorting folder...done")
306 (mh-scan-folder mh-current-folder
"all")
307 (cond (threaded-flag (mh-toggle-threads))
308 (mh-index-data (mh-index-insert-folder-headers)))))
311 (defun mh-undo-folder (&rest ignore
)
312 "Undo all pending deletes and refiles in current folder.
313 Argument IGNORE is deprecated."
315 (cond ((or mh-do-not-confirm-flag
316 (yes-or-no-p "Undo all commands in folder? "))
317 (setq mh-delete-list nil
320 mh-next-direction
'forward
)
321 (with-mh-folder-updating (nil)
322 (mh-unmark-all-headers t
)))
324 (message "Commands not undone.")
325 ;; Remove by 2003-06-30 if nothing seems amiss. XXX
330 (defun mh-store-msg (directory)
331 "Store the file(s) contained in the current message into DIRECTORY.
332 The message can contain a shar file or uuencoded file.
333 Default directory is the last directory used, or initially the value of
334 `mh-store-default-directory' or the current directory."
335 (interactive (list (let ((udir (or mh-store-default-directory
337 (read-file-name "Store message in directory: "
339 (let ((msg-file-to-store (mh-msg-filename (mh-get-msg-num t
))))
341 (set-buffer (get-buffer-create mh-temp-buffer
))
343 (insert-file-contents msg-file-to-store
)
344 (mh-store-buffer directory
))))
347 (defun mh-store-buffer (directory)
348 "Store the file(s) contained in the current buffer into DIRECTORY.
349 The buffer can contain a shar file or uuencoded file.
350 Default directory is the last directory used, or initially the value of
351 `mh-store-default-directory' or the current directory."
352 (interactive (list (let ((udir (or mh-store-default-directory
354 (read-file-name "Store buffer in directory: "
356 (let ((store-directory (expand-file-name directory
))
357 (sh-start (save-excursion
358 (goto-char (point-min))
359 (if (re-search-forward
360 "^#![ \t]*/bin/sh\\|^#\\|^: " nil t
)
362 ;; The "cut here" pattern was removed from above
363 ;; because it seemed to hurt more than help.
364 ;; But keep this to make it easier to put it back.
365 (if (looking-at "^[^a-z0-9\"]*cut here\\b")
368 (if (looking-at "^[#:]....+\n\\( ?\n\\)?end$")
369 nil
;most likely end of a uuencode
372 (uudecode-filename "(unknown filename)")
376 (goto-char (point-min))
377 (if (re-search-forward "^begin [0-7]+ " nil t
)
378 (setq uudecode-filename
379 (buffer-substring (point)
380 (progn (end-of-line) (point)))))))
382 (set-buffer (get-buffer-create mh-log-buffer
))
383 (setq log-begin
(mh-truncate-log-buffer))
384 (if (not (file-directory-p store-directory
))
386 (insert "mkdir " directory
"\n")
387 (call-process "mkdir" nil mh-log-buffer t store-directory
)))
388 (insert "cd " directory
"\n")
389 (setq mh-store-default-directory directory
)
392 (setq command
"uudecode")
393 (insert uudecode-filename
" being uudecoded...\n"))))
394 (set-window-start (display-buffer mh-log-buffer
) log-begin
) ;watch progress
395 (let ((default-directory (file-name-as-directory store-directory
)))
396 (if (equal (call-process-region sh-start
(point-max) command
400 (set-buffer mh-log-buffer
)
401 (insert "\n(mh-store finished)\n"))
402 (error "Error occurred during execution of %s" command
)))))
409 (defun mh-ephem-message (string)
410 "Display STRING in the minibuffer momentarily."
411 (message "%s" string
)
417 "Display cheat sheet for the MH-Folder commands in minibuffer."
420 (substitute-command-keys
421 (mapconcat 'identity
(cdr (assoc nil mh-help-messages
)) ""))))
424 (defun mh-prefix-help ()
425 "Display cheat sheet for the commands of the current prefix in minibuffer."
427 ;; We got here because the user pressed a `?', but he pressed a prefix key
428 ;; before that. Since the the key vector starts at index 0, the index of the
429 ;; last keystroke is length-1 and thus the second to last keystroke is at
430 ;; length-2. We use that information to obtain a suitable prefix character
431 ;; from the recent keys.
432 (let* ((keys (recent-keys))
433 (prefix-char (elt keys
(- (length keys
) 2))))
435 (substitute-command-keys
436 (mapconcat 'identity
(cdr (assoc prefix-char mh-help-messages
)) "")))))
441 ;;; indent-tabs-mode: nil
442 ;;; sentence-end-double-space: nil
445 ;;; arch-tag: 1936c4f1-4843-438e-bc4b-a63bb75a7762
446 ;;; mh-funcs.el ends here