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