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