Skip to content

Instantly share code, notes, and snippets.

@sachac
Created February 2, 2015 12:50
Show Gist options
  • Save sachac/16d9da96ac22d615f808 to your computer and use it in GitHub Desktop.
Save sachac/16d9da96ac22d615f808 to your computer and use it in GitHub Desktop.
(ert-deftest sacha/org-capture-prefill-template ()
(should
;; It should fill things in one field at a time
(string=
(sacha/org-capture-prefill-template
"* TODO %^{Task}\nSCHEDULED: %^t\n:PROPERTIES:\n:Effort: %^{effort|1:00|0:05|0:15|0:30|2:00|4:00}\n:END:\n%?\n"
"Hello World")
"* TODO Hello World\nSCHEDULED: %^t\n:PROPERTIES:\n:Effort: %^{effort|1:00|0:05|0:15|0:30|2:00|4:00}\n:END:\n%?\n"
))
(should
(string=
(sacha/org-capture-prefill-template
"* TODO %^{Task}\nSCHEDULED: %^t\n:PROPERTIES:\n:Effort: %^{effort|1:00|0:05|0:15|0:30|2:00|4:00}\n:END:\n%?\n"
"Hello World" "<2015-01-01>")
"* TODO Hello World\nSCHEDULED: <2015-01-01>\n:PROPERTIES:\n:Effort: %^{effort|1:00|0:05|0:15|0:30|2:00|4:00}\n:END:\n%?\n"))
(should
(string=
(sacha/org-capture-prefill-template
"* TODO %^{Task}\nSCHEDULED: %^t\n:PROPERTIES:\n:Effort: %^{effort|1:00|0:05|0:15|0:30|2:00|4:00}\n:END:\n%?\n"
"Hello World" "<2015-01-01>" "0:05")
"* TODO Hello World\nSCHEDULED: <2015-01-01>\n:PROPERTIES:\n:Effort: 0:05\n:END:\n%?\n")))
(defun sacha/org-capture-prefill-template (template &rest values)
"Pre-fill TEMPLATE with VALUES."
(setq template (or template (org-capture-get :template)))
(with-temp-buffer
(insert template)
(goto-char (point-min))
(while (re-search-forward
(concat "%\\("
"\\[\\(.+\\)\\]\\|"
"<\\([^>\n]+\\)>\\|"
"\\([tTuUaliAcxkKInfF]\\)\\|"
"\\(:[-a-zA-Z]+\\)\\|"
"\\^\\({\\([^}]*\\)}\\)"
"?\\([gGtTuUCLp]\\)?\\|"
"%\\\\\\([1-9][0-9]*\\)"
"\\)") nil t)
(if (car values)
(replace-match (car values) nil t))
(setq values (cdr values)))
(buffer-string)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment