org-capture.el 56 KB

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