org-capture.el 61 KB

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