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