org-capture.el 58 KB

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