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