org-capture.el 68 KB

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