org-capture.el 70 KB

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