org-capture.el 64 KB

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