org-capture.el 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. ;;; org-capture.el --- Fast note taking in Org-mode
  2. ;; Copyright (C) 2010 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. ;; Version: 6.36trans
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; This file contains an alternaive implementation of the same functionality
  24. ;; that is also provided by org-remember.el. The implementation is more
  25. ;; streamlined, can produce more target types (e.g. plain list items or
  26. ;; table lines). Also, it does not use a temporary buffer for editing
  27. ;; the captured entry - instead it uses an indirect buffer that visits
  28. ;; the new entry already in the target buffer (this was an idea by Samuel
  29. ;; Wales). John Wiegley's excellent `remember.el' is not needed for this
  30. ;; implementation, even though we borrow heavily from its ideas.
  31. ;; This implementation heavily draws on ideas by James TD Smith and
  32. ;; Samuel Wales, and, of cause, uses John Wiegley's remember.el as inspiration.
  33. ;;; TODO
  34. ;; - find a clever way to not always insert an annotation maybe a
  35. ;; predicate function that can check for conditions for %a to be
  36. ;; used. This could be one of the properties.
  37. ;; - Should there be plist members that arrange for properties to be
  38. ;; asked for, like James proposed in his RFC?
  39. ;;; Code:
  40. (eval-when-compile
  41. (require 'cl))
  42. (require 'org)
  43. (require 'org-mks)
  44. (declare-function org-datetree-find-date-create "org-datetree"
  45. (DATE &optional KEEP-RESTRICTION))
  46. (declare-function org-table-get-specials "org-table" ())
  47. (defvar org-remember-default-headline)
  48. (defvar org-remember-templates)
  49. (defvar org-table-hlines)
  50. (defvar org-capture-clock-was-started nil
  51. "Internal flag, noting if the clock was started.")
  52. (defvar org-capture-last-stored-marker (make-marker)
  53. "Marker pointing to the entry most recently stored with `org-capture'.")
  54. ;; The following variable is scoped dynamically by org-protocol
  55. ;; to indicate that the link properties have already been stored
  56. (defvar org-capture-link-is-already-stored nil)
  57. (defgroup org-capture nil
  58. "Options concerning capturing new entries."
  59. :tag "Org Capture"
  60. :group 'org)
  61. (defcustom org-capture-templates nil
  62. "Templates for the creation of new entries.
  63. Each entry is a list with the following items:
  64. keys The keys that will select the template, as a string, characters
  65. only, for example \"a\" for a template to be selected with a
  66. single key, or \"bt\" for selection with two keys. When using
  67. several keys, keys using the same prefix key must be together
  68. in the list and preceded by a 2-element entry explaining the
  69. prefix key, for example
  70. (\"b\" \"Templates for marking stuff to buy\")
  71. The \"C\" key is used by default for quick access to the
  72. customization of the template variable. But if you want to use
  73. that key for a template, you can.
  74. description A short string describing the template, will be shown during
  75. selection.
  76. type The type of entry. Valid are:
  77. entry an Org-mode node, with a headline. Will be
  78. filed as the child of the target entry or as
  79. a top-level entry.
  80. item a plain list item, placed in the first plain
  81. list a the target location.
  82. checkitem a checkbox item. This only differs from the
  83. plain lis item by the default template.
  84. table-line a new line in the first table at target location.
  85. plain text to be inserted as it is.
  86. target Specification of where the captured item should be placed.
  87. In Org-mode files, targets usually define a node. Entries will
  88. become children of this node, other types will be added to the
  89. table or list in the body of this node.
  90. Valid values are:
  91. (file \"path/to/file\")
  92. Text will be placed at the beginning or end of that file
  93. (id \"id of existing org entry\")
  94. Filing as child of this entry, or in the body of the entry
  95. (file+headline \"path/to/file\" \"node headline\")
  96. Fast configuration if the target heading is unique in the file
  97. (file+olp \"path/to/file\" \"Level 1 heading\" \"Level 2\" ...)
  98. For non-unique headings, the full path is safer
  99. (file+regexp \"path/to/file\" \"regexp to find location\")
  100. (file+datetree \"path/to/file\")
  101. Will create a heading in a date tree.
  102. (file+function \"path/to/file\" function-finding-location)
  103. A function to find the right location in the file.
  104. (clock)
  105. File to the entry that is currently being clocked.
  106. (function function-finding-location)
  107. Most general way, write your own function to find both
  108. file and location.
  109. template The template for creating the capture item. If you leave this
  110. empty, an appropriate default template will be used. See below
  111. for more details.
  112. The rest of the entry is a property list of additional options. Recognized
  113. properties are:
  114. :prepend Normally new captured information will be appended at
  115. the target location (last child, last table line,
  116. last list item...). Setting this property will
  117. change that.
  118. :immediate-finish When set, do not offer to edit the information, just
  119. file it away immediately. This makes sense if the
  120. template only needs information that can be added
  121. automatically.
  122. :empty-lines Set this to the number of lines the should be inserted
  123. before and after the new item. Default 0, only common
  124. other value is 1.
  125. :clock-in Start the clock in this item.
  126. :clock-resume Start the interrupted clock when finishing the capture.
  127. :unnarrowed Do not narrow the target buffer, simply show the
  128. full buffer. Default is to narrow it so that you
  129. only see the new stuff.
  130. :table-line-pos Specification of the location in the table where the
  131. new line should be inserted. It looks like \"II-3\"
  132. which means that the new line should become the third
  133. line before the second horizontal separaor line.
  134. The template defined the text to be inserted. Often then this is an org-mode
  135. entry (so the first line should start with a star) that will be filed as a
  136. child of the target headline. It can also be freely formatted text.
  137. Furthermore, the following %-escapes will be replaced with content:
  138. %^{prompt} Prompt the user for a string and replace this sequence with it.
  139. A default value and a completion table ca be specified like this:
  140. %^{prompt|default|completion2|completion3|...}
  141. %t time stamp, date only
  142. %T time stamp with date and time
  143. %u, %U like the above, but inactive time stamps
  144. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
  145. You may define a prompt like %^{Please specify birthday
  146. %n user name (taken from `user-full-name')
  147. %a annotation, normally the link created with `org-store-link'
  148. %i initial content, copied from the active region. If %i is
  149. indented, the entire inserted text will be indented as well.
  150. %c current kill ring head
  151. %x content of the X clipboard
  152. %^C Interactive selection of which kill or clip to use
  153. %^L Like %^C, but insert as link
  154. %k title of currently clocked task
  155. %K link to currently clocked task
  156. %^g prompt for tags, with completion on tags in target file
  157. %^G prompt for tags, with completion all tags in all agenda files
  158. %^{prop}p Prompt the user for a value for property `prop'
  159. %:keyword specific information for certain link types, see below
  160. %[pathname] insert the contents of the file given by `pathname'
  161. %(sexp) evaluate elisp `(sexp)' and replace with the result
  162. %? After completing the template, position cursor here.
  163. Apart from these general escapes, you can access information specific to the
  164. link type that is created. For example, calling `org-capture' in emails
  165. or gnus will record the author and the subject of the message, which you
  166. can access with \"%:author\" and \"%:subject\", respectively. Here is a
  167. complete list of what is recorded for each link type.
  168. Link type | Available information
  169. -------------------+------------------------------------------------------
  170. bbdb | %:type %:name %:company
  171. vm, wl, mh, rmail | %:type %:subject %:message-id
  172. | %:from %:fromname %:fromaddress
  173. | %:to %:toname %:toaddress
  174. | %:fromto (either \"to NAME\" or \"from NAME\")
  175. gnus | %:group, for messages also all email fields
  176. w3, w3m | %:type %:url
  177. info | %:type %:file %:node
  178. calendar | %:type %:date"
  179. :group 'org-capture
  180. :type
  181. '(repeat
  182. (choice :value ("" "" entry (file "~/org/notes.org") "")
  183. (list :tag "Multikey description"
  184. (string :tag "Keys ")
  185. (string :tag "Description"))
  186. (list :tag "Template entry"
  187. (string :tag "Keys ")
  188. (string :tag "Description ")
  189. (choice :tag "Capture Type " :value entry
  190. (const :tag "Org entry" entry)
  191. (const :tag "Plain list item" item)
  192. (const :tag "Checkbox item" checkitem)
  193. (const :tag "Plain text" plain)
  194. (const :tag "Table line" table-line))
  195. (choice :tag "Target location"
  196. (list :tag "File"
  197. (const :format "" file)
  198. (file :tag " File"))
  199. (list :tag "ID"
  200. (const :format "" id)
  201. (string :tag " ID"))
  202. (list :tag "File & Headline"
  203. (const :format "" file+headline)
  204. (file :tag " File ")
  205. (string :tag " Headline"))
  206. (list :tag "File & Outline path"
  207. (const :format "" file+olp)
  208. (file :tag " File ")
  209. (repeat :tag "Outline path" :inline t
  210. (string :tag "Headline")))
  211. (list :tag "File & Regexp"
  212. (const :format "" file+regexp)
  213. (file :tag " File ")
  214. (regexp :tag " Regexp"))
  215. (list :tag "File & Date tree"
  216. (const :format "" file+datetree)
  217. (file :tag " File"))
  218. (list :tag "File & function"
  219. (const :format "" file+function)
  220. (file :tag " File ")
  221. (sexp :tag " Function"))
  222. (list :tag "Current clocking task"
  223. (const :format "" clock))
  224. (list :tag "Function"
  225. (const :format "" function)
  226. (sexp :tag " Function")))
  227. (string :tag "Template (opt) ")
  228. (plist :inline t
  229. ;; Give the most common options as checkboxes
  230. :options (((const :format "%v " :prepend) (const t))
  231. ((const :format "%v " :immediate-finish) (const t))
  232. ((const :format "%v " :empty-lines) (const 1))
  233. ((const :format "%v " :clock-in) (const t))
  234. ((const :format "%v " :clock-resume) (const t))
  235. ((const :format "%v " :unnarrowed) (const t))))))))
  236. (defcustom org-capture-before-finalize-hook nil
  237. "Hook that is run right before a remember process is finalized.
  238. The remember buffer is still current when this hook runs."
  239. :group 'org-capture
  240. :type 'hook)
  241. ;;; The property list for keeping information about the capture process
  242. (defvar org-capture-plist nil
  243. "Plist for the current capture process, global, to avoid having to pass it.")
  244. (defvar org-capture-current-plist nil
  245. "Local variable holding the plist in a capture buffer.
  246. This is used to store the plist for use when finishing a capture process.
  247. Another such process might have changed the global varaible by then.")
  248. (defun org-capture-put (&rest stuff)
  249. (while stuff
  250. (setq org-capture-plist (plist-put org-capture-plist
  251. (pop stuff) (pop stuff)))))
  252. (defun org-capture-get (prop &optional local)
  253. (plist-get (if local org-capture-current-plist org-capture-plist) prop))
  254. (defun org-capture-member (prop)
  255. (plist-get org-capture-plist prop))
  256. ;;; The minor mode
  257. (defvar org-capture-mode-map (make-sparse-keymap)
  258. "Keymap for org-capture-mode, a minor mode.
  259. Use this map to set additional keybindings for when Org-mode is used
  260. for a Remember buffer.")
  261. (defvar org-capture-mode-hook nil
  262. "Hook for the minor `org-capture-mode'.")
  263. (define-minor-mode org-capture-mode
  264. "Minor mode for special key bindings in a remember buffer."
  265. nil " Rem" org-capture-mode-map
  266. (org-set-local
  267. 'header-line-format
  268. "Capture buffer. Finish `C-c C-c', refile `C-c C-w', abort `C-c C-k'.")
  269. (run-hooks 'org-capture-mode-hook))
  270. (define-key org-capture-mode-map "\C-c\C-c" 'org-capture-finalize)
  271. (define-key org-capture-mode-map "\C-c\C-k" 'org-capture-kill)
  272. (define-key org-capture-mode-map "\C-c\C-w" 'org-capture-refile)
  273. ;;; The main commands
  274. ;;;###autoload
  275. (defun org-capture (&optional goto keys)
  276. "Capture something.
  277. This will let you select a template from org-capture-templates, and then
  278. file new captured information. The text is immediately inserted at the
  279. target location, and an indirect buffer is shown where you can edit it.
  280. Pressing `C-c C-c' brings you back to the previous state of Emacs,
  281. so that you can continue your work.
  282. When called interactively with a `C-u' prefix argument GOTO, don't capture
  283. anything, just go to the file/headline where the selected template
  284. stores its notes. With a double prefix arg `C-u C-u', go to the last
  285. note stored.
  286. When called with a `C-0' (zero) prefix, insert a template at point.
  287. Lisp programs can set KEYS to a string associated with a template in
  288. `org-capture-templates'. In this case, interactive selection will be
  289. bypassed."
  290. (interactive "P")
  291. (cond
  292. ((equal goto '(4)) (org-capture-goto-target))
  293. ((equal goto '(16)) (org-capture-goto-last-stored))
  294. (t
  295. ;; FIXME: Are these needed?
  296. (let* ((orig-buf (current-buffer))
  297. (annotation (if org-capture-link-is-already-stored
  298. (plist-get org-store-link-plist :annotation)
  299. (org-store-link nil)))
  300. (initial (and (org-region-active-p)
  301. (buffer-substring (point) (mark))))
  302. (entry (org-capture-select-template keys)))
  303. (cond
  304. ((equal entry "C")
  305. (customize-variable 'org-capture-templates))
  306. ((equal entry "q")
  307. (error "Abort"))
  308. (t
  309. (org-capture-set-plist entry)
  310. (org-capture-put :original-buffer orig-buf :annotation annotation
  311. :initial initial)
  312. (org-capture-put :default-time
  313. (or org-overriding-default-time
  314. (org-current-time)))
  315. (org-capture-set-target-location)
  316. (condition-case error
  317. (org-capture-put :template (org-capture-fill-template))
  318. ((error quit)
  319. (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
  320. (error "Capture abort: %s" error)))
  321. (if (equal goto 0)
  322. ;;insert at point
  323. (org-capture-insert-template-here)
  324. (org-capture-place-template)
  325. (if (org-capture-get :immediate-finish)
  326. (org-capture-finalize)
  327. (if (and (org-mode-p)
  328. (org-capture-get :clock-in))
  329. (condition-case nil
  330. (progn
  331. (if (org-clock-is-active)
  332. (org-capture-put :interrupted-clock
  333. (copy-marker org-clock-marker)))
  334. (org-clock-in)
  335. (org-set-local 'org-capture-clock-was-started t))
  336. (error
  337. "Could not start the clock in this capture buffer")))))))))))
  338. (defun org-capture-finalize ()
  339. "Finalize the capture process."
  340. (interactive)
  341. (unless (and org-capture-mode
  342. (buffer-base-buffer (current-buffer)))
  343. (error "This does not seem to be a capture buffer for Org-mode"))
  344. ;; Did we start the clock in this capture buffer?
  345. (when (and org-capture-clock-was-started
  346. org-clock-marker (marker-buffer org-clock-marker)
  347. (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
  348. (> org-clock-marker (point-min))
  349. (< org-clock-marker (point-max)))
  350. ;; Looks like the clock we started is still running. Clock out.
  351. (let (org-log-note-clock-out) (org-clock-out))
  352. (when (and (org-capture-get :clock-resume 'local)
  353. (markerp (org-capture-get :interrupted-clock 'local))
  354. (buffer-live-p (marker-buffer
  355. (org-capture-get :interrupted-clock 'local))))
  356. (org-with-point-at (org-capture-get :interrupted-clock 'local)
  357. (org-clock-in))
  358. (message "Interrupted clock has been resumed")))
  359. (let ((beg (point-min))
  360. (end (point-max))
  361. (abort-note nil))
  362. (widen)
  363. (if org-note-abort
  364. (let ((m1 (org-capture-get :begin-marker 'local))
  365. (m2 (org-capture-get :end-marker 'local)))
  366. (if (and m1 m2 (= m1 beg) (= m2 end))
  367. (progn
  368. (setq abort-note 'clean)
  369. (kill-region m1 m2))
  370. (setq abort-note 'dirty)))
  371. ;; Make sure that the empty lines after are correct
  372. (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
  373. (member (org-capture-get :type 'local)
  374. '(entry item checkitem plain)))
  375. (save-excursion
  376. (goto-char end)
  377. (org-capture-empty-lines-after
  378. (or (org-capture-get :empty-lines 'local) 0))))
  379. ;; Postprocessing: Update Statistics cookies, do the sorting
  380. (when (org-mode-p)
  381. (save-excursion
  382. (when (ignore-errors (org-back-to-heading))
  383. (org-update-parent-todo-statistics)
  384. (org-update-checkbox-count)))
  385. ;; FIXME Here we should do the sorting
  386. ;; If we have added a table line, maybe recompute?
  387. (when (and (eq (org-capture-get :type 'local) 'table-line)
  388. (org-at-table-p))
  389. (if (org-table-get-stored-formulas)
  390. (org-table-recalculate 'all) ;; FIXME: Should we iterate???
  391. (org-table-align)))
  392. )
  393. ;; Store this place as the last one where we stored something
  394. ;; Do the marking in the base buffer, so that it makes sense after
  395. ;; the indirect buffer has been killed.
  396. (let ((pos (point)))
  397. (with-current-buffer (buffer-base-buffer (current-buffer))
  398. (save-excursion
  399. (save-restriction
  400. (widen)
  401. (goto-char pos)
  402. (bookmark-set "org-capture-last-stored")
  403. (move-marker org-capture-last-stored-marker (point))))))
  404. ;; Run the hook
  405. (run-hooks 'org-capture-before-finalize-hook)
  406. )
  407. ;; Kill the indirect buffer
  408. (save-buffer)
  409. (let ((return-wconf (org-capture-get :return-to-wconf 'local)))
  410. (kill-buffer (current-buffer))
  411. ;; Restore the window configuration before capture
  412. (set-window-configuration return-wconf))
  413. (when abort-note
  414. (cond
  415. ((equal abort-note 'clean)
  416. (message "Capture process aborted and target file cleaned up"))
  417. ((equal abort-note 'dirty)
  418. (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))))
  419. (defun org-capture-refile ()
  420. "Finalize the current capture and then refile the entry.
  421. Refiling is done from the base buffer, because the indirect buffer is then
  422. already gone."
  423. (interactive)
  424. (unless (eq (org-capture-get :type 'local) 'entry)
  425. (error
  426. "Refiling from a capture buffer makes only sense for `entry'-type templates"))
  427. (let ((pos (point)) (base (buffer-base-buffer (current-buffer))))
  428. (org-capture-finalize)
  429. (save-window-excursion
  430. (with-current-buffer (or base (current-buffer))
  431. (save-excursion
  432. (save-restriction
  433. (widen)
  434. (goto-char pos)
  435. (call-interactively 'org-refile)))))))
  436. (defun org-capture-kill ()
  437. "Abort the current capture process."
  438. (interactive)
  439. ;; FIXME: This does not do the right thing, we need to remove the new stuff
  440. ;; By hand it is easy: undo, then kill the buffer
  441. (let ((org-note-abort t) (org-capture-before-finalize-hook nil))
  442. (org-capture-finalize)))
  443. (defun org-capture-goto-last-stored ()
  444. "Go to the location where the last remember note was stored."
  445. (interactive)
  446. (org-goto-marker-or-bmk org-capture-last-stored-marker
  447. "org-capture-last-stored")
  448. (message "This is the last note stored by a capture process"))
  449. ;;; Supporting functions for handling the process
  450. (defun org-capture-set-target-location (&optional target)
  451. "Find target buffer and position and store then in the property list."
  452. (let ((target-entry-p t))
  453. (setq target (or target (org-capture-get :target)))
  454. (save-excursion
  455. (cond
  456. ((eq (car target) 'file)
  457. (set-buffer (org-capture-target-buffer (nth 1 target)))
  458. (setq target-entry-p nil))
  459. ((eq (car target) 'id)
  460. (let ((loc (org-id-find (nth 1 target))))
  461. (if (not loc)
  462. (error "Cannot find target ID \"%s\"" (nth 1 target))
  463. (set-buffer (org-capture-target-buffer (car loc)))
  464. (goto-char (cdr loc)))))
  465. ((eq (car target) 'file+headline)
  466. (set-buffer (org-capture-target-buffer (nth 1 target)))
  467. (let ((hd (nth 2 target)))
  468. (goto-char (point-min))
  469. (if (re-search-forward
  470. (format org-complex-heading-regexp-format (regexp-quote hd))
  471. nil t)
  472. (goto-char (point-at-bol))
  473. (goto-char (point-max))
  474. (or (bolp) (insert "\n"))
  475. (insert "* " hd "\n")
  476. (beginning-of-line 0))))
  477. ((eq (car target) 'file+olp)
  478. (let ((m (org-find-olp (cdr target))))
  479. (set-buffer (marker-buffer m))
  480. (goto-char m)))
  481. ((eq (car target) 'file+regexp)
  482. (set-buffer (org-capture-target-buffer (nth 1 target)))
  483. (goto-char (point-min))
  484. (if (re-search-forward (nth 2 target) nil t)
  485. (progn
  486. (goto-char (match-beginning 0))
  487. (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
  488. (kill-buffer (current-buffer))
  489. (error "No match for target regexp in file %s" (nth 1 target))))
  490. ((eq (car target) 'file+datetree)
  491. (require 'org-datetree)
  492. (set-buffer (org-capture-target-buffer (nth 1 target)))
  493. ;; Make a date tree entry, with the current date (or yesterday,
  494. ;; if we are extending dates for a couple of hours)
  495. (org-datetree-find-date-create
  496. (calendar-gregorian-from-absolute
  497. (if org-overriding-default-time
  498. (time-to-days org-overriding-default-time)
  499. (time-to-days
  500. (time-subtract (current-time)
  501. (list 0 (* 3600 org-extend-today-until) 0)))))))
  502. ((eq (car target) 'file+function)
  503. (set-buffer (org-capture-target-buffer (nth 1 target)))
  504. (funcall (nth 1 target))
  505. (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
  506. ((eq (car target) 'clock)
  507. (if (and (markerp org-clock-hd-marker)
  508. (marker-buffer org-clock-hd-marker))
  509. (progn (set-buffer (marker-buffer org-clock-hd-marker))
  510. (goto-char org-clock-hd-marker))
  511. (error "No running clock that could be used as capture target")))
  512. (t (error "Invalid capture target specification")))
  513. (org-capture-put :buffer (current-buffer) :pos (point)
  514. :target-entry-p target-entry-p))))
  515. (defun org-capture-target-buffer (file)
  516. "Get a buffer for FILE."
  517. (or (org-find-base-buffer-visiting file)
  518. (find-file-noselect (expand-file-name file org-directory))))
  519. (defun org-capture-steal-local-variables (buffer)
  520. "Install Org-mode local variables"
  521. (mapc (lambda (v)
  522. (ignore-errors (org-set-local (car v) (cdr v))))
  523. (buffer-local-variables buffer)))
  524. (defun org-capture-place-template ()
  525. "Insert the template at the target location, and display the buffer."
  526. (org-capture-put :return-to-wconf (current-window-configuration))
  527. (delete-other-windows)
  528. (org-switch-to-buffer-other-window
  529. (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
  530. (show-all)
  531. (goto-char (org-capture-get :pos))
  532. (org-set-local 'org-capture-target-marker
  533. (move-marker (make-marker) (point)))
  534. (let* ((template (org-capture-get :template))
  535. (type (org-capture-get :type)))
  536. (case type
  537. ((nil entry) (org-capture-place-entry))
  538. (table-line (org-capture-place-table-line))
  539. (plain (org-capture-place-plain-text))
  540. (item (org-capture-place-item))))
  541. (org-capture-mode 1)
  542. (org-set-local 'org-capture-current-plist org-capture-plist))
  543. (defun org-capture-place-entry ()
  544. "Place the template as a new Org entry."
  545. (let* ((txt (org-capture-get :template))
  546. (reversed (org-capture-get :prepend))
  547. (target-entry-p (org-capture-get :target-entry-p))
  548. level beg end)
  549. (cond
  550. ((not target-entry-p)
  551. ;; Insert as top-level entry, either at beginning or at end of file
  552. (setq level 1)
  553. (if reversed
  554. (progn (goto-char (point-min))
  555. (outline-next-heading))
  556. (goto-char (point-max))
  557. (or (bolp) (insert "\n"))))
  558. (t
  559. ;; Insert as a child of the current entry
  560. (and (looking-at "\\*+")
  561. (setq level (- (match-end 0) (match-beginning 0))))
  562. (setq level (org-get-valid-level (or level 1) 1))
  563. (if reversed
  564. (progn
  565. (outline-next-heading)
  566. (or (bolp) (insert "\n")))
  567. (org-end-of-subtree t t)
  568. (or (bolp) (insert "\n")))))
  569. (org-capture-empty-lines-before)
  570. (setq beg (point))
  571. (org-paste-subtree level txt 'for-yank)
  572. (org-capture-empty-lines-after 1)
  573. (outline-next-heading)
  574. (setq end (point))
  575. (org-capture-mark-kill-region beg (1- end))
  576. (org-capture-narrow beg (1- end))
  577. (if (re-search-forward "%\\?" end t) (replace-match ""))))
  578. (defun org-capture-place-item ()
  579. "Place the template as a new plain list item."
  580. (let* ((txt (org-capture-get :template))
  581. (target-entry-p (org-capture-get :target-entry-p))
  582. ind beg end)
  583. (cond
  584. ((not target-entry-p)
  585. ;; Insert as top-level entry, either at beginning or at end of file
  586. (setq beg (point-min) end (point-max)))
  587. (t
  588. (setq beg (1+ (point-at-eol))
  589. end (save-excursion (outline-next-heading) (point)))))
  590. (if (org-capture-get :prepend)
  591. (progn
  592. (goto-char beg)
  593. (if (re-search-forward (concat "^" (org-item-re)) nil t)
  594. (progn
  595. (goto-char (match-beginning 0))
  596. (setq ind (org-get-indentation)))
  597. (goto-char end)
  598. (setq ind 0)))
  599. (goto-char end)
  600. (if (re-search-backward (concat "^" (org-item-re)) nil t)
  601. (progn
  602. (setq ind (org-get-indentation))
  603. (org-end-of-item))
  604. (setq ind 0)))
  605. ;; Remove common indentation
  606. (setq txt (org-remove-indentation txt))
  607. ;; Make sure this is indeed an item
  608. (unless (string-match (concat "\\`" (org-item-re)) txt)
  609. (setq txt (concat "- "
  610. (mapconcat 'identity (split-string txt "\n")
  611. "\n "))))
  612. ;; Set the correct indentation, depending on context
  613. (setq ind (make-string ind ?\ ))
  614. (setq txt (concat ind
  615. (mapconcat 'identity (split-string txt "\n")
  616. (concat "\n" ind))
  617. "\n"))
  618. ;; Insert, with surrounding empty lines
  619. (org-capture-empty-lines-before)
  620. (setq beg (point))
  621. (insert txt)
  622. (or (bolp) (insert "\n"))
  623. (org-capture-empty-lines-after 1)
  624. (forward-char 1)
  625. (setq end (point))
  626. (org-capture-mark-kill-region beg (1- end))
  627. (org-capture-narrow beg (1- end))
  628. (if (re-search-forward "%\\?" end t) (replace-match ""))))
  629. (defun org-capture-place-table-line ()
  630. "Place the template as a table line."
  631. (require 'org-table)
  632. (let* ((txt (org-capture-get :template))
  633. (target-entry-p (org-capture-get :target-entry-p))
  634. (table-line-pos (org-capture-get :table-line-pos))
  635. ind beg end)
  636. (cond
  637. ((not target-entry-p)
  638. ;; Table is not necessarily under a heading
  639. (setq beg (point-min) end (point-max)))
  640. (t
  641. ;; WE are at a heading, limit search to the body
  642. (setq beg (1+ (point-at-eol))
  643. end (save-excursion (outline-next-heading) (point)))))
  644. (if (re-search-forward org-table-dataline-regexp end t)
  645. (let ((b (org-table-begin)) (e (org-table-end)))
  646. (goto-char e)
  647. (if (looking-at "[ \t]*#\\+TBLFM:")
  648. (forward-line 1))
  649. (narrow-to-region b (point)))
  650. (goto-char end)
  651. (insert "\n\n")
  652. (narrow-to-region (1- (point)) (point)))
  653. ;; We are narrowed to the table, or to an empty line if there was no table
  654. ;; Check if the template is good
  655. (if (not (string-match org-table-dataline-regexp txt))
  656. (setq txt "| %?Bad template |\n"))
  657. (cond
  658. ((and table-line-pos
  659. (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
  660. ;; we have a complex line specification
  661. (goto-char (point-min))
  662. (let ((nh (- (match-end 1) (match-beginning 1)))
  663. (delta (string-to-number (match-string 2 table-line-pos)))
  664. ll)
  665. ;; The user wants a special position in the table
  666. (org-table-get-specials)
  667. (setq ll (aref org-table-hlines nh))
  668. (unless ll (error "Invalid table line specification \"%s\""
  669. table-line-pos))
  670. (setq ll (+ ll delta (if (< delta 0) 0 -1)))
  671. (org-goto-line ll)
  672. (org-table-insert-row 'below)
  673. (beginning-of-line 1)
  674. (delete-region (point) (1+ (point-at-eol)))
  675. (setq beg (point))
  676. (insert txt)
  677. (setq end (point))))
  678. ((org-capture-get :prepend)
  679. (goto-char (point-min))
  680. (re-search-forward org-table-hline-regexp nil t)
  681. (beginning-of-line 1)
  682. (re-search-forward org-table-dataline-regexp nil t)
  683. (beginning-of-line 1)
  684. (setq beg (point))
  685. (org-table-insert-row)
  686. (beginning-of-line 1)
  687. (delete-region (point) (1+ (point-at-eol)))
  688. (insert txt)
  689. (setq end (point)))
  690. (t
  691. (goto-char (point-max))
  692. (re-search-backward org-table-dataline-regexp nil t)
  693. (beginning-of-line 1)
  694. (org-table-insert-row 'below)
  695. (beginning-of-line 1)
  696. (delete-region (point) (1+ (point-at-eol)))
  697. (setq beg (point))
  698. (insert txt)
  699. (setq end (point))))
  700. (goto-char beg)
  701. (if (re-search-forward "%\\?" end t) (replace-match ""))
  702. (org-table-align)))
  703. (defun org-capture-place-plain-text ()
  704. "Place the template plainly."
  705. (let* ((txt (org-capture-get :template))
  706. beg end)
  707. (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))
  708. (or (bolp) (newline))
  709. (org-capture-empty-lines-before)
  710. (setq beg (point))
  711. (insert txt)
  712. (org-capture-empty-lines-after 1)
  713. (setq end (point))
  714. (org-capture-mark-kill-region beg (1- end))
  715. (org-capture-narrow beg (1- end))
  716. (if (re-search-forward "%\\?" end t) (replace-match ""))))
  717. (defun org-capture-mark-kill-region (beg end)
  718. "Mark the region that will have to be killed when aborting capture."
  719. (let ((m1 (move-marker (make-marker) beg))
  720. (m2 (move-marker (make-marker) end)))
  721. (org-capture-put :begin-marker m1)
  722. (org-capture-put :end-marker m2)))
  723. (defun org-capture-narrow (beg end)
  724. "Narrow, unless configuraion says not to narrow."
  725. (unless (org-capture-get :unnarrowed)
  726. (narrow-to-region beg end)
  727. (goto-char beg)))
  728. (defun org-capture-empty-lines-before (&optional n)
  729. "Arrange for the correct number of empty lines before the insertion point.
  730. Point will be after the empty lines, so insertion can direcetly be done."
  731. (setq n (or n (org-capture-get :empty-lines) 0))
  732. (let ((pos (point)))
  733. (org-back-over-empty-lines)
  734. (delete-region (point) pos)
  735. (newline n)))
  736. (defun org-capture-empty-lines-after (&optional n)
  737. "Arrange for the correct number of empty lines after the inserted string.
  738. Point will remain at the first line after the inserted text."
  739. (setq n (or n (org-capture-get :empty-lines) 0))
  740. (org-back-over-empty-lines)
  741. (while (looking-at "[ \t]*\n") (replace-match ""))
  742. (let ((pos (point)))
  743. (newline n)
  744. (goto-char pos)))
  745. (defvar org-clock-marker) ; Defined in org.el
  746. ;;;###autoload
  747. (defun org-capture-insert-template-here ()
  748. (let* ((template (org-capture-get :template))
  749. (type (org-capture-get :type))
  750. beg end pp)
  751. (or (bolp) (newline))
  752. (setq beg (point))
  753. (cond
  754. ((and (eq type 'entry) (org-mode-p))
  755. (org-paste-subtree nil template t))
  756. ((and (memq type '(item checkitem))
  757. (org-mode-p)
  758. (save-excursion (skip-chars-backward " \t\n")
  759. (setq pp (point))
  760. (org-in-item-p)))
  761. (goto-char pp)
  762. (org-insert-item)
  763. (skip-chars-backward " ")
  764. (skip-chars-backward "-+*0123456789).")
  765. (delete-region (point) (point-at-eol))
  766. (setq beg (point))
  767. (org-remove-indentation template)
  768. (insert template)
  769. (org-capture-empty-lines-after)
  770. (goto-char beg)
  771. (org-maybe-renumber-ordered-list)
  772. (org-end-of-item)
  773. (setq end (point)))
  774. (t (insert template)))
  775. (setq end (point))
  776. (goto-char beg)
  777. (if (re-search-forward "%\\?" end t)
  778. (replace-match ""))))
  779. (defun org-capture-set-plist (entry)
  780. "Initialize the property list from the template definition."
  781. (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
  782. (org-capture-put :key (car entry) :description (nth 1 entry)
  783. :target (nth 3 entry))
  784. (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
  785. (when (or (not txt) (not (string-match "\\S-" txt)))
  786. ;; The template may be empty or omitted for special types.
  787. ;; Here we insert the default templates for such cases.
  788. (cond
  789. ((eq type 'item) (setq txt "- %?"))
  790. ((eq type 'checkitem) (setq txt "- [ ] %?"))
  791. ((eq type 'table-line) (setq txt "| %? |"))
  792. ((member type '(nil entry)) (setq txt "* %?\n %a"))))
  793. (org-capture-put :template txt :type type)))
  794. (defun org-capture-goto-target (&optional template-key)
  795. "Go to the target location of a capture template.
  796. The user is queried for the template."
  797. (interactive)
  798. (let* (org-select-template-temp-major-mode
  799. (entry (org-capture-select-template template-key)))
  800. (unless entry
  801. (error "No capture emplate selected"))
  802. (org-capture-set-plist entry)
  803. (org-capture-set-target-location)
  804. (switch-to-buffer (org-capture-get :buffer))
  805. (goto-char (org-capture-get :pos))))
  806. (defun org-capture-get-indirect-buffer (&optional buffer prefix)
  807. "Make an indirect buffer for a capture process.
  808. Use PREFIX as a prefix for the name of the indirect buffer."
  809. (setq buffer (or buffer (current-buffer)))
  810. (let ((n 1) (base (buffer-name buffer)) bname)
  811. (setq bname (concat prefix "-" base))
  812. (while (buffer-live-p (get-buffer bname))
  813. (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
  814. (condition-case nil
  815. (make-indirect-buffer buffer bname 'clone)
  816. (error (make-indirect-buffer buffer bname)))))
  817. ;;; The template code
  818. (defun org-capture-select-template (&optional keys)
  819. "Select a capture template.
  820. Lisp programs can force the template by setting KEYS to a string."
  821. (when org-capture-templates
  822. (if keys
  823. (or (assoc keys org-capture-templates)
  824. (error "No capture template referred to by \"%s\" keys" keys))
  825. (if (= 1 (length org-capture-templates))
  826. (car org-capture-templates)
  827. (org-mks org-capture-templates
  828. "Select a capture template\n========================="
  829. "Template key: "
  830. '(("C" "Customize org-capture-templates")
  831. ("q" "Abort")))))))
  832. (defun org-capture-fill-template (&optional template initial annotation)
  833. "Fill a template and return the filled template as a string.
  834. The template may still contain \"%?\" for cursor positioning."
  835. (setq template (or template (org-capture-get :template)))
  836. (when (stringp initial)
  837. (setq initial (org-no-properties initial))
  838. (remove-text-properties 0 (length initial) '(read-only t) initial))
  839. (let* ((buffer (org-capture-get :buffer))
  840. (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
  841. (ct (org-capture-get :default-time))
  842. (dct (decode-time ct))
  843. (ct1
  844. (if (< (nth 2 dct) org-extend-today-until)
  845. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  846. ct))
  847. (plist-p (if org-store-link-plist t nil))
  848. (v-c (and (> (length kill-ring) 0) (current-kill 0)))
  849. (v-x (or (org-get-x-clipboard 'PRIMARY)
  850. (org-get-x-clipboard 'CLIPBOARD)
  851. (org-get-x-clipboard 'SECONDARY)))
  852. (v-t (format-time-string (car org-time-stamp-formats) ct))
  853. (v-T (format-time-string (cdr org-time-stamp-formats) ct))
  854. (v-u (concat "[" (substring v-t 1 -1) "]"))
  855. (v-U (concat "[" (substring v-T 1 -1) "]"))
  856. ;; `initial' and `annotation' might habe been passed.
  857. ;; But if the property list has them, we prefer those values
  858. (v-i (or (plist-get org-store-link-plist :initial)
  859. initial
  860. (org-capture-get :initial)
  861. ""))
  862. (v-a (or (plist-get org-store-link-plist :annotation)
  863. annotation
  864. (org-capture-get :annotation)
  865. ""))
  866. ;; Is the link empty? Then we do not want it...
  867. (v-a (if (equal v-a "[[]]") "" v-a))
  868. (clipboards (remove nil (list v-i
  869. (org-get-x-clipboard 'PRIMARY)
  870. (org-get-x-clipboard 'CLIPBOARD)
  871. (org-get-x-clipboard 'SECONDARY)
  872. v-c)))
  873. (v-A (if (and v-a
  874. (string-match
  875. "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  876. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  877. v-a))
  878. (v-n user-full-name)
  879. (v-k (if (marker-buffer org-clock-marker)
  880. (org-substring-no-properties org-clock-heading)))
  881. (v-K (if (marker-buffer org-clock-marker)
  882. (org-make-link-string
  883. (buffer-file-name (marker-buffer org-clock-marker))
  884. org-clock-heading)))
  885. v-I
  886. (org-startup-folded nil)
  887. (org-inhibit-startup t)
  888. org-time-was-given org-end-time-was-given x
  889. prompt completions char time pos default histvar)
  890. (setq org-store-link-plist
  891. (plist-put org-store-link-plist :annotation v-a)
  892. org-store-link-plist
  893. (plist-put org-store-link-plist :initial v-i))
  894. (unless template (setq template "") (message "No template") (ding)
  895. (sit-for 1))
  896. (save-window-excursion
  897. (delete-other-windows)
  898. (switch-to-buffer (get-buffer-create "*Capture*"))
  899. (erase-buffer)
  900. (insert template)
  901. (goto-char (point-min))
  902. (org-capture-steal-local-variables buffer)
  903. (setq buffer-file-name nil)
  904. ;; Simple %-escapes
  905. (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
  906. (unless (org-capture-escaped-%)
  907. (when (and initial (equal (match-string 0) "%i"))
  908. (save-match-data
  909. (let* ((lead (buffer-substring
  910. (point-at-bol) (match-beginning 0))))
  911. (setq v-i (mapconcat 'identity
  912. (org-split-string initial "\n")
  913. (concat "\n" lead))))))
  914. (replace-match
  915. (or (eval (intern (concat "v-" (match-string 1)))) "")
  916. t t)))
  917. ;; %[] Insert contents of a file.
  918. (goto-char (point-min))
  919. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  920. (unless (org-capture-escaped-%)
  921. (let ((start (match-beginning 0))
  922. (end (match-end 0))
  923. (filename (expand-file-name (match-string 1))))
  924. (goto-char start)
  925. (delete-region start end)
  926. (condition-case error
  927. (insert-file-contents filename)
  928. (error (insert (format "%%![Couldn't insert %s: %s]"
  929. filename error)))))))
  930. ;; %() embedded elisp
  931. (goto-char (point-min))
  932. (while (re-search-forward "%\\((.+)\\)" nil t)
  933. (unless (org-capture-escaped-%)
  934. (goto-char (match-beginning 0))
  935. (let ((template-start (point)))
  936. (forward-char 1)
  937. (let ((result
  938. (condition-case error
  939. (eval (read (current-buffer)))
  940. (error (format "%%![Error: %s]" error)))))
  941. (delete-region template-start (point))
  942. (insert result)))))
  943. ;; From the property list
  944. (when plist-p
  945. (goto-char (point-min))
  946. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  947. (unless (org-capture-escaped-%)
  948. (and (setq x (or (plist-get org-store-link-plist
  949. (intern (match-string 1))) ""))
  950. (replace-match x t t)))))
  951. ;; Turn on org-mode in temp buffer, set local variables
  952. ;; This is to support completion in interactive prompts
  953. (let ((org-inhibit-startup t)) (org-mode))
  954. ;; Interactive template entries
  955. (goto-char (point-min))
  956. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?"
  957. nil t)
  958. (unless (org-capture-escaped-%)
  959. (setq char (if (match-end 3) (match-string 3))
  960. prompt (if (match-end 2) (match-string 2)))
  961. (goto-char (match-beginning 0))
  962. (replace-match "")
  963. (setq completions nil default nil)
  964. (when prompt
  965. (setq completions (org-split-string prompt "|")
  966. prompt (pop completions)
  967. default (car completions)
  968. histvar (intern (concat
  969. "org-capture-template-prompt-history::"
  970. (or prompt "")))
  971. completions (mapcar 'list completions)))
  972. (cond
  973. ((member char '("G" "g"))
  974. (let* ((org-last-tags-completion-table
  975. (org-global-tags-completion-table
  976. (if (equal char "G")
  977. (org-agenda-files)
  978. (and file (list file)))))
  979. (org-add-colon-after-tag-completion t)
  980. (ins (org-icompleting-read
  981. (if prompt (concat prompt ": ") "Tags: ")
  982. 'org-tags-completion-function nil nil nil
  983. 'org-tags-history)))
  984. (setq ins (mapconcat 'identity
  985. (org-split-string
  986. ins (org-re "[^[:alnum:]_@]+"))
  987. ":"))
  988. (when (string-match "\\S-" ins)
  989. (or (equal (char-before) ?:) (insert ":"))
  990. (insert ins)
  991. (or (equal (char-after) ?:) (insert ":")))))
  992. ((equal char "C")
  993. (cond ((= (length clipboards) 1) (insert (car clipboards)))
  994. ((> (length clipboards) 1)
  995. (insert (read-string "Clipboard/kill value: "
  996. (car clipboards) '(clipboards . 1)
  997. (car clipboards))))))
  998. ((equal char "L")
  999. (cond ((= (length clipboards) 1)
  1000. (org-insert-link 0 (car clipboards)))
  1001. ((> (length clipboards) 1)
  1002. (org-insert-link 0 (read-string "Clipboard/kill value: "
  1003. (car clipboards)
  1004. '(clipboards . 1)
  1005. (car clipboards))))))
  1006. ((equal char "p")
  1007. (let*
  1008. ((prop (org-substring-no-properties prompt))
  1009. (pall (concat prop "_ALL"))
  1010. (allowed
  1011. (with-current-buffer
  1012. (get-buffer (file-name-nondirectory file))
  1013. (or (cdr (assoc pall org-file-properties))
  1014. (cdr (assoc pall org-global-properties))
  1015. (cdr (assoc pall org-global-properties-fixed)))))
  1016. (existing (with-current-buffer
  1017. (get-buffer (file-name-nondirectory file))
  1018. (mapcar 'list (org-property-values prop))))
  1019. (propprompt (concat "Value for " prop ": "))
  1020. (val (if allowed
  1021. (org-completing-read
  1022. propprompt
  1023. (mapcar 'list (org-split-string allowed
  1024. "[ \t]+"))
  1025. nil 'req-match)
  1026. (org-completing-read-no-i propprompt
  1027. existing nil nil
  1028. "" nil ""))))
  1029. (org-set-property prop val)))
  1030. (char
  1031. ;; These are the date/time related ones
  1032. (setq org-time-was-given (equal (upcase char) char))
  1033. (setq time (org-read-date (equal (upcase char) "U") t nil
  1034. prompt))
  1035. (org-insert-time-stamp time org-time-was-given
  1036. (member char '("u" "U"))
  1037. nil nil (list org-end-time-was-given)))
  1038. (t
  1039. (let (org-completion-use-ido)
  1040. (insert (org-completing-read-no-i
  1041. (concat (if prompt prompt "Enter string")
  1042. (if default (concat " [" default "]"))
  1043. ": ")
  1044. completions nil nil nil histvar default)))))))
  1045. ;; Make sure there are no empty lines before the text, and that
  1046. ;; it ends with a newline character
  1047. (goto-char (point-min))
  1048. (while (looking-at "[ \t]*\n") (replace-match ""))
  1049. (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
  1050. ;; Return the expanded tempate and kill the temporary buffer
  1051. (untabify (point-min) (point-max))
  1052. (set-buffer-modified-p nil)
  1053. (prog1 (buffer-string) (kill-buffer (current-buffer))))))
  1054. (defun org-capture-escaped-% ()
  1055. "Check if % was escaped - if yes, unescape it now."
  1056. (if (equal (char-before (match-beginning 0)) ?\\)
  1057. (progn
  1058. (delete-region (1- (match-beginning 0)) (match-beginning 0))
  1059. t)
  1060. nil))
  1061. ;;;###autoload
  1062. (defun org-capture-import-remember-templates ()
  1063. "Set org-capture-templates to be similar to `org-remember-templates'."
  1064. (interactive)
  1065. (when (and (yes-or-no-p
  1066. "Import old remember templates into org-capture-templates? ")
  1067. (yes-or-no-p
  1068. "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
  1069. (require 'org-remember)
  1070. (setq org-capture-templates
  1071. (mapcar
  1072. (lambda (entry)
  1073. (let ((desc (car entry))
  1074. (key (char-to-string (nth 1 entry)))
  1075. (template (nth 2 entry))
  1076. (file (or (nth 3 entry) org-default-notes-file))
  1077. (position (or (nth 4 entry) org-remember-default-headline))
  1078. (type 'entry)
  1079. (prepend org-reverse-note-order)
  1080. immediate target)
  1081. (cond
  1082. ((member position '(top bottom))
  1083. (setq target (list 'file file)
  1084. prepend (eq position 'top)))
  1085. ((eq position 'date-tree)
  1086. (setq target (list 'file+datetree file)
  1087. prepend nil))
  1088. (t (setq target (list 'file+headline file position))))
  1089. (when (string-match "%!" template)
  1090. (setq template (replace-match "" t t template)
  1091. immediate t))
  1092. (append (list key desc type target template)
  1093. (if prepend '(:prepend t))
  1094. (if immediate '(:immediate-finish t)))))
  1095. org-remember-templates))))
  1096. (provide 'org-capture)
  1097. ;; arch-tag: 986bf41b-8ada-4e28-bf20-e8388a7205a0
  1098. ;;; org-capture.el ends here