org-capture.el 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. ;;; org-capture.el --- Fast note taking in Org-mode
  2. ;; Copyright (C) 2010 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 6.36trans
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; This file contains an alternaive implementation of the same functionality
  24. ;; that is also provided by org-remember.el. The implementation is more
  25. ;; streamlined, can produce more target types (e.g. plain list items or
  26. ;; table lines). Also, it does not use a temporary buffer for editing
  27. ;; the captured entry - instead it uses an indirect buffer that visits
  28. ;; the new entry already in the target buffer (this was an idea by Samuel
  29. ;; Wales). John Wiegley's excellent `remember.el' is not needed for this
  30. ;; implementation, even though we borrow heavily from its ideas.
  31. ;; This implementation heavily draws on ideas by James TD Smith and
  32. ;; Samuel Wales, and, of cause, uses John Wiegley's remember.el as inspiration.
  33. ;;; TODO
  34. ;; - find a clever way to not always insert an annotation maybe a
  35. ;; predicate function that can check for conditions for %a to be
  36. ;; used. This could be one of the properties.
  37. ;; - Should there be plist members that arrange for properties to be
  38. ;; asked for, like James proposed in his RFC?
  39. ;;; Code:
  40. (eval-when-compile
  41. (require 'cl))
  42. (require 'org)
  43. (require 'org-mks)
  44. (declare-function org-datetree-find-date-create "org-datetree"
  45. (DATE &optional KEEP-RESTRICTION))
  46. (defvar org-remember-default-headline)
  47. (defvar org-remember-templates)
  48. (defvar org-capture-clock-was-started nil
  49. "Internal flag, noting if the clock was started.")
  50. (defvar org-capture-last-stored-marker (make-marker)
  51. "Marker pointing to the entry most recently stored with `org-capture'.")
  52. (defgroup org-capture nil
  53. "Options concerning capturing new entries."
  54. :tag "Org Capture"
  55. :group 'org)
  56. (defcustom org-capture-templates nil
  57. "Templates for the creation of new entries.
  58. Each entry is a list with the following items:
  59. keys The keys that will select the template, as a string, characters
  60. only, for example \"a\" for a template to be selected with a
  61. single key, or \"bt\" for selection with two keys. When using
  62. several keys, keys using the same prefix key must be together
  63. in the list and preceded by a 2-element entry explaining the
  64. prefix key, for example
  65. (\"b\" \"Templates for marking stuff to buy\")
  66. The \"C\" key is used by default for quick access to the
  67. customization of the template variable. But if you want to use
  68. that key for a template, you can.
  69. description A short string describing the template, will be shown during
  70. selection.
  71. type The type of entry. Valid are:
  72. entry an Org-mode node, with a headline. Will be
  73. filed as the child of the target entry or as
  74. a top-level entry.
  75. item a plain list item, placed in the first plain
  76. list a the target location.
  77. checkitem a checkbox item. This only differs from the
  78. plain lis item by the default template.
  79. table-line a new line in the first table at target location.
  80. plain text to be inserted as it is.
  81. target Specification of where the captured item should be placed.
  82. In Org-mode files, targets usually define a node. Entries will
  83. become children of this node, other types will be added to the
  84. table or list in the body of this node.
  85. Valid values are:
  86. (file \"path/to/file\")
  87. Text will be placed at the beginning or end of that file
  88. (id \"id of existing org entry\")
  89. Filing as child of this entry, or in the body of the entry
  90. (file+headline \"path/to/file\" \"node headline\")
  91. Fast configuration if the target heading is unique in the file
  92. (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
  93. For non-unique headings, the full path is safer
  94. (file+regexp \"path/to/file\" \"regexp to find location\")
  95. (file+datetree \"path/to/file\")
  96. Will create a heading in a date tree.
  97. (file+function \"path/to/file\" function-finding-location)
  98. A function to find the right location in the file.
  99. (clock)
  100. File to the entry that is currently being clocked.
  101. (function function-finding-location)
  102. Most general way, write your own function to find both
  103. file and location.
  104. template The template for creating the capture item. If you leave this
  105. empty, an appropriate default template will be used. See below
  106. for more details.
  107. The rest of the entry is a property list of additional options. Recognized
  108. properties are:
  109. :prepend Normally new captured information will be appended at
  110. the target location (last child, last table line,
  111. last list item...). Setting this property will
  112. change that.
  113. :immediate-finish When set, do not offer to edit the information, just
  114. file it away immediately. This makes sense if the
  115. template only needs information that can be added
  116. automatically.
  117. :empty-lines Set this to the number of lines the should be inserted
  118. before and after the new item. Default 0, only common
  119. other value is 1.
  120. :clock-in Start the clock in this item.
  121. :clock-resume Start the interrupted clock when finishing the capture.
  122. :unnarrowed Do not narrow the target buffer, simply show the
  123. full buffer. Default is to narrow it so that you
  124. only see the new stuff.
  125. The template defined the text to be inserted. Often then this is an org-mode
  126. entry (so the first line should start with a star) that will be filed as a
  127. child of the target headline. It can also be freely formatted text.
  128. Furthermore, the following %-escapes will be replaced with content:
  129. %^{prompt} Prompt the user for a string and replace this sequence with it.
  130. A default value and a completion table ca be specified like this:
  131. %^{prompt|default|completion2|completion3|...}
  132. %t time stamp, date only
  133. %T time stamp with date and time
  134. %u, %U like the above, but inactive time stamps
  135. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
  136. You may define a prompt like %^{Please specify birthday
  137. %n user name (taken from `user-full-name')
  138. %a annotation, normally the link created with `org-store-link'
  139. %i initial content, copied from the active region. If %i is
  140. indented, the entire inserted text will be indented as well.
  141. %c current kill ring head
  142. %x content of the X clipboard
  143. %^C Interactive selection of which kill or clip to use
  144. %^L Like %^C, but insert as link
  145. %k title of currently clocked task
  146. %K link to currently clocked task
  147. %^g prompt for tags, with completion on tags in target file
  148. %^G prompt for tags, with completion all tags in all agenda files
  149. %^{prop}p Prompt the user for a value for property `prop'
  150. %:keyword specific information for certain link types, see below
  151. %[pathname] insert the contents of the file given by `pathname'
  152. %(sexp) evaluate elisp `(sexp)' and replace with the result
  153. %? After completing the template, position cursor here.
  154. Apart from these general escapes, you can access information specific to the
  155. link type that is created. For example, calling `org-capture' in emails
  156. or gnus will record the author and the subject of the message, which you
  157. can access with \"%:author\" and \"%:subject\", respectively. Here is a
  158. complete list of what is recorded for each link type.
  159. Link type | Available information
  160. -------------------+------------------------------------------------------
  161. bbdb | %:type %:name %:company
  162. vm, wl, mh, rmail | %:type %:subject %:message-id
  163. | %:from %:fromname %:fromaddress
  164. | %:to %:toname %:toaddress
  165. | %:fromto (either \"to NAME\" or \"from NAME\")
  166. gnus | %:group, for messages also all email fields
  167. w3, w3m | %:type %:url
  168. info | %:type %:file %:node
  169. calendar | %:type %:date"
  170. :group 'org-capture
  171. :type
  172. '(repeat
  173. (choice :value ("" "" entry (file "~/org/notes.org") "")
  174. (list :tag "Multikey description"
  175. (string :tag "Keys ")
  176. (string :tag "Description"))
  177. (list :tag "Template entry"
  178. (string :tag "Keys ")
  179. (string :tag "Description ")
  180. (choice :tag "Capture Type " :value entry
  181. (const :tag "Org entry" entry)
  182. (const :tag "Plain list item" item)
  183. (const :tag "Checkbox item" checkitem)
  184. (const :tag "Plain text" plain)
  185. (const :tag "Table line" table-line))
  186. (choice :tag "Target location"
  187. (list :tag "File"
  188. (const :format "" file)
  189. (file :tag " File"))
  190. (list :tag "ID"
  191. (const :format "" id)
  192. (string :tag " ID"))
  193. (list :tag "File & Headline"
  194. (const :format "" file+headline)
  195. (file :tag " File ")
  196. (string :tag " Headline"))
  197. (list :tag "File & Outline path"
  198. (const :format "" file+olp)
  199. (file :tag " File ")
  200. (repeat :tag "Outline path" :inline t
  201. (string :tag "Headline")))
  202. (list :tag "File & Regexp"
  203. (const :format "" file+regexp)
  204. (file :tag " File ")
  205. (regexp :tag " Regexp"))
  206. (list :tag "File & Date tree"
  207. (const :format "" file+datetree)
  208. (file :tag " File"))
  209. (list :tag "File & function"
  210. (const :format "" file+function)
  211. (file :tag " File ")
  212. (sexp :tag " Function"))
  213. (list :tag "Current clocking task"
  214. (const :format "" clock))
  215. (list :tag "Function"
  216. (const :format "" function)
  217. (sexp :tag " Function")))
  218. (string :tag "Template (opt) ")
  219. (plist :inline t
  220. ;; Give the most common options as checkboxes
  221. :options (((const :format "%v " :prepend) (const t))
  222. ((const :format "%v " :immediate-finish) (const t))
  223. ((const :format "%v " :empty-lines) (const 1))
  224. ((const :format "%v " :clock-in) (const t))
  225. ((const :format "%v " :clock-resume) (const t))
  226. ((const :format "%v " :unnarrowed) (const t))))))))
  227. (defcustom org-capture-before-finalize-hook nil
  228. "Hook that is run right before a remember process is finalized.
  229. The remember buffer is still current when this hook runs."
  230. :group 'org-capture
  231. :type 'hook)
  232. ;;; The property list for keeping information about the capture process
  233. (defvar org-capture-plist nil
  234. "Plist for the current capture process, global, to avoid having to pass it.")
  235. (defvar org-capture-current-plist nil
  236. "Local variable holding the plist in a capture buffer.
  237. This is used to store the plist for use when finishing a capture process.
  238. Another such process might have changed the global varaible by then.")
  239. (defun org-capture-put (&rest stuff)
  240. (while stuff
  241. (setq org-capture-plist (plist-put org-capture-plist
  242. (pop stuff) (pop stuff)))))
  243. (defun org-capture-get (prop &optional local)
  244. (plist-get (if local org-capture-current-plist org-capture-plist) prop))
  245. (defun org-capture-member (prop)
  246. (plist-get org-capture-plist prop))
  247. ;;; The minor mode
  248. (defvar org-capture-mode-map (make-sparse-keymap)
  249. "Keymap for org-capture-mode, a minor mode.
  250. Use this map to set additional keybindings for when Org-mode is used
  251. for a Remember buffer.")
  252. (defvar org-capture-mode-hook nil
  253. "Hook for the minor `org-capture-mode'.")
  254. (define-minor-mode org-capture-mode
  255. "Minor mode for special key bindings in a remember buffer."
  256. nil " Rem" org-capture-mode-map
  257. (org-set-local
  258. 'header-line-format
  259. "Capture buffer. Finish `C-c C-c', refile `C-c C-w', abort `C-c C-k'.")
  260. (run-hooks 'org-capture-mode-hook))
  261. (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
  262. (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
  263. (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
  264. ;;; The main commands
  265. ;;;###autoload
  266. (defun org-capture (&optional goto keys)
  267. "Capture something.
  268. This will let you select a template from org-capture-templates, and then
  269. file new captured information. The text is immediately inserted at the
  270. target location, and an indirect buffer is shown where you can edit it.
  271. Pressing `C-c C-c' brings you back to the previous state of Emacs,
  272. so that you can continue your work.
  273. When called interactively with a `C-u' prefix argument GOTO, don't capture
  274. anything, just go to the file/headline where the selected template
  275. stores its notes. With a double prefix arg `C-u C-u', go to the last
  276. note stored.
  277. When called with a `C-0' (zero) prefix, insert a template at point.
  278. Lisp programs can set KEYS to a string associated with a template in
  279. `org-capture-templates'. In this case, interactive selection will be
  280. bypassed."
  281. (interactive "P")
  282. (cond
  283. ((equal goto '(4)) (org-capture-goto-target))
  284. ((equal goto '(16)) (org-capture-goto-last-stored))
  285. (t
  286. ;; set temporary variables that will be needed in
  287. ;; `org-select-remember-template'
  288. (let* ((orig-buf (current-buffer))
  289. (annotation (org-store-link nil))
  290. (initial (and (org-region-active-p)
  291. (buffer-substring (point) (mark))))
  292. (entry (org-capture-select-template keys)))
  293. (if (equal entry "C")
  294. (customize-variable 'org-capture-templates)
  295. (org-capture-set-plist entry)
  296. (org-capture-put :original-buffer orig-buf :annotation annotation
  297. :initial initial)
  298. (org-capture-put :default-time
  299. (or org-overriding-default-time
  300. (org-current-time)))
  301. (org-capture-set-target-location)
  302. (org-capture-put :template (org-capture-fill-template))
  303. (if (equal goto 0)
  304. ;;insert at point
  305. (org-capture-insert-template-here)
  306. (org-capture-place-template)
  307. (if (org-capture-get :immediate-finish)
  308. (org-capture-finalize)
  309. (if (and (org-mode-p)
  310. (org-capture-get :clock-in))
  311. (condition-case nil
  312. (progn
  313. (if (org-clock-is-active)
  314. (org-capture-put :interrupted-clock
  315. (copy-marker org-clock-marker)))
  316. (org-clock-in)
  317. (org-set-local 'org-capture-clock-was-started t))
  318. (error
  319. "Could not start the clock in this capture buffer"))))))))))
  320. (defun org-capture-finalize ()
  321. "Finalize the capture process."
  322. (interactive)
  323. (unless (and org-capture-mode
  324. (buffer-base-buffer (current-buffer)))
  325. (error "This does not seem to be a capture buffer for Org-mode"))
  326. (let ((beg (point-min))
  327. (end (point-max)))
  328. (widen)
  329. ;; Make sure that the empty lines after are correct
  330. (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
  331. (member (org-capture-get :type 'local)
  332. '(entry item checkitem plain)))
  333. (save-excursion
  334. (goto-char end)
  335. (org-capture-empty-lines-after
  336. (or (org-capture-get :empty-lines 'local) 0))))
  337. ;; Postprocessing: Update Statistics cookies, do the sorting
  338. (when (org-mode-p)
  339. (save-excursion
  340. (when (ignore-errors (org-back-to-heading))
  341. (org-update-parent-todo-statistics)
  342. (org-update-checkbox-count)))
  343. ;; FIXME Here we should do the sorting
  344. ;; If we have added a table line, maybe recompute?
  345. (when (and (eq (org-capture-get :type 'local) 'table-line)
  346. (org-at-table-p))
  347. (if (org-table-get-stored-formulas)
  348. (org-table-recalculate 'all) ;; FIXME: Should we iterate???
  349. (org-table-align)))
  350. )
  351. ;; Store this place as the last one where we stored something
  352. ;; Do the marking in the base buffer, so that it makes sense after
  353. ;; the indirect buffer has been killed.
  354. (let ((pos (point)))
  355. (with-current-buffer (buffer-base-buffer (current-buffer))
  356. (save-excursion
  357. (save-restriction
  358. (widen)
  359. (goto-char pos)
  360. (bookmark-set "org-capture-last-stored")
  361. (move-marker org-capture-last-stored-marker (point))))))
  362. ;; Run the hook
  363. (run-hooks 'org-capture-before-finalize-hook)
  364. ;; Did we start the clock in this capture buffer?
  365. (when (and org-capture-clock-was-started
  366. org-clock-marker (marker-buffer org-clock-marker)
  367. (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
  368. (> org-clock-marker (point-min))
  369. (< org-clock-marker (point-max)))
  370. ;; Looks like the clock we started is still running. Clock out.
  371. (let (org-log-note-clock-out) (org-clock-out))
  372. (when (and (org-capture-get :clock-resume 'local)
  373. (markerp (org-capture-get :interrupted-clock 'local))
  374. (buffer-live-p (marker-buffer
  375. (org-capture-get :interrupted-clock 'local))))
  376. (org-with-point-at (org-capture-get :interrupted-clock 'local)
  377. (org-clock-in))
  378. (message "Interrupted clock has been resumed")))
  379. ;; Kill the indirect buffer
  380. (save-buffer)
  381. (let ((return-wconf (org-capture-get :return-to-wconf 'local)))
  382. (kill-buffer (current-buffer))
  383. ;; Restore the window configuration before capture
  384. (set-window-configuration return-wconf))))
  385. (defun org-capture-refile ()
  386. "Finalize the current capture and then refile the entry.
  387. Refiling is done from the base buffer, because the indirect buffer is then
  388. already gone."
  389. (interactive)
  390. (let ((pos (point)) (base (buffer-base-buffer (current-buffer))))
  391. (org-capture-finalize)
  392. (save-window-excursion
  393. (with-current-buffer (or base (current-buffer))
  394. (save-excursion
  395. (save-restriction
  396. (widen)
  397. (goto-char pos)
  398. (call-interactively 'org-refile)))))))
  399. (defun org-capture-kill ()
  400. "Abort the current capture process."
  401. (interactive)
  402. ;; FIXME: This does not do the right thing, we need to remove the new stuff
  403. ;; By hand it is easy: undo, then kill the buffer
  404. (let ((org-note-abort t))
  405. (org-capture-finalize)))
  406. (defun org-capture-goto-last-stored ()
  407. "Go to the location where the last remember note was stored."
  408. (interactive)
  409. (org-goto-marker-or-bmk org-capture-last-stored-marker
  410. "org-capture-last-stored")
  411. (message "This is the last note stored by a capture process"))
  412. ;;; Supporting functions for handling the process
  413. (defun org-capture-set-target-location (&optional target)
  414. "Find target buffer and position and store then in the property list."
  415. (let ((target-entry-p t))
  416. (setq target (or target (org-capture-get :target)))
  417. (save-excursion
  418. (cond
  419. ((eq (car target) 'file)
  420. (set-buffer (org-capture-target-buffer (nth 1 target)))
  421. (setq target-entry-p nil))
  422. ((eq (car target) 'id)
  423. (let ((loc (org-id-find (nth 1 target))))
  424. (if (not loc)
  425. (error "Cannot find target ID \"%s\"" (nth 1 target))
  426. (set-buffer (org-capture-target-buffer (car loc)))
  427. (goto-char (cdr loc)))))
  428. ((eq (car target) 'file+headline)
  429. (set-buffer (org-capture-target-buffer (nth 1 target)))
  430. (let ((hd (nth 2 target)))
  431. (goto-char (point-min))
  432. (if (re-search-forward
  433. (format org-complex-heading-regexp-format (regexp-quote hd))
  434. nil t)
  435. (goto-char (point-at-bol))
  436. (goto-char (point-max))
  437. (or (bolp) (insert "\n"))
  438. (insert "* " hd "\n")
  439. (beginning-of-line 0))))
  440. ((eq (car target) 'file+olp)
  441. (let ((m (org-find-olp (cdr target))))
  442. (set-buffer (marker-buffer m))
  443. (goto-char m)))
  444. ((eq (car target) 'file+regexp)
  445. (set-buffer (org-capture-target-buffer (nth 1 target)))
  446. (goto-char (point-min))
  447. (if (re-search-forward (nth 1 target) nil t)
  448. (progn
  449. (goto-char (match-beginning 0))
  450. (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
  451. (kill-buffer (current-buffer))
  452. (error "No match for target regexp in file %s" (nth 1 target))))
  453. ((eq (car target) 'file+datetree)
  454. (require 'org-datetree)
  455. (set-buffer (org-capture-target-buffer (nth 1 target)))
  456. ;; Make a date tree entry, with the current date (or yesterday,
  457. ;; if we are extending dates for a couple of hours)
  458. (org-datetree-find-date-create
  459. (calendar-gregorian-from-absolute
  460. (time-to-days
  461. (time-subtract (current-time)
  462. (list 0 (* 3600 org-extend-today-until) 0))))))
  463. ((eq (car target) 'file+function)
  464. (set-buffer (org-capture-target-buffer (nth 1 target)))
  465. (funcall (nth 1 target))
  466. (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
  467. ((eq (car target) 'clock)
  468. (if (and (markerp org-clock-hd-marker)
  469. (marker-buffer org-clock-hd-marker))
  470. (progn (set-buffer (org-capture-target-buffer
  471. (marker-buffer org-clock-hd-marker)))
  472. (goto-char org-clock-hd-marker))
  473. (error "No running clock that could be used as capture target")))
  474. (t (error "Invalid capture target specification")))
  475. (org-capture-put :buffer (current-buffer) :pos (point)
  476. :target-entry-p target-entry-p))))
  477. (defun org-capture-target-buffer (file)
  478. "Get a buffer for FILE."
  479. (or (org-find-base-buffer-visiting file)
  480. (find-file-noselect (expand-file-name file org-directory))))
  481. (defun org-capture-steal-local-variables (buffer)
  482. "Install Org-mode local variables"
  483. (mapc (lambda (v)
  484. (ignore-errors (org-set-local (car v) (cdr v))))
  485. (buffer-local-variables buffer)))
  486. (defun org-capture-place-template ()
  487. "Insert the template at the target location, and display the buffer."
  488. (org-capture-put :return-to-wconf (current-window-configuration))
  489. (delete-other-windows)
  490. (org-switch-to-buffer-other-window
  491. (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
  492. (show-all)
  493. (goto-char (org-capture-get :pos))
  494. (org-set-local 'org-capture-target-marker
  495. (move-marker (make-marker) (point)))
  496. (let* ((template (org-capture-get :template))
  497. (type (org-capture-get :type)))
  498. (case type
  499. ((nil entry) (org-capture-place-entry))
  500. (table-line (org-capture-place-table-line))
  501. (plain (org-capture-place-plain-text))
  502. (item (org-capture-place-item))))
  503. (org-capture-mode 1)
  504. (org-set-local 'org-capture-current-plist org-capture-plist))
  505. (defun org-capture-place-entry ()
  506. "Place the template as a new Org entry."
  507. (let* ((txt (org-capture-get :template))
  508. (reversed (org-capture-get :prepend))
  509. (target-entry-p (org-capture-get :target-entry-p))
  510. level beg end)
  511. (cond
  512. ((not target-entry-p)
  513. ;; Insert as top-level entry, either at beginning or at end of file
  514. (setq level 1)
  515. (if reversed
  516. (progn (goto-char (point-min))
  517. (outline-next-heading))
  518. (goto-char (point-max))
  519. (or (bolp) (insert "\n"))))
  520. (t
  521. ;; Insert as a child of the current entry
  522. (and (looking-at "\\*+")
  523. (setq level (- (match-end 0) (match-beginning 0))))
  524. (setq level (org-get-valid-level (or level 1) 1))
  525. (if reversed
  526. (progn
  527. (outline-next-heading)
  528. (or (bolp) (insert "\n")))
  529. (org-end-of-subtree t t)
  530. (or (bolp) (insert "\n")))))
  531. (org-capture-empty-lines-before)
  532. (setq beg (point))
  533. (org-paste-subtree level txt 'for-yank)
  534. (org-capture-empty-lines-after 1)
  535. (outline-next-heading)
  536. (setq end (point))
  537. (org-capture-narrow beg (1- end))
  538. (if (re-search-forward "%\\?" end t) (replace-match ""))))
  539. (defun org-capture-place-item ()
  540. "Place the template as a new plain list item."
  541. (let* ((txt (org-capture-get :template))
  542. (target-entry-p (org-capture-get :target-entry-p))
  543. ind beg end)
  544. (cond
  545. ((not target-entry-p)
  546. ;; Insert as top-level entry, either at beginning or at end of file
  547. (setq beg (point-min) end (point-max)))
  548. (t
  549. (setq beg (1+ (point-at-eol))
  550. end (save-excursion (outline-next-heading) (point)))))
  551. (if (org-capture-get :prepend)
  552. (progn
  553. (goto-char beg)
  554. (if (re-search-forward (concat "^" (org-item-re)) nil t)
  555. (progn
  556. (goto-char (match-beginning 0))
  557. (setq ind (org-get-indentation)))
  558. (goto-char end)
  559. (setq ind 0)))
  560. (goto-char end)
  561. (if (re-search-backward (concat "^" (org-item-re)) nil t)
  562. (progn
  563. (setq ind (org-get-indentation))
  564. (org-end-of-item))
  565. (setq ind 0)))
  566. ;; Remove common indentation
  567. (setq txt (org-remove-indentation txt))
  568. ;; Make sure this is indeed an item
  569. (unless (string-match (concat "\\`" (org-item-re)) txt)
  570. (setq txt (concat "- "
  571. (mapconcat 'identity (split-string txt "\n")
  572. "\n "))))
  573. ;; Set the correct indentation, depending on context
  574. (setq ind (make-string ind ?\ ))
  575. (setq txt (concat ind
  576. (mapconcat 'identity (split-string txt "\n")
  577. (concat "\n" ind))))
  578. ;; Insert, with surrounding empty lines
  579. (org-capture-empty-lines-before)
  580. (setq beg (point))
  581. (insert txt)
  582. (org-capture-empty-lines-after 1)
  583. (setq end (point))
  584. (org-capture-narrow beg (1- end))
  585. (if (re-search-forward "%\\?" end t) (replace-match ""))))
  586. (defun org-capture-place-table-line ()
  587. "Place the template as a table line."
  588. (let* ((txt (org-capture-get :template))
  589. (target-entry-p (org-capture-get :target-entry-p))
  590. ind beg end)
  591. (cond
  592. ((not target-entry-p)
  593. (setq beg (point-min) end (point-max)))
  594. (t
  595. (setq beg (1+ (point-at-eol))
  596. end (save-excursion (outline-next-heading) (point)))))
  597. (if (re-search-forward org-table-dataline-regexp end t)
  598. (let ((b (org-table-begin)) (e (org-table-end)))
  599. (goto-char e)
  600. (if (looking-at "[ \t]*#\\+TBLFM:")
  601. (forward-line 1))
  602. (narrow-to-region b (point)))
  603. (goto-char end)
  604. (insert "\n\n")
  605. (narrow-to-region (1- (point)) (point)))
  606. ;; We are narrowed to the table, or to an empty line if there was no table
  607. ;; Check if the template is good
  608. (if (not (string-match org-table-dataline-regexp txt))
  609. (setq txt "| %?Bad template |\n"))
  610. (if (org-capture-get :prepend)
  611. (progn
  612. (goto-char (point-min))
  613. (re-search-forward org-table-hline-regexp nil t)
  614. (beginning-of-line 1)
  615. (re-search-forward org-table-dataline-regexp nil t)
  616. (beginning-of-line 1)
  617. (setq beg (point))
  618. (org-table-insert-row)
  619. (beginning-of-line 1)
  620. (delete-region (point) (1+ (point-at-eol)))
  621. (insert txt)
  622. (setq end (point)))
  623. (goto-char (point-max))
  624. (re-search-backward org-table-dataline-regexp nil t)
  625. (beginning-of-line 1)
  626. (org-table-insert-row 'below)
  627. (beginning-of-line 1)
  628. (delete-region (point) (1+ (point-at-eol)))
  629. (setq beg (point))
  630. (insert txt)
  631. (setq end (point)))
  632. (goto-char beg)
  633. (if (re-search-forward "%\\?" end t) (replace-match ""))
  634. (org-table-align)))
  635. (defun org-capture-place-plain-text ()
  636. "Place the template plainly."
  637. (let* ((txt (org-capture-get :template))
  638. beg end)
  639. (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))
  640. (or (bolp) (newline))
  641. (org-capture-empty-lines-before)
  642. (setq beg (point))
  643. (insert txt)
  644. (org-capture-empty-lines-after 1)
  645. (setq end (point))
  646. (org-capture-narrow beg (1- end))
  647. (if (re-search-forward "%\\?" end t) (replace-match ""))))
  648. (defun org-capture-narrow (beg end)
  649. "Narrow, unless configuraion says not to narrow."
  650. (unless (org-capture-get :unnarrowed)
  651. (narrow-to-region beg end)
  652. (goto-char beg)))
  653. (defun org-capture-empty-lines-before (&optional n)
  654. "Arrange for the correct number of empty lines before the insertion point.
  655. Point will be after the empty lines, so insertion can direcetly be done."
  656. (setq n (or n (org-capture-get :empty-lines) 0))
  657. (let ((pos (point)))
  658. (org-back-over-empty-lines)
  659. (delete-region (point) pos)
  660. (newline n)))
  661. (defun org-capture-empty-lines-after (&optional n)
  662. "Arrange for the correct number of empty lines after the inserted string.
  663. Point will remain at the first line after the inserted text."
  664. (setq n (or n (org-capture-get :empty-lines) 0))
  665. (org-back-over-empty-lines)
  666. (while (looking-at "[ \t]*\n") (replace-match ""))
  667. (let ((pos (point)))
  668. (newline n)
  669. (goto-char pos)))
  670. (defvar org-clock-marker) ; Defined in org.el
  671. ;;;###autoload
  672. (defun org-capture-insert-template-here ()
  673. (let* ((template (org-capture-get :template))
  674. (type (org-capture-get :type))
  675. beg end pp)
  676. (or (bolp) (newline))
  677. (setq beg (point))
  678. (cond
  679. ((and (eq type 'entry) (org-mode-p))
  680. (org-paste-subtree nil template t))
  681. ((and (memq type '(item checkitem))
  682. (org-mode-p)
  683. (save-excursion (skip-chars-backward " \t\n")
  684. (setq pp (point))
  685. (org-in-item-p)))
  686. (goto-char pp)
  687. (org-insert-item)
  688. (skip-chars-backward " ")
  689. (skip-chars-backward "-+*0123456789).")
  690. (delete-region (point) (point-at-eol))
  691. (setq beg (point))
  692. (org-remove-indentation template)
  693. (insert template)
  694. (org-capture-empty-lines-after)
  695. (goto-char beg)
  696. (org-maybe-renumber-ordered-list)
  697. (org-end-of-item)
  698. (setq end (point)))
  699. (t (insert template)))
  700. (setq end (point))
  701. (goto-char beg)
  702. (if (re-search-forward "%\\?" end t)
  703. (replace-match ""))))
  704. (defun org-capture-set-plist (entry)
  705. "Initialize the property list from the template definition."
  706. (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
  707. (org-capture-put :key (car entry) :description (nth 1 entry)
  708. :target (nth 3 entry))
  709. (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
  710. (when (or (not txt) (not (string-match "\\S-" txt)))
  711. ;; The template may be empty or omitted for special types.
  712. ;; Here we insert the default templates for such cases.
  713. (cond
  714. ((eq type 'item) (setq txt "- %?"))
  715. ((eq type 'checkitem) (setq txt "- [ ] %?"))
  716. ((eq type 'table-line) (setq txt "| %? |"))
  717. ((member type '(nil entry)) (setq txt "* %?\n %a"))))
  718. (org-capture-put :template txt :type type)))
  719. (defun org-capture-goto-target (&optional template-key)
  720. "Go to the target location of a capture template.
  721. The user is queried for the template."
  722. (interactive)
  723. (let* (org-select-template-temp-major-mode
  724. (entry (org-capture-select-template template-key)))
  725. (unless entry
  726. (error "No capture emplate selected"))
  727. (org-capture-set-plist entry)
  728. (org-capture-set-target-location)
  729. (switch-to-buffer (org-capture-get :buffer))
  730. (goto-char (org-capture-get :pos))))
  731. (defun org-capture-get-indirect-buffer (&optional buffer prefix)
  732. "Make an indirect buffer for a capture process.
  733. Use PREFIX as a prefix for the name of the indirect buffer."
  734. (setq buffer (or buffer (current-buffer)))
  735. (let ((n 1) (base (buffer-name buffer)) bname)
  736. (setq bname (concat prefix "-" base))
  737. (while (buffer-live-p (get-buffer bname))
  738. (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
  739. (condition-case nil
  740. (make-indirect-buffer buffer bname 'clone)
  741. (error (make-indirect-buffer buffer bname)))))
  742. ;;; The template code
  743. (defun org-capture-select-template (&optional keys)
  744. "Select a capture template.
  745. Lisp programs can force the template by setting KEYS to a string."
  746. (when org-capture-templates
  747. (if keys
  748. (or (assoc keys org-capture-templates)
  749. (error "No capture template referred to by \"%s\" keys" keys))
  750. (org-mks org-capture-templates
  751. "Select a capture template\n========================="
  752. "Template key: "
  753. '(("C" "Customize org-capture-templates"))))))
  754. (defun org-capture-fill-template (&optional template initial annotation)
  755. "Fill a template and return the filled template as a string.
  756. The template may still contain \"%?\" for cursor positioning."
  757. (setq template (or template (org-capture-get :template)))
  758. (when (stringp initial)
  759. (setq initial (org-no-properties initial))
  760. (remove-text-properties 0 (length initial) '(read-only t) initial))
  761. (let* ((buffer (org-capture-get :buffer))
  762. (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
  763. (ct (org-capture-get :default-time))
  764. (dct (decode-time ct))
  765. (ct1
  766. (if (< (nth 2 dct) org-extend-today-until)
  767. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  768. ct))
  769. (plist-p (if org-store-link-plist t nil))
  770. (v-c (and (> (length kill-ring) 0) (current-kill 0)))
  771. (v-x (or (org-get-x-clipboard 'PRIMARY)
  772. (org-get-x-clipboard 'CLIPBOARD)
  773. (org-get-x-clipboard 'SECONDARY)))
  774. (v-t (format-time-string (car org-time-stamp-formats) ct))
  775. (v-T (format-time-string (cdr org-time-stamp-formats) ct))
  776. (v-u (concat "[" (substring v-t 1 -1) "]"))
  777. (v-U (concat "[" (substring v-T 1 -1) "]"))
  778. ;; `initial' and `annotation' might habe been passed.
  779. ;; But if the property list has them, we prefer those values
  780. (v-i (or (plist-get org-store-link-plist :initial)
  781. initial
  782. (org-capture-get :initial)
  783. ""))
  784. (v-a (or (plist-get org-store-link-plist :annotation)
  785. annotation
  786. (org-capture-get :annotation)
  787. ""))
  788. ;; Is the link empty? Then we do not want it...
  789. (v-a (if (equal v-a "[[]]") "" v-a))
  790. (clipboards (remove nil (list v-i
  791. (org-get-x-clipboard 'PRIMARY)
  792. (org-get-x-clipboard 'CLIPBOARD)
  793. (org-get-x-clipboard 'SECONDARY)
  794. v-c)))
  795. (v-A (if (and v-a
  796. (string-match
  797. "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  798. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  799. v-a))
  800. (v-n user-full-name)
  801. (v-k (if (marker-buffer org-clock-marker)
  802. (org-substring-no-properties org-clock-heading)))
  803. (v-K (if (marker-buffer org-clock-marker)
  804. (org-make-link-string
  805. (buffer-file-name (marker-buffer org-clock-marker))
  806. org-clock-heading)))
  807. v-I
  808. (org-startup-folded nil)
  809. (org-inhibit-startup t)
  810. org-time-was-given org-end-time-was-given x
  811. prompt completions char time pos default histvar)
  812. (setq org-store-link-plist
  813. (plist-put org-store-link-plist :annotation v-a)
  814. org-store-link-plist
  815. (plist-put org-store-link-plist :initial v-i))
  816. (unless template (setq template "") (message "No template") (ding)
  817. (sit-for 1))
  818. (save-window-excursion
  819. (delete-other-windows)
  820. (switch-to-buffer (get-buffer-create "*Capture*"))
  821. (insert template)
  822. (goto-char (point-min))
  823. (org-capture-steal-local-variables buffer)
  824. ;; Simple %-escapes
  825. (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
  826. (unless (org-capture-escaped-%)
  827. (when (and initial (equal (match-string 0) "%i"))
  828. (save-match-data
  829. (let* ((lead (buffer-substring
  830. (point-at-bol) (match-beginning 0))))
  831. (setq v-i (mapconcat 'identity
  832. (org-split-string initial "\n")
  833. (concat "\n" lead))))))
  834. (replace-match
  835. (or (eval (intern (concat "v-" (match-string 1)))) "")
  836. t t)))
  837. ;; %[] Insert contents of a file.
  838. (goto-char (point-min))
  839. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  840. (unless (org-capture-escaped-%)
  841. (let ((start (match-beginning 0))
  842. (end (match-end 0))
  843. (filename (expand-file-name (match-string 1))))
  844. (goto-char start)
  845. (delete-region start end)
  846. (condition-case error
  847. (insert-file-contents filename)
  848. (error (insert (format "%%![Couldn't insert %s: %s]"
  849. filename error)))))))
  850. ;; %() embedded elisp
  851. (goto-char (point-min))
  852. (while (re-search-forward "%\\((.+)\\)" nil t)
  853. (unless (org-capture-escaped-%)
  854. (goto-char (match-beginning 0))
  855. (let ((template-start (point)))
  856. (forward-char 1)
  857. (let ((result
  858. (condition-case error
  859. (eval (read (current-buffer)))
  860. (error (format "%%![Error: %s]" error)))))
  861. (delete-region template-start (point))
  862. (insert result)))))
  863. ;; From the property list
  864. (when plist-p
  865. (goto-char (point-min))
  866. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  867. (unless (org-capture-escaped-%)
  868. (and (setq x (or (plist-get org-store-link-plist
  869. (intern (match-string 1))) ""))
  870. (replace-match x t t)))))
  871. ;; Turn on org-mode in temp buffer, set local variables
  872. ;; This is to support completion in interactive prompts
  873. (let ((org-inhibit-startup t)) (org-mode))
  874. ;; Interactive template entries
  875. (goto-char (point-min))
  876. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?"
  877. nil t)
  878. (unless (org-capture-escaped-%)
  879. (setq char (if (match-end 3) (match-string 3))
  880. prompt (if (match-end 2) (match-string 2)))
  881. (goto-char (match-beginning 0))
  882. (replace-match "")
  883. (setq completions nil default nil)
  884. (when prompt
  885. (setq completions (org-split-string prompt "|")
  886. prompt (pop completions)
  887. default (car completions)
  888. histvar (intern (concat
  889. "org-capture-template-prompt-history::"
  890. (or prompt "")))
  891. completions (mapcar 'list completions)))
  892. (cond
  893. ((member char '("G" "g"))
  894. (let* ((org-last-tags-completion-table
  895. (org-global-tags-completion-table
  896. (if (equal char "G")
  897. (org-agenda-files)
  898. (and file (list file)))))
  899. (org-add-colon-after-tag-completion t)
  900. (ins (org-icompleting-read
  901. (if prompt (concat prompt ": ") "Tags: ")
  902. 'org-tags-completion-function nil nil nil
  903. 'org-tags-history)))
  904. (setq ins (mapconcat 'identity
  905. (org-split-string
  906. ins (org-re "[^[:alnum:]_@]+"))
  907. ":"))
  908. (when (string-match "\\S-" ins)
  909. (or (equal (char-before) ?:) (insert ":"))
  910. (insert ins)
  911. (or (equal (char-after) ?:) (insert ":")))))
  912. ((equal char "C")
  913. (cond ((= (length clipboards) 1) (insert (car clipboards)))
  914. ((> (length clipboards) 1)
  915. (insert (read-string "Clipboard/kill value: "
  916. (car clipboards) '(clipboards . 1)
  917. (car clipboards))))))
  918. ((equal char "L")
  919. (cond ((= (length clipboards) 1)
  920. (org-insert-link 0 (car clipboards)))
  921. ((> (length clipboards) 1)
  922. (org-insert-link 0 (read-string "Clipboard/kill value: "
  923. (car clipboards)
  924. '(clipboards . 1)
  925. (car clipboards))))))
  926. ((equal char "p")
  927. (let*
  928. ((prop (org-substring-no-properties prompt))
  929. (pall (concat prop "_ALL"))
  930. (allowed
  931. (with-current-buffer
  932. (get-buffer (file-name-nondirectory file))
  933. (or (cdr (assoc pall org-file-properties))
  934. (cdr (assoc pall org-global-properties))
  935. (cdr (assoc pall org-global-properties-fixed)))))
  936. (existing (with-current-buffer
  937. (get-buffer (file-name-nondirectory file))
  938. (mapcar 'list (org-property-values prop))))
  939. (propprompt (concat "Value for " prop ": "))
  940. (val (if allowed
  941. (org-completing-read
  942. propprompt
  943. (mapcar 'list (org-split-string allowed
  944. "[ \t]+"))
  945. nil 'req-match)
  946. (org-completing-read-no-i propprompt
  947. existing nil nil
  948. "" nil ""))))
  949. (org-set-property prop val)))
  950. (char
  951. ;; These are the date/time related ones
  952. (setq org-time-was-given (equal (upcase char) char))
  953. (setq time (org-read-date (equal (upcase char) "U") t nil
  954. prompt))
  955. (org-insert-time-stamp time org-time-was-given
  956. (member char '("u" "U"))
  957. nil nil (list org-end-time-was-given)))
  958. (t
  959. (let (org-completion-use-ido)
  960. (insert (org-completing-read-no-i
  961. (concat (if prompt prompt "Enter string")
  962. (if default (concat " [" default "]"))
  963. ": ")
  964. completions nil nil nil histvar default)))))))
  965. ;; Make sure there are no empty lines before the text, and that
  966. ;; it ends with a newline character
  967. (goto-char (point-min))
  968. (while (looking-at "[ \t]*\n") (replace-match ""))
  969. (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
  970. ;; Return the expanded tempate and kill the temporary buffer
  971. (untabify (point-min) (point-max))
  972. (set-buffer-modified-p nil)
  973. (prog1 (buffer-string) (kill-buffer (current-buffer))))))
  974. (defun org-capture-escaped-% ()
  975. "Check if % was escaped - if yes, unescape it now."
  976. (if (equal (char-before (match-beginning 0)) ?\\)
  977. (progn
  978. (delete-region (1- (match-beginning 0)) (match-beginning 0))
  979. t)
  980. nil))
  981. ;;;###autoload
  982. (defun org-capture-import-remember-templates ()
  983. "Set org-capture-templates to be similar to `org-remember-templates'."
  984. (interactive)
  985. (when (and (yes-or-no-p
  986. "Import old remember templates into org-capture-templates? ")
  987. (yes-or-no-p
  988. "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
  989. (require 'org-remember)
  990. (setq org-capture-templates
  991. (mapcar
  992. (lambda (entry)
  993. (let ((desc (car entry))
  994. (key (char-to-string (nth 1 entry)))
  995. (template (nth 2 entry))
  996. (file (or (nth 3 entry) org-default-notes-file))
  997. (position (or (nth 4 entry) org-remember-default-headline))
  998. (type 'entry)
  999. (prepend org-reverse-note-order)
  1000. immediate target)
  1001. (cond
  1002. ((member position '(top bottom))
  1003. (setq target (list 'file file)
  1004. prepend (eq position 'top)))
  1005. ((eq position 'date-tree)
  1006. (setq target (list 'file+datetree file)
  1007. prepend nil))
  1008. (t (setq target (list 'file+headline file position))))
  1009. (when (string-match "%!" template)
  1010. (setq template (replace-match "" t t template)
  1011. immediate t))
  1012. (append (list key desc type target template)
  1013. (if prepend '(:prepend t))
  1014. (if immediate '(:immediate-finish t)))))
  1015. org-remember-templates))))
  1016. (provide 'org-capture)
  1017. ;; arch-tag: 986bf41b-8ada-4e28-bf20-e8388a7205a0
  1018. ;;; org-capture.el ends here