org-capture.el 55 KB

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