org-capture.el 49 KB

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