(pop-up-frame-function): Remove choice nil since it
[emacs.git] / lisp / mh-e / mh-alias.el
blob69e47559a38cc6aa98507d8138229eb87664e041
1 ;;; mh-alias.el --- MH-E mail alias completion and expansion
3 ;; Copyright (C) 1994, 1995, 1996, 1997,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
7 ;; Author: Peter S. Galbraith <psg@debian.org>
8 ;; Maintainer: Bill Wohler <wohler@newt.com>
9 ;; Keywords: mail
10 ;; See: mh-e.el
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;;; Change Log:
31 ;;; Code:
33 (require 'mh-e)
35 (mh-require-cl)
37 (require 'goto-addr)
39 (defvar mh-alias-alist 'not-read
40 "Alist of MH aliases.")
41 (defvar mh-alias-blind-alist nil
42 "Alist of MH aliases that are blind lists.")
43 (defvar mh-alias-passwd-alist nil
44 "Alist of aliases extracted from passwd file and their expansions.")
45 (defvar mh-alias-tstamp nil
46 "Time aliases were last loaded.")
47 (defvar mh-alias-read-address-map
48 (let ((map (copy-keymap minibuffer-local-completion-map)))
49 (define-key map "," 'mh-alias-minibuffer-confirm-address)
50 (define-key map " " 'self-insert-command)
51 map))
53 (defvar mh-alias-system-aliases
54 '("/etc/nmh/MailAliases" "/etc/mh/MailAliases"
55 "/usr/lib/mh/MailAliases" "/usr/share/mailutils/mh/MailAliases"
56 "/etc/passwd")
57 "*A list of system files which are a source of aliases.
58 If these files are modified, they are automatically reread. This list
59 need include only system aliases and the passwd file, since personal
60 alias files listed in your \"Aliasfile:\" MH profile component are
61 automatically included. You can update the alias list manually using
62 \\[mh-alias-reload].")
66 ;;; Alias Loading
68 (defun mh-alias-tstamp (arg)
69 "Check whether alias files have been modified.
70 Return t if any file listed in the Aliasfile MH profile component has
71 been modified since the timestamp.
72 If ARG is non-nil, set timestamp with the current time."
73 (if arg
74 (let ((time (current-time)))
75 (setq mh-alias-tstamp (list (nth 0 time) (nth 1 time))))
76 (let ((stamp))
77 (car (memq t (mapcar
78 (function
79 (lambda (file)
80 (when (and file (file-exists-p file))
81 (setq stamp (nth 5 (file-attributes file)))
82 (or (> (car stamp) (car mh-alias-tstamp))
83 (and (= (car stamp) (car mh-alias-tstamp))
84 (> (cadr stamp) (cadr mh-alias-tstamp)))))))
85 (mh-alias-filenames t)))))))
87 (defun mh-alias-filenames (arg)
88 "Return list of filenames that contain aliases.
89 The filenames come from the Aliasfile profile component and are
90 expanded.
91 If ARG is non-nil, filenames listed in `mh-alias-system-aliases' are
92 appended."
93 (or mh-progs (mh-find-path))
94 (save-excursion
95 (let* ((filename (mh-profile-component "Aliasfile"))
96 (filelist (and filename (split-string filename "[ \t]+")))
97 (userlist
98 (mapcar
99 (function
100 (lambda (file)
101 (if (and mh-user-path file
102 (file-exists-p (expand-file-name file mh-user-path)))
103 (expand-file-name file mh-user-path))))
104 filelist)))
105 (if arg
106 (if (stringp mh-alias-system-aliases)
107 (append userlist (list mh-alias-system-aliases))
108 (append userlist mh-alias-system-aliases))
109 userlist))))
111 (defun mh-alias-gecos-name (gecos-name username comma-separator)
112 "Return a usable address string from a GECOS-NAME and USERNAME.
113 Use only part of the GECOS-NAME up to the first comma if
114 COMMA-SEPARATOR is non-nil."
115 (let ((res gecos-name))
116 ;; Keep only string until first comma if COMMA-SEPARATOR is t.
117 (if (and comma-separator
118 (string-match "^\\([^,]+\\)," res))
119 (setq res (match-string 1 res)))
120 ;; Replace "&" with capitalized username
121 (if (string-match "&" res)
122 (setq res (mh-replace-regexp-in-string "&" (capitalize username) res)))
123 ;; Remove " character
124 (if (string-match "\"" res)
125 (setq res (mh-replace-regexp-in-string "\"" "" res)))
126 ;; If empty string, use username instead
127 (if (string-equal "" res)
128 (setq res username))
129 ;; Surround by quotes if doesn't consist of simple characters
130 (if (not (string-match "^[ a-zA-Z0-9-]+$" res))
131 (setq res (concat "\"" res "\"")))
132 res))
134 (defun mh-alias-local-users ()
135 "Return an alist of local users from /etc/passwd.
136 Exclude all aliases already in `mh-alias-alist' from \"ali\""
137 (let (passwd-alist)
138 (save-excursion
139 (set-buffer (get-buffer-create mh-temp-buffer))
140 (erase-buffer)
141 (cond
142 ((eq mh-alias-local-users t)
143 (if (file-readable-p "/etc/passwd")
144 (insert-file-contents "/etc/passwd")))
145 ((stringp mh-alias-local-users)
146 (insert mh-alias-local-users "\n")
147 (shell-command-on-region (point-min) (point-max) mh-alias-local-users t)
148 (goto-char (point-min))))
149 (while (< (point) (point-max))
150 (cond
151 ((looking-at "\\([^:]*\\):[^:]*:\\([^:]*\\):[^:]*:\\([^:]*\\):")
152 (when (> (string-to-number (match-string 2)) 200)
153 (let* ((username (match-string 1))
154 (gecos-name (match-string 3))
155 (realname (mh-alias-gecos-name
156 gecos-name username
157 mh-alias-passwd-gecos-comma-separator-flag))
158 (alias-name (if mh-alias-local-users-prefix
159 (concat mh-alias-local-users-prefix
160 (mh-alias-suggest-alias realname t))
161 username))
162 (alias-translation
163 (if (string-equal username realname)
164 (concat "<" username ">")
165 (concat realname " <" username ">"))))
166 (when (not (mh-assoc-string alias-name mh-alias-alist t))
167 (setq passwd-alist (cons (list alias-name alias-translation)
168 passwd-alist)))))))
169 (forward-line 1)))
170 passwd-alist))
172 (defun mh-alias-reload ()
173 "Reload MH aliases.
175 Since aliases are updated frequently, MH-E reloads aliases
176 automatically whenever an alias lookup occurs if an alias source has
177 changed. Sources include files listed in your \"Aliasfile:\" profile
178 component and your password file if option `mh-alias-local-users' is
179 turned on. However, you can reload your aliases manually by calling
180 this command directly.
182 This function runs `mh-alias-reloaded-hook' after the aliases have
183 been loaded."
184 (interactive)
185 (save-excursion
186 (message "Loading MH aliases...")
187 (mh-alias-tstamp t)
188 (mh-exec-cmd-quiet t "ali" "-nolist" "-nouser")
189 (setq mh-alias-alist nil)
190 (setq mh-alias-blind-alist nil)
191 (while (< (point) (point-max))
192 (cond
193 ((looking-at "^[ \t]")) ;Continuation line
194 ((looking-at "\\(.+\\): .+: .*$") ; A new -blind- MH alias
195 (when (not (mh-assoc-string (match-string 1) mh-alias-blind-alist t))
196 (setq mh-alias-blind-alist
197 (cons (list (match-string 1)) mh-alias-blind-alist))
198 (setq mh-alias-alist (cons (list (match-string 1)) mh-alias-alist))))
199 ((looking-at "\\(.+\\): .*$") ; A new MH alias
200 (when (not (mh-assoc-string (match-string 1) mh-alias-alist t))
201 (setq mh-alias-alist
202 (cons (list (match-string 1)) mh-alias-alist)))))
203 (forward-line 1)))
204 (when mh-alias-local-users
205 (setq mh-alias-passwd-alist (mh-alias-local-users))
206 ;; Update aliases with local users, but leave existing aliases alone.
207 (let ((local-users mh-alias-passwd-alist)
208 user)
209 (while local-users
210 (setq user (car local-users))
211 (if (not (mh-assoc-string (car user) mh-alias-alist t))
212 (setq mh-alias-alist (append mh-alias-alist (list user))))
213 (setq local-users (cdr local-users)))))
214 (run-hooks 'mh-alias-reloaded-hook)
215 (message "Loading MH aliases...done"))
217 ;;;###mh-autoload
218 (defun mh-alias-reload-maybe ()
219 "Load new MH aliases."
220 (if (or (eq mh-alias-alist 'not-read) ; Doesn't exist?
221 (mh-alias-tstamp nil)) ; Out of date?
222 (mh-alias-reload)))
226 ;;; Alias Expansion
228 (defun mh-alias-ali (alias &optional user)
229 "Return ali expansion for ALIAS.
230 ALIAS must be a string for a single alias.
231 If USER is t, then assume ALIAS is an address and call ali -user. ali
232 returns the string unchanged if not defined. The same is done here."
233 (condition-case err
234 (save-excursion
235 (let ((user-arg (if user "-user" "-nouser")))
236 (mh-exec-cmd-quiet t "ali" user-arg "-nolist" alias))
237 (goto-char (point-max))
238 (if (looking-at "^$") (delete-backward-char 1))
239 (buffer-substring (point-min)(point-max)))
240 (error (progn
241 (message "%s" (error-message-string err))
242 alias))))
244 ;;;###mh-autoload
245 (defun mh-alias-expand (alias)
246 "Return expansion for ALIAS.
247 Blind aliases or users from /etc/passwd are not expanded."
248 (cond
249 ((mh-assoc-string alias mh-alias-blind-alist t)
250 alias) ; Don't expand a blind alias
251 ((mh-assoc-string alias mh-alias-passwd-alist t)
252 (cadr (mh-assoc-string alias mh-alias-passwd-alist t)))
254 (mh-alias-ali alias))))
256 (mh-require 'crm nil t) ; completing-read-multiple
257 (mh-require 'multi-prompt nil t)
259 ;;;###mh-autoload
260 (defun mh-read-address (prompt)
261 "Read an address from the minibuffer with PROMPT."
262 (mh-alias-reload-maybe)
263 (if (not mh-alias-alist) ; If still no aliases, just prompt
264 (read-string prompt)
265 (let* ((minibuffer-local-completion-map mh-alias-read-address-map)
266 (completion-ignore-case mh-alias-completion-ignore-case-flag)
267 (the-answer
268 (cond ((fboundp 'completing-read-multiple)
269 (mh-funcall-if-exists
270 completing-read-multiple prompt mh-alias-alist nil nil))
271 ((featurep 'multi-prompt)
272 (mh-funcall-if-exists
273 multi-prompt "," nil prompt mh-alias-alist nil nil))
274 (t (split-string
275 (completing-read prompt mh-alias-alist nil nil) ",")))))
276 (if (not mh-alias-expand-aliases-flag)
277 (mapconcat 'identity the-answer ", ")
278 ;; Loop over all elements, checking if in passwd aliast or blind first
279 (mapconcat 'mh-alias-expand the-answer ",\n ")))))
281 ;;;###mh-autoload
282 (defun mh-alias-minibuffer-confirm-address ()
283 "Display the alias expansion if `mh-alias-flash-on-comma' is non-nil."
284 (interactive)
285 (when mh-alias-flash-on-comma
286 (save-excursion
287 (let* ((case-fold-search t)
288 (beg (mh-beginning-of-word))
289 (the-name (buffer-substring-no-properties beg (point))))
290 (if (mh-assoc-string the-name mh-alias-alist t)
291 (message "%s -> %s" the-name (mh-alias-expand the-name))
292 ;; Check if if was a single word likely to be an alias
293 (if (and (equal mh-alias-flash-on-comma 1)
294 (not (string-match " " the-name)))
295 (message "No alias for %s" the-name))))))
296 (self-insert-command 1))
298 ;;;###mh-autoload
299 (defun mh-alias-letter-expand-alias ()
300 "Expand mail alias before point."
301 (mh-alias-reload-maybe)
302 (let* ((end (point))
303 (begin (mh-beginning-of-word))
304 (input (buffer-substring-no-properties begin end)))
305 (mh-complete-word input mh-alias-alist begin end)
306 (when mh-alias-expand-aliases-flag
307 (let* ((end (point))
308 (expansion (mh-alias-expand (buffer-substring begin end))))
309 (delete-region begin end)
310 (insert expansion)))))
314 ;;; Alias File Updating
316 (defun mh-alias-suggest-alias (string &optional no-comma-swap)
317 "Suggest an alias for STRING.
318 Don't reverse the order of strings separated by a comma if
319 NO-COMMA-SWAP is non-nil."
320 (cond
321 ((string-match "^<\\(.*\\)>$" string)
322 ;; <somename@foo.bar> -> recurse, stripping brackets.
323 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
324 ((string-match "^\\sw+$" string)
325 ;; One word -> downcase it.
326 (downcase string))
327 ((string-match "^\\(\\sw+\\)\\s-+\\(\\sw+\\)$" string)
328 ;; Two words -> first.last
329 (downcase
330 (format "%s.%s" (match-string 1 string) (match-string 2 string))))
331 ((string-match "^\\([-a-zA-Z0-9._]+\\)@[-a-zA-z0-9_]+\\.+[a-zA-Z0-9]+$"
332 string)
333 ;; email only -> downcase username
334 (downcase (match-string 1 string)))
335 ((string-match "^\"\\(.*\\)\".*" string)
336 ;; "Some name" <somename@foo.bar> -> recurse -> "Some name"
337 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
338 ((string-match "^\\(.*\\) +<.*>$" string)
339 ;; Some name <somename@foo.bar> -> recurse -> Some name
340 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
341 ((string-match (concat goto-address-mail-regexp " +(\\(.*\\))$") string)
342 ;; somename@foo.bar (Some name) -> recurse -> Some name
343 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
344 ((string-match "^\\(Dr\\|Prof\\)\\.? +\\(.*\\)" string)
345 ;; Strip out title
346 (mh-alias-suggest-alias (match-string 2 string) no-comma-swap))
347 ((string-match "^\\(.*\\), +\\(Jr\\.?\\|II+\\)$" string)
348 ;; Strip out tails with comma
349 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
350 ((string-match "^\\(.*\\) +\\(Jr\\.?\\|II+\\)$" string)
351 ;; Strip out tails
352 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
353 ((string-match "^\\(\\sw+\\) +[A-Z]\\.? +\\(.*\\)$" string)
354 ;; Strip out initials
355 (mh-alias-suggest-alias
356 (format "%s %s" (match-string 1 string) (match-string 2 string))
357 no-comma-swap))
358 ((and (not no-comma-swap)
359 (string-match "^\\([^,]+\\), +\\(.*\\)$" string))
360 ;; Reverse order of comma-separated fields to handle:
361 ;; From: "Galbraith, Peter" <psg@debian.org>
362 ;; but don't this for a name string extracted from the passwd file
363 ;; with mh-alias-passwd-gecos-comma-separator-flag set to nil.
364 (mh-alias-suggest-alias
365 (format "%s %s" (match-string 2 string) (match-string 1 string))
366 no-comma-swap))
368 ;; Output string, with spaces replaced by dots.
369 (mh-alias-canonicalize-suggestion string))))
371 (defun mh-alias-canonicalize-suggestion (string)
372 "Process STRING to replace spaces by periods.
373 First all spaces and commas are replaced by periods. Then every run of
374 consecutive periods are replaced with a single period. Finally the
375 string is converted to lower case."
376 (with-temp-buffer
377 (insert string)
378 ;; Replace spaces with periods
379 (goto-char (point-min))
380 (while (re-search-forward " +" nil t)
381 (replace-match "." nil nil))
382 ;; Replace commas with periods
383 (goto-char (point-min))
384 (while (re-search-forward ",+" nil t)
385 (replace-match "." nil nil))
386 ;; Replace consecutive periods with a single period
387 (goto-char (point-min))
388 (while (re-search-forward "\\.\\.+" nil t)
389 (replace-match "." nil nil))
390 ;; Convert to lower case
391 (downcase-region (point-min) (point-max))
392 ;; Whew! all done...
393 (buffer-string)))
395 (defun mh-alias-which-file-has-alias (alias file-list)
396 "Return the name of writable file which defines ALIAS from list FILE-LIST."
397 (save-excursion
398 (set-buffer (get-buffer-create mh-temp-buffer))
399 (let ((the-list file-list)
400 (found))
401 (while the-list
402 (erase-buffer)
403 (when (file-writable-p (car file-list))
404 (insert-file-contents (car file-list))
405 (if (re-search-forward (concat "^" (regexp-quote alias) ":") nil t)
406 (setq found (car file-list)
407 the-list nil)
408 (setq the-list (cdr the-list)))))
409 found)))
411 (defun mh-alias-insert-file (&optional alias)
412 "Return filename which should be used to add ALIAS.
413 The value of the option `mh-alias-insert-file' is used if non-nil\;
414 otherwise the value of the \"Aliasfile:\" profile component is used.
415 If the alias already exists, try to return the name of the file that
416 contains it."
417 (cond
418 ((and mh-alias-insert-file (listp mh-alias-insert-file))
419 (if (not (elt mh-alias-insert-file 1)) ; Only one entry, use it
420 (car mh-alias-insert-file)
421 (if (or (not alias)
422 (string-equal alias (mh-alias-ali alias))) ;alias doesn't exist
423 (completing-read "Alias file: "
424 (mapcar 'list mh-alias-insert-file) nil t)
425 (or (mh-alias-which-file-has-alias alias mh-alias-insert-file)
426 (completing-read "Alias file: "
427 (mapcar 'list mh-alias-insert-file) nil t)))))
428 ((and mh-alias-insert-file (stringp mh-alias-insert-file))
429 mh-alias-insert-file)
431 ;; writable ones returned from (mh-alias-filenames):
432 (let ((autolist (delq nil (mapcar (lambda (file)
433 (if (and (file-writable-p file)
434 (not (string-equal
435 file "/etc/passwd")))
436 file))
437 (mh-alias-filenames t)))))
438 (cond
439 ((not autolist)
440 (error "No writable alias file;
441 set `mh-alias-insert-file' or the \"Aliasfile:\" profile component"))
442 ((not (elt autolist 1)) ; Only one entry, use it
443 (car autolist))
444 ((or (not alias)
445 (string-equal alias (mh-alias-ali alias))) ;alias doesn't exist
446 (completing-read "Alias file: " (mapcar 'list autolist) nil t))
448 (or (mh-alias-which-file-has-alias alias autolist)
449 (completing-read "Alias file: "
450 (mapcar 'list autolist) nil t))))))))
452 ;;;###mh-autoload
453 (defun mh-alias-address-to-alias (address)
454 "Return the ADDRESS alias if defined, or nil."
455 (let* ((aliases (mh-alias-ali address t)))
456 (if (string-equal aliases address)
457 nil ; ali returned same string -> no.
458 ;; Double-check that we have an individual alias. This means that the
459 ;; alias doesn't expand into a list (of which this address is part).
460 (car (delq nil (mapcar
461 (function
462 (lambda (alias)
463 (let ((recurse (mh-alias-ali alias nil)))
464 (if (string-match ".*,.*" recurse)
466 alias))))
467 (split-string aliases ", +")))))))
469 ;;;###mh-autoload
470 (defun mh-alias-for-from-p ()
471 "Return t if sender's address has a corresponding alias."
472 (mh-alias-reload-maybe)
473 (save-excursion
474 (if (not (mh-folder-line-matches-show-buffer-p))
475 nil ;No corresponding show buffer
476 (if (eq major-mode 'mh-folder-mode)
477 (set-buffer mh-show-buffer))
478 (let ((from-header (mh-extract-from-header-value)))
479 (and from-header
480 (mh-alias-address-to-alias from-header))))))
482 (defun mh-alias-add-alias-to-file (alias address &optional file)
483 "Add ALIAS for ADDRESS in alias FILE without alias check or prompts.
484 Prompt for alias file if not provided and there is more than one
485 candidate.
487 If the alias exists already, you will have the choice of
488 inserting the new alias before or after the old alias. In the
489 former case, this alias will be used when sending mail to this
490 alias. In the latter case, the alias serves as an additional
491 folder name hint when filing messages."
492 (if (not file)
493 (setq file (mh-alias-insert-file alias)))
494 (save-excursion
495 (set-buffer (find-file-noselect file))
496 (goto-char (point-min))
497 (let ((alias-search (concat alias ":"))
498 (letter)
499 (case-fold-search t))
500 (cond
501 ;; Search for exact match (if we had the same alias before)
502 ((re-search-forward
503 (concat "^" (regexp-quote alias-search) " *\\(.*\\)") nil t)
504 (let ((answer (read-string
505 (format (concat "Alias %s exists; insert new address "
506 "[b]efore or [a]fter: ")
507 (match-string 1))))
508 (case-fold-search t))
509 (cond ((string-match "^b" answer))
510 ((string-match "^a" answer)
511 (forward-line 1))
513 (error "Unrecognized response")))))
514 ;; No, so sort-in at the right place
515 ;; search for "^alias", then "^alia", etc.
516 ((eq mh-alias-insertion-location 'sorted)
517 (setq letter (substring alias-search -1)
518 alias-search (substring alias-search 0 -1))
519 (while (and (not (equal alias-search ""))
520 (not (re-search-forward
521 (concat "^" (regexp-quote alias-search)) nil t)))
522 (setq letter (substring alias-search -1)
523 alias-search (substring alias-search 0 -1)))
524 ;; Next, move forward to sort alphabetically for following letters
525 (beginning-of-line)
526 (while (re-search-forward
527 (concat "^" (regexp-quote alias-search) "[a-" letter "]")
528 nil t)
529 (forward-line 1)))
530 ((eq mh-alias-insertion-location 'bottom)
531 (goto-char (point-max)))
532 ((eq mh-alias-insertion-location 'top)
533 (goto-char (point-min)))))
534 (beginning-of-line)
535 (insert (format "%s: %s\n" alias address))
536 (save-buffer)))
538 (defun mh-alias-add-alias (alias address)
539 "Add ALIAS for ADDRESS in personal alias file.
541 This function prompts you for an alias and address. If the alias
542 exists already, you will have the choice of inserting the new
543 alias before or after the old alias. In the former case, this
544 alias will be used when sending mail to this alias. In the latter
545 case, the alias serves as an additional folder name hint when
546 filing messages."
547 (interactive "P\nP")
548 (mh-alias-reload-maybe)
549 (setq alias (completing-read "Alias: " mh-alias-alist nil nil alias))
550 (if (and address (string-match "^<\\(.*\\)>$" address))
551 (setq address (match-string 1 address)))
552 (setq address (read-string "Address: " address))
553 (if (string-match "^<\\(.*\\)>$" address)
554 (setq address (match-string 1 address)))
555 (let ((address-alias (mh-alias-address-to-alias address))
556 (alias-address (mh-alias-expand alias)))
557 (if (string-equal alias-address alias)
558 (setq alias-address nil))
559 (cond
560 ((and (equal alias address-alias)
561 (equal address alias-address))
562 (message "Already defined as %s" alias-address))
563 (address-alias
564 (if (y-or-n-p (format "Address has alias %s; set new one? "
565 address-alias))
566 (mh-alias-add-alias-to-file alias address)))
568 (mh-alias-add-alias-to-file alias address)))))
570 ;;;###mh-autoload
571 (defun mh-alias-grab-from-field ()
572 "Add alias for the sender of the current message."
573 (interactive)
574 (mh-alias-reload-maybe)
575 (save-excursion
576 (cond
577 ((mh-folder-line-matches-show-buffer-p)
578 (set-buffer mh-show-buffer))
579 ((and (eq major-mode 'mh-folder-mode)
580 (mh-get-msg-num nil))
581 (set-buffer (get-buffer-create mh-temp-buffer))
582 (insert-file-contents (mh-msg-filename (mh-get-msg-num t))))
583 ((eq major-mode 'mh-folder-mode)
584 (error "Cursor not pointing to a message")))
585 (let* ((address (or (mh-extract-from-header-value)
586 (error "Message has no From: header")))
587 (alias (mh-alias-suggest-alias address)))
588 (mh-alias-add-alias alias address))))
590 (defun mh-alias-add-address-under-point ()
591 "Insert an alias for address under point."
592 (interactive)
593 (let ((address (goto-address-find-address-at-point)))
594 (if address
595 (mh-alias-add-alias nil address)
596 (message "No email address found under point"))))
598 (defun mh-alias-apropos (regexp)
599 "Show all aliases or addresses that match a regular expression REGEXP."
600 (interactive "sAlias regexp: ")
601 (if mh-alias-local-users
602 (mh-alias-reload-maybe))
603 (let ((matches "")
604 (group-matches "")
605 (passwd-matches))
606 (save-excursion
607 (message "Reading MH aliases...")
608 (mh-exec-cmd-quiet t "ali" "-nolist" "-nouser")
609 (message "Parsing MH aliases...")
610 (while (re-search-forward regexp nil t)
611 (beginning-of-line)
612 (cond
613 ((looking-at "^[ \t]") ;Continuation line
614 (setq group-matches
615 (concat group-matches
616 (buffer-substring
617 (save-excursion
618 (or (re-search-backward "^[^ \t]" nil t)
619 (point)))
620 (progn
621 (if (re-search-forward "^[^ \t]" nil t)
622 (forward-char -1))
623 (point))))))
625 (setq matches
626 (concat matches
627 (buffer-substring (point)(progn (end-of-line)(point)))
628 "\n")))))
629 (message "Parsing MH aliases...done")
630 (when mh-alias-local-users
631 (message "Making passwd aliases...")
632 (setq passwd-matches
633 (mapconcat
634 '(lambda (elem)
635 (if (or (string-match regexp (car elem))
636 (string-match regexp (cadr elem)))
637 (format "%s: %s\n" (car elem) (cadr elem))))
638 mh-alias-passwd-alist ""))
639 (message "Making passwd aliases...done")))
640 (if (and (string-equal "" matches)
641 (string-equal "" group-matches)
642 (string-equal "" passwd-matches))
643 (message "No matches")
644 (with-output-to-temp-buffer mh-aliases-buffer
645 (if (not (string-equal "" matches))
646 (princ matches))
647 (when (not (string-equal group-matches ""))
648 (princ "\nGroup Aliases:\n\n")
649 (princ group-matches))
650 (when (not (string-equal passwd-matches ""))
651 (princ "\nLocal User Aliases:\n\n")
652 (princ passwd-matches))))))
654 (defun mh-folder-line-matches-show-buffer-p ()
655 "Return t if the message under point in folder-mode is in the show buffer.
656 Return nil in any other circumstance (no message under point, no
657 show buffer, the message in the show buffer doesn't match."
658 (and (eq major-mode 'mh-folder-mode)
659 (mh-get-msg-num nil)
660 mh-show-buffer
661 (get-buffer mh-show-buffer)
662 (buffer-file-name (get-buffer mh-show-buffer))
663 (string-match ".*/\\([0-9]+\\)$"
664 (buffer-file-name (get-buffer mh-show-buffer)))
665 (string-equal
666 (match-string 1 (buffer-file-name (get-buffer mh-show-buffer)))
667 (int-to-string (mh-get-msg-num nil)))))
669 (provide 'mh-alias)
671 ;; Local Variables:
672 ;; indent-tabs-mode: nil
673 ;; sentence-end-double-space: nil
674 ;; End:
676 ;; arch-tag: 49879e46-5aa3-4569-bece-e5a58731d690
677 ;;; mh-alias.el ends here