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