org-capture.el 51 KB

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