org-capture.el 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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. (defcustom org-capture-templates-contexts nil
  368. "Bind capture keys with rules on where to display them.
  369. For example, if you have a capture template \"c\" and you want
  370. this template to be accessible only from message-mode buffers,
  371. use this:
  372. '((\"c\" (in-mode . \"message-mode\")))
  373. Here are the available checks:
  374. in-file: template displayed only in matching files
  375. in-mode: template displayed only in matching modes
  376. not-in-file: template not displayed in matching files
  377. not-in-mode: template not displayed in matching modes
  378. If you define several checks, the capture template will be
  379. accessible if there is at least one valid check."
  380. ;; :version "24.3"
  381. :group 'org-capture
  382. :type '(repeat (cons :tag "Rule"
  383. (string :tag "Capture key")
  384. (repeat :tag "Available when"
  385. (cons :tag "Condition"
  386. (choice
  387. (const :tag "In file" in-file)
  388. (const :tag "Not in file" not-in-file)
  389. (const :tag "In mode" in-mode)
  390. (const :tag "Not in mode" not-in-mode))
  391. (regexp))))))
  392. ;;;###autoload
  393. (defun org-capture (&optional goto keys)
  394. "Capture something.
  395. \\<org-capture-mode-map>
  396. This will let you select a template from `org-capture-templates', and then
  397. file the newly captured information. The text is immediately inserted
  398. at the target location, and an indirect buffer is shown where you can
  399. edit it. Pressing \\[org-capture-finalize] brings you back to the previous state
  400. of Emacs, so that you can continue your work.
  401. When called interactively with a \\[universal-argument] prefix argument GOTO, don't capture
  402. anything, just go to the file/headline where the selected template
  403. stores its notes. With a double prefix argument \
  404. \\[universal-argument] \\[universal-argument], go to the last note
  405. stored.
  406. When called with a `C-0' (zero) prefix, insert a template at point.
  407. Lisp programs can set KEYS to a string associated with a template in
  408. `org-capture-templates'. In this case, interactive selection will be
  409. bypassed."
  410. (interactive "P")
  411. (cond
  412. ((equal goto '(4)) (org-capture-goto-target))
  413. ((equal goto '(16)) (org-capture-goto-last-stored))
  414. (t
  415. ;; FIXME: Are these needed?
  416. (let* ((orig-buf (current-buffer))
  417. (annotation (if (and (boundp 'org-capture-link-is-already-stored)
  418. org-capture-link-is-already-stored)
  419. (plist-get org-store-link-plist :annotation)
  420. (ignore-errors (org-store-link nil))))
  421. (entry (org-capture-select-template keys))
  422. initial)
  423. (setq initial (or org-capture-initial
  424. (and (org-region-active-p)
  425. (buffer-substring (point) (mark)))))
  426. (when (stringp initial)
  427. (remove-text-properties 0 (length initial) '(read-only t) initial))
  428. (when (stringp annotation)
  429. (remove-text-properties 0 (length annotation)
  430. '(read-only t) annotation))
  431. (cond
  432. ((equal entry "C")
  433. (customize-variable 'org-capture-templates))
  434. ((equal entry "q")
  435. (error "Abort"))
  436. (t
  437. (org-capture-set-plist entry)
  438. (org-capture-get-template)
  439. (org-capture-put :original-buffer orig-buf
  440. :original-file (or (buffer-file-name orig-buf)
  441. (and (featurep 'dired)
  442. (car (rassq orig-buf
  443. dired-buffers))))
  444. :original-file-nondirectory
  445. (and (buffer-file-name orig-buf)
  446. (file-name-nondirectory
  447. (buffer-file-name orig-buf)))
  448. :annotation annotation
  449. :initial initial)
  450. (org-capture-put :default-time
  451. (or org-overriding-default-time
  452. (org-current-time)))
  453. (org-capture-set-target-location)
  454. (condition-case error
  455. (org-capture-put :template (org-capture-fill-template))
  456. ((error quit)
  457. (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
  458. (error "Capture abort: %s" error)))
  459. (setq org-capture-clock-keep (org-capture-get :clock-keep))
  460. (if (equal goto 0)
  461. ;;insert at point
  462. (org-capture-insert-template-here)
  463. (condition-case error
  464. (org-capture-place-template)
  465. ((error quit)
  466. (if (and (buffer-base-buffer (current-buffer))
  467. (string-match "\\`CAPTURE-" (buffer-name)))
  468. (kill-buffer (current-buffer)))
  469. (set-window-configuration (org-capture-get :return-to-wconf))
  470. (error "Capture template `%s': %s"
  471. (org-capture-get :key)
  472. (nth 1 error))))
  473. (if (and (derived-mode-p 'org-mode)
  474. (org-capture-get :clock-in))
  475. (condition-case nil
  476. (progn
  477. (if (org-clock-is-active)
  478. (org-capture-put :interrupted-clock
  479. (copy-marker org-clock-marker)))
  480. (org-clock-in)
  481. (org-set-local 'org-capture-clock-was-started t))
  482. (error
  483. "Could not start the clock in this capture buffer")))
  484. (if (org-capture-get :immediate-finish)
  485. (org-capture-finalize nil)))))))))
  486. (defun org-capture-get-template ()
  487. "Get the template from a file or a function if necessary."
  488. (let ((txt (org-capture-get :template)) file)
  489. (cond
  490. ((and (listp txt) (eq (car txt) 'file))
  491. (if (file-exists-p
  492. (setq file (expand-file-name (nth 1 txt) org-directory)))
  493. (setq txt (org-file-contents file))
  494. (setq txt (format "* Template file %s not found" (nth 1 txt)))))
  495. ((and (listp txt) (eq (car txt) 'function))
  496. (if (fboundp (nth 1 txt))
  497. (setq txt (funcall (nth 1 txt)))
  498. (setq txt (format "* Template function %s not found" (nth 1 txt)))))
  499. ((not txt) (setq txt ""))
  500. ((stringp txt))
  501. (t (setq txt "* Invalid capture template")))
  502. (org-capture-put :template txt)))
  503. (defun org-capture-finalize (&optional stay-with-capture)
  504. "Finalize the capture process.
  505. With prefix argument STAY-WITH-CAPTURE, jump to the location of the
  506. captured item after finalizing."
  507. (interactive "P")
  508. (unless (and org-capture-mode
  509. (buffer-base-buffer (current-buffer)))
  510. (error "This does not seem to be a capture buffer for Org-mode"))
  511. (run-hooks 'org-capture-prepare-finalize-hook)
  512. ;; Did we start the clock in this capture buffer?
  513. (when (and org-capture-clock-was-started
  514. org-clock-marker (marker-buffer org-clock-marker)
  515. (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
  516. (> org-clock-marker (point-min))
  517. (< org-clock-marker (point-max)))
  518. ;; Looks like the clock we started is still running. Clock out.
  519. (when (not org-capture-clock-keep) (let (org-log-note-clock-out) (org-clock-out)))
  520. (when (and (not org-capture-clock-keep)
  521. (org-capture-get :clock-resume 'local)
  522. (markerp (org-capture-get :interrupted-clock 'local))
  523. (buffer-live-p (marker-buffer
  524. (org-capture-get :interrupted-clock 'local))))
  525. (let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
  526. (org-with-point-at clock-in-task
  527. (org-clock-in)))
  528. (message "Interrupted clock has been resumed")))
  529. (let ((beg (point-min))
  530. (end (point-max))
  531. (abort-note nil))
  532. ;; Store the size of the capture buffer
  533. (org-capture-put :captured-entry-size (- (point-max) (point-min)))
  534. (widen)
  535. ;; Store the insertion point in the target buffer
  536. (org-capture-put :insertion-point (point))
  537. (if org-note-abort
  538. (let ((m1 (org-capture-get :begin-marker 'local))
  539. (m2 (org-capture-get :end-marker 'local)))
  540. (if (and m1 m2 (= m1 beg) (= m2 end))
  541. (progn
  542. (setq m2 (if (cdr (assoc 'heading org-blank-before-new-entry))
  543. m2 (1+ m2))
  544. m2 (if (< (point-max) m2) (point-max) m2))
  545. (setq abort-note 'clean)
  546. (kill-region m1 m2))
  547. (setq abort-note 'dirty)))
  548. ;; Make sure that the empty lines after are correct
  549. (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
  550. (member (org-capture-get :type 'local)
  551. '(entry item checkitem plain)))
  552. (save-excursion
  553. (goto-char end)
  554. (or (bolp) (newline))
  555. (org-capture-empty-lines-after
  556. (or (org-capture-get :empty-lines-after 'local)
  557. (org-capture-get :empty-lines 'local) 0))))
  558. ;; Postprocessing: Update Statistics cookies, do the sorting
  559. (when (derived-mode-p 'org-mode)
  560. (save-excursion
  561. (when (ignore-errors (org-back-to-heading))
  562. (org-update-parent-todo-statistics)
  563. (org-update-checkbox-count)))
  564. ;; FIXME Here we should do the sorting
  565. ;; If we have added a table line, maybe recompute?
  566. (when (and (eq (org-capture-get :type 'local) 'table-line)
  567. (org-at-table-p))
  568. (if (org-table-get-stored-formulas)
  569. (org-table-recalculate 'all) ;; FIXME: Should we iterate???
  570. (org-table-align))))
  571. ;; Store this place as the last one where we stored something
  572. ;; Do the marking in the base buffer, so that it makes sense after
  573. ;; the indirect buffer has been killed.
  574. (when org-capture-bookmark
  575. (org-capture-bookmark-last-stored-position))
  576. ;; Run the hook
  577. (run-hooks 'org-capture-before-finalize-hook))
  578. ;; Kill the indirect buffer
  579. (save-buffer)
  580. (let ((return-wconf (org-capture-get :return-to-wconf 'local))
  581. (new-buffer (org-capture-get :new-buffer 'local))
  582. (kill-buffer (org-capture-get :kill-buffer 'local))
  583. (base-buffer (buffer-base-buffer (current-buffer))))
  584. ;; Kill the indirect buffer
  585. (kill-buffer (current-buffer))
  586. ;; Narrow back the target buffer to its previous state
  587. (with-current-buffer (org-capture-get :buffer)
  588. (let ((reg (org-capture-get :initial-target-region))
  589. (pos (org-capture-get :initial-target-position))
  590. (ipt (org-capture-get :insertion-point))
  591. (size (org-capture-get :captured-entry-size)))
  592. (when reg
  593. (cond ((< ipt (car reg))
  594. ;; insertion point is before the narrowed region
  595. (narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
  596. ((> ipt (cdr reg))
  597. ;; insertion point is after the narrowed region
  598. (narrow-to-region (car reg) (cdr reg)))
  599. (t
  600. ;; insertion point is within the narrowed region
  601. (narrow-to-region (car reg) (+ size (cdr reg)))))
  602. ;; now place back the point at its original position
  603. (if (< ipt (car reg))
  604. (goto-char (+ size pos))
  605. (goto-char (if (< ipt pos) (+ size pos) pos))))))
  606. ;; Kill the target buffer if that is desired
  607. (when (and base-buffer new-buffer kill-buffer)
  608. (with-current-buffer base-buffer (save-buffer))
  609. (kill-buffer base-buffer))
  610. ;; Restore the window configuration before capture
  611. (set-window-configuration return-wconf))
  612. (run-hooks 'org-capture-after-finalize-hook)
  613. ;; Special cases
  614. (cond
  615. (abort-note
  616. (cond
  617. ((equal abort-note 'clean)
  618. (message "Capture process aborted and target buffer cleaned up"))
  619. ((equal abort-note 'dirty)
  620. (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))
  621. (stay-with-capture
  622. (org-capture-goto-last-stored)))
  623. ;; Return if we did store something
  624. (not abort-note)))
  625. (defun org-capture-refile ()
  626. "Finalize the current capture and then refile the entry.
  627. Refiling is done from the base buffer, because the indirect buffer is then
  628. already gone. Any prefix argument will be passed to the refile command."
  629. (interactive)
  630. (unless (eq (org-capture-get :type 'local) 'entry)
  631. (error
  632. "Refiling from a capture buffer makes only sense for `entry'-type templates"))
  633. (let ((pos (point))
  634. (base (buffer-base-buffer (current-buffer)))
  635. (org-refile-for-capture t))
  636. (org-capture-finalize)
  637. (save-window-excursion
  638. (with-current-buffer (or base (current-buffer))
  639. (save-excursion
  640. (save-restriction
  641. (widen)
  642. (goto-char pos)
  643. (call-interactively 'org-refile)))))))
  644. (defun org-capture-kill ()
  645. "Abort the current capture process."
  646. (interactive)
  647. ;; FIXME: This does not do the right thing, we need to remove the
  648. ;; new stuff by hand it is easy: undo, then kill the buffer
  649. (let ((org-note-abort t)
  650. (org-capture-before-finalize-hook nil))
  651. (org-capture-finalize)))
  652. (defun org-capture-goto-last-stored ()
  653. "Go to the location where the last capture note was stored."
  654. (interactive)
  655. (org-goto-marker-or-bmk org-capture-last-stored-marker
  656. "org-capture-last-stored")
  657. (message "This is the last note stored by a capture process"))
  658. ;;; Supporting functions for handling the process
  659. (defun org-capture-put-target-region-and-position ()
  660. "Store the initial region with `org-capture-put'."
  661. (org-capture-put
  662. :initial-target-region
  663. ;; Check if the buffer is currently narrowed
  664. (when (/= (buffer-size) (- (point-max) (point-min)))
  665. (cons (point-min) (point-max))))
  666. ;; store the current point
  667. (org-capture-put :initial-target-position (point)))
  668. (defun org-capture-set-target-location (&optional target)
  669. "Find target buffer and position and store then in the property list."
  670. (let ((target-entry-p t))
  671. (setq target (or target (org-capture-get :target)))
  672. (save-excursion
  673. (cond
  674. ((eq (car target) 'file)
  675. (set-buffer (org-capture-target-buffer (nth 1 target)))
  676. (org-capture-put-target-region-and-position)
  677. (widen)
  678. (setq target-entry-p nil))
  679. ((eq (car target) 'id)
  680. (let ((loc (org-id-find (nth 1 target))))
  681. (if (not loc)
  682. (error "Cannot find target ID \"%s\"" (nth 1 target))
  683. (set-buffer (org-capture-target-buffer (car loc)))
  684. (widen)
  685. (org-capture-put-target-region-and-position)
  686. (goto-char (cdr loc)))))
  687. ((eq (car target) 'file+headline)
  688. (set-buffer (org-capture-target-buffer (nth 1 target)))
  689. (org-capture-put-target-region-and-position)
  690. (widen)
  691. (let ((hd (nth 2 target)))
  692. (goto-char (point-min))
  693. (unless (derived-mode-p 'org-mode)
  694. (error
  695. "Target buffer \"%s\" for file+headline should be in Org mode"
  696. (current-buffer)))
  697. (if (re-search-forward
  698. (format org-complex-heading-regexp-format (regexp-quote hd))
  699. nil t)
  700. (goto-char (point-at-bol))
  701. (goto-char (point-max))
  702. (or (bolp) (insert "\n"))
  703. (insert "* " hd "\n")
  704. (beginning-of-line 0))))
  705. ((eq (car target) 'file+olp)
  706. (let ((m (org-find-olp
  707. (cons (org-capture-expand-file (nth 1 target))
  708. (cddr target)))))
  709. (set-buffer (marker-buffer m))
  710. (org-capture-put-target-region-and-position)
  711. (widen)
  712. (goto-char m)))
  713. ((eq (car target) 'file+regexp)
  714. (set-buffer (org-capture-target-buffer (nth 1 target)))
  715. (org-capture-put-target-region-and-position)
  716. (widen)
  717. (goto-char (point-min))
  718. (if (re-search-forward (nth 2 target) nil t)
  719. (progn
  720. (goto-char (if (org-capture-get :prepend)
  721. (match-beginning 0) (match-end 0)))
  722. (org-capture-put :exact-position (point))
  723. (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
  724. (error "No match for target regexp in file %s" (nth 1 target))))
  725. ((memq (car target) '(file+datetree file+datetree+prompt))
  726. (require 'org-datetree)
  727. (set-buffer (org-capture-target-buffer (nth 1 target)))
  728. (org-capture-put-target-region-and-position)
  729. (widen)
  730. ;; Make a date tree entry, with the current date (or yesterday,
  731. ;; if we are extending dates for a couple of hours)
  732. (org-datetree-find-date-create
  733. (calendar-gregorian-from-absolute
  734. (cond
  735. (org-overriding-default-time
  736. ;; use the overriding default time
  737. (time-to-days org-overriding-default-time))
  738. ((eq (car target) 'file+datetree+prompt)
  739. ;; prompt for date
  740. (let ((prompt-time (org-read-date
  741. nil t nil "Date for tree entry:"
  742. (current-time))))
  743. (org-capture-put :prompt-time prompt-time
  744. :default-time prompt-time)
  745. (time-to-days prompt-time)))
  746. (t
  747. ;; current date, possible corrected for late night workers
  748. (org-today))))))
  749. ((eq (car target) 'file+function)
  750. (set-buffer (org-capture-target-buffer (nth 1 target)))
  751. (org-capture-put-target-region-and-position)
  752. (widen)
  753. (funcall (nth 2 target))
  754. (org-capture-put :exact-position (point))
  755. (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
  756. ((eq (car target) 'function)
  757. (funcall (nth 1 target))
  758. (org-capture-put :exact-position (point))
  759. (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
  760. ((eq (car target) 'clock)
  761. (if (and (markerp org-clock-hd-marker)
  762. (marker-buffer org-clock-hd-marker))
  763. (progn (set-buffer (marker-buffer org-clock-hd-marker))
  764. (org-capture-put-target-region-and-position)
  765. (widen)
  766. (goto-char org-clock-hd-marker))
  767. (error "No running clock that could be used as capture target")))
  768. (t (error "Invalid capture target specification")))
  769. (org-capture-put :buffer (current-buffer) :pos (point)
  770. :target-entry-p target-entry-p))))
  771. (defun org-capture-expand-file (file)
  772. "Expand functions and symbols for FILE.
  773. When FILE is a function, call it. When it is a form, evaluate
  774. it. When it is a variable, retrieve the value. Return whatever we get."
  775. (cond
  776. ((org-string-nw-p file) file)
  777. ((functionp file) (funcall file))
  778. ((and (symbolp file) (boundp file)) (symbol-value file))
  779. ((and file (consp file)) (eval file))
  780. (t file)))
  781. (defun org-capture-target-buffer (file)
  782. "Get a buffer for FILE."
  783. (setq file (org-capture-expand-file file))
  784. (setq file (or (org-string-nw-p file)
  785. org-default-notes-file
  786. (error "No notes file specified, and no default available")))
  787. (or (org-find-base-buffer-visiting file)
  788. (progn (org-capture-put :new-buffer t)
  789. (find-file-noselect (expand-file-name file org-directory)))))
  790. (defun org-capture-steal-local-variables (buffer)
  791. "Install Org-mode local variables."
  792. (mapc (lambda (v)
  793. (ignore-errors (org-set-local (car v) (cdr v))))
  794. (buffer-local-variables buffer)))
  795. (defun org-capture-place-template ()
  796. "Insert the template at the target location, and display the buffer."
  797. (org-capture-put :return-to-wconf (current-window-configuration))
  798. (delete-other-windows)
  799. (org-switch-to-buffer-other-window
  800. (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
  801. (widen)
  802. (show-all)
  803. (goto-char (org-capture-get :pos))
  804. (org-set-local 'org-capture-target-marker
  805. (move-marker (make-marker) (point)))
  806. (org-set-local 'outline-level 'org-outline-level)
  807. (let* ((template (org-capture-get :template))
  808. (type (org-capture-get :type)))
  809. (case type
  810. ((nil entry) (org-capture-place-entry))
  811. (table-line (org-capture-place-table-line))
  812. (plain (org-capture-place-plain-text))
  813. (item (org-capture-place-item))
  814. (checkitem (org-capture-place-item))))
  815. (org-capture-mode 1)
  816. (org-set-local 'org-capture-current-plist org-capture-plist))
  817. (defun org-capture-place-entry ()
  818. "Place the template as a new Org entry."
  819. (let* ((txt (org-capture-get :template))
  820. (reversed (org-capture-get :prepend))
  821. (target-entry-p (org-capture-get :target-entry-p))
  822. level beg end file)
  823. (cond
  824. ((org-capture-get :exact-position)
  825. (goto-char (org-capture-get :exact-position)))
  826. ((not target-entry-p)
  827. ;; Insert as top-level entry, either at beginning or at end of file
  828. (setq level 1)
  829. (if reversed
  830. (progn (goto-char (point-min))
  831. (or (org-at-heading-p)
  832. (outline-next-heading)))
  833. (goto-char (point-max))
  834. (or (bolp) (insert "\n"))))
  835. (t
  836. ;; Insert as a child of the current entry
  837. (and (looking-at "\\*+")
  838. (setq level (- (match-end 0) (match-beginning 0))))
  839. (setq level (org-get-valid-level (or level 1) 1))
  840. (if reversed
  841. (progn
  842. (outline-next-heading)
  843. (or (bolp) (insert "\n")))
  844. (org-end-of-subtree t nil)
  845. (or (bolp) (insert "\n")))))
  846. (org-capture-empty-lines-before)
  847. (setq beg (point))
  848. (org-capture-verify-tree txt)
  849. (org-paste-subtree level txt 'for-yank)
  850. (org-capture-empty-lines-after 1)
  851. (org-capture-position-for-last-stored beg)
  852. (outline-next-heading)
  853. (setq end (point))
  854. (org-capture-mark-kill-region beg (1- end))
  855. (org-capture-narrow beg (1- end))
  856. (if (or (re-search-backward "%\\?" beg t)
  857. (re-search-forward "%\\?" end t))
  858. (replace-match ""))))
  859. (defun org-capture-place-item ()
  860. "Place the template as a new plain list item."
  861. (let* ((txt (org-capture-get :template))
  862. (target-entry-p (org-capture-get :target-entry-p))
  863. (ind 0)
  864. beg end)
  865. (if (org-capture-get :exact-position)
  866. (goto-char (org-capture-get :exact-position))
  867. (cond
  868. ((not target-entry-p)
  869. ;; Insert as top-level entry, either at beginning or at end of file
  870. (setq beg (point-min) end (point-max)))
  871. (t
  872. (setq beg (1+ (point-at-eol))
  873. end (save-excursion (outline-next-heading) (point)))))
  874. (if (org-capture-get :prepend)
  875. (progn
  876. (goto-char beg)
  877. (if (org-list-search-forward (org-item-beginning-re) end t)
  878. (progn
  879. (goto-char (match-beginning 0))
  880. (setq ind (org-get-indentation)))
  881. (goto-char end)
  882. (setq ind 0)))
  883. (goto-char end)
  884. (if (org-list-search-backward (org-item-beginning-re) beg t)
  885. (progn
  886. (setq ind (org-get-indentation))
  887. (org-end-of-item))
  888. (setq ind 0))))
  889. ;; Remove common indentation
  890. (setq txt (org-remove-indentation txt))
  891. ;; Make sure this is indeed an item
  892. (unless (string-match (concat "\\`" (org-item-re)) txt)
  893. (setq txt (concat "- "
  894. (mapconcat 'identity (split-string txt "\n")
  895. "\n "))))
  896. ;; Set the correct indentation, depending on context
  897. (setq ind (make-string ind ?\ ))
  898. (setq txt (concat ind
  899. (mapconcat 'identity (split-string txt "\n")
  900. (concat "\n" ind))
  901. "\n"))
  902. ;; Insert, with surrounding empty lines
  903. (org-capture-empty-lines-before)
  904. (setq beg (point))
  905. (insert txt)
  906. (or (bolp) (insert "\n"))
  907. (org-capture-empty-lines-after 1)
  908. (org-capture-position-for-last-stored beg)
  909. (forward-char 1)
  910. (setq end (point))
  911. (org-capture-mark-kill-region beg (1- end))
  912. (org-capture-narrow beg (1- end))
  913. (if (or (re-search-backward "%\\?" beg t)
  914. (re-search-forward "%\\?" end t))
  915. (replace-match ""))))
  916. (defun org-capture-place-table-line ()
  917. "Place the template as a table line."
  918. (require 'org-table)
  919. (let* ((txt (org-capture-get :template))
  920. (target-entry-p (org-capture-get :target-entry-p))
  921. (table-line-pos (org-capture-get :table-line-pos))
  922. ind beg end)
  923. (cond
  924. ((org-capture-get :exact-position)
  925. (goto-char (org-capture-get :exact-position)))
  926. ((not target-entry-p)
  927. ;; Table is not necessarily under a heading
  928. (setq beg (point-min) end (point-max)))
  929. (t
  930. ;; WE are at a heading, limit search to the body
  931. (setq beg (1+ (point-at-eol))
  932. end (save-excursion (outline-next-heading) (point)))))
  933. (if (re-search-forward org-table-dataline-regexp end t)
  934. (let ((b (org-table-begin)) (e (org-table-end)) (case-fold-search t))
  935. (goto-char e)
  936. (if (looking-at "[ \t]*#\\+tblfm:")
  937. (forward-line 1))
  938. (narrow-to-region b (point)))
  939. (goto-char end)
  940. (insert "\n| |\n|----|\n| |\n")
  941. (narrow-to-region (1+ end) (point)))
  942. ;; We are narrowed to the table, or to an empty line if there was no table
  943. ;; Check if the template is good
  944. (if (not (string-match org-table-dataline-regexp txt))
  945. (setq txt "| %?Bad template |\n"))
  946. (cond
  947. ((and table-line-pos
  948. (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
  949. ;; we have a complex line specification
  950. (goto-char (point-min))
  951. (let ((nh (- (match-end 1) (match-beginning 1)))
  952. (delta (string-to-number (match-string 2 table-line-pos)))
  953. ll)
  954. ;; The user wants a special position in the table
  955. (org-table-get-specials)
  956. (setq ll (ignore-errors (aref org-table-hlines nh)))
  957. (unless ll (error "Invalid table line specification \"%s\""
  958. table-line-pos))
  959. (setq ll (+ ll delta (if (< delta 0) 0 -1)))
  960. (org-goto-line ll)
  961. (org-table-insert-row 'below)
  962. (beginning-of-line 1)
  963. (delete-region (point) (1+ (point-at-eol)))
  964. (setq beg (point))
  965. (insert txt)
  966. (setq end (point))))
  967. ((org-capture-get :prepend)
  968. (goto-char (point-min))
  969. (re-search-forward org-table-hline-regexp nil t)
  970. (beginning-of-line 1)
  971. (re-search-forward org-table-dataline-regexp nil t)
  972. (beginning-of-line 1)
  973. (setq beg (point))
  974. (org-table-insert-row)
  975. (beginning-of-line 1)
  976. (delete-region (point) (1+ (point-at-eol)))
  977. (insert txt)
  978. (setq end (point)))
  979. (t
  980. (goto-char (point-max))
  981. (re-search-backward org-table-dataline-regexp nil t)
  982. (beginning-of-line 1)
  983. (org-table-insert-row 'below)
  984. (beginning-of-line 1)
  985. (delete-region (point) (1+ (point-at-eol)))
  986. (setq beg (point))
  987. (insert txt)
  988. (setq end (point))))
  989. (goto-char beg)
  990. (org-capture-position-for-last-stored 'table-line)
  991. (if (or (re-search-backward "%\\?" beg t)
  992. (re-search-forward "%\\?" end t))
  993. (replace-match ""))
  994. (org-table-align)))
  995. (defun org-capture-place-plain-text ()
  996. "Place the template plainly.
  997. If the target locator points at an Org node, place the template into
  998. the text of the entry, before the first child. If not, place the
  999. template at the beginning or end of the file.
  1000. Of course, if exact position has been required, just put it there."
  1001. (let* ((txt (org-capture-get :template))
  1002. beg end)
  1003. (cond
  1004. ((org-capture-get :exact-position)
  1005. (goto-char (org-capture-get :exact-position)))
  1006. ((and (org-capture-get :target-entry-p)
  1007. (bolp)
  1008. (looking-at org-outline-regexp))
  1009. ;; we should place the text into this entry
  1010. (if (org-capture-get :prepend)
  1011. ;; Skip meta data and drawers
  1012. (org-end-of-meta-data-and-drawers)
  1013. ;; go to ent of the entry text, before the next headline
  1014. (outline-next-heading)))
  1015. (t
  1016. ;; beginning or end of file
  1017. (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
  1018. (or (bolp) (newline))
  1019. (org-capture-empty-lines-before)
  1020. (setq beg (point))
  1021. (insert txt)
  1022. (org-capture-empty-lines-after 1)
  1023. (org-capture-position-for-last-stored beg)
  1024. (setq end (point))
  1025. (org-capture-mark-kill-region beg (1- end))
  1026. (org-capture-narrow beg (1- end))
  1027. (if (or (re-search-backward "%\\?" beg t)
  1028. (re-search-forward "%\\?" end t))
  1029. (replace-match ""))))
  1030. (defun org-capture-mark-kill-region (beg end)
  1031. "Mark the region that will have to be killed when aborting capture."
  1032. (let ((m1 (move-marker (make-marker) beg))
  1033. (m2 (move-marker (make-marker) end)))
  1034. (org-capture-put :begin-marker m1)
  1035. (org-capture-put :end-marker m2)))
  1036. (defun org-capture-position-for-last-stored (where)
  1037. "Memorize the position that should later become the position of last capture."
  1038. (cond
  1039. ((integerp where)
  1040. (org-capture-put :position-for-last-stored
  1041. (move-marker (make-marker) where
  1042. (or (buffer-base-buffer (current-buffer))
  1043. (current-buffer)))))
  1044. ((eq where 'table-line)
  1045. (org-capture-put :position-for-last-stored
  1046. (list 'table-line
  1047. (org-table-current-dline))))
  1048. (t (error "This should not happen"))))
  1049. (defun org-capture-bookmark-last-stored-position ()
  1050. "Bookmark the last-captured position."
  1051. (let* ((where (org-capture-get :position-for-last-stored 'local))
  1052. (pos (cond
  1053. ((markerp where)
  1054. (prog1 (marker-position where)
  1055. (move-marker where nil)))
  1056. ((and (listp where) (eq (car where) 'table-line))
  1057. (if (org-at-table-p)
  1058. (save-excursion
  1059. (org-table-goto-line (nth 1 where))
  1060. (point-at-bol))
  1061. (point))))))
  1062. (with-current-buffer (buffer-base-buffer (current-buffer))
  1063. (save-excursion
  1064. (save-restriction
  1065. (widen)
  1066. (goto-char pos)
  1067. (bookmark-set "org-capture-last-stored")
  1068. (move-marker org-capture-last-stored-marker (point)))))))
  1069. (defun org-capture-narrow (beg end)
  1070. "Narrow, unless configuration says not to narrow."
  1071. (unless (org-capture-get :unnarrowed)
  1072. (narrow-to-region beg end)
  1073. (goto-char beg)))
  1074. (defun org-capture-empty-lines-before (&optional n)
  1075. "Arrange for the correct number of empty lines before the insertion point.
  1076. Point will be after the empty lines, so insertion can directly be done."
  1077. (setq n (or n (org-capture-get :empty-lines-before)
  1078. (org-capture-get :empty-lines) 0))
  1079. (let ((pos (point)))
  1080. (org-back-over-empty-lines)
  1081. (delete-region (point) pos)
  1082. (if (> n 0) (newline n))))
  1083. (defun org-capture-empty-lines-after (&optional n)
  1084. "Arrange for the correct number of empty lines after the inserted string.
  1085. Point will remain at the first line after the inserted text."
  1086. (setq n (or n (org-capture-get :empty-lines-after)
  1087. (org-capture-get :empty-lines) 0))
  1088. (org-back-over-empty-lines)
  1089. (while (looking-at "[ \t]*\n") (replace-match ""))
  1090. (let ((pos (point)))
  1091. (if (> n 0) (newline n))
  1092. (goto-char pos)))
  1093. (defvar org-clock-marker) ; Defined in org.el
  1094. ;;;###autoload
  1095. (defun org-capture-insert-template-here ()
  1096. (let* ((template (org-capture-get :template))
  1097. (type (org-capture-get :type))
  1098. beg end pp)
  1099. (or (bolp) (newline))
  1100. (setq beg (point))
  1101. (cond
  1102. ((and (eq type 'entry) (derived-mode-p 'org-mode))
  1103. (org-capture-verify-tree (org-capture-get :template))
  1104. (org-paste-subtree nil template t))
  1105. ((and (memq type '(item checkitem))
  1106. (derived-mode-p 'org-mode)
  1107. (save-excursion (skip-chars-backward " \t\n")
  1108. (setq pp (point))
  1109. (org-in-item-p)))
  1110. (goto-char pp)
  1111. (org-insert-item)
  1112. (skip-chars-backward " ")
  1113. (skip-chars-backward "-+*0123456789).")
  1114. (delete-region (point) (point-at-eol))
  1115. (setq beg (point))
  1116. (org-remove-indentation template)
  1117. (insert template)
  1118. (org-capture-empty-lines-after)
  1119. (goto-char beg)
  1120. (org-list-repair)
  1121. (org-end-of-item)
  1122. (setq end (point)))
  1123. (t (insert template)))
  1124. (setq end (point))
  1125. (goto-char beg)
  1126. (if (re-search-forward "%\\?" end t)
  1127. (replace-match ""))))
  1128. (defun org-capture-set-plist (entry)
  1129. "Initialize the property list from the template definition."
  1130. (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
  1131. (org-capture-put :key (car entry) :description (nth 1 entry)
  1132. :target (nth 3 entry))
  1133. (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
  1134. (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
  1135. ;; The template may be empty or omitted for special types.
  1136. ;; Here we insert the default templates for such cases.
  1137. (cond
  1138. ((eq type 'item) (setq txt "- %?"))
  1139. ((eq type 'checkitem) (setq txt "- [ ] %?"))
  1140. ((eq type 'table-line) (setq txt "| %? |"))
  1141. ((member type '(nil entry)) (setq txt "* %?\n %a"))))
  1142. (org-capture-put :template txt :type type)))
  1143. (defun org-capture-goto-target (&optional template-key)
  1144. "Go to the target location of a capture template.
  1145. The user is queried for the template."
  1146. (interactive)
  1147. (let* (org-select-template-temp-major-mode
  1148. (entry (org-capture-select-template template-key)))
  1149. (unless entry
  1150. (error "No capture template selected"))
  1151. (org-capture-set-plist entry)
  1152. (org-capture-set-target-location)
  1153. (org-pop-to-buffer-same-window (org-capture-get :buffer))
  1154. (goto-char (org-capture-get :pos))))
  1155. (defun org-capture-get-indirect-buffer (&optional buffer prefix)
  1156. "Make an indirect buffer for a capture process.
  1157. Use PREFIX as a prefix for the name of the indirect buffer."
  1158. (setq buffer (or buffer (current-buffer)))
  1159. (let ((n 1) (base (buffer-name buffer)) bname)
  1160. (setq bname (concat prefix "-" base))
  1161. (while (buffer-live-p (get-buffer bname))
  1162. (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
  1163. (condition-case nil
  1164. (make-indirect-buffer buffer bname 'clone)
  1165. (error
  1166. (let ((buf (make-indirect-buffer buffer bname)))
  1167. (with-current-buffer buf (org-mode))
  1168. buf)))))
  1169. (defun org-capture-verify-tree (tree)
  1170. "Throw error if TREE is not a valid tree."
  1171. (unless (org-kill-is-subtree-p tree)
  1172. (error "Template is not a valid Org entry or tree")))
  1173. ;;; The template code
  1174. (defun org-capture-select-template (&optional keys)
  1175. "Select a capture template.
  1176. Lisp programs can force the template by setting KEYS to a string."
  1177. (let ((org-capture-templates
  1178. (or (org-contextualize-agenda-or-capture
  1179. org-capture-templates org-capture-templates-contexts)
  1180. '(("t" "Task" entry (file+headline "" "Tasks")
  1181. "* TODO %?\n %u\n %a")))))
  1182. (if keys
  1183. (or (assoc keys org-capture-templates)
  1184. (error "No capture template referred to by \"%s\" keys" keys))
  1185. (org-mks org-capture-templates
  1186. "Select a capture template\n========================="
  1187. "Template key: "
  1188. '(("C" "Customize org-capture-templates")
  1189. ("q" "Abort"))))))
  1190. (defun org-capture-fill-template (&optional template initial annotation)
  1191. "Fill a template and return the filled template as a string.
  1192. The template may still contain \"%?\" for cursor positioning."
  1193. (setq template (or template (org-capture-get :template)))
  1194. (when (stringp initial)
  1195. (setq initial (org-no-properties initial)))
  1196. (let* ((buffer (org-capture-get :buffer))
  1197. (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
  1198. (ct (org-capture-get :default-time))
  1199. (dct (decode-time ct))
  1200. (ct1
  1201. (if (< (nth 2 dct) org-extend-today-until)
  1202. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  1203. ct))
  1204. (plist-p (if org-store-link-plist t nil))
  1205. (v-c (and (> (length kill-ring) 0) (current-kill 0)))
  1206. (v-x (or (org-get-x-clipboard 'PRIMARY)
  1207. (org-get-x-clipboard 'CLIPBOARD)
  1208. (org-get-x-clipboard 'SECONDARY)))
  1209. (v-t (format-time-string (car org-time-stamp-formats) ct))
  1210. (v-T (format-time-string (cdr org-time-stamp-formats) ct))
  1211. (v-u (concat "[" (substring v-t 1 -1) "]"))
  1212. (v-U (concat "[" (substring v-T 1 -1) "]"))
  1213. ;; `initial' and `annotation' might habe been passed.
  1214. ;; But if the property list has them, we prefer those values
  1215. (v-i (or (plist-get org-store-link-plist :initial)
  1216. initial
  1217. (org-capture-get :initial)
  1218. ""))
  1219. (v-a (or (plist-get org-store-link-plist :annotation)
  1220. annotation
  1221. (org-capture-get :annotation)
  1222. ""))
  1223. ;; Is the link empty? Then we do not want it...
  1224. (v-a (if (equal v-a "[[]]") "" v-a))
  1225. (clipboards (remove nil (list v-i
  1226. (org-get-x-clipboard 'PRIMARY)
  1227. (org-get-x-clipboard 'CLIPBOARD)
  1228. (org-get-x-clipboard 'SECONDARY)
  1229. v-c)))
  1230. (l-re "\\[\\[\\(.*?\\)\\]\\(\\[.*?\\]\\)?\\]")
  1231. (v-A (if (and v-a (string-match l-re v-a))
  1232. (replace-match "[[\\1][%^{Link description}]]" nil nil v-a)
  1233. v-a))
  1234. (v-l (if (and v-a (string-match l-re v-a))
  1235. (replace-match "\\1" nil nil v-a)
  1236. v-a))
  1237. (v-n user-full-name)
  1238. (v-k (if (marker-buffer org-clock-marker)
  1239. (org-no-properties org-clock-heading)))
  1240. (v-K (if (marker-buffer org-clock-marker)
  1241. (org-make-link-string
  1242. (buffer-file-name (marker-buffer org-clock-marker))
  1243. org-clock-heading)))
  1244. (v-f (or (org-capture-get :original-file-nondirectory) ""))
  1245. (v-F (or (org-capture-get :original-file) ""))
  1246. v-I
  1247. (org-startup-folded nil)
  1248. (org-inhibit-startup t)
  1249. org-time-was-given org-end-time-was-given x
  1250. prompt completions char time pos default histvar strings)
  1251. (setq org-store-link-plist
  1252. (plist-put org-store-link-plist :annotation v-a)
  1253. org-store-link-plist
  1254. (plist-put org-store-link-plist :initial v-i))
  1255. (setq initial v-i)
  1256. (unless template (setq template "") (message "No template") (ding)
  1257. (sit-for 1))
  1258. (save-window-excursion
  1259. (delete-other-windows)
  1260. (org-pop-to-buffer-same-window (get-buffer-create "*Capture*"))
  1261. (erase-buffer)
  1262. (insert template)
  1263. (goto-char (point-min))
  1264. (org-capture-steal-local-variables buffer)
  1265. (setq buffer-file-name nil)
  1266. ;; %[] Insert contents of a file.
  1267. (goto-char (point-min))
  1268. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  1269. (unless (org-capture-escaped-%)
  1270. (let ((start (match-beginning 0))
  1271. (end (match-end 0))
  1272. (filename (expand-file-name (match-string 1))))
  1273. (goto-char start)
  1274. (delete-region start end)
  1275. (condition-case error
  1276. (insert-file-contents filename)
  1277. (error (insert (format "%%![Couldn't insert %s: %s]"
  1278. filename error)))))))
  1279. ;; %() embedded elisp
  1280. (org-capture-expand-embedded-elisp)
  1281. ;; The current time
  1282. (goto-char (point-min))
  1283. (while (re-search-forward "%<\\([^>\n]+\\)>" nil t)
  1284. (replace-match (format-time-string (match-string 1)) t t))
  1285. ;; Simple %-escapes
  1286. (goto-char (point-min))
  1287. (while (re-search-forward "%\\([tTuUaliAcxkKInfF]\\)" nil t)
  1288. (unless (org-capture-escaped-%)
  1289. (when (and initial (equal (match-string 0) "%i"))
  1290. (save-match-data
  1291. (let* ((lead (buffer-substring
  1292. (point-at-bol) (match-beginning 0))))
  1293. (setq v-i (mapconcat 'identity
  1294. (org-split-string initial "\n")
  1295. (concat "\n" lead))))))
  1296. (replace-match
  1297. (or (eval (intern (concat "v-" (match-string 1)))) "")
  1298. t t)))
  1299. ;; From the property list
  1300. (when plist-p
  1301. (goto-char (point-min))
  1302. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  1303. (unless (org-capture-escaped-%)
  1304. (and (setq x (or (plist-get org-store-link-plist
  1305. (intern (match-string 1))) ""))
  1306. (replace-match x t t)))))
  1307. ;; Turn on org-mode in temp buffer, set local variables
  1308. ;; This is to support completion in interactive prompts
  1309. (let ((org-inhibit-startup t)) (org-mode))
  1310. ;; Interactive template entries
  1311. (goto-char (point-min))
  1312. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?"
  1313. nil t)
  1314. (unless (org-capture-escaped-%)
  1315. (setq char (if (match-end 3) (match-string-no-properties 3))
  1316. prompt (if (match-end 2) (match-string-no-properties 2)))
  1317. (goto-char (match-beginning 0))
  1318. (replace-match "")
  1319. (setq completions nil default nil)
  1320. (when prompt
  1321. (setq completions (org-split-string prompt "|")
  1322. prompt (pop completions)
  1323. default (car completions)
  1324. histvar (intern (concat
  1325. "org-capture-template-prompt-history::"
  1326. (or prompt "")))
  1327. completions (mapcar 'list completions)))
  1328. (unless (boundp histvar) (set histvar nil))
  1329. (cond
  1330. ((member char '("G" "g"))
  1331. (let* ((org-last-tags-completion-table
  1332. (org-global-tags-completion-table
  1333. (if (equal char "G")
  1334. (org-agenda-files)
  1335. (and file (list file)))))
  1336. (org-add-colon-after-tag-completion t)
  1337. (ins (org-icompleting-read
  1338. (if prompt (concat prompt ": ") "Tags: ")
  1339. 'org-tags-completion-function nil nil nil
  1340. 'org-tags-history)))
  1341. (setq ins (mapconcat 'identity
  1342. (org-split-string
  1343. ins (org-re "[^[:alnum:]_@#%]+"))
  1344. ":"))
  1345. (when (string-match "\\S-" ins)
  1346. (or (equal (char-before) ?:) (insert ":"))
  1347. (insert ins)
  1348. (or (equal (char-after) ?:) (insert ":"))
  1349. (and (org-at-heading-p) (org-set-tags nil 'align)))))
  1350. ((equal char "C")
  1351. (cond ((= (length clipboards) 1) (insert (car clipboards)))
  1352. ((> (length clipboards) 1)
  1353. (insert (read-string "Clipboard/kill value: "
  1354. (car clipboards) '(clipboards . 1)
  1355. (car clipboards))))))
  1356. ((equal char "L")
  1357. (cond ((= (length clipboards) 1)
  1358. (org-insert-link 0 (car clipboards)))
  1359. ((> (length clipboards) 1)
  1360. (org-insert-link 0 (read-string "Clipboard/kill value: "
  1361. (car clipboards)
  1362. '(clipboards . 1)
  1363. (car clipboards))))))
  1364. ((equal char "p")
  1365. (org-set-property (org-no-properties prompt) nil))
  1366. (char
  1367. ;; These are the date/time related ones
  1368. (setq org-time-was-given (equal (upcase char) char))
  1369. (setq time (org-read-date (equal (upcase char) char) t nil
  1370. prompt))
  1371. (if (equal (upcase char) char) (setq org-time-was-given t))
  1372. (org-insert-time-stamp time org-time-was-given
  1373. (member char '("u" "U"))
  1374. nil nil (list org-end-time-was-given)))
  1375. (t
  1376. (let (org-completion-use-ido)
  1377. (push (org-completing-read-no-i
  1378. (concat (if prompt prompt "Enter string")
  1379. (if default (concat " [" default "]"))
  1380. ": ")
  1381. completions nil nil nil histvar default)
  1382. strings)
  1383. (insert (car strings)))))))
  1384. ;; Replace %n escapes with nth %^{...} string
  1385. (setq strings (nreverse strings))
  1386. (goto-char (point-min))
  1387. (while (re-search-forward "%\\\\\\([1-9][0-9]*\\)" nil t)
  1388. (unless (org-capture-escaped-%)
  1389. (replace-match
  1390. (nth (1- (string-to-number (match-string 1))) strings)
  1391. nil t)))
  1392. ;; Make sure there are no empty lines before the text, and that
  1393. ;; it ends with a newline character
  1394. (goto-char (point-min))
  1395. (while (looking-at "[ \t]*\n") (replace-match ""))
  1396. (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
  1397. ;; Return the expanded template and kill the temporary buffer
  1398. (untabify (point-min) (point-max))
  1399. (set-buffer-modified-p nil)
  1400. (prog1 (buffer-string) (kill-buffer (current-buffer))))))
  1401. (defun org-capture-escaped-% ()
  1402. "Check if % was escaped - if yes, unescape it now."
  1403. (if (equal (char-before (match-beginning 0)) ?\\)
  1404. (progn
  1405. (delete-region (1- (match-beginning 0)) (match-beginning 0))
  1406. t)
  1407. nil))
  1408. (defun org-capture-expand-embedded-elisp ()
  1409. "Evaluate embedded elisp %(sexp) and replace with the result."
  1410. (goto-char (point-min))
  1411. (while (re-search-forward "%(" nil t)
  1412. (unless (org-capture-escaped-%)
  1413. (goto-char (match-beginning 0))
  1414. (let ((template-start (point)))
  1415. (forward-char 1)
  1416. (let ((result (org-eval (read (current-buffer)))))
  1417. (delete-region template-start (point))
  1418. (insert result))))))
  1419. (defun org-capture-inside-embedded-elisp-p ()
  1420. "Return non-nil if point is inside of embedded elisp %(sexp)."
  1421. (let (beg end)
  1422. (with-syntax-table emacs-lisp-mode-syntax-table
  1423. (save-excursion
  1424. ;; `looking-at' and `search-backward' below do not match the "%(" if
  1425. ;; point is in its middle
  1426. (when (equal (char-before) ?%)
  1427. (backward-char))
  1428. (save-match-data
  1429. (when (or (looking-at "%(") (search-backward "%(" nil t))
  1430. (setq beg (point))
  1431. (setq end (progn (forward-char) (forward-sexp) (1- (point)))))))
  1432. (when (and beg end)
  1433. (and (<= (point) end) (>= (point) beg))))))
  1434. ;;;###autoload
  1435. (defun org-capture-import-remember-templates ()
  1436. "Set org-capture-templates to be similar to `org-remember-templates'."
  1437. (interactive)
  1438. (when (and (yes-or-no-p
  1439. "Import old remember templates into org-capture-templates? ")
  1440. (yes-or-no-p
  1441. "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
  1442. (require 'org-remember)
  1443. (setq org-capture-templates
  1444. (mapcar
  1445. (lambda (entry)
  1446. (let ((desc (car entry))
  1447. (key (char-to-string (nth 1 entry)))
  1448. (template (nth 2 entry))
  1449. (file (or (nth 3 entry) org-default-notes-file))
  1450. (position (or (nth 4 entry) org-remember-default-headline))
  1451. (type 'entry)
  1452. (prepend org-reverse-note-order)
  1453. immediate target)
  1454. (cond
  1455. ((member position '(top bottom))
  1456. (setq target (list 'file file)
  1457. prepend (eq position 'top)))
  1458. ((eq position 'date-tree)
  1459. (setq target (list 'file+datetree file)
  1460. prepend nil))
  1461. (t (setq target (list 'file+headline file position))))
  1462. (when (string-match "%!" template)
  1463. (setq template (replace-match "" t t template)
  1464. immediate t))
  1465. (append (list key desc type target template)
  1466. (if prepend '(:prepend t))
  1467. (if immediate '(:immediate-finish t)))))
  1468. org-remember-templates))))
  1469. (provide 'org-capture)
  1470. ;;; org-capture.el ends here