org-capture.el 54 KB

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