Function: mail-strip-quoted-names

mail-strip-quoted-names is a byte-compiled function defined in mail-utils.el.gz.

Signature

(mail-strip-quoted-names ADDRESS)

Documentation

Delete comments and quoted strings in an address list ADDRESS.

Also delete leading/trailing whitespace and replace FOO <BAR> with just BAR. Return a modified address list.

Source Code

;; Defined in /usr/src/emacs/lisp/mail/mail-utils.el.gz
(defun mail-strip-quoted-names (address)
  "Delete comments and quoted strings in an address list ADDRESS.
Also delete leading/trailing whitespace and replace FOO <BAR> with just BAR.
Return a modified address list."
  (when address
    (if mail-use-rfc822
	(mapconcat #'identity (rfc822-addresses address) ", ")
      (let (pos)

        ;; Strip comments.
        (while (setq pos (string-match
                          "[ \t]*(\\([^()\\]\\|\\\\.\\|\\\\\n\\)*)"
                          address))
          (setq address (replace-match "" nil nil address 0)))

        ;; strip surrounding whitespace
        (string-match "\\`[ \t\n]*" address)
        (setq address (substring address
                                 (match-end 0)
                                 (string-match "[ \t\n]*\\'" address
                                               (match-end 0))))

        ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
        (setq pos 0)
        (while (setq pos (string-match
                          "\\([ \t]?\\)\\([ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*\\)"
			  address pos))
          ;; If the next thing is "@", we have "foo bar"@host.  Leave it.
          (if (and (> (length address) (match-end 0))
                   (= (aref address (match-end 0)) ?@))
              (setq pos (match-end 0))
            ;; Otherwise discard the "..." part.
            (setq address (replace-match "" nil nil address 2))))
        ;; If this address contains <...>, replace it with just
        ;; the part between the <...>.
        (while (setq pos (string-match "\\(,\\s-*\\|\\`\\)\\([^,]*<\\([^>,:]*\\)>[^,]*\\)\\(\\s-*,\\|\\'\\)"
                                       address))
          (setq address (replace-match (match-string 3 address)
                                       nil 'literal address 2)))
        address))))