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