org-capture.el 61 KB

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