Merge from emacs--rel--22
[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, 2007, 2008 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 3 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;;; Change Log:
29
30 ;;; Code:
31
32 (require 'mh-e)
33
34 (mh-require-cl)
35
36 (require 'goto-addr)
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
47 (let ((map (copy-keymap minibuffer-local-completion-map)))
48 (define-key map "," 'mh-alias-minibuffer-confirm-address)
49 (define-key map " " 'self-insert-command)
50 map))
51
52 (defvar mh-alias-system-aliases
53 '("/etc/nmh/MailAliases" "/etc/mh/MailAliases"
54 "/usr/lib/mh/MailAliases" "/usr/share/mailutils/mh/MailAliases"
55 "/etc/passwd")
56 "*A list of system files which are a source of aliases.
57 If these files are modified, they are automatically reread. This list
58 need include only system aliases and the passwd file, since personal
59 alias files listed in your \"Aliasfile:\" MH profile component are
60 automatically included. You can update the alias list manually using
61 \\[mh-alias-reload].")
62
63 \f
64
65 ;;; Alias Loading
66
67 (defun mh-alias-tstamp (arg)
68 "Check whether alias files have been modified.
69 Return t if any file listed in the Aliasfile MH profile component has
70 been modified since the timestamp.
71 If ARG is non-nil, set timestamp with the current time."
72 (if arg
73 (let ((time (current-time)))
74 (setq mh-alias-tstamp (list (nth 0 time) (nth 1 time))))
75 (let ((stamp))
76 (car (memq t (mapcar
77 (function
78 (lambda (file)
79 (when (and file (file-exists-p file))
80 (setq stamp (nth 5 (file-attributes file)))
81 (or (> (car stamp) (car mh-alias-tstamp))
82 (and (= (car stamp) (car mh-alias-tstamp))
83 (> (cadr stamp) (cadr mh-alias-tstamp)))))))
84 (mh-alias-filenames t)))))))
85
86 (defun mh-alias-filenames (arg)
87 "Return list of filenames that contain aliases.
88 The filenames come from the Aliasfile profile component and are
89 expanded.
90 If ARG is non-nil, filenames listed in `mh-alias-system-aliases' are
91 appended."
92 (or mh-progs (mh-find-path))
93 (save-excursion
94 (let* ((filename (mh-profile-component "Aliasfile"))
95 (filelist (and filename (split-string filename "[ \t]+")))
96 (userlist
97 (mapcar
98 (function
99 (lambda (file)
100 (if (and mh-user-path file
101 (file-exists-p (expand-file-name file mh-user-path)))
102 (expand-file-name file mh-user-path))))
103 filelist)))
104 (if arg
105 (if (stringp mh-alias-system-aliases)
106 (append userlist (list mh-alias-system-aliases))
107 (append userlist mh-alias-system-aliases))
108 userlist))))
109
110 (defun mh-alias-gecos-name (gecos-name username comma-separator)
111 "Return a usable address string from a GECOS-NAME and USERNAME.
112 Use only part of the GECOS-NAME up to the first comma if
113 COMMA-SEPARATOR is non-nil."
114 (let ((res gecos-name))
115 ;; Keep only string until first comma if COMMA-SEPARATOR is t.
116 (if (and comma-separator
117 (string-match "^\\([^,]+\\)," res))
118 (setq res (match-string 1 res)))
119 ;; Replace "&" with capitalized username
120 (if (string-match "&" res)
121 (setq res (mh-replace-regexp-in-string "&" (capitalize username) res)))
122 ;; Remove " character
123 (if (string-match "\"" res)
124 (setq res (mh-replace-regexp-in-string "\"" "" res)))
125 ;; If empty string, use username instead
126 (if (string-equal "" res)
127 (setq res username))
128 ;; Surround by quotes if doesn't consist of simple characters
129 (if (not (string-match "^[ a-zA-Z0-9-]+$" res))
130 (setq res (concat "\"" res "\"")))
131 res))
132
133 (defun mh-alias-local-users ()
134 "Return an alist of local users from /etc/passwd.
135 Exclude all aliases already in `mh-alias-alist' from \"ali\""
136 (let (passwd-alist)
137 (save-excursion
138 (set-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)
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-backward-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 aliast 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 if 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* ((end (point))
302 (begin (mh-beginning-of-word))
303 (input (buffer-substring-no-properties begin end)))
304 (mh-complete-word input mh-alias-alist begin end)
305 (when mh-alias-expand-aliases-flag
306 (let* ((end (point))
307 (expansion (mh-alias-expand (buffer-substring begin end))))
308 (delete-region begin end)
309 (insert expansion)))))
310
311 \f
312
313 ;;; Alias File Updating
314
315 (defun mh-alias-suggest-alias (string &optional no-comma-swap)
316 "Suggest an alias for STRING.
317 Don't reverse the order of strings separated by a comma if
318 NO-COMMA-SWAP is non-nil."
319 (cond
320 ((string-match "^<\\(.*\\)>$" string)
321 ;; <somename@foo.bar> -> recurse, stripping brackets.
322 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
323 ((string-match "^\\sw+$" string)
324 ;; One word -> downcase it.
325 (downcase string))
326 ((string-match "^\\(\\sw+\\)\\s-+\\(\\sw+\\)$" string)
327 ;; Two words -> first.last
328 (downcase
329 (format "%s.%s" (match-string 1 string) (match-string 2 string))))
330 ((string-match "^\\([-a-zA-Z0-9._]+\\)@[-a-zA-z0-9_]+\\.+[a-zA-Z0-9]+$"
331 string)
332 ;; email only -> downcase username
333 (downcase (match-string 1 string)))
334 ((string-match "^\"\\(.*\\)\".*" string)
335 ;; "Some name" <somename@foo.bar> -> recurse -> "Some name"
336 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
337 ((string-match "^\\(.*\\) +<.*>$" string)
338 ;; Some name <somename@foo.bar> -> recurse -> Some name
339 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
340 ((string-match (concat goto-address-mail-regexp " +(\\(.*\\))$") string)
341 ;; somename@foo.bar (Some name) -> recurse -> Some name
342 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
343 ((string-match "^\\(Dr\\|Prof\\)\\.? +\\(.*\\)" string)
344 ;; Strip out title
345 (mh-alias-suggest-alias (match-string 2 string) no-comma-swap))
346 ((string-match "^\\(.*\\), +\\(Jr\\.?\\|II+\\)$" string)
347 ;; Strip out tails with comma
348 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
349 ((string-match "^\\(.*\\) +\\(Jr\\.?\\|II+\\)$" string)
350 ;; Strip out tails
351 (mh-alias-suggest-alias (match-string 1 string) no-comma-swap))
352 ((string-match "^\\(\\sw+\\) +[A-Z]\\.? +\\(.*\\)$" string)
353 ;; Strip out initials
354 (mh-alias-suggest-alias
355 (format "%s %s" (match-string 1 string) (match-string 2 string))
356 no-comma-swap))
357 ((and (not no-comma-swap)
358 (string-match "^\\([^,]+\\), +\\(.*\\)$" string))
359 ;; Reverse order of comma-separated fields to handle:
360 ;; From: "Galbraith, Peter" <psg@debian.org>
361 ;; but don't this for a name string extracted from the passwd file
362 ;; with mh-alias-passwd-gecos-comma-separator-flag set to nil.
363 (mh-alias-suggest-alias
364 (format "%s %s" (match-string 2 string) (match-string 1 string))
365 no-comma-swap))
366 (t
367 ;; Output string, with spaces replaced by dots.
368 (mh-alias-canonicalize-suggestion string))))
369
370 (defun mh-alias-canonicalize-suggestion (string)
371 "Process STRING to replace spaces by periods.
372 First all spaces and commas are replaced by periods. Then every run of
373 consecutive periods are replaced with a single period. Finally the
374 string is converted to lower case."
375 (with-temp-buffer
376 (insert string)
377 ;; Replace spaces with periods
378 (goto-char (point-min))
379 (while (re-search-forward " +" nil t)
380 (replace-match "." nil nil))
381 ;; Replace commas with periods
382 (goto-char (point-min))
383 (while (re-search-forward ",+" nil t)
384 (replace-match "." nil nil))
385 ;; Replace consecutive periods with a single period
386 (goto-char (point-min))
387 (while (re-search-forward "\\.\\.+" nil t)
388 (replace-match "." nil nil))
389 ;; Convert to lower case
390 (downcase-region (point-min) (point-max))
391 ;; Whew! all done...
392 (buffer-string)))
393
394 (defun mh-alias-which-file-has-alias (alias file-list)
395 "Return the name of writable file which defines ALIAS from list FILE-LIST."
396 (save-excursion
397 (set-buffer (get-buffer-create mh-temp-buffer))
398 (let ((the-list file-list)
399 (found))
400 (while the-list
401 (erase-buffer)
402 (when (file-writable-p (car file-list))
403 (insert-file-contents (car file-list))
404 (if (re-search-forward (concat "^" (regexp-quote alias) ":") nil t)
405 (setq found (car file-list)
406 the-list nil)
407 (setq the-list (cdr the-list)))))
408 found)))
409
410 (defun mh-alias-insert-file (&optional alias)
411 "Return filename which should be used to add ALIAS.
412 The value of the option `mh-alias-insert-file' is used if non-nil\;
413 otherwise the value of the \"Aliasfile:\" profile component is used.
414 If the alias already exists, try to return the name of the file that
415 contains it."
416 (cond
417 ((and mh-alias-insert-file (listp mh-alias-insert-file))
418 (if (not (elt mh-alias-insert-file 1)) ; Only one entry, use it
419 (car mh-alias-insert-file)
420 (if (or (not alias)
421 (string-equal alias (mh-alias-ali alias))) ;alias doesn't exist
422 (completing-read "Alias file: "
423 (mapcar 'list mh-alias-insert-file) nil t)
424 (or (mh-alias-which-file-has-alias alias mh-alias-insert-file)
425 (completing-read "Alias file: "
426 (mapcar 'list mh-alias-insert-file) nil t)))))
427 ((and mh-alias-insert-file (stringp mh-alias-insert-file))
428 mh-alias-insert-file)
429 (t
430 ;; writable ones returned from (mh-alias-filenames):
431 (let ((autolist (delq nil (mapcar (lambda (file)
432 (if (and (file-writable-p file)
433 (not (string-equal
434 file "/etc/passwd")))
435 file))
436 (mh-alias-filenames t)))))
437 (cond
438 ((not autolist)
439 (error "No writable alias file;
440 set `mh-alias-insert-file' or the \"Aliasfile:\" profile component"))
441 ((not (elt autolist 1)) ; Only one entry, use it
442 (car autolist))
443 ((or (not alias)
444 (string-equal alias (mh-alias-ali alias))) ;alias doesn't exist
445 (completing-read "Alias file: " (mapcar 'list autolist) nil t))
446 (t
447 (or (mh-alias-which-file-has-alias alias autolist)
448 (completing-read "Alias file: "
449 (mapcar 'list autolist) nil t))))))))
450
451 ;;;###mh-autoload
452 (defun mh-alias-address-to-alias (address)
453 "Return the ADDRESS alias if defined, or nil."
454 (let* ((aliases (mh-alias-ali address t)))
455 (if (string-equal aliases address)
456 nil ; ali returned same string -> no.
457 ;; Double-check that we have an individual alias. This means that the
458 ;; alias doesn't expand into a list (of which this address is part).
459 (car (delq nil (mapcar
460 (function
461 (lambda (alias)
462 (let ((recurse (mh-alias-ali alias nil)))
463 (if (string-match ".*,.*" recurse)
464 nil
465 alias))))
466 (split-string aliases ", +")))))))
467
468 ;;;###mh-autoload
469 (defun mh-alias-for-from-p ()
470 "Return t if sender's address has a corresponding alias."
471 (mh-alias-reload-maybe)
472 (save-excursion
473 (if (not (mh-folder-line-matches-show-buffer-p))
474 nil ;No corresponding show buffer
475 (if (eq major-mode 'mh-folder-mode)
476 (set-buffer mh-show-buffer))
477 (let ((from-header (mh-extract-from-header-value)))
478 (and from-header
479 (mh-alias-address-to-alias from-header))))))
480
481 (defun mh-alias-add-alias-to-file (alias address &optional file)
482 "Add ALIAS for ADDRESS in alias FILE without alias check or prompts.
483 Prompt for alias file if not provided and there is more than one
484 candidate.
485
486 If the alias exists already, you will have the choice of
487 inserting the new alias before or after the old alias. In the
488 former case, this alias will be used when sending mail to this
489 alias. In the latter case, the alias serves as an additional
490 folder name hint when filing messages."
491 (if (not file)
492 (setq file (mh-alias-insert-file alias)))
493 (save-excursion
494 (set-buffer (find-file-noselect file))
495 (goto-char (point-min))
496 (let ((alias-search (concat alias ":"))
497 (letter)
498 (case-fold-search t))
499 (cond
500 ;; Search for exact match (if we had the same alias before)
501 ((re-search-forward
502 (concat "^" (regexp-quote alias-search) " *\\(.*\\)") nil t)
503 (let ((answer (read-string
504 (format (concat "Alias %s exists; insert new address "
505 "[b]efore or [a]fter: ")
506 (match-string 1))))
507 (case-fold-search t))
508 (cond ((string-match "^b" answer))
509 ((string-match "^a" answer)
510 (forward-line 1))
511 (t
512 (error "Unrecognized response")))))
513 ;; No, so sort-in at the right place
514 ;; search for "^alias", then "^alia", etc.
515 ((eq mh-alias-insertion-location 'sorted)
516 (setq letter (substring alias-search -1)
517 alias-search (substring alias-search 0 -1))
518 (while (and (not (equal alias-search ""))
519 (not (re-search-forward
520 (concat "^" (regexp-quote alias-search)) nil t)))
521 (setq letter (substring alias-search -1)
522 alias-search (substring alias-search 0 -1)))
523 ;; Next, move forward to sort alphabetically for following letters
524 (beginning-of-line)
525 (while (re-search-forward
526 (concat "^" (regexp-quote alias-search) "[a-" letter "]")
527 nil t)
528 (forward-line 1)))
529 ((eq mh-alias-insertion-location 'bottom)
530 (goto-char (point-max)))
531 ((eq mh-alias-insertion-location 'top)
532 (goto-char (point-min)))))
533 (beginning-of-line)
534 (insert (format "%s: %s\n" alias address))
535 (save-buffer)))
536
537 (defun mh-alias-add-alias (alias address)
538 "Add ALIAS for ADDRESS in personal alias file.
539
540 This function prompts you for an alias and address. If the alias
541 exists already, you will have the choice of inserting the new
542 alias before or after the old alias. In the former case, this
543 alias will be used when sending mail to this alias. In the latter
544 case, the alias serves as an additional folder name hint when
545 filing messages."
546 (interactive "P\nP")
547 (mh-alias-reload-maybe)
548 (setq alias (completing-read "Alias: " mh-alias-alist nil nil alias))
549 (if (and address (string-match "^<\\(.*\\)>$" address))
550 (setq address (match-string 1 address)))
551 (setq address (read-string "Address: " address))
552 (if (string-match "^<\\(.*\\)>$" address)
553 (setq address (match-string 1 address)))
554 (let ((address-alias (mh-alias-address-to-alias address))
555 (alias-address (mh-alias-expand alias)))
556 (if (string-equal alias-address alias)
557 (setq alias-address nil))
558 (cond
559 ((and (equal alias address-alias)
560 (equal address alias-address))
561 (message "Already defined as %s" alias-address))
562 (address-alias
563 (if (y-or-n-p (format "Address has alias %s; set new one? "
564 address-alias))
565 (mh-alias-add-alias-to-file alias address)))
566 (t
567 (mh-alias-add-alias-to-file alias address)))))
568
569 ;;;###mh-autoload
570 (defun mh-alias-grab-from-field ()
571 "Add alias for the sender of the current message."
572 (interactive)
573 (mh-alias-reload-maybe)
574 (save-excursion
575 (cond
576 ((mh-folder-line-matches-show-buffer-p)
577 (set-buffer mh-show-buffer))
578 ((and (eq major-mode 'mh-folder-mode)
579 (mh-get-msg-num nil))
580 (set-buffer (get-buffer-create mh-temp-buffer))
581 (insert-file-contents (mh-msg-filename (mh-get-msg-num t))))
582 ((eq major-mode 'mh-folder-mode)
583 (error "Cursor not pointing to a message")))
584 (let* ((address (or (mh-extract-from-header-value)
585 (error "Message has no From: header")))
586 (alias (mh-alias-suggest-alias address)))
587 (mh-alias-add-alias alias address))))
588
589 (defun mh-alias-add-address-under-point ()
590 "Insert an alias for address under point."
591 (interactive)
592 (let ((address (goto-address-find-address-at-point)))
593 (if address
594 (mh-alias-add-alias nil address)
595 (message "No email address found under point"))))
596
597 (defun mh-alias-apropos (regexp)
598 "Show all aliases or addresses that match a regular expression REGEXP."
599 (interactive "sAlias regexp: ")
600 (if mh-alias-local-users
601 (mh-alias-reload-maybe))
602 (let ((matches "")
603 (group-matches "")
604 (passwd-matches))
605 (save-excursion
606 (message "Reading MH aliases...")
607 (mh-exec-cmd-quiet t "ali" "-nolist" "-nouser")
608 (message "Parsing MH aliases...")
609 (while (re-search-forward regexp nil t)
610 (beginning-of-line)
611 (cond
612 ((looking-at "^[ \t]") ;Continuation line
613 (setq group-matches
614 (concat group-matches
615 (buffer-substring
616 (save-excursion
617 (or (re-search-backward "^[^ \t]" nil t)
618 (point)))
619 (progn
620 (if (re-search-forward "^[^ \t]" nil t)
621 (forward-char -1))
622 (point))))))
623 (t
624 (setq matches
625 (concat matches
626 (buffer-substring (point)(progn (end-of-line)(point)))
627 "\n")))))
628 (message "Parsing MH aliases...done")
629 (when mh-alias-local-users
630 (message "Making passwd aliases...")
631 (setq passwd-matches
632 (mapconcat
633 '(lambda (elem)
634 (if (or (string-match regexp (car elem))
635 (string-match regexp (cadr elem)))
636 (format "%s: %s\n" (car elem) (cadr elem))))
637 mh-alias-passwd-alist ""))
638 (message "Making passwd aliases...done")))
639 (if (and (string-equal "" matches)
640 (string-equal "" group-matches)
641 (string-equal "" passwd-matches))
642 (message "No matches")
643 (with-output-to-temp-buffer mh-aliases-buffer
644 (if (not (string-equal "" matches))
645 (princ matches))
646 (when (not (string-equal group-matches ""))
647 (princ "\nGroup Aliases:\n\n")
648 (princ group-matches))
649 (when (not (string-equal passwd-matches ""))
650 (princ "\nLocal User Aliases:\n\n")
651 (princ passwd-matches))))))
652
653 (defun mh-folder-line-matches-show-buffer-p ()
654 "Return t if the message under point in folder-mode is in the show buffer.
655 Return nil in any other circumstance (no message under point, no
656 show buffer, the message in the show buffer doesn't match."
657 (and (eq major-mode 'mh-folder-mode)
658 (mh-get-msg-num nil)
659 mh-show-buffer
660 (get-buffer mh-show-buffer)
661 (buffer-file-name (get-buffer mh-show-buffer))
662 (string-match ".*/\\([0-9]+\\)$"
663 (buffer-file-name (get-buffer mh-show-buffer)))
664 (string-equal
665 (match-string 1 (buffer-file-name (get-buffer mh-show-buffer)))
666 (int-to-string (mh-get-msg-num nil)))))
667
668 (provide 'mh-alias)
669
670 ;; Local Variables:
671 ;; indent-tabs-mode: nil
672 ;; sentence-end-double-space: nil
673 ;; End:
674
675 ;; arch-tag: 49879e46-5aa3-4569-bece-e5a58731d690
676 ;;; mh-alias.el ends here