(defcustom notmuch-address-completion-style 'completion-at-point
  "Select the framework to use for email address completion.

This variable determines which of the completion frameworks to
use in the header area in `notmuch-message-mode' for completing
email addresses.

When set to `completion-at-point' (the default), in-buffer
completion (i.e. where the point is) is used.  This will trigger
any third party package(s) providing a user interface on top of
`completion-at-point'.

When set to `completing-read', completion candidates will be
offered in the minibuffer using `completing-read'.  This will
trigger any third party package(s) providing a user interface on
top of `completing-read'."
  :type '(choice (const :tag "in-buffer (completion-at-point)" completion-at-point)
                 (const :tag "minibuffer (completing-read)"    completing-read))
  :group 'notmuch-send
  :group 'notmuch-address
  :group 'notmuch-external)

(make-obsolete-variable notmuch-address-use-company
                        "`company' support has been removed from
notumuch-address. To continue using `company', set
`notmuch-address-completion-style' to 'completion-at-point, and
configure `company' to use the `company-capf' backend in
`message-mode'."
                        "0.35")

(defun notmuch-address-complete-at-point (beg end orig)
  "Provide a completion table for email address completion candidates.

This function is intended to be hooked into
`message-completion-alist' with an appropriate regular expression
matching message headers consisting of email recipients.

It returns a completion table as is expected for functions listed
in `completion-at-point-functions'."
  (setq-local completion-styles '(substring flex))
  (list beg end
        (completion-table-dynamic
         (lambda (orig) (notmuch-address-options orig)))))

(defun notmuch-address-completing-read (beg end orig)
  "Choose email address completion candidates using `completing-read'."
  (let* ((options (with-temp-message "Looking for completion candidates..."
		    (notmuch-address-options orig)))
	 (num-options (length options))
	 (chosen (cond
		  ((eq num-options 0)
		   nil)
		  ((eq num-options 1)
		   (car options))
		  (t
		   (funcall notmuch-address-selection-function
			    (format "Address (%s matches): " num-options)
			    options
			    orig)))))
    (if chosen
	(progn
	  (push chosen notmuch-address-history)
	  (delete-region beg end)
	  (insert chosen)
	  (run-hook-with-args 'notmuch-address-post-completion-functions
			      chosen))
      (message "No matches.")
      (ding))))

(defun notmuch-address-expand-name ()
  (cond
   (notmuch-address-command
    (let* ((end (point))
	   (beg (save-excursion
		  (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
		  (goto-char (match-end 0))
		  (point)))
	   (orig (buffer-substring-no-properties beg end))
	   (completion-ignore-case t))
      (cond
       ((eq notmuch-address-completion-style 'completion-at-point)
        (notmuch-address-complete-at-point beg end orig))
       ((eq notmuch-address-completion-style 'completing-read) 
        (notmuch-address-completing-read beg end orig))
       (t nil))))
    (t nil)))


(provide 'patch-notmuch-address)
