org-capture.el 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. (org-capture-put :template (org-capture-fill-template))
  317. (if (equal goto 0)
  318. ;;insert at point
  319. (org-capture-insert-template-here)
  320. (org-capture-place-template)
  321. (if (org-capture-get :immediate-finish)
  322. (org-capture-finalize)
  323. (if (and (org-mode-p)
  324. (org-capture-get :clock-in))
  325. (condition-case nil
  326. (progn
  327. (if (org-clock-is-active)
  328. (org-capture-put :interrupted-clock
  329. (copy-marker org-clock-marker)))
  330. (org-clock-in)
  331. (org-set-local 'org-capture-clock-was-started t))
  332. (error
  333. "Could not start the clock in this capture buffer")))))))))))
  334. (defun org-capture-finalize ()
  335. "Finalize the capture process."
  336. (interactive)
  337. (unless (and org-capture-mode
  338. (buffer-base-buffer (current-buffer)))
  339. (error "This does not seem to be a capture buffer for Org-mode"))
  340. ;; Did we start the clock in this capture buffer?
  341. (when (and org-capture-clock-was-started
  342. org-clock-marker (marker-buffer org-clock-marker)
  343. (equal (marker-buffer org-clock-marker) (buffer-base-buffer))
  344. (> org-clock-marker (point-min))
  345. (< org-clock-marker (point-max)))
  346. ;; Looks like the clock we started is still running. Clock out.
  347. (let (org-log-note-clock-out) (org-clock-out))
  348. (when (and (org-capture-get :clock-resume 'local)
  349. (markerp (org-capture-get :interrupted-clock 'local))
  350. (buffer-live-p (marker-buffer
  351. (org-capture-get :interrupted-clock 'local))))
  352. (org-with-point-at (org-capture-get :interrupted-clock 'local)
  353. (org-clock-in))
  354. (message "Interrupted clock has been resumed")))
  355. (let ((beg (point-min))
  356. (end (point-max))
  357. (abort-note nil))
  358. (widen)
  359. (if org-note-abort
  360. (let ((m1 (org-capture-get :begin-marker 'local))
  361. (m2 (org-capture-get :end-marker 'local)))
  362. (if (and m1 m2 (= m1 beg) (= m2 end))
  363. (progn
  364. (setq abort-note 'clean)
  365. (kill-region m1 m2))
  366. (setq abort-note 'dirty)))
  367. ;; Make sure that the empty lines after are correct
  368. (when (and (> (point-max) end) ; indeed, the buffer was still narrowed
  369. (member (org-capture-get :type 'local)
  370. '(entry item checkitem plain)))
  371. (save-excursion
  372. (goto-char end)
  373. (org-capture-empty-lines-after
  374. (or (org-capture-get :empty-lines 'local) 0))))
  375. ;; Postprocessing: Update Statistics cookies, do the sorting
  376. (when (org-mode-p)
  377. (save-excursion
  378. (when (ignore-errors (org-back-to-heading))
  379. (org-update-parent-todo-statistics)
  380. (org-update-checkbox-count)))
  381. ;; FIXME Here we should do the sorting
  382. ;; If we have added a table line, maybe recompute?
  383. (when (and (eq (org-capture-get :type 'local) 'table-line)
  384. (org-at-table-p))
  385. (if (org-table-get-stored-formulas)
  386. (org-table-recalculate 'all) ;; FIXME: Should we iterate???
  387. (org-table-align)))
  388. )
  389. ;; Store this place as the last one where we stored something
  390. ;; Do the marking in the base buffer, so that it makes sense after
  391. ;; the indirect buffer has been killed.
  392. (let ((pos (point)))
  393. (with-current-buffer (buffer-base-buffer (current-buffer))
  394. (save-excursion
  395. (save-restriction
  396. (widen)
  397. (goto-char pos)
  398. (bookmark-set "org-capture-last-stored")
  399. (move-marker org-capture-last-stored-marker (point))))))
  400. ;; Run the hook
  401. (run-hooks 'org-capture-before-finalize-hook)
  402. )
  403. ;; Kill the indirect buffer
  404. (save-buffer)
  405. (let ((return-wconf (org-capture-get :return-to-wconf 'local)))
  406. (kill-buffer (current-buffer))
  407. ;; Restore the window configuration before capture
  408. (set-window-configuration return-wconf))
  409. (when abort-note
  410. (cond
  411. ((equal abort-note 'clean)
  412. (message "Capture process aborted and target file cleaned up"))
  413. ((equal abort-note 'dirty)
  414. (error "Capture process aborted, but target buffer could not be cleaned up correctly"))))))
  415. (defun org-capture-refile ()
  416. "Finalize the current capture and then refile the entry.
  417. Refiling is done from the base buffer, because the indirect buffer is then
  418. already gone."
  419. (interactive)
  420. (unless (eq (org-capture-get :type 'local) 'entry)
  421. (error
  422. "Refiling from a capture buffer makes only sense for `entry'-type templates"))
  423. (let ((pos (point)) (base (buffer-base-buffer (current-buffer))))
  424. (org-capture-finalize)
  425. (save-window-excursion
  426. (with-current-buffer (or base (current-buffer))
  427. (save-excursion
  428. (save-restriction
  429. (widen)
  430. (goto-char pos)
  431. (call-interactively 'org-refile)))))))
  432. (defun org-capture-kill ()
  433. "Abort the current capture process."
  434. (interactive)
  435. ;; FIXME: This does not do the right thing, we need to remove the new stuff
  436. ;; By hand it is easy: undo, then kill the buffer
  437. (let ((org-note-abort t) (org-capture-before-finalize-hook nil))
  438. (org-capture-finalize)))
  439. (defun org-capture-goto-last-stored ()
  440. "Go to the location where the last remember note was stored."
  441. (interactive)
  442. (org-goto-marker-or-bmk org-capture-last-stored-marker
  443. "org-capture-last-stored")
  444. (message "This is the last note stored by a capture process"))
  445. ;;; Supporting functions for handling the process
  446. (defun org-capture-set-target-location (&optional target)
  447. "Find target buffer and position and store then in the property list."
  448. (let ((target-entry-p t))
  449. (setq target (or target (org-capture-get :target)))
  450. (save-excursion
  451. (cond
  452. ((eq (car target) 'file)
  453. (set-buffer (org-capture-target-buffer (nth 1 target)))
  454. (setq target-entry-p nil))
  455. ((eq (car target) 'id)
  456. (let ((loc (org-id-find (nth 1 target))))
  457. (if (not loc)
  458. (error "Cannot find target ID \"%s\"" (nth 1 target))
  459. (set-buffer (org-capture-target-buffer (car loc)))
  460. (goto-char (cdr loc)))))
  461. ((eq (car target) 'file+headline)
  462. (set-buffer (org-capture-target-buffer (nth 1 target)))
  463. (let ((hd (nth 2 target)))
  464. (goto-char (point-min))
  465. (if (re-search-forward
  466. (format org-complex-heading-regexp-format (regexp-quote hd))
  467. nil t)
  468. (goto-char (point-at-bol))
  469. (goto-char (point-max))
  470. (or (bolp) (insert "\n"))
  471. (insert "* " hd "\n")
  472. (beginning-of-line 0))))
  473. ((eq (car target) 'file+olp)
  474. (let ((m (org-find-olp (cdr target))))
  475. (set-buffer (marker-buffer m))
  476. (goto-char m)))
  477. ((eq (car target) 'file+regexp)
  478. (set-buffer (org-capture-target-buffer (nth 1 target)))
  479. (goto-char (point-min))
  480. (if (re-search-forward (nth 1 target) nil t)
  481. (progn
  482. (goto-char (match-beginning 0))
  483. (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
  484. (kill-buffer (current-buffer))
  485. (error "No match for target regexp in file %s" (nth 1 target))))
  486. ((eq (car target) 'file+datetree)
  487. (require 'org-datetree)
  488. (set-buffer (org-capture-target-buffer (nth 1 target)))
  489. ;; Make a date tree entry, with the current date (or yesterday,
  490. ;; if we are extending dates for a couple of hours)
  491. (org-datetree-find-date-create
  492. (calendar-gregorian-from-absolute
  493. (if org-overriding-default-time
  494. (time-to-days org-overriding-default-time)
  495. (time-to-days
  496. (time-subtract (current-time)
  497. (list 0 (* 3600 org-extend-today-until) 0)))))))
  498. ((eq (car target) 'file+function)
  499. (set-buffer (org-capture-target-buffer (nth 1 target)))
  500. (funcall (nth 1 target))
  501. (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
  502. ((eq (car target) 'clock)
  503. (if (and (markerp org-clock-hd-marker)
  504. (marker-buffer org-clock-hd-marker))
  505. (progn (set-buffer (marker-buffer org-clock-hd-marker))
  506. (goto-char org-clock-hd-marker))
  507. (error "No running clock that could be used as capture target")))
  508. (t (error "Invalid capture target specification")))
  509. (org-capture-put :buffer (current-buffer) :pos (point)
  510. :target-entry-p target-entry-p))))
  511. (defun org-capture-target-buffer (file)
  512. "Get a buffer for FILE."
  513. (or (org-find-base-buffer-visiting file)
  514. (find-file-noselect (expand-file-name file org-directory))))
  515. (defun org-capture-steal-local-variables (buffer)
  516. "Install Org-mode local variables"
  517. (mapc (lambda (v)
  518. (ignore-errors (org-set-local (car v) (cdr v))))
  519. (buffer-local-variables buffer)))
  520. (defun org-capture-place-template ()
  521. "Insert the template at the target location, and display the buffer."
  522. (org-capture-put :return-to-wconf (current-window-configuration))
  523. (delete-other-windows)
  524. (org-switch-to-buffer-other-window
  525. (org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
  526. (show-all)
  527. (goto-char (org-capture-get :pos))
  528. (org-set-local 'org-capture-target-marker
  529. (move-marker (make-marker) (point)))
  530. (let* ((template (org-capture-get :template))
  531. (type (org-capture-get :type)))
  532. (case type
  533. ((nil entry) (org-capture-place-entry))
  534. (table-line (org-capture-place-table-line))
  535. (plain (org-capture-place-plain-text))
  536. (item (org-capture-place-item))))
  537. (org-capture-mode 1)
  538. (org-set-local 'org-capture-current-plist org-capture-plist))
  539. (defun org-capture-place-entry ()
  540. "Place the template as a new Org entry."
  541. (let* ((txt (org-capture-get :template))
  542. (reversed (org-capture-get :prepend))
  543. (target-entry-p (org-capture-get :target-entry-p))
  544. level beg end)
  545. (cond
  546. ((not target-entry-p)
  547. ;; Insert as top-level entry, either at beginning or at end of file
  548. (setq level 1)
  549. (if reversed
  550. (progn (goto-char (point-min))
  551. (outline-next-heading))
  552. (goto-char (point-max))
  553. (or (bolp) (insert "\n"))))
  554. (t
  555. ;; Insert as a child of the current entry
  556. (and (looking-at "\\*+")
  557. (setq level (- (match-end 0) (match-beginning 0))))
  558. (setq level (org-get-valid-level (or level 1) 1))
  559. (if reversed
  560. (progn
  561. (outline-next-heading)
  562. (or (bolp) (insert "\n")))
  563. (org-end-of-subtree t t)
  564. (or (bolp) (insert "\n")))))
  565. (org-capture-empty-lines-before)
  566. (setq beg (point))
  567. (org-paste-subtree level txt 'for-yank)
  568. (org-capture-empty-lines-after 1)
  569. (outline-next-heading)
  570. (setq end (point))
  571. (org-capture-mark-kill-region beg (1- end))
  572. (org-capture-narrow beg (1- end))
  573. (if (re-search-forward "%\\?" end t) (replace-match ""))))
  574. (defun org-capture-place-item ()
  575. "Place the template as a new plain list item."
  576. (let* ((txt (org-capture-get :template))
  577. (target-entry-p (org-capture-get :target-entry-p))
  578. ind beg end)
  579. (cond
  580. ((not target-entry-p)
  581. ;; Insert as top-level entry, either at beginning or at end of file
  582. (setq beg (point-min) end (point-max)))
  583. (t
  584. (setq beg (1+ (point-at-eol))
  585. end (save-excursion (outline-next-heading) (point)))))
  586. (if (org-capture-get :prepend)
  587. (progn
  588. (goto-char beg)
  589. (if (re-search-forward (concat "^" (org-item-re)) nil t)
  590. (progn
  591. (goto-char (match-beginning 0))
  592. (setq ind (org-get-indentation)))
  593. (goto-char end)
  594. (setq ind 0)))
  595. (goto-char end)
  596. (if (re-search-backward (concat "^" (org-item-re)) nil t)
  597. (progn
  598. (setq ind (org-get-indentation))
  599. (org-end-of-item))
  600. (setq ind 0)))
  601. ;; Remove common indentation
  602. (setq txt (org-remove-indentation txt))
  603. ;; Make sure this is indeed an item
  604. (unless (string-match (concat "\\`" (org-item-re)) txt)
  605. (setq txt (concat "- "
  606. (mapconcat 'identity (split-string txt "\n")
  607. "\n "))))
  608. ;; Set the correct indentation, depending on context
  609. (setq ind (make-string ind ?\ ))
  610. (setq txt (concat ind
  611. (mapconcat 'identity (split-string txt "\n")
  612. (concat "\n" ind))
  613. "\n"))
  614. ;; Insert, with surrounding empty lines
  615. (org-capture-empty-lines-before)
  616. (setq beg (point))
  617. (insert txt)
  618. (or (bolp) (insert "\n"))
  619. (org-capture-empty-lines-after 1)
  620. (forward-char 1)
  621. (setq end (point))
  622. (org-capture-mark-kill-region beg (1- end))
  623. (org-capture-narrow beg (1- end))
  624. (if (re-search-forward "%\\?" end t) (replace-match ""))))
  625. (defun org-capture-place-table-line ()
  626. "Place the template as a table line."
  627. (require 'org-table)
  628. (let* ((txt (org-capture-get :template))
  629. (target-entry-p (org-capture-get :target-entry-p))
  630. (table-line-pos (org-capture-get :table-line-pos))
  631. ind beg end)
  632. (cond
  633. ((not target-entry-p)
  634. ;; Table is not necessarily under a heading
  635. (setq beg (point-min) end (point-max)))
  636. (t
  637. ;; WE are at a heading, limit search to the body
  638. (setq beg (1+ (point-at-eol))
  639. end (save-excursion (outline-next-heading) (point)))))
  640. (if (re-search-forward org-table-dataline-regexp end t)
  641. (let ((b (org-table-begin)) (e (org-table-end)))
  642. (goto-char e)
  643. (if (looking-at "[ \t]*#\\+TBLFM:")
  644. (forward-line 1))
  645. (narrow-to-region b (point)))
  646. (goto-char end)
  647. (insert "\n\n")
  648. (narrow-to-region (1- (point)) (point)))
  649. ;; We are narrowed to the table, or to an empty line if there was no table
  650. ;; Check if the template is good
  651. (if (not (string-match org-table-dataline-regexp txt))
  652. (setq txt "| %?Bad template |\n"))
  653. (cond
  654. ((and table-line-pos
  655. (string-match "\\(I+\\)\\([-+][0-9]\\)" table-line-pos))
  656. ;; we have a complex line specification
  657. (goto-char (point-min))
  658. (let ((nh (- (match-end 1) (match-beginning 1)))
  659. (delta (string-to-number (match-string 2 table-line-pos)))
  660. ll)
  661. ;; The user wants a special position in the table
  662. (org-table-get-specials)
  663. (setq ll (aref org-table-hlines nh))
  664. (unless ll (error "Invalid table line specification \"%s\""
  665. table-line-pos))
  666. (setq ll (+ ll delta (if (< delta 0) 0 -1)))
  667. (org-goto-line ll)
  668. (org-table-insert-row 'below)
  669. (beginning-of-line 1)
  670. (delete-region (point) (1+ (point-at-eol)))
  671. (setq beg (point))
  672. (insert txt)
  673. (setq end (point))))
  674. ((org-capture-get :prepend)
  675. (goto-char (point-min))
  676. (re-search-forward org-table-hline-regexp nil t)
  677. (beginning-of-line 1)
  678. (re-search-forward org-table-dataline-regexp nil t)
  679. (beginning-of-line 1)
  680. (setq beg (point))
  681. (org-table-insert-row)
  682. (beginning-of-line 1)
  683. (delete-region (point) (1+ (point-at-eol)))
  684. (insert txt)
  685. (setq end (point)))
  686. (t
  687. (goto-char (point-max))
  688. (re-search-backward org-table-dataline-regexp nil t)
  689. (beginning-of-line 1)
  690. (org-table-insert-row 'below)
  691. (beginning-of-line 1)
  692. (delete-region (point) (1+ (point-at-eol)))
  693. (setq beg (point))
  694. (insert txt)
  695. (setq end (point))))
  696. (goto-char beg)
  697. (if (re-search-forward "%\\?" end t) (replace-match ""))
  698. (org-table-align)))
  699. (defun org-capture-place-plain-text ()
  700. "Place the template plainly."
  701. (let* ((txt (org-capture-get :template))
  702. beg end)
  703. (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))
  704. (or (bolp) (newline))
  705. (org-capture-empty-lines-before)
  706. (setq beg (point))
  707. (insert txt)
  708. (org-capture-empty-lines-after 1)
  709. (setq end (point))
  710. (org-capture-mark-kill-region beg (1- end))
  711. (org-capture-narrow beg (1- end))
  712. (if (re-search-forward "%\\?" end t) (replace-match ""))))
  713. (defun org-capture-mark-kill-region (beg end)
  714. "Mark the region that will have to be killed when aborting capture."
  715. (let ((m1 (move-marker (make-marker) beg))
  716. (m2 (move-marker (make-marker) end)))
  717. (org-capture-put :begin-marker m1)
  718. (org-capture-put :end-marker m2)))
  719. (defun org-capture-narrow (beg end)
  720. "Narrow, unless configuraion says not to narrow."
  721. (unless (org-capture-get :unnarrowed)
  722. (narrow-to-region beg end)
  723. (goto-char beg)))
  724. (defun org-capture-empty-lines-before (&optional n)
  725. "Arrange for the correct number of empty lines before the insertion point.
  726. Point will be after the empty lines, so insertion can direcetly be done."
  727. (setq n (or n (org-capture-get :empty-lines) 0))
  728. (let ((pos (point)))
  729. (org-back-over-empty-lines)
  730. (delete-region (point) pos)
  731. (newline n)))
  732. (defun org-capture-empty-lines-after (&optional n)
  733. "Arrange for the correct number of empty lines after the inserted string.
  734. Point will remain at the first line after the inserted text."
  735. (setq n (or n (org-capture-get :empty-lines) 0))
  736. (org-back-over-empty-lines)
  737. (while (looking-at "[ \t]*\n") (replace-match ""))
  738. (let ((pos (point)))
  739. (newline n)
  740. (goto-char pos)))
  741. (defvar org-clock-marker) ; Defined in org.el
  742. ;;;###autoload
  743. (defun org-capture-insert-template-here ()
  744. (let* ((template (org-capture-get :template))
  745. (type (org-capture-get :type))
  746. beg end pp)
  747. (or (bolp) (newline))
  748. (setq beg (point))
  749. (cond
  750. ((and (eq type 'entry) (org-mode-p))
  751. (org-paste-subtree nil template t))
  752. ((and (memq type '(item checkitem))
  753. (org-mode-p)
  754. (save-excursion (skip-chars-backward " \t\n")
  755. (setq pp (point))
  756. (org-in-item-p)))
  757. (goto-char pp)
  758. (org-insert-item)
  759. (skip-chars-backward " ")
  760. (skip-chars-backward "-+*0123456789).")
  761. (delete-region (point) (point-at-eol))
  762. (setq beg (point))
  763. (org-remove-indentation template)
  764. (insert template)
  765. (org-capture-empty-lines-after)
  766. (goto-char beg)
  767. (org-maybe-renumber-ordered-list)
  768. (org-end-of-item)
  769. (setq end (point)))
  770. (t (insert template)))
  771. (setq end (point))
  772. (goto-char beg)
  773. (if (re-search-forward "%\\?" end t)
  774. (replace-match ""))))
  775. (defun org-capture-set-plist (entry)
  776. "Initialize the property list from the template definition."
  777. (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
  778. (org-capture-put :key (car entry) :description (nth 1 entry)
  779. :target (nth 3 entry))
  780. (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
  781. (when (or (not txt) (not (string-match "\\S-" txt)))
  782. ;; The template may be empty or omitted for special types.
  783. ;; Here we insert the default templates for such cases.
  784. (cond
  785. ((eq type 'item) (setq txt "- %?"))
  786. ((eq type 'checkitem) (setq txt "- [ ] %?"))
  787. ((eq type 'table-line) (setq txt "| %? |"))
  788. ((member type '(nil entry)) (setq txt "* %?\n %a"))))
  789. (org-capture-put :template txt :type type)))
  790. (defun org-capture-goto-target (&optional template-key)
  791. "Go to the target location of a capture template.
  792. The user is queried for the template."
  793. (interactive)
  794. (let* (org-select-template-temp-major-mode
  795. (entry (org-capture-select-template template-key)))
  796. (unless entry
  797. (error "No capture emplate selected"))
  798. (org-capture-set-plist entry)
  799. (org-capture-set-target-location)
  800. (switch-to-buffer (org-capture-get :buffer))
  801. (goto-char (org-capture-get :pos))))
  802. (defun org-capture-get-indirect-buffer (&optional buffer prefix)
  803. "Make an indirect buffer for a capture process.
  804. Use PREFIX as a prefix for the name of the indirect buffer."
  805. (setq buffer (or buffer (current-buffer)))
  806. (let ((n 1) (base (buffer-name buffer)) bname)
  807. (setq bname (concat prefix "-" base))
  808. (while (buffer-live-p (get-buffer bname))
  809. (setq bname (concat prefix "-" (number-to-string (incf n)) "-" base)))
  810. (condition-case nil
  811. (make-indirect-buffer buffer bname 'clone)
  812. (error (make-indirect-buffer buffer bname)))))
  813. ;;; The template code
  814. (defun org-capture-select-template (&optional keys)
  815. "Select a capture template.
  816. Lisp programs can force the template by setting KEYS to a string."
  817. (when org-capture-templates
  818. (if keys
  819. (or (assoc keys org-capture-templates)
  820. (error "No capture template referred to by \"%s\" keys" keys))
  821. (if (= 1 (length org-capture-templates))
  822. (car org-capture-templates)
  823. (org-mks org-capture-templates
  824. "Select a capture template\n========================="
  825. "Template key: "
  826. '(("C" "Customize org-capture-templates")
  827. ("q" "Abort")))))))
  828. (defun org-capture-fill-template (&optional template initial annotation)
  829. "Fill a template and return the filled template as a string.
  830. The template may still contain \"%?\" for cursor positioning."
  831. (setq template (or template (org-capture-get :template)))
  832. (when (stringp initial)
  833. (setq initial (org-no-properties initial))
  834. (remove-text-properties 0 (length initial) '(read-only t) initial))
  835. (let* ((buffer (org-capture-get :buffer))
  836. (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
  837. (ct (org-capture-get :default-time))
  838. (dct (decode-time ct))
  839. (ct1
  840. (if (< (nth 2 dct) org-extend-today-until)
  841. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  842. ct))
  843. (plist-p (if org-store-link-plist t nil))
  844. (v-c (and (> (length kill-ring) 0) (current-kill 0)))
  845. (v-x (or (org-get-x-clipboard 'PRIMARY)
  846. (org-get-x-clipboard 'CLIPBOARD)
  847. (org-get-x-clipboard 'SECONDARY)))
  848. (v-t (format-time-string (car org-time-stamp-formats) ct))
  849. (v-T (format-time-string (cdr org-time-stamp-formats) ct))
  850. (v-u (concat "[" (substring v-t 1 -1) "]"))
  851. (v-U (concat "[" (substring v-T 1 -1) "]"))
  852. ;; `initial' and `annotation' might habe been passed.
  853. ;; But if the property list has them, we prefer those values
  854. (v-i (or (plist-get org-store-link-plist :initial)
  855. initial
  856. (org-capture-get :initial)
  857. ""))
  858. (v-a (or (plist-get org-store-link-plist :annotation)
  859. annotation
  860. (org-capture-get :annotation)
  861. ""))
  862. ;; Is the link empty? Then we do not want it...
  863. (v-a (if (equal v-a "[[]]") "" v-a))
  864. (clipboards (remove nil (list v-i
  865. (org-get-x-clipboard 'PRIMARY)
  866. (org-get-x-clipboard 'CLIPBOARD)
  867. (org-get-x-clipboard 'SECONDARY)
  868. v-c)))
  869. (v-A (if (and v-a
  870. (string-match
  871. "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  872. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  873. v-a))
  874. (v-n user-full-name)
  875. (v-k (if (marker-buffer org-clock-marker)
  876. (org-substring-no-properties org-clock-heading)))
  877. (v-K (if (marker-buffer org-clock-marker)
  878. (org-make-link-string
  879. (buffer-file-name (marker-buffer org-clock-marker))
  880. org-clock-heading)))
  881. v-I
  882. (org-startup-folded nil)
  883. (org-inhibit-startup t)
  884. org-time-was-given org-end-time-was-given x
  885. prompt completions char time pos default histvar)
  886. (setq org-store-link-plist
  887. (plist-put org-store-link-plist :annotation v-a)
  888. org-store-link-plist
  889. (plist-put org-store-link-plist :initial v-i))
  890. (unless template (setq template "") (message "No template") (ding)
  891. (sit-for 1))
  892. (save-window-excursion
  893. (delete-other-windows)
  894. (switch-to-buffer (get-buffer-create "*Capture*"))
  895. (insert template)
  896. (goto-char (point-min))
  897. (org-capture-steal-local-variables buffer)
  898. ;; Simple %-escapes
  899. (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
  900. (unless (org-capture-escaped-%)
  901. (when (and initial (equal (match-string 0) "%i"))
  902. (save-match-data
  903. (let* ((lead (buffer-substring
  904. (point-at-bol) (match-beginning 0))))
  905. (setq v-i (mapconcat 'identity
  906. (org-split-string initial "\n")
  907. (concat "\n" lead))))))
  908. (replace-match
  909. (or (eval (intern (concat "v-" (match-string 1)))) "")
  910. t t)))
  911. ;; %[] Insert contents of a file.
  912. (goto-char (point-min))
  913. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  914. (unless (org-capture-escaped-%)
  915. (let ((start (match-beginning 0))
  916. (end (match-end 0))
  917. (filename (expand-file-name (match-string 1))))
  918. (goto-char start)
  919. (delete-region start end)
  920. (condition-case error
  921. (insert-file-contents filename)
  922. (error (insert (format "%%![Couldn't insert %s: %s]"
  923. filename error)))))))
  924. ;; %() embedded elisp
  925. (goto-char (point-min))
  926. (while (re-search-forward "%\\((.+)\\)" nil t)
  927. (unless (org-capture-escaped-%)
  928. (goto-char (match-beginning 0))
  929. (let ((template-start (point)))
  930. (forward-char 1)
  931. (let ((result
  932. (condition-case error
  933. (eval (read (current-buffer)))
  934. (error (format "%%![Error: %s]" error)))))
  935. (delete-region template-start (point))
  936. (insert result)))))
  937. ;; From the property list
  938. (when plist-p
  939. (goto-char (point-min))
  940. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  941. (unless (org-capture-escaped-%)
  942. (and (setq x (or (plist-get org-store-link-plist
  943. (intern (match-string 1))) ""))
  944. (replace-match x t t)))))
  945. ;; Turn on org-mode in temp buffer, set local variables
  946. ;; This is to support completion in interactive prompts
  947. (let ((org-inhibit-startup t)) (org-mode))
  948. ;; Interactive template entries
  949. (goto-char (point-min))
  950. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?"
  951. nil t)
  952. (unless (org-capture-escaped-%)
  953. (setq char (if (match-end 3) (match-string 3))
  954. prompt (if (match-end 2) (match-string 2)))
  955. (goto-char (match-beginning 0))
  956. (replace-match "")
  957. (setq completions nil default nil)
  958. (when prompt
  959. (setq completions (org-split-string prompt "|")
  960. prompt (pop completions)
  961. default (car completions)
  962. histvar (intern (concat
  963. "org-capture-template-prompt-history::"
  964. (or prompt "")))
  965. completions (mapcar 'list completions)))
  966. (cond
  967. ((member char '("G" "g"))
  968. (let* ((org-last-tags-completion-table
  969. (org-global-tags-completion-table
  970. (if (equal char "G")
  971. (org-agenda-files)
  972. (and file (list file)))))
  973. (org-add-colon-after-tag-completion t)
  974. (ins (org-icompleting-read
  975. (if prompt (concat prompt ": ") "Tags: ")
  976. 'org-tags-completion-function nil nil nil
  977. 'org-tags-history)))
  978. (setq ins (mapconcat 'identity
  979. (org-split-string
  980. ins (org-re "[^[:alnum:]_@]+"))
  981. ":"))
  982. (when (string-match "\\S-" ins)
  983. (or (equal (char-before) ?:) (insert ":"))
  984. (insert ins)
  985. (or (equal (char-after) ?:) (insert ":")))))
  986. ((equal char "C")
  987. (cond ((= (length clipboards) 1) (insert (car clipboards)))
  988. ((> (length clipboards) 1)
  989. (insert (read-string "Clipboard/kill value: "
  990. (car clipboards) '(clipboards . 1)
  991. (car clipboards))))))
  992. ((equal char "L")
  993. (cond ((= (length clipboards) 1)
  994. (org-insert-link 0 (car clipboards)))
  995. ((> (length clipboards) 1)
  996. (org-insert-link 0 (read-string "Clipboard/kill value: "
  997. (car clipboards)
  998. '(clipboards . 1)
  999. (car clipboards))))))
  1000. ((equal char "p")
  1001. (let*
  1002. ((prop (org-substring-no-properties prompt))
  1003. (pall (concat prop "_ALL"))
  1004. (allowed
  1005. (with-current-buffer
  1006. (get-buffer (file-name-nondirectory file))
  1007. (or (cdr (assoc pall org-file-properties))
  1008. (cdr (assoc pall org-global-properties))
  1009. (cdr (assoc pall org-global-properties-fixed)))))
  1010. (existing (with-current-buffer
  1011. (get-buffer (file-name-nondirectory file))
  1012. (mapcar 'list (org-property-values prop))))
  1013. (propprompt (concat "Value for " prop ": "))
  1014. (val (if allowed
  1015. (org-completing-read
  1016. propprompt
  1017. (mapcar 'list (org-split-string allowed
  1018. "[ \t]+"))
  1019. nil 'req-match)
  1020. (org-completing-read-no-i propprompt
  1021. existing nil nil
  1022. "" nil ""))))
  1023. (org-set-property prop val)))
  1024. (char
  1025. ;; These are the date/time related ones
  1026. (setq org-time-was-given (equal (upcase char) char))
  1027. (setq time (org-read-date (equal (upcase char) "U") t nil
  1028. prompt))
  1029. (org-insert-time-stamp time org-time-was-given
  1030. (member char '("u" "U"))
  1031. nil nil (list org-end-time-was-given)))
  1032. (t
  1033. (let (org-completion-use-ido)
  1034. (insert (org-completing-read-no-i
  1035. (concat (if prompt prompt "Enter string")
  1036. (if default (concat " [" default "]"))
  1037. ": ")
  1038. completions nil nil nil histvar default)))))))
  1039. ;; Make sure there are no empty lines before the text, and that
  1040. ;; it ends with a newline character
  1041. (goto-char (point-min))
  1042. (while (looking-at "[ \t]*\n") (replace-match ""))
  1043. (if (re-search-forward "[ \t\n]*\\'" nil t) (replace-match "\n"))
  1044. ;; Return the expanded tempate and kill the temporary buffer
  1045. (untabify (point-min) (point-max))
  1046. (set-buffer-modified-p nil)
  1047. (prog1 (buffer-string) (kill-buffer (current-buffer))))))
  1048. (defun org-capture-escaped-% ()
  1049. "Check if % was escaped - if yes, unescape it now."
  1050. (if (equal (char-before (match-beginning 0)) ?\\)
  1051. (progn
  1052. (delete-region (1- (match-beginning 0)) (match-beginning 0))
  1053. t)
  1054. nil))
  1055. ;;;###autoload
  1056. (defun org-capture-import-remember-templates ()
  1057. "Set org-capture-templates to be similar to `org-remember-templates'."
  1058. (interactive)
  1059. (when (and (yes-or-no-p
  1060. "Import old remember templates into org-capture-templates? ")
  1061. (yes-or-no-p
  1062. "Note that this will remove any templates currently defined in `org-capture-templates'. Do you still want to go ahead? "))
  1063. (require 'org-remember)
  1064. (setq org-capture-templates
  1065. (mapcar
  1066. (lambda (entry)
  1067. (let ((desc (car entry))
  1068. (key (char-to-string (nth 1 entry)))
  1069. (template (nth 2 entry))
  1070. (file (or (nth 3 entry) org-default-notes-file))
  1071. (position (or (nth 4 entry) org-remember-default-headline))
  1072. (type 'entry)
  1073. (prepend org-reverse-note-order)
  1074. immediate target)
  1075. (cond
  1076. ((member position '(top bottom))
  1077. (setq target (list 'file file)
  1078. prepend (eq position 'top)))
  1079. ((eq position 'date-tree)
  1080. (setq target (list 'file+datetree file)
  1081. prepend nil))
  1082. (t (setq target (list 'file+headline file position))))
  1083. (when (string-match "%!" template)
  1084. (setq template (replace-match "" t t template)
  1085. immediate t))
  1086. (append (list key desc type target template)
  1087. (if prepend '(:prepend t))
  1088. (if immediate '(:immediate-finish t)))))
  1089. org-remember-templates))))
  1090. (provide 'org-capture)
  1091. ;; arch-tag: 986bf41b-8ada-4e28-bf20-e8388a7205a0
  1092. ;;; org-capture.el ends here