1 ;;; mailalias.el --- expand and complete mailing address aliases
3 ;; Copyright (C) 1985, 1987, 1995, 1996, 1997 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Basic functions for defining and expanding mail aliases.
28 ;; These seal off the interface to the alias-definition parts of a
29 ;; .mailrc file formatted for BSD's Mail or USL's mailx.
35 (defgroup mailalias nil
36 "Expanding mail aliases"
39 (defcustom mail-passwd-files
'("/etc/passwd")
40 "*List of files from which to determine valid user names."
41 :type
'(repeat string
)
44 (defcustom mail-passwd-command nil
45 "*Shell command to retrieve text to add to `/etc/passwd', or nil."
46 :type
'(choice string
(const nil
))
49 (defvar mail-directory-names t
50 "Alist of mail address directory entries.
51 When t this still needs to be initialized.")
53 (defvar mail-address-field-regexp
54 "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):")
56 (defcustom mail-complete-alist
57 ;; Don't use backquote here; we don't want backquote to get loaded
58 ;; just because of loading this file.
59 (cons (cons mail-address-field-regexp
'(mail-get-names pattern
))
60 '(("Newsgroups:" .
(if (boundp 'gnus-active-hashtb
)
62 (if (boundp news-group-article-assoc
)
63 news-group-article-assoc
)))
64 ("Followup-To:" .
(mail-sentto-newsgroups))
65 ;;("Distribution:" ???)
67 "*Alist of header field and expression to return alist for completion.
68 The expression may reference the variable `pattern'
69 which will hold the string being completed.
70 If not on matching header, `mail-complete-function' gets called instead."
73 (put 'mail-complete-alist
'risky-local-variable t
)
76 (defcustom mail-complete-style
'angles
77 "*Specifies how \\[mail-complete] formats the full name when it completes.
78 If `nil', they contain just the return address like:
80 If `parens', they look like:
81 king@grassland.com (Elvis Parsley)
82 If `angles', they look like:
83 Elvis Parsley <king@grassland.com>"
84 :type
'(choice (const angles
) (const parens
) (const nil
))
87 (defcustom mail-complete-function
'ispell-complete-word
88 "*Function to call when completing outside `mail-complete-alist'-header."
89 :type
'(choice function
(const nil
))
92 (defcustom mail-directory-function nil
93 "*Function to get completions from directory service or `nil' for none.
94 See `mail-directory-requery'."
95 :type
'(choice function
(const nil
))
98 ;; This is for when the directory is huge, or changes frequently.
99 (defcustom mail-directory-requery nil
100 "*When non-`nil' call `mail-directory-function' for each completion.
101 In that case, one argument gets passed to the function, the partial string
106 (defcustom mail-directory-process nil
107 "*Shell command to get the list of names from a mail directory.
108 This value is used when the value of `mail-directory-function'
109 is `mail-directory-process'. The value should be a list
110 of the form (COMMAND ARG ...), where each of the list elements
111 is evaluated. When `mail-directory-requery' is non-nil, during
112 evaluation of these elements, the variable `pattern' contains
113 the partial input being completed.
115 The value might look like this:
117 '(remote-shell-program \"HOST\" \"-nl\" \"USER\" \"COMMAND\")
121 '(remote-shell-program \"HOST\" \"-n\" \"COMMAND '^\" pattern \"'\")"
124 (put 'mail-directory-process
'risky-local-variable t
)
126 (defcustom mail-directory-stream nil
127 "*List of (HOST SERVICE) for stream connection to mail directory."
130 (put 'mail-directory-stream
'risky-local-variable t
)
132 (defcustom mail-directory-parser nil
133 "*How to interpret the output of `mail-directory-function'.
134 Three types of values are possible:
136 - nil means to gather each line as one name
137 - regexp means first \\(grouping\\) in successive matches is name
138 - function called at beginning of buffer that returns an alist of names"
139 :type
'(choice (const nil
) regexp function
)
141 (put 'mail-directory-parser
'risky-local-variable t
)
143 ;; Internal variables.
146 "Alist of local users, aliases and directory entries as available.
147 Elements have the form (MAILNAME) or (MAILNAME . FULLNAME).
148 If the value means t, it means the real value should be calculated
149 for the next use. this is used in `mail-complete'.")
151 (defvar mail-local-names t
152 "Alist of local users.
153 When t this still needs to be initialized.")
156 ;; Called from sendmail-send-it, or similar functions,
157 ;; only if some mail aliases are defined.
159 (defun expand-mail-aliases (beg end
&optional exclude
)
160 "Expand all mail aliases in suitable header fields found between BEG and END.
161 If interactive, expand in header fields before `mail-header-separator'.
162 Suitable header fields are `To', `From', `CC' and `BCC', `Reply-to', and
163 their `Resent-' variants.
165 Optional second arg EXCLUDE may be a regular expression defining text to be
166 removed from alias expansions."
169 (list (goto-char (point-min))
170 (search-forward (concat "\n" mail-header-separator
"\n")))))
171 (sendmail-sync-aliases)
172 (when (eq mail-aliases t
)
173 (setq mail-aliases nil
)
174 (build-mail-aliases))
177 (setq end
(set-marker (make-marker) end
))
178 (let ((case-fold-search nil
))
179 (while (let ((case-fold-search t
))
180 (re-search-forward mail-address-field-regexp end t
))
181 (skip-chars-forward " \t")
184 ;; DISABLED-ALIASES records aliases temporarily disabled
185 ;; while we scan text that resulted from expanding those aliases.
186 ;; Each element is (ALIAS . TILL-WHEN), where TILL-WHEN
187 ;; is where to reenable the alias (expressed as number of chars
188 ;; counting from END1).
189 (disabled-aliases nil
))
190 (re-search-forward "^[^ \t]" end
'move
)
192 (skip-chars-backward " \t\n")
193 (setq end1
(point-marker))
195 (while (< (point) end1
)
197 ;; Reenable any aliases which were disabled for ranges
198 ;; that we have passed out of.
199 (while (and disabled-aliases
200 (> pos
(- end1
(cdr (car disabled-aliases
)))))
201 (setq disabled-aliases
(cdr disabled-aliases
)))
202 ;; EPOS gets position of end of next name;
203 ;; SEPLEN gets length of whitespace&separator that follows it.
204 (if (re-search-forward "[ \t]*[\n,][ \t]*" end1 t
)
205 (setq epos
(match-beginning 0)
206 seplen
(- (point) epos
))
207 (setq epos
(marker-position end1
) seplen
0))
208 (let ((string (buffer-substring-no-properties pos epos
))
210 (if (and (not (assoc string disabled-aliases
))
211 (setq translation
(cdr (assoc string mail-aliases
))))
213 ;; This name is an alias. Disable it.
214 (setq disabled-aliases
(cons (cons string
(- end1 epos
))
216 ;; Replace the alias with its expansion
217 ;; then rescan the expansion for more aliases.
221 (let ((regexp (concat "\\b\\(" exclude
"\\)\\b"))
222 (end (point-marker)))
224 (while (re-search-forward regexp end t
)
227 (delete-region (point) (+ (point) (- epos pos
)))
229 ;; Name is not an alias. Skip to start of next name.
231 (forward-char seplen
))))
232 (set-marker end1 nil
)))
233 (set-marker end nil
))))
235 ;; Called by mail-setup, or similar functions, only if the file specified
236 ;; by mail-personal-alias-file (usually `~/.mailrc') exists.
237 (defun build-mail-aliases (&optional file
)
238 "Read mail aliases from personal aliases file and set `mail-aliases'.
239 By default, this is the file specified by `mail-personal-alias-file'."
240 (setq file
(expand-file-name (or file mail-personal-alias-file
)))
241 ;; In case mail-aliases is t, make sure define-mail-alias
242 ;; does not recursively call build-mail-aliases.
243 (setq mail-aliases nil
)
245 (obuf (current-buffer)))
248 (setq buffer
(generate-new-buffer " mailrc"))
251 (cond ((get-file-buffer file
)
252 (insert (save-excursion
253 (set-buffer (get-file-buffer file
))
254 (buffer-substring-no-properties
255 (point-min) (point-max)))))
256 ((file-exists-p file
) (insert-file-contents file
))
257 ((file-exists-p (setq file
(concat "~/" file
)))
258 (insert-file-contents file
))
260 ;; Don't lose if no final newline.
261 (goto-char (point-max))
262 (or (eq (preceding-char) ?
\n) (newline))
263 (goto-char (point-min))
264 ;; handle "\\\n" continuation lines
267 (if (= (preceding-char) ?
\\)
268 (progn (delete-char -
1) (delete-char 1) (insert ?\
))
270 (goto-char (point-min))
271 ;; handle `source' directives -- Eddy/1994/May/25
272 (cond ((re-search-forward "^source[ \t]+" nil t
)
273 (re-search-forward "\\S-+")
274 (setq file
(buffer-substring-no-properties
275 (match-beginning 0) (match-end 0)))
277 (insert "# ") ; to ensure we don't re-process this file
279 (t (setq file nil
))))
280 (goto-char (point-min))
281 (while (re-search-forward
282 "^\\(a\\|alias\\|g\\|group\\)[ \t]+\\([^ \t]+\\)" nil t
)
283 (let* ((name (match-string 2))
284 (start (progn (skip-chars-forward " \t") (point))))
288 (buffer-substring-no-properties start
(point))
291 (if buffer
(kill-buffer buffer
))
294 ;; Always autoloadable in case the user wants to define aliases
295 ;; interactively or in .emacs.
297 (defun define-mail-alias (name definition
&optional from-mailrc-file
)
298 "Define NAME as a mail alias that translates to DEFINITION.
299 This means that sending a message to NAME will actually send to DEFINITION.
301 Normally, the addresses in DEFINITION must be separated by commas.
302 If FROM-MAILRC-FILE is non-nil, then addresses in DEFINITION
303 can be separated by spaces; an address can contain spaces
304 if it is quoted with double-quotes."
306 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
307 ;; Read the defaults first, if we have not done so.
308 ;; But not if we are doing that already right now.
309 (unless from-mailrc-file
310 (sendmail-sync-aliases))
311 (if (eq mail-aliases t
)
313 (setq mail-aliases nil
)
314 (if (file-exists-p mail-personal-alias-file
)
315 (build-mail-aliases))))
316 ;; strip garbage from front and end
317 (if (string-match "\\`[ \t\n,]+" definition
)
318 (setq definition
(substring definition
(match-end 0))))
319 (if (string-match "[ \t\n,]+\\'" definition
)
320 (setq definition
(substring definition
0 (match-beginning 0))))
322 ;; If DEFINITION is null string, avoid looping even once.
323 (start (and (not (equal definition
"")) 0))
324 (L (length definition
))
328 (setq convert-backslash nil
)
329 ;; If we're reading from the mailrc file, then addresses are delimited
330 ;; by spaces, and addresses with embedded spaces must be surrounded by
331 ;; double-quotes. Otherwise, addresses are separated by commas.
333 (if (eq ?
\" (aref definition start
))
334 (progn (string-match "[^\\]\\(\\([\\][\\]\\)*\\)\"[ \t,]*" definition start
)
335 (setq start
(1+ start
)
337 convert-backslash t
))
338 (setq end
(string-match "[ \t,]+" definition start
)))
339 (setq end
(string-match "[ \t\n,]*,[ \t\n,]*" definition start
)))
340 (let ((temp (substring definition start end
))
345 (if convert-backslash
346 (while (string-match "[\\]" temp pos
)
347 (setq temp
(replace-match "" t t temp
))
349 (setq start
(1- start
)))
350 (setq pos
(match-end 0))))
351 (setq result
(cons temp result
))))
352 (setq definition
(mapconcat (function identity
)
355 (setq tem
(assoc name mail-aliases
))
357 (rplacd tem definition
)
358 (setq mail-aliases
(cons (cons name definition
) mail-aliases
)
362 (defun mail-complete (arg)
363 "Perform completion on header field or word preceding point.
364 Completable headers are according to `mail-complete-alist'. If none matches
365 current header, calls `mail-complete-function' and passes prefix arg if any."
367 ;; Read the defaults first, if we have not done so.
368 (sendmail-sync-aliases)
369 (if (eq mail-aliases t
)
371 (setq mail-aliases nil
)
372 (if (file-exists-p mail-personal-alias-file
)
373 (build-mail-aliases))))
374 (let ((list mail-complete-alist
))
375 (if (and (save-excursion (search-forward
376 (concat "\n" mail-header-separator
"\n")
379 (if (re-search-backward "^[^\t]" nil t
)
381 (if (looking-at (car (car list
)))
382 (setq arg
(cdr (car list
))
384 (setq list
(cdr list
)))))
388 (skip-chars-backward "^ \t<,:")
390 (pattern (buffer-substring beg end
))
392 (setq list
(eval arg
)
393 completion
(try-completion pattern list
))
394 (cond ((eq completion t
))
396 (message "Can't find completion for \"%s\"" pattern
)
398 ((not (string= pattern completion
))
399 (delete-region beg end
)
400 (let ((alist-elt (assoc completion mail-names
)))
402 (cond ((eq mail-complete-style
'parens
)
403 (insert completion
" (" (cdr alist-elt
) ")"))
404 ((eq mail-complete-style
'angles
)
405 (insert (cdr alist-elt
) " <" completion
">"))
407 (insert completion
)))
408 (insert completion
))))
410 (message "Making completion list...")
411 (with-output-to-temp-buffer "*Completions*"
412 (display-completion-list
413 (all-completions pattern list
)))
414 (message "Making completion list...%s" "done"))))
415 (funcall mail-complete-function arg
))))
417 (defun mail-get-names (pattern)
418 "Fetch local users and global mail addresses for completion.
419 Consults `/etc/passwd' and a directory service if one is set up via
420 `mail-directory-function'.
421 PATTERN is the string we want to complete."
422 (if (eq mail-local-names t
)
424 (set-buffer (generate-new-buffer " passwd"))
425 (let ((files mail-passwd-files
))
427 (insert-file-contents (car files
) nil nil nil t
)
428 (setq files
(cdr files
))))
429 (if mail-passwd-command
430 (call-process shell-file-name nil t nil
431 shell-command-switch mail-passwd-command
))
432 (beginning-of-buffer)
433 (setq mail-local-names nil
)
435 ;;Recognize lines like
436 ;; nobody:*:65534:65534::/:
437 ;; +demo::::::/bin/csh
441 ;; The second \(...\) matches the user id.
442 (if (looking-at "\\+?\\([^:@\n+]+\\):[^:\n]*:\\([^\n:]*\\):")
443 (add-to-list 'mail-local-names
444 (cons (match-string 1)
446 (string-to-int (match-string 2))))))
447 (beginning-of-line 2))
448 (kill-buffer (current-buffer))))
449 (if (or (eq mail-names t
)
450 (eq mail-directory-names t
))
452 (and mail-directory-function
453 (eq mail-directory-names t
)
455 (mail-directory (if mail-directory-requery pattern
))))
456 (or mail-directory-requery
457 (setq mail-directory-names directory
))
461 (sort (append (if (consp mail-aliases
)
463 (function (lambda (a) (list (car a
))))
465 (if (consp mail-local-names
)
467 (or directory mail-directory-names
))
469 ;; should cache downcased strings
470 (string< (downcase (car a
))
471 (downcase (car b
)))))))))
475 (defun mail-directory (pattern)
476 "Call directory to get names matching PATTERN or all if `nil'.
477 Calls `mail-directory-function' and applies `mail-directory-parser' to output."
479 (message "Querying directory...")
480 (set-buffer (generate-new-buffer " *mail-directory*"))
481 (funcall mail-directory-function pattern
)
484 (if (stringp mail-directory-parser
)
485 (while (re-search-forward mail-directory-parser nil t
)
487 (cons (match-string 1) directory
)))
488 (if mail-directory-parser
489 (setq directory
(funcall mail-directory-parser
))
492 (cons (buffer-substring (point)
499 (kill-buffer (current-buffer))
500 (message "Querying directory...done")
504 (defun mail-directory-process (pattern)
505 "Run a shell command to output names in directory.
506 See `mail-directory-process'."
507 (apply 'call-process
(eval (car mail-directory-process
)) nil t nil
508 (mapcar 'eval
(cdr mail-directory-process
))))
510 ;; This should handle a dialog. Currently expects port to spit out names.
511 (defun mail-directory-stream (pattern)
512 "Open a stream to retrieve names in directory.
513 See `mail-directory-stream'."
514 (let (mailalias-done)
515 (set-process-sentinel
516 (apply 'open-network-stream
"mailalias" (current-buffer)
517 mail-directory-stream
)
519 (setq mailalias-done t
)))
520 (while (not mailalias-done
)
523 (defun mail-sentto-newsgroups ()
524 "Return all entries from Newsgroups: header as completion alist."
526 (if (mail-position-on-field "newsgroups" t
)
527 (let ((point (point))
529 (while (< (skip-chars-backward "^:, \t\n") 0)
530 (setq list
`((,(buffer-substring (point) point
))
532 (skip-chars-backward ", \t\n")
533 (setq point
(point)))
538 ;;; mailalias.el ends here