org-capture.el 63 KB

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