;;; bandali-message.el --- bandali's message.el setup  -*- lexical-binding: t; -*-

;; Copyright (C) 2018-2020  Amin Bandali

;; Author: Amin Bandali <bandali@gnu.org>
;; Keywords: mail, news

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; My setup for message.el.

;;; Code:

(with-eval-after-load 'message
  ;; :bind (:map message-mode-map ("<C-return>" . b/insert-asterism))
  ;; redefine for a simplified In-Reply-To header
  ;; (see https://todo.sr.ht/~sircmpwn/lists.sr.ht/67)
  (defun message-make-in-reply-to ()
    "Return the In-Reply-To header for this message."
    (when message-reply-headers
      (let ((from (mail-header-from message-reply-headers))
            (msg-id (mail-header-id message-reply-headers)))
        (when from
          msg-id))))

  (defconst b/message-cite-style-format "On %Y-%m-%d %l:%M %p, %N wrote:")
  (defconst message-cite-style-bandali
    '((message-cite-function  'message-cite-original)
      (message-citation-line-function  'message-insert-formatted-citation-line)
      (message-cite-reply-position 'traditional)
      (message-yank-prefix  "> ")
      (message-yank-cited-prefix  ">")
      (message-yank-empty-prefix  ">")
      (message-citation-line-format
       (if b/message-cite-say-hi
           (concat "Hi %F,\n\n" b/message-cite-style-format)
         b/message-cite-style-format)))
    "Citation style based on Mozilla Thunderbird's. Use with message-cite-style.")
  (csetq
   message-elide-ellipsis "[...]\n"
   ;; message-cite-style 'message-cite-style-bandali
   message-citation-line-format "%N writes:\n"
   message-citation-line-function 'message-insert-formatted-citation-line
   message-kill-buffer-on-exit t
   message-send-mail-function 'message-send-mail-with-sendmail
   message-sendmail-envelope-from 'header
   message-subscribed-address-functions
   '(gnus-find-subscribed-addresses)
   message-dont-reply-to-names
   "\\(\\(amin@shemshak\\.org\\)\\|\\(.*@aminb\\.org\\)\\|\\(\\(bandali\\|mab\\|aminb?\\)@gnu\\.org\\)\\|bandali@fsf\\.org\\|\\(a?bandali@\\(csclub\\.\\)?uwaterloo\\.ca\\)\\|amin\\.bandali@savoirfairelinux\\.com\\)")
  ;; (require 'company-ebdb)
  ;; (custom-set-faces
  ;;  '(message-header-subject
  ;;    ((t (:foreground "#111" :weight semi-bold))))
  ;;  '(message-header-to
  ;;    ((t (:foreground "#111" :weight normal))))
  ;;  '(message-header-cc
  ;;    ((t (:foreground "#333" :weight normal))))

  ;; footnote
  (with-eval-after-load 'footnote
    ;; (csetq footnote-start-tag ""
    ;;        footnote-end-tag   ""
    ;;        footnote-style     'unicode)
    (when (featurep 'which-key)
      (which-key-add-major-mode-key-based-replacements 'message-mode
        "C-c f n" "footnote")))

  ;; orgalist
  (add-hook 'message-mode-hook
            (lambda ()
              ;; work around incompatibility between
              ;; orgalist and yasnippet
              (yas-minor-mode -1)
              (orgalist-mode 1)
              (yas-minor-mode 1)))

  ;; local key bindings
  (defvar b/footnote-prefix-map)
  (define-prefix-command 'b/footnote-prefix-map)
  (define-key message-mode-map (kbd "C-c f n")
    'b/footnote-prefix-map)
  (define-key b/footnote-prefix-map (kbd "a")
    #'footnote-add-footnote)
  (define-key b/footnote-prefix-map (kbd "b")
    #'footnote-back-to-message)
  (define-key b/footnote-prefix-map (kbd "c")
    #'footnote-cycle-style)
  (define-key b/footnote-prefix-map (kbd "d")
    #'footnote-delete-footnote)
  (define-key b/footnote-prefix-map (kbd "g")
    #'footnote-goto-footnote)
  (define-key b/footnote-prefix-map (kbd "r")
    #'footnote-renumber-footnotes)
  (define-key b/footnote-prefix-map (kbd "s")
    #'footnote-set-style))
;; hooks
;; (add-hook 'message-setup-hook #'mml-secure-message-sign-pgpmime)
(add-hook 'message-mode-hook #'flyspell-mode)
(add-hook 'message-mode-hook
          (lambda ()
            ;; (setq-local fill-column b/fill-column
            ;;             message-fill-column b/fill-column)
            (eval-when-compile (defvar company-idle-delay))
            (make-local-variable 'company-idle-delay)
            (setq company-idle-delay 0.2)))
(add-hook 'message-mode-hook
          (lambda ()
            (local-unset-key (kbd "C-c C-s"))))

;; (with-eval-after-load 'message-x
;;   (csetq
;;    message-x-completion-alist
;;    '(("\\([rR]esent-\\|[rR]eply-\\)?[tT]o:\\|[bB]?[cC][cC]:"
;;       . gnus-harvest-find-address)
;;      ((if
;;           (boundp
;;            (quote message-newgroups-header-regexp))
;;           message-newgroups-header-regexp message-newsgroups-header-regexp)
;;       . message-expand-group))))

;; (require 'gnus-harvest)
;; (if (featurep 'message-x)
;;     (gnus-harvest-install 'message-x)
;;   (gnus-harvest-install))

(provide 'bandali-message)
;;; bandali-message.el ends here