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