Fix bug in handling GnuPG's TRUST_MARGINAL status
[emacs.git] / lisp / mh-e / mh-alias.el
blobd62ac671ea13db75d4d40e9cacdaaef980f54bc5
1 ;;; mh-alias.el --- MH-E mail alias completion and expansion
3 ;; Copyright (C) 1994-1997, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: Peter S. Galbraith <psg@debian.org>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
7 ;; Keywords: mail
8 ;; See: mh-e.el
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
27 ;;; Change Log:
29 ;;; Code:
31 (require 'mh-e)
33 (mh-require-cl)
35 (require 'goto-addr)
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))
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)
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 (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))
171 (defun mh-alias-reload ()
172 "Reload MH aliases.
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.
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"))
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)))
225 ;;; Alias Expansion
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))))
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)))
253 (mh-alias-ali alias))))
255 (eval-and-compile
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 alias 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 it 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* ((begin (mh-beginning-of-word))
303 (end (save-excursion
304 (goto-char begin)
305 (mh-beginning-of-word -1))))
306 (when (>= end (point))
307 (list
308 begin (if (fboundp 'completion-at-point) end (point))
309 (if (not mh-alias-expand-aliases-flag)
310 mh-alias-alist
311 (lambda (string pred action)
312 (case action
313 ((nil)
314 (let ((res (try-completion string mh-alias-alist pred)))
315 (if (or (eq res t)
316 (and (stringp res)
317 (eq t (try-completion res mh-alias-alist pred))))
318 (or (mh-alias-expand (if (stringp res) res string))
319 res)
320 res)))
321 ((t) (all-completions string mh-alias-alist pred))
322 ((lambda) (mh-test-completion string mh-alias-alist pred)))))))))
325 ;;; Alias File Updating
327 (defun mh-alias-suggest-alias (string &optional no-comma-swap)
328 "Suggest an alias for STRING.
329 Don't reverse the order of strings separated by a comma if
330 NO-COMMA-SWAP is non-nil."
331 (cond
332 ((string-match "^<\\(.*\\)>$" string)
333 ;; <somename@foo.bar> -> recurse, stripping brackets.
334 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
335 ((string-match "^\\sw+$" string)
336 ;; One word -> downcase it.
337 (downcase string))
338 ((string-match "^\\(\\sw+\\)\\s-+\\(\\sw+\\)$" string)
339 ;; Two words -> first.last
340 (downcase
341 (format "%s.%s" (match-string 1 string) (match-string 2 string))))
342 ((string-match "^\\([-a-zA-Z0-9._]+\\)@[-a-zA-z0-9_]+\\.+[a-zA-Z0-9]+$"
343 string)
344 ;; email only -> downcase username
345 (downcase (match-string 1 string)))
346 ((string-match "^\"\\(.*\\)\".*" string)
347 ;; "Some name" <somename@foo.bar> -> recurse -> "Some name"
348 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
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 (concat goto-address-mail-regexp " +(\\(.*\\))$") string)
353 ;; somename@foo.bar (Some name) -> recurse -> Some name
354 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
355 ((string-match "^\\(Dr\\|Prof\\)\\.? +\\(.*\\)" string)
356 ;; Strip out title
357 (mh-alias-suggest-alias (match-string 2 string) no-comma-swap))
358 ((string-match "^\\(.*\\), +\\(Jr\\.?\\|II+\\)$" string)
359 ;; Strip out tails with comma
360 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
361 ((string-match "^\\(.*\\) +\\(Jr\\.?\\|II+\\)$" string)
362 ;; Strip out tails
363 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
364 ((string-match "^\\(\\sw+\\) +[A-Z]\\.? +\\(.*\\)$" string)
365 ;; Strip out initials
366 (mh-alias-suggest-alias
367 (format "%s %s" (match-string 1 string) (match-string 2 string))
368 no-comma-swap))
369 ((and (not no-comma-swap)
370 (string-match "^\\([^,]+\\), +\\(.*\\)$" string))
371 ;; Reverse order of comma-separated fields to handle:
372 ;; From: "Galbraith, Peter" <psg@debian.org>
373 ;; but don't this for a name string extracted from the passwd file
374 ;; with mh-alias-passwd-gecos-comma-separator-flag set to nil.
375 (mh-alias-suggest-alias
376 (format "%s %s" (match-string 2 string) (match-string 1 string))
377 no-comma-swap))
379 ;; Output string, with spaces replaced by dots.
380 (mh-alias-canonicalize-suggestion string))))
382 (defun mh-alias-canonicalize-suggestion (string)
383 "Process STRING to replace spaces by periods.
384 First all spaces and commas are replaced by periods. Then every run of
385 consecutive periods are replaced with a single period. Finally the
386 string is converted to lower case."
387 (with-temp-buffer
388 (insert string)
389 ;; Replace spaces with periods
390 (goto-char (point-min))
391 (while (re-search-forward " +" nil t)
392 (replace-match "." nil nil))
393 ;; Replace commas with periods
394 (goto-char (point-min))
395 (while (re-search-forward ",+" nil t)
396 (replace-match "." nil nil))
397 ;; Replace consecutive periods with a single period
398 (goto-char (point-min))
399 (while (re-search-forward "\\.\\.+" nil t)
400 (replace-match "." nil nil))
401 ;; Convert to lower case
402 (downcase-region (point-min) (point-max))
403 ;; Whew! all done...
404 (buffer-string)))
406 (defun mh-alias-which-file-has-alias (alias file-list)
407 "Return the name of writable file which defines ALIAS from list FILE-LIST."
408 (with-current-buffer (get-buffer-create mh-temp-buffer)
409 (let ((the-list file-list)
410 (found))
411 (while the-list
412 (erase-buffer)
413 (when (file-writable-p (car file-list))
414 (insert-file-contents (car file-list))
415 (if (re-search-forward (concat "^" (regexp-quote alias) ":") nil t)
416 (setq found (car file-list)
417 the-list nil)
418 (setq the-list (cdr the-list)))))
419 found)))
421 (defun mh-alias-insert-file (&optional alias)
422 "Return filename which should be used to add ALIAS.
423 The value of the option `mh-alias-insert-file' is used if non-nil;
424 otherwise the value of the \"Aliasfile:\" profile component is used.
425 If the alias already exists, try to return the name of the file that
426 contains it."
427 (cond
428 ((and mh-alias-insert-file (listp mh-alias-insert-file))
429 (if (not (elt mh-alias-insert-file 1)) ; Only one entry, use it
430 (car mh-alias-insert-file)
431 (if (or (not alias)
432 (string-equal alias (mh-alias-ali alias))) ;alias doesn't exist
433 (completing-read "Alias file: "
434 (mapcar 'list mh-alias-insert-file) nil t)
435 (or (mh-alias-which-file-has-alias alias mh-alias-insert-file)
436 (completing-read "Alias file: "
437 (mapcar 'list mh-alias-insert-file) nil t)))))
438 ((and mh-alias-insert-file (stringp mh-alias-insert-file))
439 mh-alias-insert-file)
441 ;; writable ones returned from (mh-alias-filenames):
442 (let ((autolist (delq nil (mapcar (lambda (file)
443 (if (and (file-writable-p file)
444 (not (string-equal
445 file "/etc/passwd")))
446 file))
447 (mh-alias-filenames t)))))
448 (cond
449 ((not autolist)
450 (error "No writable alias file;
451 set `mh-alias-insert-file' or the \"Aliasfile:\" profile component"))
452 ((not (elt autolist 1)) ; Only one entry, use it
453 (car autolist))
454 ((or (not alias)
455 (string-equal alias (mh-alias-ali alias))) ;alias doesn't exist
456 (completing-read "Alias file: " (mapcar 'list autolist) nil t))
458 (or (mh-alias-which-file-has-alias alias autolist)
459 (completing-read "Alias file: "
460 (mapcar 'list autolist) nil t))))))))
462 ;;;###mh-autoload
463 (defun mh-alias-address-to-alias (address)
464 "Return the ADDRESS alias if defined, or nil."
465 (let* ((aliases (mh-alias-ali address t)))
466 (if (string-equal aliases address)
467 nil ; ali returned same string -> no.
468 ;; Double-check that we have an individual alias. This means that the
469 ;; alias doesn't expand into a list (of which this address is part).
470 (car (delq nil (mapcar
471 (function
472 (lambda (alias)
473 (let ((recurse (mh-alias-ali alias nil)))
474 (if (string-match ".*,.*" recurse)
476 alias))))
477 (split-string aliases ", +")))))))
479 ;;;###mh-autoload
480 (defun mh-alias-for-from-p ()
481 "Return t if sender's address has a corresponding alias."
482 (mh-alias-reload-maybe)
483 (save-excursion
484 (if (not (mh-folder-line-matches-show-buffer-p))
485 nil ;No corresponding show buffer
486 (if (eq major-mode 'mh-folder-mode)
487 (set-buffer mh-show-buffer))
488 (let ((from-header (mh-extract-from-header-value)))
489 (and from-header
490 (mh-alias-address-to-alias from-header))))))
492 (defun mh-alias-add-alias-to-file (alias address &optional file)
493 "Add ALIAS for ADDRESS in alias FILE without alias check or prompts.
494 Prompt for alias file if not provided and there is more than one
495 candidate.
497 If the alias exists already, you will have the choice of
498 inserting the new alias before or after the old alias. In the
499 former case, this alias will be used when sending mail to this
500 alias. In the latter case, the alias serves as an additional
501 folder name hint when filing messages."
502 (if (not file)
503 (setq file (mh-alias-insert-file alias)))
504 (with-current-buffer (find-file-noselect file)
505 (goto-char (point-min))
506 (let ((alias-search (concat alias ":"))
507 (letter)
508 (case-fold-search t))
509 (cond
510 ;; Search for exact match (if we had the same alias before)
511 ((re-search-forward
512 (concat "^" (regexp-quote alias-search) " *\\(.*\\)") nil t)
513 (let ((answer (read-string
514 (format (concat "Alias %s exists; insert new address "
515 "[b]efore or [a]fter: ")
516 (match-string 1))))
517 (case-fold-search t))
518 (cond ((string-match "^b" answer))
519 ((string-match "^a" answer)
520 (forward-line 1))
522 (error "Unrecognized response")))))
523 ;; No, so sort-in at the right place
524 ;; search for "^alias", then "^alia", etc.
525 ((eq mh-alias-insertion-location 'sorted)
526 (setq letter (substring alias-search -1)
527 alias-search (substring alias-search 0 -1))
528 (while (and (not (equal alias-search ""))
529 (not (re-search-forward
530 (concat "^" (regexp-quote alias-search)) nil t)))
531 (setq letter (substring alias-search -1)
532 alias-search (substring alias-search 0 -1)))
533 ;; Next, move forward to sort alphabetically for following letters
534 (beginning-of-line)
535 (while (re-search-forward
536 (concat "^" (regexp-quote alias-search) "[a-" letter "]")
537 nil t)
538 (forward-line 1)))
539 ((eq mh-alias-insertion-location 'bottom)
540 (goto-char (point-max)))
541 ((eq mh-alias-insertion-location 'top)
542 (goto-char (point-min)))))
543 (beginning-of-line)
544 (insert (format "%s: %s\n" alias address))
545 (save-buffer)))
547 (defun mh-alias-add-alias (alias address)
548 "Add ALIAS for ADDRESS in personal alias file.
550 This function prompts you for an alias and address. If the alias
551 exists already, you will have the choice of inserting the new
552 alias before or after the old alias. In the former case, this
553 alias will be used when sending mail to this alias. In the latter
554 case, the alias serves as an additional folder name hint when
555 filing messages."
556 (interactive "P\nP")
557 (mh-alias-reload-maybe)
558 (setq alias (completing-read "Alias: " mh-alias-alist nil nil alias))
559 (if (and address (string-match "^<\\(.*\\)>$" address))
560 (setq address (match-string 1 address)))
561 (setq address (read-string "Address: " address))
562 (if (string-match "^<\\(.*\\)>$" address)
563 (setq address (match-string 1 address)))
564 (let ((address-alias (mh-alias-address-to-alias address))
565 (alias-address (mh-alias-expand alias)))
566 (if (string-equal alias-address alias)
567 (setq alias-address nil))
568 (cond
569 ((and (equal alias address-alias)
570 (equal address alias-address))
571 (message "Already defined as %s" alias-address))
572 (address-alias
573 (if (y-or-n-p (format "Address has alias %s; set new one? "
574 address-alias))
575 (mh-alias-add-alias-to-file alias address)))
577 (mh-alias-add-alias-to-file alias address)))))
579 ;;;###mh-autoload
580 (defun mh-alias-grab-from-field ()
581 "Add alias for the sender of the current message."
582 (interactive)
583 (mh-alias-reload-maybe)
584 (save-excursion
585 (cond
586 ((mh-folder-line-matches-show-buffer-p)
587 (set-buffer mh-show-buffer))
588 ((and (eq major-mode 'mh-folder-mode)
589 (mh-get-msg-num nil))
590 (set-buffer (get-buffer-create mh-temp-buffer))
591 (insert-file-contents (mh-msg-filename (mh-get-msg-num t))))
592 ((eq major-mode 'mh-folder-mode)
593 (user-error "Cursor not pointing to a message")))
594 (let* ((address (or (mh-extract-from-header-value)
595 (error "Message has no From: header")))
596 (alias (mh-alias-suggest-alias address)))
597 (mh-alias-add-alias alias address))))
599 (defun mh-alias-add-address-under-point ()
600 "Insert an alias for address under point."
601 (interactive)
602 (let ((address (goto-address-find-address-at-point)))
603 (if address
604 (mh-alias-add-alias nil address)
605 (message "No email address found under point"))))
607 (defun mh-alias-apropos (regexp)
608 "Show all aliases or addresses that match a regular expression REGEXP."
609 (interactive "sAlias regexp: ")
610 (if mh-alias-local-users
611 (mh-alias-reload-maybe))
612 (let ((matches "")
613 (group-matches "")
614 (passwd-matches))
615 (save-excursion
616 (message "Reading MH aliases...")
617 (mh-exec-cmd-quiet t "ali" "-nolist" "-nouser")
618 (message "Parsing MH aliases...")
619 (while (re-search-forward regexp nil t)
620 (beginning-of-line)
621 (cond
622 ((looking-at "^[ \t]") ;Continuation line
623 (setq group-matches
624 (concat group-matches
625 (buffer-substring
626 (save-excursion
627 (or (re-search-backward "^[^ \t]" nil t)
628 (point)))
629 (progn
630 (if (re-search-forward "^[^ \t]" nil t)
631 (forward-char -1))
632 (point))))))
634 (setq matches
635 (concat matches
636 (buffer-substring (point)(progn (end-of-line)(point)))
637 "\n")))))
638 (message "Parsing MH aliases...done")
639 (when mh-alias-local-users
640 (message "Making passwd aliases...")
641 (setq passwd-matches
642 (mapconcat
643 (lambda (elem)
644 (if (or (string-match regexp (car elem))
645 (string-match regexp (cadr elem)))
646 (format "%s: %s\n" (car elem) (cadr elem))))
647 mh-alias-passwd-alist ""))
648 (message "Making passwd aliases...done")))
649 (if (and (string-equal "" matches)
650 (string-equal "" group-matches)
651 (string-equal "" passwd-matches))
652 (message "No matches")
653 (with-output-to-temp-buffer mh-aliases-buffer
654 (if (not (string-equal "" matches))
655 (princ matches))
656 (when (not (string-equal group-matches ""))
657 (princ "\nGroup Aliases:\n\n")
658 (princ group-matches))
659 (when (not (string-equal passwd-matches ""))
660 (princ "\nLocal User Aliases:\n\n")
661 (princ passwd-matches))))))
663 (defun mh-folder-line-matches-show-buffer-p ()
664 "Return t if the message under point in folder-mode is in the show buffer.
665 Return nil in any other circumstance (no message under point, no
666 show buffer, the message in the show buffer doesn't match."
667 (and (eq major-mode 'mh-folder-mode)
668 (mh-get-msg-num nil)
669 mh-show-buffer
670 (get-buffer mh-show-buffer)
671 (buffer-file-name (get-buffer mh-show-buffer))
672 (string-match ".*/\\([0-9]+\\)$"
673 (buffer-file-name (get-buffer mh-show-buffer)))
674 (string-equal
675 (match-string 1 (buffer-file-name (get-buffer mh-show-buffer)))
676 (int-to-string (mh-get-msg-num nil)))))
678 (provide 'mh-alias)
680 ;; Local Variables:
681 ;; indent-tabs-mode: nil
682 ;; sentence-end-double-space: nil
683 ;; End:
685 ;;; mh-alias.el ends here