org-capture.el 56 KB

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