org-capture.el 52 KB

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