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, |
d7a0267c | 4 | ;; 2004, 2005, 2006, 2007 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 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
ceaeecb0 | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
80a677d9 JA |
14 | ;; any later version. |
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 | |
b578f267 | 22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
3a35cf56 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
80a677d9 | 25 | |
e41b2db1 ER |
26 | ;;; Commentary: |
27 | ||
28 | ;; Basic functions for defining and expanding mail aliases. | |
29 | ;; These seal off the interface to the alias-definition parts of a | |
30 | ;; .mailrc file formatted for BSD's Mail or USL's mailx. | |
31 | ||
e5167999 | 32 | ;;; Code: |
80a677d9 | 33 | |
594906dd | 34 | (require 'sendmail) |
c2db7f31 | 35 | |
31407d5d | 36 | (defgroup mailalias nil |
f5684faf | 37 | "Expanding mail aliases." |
31407d5d | 38 | :group 'mail) |
48919e0f | 39 | |
31407d5d RS |
40 | (defcustom mail-passwd-files '("/etc/passwd") |
41 | "*List of files from which to determine valid user names." | |
42 | :type '(repeat string) | |
43 | :group 'mailalias) | |
8e3beae0 | 44 | |
31407d5d RS |
45 | (defcustom mail-passwd-command nil |
46 | "*Shell command to retrieve text to add to `/etc/passwd', or nil." | |
47 | :type '(choice string (const nil)) | |
48 | :group 'mailalias) | |
8e3beae0 | 49 | |
48919e0f RS |
50 | (defvar mail-directory-names t |
51 | "Alist of mail address directory entries. | |
52 | When t this still needs to be initialized.") | |
53 | ||
54 | (defvar mail-address-field-regexp | |
55 | "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):") | |
56 | ||
31407d5d | 57 | (defcustom mail-complete-alist |
5695bf0c RS |
58 | ;; Don't use backquote here; we don't want backquote to get loaded |
59 | ;; just because of loading this file. | |
310d0d40 RS |
60 | ;; Don't refer to mail-address-field-regexp here; |
61 | ;; that confuses some things such as cus-dep.el. | |
62 | (cons '("^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):" | |
63 | . (mail-get-names pattern)) | |
5695bf0c RS |
64 | '(("Newsgroups:" . (if (boundp 'gnus-active-hashtb) |
65 | gnus-active-hashtb | |
66 | (if (boundp news-group-article-assoc) | |
67 | news-group-article-assoc))) | |
68 | ("Followup-To:" . (mail-sentto-newsgroups)) | |
69 | ;;("Distribution:" ???) | |
70 | )) | |
31407d5d RS |
71 | "*Alist of header field and expression to return alist for completion. |
72 | The expression may reference the variable `pattern' | |
73 | which will hold the string being completed. | |
74 | If not on matching header, `mail-complete-function' gets called instead." | |
bd09e573 | 75 | :type 'alist |
31407d5d RS |
76 | :group 'mailalias) |
77 | (put 'mail-complete-alist 'risky-local-variable t) | |
48919e0f | 78 | |
e694581d | 79 | ;;;###autoload |
31407d5d | 80 | (defcustom mail-complete-style 'angles |
e694581d RS |
81 | "*Specifies how \\[mail-complete] formats the full name when it completes. |
82 | If `nil', they contain just the return address like: | |
83 | king@grassland.com | |
84 | If `parens', they look like: | |
85 | king@grassland.com (Elvis Parsley) | |
86 | If `angles', they look like: | |
31407d5d RS |
87 | Elvis Parsley <king@grassland.com>" |
88 | :type '(choice (const angles) (const parens) (const nil)) | |
89 | :group 'mailalias) | |
48919e0f | 90 | |
31407d5d RS |
91 | (defcustom mail-complete-function 'ispell-complete-word |
92 | "*Function to call when completing outside `mail-complete-alist'-header." | |
93 | :type '(choice function (const nil)) | |
94 | :group 'mailalias) | |
48919e0f | 95 | |
31407d5d | 96 | (defcustom mail-directory-function nil |
25345117 | 97 | "*Function to get completions from directory service or nil for none. |
31407d5d RS |
98 | See `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 |
25345117 | 104 | "*When non-nil call `mail-directory-function' for each completion. |
48919e0f | 105 | In that case, one argument gets passed to the function, the partial string |
31407d5d RS |
106 | entered so far." |
107 | :type 'boolean | |
108 | :group 'mailalias) | |
48919e0f | 109 | |
31407d5d RS |
110 | (defcustom mail-directory-process nil |
111 | "*Shell command to get the list of names from a mail directory. | |
112 | This value is used when the value of `mail-directory-function' | |
113 | is `mail-directory-process'. The value should be a list | |
114 | of the form (COMMAND ARG ...), where each of the list elements | |
f6250436 RS |
115 | is evaluated. COMMAND should evaluate to a string. When |
116 | `mail-directory-requery' is non-nil, during evaluation of these | |
117 | elements, the variable `pattern' contains the partial input being | |
118 | completed. `pattern' is nil when `mail-directory-requery' is nil. | |
48919e0f | 119 | |
31407d5d | 120 | The value might look like this: |
48919e0f RS |
121 | |
122 | '(remote-shell-program \"HOST\" \"-nl\" \"USER\" \"COMMAND\") | |
123 | ||
31407d5d | 124 | or 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 RS |
131 | (defcustom mail-directory-stream nil |
132 | "*List of (HOST SERVICE) for stream connection to mail directory." | |
133 | :type 'sexp | |
134 | :group 'mailalias) | |
135 | (put 'mail-directory-stream 'risky-local-variable t) | |
48919e0f | 136 | |
31407d5d RS |
137 | (defcustom mail-directory-parser nil |
138 | "*How to interpret the output of `mail-directory-function'. | |
48919e0f RS |
139 | Three 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. | |
152 | Elements have the form (MAILNAME) or (MAILNAME . FULLNAME). | |
153 | If the value means t, it means the real value should be calculated | |
f6250436 | 154 | for the next use. This is used in `mail-complete'.") |
31407d5d RS |
155 | |
156 | (defvar mail-local-names t | |
157 | "Alist of local users. | |
158 | When 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 | 166 | If interactive, expand in header fields. |
1abe8488 KH |
167 | Suitable header fields are `To', `From', `CC' and `BCC', `Reply-to', and |
168 | their `Resent-' variants. | |
169 | ||
96846422 RS |
170 | Optional second arg EXCLUDE may be a regular expression defining text to be |
171 | removed 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)) | |
212 | (setq epos (marker-position end1) seplen 0)) | |
213 | (let ((string (buffer-substring-no-properties pos epos)) | |
214 | translation) | |
215 | (if (and (not (assoc string disabled-aliases)) | |
216 | (setq translation (cdr (assoc string mail-aliases)))) | |
217 | (progn | |
218 | ;; This name is an alias. Disable it. | |
219 | (setq disabled-aliases (cons (cons string (- end1 epos)) | |
220 | disabled-aliases)) | |
221 | ;; Replace the alias with its expansion | |
222 | ;; then rescan the expansion for more aliases. | |
223 | (goto-char pos) | |
224 | (insert translation) | |
225 | (when exclude | |
226 | (let ((regexp (concat "\\b\\(" exclude "\\)\\b")) | |
80a677d9 JA |
227 | (end (point-marker))) |
228 | (goto-char pos) | |
229 | (while (re-search-forward regexp end t) | |
230 | (replace-match "")) | |
231 | (goto-char end))) | |
e28449ed SM |
232 | (delete-region (point) (+ (point) (- epos pos))) |
233 | (goto-char pos)) | |
234 | ;; Name is not an alias. Skip to start of next name. | |
235 | (goto-char epos) | |
236 | (forward-char seplen)))) | |
237 | (set-marker end1 nil))) | |
238 | (set-marker end nil)))) | |
80a677d9 | 239 | |
9d73ab0d NF |
240 | ;; Called by mail-setup, or similar functions, only if the file specified |
241 | ;; by mail-personal-alias-file (usually `~/.mailrc') exists. | |
80a677d9 | 242 | (defun build-mail-aliases (&optional file) |
9d73ab0d NF |
243 | "Read mail aliases from personal aliases file and set `mail-aliases'. |
244 | By default, this is the file specified by `mail-personal-alias-file'." | |
245 | (setq file (expand-file-name (or file mail-personal-alias-file))) | |
3c3d71d9 RS |
246 | ;; In case mail-aliases is t, make sure define-mail-alias |
247 | ;; does not recursively call build-mail-aliases. | |
248 | (setq mail-aliases nil) | |
80a677d9 JA |
249 | (let ((buffer nil) |
250 | (obuf (current-buffer))) | |
251 | (unwind-protect | |
252 | (progn | |
48919e0f | 253 | (setq buffer (generate-new-buffer " mailrc")) |
80a677d9 | 254 | (set-buffer buffer) |
3c55fda0 RS |
255 | (while file |
256 | (cond ((get-file-buffer file) | |
257 | (insert (save-excursion | |
258 | (set-buffer (get-file-buffer file)) | |
599d82c8 RS |
259 | (buffer-substring-no-properties |
260 | (point-min) (point-max))))) | |
3c55fda0 RS |
261 | ((file-exists-p file) (insert-file-contents file)) |
262 | ((file-exists-p (setq file (concat "~/" file))) | |
263 | (insert-file-contents file)) | |
264 | (t (setq file nil))) | |
1296b4cd RS |
265 | (goto-char (point-min)) |
266 | ;; Delete comments from the contents. | |
267 | (while (search-forward "# " nil t) | |
268 | (let ((p (- (point) 2))) | |
269 | (end-of-line) | |
270 | (delete-region p (point)))) | |
3c55fda0 RS |
271 | ;; Don't lose if no final newline. |
272 | (goto-char (point-max)) | |
273 | (or (eq (preceding-char) ?\n) (newline)) | |
274 | (goto-char (point-min)) | |
275 | ;; handle "\\\n" continuation lines | |
276 | (while (not (eobp)) | |
277 | (end-of-line) | |
278 | (if (= (preceding-char) ?\\) | |
279 | (progn (delete-char -1) (delete-char 1) (insert ?\ )) | |
80a677d9 | 280 | (forward-char 1))) |
3c55fda0 RS |
281 | (goto-char (point-min)) |
282 | ;; handle `source' directives -- Eddy/1994/May/25 | |
283 | (cond ((re-search-forward "^source[ \t]+" nil t) | |
284 | (re-search-forward "\\S-+") | |
599d82c8 RS |
285 | (setq file (buffer-substring-no-properties |
286 | (match-beginning 0) (match-end 0))) | |
3c55fda0 RS |
287 | (beginning-of-line) |
288 | (insert "# ") ; to ensure we don't re-process this file | |
289 | (beginning-of-line)) | |
290 | (t (setq file nil)))) | |
80a677d9 | 291 | (goto-char (point-min)) |
d9817d8c | 292 | (while (re-search-forward |
c051dfe5 | 293 | "^\\(a\\|alias\\|g\\|group\\)[ \t]+\\([^ \t\n]+\\)" nil t) |
d9817d8c | 294 | (let* ((name (match-string 2)) |
c051dfe5 KH |
295 | (start (progn (skip-chars-forward " \t") (point))) |
296 | value) | |
80a677d9 | 297 | (end-of-line) |
c051dfe5 KH |
298 | (setq value (buffer-substring-no-properties start (point))) |
299 | (unless (equal value "") | |
300 | (define-mail-alias name value t)))) | |
80a677d9 JA |
301 | mail-aliases) |
302 | (if buffer (kill-buffer buffer)) | |
303 | (set-buffer obuf)))) | |
304 | ||
305 | ;; Always autoloadable in case the user wants to define aliases | |
306 | ;; interactively or in .emacs. | |
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 | 310 | This means that sending a message to NAME will actually send to DEFINITION. |
fa786521 RS |
311 | |
312 | Normally, the addresses in DEFINITION must be separated by commas. | |
a1506d29 | 313 | If FROM-MAILRC-FILE is non-nil, then addresses in DEFINITION |
fa786521 RS |
314 | can be separated by spaces; an address can contain spaces |
315 | if 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)))) |
37e379dd | 332 | (let ((result '()) |
cc4b6c02 RS |
333 | ;; If DEFINITION is null string, avoid looping even once. |
334 | (start (and (not (equal definition "")) 0)) | |
37e379dd | 335 | (L (length definition)) |
255359cb | 336 | convert-backslash |
37e379dd RS |
337 | end tem) |
338 | (while start | |
255359cb | 339 | (setq convert-backslash nil) |
37e379dd RS |
340 | ;; If we're reading from the mailrc file, then addresses are delimited |
341 | ;; by spaces, and addresses with embedded spaces must be surrounded by | |
342 | ;; double-quotes. Otherwise, addresses are separated by commas. | |
343 | (if from-mailrc-file | |
344 | (if (eq ?\" (aref definition start)) | |
8332fa80 RS |
345 | ;; The following test on `found' compensates for a bug |
346 | ;; in match-end, which does not return nil when match | |
347 | ;; failed. | |
348 | (let ((found (string-match "[^\\]\\(\\([\\][\\]\\)*\\)\"[ \t,]*" | |
349 | definition start))) | |
350 | (setq start (1+ start) | |
351 | end (and found (match-end 1)) | |
352 | convert-backslash t)) | |
cc4b6c02 RS |
353 | (setq end (string-match "[ \t,]+" definition start))) |
354 | (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start))) | |
255359cb RS |
355 | (let ((temp (substring definition start end)) |
356 | (pos 0)) | |
357 | (setq start (and end | |
358 | (/= (match-end 0) L) | |
359 | (match-end 0))) | |
360 | (if convert-backslash | |
361 | (while (string-match "[\\]" temp pos) | |
362 | (setq temp (replace-match "" t t temp)) | |
a1506d29 | 363 | (if start |
255359cb RS |
364 | (setq start (1- start))) |
365 | (setq pos (match-end 0)))) | |
366 | (setq result (cons temp result)))) | |
37e379dd RS |
367 | (setq definition (mapconcat (function identity) |
368 | (nreverse result) | |
369 | ", ")) | |
80a677d9 JA |
370 | (setq tem (assoc name mail-aliases)) |
371 | (if tem | |
372 | (rplacd tem definition) | |
48919e0f RS |
373 | (setq mail-aliases (cons (cons name definition) mail-aliases) |
374 | mail-names t)))) | |
375 | ||
c0fd4267 | 376 | ;;;###autoload |
48919e0f RS |
377 | (defun mail-complete (arg) |
378 | "Perform completion on header field or word preceding point. | |
379 | Completable headers are according to `mail-complete-alist'. If none matches | |
380 | current header, calls `mail-complete-function' and passes prefix arg if any." | |
381 | (interactive "P") | |
9a03a6f4 SM |
382 | ;; Read the defaults first, if we have not done so. |
383 | (sendmail-sync-aliases) | |
384 | (if (eq mail-aliases t) | |
385 | (progn | |
386 | (setq mail-aliases nil) | |
387 | (if (file-exists-p mail-personal-alias-file) | |
388 | (build-mail-aliases)))) | |
48919e0f | 389 | (let ((list mail-complete-alist)) |
baaddce7 | 390 | (if (and (< 0 (mail-header-end)) |
48919e0f RS |
391 | (save-excursion |
392 | (if (re-search-backward "^[^\t]" nil t) | |
393 | (while list | |
394 | (if (looking-at (car (car list))) | |
395 | (setq arg (cdr (car list)) | |
396 | list ()) | |
397 | (setq list (cdr list))))) | |
398 | arg)) | |
399 | (let* ((end (point)) | |
400 | (beg (save-excursion | |
401 | (skip-chars-backward "^ \t<,:") | |
402 | (point))) | |
403 | (pattern (buffer-substring beg end)) | |
404 | completion) | |
405 | (setq list (eval arg) | |
406 | completion (try-completion pattern list)) | |
407 | (cond ((eq completion t)) | |
408 | ((null completion) | |
409 | (message "Can't find completion for \"%s\"" pattern) | |
410 | (ding)) | |
411 | ((not (string= pattern completion)) | |
412 | (delete-region beg end) | |
e694581d RS |
413 | (let ((alist-elt (assoc completion mail-names))) |
414 | (if (cdr alist-elt) | |
415 | (cond ((eq mail-complete-style 'parens) | |
416 | (insert completion " (" (cdr alist-elt) ")")) | |
417 | ((eq mail-complete-style 'angles) | |
418 | (insert (cdr alist-elt) " <" completion ">")) | |
419 | (t | |
420 | (insert completion))) | |
421 | (insert completion)))) | |
48919e0f RS |
422 | (t |
423 | (message "Making completion list...") | |
424 | (with-output-to-temp-buffer "*Completions*" | |
425 | (display-completion-list | |
426 | (all-completions pattern list))) | |
427 | (message "Making completion list...%s" "done")))) | |
428 | (funcall mail-complete-function arg)))) | |
429 | ||
430 | (defun mail-get-names (pattern) | |
e694581d | 431 | "Fetch local users and global mail addresses for completion. |
48919e0f | 432 | Consults `/etc/passwd' and a directory service if one is set up via |
e694581d RS |
433 | `mail-directory-function'. |
434 | PATTERN is the string we want to complete." | |
48919e0f RS |
435 | (if (eq mail-local-names t) |
436 | (save-excursion | |
437 | (set-buffer (generate-new-buffer " passwd")) | |
8e3beae0 RS |
438 | (let ((files mail-passwd-files)) |
439 | (while files | |
440 | (insert-file-contents (car files) nil nil nil t) | |
441 | (setq files (cdr files)))) | |
442 | (if mail-passwd-command | |
443 | (call-process shell-file-name nil t nil | |
444 | shell-command-switch mail-passwd-command)) | |
637502c3 | 445 | (goto-char (point-min)) |
8e3beae0 | 446 | (setq mail-local-names nil) |
48919e0f | 447 | (while (not (eobp)) |
8bc49f8f RS |
448 | ;;Recognize lines like |
449 | ;; nobody:*:65534:65534::/: | |
450 | ;; +demo::::::/bin/csh | |
451 | ;; +ethanb | |
452 | ;;while skipping | |
453 | ;; +@SOFTWARE | |
e694581d RS |
454 | ;; The second \(...\) matches the user id. |
455 | (if (looking-at "\\+?\\([^:@\n+]+\\):[^:\n]*:\\([^\n:]*\\):") | |
456 | (add-to-list 'mail-local-names | |
457 | (cons (match-string 1) | |
458 | (user-full-name | |
027a4b6b | 459 | (string-to-number (match-string 2)))))) |
48919e0f RS |
460 | (beginning-of-line 2)) |
461 | (kill-buffer (current-buffer)))) | |
462 | (if (or (eq mail-names t) | |
e694581d | 463 | (eq mail-directory-names t)) |
48919e0f RS |
464 | (let (directory) |
465 | (and mail-directory-function | |
466 | (eq mail-directory-names t) | |
467 | (setq directory | |
468 | (mail-directory (if mail-directory-requery pattern)))) | |
e694581d RS |
469 | (or mail-directory-requery |
470 | (setq mail-directory-names directory)) | |
48919e0f RS |
471 | (if (or directory |
472 | (eq mail-names t)) | |
473 | (setq mail-names | |
e694581d RS |
474 | (sort (append (if (consp mail-aliases) |
475 | (mapcar | |
476 | (function (lambda (a) (list (car a)))) | |
477 | mail-aliases)) | |
48919e0f RS |
478 | (if (consp mail-local-names) |
479 | mail-local-names) | |
f6250436 RS |
480 | (or directory |
481 | (when (consp mail-directory-names) | |
482 | mail-directory-names))) | |
48919e0f RS |
483 | (lambda (a b) |
484 | ;; should cache downcased strings | |
485 | (string< (downcase (car a)) | |
e694581d | 486 | (downcase (car b))))))))) |
48919e0f RS |
487 | mail-names) |
488 | ||
489 | ||
490 | (defun mail-directory (pattern) | |
f6250436 RS |
491 | "Use mail-directory facility to get user names matching PATTERN. |
492 | If PATTERN is nil, get all the defined user names. | |
493 | This function calls `mail-directory-function' to query the directory, | |
494 | then uses `mail-directory-parser' to parse the output it returns." | |
48919e0f RS |
495 | (save-excursion |
496 | (message "Querying directory...") | |
497 | (set-buffer (generate-new-buffer " *mail-directory*")) | |
498 | (funcall mail-directory-function pattern) | |
499 | (goto-char 1) | |
500 | (let (directory) | |
501 | (if (stringp mail-directory-parser) | |
502 | (while (re-search-forward mail-directory-parser nil t) | |
503 | (setq directory | |
e694581d | 504 | (cons (match-string 1) directory))) |
48919e0f RS |
505 | (if mail-directory-parser |
506 | (setq directory (funcall mail-directory-parser)) | |
507 | (while (not (eobp)) | |
508 | (setq directory | |
e694581d RS |
509 | (cons (buffer-substring (point) |
510 | (progn | |
511 | (forward-line) | |
512 | (if (bolp) | |
513 | (1- (point)) | |
514 | (point)))) | |
515 | directory))))) | |
48919e0f RS |
516 | (kill-buffer (current-buffer)) |
517 | (message "Querying directory...done") | |
518 | directory))) | |
519 | ||
520 | ||
521 | (defun mail-directory-process (pattern) | |
31407d5d | 522 | "Run a shell command to output names in directory. |
48919e0f | 523 | See `mail-directory-process'." |
f6250436 RS |
524 | (when (consp mail-directory-process) |
525 | (apply 'call-process (eval (car mail-directory-process)) nil t nil | |
526 | (mapcar 'eval (cdr mail-directory-process))))) | |
48919e0f RS |
527 | |
528 | ;; This should handle a dialog. Currently expects port to spit out names. | |
529 | (defun mail-directory-stream (pattern) | |
530 | "Open a stream to retrieve names in directory. | |
531 | See `mail-directory-stream'." | |
532 | (let (mailalias-done) | |
533 | (set-process-sentinel | |
534 | (apply 'open-network-stream "mailalias" (current-buffer) | |
535 | mail-directory-stream) | |
906f211e | 536 | (lambda (x y) |
48919e0f RS |
537 | (setq mailalias-done t))) |
538 | (while (not mailalias-done) | |
539 | (sit-for .1)))) | |
540 | ||
541 | (defun mail-sentto-newsgroups () | |
542 | "Return all entries from Newsgroups: header as completion alist." | |
543 | (save-excursion | |
544 | (if (mail-position-on-field "newsgroups" t) | |
545 | (let ((point (point)) | |
546 | list) | |
547 | (while (< (skip-chars-backward "^:, \t\n") 0) | |
548 | (setq list `((,(buffer-substring (point) point)) | |
549 | ,@list)) | |
550 | (skip-chars-backward ", \t\n") | |
551 | (setq point (point))) | |
552 | list)))) | |
6594deb0 | 553 | |
e8a57935 JB |
554 | (provide 'mailalias) |
555 | ||
ab5796a9 | 556 | ;;; arch-tag: 1d6a0f87-eb34-4d45-8816-60c1b952cf46 |
6594deb0 | 557 | ;;; mailalias.el ends here |