org-capture.el 51 KB

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