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