88155
|
1 ;;; mh-init.el --- MH-E initialization
|
|
2
|
|
3 ;; Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Peter S. Galbraith <psg@debian.org>
|
|
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
|
|
7 ;; Keywords: mail
|
|
8 ;; See: mh-e.el
|
|
9
|
|
10 ;; This file is part of GNU Emacs.
|
|
11
|
|
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)
|
|
15 ;; any later version.
|
|
16
|
|
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.
|
|
21
|
|
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.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; Sets up the MH variant (currently nmh, MH, or GNU mailutils).
|
|
30 ;;
|
|
31 ;; Users may customize `mh-variant' to switch between available variants.
|
|
32 ;; Available MH variants are returned by the function `mh-variants'.
|
|
33 ;; Developers may check which variant is currently in use with the
|
|
34 ;; variable `mh-variant-in-use' or the function `mh-variant-p'.
|
|
35 ;;
|
|
36 ;; Also contains code that is used at load or initialization time only.
|
|
37
|
|
38 ;;; Change Log:
|
|
39
|
|
40 ;;; Code:
|
|
41
|
|
42 ;;(message "> mh-init")
|
|
43 (eval-when-compile (require 'mh-acros))
|
|
44 (mh-require-cl)
|
|
45 (require 'mh-buffers)
|
|
46 (require 'mh-exec)
|
|
47 ;;(message "< mh-init")
|
|
48
|
|
49 (defvar mh-sys-path
|
|
50 '("/usr/local/nmh/bin" ; nmh default
|
|
51 "/usr/local/bin/mh/"
|
|
52 "/usr/local/mh/"
|
|
53 "/usr/bin/mh/" ; Ultrix 4.2, Linux
|
|
54 "/usr/new/mh/" ; Ultrix < 4.2
|
|
55 "/usr/contrib/mh/bin/" ; BSDI
|
|
56 "/usr/pkg/bin/" ; NetBSD
|
|
57 "/usr/local/bin/"
|
|
58 "/usr/local/bin/mu-mh/" ; GNU mailutils - default
|
|
59 "/usr/bin/mu-mh/") ; GNU mailutils - packaged
|
|
60 "List of directories to search for variants of the MH variant.
|
|
61 The list `exec-path' is searched in addition to this list.
|
|
62 There's no need for users to modify this list. Instead add extra
|
|
63 directories to the customizable variable `mh-path'.")
|
|
64
|
|
65 ;; Set for local environment:
|
|
66 ;; mh-progs and mh-lib used to be set in paths.el, which tried to
|
|
67 ;; figure out at build time which of several possible directories MH
|
|
68 ;; was installed into. But if you installed MH after building Emacs,
|
|
69 ;; this would almost certainly be wrong, so now we do it at run time.
|
|
70
|
|
71 (defvar mh-flists-present-flag nil
|
|
72 "Non-nil means that we have \"flists\".")
|
|
73
|
|
74 (defvar mh-variants nil
|
|
75 "List describing known MH variants.
|
|
76 Do not access this variable directly as it may not have yet been initialized.
|
|
77 Use the function `mh-variants' instead.")
|
|
78
|
|
79 ;;;###mh-autoload
|
|
80 (defun mh-variants ()
|
|
81 "Return a list of installed variants of MH on the system.
|
|
82 This function looks for MH in `mh-sys-path', `mh-path' and
|
|
83 `exec-path'. The format of the list of variants that is returned
|
|
84 is described by the variable `mh-variants'."
|
|
85 (if mh-variants
|
|
86 mh-variants
|
|
87 (let ((list-unique))
|
|
88 ;; Make a unique list of directories, keeping the given order.
|
|
89 ;; We don't want the same MH variant to be listed multiple times.
|
|
90 (loop for dir in (append mh-path mh-sys-path exec-path) do
|
|
91 (setq dir (file-chase-links (directory-file-name dir)))
|
|
92 (add-to-list 'list-unique dir))
|
|
93 (loop for dir in (nreverse list-unique) do
|
|
94 (when (and dir (file-directory-p dir) (file-readable-p dir))
|
|
95 (let ((variant (mh-variant-info dir)))
|
|
96 (if variant
|
|
97 (add-to-list 'mh-variants variant)))))
|
|
98 mh-variants)))
|
|
99
|
|
100 (defun mh-variant-info (dir)
|
|
101 "Return MH variant found in DIR, or nil if none present."
|
|
102 (save-excursion
|
|
103 (let ((tmp-buffer (get-buffer-create mh-temp-buffer)))
|
|
104 (set-buffer tmp-buffer)
|
|
105 (cond
|
|
106 ((mh-variant-mh-info dir))
|
|
107 ((mh-variant-nmh-info dir))
|
|
108 ((mh-variant-mu-mh-info dir))))))
|
|
109
|
|
110 (defun mh-variant-mh-info (dir)
|
|
111 "Return info for MH variant in DIR assuming a temporary buffer is setup."
|
|
112 ;; MH does not have the -version option.
|
|
113 ;; Its version number is included in the output of "-help" as:
|
|
114 ;;
|
|
115 ;; version: MH 6.8.4 #2[UCI] (burrito) of Fri Jan 15 20:01:39 EST 1999
|
|
116 ;; options: [ATHENA] [BIND] [DUMB] [LIBLOCKFILE] [LOCALE] [MAILGROUP] [MHE]
|
|
117 ;; [MHRC] [MIME] [MORE='"/usr/bin/sensible-pager"'] [NLINK_HACK]
|
|
118 ;; [NORUSERPASS] [OVERHEAD] [POP] [POPSERVICE='"pop-3"'] [RENAME]
|
|
119 ;; [RFC1342] [RPATHS] [RPOP] [SENDMTS] [SMTP] [SOCKETS]
|
|
120 ;; [SPRINTFTYPE=int] [SVR4] [SYS5] [SYS5DIR] [TERMINFO]
|
|
121 ;; [TYPESIG=void] [UNISTD] [UTK] [VSPRINTF]
|
|
122 (let ((mhparam (expand-file-name "mhparam" dir)))
|
|
123 (when (mh-file-command-p mhparam)
|
|
124 (erase-buffer)
|
|
125 (call-process mhparam nil '(t nil) nil "-help")
|
|
126 (goto-char (point-min))
|
|
127 (when (search-forward-regexp "version: MH \\(\\S +\\)" nil t)
|
|
128 (let ((version (format "MH %s" (match-string 1))))
|
|
129 (erase-buffer)
|
|
130 (call-process mhparam nil '(t nil) nil "libdir")
|
|
131 (goto-char (point-min))
|
|
132 (when (search-forward-regexp "^.*$" nil t)
|
|
133 (let ((libdir (match-string 0)))
|
|
134 `(,version
|
|
135 (variant mh)
|
|
136 (mh-lib-progs ,libdir)
|
|
137 (mh-lib ,libdir)
|
|
138 (mh-progs ,dir)
|
|
139 (flists nil)))))))))
|
|
140
|
|
141 (defun mh-variant-mu-mh-info (dir)
|
|
142 "Return info for GNU mailutils variant in DIR.
|
|
143 This assumes that a temporary buffer is setup."
|
|
144 ;; 'mhparam -version' output:
|
|
145 ;; mhparam (GNU mailutils 0.3.2)
|
|
146 (let ((mhparam (expand-file-name "mhparam" dir)))
|
|
147 (when (mh-file-command-p mhparam)
|
|
148 (erase-buffer)
|
|
149 (call-process mhparam nil '(t nil) nil "-version")
|
|
150 (goto-char (point-min))
|
|
151 (when (search-forward-regexp "mhparam (\\(GNU [Mm]ailutils \\S +\\))"
|
|
152 nil t)
|
|
153 (let ((version (match-string 1))
|
|
154 (mh-progs dir))
|
|
155 `(,version
|
|
156 (variant mu-mh)
|
|
157 (mh-lib-progs ,(mh-profile-component "libdir"))
|
|
158 (mh-lib ,(mh-profile-component "etcdir"))
|
|
159 (mh-progs ,dir)
|
|
160 (flists ,(file-exists-p
|
|
161 (expand-file-name "flists" dir)))))))))
|
|
162
|
|
163 (defun mh-variant-nmh-info (dir)
|
|
164 "Return info for nmh variant in DIR assuming a temporary buffer is setup."
|
|
165 ;; `mhparam -version' outputs:
|
|
166 ;; mhparam -- nmh-1.1-RC1 [compiled on chaak at Fri Jun 20 11:03:28 PDT 2003]
|
|
167 (let ((mhparam (expand-file-name "mhparam" dir)))
|
|
168 (when (mh-file-command-p mhparam)
|
|
169 (erase-buffer)
|
|
170 (call-process mhparam nil '(t nil) nil "-version")
|
|
171 (goto-char (point-min))
|
|
172 (when (search-forward-regexp "mhparam -- nmh-\\(\\S +\\)" nil t)
|
|
173 (let ((version (format "nmh %s" (match-string 1)))
|
|
174 (mh-progs dir))
|
|
175 `(,version
|
|
176 (variant nmh)
|
|
177 (mh-lib-progs ,(mh-profile-component "libdir"))
|
|
178 (mh-lib ,(mh-profile-component "etcdir"))
|
|
179 (mh-progs ,dir)
|
|
180 (flists ,(file-exists-p
|
|
181 (expand-file-name "flists" dir)))))))))
|
|
182
|
|
183 (defun mh-file-command-p (file)
|
|
184 "Return t if file FILE is the name of a executable regular file."
|
|
185 (and (file-regular-p file) (file-executable-p file)))
|
|
186
|
|
187 (defvar mh-variant-in-use nil
|
|
188 "The MH variant currently in use; a string with variant and version number.
|
|
189 This differs from `mh-variant' when the latter is set to
|
|
190 \"autodetect\".")
|
|
191
|
|
192 ;;;###mh-autoload
|
|
193 (defun mh-variant-set (variant)
|
|
194 "Set the MH variant to VARIANT.
|
|
195 Sets `mh-progs', `mh-lib', `mh-lib-progs' and
|
|
196 `mh-flists-present-flag'.
|
|
197 If the VARIANT is \"autodetect\", then first try nmh, then MH and
|
|
198 finally GNU mailutils."
|
|
199 (interactive
|
|
200 (list (completing-read
|
|
201 "MH variant: "
|
|
202 (mapcar (lambda (x) (list (car x))) (mh-variants))
|
|
203 nil t)))
|
|
204 (let ((valid-list (mapcar (lambda (x) (car x)) (mh-variants))))
|
|
205 (cond
|
|
206 ((eq variant 'none))
|
|
207 ((eq variant 'autodetect)
|
|
208 (cond
|
|
209 ((mh-variant-set-variant 'nmh)
|
|
210 (message "%s installed as MH variant" mh-variant-in-use))
|
|
211 ((mh-variant-set-variant 'mh)
|
|
212 (message "%s installed as MH variant" mh-variant-in-use))
|
|
213 ((mh-variant-set-variant 'mu-mh)
|
|
214 (message "%s installed as MH variant" mh-variant-in-use))
|
|
215 (t
|
|
216 (message "No MH variant found on the system"))))
|
|
217 ((member variant valid-list)
|
|
218 (when (not (mh-variant-set-variant variant))
|
|
219 (message "Warning: %s variant not found. Autodetecting..." variant)
|
|
220 (mh-variant-set 'autodetect)))
|
|
221 (t
|
|
222 (message "Unknown variant; use %s"
|
|
223 (mapconcat '(lambda (x) (format "%s" (car x)))
|
|
224 (mh-variants) " or "))))))
|
|
225
|
|
226 (defun mh-variant-set-variant (variant)
|
|
227 "Setup the system variables for the MH variant named VARIANT.
|
|
228 If VARIANT is a string, use that key in the alist returned by the
|
|
229 function `mh-variants'.
|
|
230 If VARIANT is a symbol, select the first entry that matches that
|
|
231 variant."
|
|
232 (cond
|
|
233 ((stringp variant) ;e.g. "nmh 1.1-RC1"
|
|
234 (when (assoc variant (mh-variants))
|
|
235 (let* ((alist (cdr (assoc variant (mh-variants))))
|
|
236 (lib-progs (cadr (assoc 'mh-lib-progs alist)))
|
|
237 (lib (cadr (assoc 'mh-lib alist)))
|
|
238 (progs (cadr (assoc 'mh-progs alist)))
|
|
239 (flists (cadr (assoc 'flists alist))))
|
|
240 ;;(set-default mh-variant variant)
|
|
241 (setq mh-x-mailer-string nil
|
|
242 mh-flists-present-flag flists
|
|
243 mh-lib-progs lib-progs
|
|
244 mh-lib lib
|
|
245 mh-progs progs
|
|
246 mh-variant-in-use variant))))
|
|
247 ((symbolp variant) ;e.g. 'nmh (pick the first match)
|
|
248 (loop for variant-list in (mh-variants)
|
|
249 when (eq variant (cadr (assoc 'variant (cdr variant-list))))
|
|
250 return (let* ((version (car variant-list))
|
|
251 (alist (cdr variant-list))
|
|
252 (lib-progs (cadr (assoc 'mh-lib-progs alist)))
|
|
253 (lib (cadr (assoc 'mh-lib alist)))
|
|
254 (progs (cadr (assoc 'mh-progs alist)))
|
|
255 (flists (cadr (assoc 'flists alist))))
|
|
256 ;;(set-default mh-variant flavor)
|
|
257 (setq mh-x-mailer-string nil
|
|
258 mh-flists-present-flag flists
|
|
259 mh-lib-progs lib-progs
|
|
260 mh-lib lib
|
|
261 mh-progs progs
|
|
262 mh-variant-in-use version)
|
|
263 t)))))
|
|
264
|
|
265 ;;;###mh-autoload
|
|
266 (defun mh-variant-p (&rest variants)
|
|
267 "Return t if variant is any of VARIANTS.
|
|
268 Currently known variants are 'MH, 'nmh, and 'mu-mh."
|
|
269 (let ((variant-in-use
|
|
270 (cadr (assoc 'variant (assoc mh-variant-in-use (mh-variants))))))
|
|
271 (not (null (member variant-in-use variants)))))
|
|
272
|
|
273
|
|
274
|
|
275 ;;; Read MH Profile
|
|
276
|
|
277 (defvar mh-find-path-run nil
|
|
278 "Non-nil if `mh-find-path' has been run already.
|
|
279 Do not access this variable; `mh-find-path' already uses it to
|
|
280 avoid running more than once.")
|
|
281
|
|
282 (defun mh-find-path ()
|
|
283 "Set variables from user's MH profile.
|
|
284
|
|
285 This function sets `mh-user-path' from your \"Path:\" MH profile
|
|
286 component (but defaults to \"Mail\" if one isn't present),
|
|
287 `mh-draft-folder' from \"Draft-Folder:\", `mh-unseen-seq' from
|
|
288 \"Unseen-Sequence:\", `mh-previous-seq' from
|
|
289 \"Previous-Sequence:\", and `mh-inbox' from \"Inbox:\" (defaults
|
|
290 to \"+inbox\").
|
|
291
|
|
292 The hook `mh-find-path-hook' is run after these variables have
|
|
293 been set. This hook can be used the change the value of these
|
|
294 variables if you need to run with different values between MH and
|
|
295 MH-E."
|
|
296 (unless mh-find-path-run
|
|
297 ;; Sanity checks.
|
|
298 (if (and (getenv "MH")
|
|
299 (not (file-readable-p (getenv "MH"))))
|
|
300 (error "MH environment variable contains unreadable file %s"
|
|
301 (getenv "MH")))
|
|
302 (if (null (mh-variants))
|
|
303 (error "Install MH and run install-mh before running MH-E"))
|
|
304 (let ((profile "~/.mh_profile"))
|
|
305 (if (not (file-readable-p profile))
|
|
306 (error "Run install-mh before running MH-E")))
|
|
307 ;; Read MH profile.
|
|
308 (setq mh-user-path (mh-profile-component "Path"))
|
|
309 (if (not mh-user-path)
|
|
310 (setq mh-user-path "Mail"))
|
|
311 (setq mh-user-path
|
|
312 (file-name-as-directory
|
|
313 (expand-file-name mh-user-path (expand-file-name "~"))))
|
|
314 (unless mh-x-image-cache-directory
|
|
315 (setq mh-x-image-cache-directory
|
|
316 (expand-file-name ".mhe-x-image-cache" mh-user-path)))
|
|
317 (setq mh-draft-folder (mh-profile-component "Draft-Folder"))
|
|
318 (if mh-draft-folder
|
|
319 (progn
|
|
320 (if (not (mh-folder-name-p mh-draft-folder))
|
|
321 (setq mh-draft-folder (format "+%s" mh-draft-folder)))
|
|
322 (if (not (file-exists-p (mh-expand-file-name mh-draft-folder)))
|
|
323 (error
|
|
324 "Draft folder \"%s\" not found; create it and try again"
|
|
325 (mh-expand-file-name mh-draft-folder)))))
|
|
326 (setq mh-inbox (mh-profile-component "Inbox"))
|
|
327 (cond ((not mh-inbox)
|
|
328 (setq mh-inbox "+inbox"))
|
|
329 ((not (mh-folder-name-p mh-inbox))
|
|
330 (setq mh-inbox (format "+%s" mh-inbox))))
|
|
331 (setq mh-unseen-seq (mh-profile-component "Unseen-Sequence"))
|
|
332 (if mh-unseen-seq
|
|
333 (setq mh-unseen-seq (intern mh-unseen-seq))
|
|
334 (setq mh-unseen-seq 'unseen)) ;old MH default?
|
|
335 (setq mh-previous-seq (mh-profile-component "Previous-Sequence"))
|
|
336 (if mh-previous-seq
|
|
337 (setq mh-previous-seq (intern mh-previous-seq)))
|
|
338 (run-hooks 'mh-find-path-hook)
|
|
339 (mh-collect-folder-names)
|
|
340 (setq mh-find-path-run t)))
|
|
341
|
|
342
|
|
343
|
|
344 ;;; MH profile
|
|
345
|
|
346 (defun mh-profile-component (component)
|
|
347 "Return COMPONENT value from mhparam, or nil if unset."
|
|
348 (save-excursion
|
|
349 (mh-exec-cmd-quiet nil "mhparam" "-components" component)
|
|
350 (mh-profile-component-value component)))
|
|
351
|
|
352 (defun mh-profile-component-value (component)
|
|
353 "Find and return the value of COMPONENT in the current buffer.
|
|
354 Returns nil if the component is not in the buffer."
|
|
355 (let ((case-fold-search t))
|
|
356 (goto-char (point-min))
|
|
357 (cond ((not (re-search-forward (format "^%s:" component) nil t)) nil)
|
|
358 ((looking-at "[\t ]*$") nil)
|
|
359 (t
|
|
360 (re-search-forward "[\t ]*\\([^\t \n].*\\)$" nil t)
|
|
361 (let ((start (match-beginning 1)))
|
|
362 (end-of-line)
|
|
363 (buffer-substring start (point)))))))
|
|
364
|
|
365
|
|
366
|
|
367 ;;; MH-E images
|
|
368
|
|
369 ;; Shush compiler.
|
|
370 (eval-when-compile (defvar image-load-path))
|
|
371
|
|
372 (defvar mh-image-load-path-called-flag nil)
|
|
373
|
|
374 ;;;###mh-autoload
|
|
375 (defun mh-image-load-path ()
|
|
376 "Ensure that the MH-E images are accessible by `find-image'.
|
|
377 Images for MH-E are found in ../../etc/images relative to the
|
|
378 files in \"lisp/mh-e\". If `image-load-path' exists (since Emacs
|
|
379 22), then the images directory is added to it if isn't already
|
|
380 there. Otherwise, the images directory is added to the
|
|
381 `load-path' if it isn't already there."
|
|
382 (unless mh-image-load-path-called-flag
|
|
383 (let (mh-library-name mh-image-load-path)
|
|
384 ;; First, find mh-e in the load-path.
|
|
385 (setq mh-library-name (locate-library "mh-e"))
|
|
386 (if (not mh-library-name)
|
|
387 (error "Can not find MH-E in load-path"))
|
|
388 (setq mh-image-load-path
|
|
389 (expand-file-name (concat (file-name-directory mh-library-name)
|
|
390 "../../etc/images")))
|
|
391 (if (not (file-exists-p mh-image-load-path))
|
|
392 (error "Can not find image directory %s" mh-image-load-path))
|
|
393 (if (boundp 'image-load-path)
|
|
394 (add-to-list 'image-load-path mh-image-load-path)
|
|
395 (add-to-list 'load-path mh-image-load-path)))
|
|
396 (setq mh-image-load-path-called-flag t)))
|
|
397
|
|
398
|
|
399
|
|
400 ;;; Support routines for mh-customize.el
|
|
401
|
|
402 (defvar mh-min-colors-defined-flag (and (not mh-xemacs-flag)
|
|
403 (>= emacs-major-version 22))
|
|
404 "Non-nil means defface supports min-colors display requirement.")
|
|
405
|
|
406 (defun mh-defface-compat (spec)
|
|
407 "Convert SPEC for defface if necessary to run on older platforms.
|
|
408 Modifies SPEC in place and returns it. See `defface' for the spec definition.
|
|
409
|
|
410 When `mh-min-colors-defined-flag' is nil, this function finds
|
|
411 display entries with \"min-colors\" requirements and either
|
|
412 removes the \"min-colors\" requirement or strips the display
|
|
413 entirely if the display does not support the number of specified
|
|
414 colors."
|
|
415 (if mh-min-colors-defined-flag
|
|
416 spec
|
|
417 (let ((cells (display-color-cells))
|
|
418 new-spec)
|
|
419 ;; Remove entries with min-colors, or delete them if we have fewer colors
|
|
420 ;; than they specify.
|
|
421 (loop for entry in (reverse spec) do
|
|
422 (let ((requirement (if (eq (car entry) t)
|
|
423 nil
|
|
424 (assoc 'min-colors (car entry)))))
|
|
425 (if requirement
|
|
426 (when (>= cells (nth 1 requirement))
|
|
427 (setq new-spec (cons (cons (delq requirement (car entry))
|
|
428 (cdr entry))
|
|
429 new-spec)))
|
|
430 (setq new-spec (cons entry new-spec)))))
|
|
431 new-spec)))
|
|
432
|
|
433 (provide 'mh-init)
|
|
434
|
|
435 ;; Local Variables:
|
|
436 ;; indent-tabs-mode: nil
|
|
437 ;; sentence-end-double-space: nil
|
|
438 ;; End:
|
|
439
|
|
440 ;; arch-tag: e8372aeb-d803-42b1-9c95-3c93ad22f63c
|
|
441 ;;; mh-init.el ends here
|