org-capture.el 41 KB

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