org-capture.el 50 KB

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