org-capture.el 57 KB

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