org-capture.el 49 KB

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