org-capture.el 49 KB

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