org-capture.el 57 KB

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