 ;; single space after period ends sentence
 (setq sentence-end-double-space nil)
 
+(setq
+ user-mail-address "omajid@redhat.com")
+
 ;; show matching parenthesis
 (show-paren-mode 1)
 ;; by default, there’s a small delay before showing a matching parenthesis

  (defhydra my-hydra-main (:color blue)
    "Main"
    ("b" my-file-bug "bugs")
    ("i" (find-file user-init-file) "init file")
    ("o" my-hydra-org/body "org")
    ("p" my-hydra-projects/body "projects")

  (add-hook 'rpm-spec-mode-hook #'ws-butler-mode)
  (add-hook 'text-mode-hook #'ws-butler-mode))

;;;
;;; Mail
;;;

(defvar my-status-mail-recipient nil
  "Email address used for sending status mails.")

(defun my-get-file-contents (filename)
  "Get the contents of the file with FILENAME as one string."
  (with-temp-buffer
    (insert-file-contents filename)
    (buffer-substring (point-min) (point-max))))

(use-package mu4e
  :ensure nil
  :demand
  :init
  (use-package org-mu4e
    :ensure nil
    :demand)
  (defun my-filter-contact-address (contact)
    "Don't complete email address to noreply@ or no-reply@"
    (if (string-match-p "noreply\\|no-reply\\|mojo-notify" contact)
        nil
      contact))
  (setq
   mail-user-agent 'mu4e-user-agent
   message-send-mail-function #'smtpmail-send-it
   mu4e-change-filenames-when-moving t
   mu4e-compose-signature (my-get-file-contents (expand-file-name "~/config/email-signature"))
   mu4e-contact-process-function #'my-filter-contact-address
   mu4e-drafts-folder "/[Gmail]/Drafts"
   mu4e-headers-include-related nil ; by default, it shows entire thread, even if the rest of the messages are in a different folder
   mu4e-index-update-in-background t
   mu4e-refile-folder "/[Gmail]/All Mail"
   mu4e-sent-folder "/[Gmail]/Sent Mail"
   mu4e-trash-folder "/[Gmail]/Trash"
   mu4e-update-interval (* 60 15)
   mu4e-view-show-addresses t
   mu4e-view-show-images t
   shr-color-visible-luminance-min 80) ; make html messages more legible on dark color schemes
  :config
  (set-face-attribute 'mu4e-header-highlight-face nil
                      :underline nil)

  (defun my-mu4e-get-mail-header (msg header-name)
    (let ((path (mu4e-message-field msg :path)))
      (my-mu4e-get-mail-header-path header-name path)))

  ;; from https://etienne.depar.is/emacs.d/mu4e.html
  (defun my-mu4e-get-mail-header-path (header-name path)
    (message "%s" path)
    (replace-regexp-in-string
     "[ \t\n]*$"
     ""
     (shell-command-to-string
      (concat "/usr/bin/sed -En '/^" header-name ":/I{:loop t;h;n;/^( |\\t)/{H;x;s/\\n//;t loop};x;p}' '" path "' | sed -n 's/^" header-name ": \\(.*\\)$/\\1/Ip'"))))

  (defun my-mu4e-unsubscribe-list (msg)
    ""
    (let* ((list-unsubscribe (my-mu4e-get-mail-header msg "list-unsubscribe"))
           (mailto-start (string-match "<mailto:" list-unsubscribe)))
      (if mailto-start
          (let* ((mailto-end (string-match ">" list-unsubscribe (+ 1 mailto-start)))
                 (mailto-address (substring list-unsubscribe (+ mailto-start 1) mailto-end)))
            (url-mailto (url-generic-parse-url mailto-address))
            (goto-char (point-min))
            (re-search-forward "--\n")
            (delete-region (point) (point-max)))
        (let* ((url-start (or (string-match "<" list-unsubscribe) 0))
               (url-end (or (string-match ">" list-unsubscribe url-start) (length list-unsubscribe)))
               (unsubscribe-url (substring list-unsubscribe url-start url-end)))
          (message "%s" unsubscribe-url)
          (browse-url unsubscribe-url)))))

  (add-to-list 'mu4e-view-actions
               '("xunsubscribe from mailing list" . my-mu4e-unsubscribe-list) t)

  (defun my-status-mail-from-org-block ()
    "Create a status report mail with a src block as the body."
    (interactive)
    (unless (org-in-block-p '("src"))
      (user-error "Not in a block"))
    (save-mark-and-excursion
      (org-babel-mark-block)
      (let ((subject (format-time-string "Status %F"))
            (recipient my-status-mail-recipient)
            (text (buffer-substring-no-properties (region-beginning) (region-end))))
        (url-mailto (url-generic-parse-url (concat "mailto:" recipient "?subject=" subject)))
        (goto-char (point-min))
        (search-forward "\n\n")
        (insert text)))))

(add-to-list 'load-path (expand-file-name "lisp" user-emacs-directory))
(require 'my-redhat-mail-config)


;;;
;;; Text/Markup Editing Modes
