org-capture.el 41 KB

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