org-capture.el 58 KB

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