org-capture.el 49 KB

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