org-capture.el 69 KB

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