org-capture.el 50 KB

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