org-capture.el 49 KB

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