Function: define-mail-alias

define-mail-alias is an autoloaded, interactive and byte-compiled function defined in mailalias.el.gz.

Signature

(define-mail-alias NAME DEFINITION &optional FROM-MAILRC-FILE)

Documentation

Define NAME as a mail alias that translates to DEFINITION.

This means that sending a message to NAME will actually send to DEFINITION.

Normally, the addresses in DEFINITION must be separated by commas. If FROM-MAILRC-FILE is non-nil, then addresses in DEFINITION can be separated by spaces; an address can contain spaces if it is quoted with double-quotes.

Probably introduced at or before Emacs version 19.14.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/mail/mailalias.el.gz
;; Always autoloadable in case the user wants to define aliases
;; interactively or in .emacs.
;; define-mail-abbrev in mailabbrev.el duplicates much of this code.
;;;###autoload
(defun define-mail-alias (name definition &optional from-mailrc-file)
  "Define NAME as a mail alias that translates to DEFINITION.
This means that sending a message to NAME will actually send to DEFINITION.

Normally, the addresses in DEFINITION must be separated by commas.
If FROM-MAILRC-FILE is non-nil, then addresses in DEFINITION
can be separated by spaces; an address can contain spaces
if it is quoted with double-quotes."

  (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
  ;; Read the defaults first, if we have not done so.
  ;; But not if we are doing that already right now.
  (unless from-mailrc-file
    (sendmail-sync-aliases))
  (if (eq mail-aliases t)
      (progn
	(setq mail-aliases nil)
	(if (file-exists-p mail-personal-alias-file)
	    (build-mail-aliases))))
  ;; strip garbage from front and end
  (if (string-match "\\`[ \t\n,]+" definition)
      (setq definition (substring definition (match-end 0))))
  (if (string-match "[ \t\n,]+\\'" definition)
      (setq definition (substring definition 0 (match-beginning 0))))

  (let* ((L (length definition))
	 (start (if (> L 0) 0))
	 end this-entry result tem)
    (while start
      (cond
       (from-mailrc-file
	;; If we're reading from the mailrc file, addresses are
	;; delimited by spaces, and addresses with embedded spaces are
	;; surrounded by non-escaped double-quotes.
	(if (eq ?\" (aref definition start))
	    (setq start (1+ start)
		  end (and (string-match
			    "[^\\]\\(\\([\\][\\]\\)*\\)\"[ \t,]*"
			    definition start)
			   (match-end 1)))
	  (setq end (string-match "[ \t,]+" definition start)))
	;; Extract the address and advance the loop past it.
	(setq this-entry (substring definition start end)
	      start (and end (/= (match-end 0) L) (match-end 0)))
	;; If the full name contains a problem character, quote it.
	(and (string-match "\\(.+?\\)[ \t]*\\(<.*>\\)" this-entry)
	     (string-match "[^- !#$%&'*+/0-9=?A-Za-z^_`{|}~]"
			   (match-string 1 this-entry))
	     (setq this-entry (replace-regexp-in-string
			       "\\(.+?\\)[ \t]*\\(<.*>\\)"
			       "\"\\1\" \\2"
			       this-entry))))
       ;; When we are not reading from .mailrc, addresses are
       ;; separated by commas.  Try to accept a rfc822-like syntax.
       ;; (Todo: extend rfc822.el to do the work for us.)
       ((equal (string-match
		"[ \t,]*\\(\"\\(?:[^\"]\\|[^\\]\\(?:[\\][\\]\\)*\"\\)*\"[ \t]*\
<[-.!#$%&'*+/0-9=?A-Za-z^_`{|}~@]+>\\)[ \t,]*"
		definition start)
	       start)
	;; If an entry has a valid [ "foo bar" <foo@example.com> ]
	;; form, use it literally .  This also allows commas in the
	;; quoted string, e.g.  [ "foo bar, jr" <foo@example.com> ]
	(setq this-entry (match-string 1 definition)
	      start (and (/= (match-end 0) L) (match-end 0))))
       (t
	;; Otherwise, read the next address by looking for a comma.
	(setq end (string-match "[ \t\n,]*,[ \t\n]*" definition start))
	(setq this-entry (substring definition start end))
	;; Advance the loop past this address.
	(setq start (and end (/= (match-end 0) L) (match-end 0)))
	;; If the full name contains a problem character, quote it.
	(and (string-match "\\(.+?\\)[ \t]*\\(<.*>\\)" this-entry)
	     (string-match "[^- !#$%&'*+/0-9=?A-Za-z^_`{|}~]"
			   (match-string 1 this-entry))
	     (setq this-entry (replace-regexp-in-string
			       "\\(.+?\\)[ \t]*\\(<.*>\\)" "\"\\1\" \\2"
			       this-entry)))))
      (push this-entry result))

    (setq definition (mapconcat (function identity)
				(nreverse result) ", "))
    (setq tem (assoc name mail-aliases))
    (if tem
	(rplacd tem definition)
      (setq mail-aliases (cons (cons name definition) mail-aliases)
	    mail-names t))))