+ (setq mail-aliases (cons (cons name definition) mail-aliases)
+ mail-names t))))
+
+;;;###autoload
+(defun mail-complete (arg)
+ "Perform completion on header field or word preceding point.
+Completable headers are according to `mail-complete-alist'. If none matches
+current header, calls `mail-complete-function' and passes prefix arg if any."
+ (interactive "P")
+ ;; Read the defaults first, if we have not done so.
+ (sendmail-sync-aliases)
+ (if (eq mail-aliases t)
+ (progn
+ (setq mail-aliases nil)
+ (if (file-exists-p mail-personal-alias-file)
+ (build-mail-aliases))))
+ (let ((list mail-complete-alist))
+ (if (and (< 0 (mail-header-end))
+ (save-excursion
+ (if (re-search-backward "^[^\t]" nil t)
+ (while list
+ (if (looking-at (car (car list)))
+ (setq arg (cdr (car list))
+ list ())
+ (setq list (cdr list)))))
+ arg))
+ (let* ((end (point))
+ (beg (save-excursion
+ (skip-chars-backward "^ \t<,:")
+ (point)))
+ (pattern (buffer-substring beg end))
+ completion)
+ (setq list (eval arg)
+ completion (try-completion pattern list))
+ (cond ((eq completion t))
+ ((null completion)
+ (message "Can't find completion for \"%s\"" pattern)
+ (ding))
+ ((not (string= pattern completion))
+ (delete-region beg end)
+ (let ((alist-elt (assoc completion mail-names)))
+ (if (cdr alist-elt)
+ (cond ((eq mail-complete-style 'parens)
+ (insert completion " (" (cdr alist-elt) ")"))
+ ((eq mail-complete-style 'angles)
+ (insert (cdr alist-elt) " <" completion ">"))
+ (t
+ (insert completion)))
+ (insert completion))))
+ (t
+ (message "Making completion list...")
+ (with-output-to-temp-buffer "*Completions*"
+ (display-completion-list
+ (all-completions pattern list)))
+ (message "Making completion list...%s" "done"))))
+ (funcall mail-complete-function arg))))
+
+(defun mail-get-names (pattern)
+ "Fetch local users and global mail addresses for completion.
+Consults `/etc/passwd' and a directory service if one is set up via
+`mail-directory-function'.
+PATTERN is the string we want to complete."
+ (if (eq mail-local-names t)
+ (save-excursion
+ (set-buffer (generate-new-buffer " passwd"))
+ (let ((files mail-passwd-files))
+ (while files
+ (insert-file-contents (car files) nil nil nil t)
+ (setq files (cdr files))))
+ (if mail-passwd-command
+ (call-process shell-file-name nil t nil
+ shell-command-switch mail-passwd-command))
+ (beginning-of-buffer)
+ (setq mail-local-names nil)
+ (while (not (eobp))
+ ;;Recognize lines like
+ ;; nobody:*:65534:65534::/:
+ ;; +demo::::::/bin/csh
+ ;; +ethanb
+ ;;while skipping
+ ;; +@SOFTWARE
+ ;; The second \(...\) matches the user id.
+ (if (looking-at "\\+?\\([^:@\n+]+\\):[^:\n]*:\\([^\n:]*\\):")
+ (add-to-list 'mail-local-names
+ (cons (match-string 1)
+ (user-full-name
+ (string-to-int (match-string 2))))))
+ (beginning-of-line 2))
+ (kill-buffer (current-buffer))))
+ (if (or (eq mail-names t)
+ (eq mail-directory-names t))
+ (let (directory)
+ (and mail-directory-function
+ (eq mail-directory-names t)
+ (setq directory
+ (mail-directory (if mail-directory-requery pattern))))
+ (or mail-directory-requery
+ (setq mail-directory-names directory))
+ (if (or directory
+ (eq mail-names t))
+ (setq mail-names
+ (sort (append (if (consp mail-aliases)
+ (mapcar
+ (function (lambda (a) (list (car a))))
+ mail-aliases))
+ (if (consp mail-local-names)
+ mail-local-names)
+ (or directory mail-directory-names))
+ (lambda (a b)
+ ;; should cache downcased strings
+ (string< (downcase (car a))
+ (downcase (car b)))))))))
+ mail-names)
+
+
+(defun mail-directory (pattern)
+ "Call directory to get names matching PATTERN or all if nil.
+Calls `mail-directory-function' and applies `mail-directory-parser' to output."
+ (save-excursion
+ (message "Querying directory...")
+ (set-buffer (generate-new-buffer " *mail-directory*"))
+ (funcall mail-directory-function pattern)
+ (goto-char 1)
+ (let (directory)
+ (if (stringp mail-directory-parser)
+ (while (re-search-forward mail-directory-parser nil t)
+ (setq directory
+ (cons (match-string 1) directory)))
+ (if mail-directory-parser
+ (setq directory (funcall mail-directory-parser))
+ (while (not (eobp))
+ (setq directory
+ (cons (buffer-substring (point)
+ (progn
+ (forward-line)
+ (if (bolp)
+ (1- (point))
+ (point))))
+ directory)))))
+ (kill-buffer (current-buffer))
+ (message "Querying directory...done")
+ directory)))
+
+
+(defun mail-directory-process (pattern)
+ "Run a shell command to output names in directory.
+See `mail-directory-process'."
+ (apply 'call-process (eval (car mail-directory-process)) nil t nil
+ (mapcar 'eval (cdr mail-directory-process))))
+
+;; This should handle a dialog. Currently expects port to spit out names.
+(defun mail-directory-stream (pattern)
+ "Open a stream to retrieve names in directory.
+See `mail-directory-stream'."
+ (let (mailalias-done)
+ (set-process-sentinel
+ (apply 'open-network-stream "mailalias" (current-buffer)
+ mail-directory-stream)
+ (lambda (x y)
+ (setq mailalias-done t)))
+ (while (not mailalias-done)
+ (sit-for .1))))
+
+(defun mail-sentto-newsgroups ()
+ "Return all entries from Newsgroups: header as completion alist."
+ (save-excursion
+ (if (mail-position-on-field "newsgroups" t)
+ (let ((point (point))
+ list)
+ (while (< (skip-chars-backward "^:, \t\n") 0)
+ (setq list `((,(buffer-substring (point) point))
+ ,@list))
+ (skip-chars-backward ", \t\n")
+ (setq point (point)))
+ list))))