org-capture.el 57 KB

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