org-capture.el 48 KB

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