org-capture.el 60 KB

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