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.
36 "Alist of local users, aliases and directory entries as available.
37 Elements have the form (MAILNAME) or (MAILNAME . FULLNAME).
38 If the value means t, it means the real value should be calculated
39 for the next use. this is used in `mail-complete'.")
41 (defvar mail-local-names t
42 "Alist of local users.
43 When t this still needs to be initialized.")
45 (defvar mail-passwd-files
'("/etc/passwd")
46 "List of files from which to determine valid user names.")
48 (defvar mail-passwd-command nil
49 "Shell command to retrieve text to add to `/etc/passwd', or nil.")
51 (defvar mail-directory-names t
52 "Alist of mail address directory entries.
53 When t this still needs to be initialized.")
55 (defvar mail-address-field-regexp
56 "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):")
58 (defvar mail-complete-alist
59 `((,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 Expression may reference variable `pattern' which is the string being completed.
69 If not on matching header, `mail-complete-function' gets called instead.")
72 (defvar mail-complete-style
'angles
73 "*Specifies how \\[mail-complete] formats the full name when it completes.
74 If `nil', they contain just the return address like:
76 If `parens', they look like:
77 king@grassland.com (Elvis Parsley)
78 If `angles', they look like:
79 Elvis Parsley <king@grassland.com>")
81 (defvar mail-complete-function
'ispell-complete-word
82 "Function to call when completing outside `mail-complete-alist'-header.")
85 (defvar mail-directory-function nil
86 "Function to get completions from directory service or `nil' for none.
87 See `mail-directory-requery'.")
90 ;; This is for when the directory is huge, or changes frequently.
91 (defvar mail-directory-requery nil
92 "When non-`nil' call `mail-directory-function' for each completion.
93 In that case, one argument gets passed to the function, the partial string
97 (defvar mail-directory-process nil
98 "Unix command when `mail-directory-function' is `mail-directory-process'.
99 This is a list of the form (COMMAND ARG ...), where each of the list elements
100 is evaluated. When `mail-directory-requery' is non-`nil', during
101 evaluation the variable `pattern' contains the partial input being completed.
104 '(remote-shell-program \"HOST\" \"-nl\" \"USER\" \"COMMAND\")
108 '(remote-shell-program \"HOST\" \"-n\" \"COMMAND '^\" pattern \"'\")")
110 (defvar mail-directory-stream
()
111 "List of (HOST SERVICE) for stream connection to mail directory.")
113 (defvar mail-directory-parser nil
114 "How to interpret the output of `mail-directory-function'.
115 Three types of values are possible:
117 - nil means to gather each line as one name
118 - regexp means first \\(grouping\\) in successive matches is name
119 - function called at beginning of buffer that returns an alist of names")
122 ;; Called from sendmail-send-it, or similar functions,
123 ;; only if some mail aliases are defined.
125 (defun expand-mail-aliases (beg end
&optional exclude
)
126 "Expand all mail aliases in suitable header fields found between BEG and END.
127 If interactive, expand in header fields before `mail-header-separator'.
128 Suitable header fields are `To', `From', `CC' and `BCC', `Reply-to', and
129 their `Resent-' variants.
131 Optional second arg EXCLUDE may be a regular expression defining text to be
132 removed from alias expansions."
135 (list (goto-char (point-min))
136 (search-forward (concat "\n" mail-header-separator
"\n")))))
137 (sendmail-sync-aliases)
138 (when (eq mail-aliases t
)
139 (setq mail-aliases nil
)
140 (build-mail-aliases))
143 (setq end
(set-marker (make-marker) end
))
144 (let ((case-fold-search nil
))
145 (while (let ((case-fold-search t
))
146 (re-search-forward mail-address-field-regexp end t
))
147 (skip-chars-forward " \t")
150 ;; DISABLED-ALIASES records aliases temporarily disabled
151 ;; while we scan text that resulted from expanding those aliases.
152 ;; Each element is (ALIAS . TILL-WHEN), where TILL-WHEN
153 ;; is where to reenable the alias (expressed as number of chars
154 ;; counting from END1).
155 (disabled-aliases nil
))
156 (re-search-forward "^[^ \t]" end
'move
)
158 (skip-chars-backward " \t\n")
159 (setq end1
(point-marker))
161 (while (< (point) end1
)
163 ;; Reenable any aliases which were disabled for ranges
164 ;; that we have passed out of.
165 (while (and disabled-aliases
166 (> pos
(- end1
(cdr (car disabled-aliases
)))))
167 (setq disabled-aliases
(cdr disabled-aliases
)))
168 ;; EPOS gets position of end of next name;
169 ;; SEPLEN gets length of whitespace&separator that follows it.
170 (if (re-search-forward "[ \t]*[\n,][ \t]*" end1 t
)
171 (setq epos
(match-beginning 0)
172 seplen
(- (point) epos
))
173 (setq epos
(marker-position end1
) seplen
0))
174 (let ((string (buffer-substring-no-properties pos epos
))
176 (if (and (not (assoc string disabled-aliases
))
177 (setq translation
(cdr (assoc string mail-aliases
))))
179 ;; This name is an alias. Disable it.
180 (setq disabled-aliases
(cons (cons string
(- end1 epos
))
182 ;; Replace the alias with its expansion
183 ;; then rescan the expansion for more aliases.
187 (let ((regexp (concat "\\b\\(" exclude
"\\)\\b"))
188 (end (point-marker)))
190 (while (re-search-forward regexp end t
)
193 (delete-region (point) (+ (point) (- epos pos
)))
195 ;; Name is not an alias. Skip to start of next name.
197 (forward-char seplen
))))
198 (set-marker end1 nil
)))
199 (set-marker end nil
))))
201 ;; Called by mail-setup, or similar functions, only if the file specified
202 ;; by mail-personal-alias-file (usually `~/.mailrc') exists.
203 (defun build-mail-aliases (&optional file
)
204 "Read mail aliases from personal aliases file and set `mail-aliases'.
205 By default, this is the file specified by `mail-personal-alias-file'."
206 (setq file
(expand-file-name (or file mail-personal-alias-file
)))
208 (obuf (current-buffer)))
211 (setq buffer
(generate-new-buffer " mailrc"))
214 (cond ((get-file-buffer file
)
215 (insert (save-excursion
216 (set-buffer (get-file-buffer file
))
217 (buffer-substring-no-properties
218 (point-min) (point-max)))))
219 ((file-exists-p file
) (insert-file-contents file
))
220 ((file-exists-p (setq file
(concat "~/" file
)))
221 (insert-file-contents file
))
223 ;; Don't lose if no final newline.
224 (goto-char (point-max))
225 (or (eq (preceding-char) ?
\n) (newline))
226 (goto-char (point-min))
227 ;; handle "\\\n" continuation lines
230 (if (= (preceding-char) ?
\\)
231 (progn (delete-char -
1) (delete-char 1) (insert ?\
))
233 (goto-char (point-min))
234 ;; handle `source' directives -- Eddy/1994/May/25
235 (cond ((re-search-forward "^source[ \t]+" nil t
)
236 (re-search-forward "\\S-+")
237 (setq file
(buffer-substring-no-properties
238 (match-beginning 0) (match-end 0)))
240 (insert "# ") ; to ensure we don't re-process this file
242 (t (setq file nil
))))
243 (goto-char (point-min))
244 (while (re-search-forward
245 "^\\(a\\|alias\\|g\\|group\\)[ \t]+\\([^ \t]+\\)" nil t
)
246 (let* ((name (match-string 2))
247 (start (progn (skip-chars-forward " \t") (point))))
251 (buffer-substring-no-properties start
(point))
254 (if buffer
(kill-buffer buffer
))
257 ;; Always autoloadable in case the user wants to define aliases
258 ;; interactively or in .emacs.
260 (defun define-mail-alias (name definition
&optional from-mailrc-file
)
261 "Define NAME as a mail alias that translates to DEFINITION.
262 This means that sending a message to NAME will actually send to DEFINITION.
264 Normally, the addresses in DEFINITION must be separated by commas.
265 If FROM-MAILRC-FILE is non-nil, then addresses in DEFINITION
266 can be separated by spaces; an address can contain spaces
267 if it is quoted with double-quotes."
269 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
270 ;; Read the defaults first, if we have not done so.
271 (sendmail-sync-aliases)
272 (if (eq mail-aliases t
)
274 (setq mail-aliases nil
)
275 (if (file-exists-p mail-personal-alias-file
)
276 (build-mail-aliases))))
277 ;; strip garbage from front and end
278 (if (string-match "\\`[ \t\n,]+" definition
)
279 (setq definition
(substring definition
(match-end 0))))
280 (if (string-match "[ \t\n,]+\\'" definition
)
281 (setq definition
(substring definition
0 (match-beginning 0))))
283 ;; If DEFINITION is null string, avoid looping even once.
284 (start (and (not (equal definition
"")) 0))
285 (L (length definition
))
288 ;; If we're reading from the mailrc file, then addresses are delimited
289 ;; by spaces, and addresses with embedded spaces must be surrounded by
290 ;; double-quotes. Otherwise, addresses are separated by commas.
292 (if (eq ?
\" (aref definition start
))
293 (setq start
(1+ start
)
294 end
(string-match "\"[ \t,]*" definition start
))
295 (setq end
(string-match "[ \t,]+" definition start
)))
296 (setq end
(string-match "[ \t\n,]*,[ \t\n,]*" definition start
)))
297 (setq result
(cons (substring definition start end
) result
))
301 (setq definition
(mapconcat (function identity
)
304 (setq tem
(assoc name mail-aliases
))
306 (rplacd tem definition
)
307 (setq mail-aliases
(cons (cons name definition
) mail-aliases
)
311 (defun mail-complete (arg)
312 "Perform completion on header field or word preceding point.
313 Completable headers are according to `mail-complete-alist'. If none matches
314 current header, calls `mail-complete-function' and passes prefix arg if any."
316 ;; Read the defaults first, if we have not done so.
317 (sendmail-sync-aliases)
318 (if (eq mail-aliases t
)
320 (setq mail-aliases nil
)
321 (if (file-exists-p mail-personal-alias-file
)
322 (build-mail-aliases))))
323 (let ((list mail-complete-alist
))
324 (if (and (save-excursion (search-forward
325 (concat "\n" mail-header-separator
"\n")
328 (if (re-search-backward "^[^\t]" nil t
)
330 (if (looking-at (car (car list
)))
331 (setq arg
(cdr (car list
))
333 (setq list
(cdr list
)))))
337 (skip-chars-backward "^ \t<,:")
339 (pattern (buffer-substring beg end
))
341 (setq list
(eval arg
)
342 completion
(try-completion pattern list
))
343 (cond ((eq completion t
))
345 (message "Can't find completion for \"%s\"" pattern
)
347 ((not (string= pattern completion
))
348 (delete-region beg end
)
349 (let ((alist-elt (assoc completion mail-names
)))
351 (cond ((eq mail-complete-style
'parens
)
352 (insert completion
" (" (cdr alist-elt
) ")"))
353 ((eq mail-complete-style
'angles
)
354 (insert (cdr alist-elt
) " <" completion
">"))
356 (insert completion
)))
357 (insert completion
))))
359 (message "Making completion list...")
360 (with-output-to-temp-buffer "*Completions*"
361 (display-completion-list
362 (all-completions pattern list
)))
363 (message "Making completion list...%s" "done"))))
364 (funcall mail-complete-function arg
))))
366 (defun mail-get-names (pattern)
367 "Fetch local users and global mail addresses for completion.
368 Consults `/etc/passwd' and a directory service if one is set up via
369 `mail-directory-function'.
370 PATTERN is the string we want to complete."
371 (if (eq mail-local-names t
)
373 (set-buffer (generate-new-buffer " passwd"))
374 (let ((files mail-passwd-files
))
376 (insert-file-contents (car files
) nil nil nil t
)
377 (setq files
(cdr files
))))
378 (if mail-passwd-command
379 (call-process shell-file-name nil t nil
380 shell-command-switch mail-passwd-command
))
381 (beginning-of-buffer)
382 (setq mail-local-names nil
)
384 ;;Recognize lines like
385 ;; nobody:*:65534:65534::/:
386 ;; +demo::::::/bin/csh
390 ;; The second \(...\) matches the user id.
391 (if (looking-at "\\+?\\([^:@\n+]+\\):[^:\n]*:\\([^\n:]*\\):")
392 (add-to-list 'mail-local-names
393 (cons (match-string 1)
395 (string-to-int (match-string 2))))))
396 (beginning-of-line 2))
397 (kill-buffer (current-buffer))))
398 (if (or (eq mail-names t
)
399 (eq mail-directory-names t
))
401 (and mail-directory-function
402 (eq mail-directory-names t
)
404 (mail-directory (if mail-directory-requery pattern
))))
405 (or mail-directory-requery
406 (setq mail-directory-names directory
))
410 (sort (append (if (consp mail-aliases
)
412 (function (lambda (a) (list (car a
))))
414 (if (consp mail-local-names
)
416 (or directory mail-directory-names
))
418 ;; should cache downcased strings
419 (string< (downcase (car a
))
420 (downcase (car b
)))))))))
424 (defun mail-directory (pattern)
425 "Call directory to get names matching PATTERN or all if `nil'.
426 Calls `mail-directory-function' and applies `mail-directory-parser' to output."
428 (message "Querying directory...")
429 (set-buffer (generate-new-buffer " *mail-directory*"))
430 (funcall mail-directory-function pattern
)
433 (if (stringp mail-directory-parser
)
434 (while (re-search-forward mail-directory-parser nil t
)
436 (cons (match-string 1) directory
)))
437 (if mail-directory-parser
438 (setq directory
(funcall mail-directory-parser
))
441 (cons (buffer-substring (point)
448 (kill-buffer (current-buffer))
449 (message "Querying directory...done")
453 (defun mail-directory-process (pattern)
454 "Call a Unix process to output names in directory.
455 See `mail-directory-process'."
456 (apply 'call-process
(eval (car mail-directory-process
)) nil t nil
457 (mapcar 'eval
(cdr mail-directory-process
))))
459 ;; This should handle a dialog. Currently expects port to spit out names.
460 (defun mail-directory-stream (pattern)
461 "Open a stream to retrieve names in directory.
462 See `mail-directory-stream'."
463 (let (mailalias-done)
464 (set-process-sentinel
465 (apply 'open-network-stream
"mailalias" (current-buffer)
466 mail-directory-stream
)
468 (setq mailalias-done t
)))
469 (while (not mailalias-done
)
472 (defun mail-sentto-newsgroups ()
473 "Return all entries from Newsgroups: header as completion alist."
475 (if (mail-position-on-field "newsgroups" t
)
476 (let ((point (point))
478 (while (< (skip-chars-backward "^:, \t\n") 0)
479 (setq list
`((,(buffer-substring (point) point
))
481 (skip-chars-backward ", \t\n")
482 (setq point
(point)))
487 ;;; mailalias.el ends here