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