Replace "Maintainer: FSF" with the emacs-devel mailing address
[bpt/emacs.git] / lisp / mail / mailalias.el
CommitLineData
daca8ba5 1;;; mailalias.el --- expand and complete mailing address aliases -*- lexical-binding: t -*-
6594deb0 2
ba318903 3;; Copyright (C) 1985, 1987, 1995-1997, 2001-2014 Free Software
ab422c4d 4;; Foundation, Inc.
3a801d0c 5
34dc21db 6;; Maintainer: emacs-devel@gnu.org
fd7fa35a 7;; Keywords: mail
e5167999 8
80a677d9
JA
9;; This file is part of GNU Emacs.
10
b1fc2b50 11;; GNU Emacs is free software: you can redistribute it and/or modify
80a677d9 12;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
80a677d9
JA
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b1fc2b50 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
80a677d9 23
e41b2db1
ER
24;;; Commentary:
25
26;; Basic functions for defining and expanding mail aliases.
27;; These seal off the interface to the alias-definition parts of a
28;; .mailrc file formatted for BSD's Mail or USL's mailx.
29
e5167999 30;;; Code:
80a677d9 31
594906dd 32(require 'sendmail)
c2db7f31 33
31407d5d 34(defgroup mailalias nil
f5684faf 35 "Expanding mail aliases."
31407d5d 36 :group 'mail)
48919e0f 37
31407d5d 38(defcustom mail-passwd-files '("/etc/passwd")
c1051e81 39 "List of files from which to determine valid user names."
31407d5d
RS
40 :type '(repeat string)
41 :group 'mailalias)
8e3beae0 42
31407d5d 43(defcustom mail-passwd-command nil
c1051e81 44 "Shell command to retrieve text to add to `/etc/passwd', or nil."
31407d5d
RS
45 :type '(choice string (const nil))
46 :group 'mailalias)
8e3beae0 47
48919e0f
RS
48(defvar mail-directory-names t
49 "Alist of mail address directory entries.
50When t this still needs to be initialized.")
51
52(defvar mail-address-field-regexp
53 "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):")
54
daca8ba5
SM
55(defvar pattern)
56
31407d5d 57(defcustom mail-complete-alist
310d0d40
RS
58 ;; Don't refer to mail-address-field-regexp here;
59 ;; that confuses some things such as cus-dep.el.
daca8ba5
SM
60 '(("^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):"
61 . (mail-get-names pattern))
62 ("Newsgroups:" . (if (boundp 'gnus-active-hashtb)
63 gnus-active-hashtb
64 (if (boundp news-group-article-assoc)
65 news-group-article-assoc)))
66 ("Followup-To:" . (mail-sentto-newsgroups))
67 ;;("Distribution:" ???)
68 )
c1051e81 69 "Alist of header field and expression to return alist for completion.
31407d5d
RS
70The expression may reference the variable `pattern'
71which will hold the string being completed.
72If not on matching header, `mail-complete-function' gets called instead."
bd09e573 73 :type 'alist
31407d5d
RS
74 :group 'mailalias)
75(put 'mail-complete-alist 'risky-local-variable t)
48919e0f 76
e694581d 77;;;###autoload
31407d5d 78(defcustom mail-complete-style 'angles
c1051e81 79 "Specifies how \\[mail-complete] formats the full name when it completes.
e694581d
RS
80If `nil', they contain just the return address like:
81 king@grassland.com
82If `parens', they look like:
83 king@grassland.com (Elvis Parsley)
84If `angles', they look like:
31407d5d
RS
85 Elvis Parsley <king@grassland.com>"
86 :type '(choice (const angles) (const parens) (const nil))
87 :group 'mailalias)
48919e0f 88
31407d5d 89(defcustom mail-complete-function 'ispell-complete-word
c1051e81 90 "Function to call when completing outside `mail-complete-alist'-header."
31407d5d
RS
91 :type '(choice function (const nil))
92 :group 'mailalias)
daca8ba5
SM
93(make-obsolete-variable 'mail-complete-function
94 'completion-at-point-functions "24.1")
48919e0f 95
31407d5d 96(defcustom mail-directory-function nil
c1051e81 97 "Function to get completions from directory service or nil for none.
31407d5d
RS
98See `mail-directory-requery'."
99 :type '(choice function (const nil))
100 :group 'mailalias)
48919e0f
RS
101
102;; This is for when the directory is huge, or changes frequently.
31407d5d 103(defcustom mail-directory-requery nil
c1051e81 104 "When non-nil call `mail-directory-function' for each completion.
48919e0f 105In that case, one argument gets passed to the function, the partial string
31407d5d
RS
106entered so far."
107 :type 'boolean
108 :group 'mailalias)
48919e0f 109
31407d5d 110(defcustom mail-directory-process nil
c1051e81 111 "Shell command to get the list of names from a mail directory.
31407d5d
RS
112This value is used when the value of `mail-directory-function'
113is `mail-directory-process'. The value should be a list
114of the form (COMMAND ARG ...), where each of the list elements
f6250436
RS
115is evaluated. COMMAND should evaluate to a string. When
116`mail-directory-requery' is non-nil, during evaluation of these
117elements, the variable `pattern' contains the partial input being
118completed. `pattern' is nil when `mail-directory-requery' is nil.
48919e0f 119
31407d5d 120The value might look like this:
48919e0f
RS
121
122 '(remote-shell-program \"HOST\" \"-nl\" \"USER\" \"COMMAND\")
123
31407d5d 124or like this:
48919e0f 125
31407d5d
RS
126 '(remote-shell-program \"HOST\" \"-n\" \"COMMAND '^\" pattern \"'\")"
127 :type 'sexp
128 :group 'mailalias)
129(put 'mail-directory-process 'risky-local-variable t)
48919e0f 130
31407d5d 131(defcustom mail-directory-stream nil
c1051e81 132 "List of (HOST SERVICE) for stream connection to mail directory."
31407d5d
RS
133 :type 'sexp
134 :group 'mailalias)
135(put 'mail-directory-stream 'risky-local-variable t)
48919e0f 136
31407d5d 137(defcustom mail-directory-parser nil
c1051e81 138 "How to interpret the output of `mail-directory-function'.
48919e0f
RS
139Three types of values are possible:
140
141 - nil means to gather each line as one name
142 - regexp means first \\(grouping\\) in successive matches is name
31407d5d 143 - function called at beginning of buffer that returns an alist of names"
94955307 144 :type '(choice (const nil) regexp function)
31407d5d
RS
145 :group 'mailalias)
146(put 'mail-directory-parser 'risky-local-variable t)
48919e0f 147
31407d5d
RS
148;; Internal variables.
149
150(defvar mail-names t
151 "Alist of local users, aliases and directory entries as available.
152Elements have the form (MAILNAME) or (MAILNAME . FULLNAME).
153If the value means t, it means the real value should be calculated
f6250436 154for the next use. This is used in `mail-complete'.")
31407d5d
RS
155
156(defvar mail-local-names t
157 "Alist of local users.
158When t this still needs to be initialized.")
159\f
48919e0f 160
80a677d9
JA
161;; Called from sendmail-send-it, or similar functions,
162;; only if some mail aliases are defined.
e28449ed 163;;;###autoload
80a677d9
JA
164(defun expand-mail-aliases (beg end &optional exclude)
165 "Expand all mail aliases in suitable header fields found between BEG and END.
30eaaa34 166If interactive, expand in header fields.
1abe8488
KH
167Suitable header fields are `To', `From', `CC' and `BCC', `Reply-to', and
168their `Resent-' variants.
169
96846422
RS
170Optional second arg EXCLUDE may be a regular expression defining text to be
171removed from alias expansions."
e28449ed
SM
172 (interactive
173 (save-excursion
174 (list (goto-char (point-min))
30eaaa34 175 (mail-header-end))))
e934d700 176 (sendmail-sync-aliases)
e28449ed
SM
177 (when (eq mail-aliases t)
178 (setq mail-aliases nil)
179 (build-mail-aliases))
180 (save-excursion
181 (goto-char beg)
182 (setq end (set-marker (make-marker) end))
183 (let ((case-fold-search nil))
184 (while (let ((case-fold-search t))
185 (re-search-forward mail-address-field-regexp end t))
186 (skip-chars-forward " \t")
187 (let ((beg1 (point))
188 end1 pos epos seplen
189 ;; DISABLED-ALIASES records aliases temporarily disabled
190 ;; while we scan text that resulted from expanding those aliases.
191 ;; Each element is (ALIAS . TILL-WHEN), where TILL-WHEN
192 ;; is where to reenable the alias (expressed as number of chars
193 ;; counting from END1).
194 (disabled-aliases nil))
195 (re-search-forward "^[^ \t]" end 'move)
196 (beginning-of-line)
197 (skip-chars-backward " \t\n")
198 (setq end1 (point-marker))
199 (goto-char beg1)
200 (while (< (point) end1)
201 (setq pos (point))
202 ;; Reenable any aliases which were disabled for ranges
203 ;; that we have passed out of.
204 (while (and disabled-aliases
205 (> pos (- end1 (cdr (car disabled-aliases)))))
206 (setq disabled-aliases (cdr disabled-aliases)))
207 ;; EPOS gets position of end of next name;
208 ;; SEPLEN gets length of whitespace&separator that follows it.
209 (if (re-search-forward "[ \t]*[\n,][ \t]*" end1 t)
210 (setq epos (match-beginning 0)
211 seplen (- (point) epos))
a10b2d03
RS
212 ;; Handle the last name in this header field.
213 ;; We already moved END1 back across whitespace after it.
214 (setq epos (marker-position end1) seplen 0))
e28449ed
SM
215 (let ((string (buffer-substring-no-properties pos epos))
216 translation)
217 (if (and (not (assoc string disabled-aliases))
218 (setq translation (cdr (assoc string mail-aliases))))
219 (progn
220 ;; This name is an alias. Disable it.
221 (setq disabled-aliases (cons (cons string (- end1 epos))
222 disabled-aliases))
223 ;; Replace the alias with its expansion
224 ;; then rescan the expansion for more aliases.
225 (goto-char pos)
226 (insert translation)
227 (when exclude
228 (let ((regexp (concat "\\b\\(" exclude "\\)\\b"))
80a677d9
JA
229 (end (point-marker)))
230 (goto-char pos)
231 (while (re-search-forward regexp end t)
232 (replace-match ""))
233 (goto-char end)))
e28449ed
SM
234 (delete-region (point) (+ (point) (- epos pos)))
235 (goto-char pos))
236 ;; Name is not an alias. Skip to start of next name.
237 (goto-char epos)
238 (forward-char seplen))))
239 (set-marker end1 nil)))
240 (set-marker end nil))))
80a677d9 241
9d73ab0d
NF
242;; Called by mail-setup, or similar functions, only if the file specified
243;; by mail-personal-alias-file (usually `~/.mailrc') exists.
80a677d9 244(defun build-mail-aliases (&optional file)
9d73ab0d
NF
245 "Read mail aliases from personal aliases file and set `mail-aliases'.
246By default, this is the file specified by `mail-personal-alias-file'."
53aff12a
GM
247 (interactive
248 (list
249 (read-file-name (format "Read mail alias file (default %s): "
250 mail-personal-alias-file)
251 nil mail-personal-alias-file t)))
9d73ab0d 252 (setq file (expand-file-name (or file mail-personal-alias-file)))
3c3d71d9
RS
253 ;; In case mail-aliases is t, make sure define-mail-alias
254 ;; does not recursively call build-mail-aliases.
255 (setq mail-aliases nil)
9421bdd1
SM
256 (with-temp-buffer
257 (while file
258 (cond ((get-file-buffer file)
259 (insert (with-current-buffer (get-file-buffer file)
260 (buffer-substring-no-properties
261 (point-min) (point-max)))))
262 ((file-exists-p file) (insert-file-contents file))
263 ((file-exists-p (setq file (expand-file-name file "~/")))
264 (insert-file-contents file))
265 (t (setq file nil)))
266 (goto-char (point-min))
267 ;; Delete comments from the contents.
268 (while (search-forward "# " nil t)
269 (let ((p (- (point) 2)))
270 (end-of-line)
271 (delete-region p (point))))
272 ;; Don't lose if no final newline.
273 (goto-char (point-max))
274 (or (eq (preceding-char) ?\n) (newline))
275 (goto-char (point-min))
276 ;; handle "\\\n" continuation lines
277 (while (not (eobp))
278 (end-of-line)
279 (if (= (preceding-char) ?\\)
280 (progn (delete-char -1) (delete-char 1) (insert ?\ ))
281 (forward-char 1)))
282 (goto-char (point-min))
283 ;; handle `source' directives -- Eddy/1994/May/25
284 (cond ((re-search-forward "^source[ \t]+" nil t)
285 (re-search-forward "\\S-+")
286 (setq file (buffer-substring-no-properties
287 (match-beginning 0) (match-end 0)))
288 (beginning-of-line)
289 (insert "# ") ; to ensure we don't re-process this file
290 (beginning-of-line))
291 (t (setq file nil))))
292 (goto-char (point-min))
293 (while (re-search-forward
294 "^\\(a\\|alias\\|g\\|group\\)[ \t]+\\([^ \t\n]+\\)" nil t)
295 (let* ((name (match-string 2))
296 (start (progn (skip-chars-forward " \t") (point)))
297 value)
298 (end-of-line)
299 (setq value (buffer-substring-no-properties start (point)))
300 (unless (equal value "")
301 (define-mail-alias name value t))))
302 mail-aliases))
80a677d9
JA
303
304;; Always autoloadable in case the user wants to define aliases
305;; interactively or in .emacs.
c8ca9217 306;; define-mail-abbrev in mailabbrev.el duplicates much of this code.
7229064d 307;;;###autoload
37e379dd 308(defun define-mail-alias (name definition &optional from-mailrc-file)
96846422 309 "Define NAME as a mail alias that translates to DEFINITION.
80a677d9 310This means that sending a message to NAME will actually send to DEFINITION.
fa786521
RS
311
312Normally, the addresses in DEFINITION must be separated by commas.
a1506d29 313If FROM-MAILRC-FILE is non-nil, then addresses in DEFINITION
fa786521
RS
314can be separated by spaces; an address can contain spaces
315if it is quoted with double-quotes."
316
80a677d9
JA
317 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
318 ;; Read the defaults first, if we have not done so.
3c3d71d9
RS
319 ;; But not if we are doing that already right now.
320 (unless from-mailrc-file
321 (sendmail-sync-aliases))
80a677d9
JA
322 (if (eq mail-aliases t)
323 (progn
324 (setq mail-aliases nil)
9d73ab0d 325 (if (file-exists-p mail-personal-alias-file)
80a677d9 326 (build-mail-aliases))))
3af1a1f3
RS
327 ;; strip garbage from front and end
328 (if (string-match "\\`[ \t\n,]+" definition)
aa228418 329 (setq definition (substring definition (match-end 0))))
3af1a1f3 330 (if (string-match "[ \t\n,]+\\'" definition)
aa228418 331 (setq definition (substring definition 0 (match-beginning 0))))
c8ca9217
CY
332
333 (let* ((L (length definition))
334 (start (if (> L 0) 0))
335 end this-entry result tem)
37e379dd 336 (while start
c8ca9217
CY
337 (cond
338 (from-mailrc-file
339 ;; If we're reading from the mailrc file, addresses are
340 ;; delimited by spaces, and addresses with embedded spaces are
341 ;; surrounded by non-escaped double-quotes.
342 (if (eq ?\" (aref definition start))
343 (setq start (1+ start)
344 end (and (string-match
345 "[^\\]\\(\\([\\][\\]\\)*\\)\"[ \t,]*"
346 definition start)
347 (match-end 1)))
348 (setq end (string-match "[ \t,]+" definition start)))
349 ;; Extract the address and advance the loop past it.
350 (setq this-entry (substring definition start end)
351 start (and end (/= (match-end 0) L) (match-end 0)))
352 ;; If the full name contains a problem character, quote it.
353 (and (string-match "\\(.+?\\)[ \t]*\\(<.*>\\)" this-entry)
354 (string-match "[^- !#$%&'*+/0-9=?A-Za-z^_`{|}~]"
355 (match-string 1 this-entry))
356 (setq this-entry (replace-regexp-in-string
357 "\\(.+?\\)[ \t]*\\(<.*>\\)"
358 "\"\\1\" \\2"
359 this-entry))))
360 ;; When we are not reading from .mailrc, addresses are
361 ;; separated by commas. Try to accept a rfc822-like syntax.
362 ;; (Todo: extend rfc822.el to do the work for us.)
363 ((equal (string-match
364 "[ \t,]*\\(\"\\(?:[^\"]\\|[^\\]\\(?:[\\][\\]\\)*\"\\)*\"[ \t]*\
365<[-.!#$%&'*+/0-9=?A-Za-z^_`{|}~@]+>\\)[ \t,]*"
366 definition start)
367 start)
368 ;; If an entry has a valid [ "foo bar" <foo@example.com> ]
369 ;; form, use it literally . This also allows commas in the
370 ;; quoted string, e.g. [ "foo bar, jr" <foo@example.com> ]
371 (setq this-entry (match-string 1 definition)
372 start (and (/= (match-end 0) L) (match-end 0))))
373 (t
374 ;; Otherwise, read the next address by looking for a comma.
375 (setq end (string-match "[ \t\n,]*,[ \t\n]*" definition start))
376 (setq this-entry (substring definition start end))
377 ;; Advance the loop past this address.
378 (setq start (and end (/= (match-end 0) L) (match-end 0)))
379 ;; If the full name contains a problem character, quote it.
380 (and (string-match "\\(.+?\\)[ \t]*\\(<.*>\\)" this-entry)
381 (string-match "[^- !#$%&'*+/0-9=?A-Za-z^_`{|}~]"
382 (match-string 1 this-entry))
383 (setq this-entry (replace-regexp-in-string
384 "\\(.+?\\)[ \t]*\\(<.*>\\)" "\"\\1\" \\2"
385 this-entry)))))
386 (push this-entry result))
387
37e379dd 388 (setq definition (mapconcat (function identity)
c8ca9217 389 (nreverse result) ", "))
80a677d9
JA
390 (setq tem (assoc name mail-aliases))
391 (if tem
392 (rplacd tem definition)
48919e0f
RS
393 (setq mail-aliases (cons (cons name definition) mail-aliases)
394 mail-names t))))
395
c0fd4267 396;;;###autoload
daca8ba5
SM
397(defun mail-completion-at-point-function ()
398 "Compute completion data for mail aliases.
399For use on `completion-at-point-functions'."
9a03a6f4
SM
400 ;; Read the defaults first, if we have not done so.
401 (sendmail-sync-aliases)
402 (if (eq mail-aliases t)
403 (progn
404 (setq mail-aliases nil)
405 (if (file-exists-p mail-personal-alias-file)
406 (build-mail-aliases))))
daca8ba5
SM
407 (let ((list mail-complete-alist)
408 (list-exp nil))
baaddce7 409 (if (and (< 0 (mail-header-end))
48919e0f 410 (save-excursion
daca8ba5 411 (if (re-search-backward "^[^\t ]" nil t)
48919e0f
RS
412 (while list
413 (if (looking-at (car (car list)))
daca8ba5 414 (setq list-exp (cdr (car list))
48919e0f
RS
415 list ())
416 (setq list (cdr list)))))
daca8ba5 417 list-exp))
48919e0f
RS
418 (let* ((end (point))
419 (beg (save-excursion
420 (skip-chars-backward "^ \t<,:")
421 (point)))
daca8ba5
SM
422 (table (completion-table-dynamic
423 (lambda (prefix)
424 (let ((pattern prefix)) (eval list-exp))))))
425 (list beg end table)))))
48919e0f 426
daca8ba5
SM
427;;;###autoload
428(defun mail-complete (arg)
429 "Perform completion on header field or word preceding point.
430Completable headers are according to `mail-complete-alist'. If none matches
431current header, calls `mail-complete-function' and passes prefix ARG if any."
59f7af81 432 (declare (obsolete mail-completion-at-point-function "24.1"))
daca8ba5
SM
433 (interactive "P")
434 ;; Read the defaults first, if we have not done so.
435 (sendmail-sync-aliases)
436 (if (eq mail-aliases t)
437 (progn
438 (setq mail-aliases nil)
439 (if (file-exists-p mail-personal-alias-file)
440 (build-mail-aliases))))
441 (let ((data (mail-completion-at-point-function)))
442 (if data
443 (apply #'completion-in-region data)
444 (funcall mail-complete-function arg))))
daca8ba5
SM
445
446(defun mail-completion-expand (table)
447 "Build new completion table that expands aliases.
448Completes like TABLE except that if the completion is a valid alias,
449it expands it to its full `mail-complete-style' form."
450 (lambda (string pred action)
451 (cond
452 ((eq action nil)
453 (let* ((comp (try-completion string table pred))
454 (name (and (listp table) comp
455 (assoc (if (stringp comp) comp string) table))))
456 (cond
457 ((null name) comp)
458 ((eq mail-complete-style 'parens)
459 (concat (car name) " (" (cdr name) ")"))
460 ((eq mail-complete-style 'angles)
461 (concat (cdr name) " <" (car name) ">"))
462 (t comp))))
463 (t
464 (complete-with-action action table string pred)))))
465
466(defun mail-get-names (prefix)
e694581d 467 "Fetch local users and global mail addresses for completion.
48919e0f 468Consults `/etc/passwd' and a directory service if one is set up via
e694581d 469`mail-directory-function'.
daca8ba5 470PREFIX is the string we want to complete."
48919e0f 471 (if (eq mail-local-names t)
9421bdd1 472 (with-current-buffer (generate-new-buffer " passwd")
8e3beae0
RS
473 (let ((files mail-passwd-files))
474 (while files
475 (insert-file-contents (car files) nil nil nil t)
476 (setq files (cdr files))))
477 (if mail-passwd-command
478 (call-process shell-file-name nil t nil
479 shell-command-switch mail-passwd-command))
637502c3 480 (goto-char (point-min))
8e3beae0 481 (setq mail-local-names nil)
48919e0f 482 (while (not (eobp))
8bc49f8f
RS
483 ;;Recognize lines like
484 ;; nobody:*:65534:65534::/:
485 ;; +demo::::::/bin/csh
486 ;; +ethanb
487 ;;while skipping
488 ;; +@SOFTWARE
e694581d
RS
489 ;; The second \(...\) matches the user id.
490 (if (looking-at "\\+?\\([^:@\n+]+\\):[^:\n]*:\\([^\n:]*\\):")
491 (add-to-list 'mail-local-names
492 (cons (match-string 1)
493 (user-full-name
027a4b6b 494 (string-to-number (match-string 2))))))
48919e0f
RS
495 (beginning-of-line 2))
496 (kill-buffer (current-buffer))))
497 (if (or (eq mail-names t)
e694581d 498 (eq mail-directory-names t))
48919e0f
RS
499 (let (directory)
500 (and mail-directory-function
501 (eq mail-directory-names t)
502 (setq directory
daca8ba5 503 (mail-directory (if mail-directory-requery prefix))))
e694581d
RS
504 (or mail-directory-requery
505 (setq mail-directory-names directory))
48919e0f
RS
506 (if (or directory
507 (eq mail-names t))
508 (setq mail-names
e694581d
RS
509 (sort (append (if (consp mail-aliases)
510 (mapcar
511 (function (lambda (a) (list (car a))))
512 mail-aliases))
48919e0f
RS
513 (if (consp mail-local-names)
514 mail-local-names)
f6250436
RS
515 (or directory
516 (when (consp mail-directory-names)
517 mail-directory-names)))
48919e0f 518 (lambda (a b)
daca8ba5 519 ;; Should cache downcased strings.
48919e0f 520 (string< (downcase (car a))
e694581d 521 (downcase (car b)))))))))
daca8ba5 522 (mail-completion-expand mail-names))
48919e0f
RS
523
524
daca8ba5
SM
525(defun mail-directory (prefix)
526 "Use mail-directory facility to get user names matching PREFIX.
527If PREFIX is nil, get all the defined user names.
f6250436
RS
528This function calls `mail-directory-function' to query the directory,
529then uses `mail-directory-parser' to parse the output it returns."
9421bdd1
SM
530 (message "Querying directory...")
531 (with-current-buffer (generate-new-buffer " *mail-directory*")
daca8ba5 532 (funcall mail-directory-function prefix)
9421bdd1 533 (goto-char (point-min))
48919e0f
RS
534 (let (directory)
535 (if (stringp mail-directory-parser)
536 (while (re-search-forward mail-directory-parser nil t)
daca8ba5 537 (push (match-string 1) directory))
48919e0f
RS
538 (if mail-directory-parser
539 (setq directory (funcall mail-directory-parser))
540 (while (not (eobp))
daca8ba5
SM
541 (push (buffer-substring (point)
542 (progn
543 (forward-line)
544 (if (bolp)
545 (1- (point))
546 (point))))
547 directory))))
48919e0f
RS
548 (kill-buffer (current-buffer))
549 (message "Querying directory...done")
550 directory)))
551
daca8ba5 552(defvar mailalias-done)
48919e0f 553
daca8ba5 554(defun mail-directory-process (prefix)
31407d5d 555 "Run a shell command to output names in directory.
48919e0f 556See `mail-directory-process'."
f6250436 557 (when (consp mail-directory-process)
daca8ba5
SM
558 (let ((pattern prefix)) ;Dynbind!
559 (apply 'call-process (eval (car mail-directory-process)) nil t nil
560 (mapcar 'eval (cdr mail-directory-process))))))
48919e0f
RS
561
562;; This should handle a dialog. Currently expects port to spit out names.
daca8ba5 563(defun mail-directory-stream (prefix)
48919e0f
RS
564 "Open a stream to retrieve names in directory.
565See `mail-directory-stream'."
daca8ba5
SM
566 (let ((mailalias-done nil)
567 (pattern prefix)) ;Dynbind!
48919e0f
RS
568 (set-process-sentinel
569 (apply 'open-network-stream "mailalias" (current-buffer)
570 mail-directory-stream)
daca8ba5 571 (lambda (_x _y)
48919e0f
RS
572 (setq mailalias-done t)))
573 (while (not mailalias-done)
574 (sit-for .1))))
575
576(defun mail-sentto-newsgroups ()
577 "Return all entries from Newsgroups: header as completion alist."
578 (save-excursion
579 (if (mail-position-on-field "newsgroups" t)
580 (let ((point (point))
581 list)
582 (while (< (skip-chars-backward "^:, \t\n") 0)
583 (setq list `((,(buffer-substring (point) point))
584 ,@list))
585 (skip-chars-backward ", \t\n")
586 (setq point (point)))
587 list))))
6594deb0 588
e8a57935
JB
589(provide 'mailalias)
590
6594deb0 591;;; mailalias.el ends here