org-capture.el 70 KB

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