org-capture.el 69 KB

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