Skip to content

Instantly share code, notes, and snippets.

@zrzka
Created February 2, 2024 12:36
Show Gist options
  • Save zrzka/a6710fee891f7bef0178a9ff7b1a66f2 to your computer and use it in GitHub Desktop.
Save zrzka/a6710fee891f7bef0178a9ff7b1a66f2 to your computer and use it in GitHub Desktop.
Capture todo
(defun rv/mu4e-message-contact-field-join (field property)
"Join mu4e message contact field PROPERTY values."
(string-join
(delq nil (mapcar (lambda (x) (plist-get x property)) field)) ", "))
(defun rv/mu4e-message-org-properties (message)
"Make a list of properties for a todo.
Return a list of `(cons NAME VALUE)'."
(let ((current-prefix-arg nil))
;; Disable prefix temporarily. It clashes w/ `rv/org-capture-todo' prefix.
(call-interactively 'org-store-link))
(let* ((subject (mu4e-message-field message :subject))
(from (mu4e-message-field message :from))
(from-name (rv/mu4e-message-contact-field-join from :name))
(from-email (rv/mu4e-message-contact-field-join from :email))
(maildir (mu4e-message-field message :maildir))
(time (mu4e-message-field message :date))
(source (pop org-stored-links)))
(list
(cons "Subject" subject)
(cons "Name" from-name)
(cons "From" from-email)
(cons "Maildir" maildir)
(cons "Date" (format-time-string "%c" time))
(cons "Message" (org-make-link-string (car source) (cadr source))))))
(defun rv/mu4e-message-at-point-org-properties ()
"Try to get a message at point, and make a list of properties for a todo.
Works only in the `mu4e-headers-mode' and `mu4e-view-mode' major
modes.
"
(when (memq major-mode '(mu4e-headers-mode mu4e-view-mode))
(rv/mu4e-message-org-properties (mu4e-message-at-point))))
(defun rv/org-insert-deadline (from-string)
"Insert a todo deadline with a relative date FROM-STRING."
(insert "DEADLINE: ")
(org-insert-time-stamp (org-read-date nil t from-string)))
(defun rv/org-capture-todo (arg)
"Sort of a smart todo capture.
Email message at point properties are set if the `major-mode' is
either `mu4e-headers-mode' or `mu4e-view-mode'. Consult
`rv/mu4e-message-at-point-org-properties' for more information.
Todo title is set to the email message subject (if available).
Marked as an undo unit for easier removal.
Deadline for today is set with a `\\[universal-argument]'.
Deadline for tomorrow is set with a `\\[universal-argument]
\\[universal-argument]'. Deadline in a week is set with a
`\\[universal-argument] \\[universal-argument]
\\[universal-argument]'.
A specific deadline (+- days) can be set with
`\\[universal-argument] N'.
"
(interactive "P")
(let ((properties (rv/mu4e-message-at-point-org-properties)))
(org-capture nil "t")
(setq subject nil)
(save-excursion
(forward-line)
(cond
;; C-u for today
((equal arg '(4))
(rv/org-insert-deadline "+0d"))
;; C-u C-u for tomorrow
((equal arg '(16))
(rv/org-insert-deadline "+1d"))
;; C-u C-u C-u for a week
((equal arg '(64))
(rv/org-insert-deadline "+1w"))
;; C-u n for n days
((numberp arg)
(rv/org-insert-deadline (format "%+dd" arg))))
(org-set-property "CREATED"
(format-time-string (org-time-stamp-format t t)
(current-time)))
(dolist (property properties)
(let ((name (car property))
(value (cdr property)))
(org-set-property name value)
(when (equal "Subject" name)
(setq subject value)))))
(when subject
(undo-boundary)
(insert subject))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment