org-capture.el 48 KB

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