org-remember.el 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. ;;; org-remember.el --- Fast note taking in Org-mode
  2. ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file contains the system to take fast notes with Org-mode.
  23. ;; This system is used together with John Wiegley's `remember.el'.
  24. ;;; Code:
  25. (eval-when-compile
  26. (require 'cl))
  27. (require 'org)
  28. (require 'org-compat)
  29. (require 'org-datetree)
  30. (declare-function remember-mode "remember" ())
  31. (declare-function remember "remember" (&optional initial))
  32. (declare-function remember-buffer-desc "remember" ())
  33. (declare-function remember-finalize "remember" ())
  34. (declare-function org-pop-to-buffer-same-window
  35. "org-compat" (&optional buffer-or-name norecord label))
  36. (defvar remember-save-after-remembering)
  37. (defvar remember-register)
  38. (defvar remember-buffer)
  39. (defvar remember-handler-functions)
  40. (defvar remember-annotation-functions)
  41. (defvar org-clock-heading)
  42. (defvar org-clock-heading-for-remember)
  43. (defgroup org-remember nil
  44. "Options concerning interaction with remember.el."
  45. :tag "Org Remember"
  46. :group 'org)
  47. (defcustom org-remember-store-without-prompt t
  48. "Non-nil means \\<org-remember-mode-map>\\[org-remember-finalize] \
  49. stores the remember note without further prompts.
  50. It then uses the file and headline specified by the template or (if the
  51. template does not specify them) by the variables `org-default-notes-file'
  52. and `org-remember-default-headline'. To force prompting anyway, use
  53. \\[universal-argument] \\[org-remember-finalize] to file the note.
  54. When this variable is nil, \\[org-remember-finalize] gives you the prompts, and
  55. \\[universal-argument] \\[org-remember-finalize] triggers the fasttrack."
  56. :group 'org-remember
  57. :type 'boolean)
  58. (defcustom org-remember-interactive-interface 'refile
  59. "The interface to be used for interactive filing of remember notes.
  60. This is only used when the interactive mode for selecting a filing
  61. location is used (see the variable `org-remember-store-without-prompt').
  62. Allowed values are:
  63. outline The interface shows an outline of the relevant file
  64. and the correct heading is found by moving through
  65. the outline or by searching with incremental search.
  66. outline-path-completion Headlines in the current buffer are offered via
  67. completion.
  68. refile Use the refile interface, and offer headlines,
  69. possibly from different buffers."
  70. :group 'org-remember
  71. :type '(choice
  72. (const :tag "Refile" refile)
  73. (const :tag "Outline" outline)
  74. (const :tag "Outline-path-completion" outline-path-completion)))
  75. (defcustom org-remember-default-headline ""
  76. "The headline that should be the default location in the notes file.
  77. When filing remember notes, the cursor will start at that position.
  78. You can set this on a per-template basis with the variable
  79. `org-remember-templates'."
  80. :group 'org-remember
  81. :type 'string)
  82. (defcustom org-remember-templates nil
  83. "Templates for the creation of remember buffers.
  84. When nil, just let remember make the buffer.
  85. When non-nil, this is a list of (up to) 6-element lists. In each entry,
  86. the first element is the name of the template, which should be a single
  87. short word. The second element is a character, a unique key to select
  88. this template. The third element is the template.
  89. The fourth element is optional and can specify a destination file for
  90. remember items created with this template. The default file is given
  91. by `org-default-notes-file'. If the file name is not an absolute path,
  92. it will be interpreted relative to `org-directory'.
  93. An optional fifth element can specify the headline in that file that should
  94. be offered first when the user is asked to file the entry. The default
  95. headline is given in the variable `org-remember-default-headline'. When
  96. this element is `top' or `bottom', the note will be placed as a level-1
  97. entry at the beginning or end of the file, respectively.
  98. An optional sixth element specifies the contexts in which the template
  99. will be offered to the user. This element can be a list of major modes
  100. or a function, and the template will only be offered if `org-remember'
  101. is called from a mode in the list, or if the function returns t.
  102. Templates that specify t or nil for the context will always be added
  103. to the list of selectable templates.
  104. The template specifies the structure of the remember buffer. It should have
  105. a first line starting with a star, to act as the org-mode headline.
  106. Furthermore, the following %-escapes will be replaced with content:
  107. %^{PROMPT} prompt the user for a string and replace this sequence with it.
  108. A default value and a completion table can be specified like this:
  109. %^{prompt|default|completion2|completion3|...}
  110. The arrow keys access a prompt-specific history.
  111. %a annotation, normally the link created with `org-store-link'
  112. %A like %a, but prompt for the description part
  113. %i initial content, copied from the active region. If %i is
  114. indented, the entire inserted text will be indented as well.
  115. %t time stamp, date only
  116. %T time stamp with date and time
  117. %u, %U like the above, but inactive time stamps
  118. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
  119. You may define a prompt like %^{Please specify birthday}t
  120. %n user name (taken from `user-full-name')
  121. %c current kill ring head
  122. %x content of the X clipboard
  123. %:keyword specific information for certain link types, see below
  124. %^C interactive selection of which kill or clip to use
  125. %^L like %^C, but insert as link
  126. %k title of the currently clocked task
  127. %K link to the currently clocked task
  128. %^g prompt for tags, completing tags in the target file
  129. %^G prompt for tags, completing all tags in all agenda files
  130. %^{PROP}p Prompt the user for a value for property PROP
  131. %[PATHNAME] insert the contents of the file given by PATHNAME
  132. %(SEXP) evaluate elisp `(SEXP)' and replace with the result
  133. %! store this note immediately after completing the template\
  134. \\<org-remember-mode-map>
  135. (skipping the \\[org-remember-finalize] that normally triggers storing)
  136. %& jump to target location immediately after storing note
  137. %? after completing the template, position cursor here.
  138. Apart from these general escapes, you can access information specific to the
  139. link type that is created. For example, calling `remember' in emails or gnus
  140. will record the author and the subject of the message, which you can access
  141. with %:fromname and %:subject, respectively. Here is a complete list of what
  142. is recorded for each link type.
  143. Link type | Available information
  144. -------------------+------------------------------------------------------
  145. bbdb | %:type %:name %:company
  146. vm, wl, mh, rmail | %:type %:subject %:message-id
  147. | %:from %:fromname %:fromaddress
  148. | %:to %:toname %:toaddress
  149. | %:fromto (either \"to NAME\" or \"from NAME\")
  150. gnus | %:group, for messages also all email fields and
  151. | %:org-date (the Date: header in Org format)
  152. w3, w3m | %:type %:url
  153. info | %:type %:file %:node
  154. calendar | %:type %:date"
  155. :group 'org-remember
  156. :get (lambda (var) ; Make sure all entries have at least 5 elements
  157. (mapcar (lambda (x)
  158. (if (not (stringp (car x))) (setq x (cons "" x)))
  159. (cond ((= (length x) 4) (append x '(nil)))
  160. ((= (length x) 3) (append x '(nil nil)))
  161. (t x)))
  162. (default-value var)))
  163. :type '(repeat
  164. :tag "enabled"
  165. (list :value ("" ?a "\n" nil nil nil)
  166. (string :tag "Name")
  167. (character :tag "Selection Key")
  168. (string :tag "Template")
  169. (choice :tag "Destination file"
  170. (file :tag "Specify")
  171. (function :tag "Function")
  172. (const :tag "Use `org-default-notes-file'" nil))
  173. (choice :tag "Destin. headline"
  174. (string :tag "Specify")
  175. (function :tag "Function")
  176. (const :tag "Use `org-remember-default-headline'" nil)
  177. (const :tag "At beginning of file" top)
  178. (const :tag "At end of file" bottom)
  179. (const :tag "In a date tree" date-tree))
  180. (choice :tag "Context"
  181. (const :tag "Use in all contexts" nil)
  182. (const :tag "Use in all contexts" t)
  183. (repeat :tag "Use only if in major mode"
  184. (symbol :tag "Major mode"))
  185. (function :tag "Perform a check against function")))))
  186. (defcustom org-remember-delete-empty-lines-at-end t
  187. "Non-nil means clean up final empty lines in remember buffer."
  188. :group 'org-remember
  189. :type 'boolean)
  190. (defcustom org-remember-before-finalize-hook nil
  191. "Hook that is run right before a remember process is finalized.
  192. The remember buffer is still current when this hook runs."
  193. :group 'org-remember
  194. :type 'hook)
  195. (defvar org-remember-mode-map (make-sparse-keymap)
  196. "Keymap for `org-remember-mode', a minor mode.
  197. Use this map to set additional keybindings for when Org-mode is used
  198. for a Remember buffer.")
  199. (defvar org-remember-mode-hook nil
  200. "Hook for the minor `org-remember-mode'.")
  201. (define-minor-mode org-remember-mode
  202. "Minor mode for special key bindings in a remember buffer."
  203. nil " Rem" org-remember-mode-map
  204. (run-hooks 'org-remember-mode-hook))
  205. (define-key org-remember-mode-map "\C-c\C-c" 'org-remember-finalize)
  206. (define-key org-remember-mode-map "\C-c\C-k" 'org-remember-kill)
  207. (defcustom org-remember-clock-out-on-exit 'query
  208. "Non-nil means stop the clock when exiting a clocking remember buffer.
  209. This only applies if the clock is running in the remember buffer. If the
  210. clock is not stopped, it continues to run in the storage location.
  211. Instead of nil or t, this may also be the symbol `query' to prompt the
  212. user each time a remember buffer with a running clock is filed away."
  213. :group 'org-remember
  214. :type '(choice
  215. (const :tag "Never" nil)
  216. (const :tag "Always" t)
  217. (const :tag "Query user" query)))
  218. (defcustom org-remember-backup-directory nil
  219. "Directory where to store all remember buffers, for backup purposes.
  220. After a remember buffer has been stored successfully, the backup file
  221. will be removed. However, if you forget to finish the remember process,
  222. the file will remain there.
  223. See also `org-remember-auto-remove-backup-files'."
  224. :group 'org-remember
  225. :type '(choice
  226. (const :tag "No backups" nil)
  227. (directory :tag "Directory")))
  228. (defcustom org-remember-auto-remove-backup-files t
  229. "Non-nil means remove remember backup files after successfully storage.
  230. When remember is finished successfully, with storing the note at the
  231. desired target, remove the backup files related to this remember process
  232. and show a message about remaining backup files, from previous, unfinished
  233. remember sessions.
  234. Backup files will only be made at all, when `org-remember-backup-directory'
  235. is set."
  236. :group 'org-remember
  237. :type 'boolean)
  238. (defcustom org-remember-warn-about-backups t
  239. "Non-nil means warn about backup files in `org-remember-backup-directory'.
  240. Set this to nil if you find that you don't need the warning.
  241. If you cancel remember calls frequently and know when they
  242. contain useful information (because you know that you made an
  243. error or Emacs crashed, for example) nil is more useful. In the
  244. opposite case, the default, t, is more useful."
  245. :group 'org-remember
  246. :type 'boolean)
  247. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  248. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  249. ;;;###autoload
  250. (defun org-remember-insinuate ()
  251. "Setup remember.el for use with Org-mode."
  252. (org-require-remember)
  253. (setq remember-annotation-functions '(org-remember-annotation))
  254. (setq remember-handler-functions '(org-remember-handler))
  255. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  256. ;;;###autoload
  257. (defun org-remember-annotation ()
  258. "Return a link to the current location as an annotation for remember.el.
  259. If you are using Org-mode files as target for data storage with
  260. remember.el, then the annotations should include a link compatible with the
  261. conventions in Org-mode. This function returns such a link."
  262. (org-store-link nil))
  263. (defconst org-remember-help
  264. "Select a destination location for the note.
  265. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  266. RET on headline -> Store as sublevel entry to current headline
  267. RET at beg-of-buf -> Append to file as level 2 headline
  268. <left>/<right> -> before/after current headline, same headings level")
  269. (defvar org-jump-to-target-location nil)
  270. (defvar org-remember-previous-location nil)
  271. (defvar org-remember-reference-date nil)
  272. (defvar org-force-remember-template-char) ;; dynamically scoped
  273. ;; Save the major mode of the buffer we called remember from
  274. (defvar org-select-template-temp-major-mode nil)
  275. ;; Temporary store the buffer where remember was called from
  276. (defvar org-select-template-original-buffer nil)
  277. (defun org-select-remember-template (&optional use-char)
  278. (when org-remember-templates
  279. (let* ((pre-selected-templates
  280. (mapcar
  281. (lambda (tpl)
  282. (let ((ctxt (nth 5 tpl))
  283. (mode org-select-template-temp-major-mode)
  284. (buf org-select-template-original-buffer))
  285. (and (or (not ctxt) (eq ctxt t)
  286. (and (listp ctxt) (memq mode ctxt))
  287. (and (functionp ctxt)
  288. (with-current-buffer buf
  289. ;; Protect the user-defined function from error
  290. (condition-case nil (funcall ctxt) (error nil)))))
  291. tpl)))
  292. org-remember-templates))
  293. ;; If no template at this point, add the default templates:
  294. (pre-selected-templates1
  295. (if (not (delq nil pre-selected-templates))
  296. (mapcar (lambda(x) (if (not (nth 5 x)) x))
  297. org-remember-templates)
  298. pre-selected-templates))
  299. ;; Then unconditionally add template for any contexts
  300. (pre-selected-templates2
  301. (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
  302. org-remember-templates)
  303. (delq nil pre-selected-templates1)))
  304. (templates (mapcar (lambda (x)
  305. (if (stringp (car x))
  306. (append (list (nth 1 x) (car x)) (cddr x))
  307. (append (list (car x) "") (cdr x))))
  308. (delq nil pre-selected-templates2)))
  309. msg
  310. (char (or use-char
  311. (cond
  312. ((= (length templates) 1)
  313. (caar templates))
  314. ((and (boundp 'org-force-remember-template-char)
  315. org-force-remember-template-char)
  316. (if (stringp org-force-remember-template-char)
  317. (string-to-char org-force-remember-template-char)
  318. org-force-remember-template-char))
  319. (t
  320. (setq msg (format
  321. "Select template: %s%s"
  322. (mapconcat
  323. (lambda (x)
  324. (cond
  325. ((not (string-match "\\S-" (nth 1 x)))
  326. (format "[%c]" (car x)))
  327. ((equal (downcase (car x))
  328. (downcase (aref (nth 1 x) 0)))
  329. (format "[%c]%s" (car x)
  330. (substring (nth 1 x) 1)))
  331. (t (format "[%c]%s" (car x) (nth 1 x)))))
  332. templates " ")
  333. (if (assoc ?C templates)
  334. ""
  335. " [C]customize templates")))
  336. (let ((inhibit-quit t) char0)
  337. (while (not char0)
  338. (message msg)
  339. (setq char0 (read-char-exclusive))
  340. (when (and (not (assoc char0 templates))
  341. (not (equal char0 ?\C-g))
  342. (not (equal char0 ?C)))
  343. (message "No such template \"%c\"" char0)
  344. (ding) (sit-for 1)
  345. (setq char0 nil)))
  346. (when (equal char0 ?\C-g)
  347. (jump-to-register remember-register)
  348. (kill-buffer remember-buffer)
  349. (error "Abort"))
  350. (when (not (assoc char0 templates))
  351. (jump-to-register remember-register)
  352. (kill-buffer remember-buffer)
  353. (customize-variable 'org-remember-templates)
  354. (error "Customize templates"))
  355. char0))))))
  356. (cddr (assoc char templates)))))
  357. ;;;###autoload
  358. (defun org-remember-apply-template (&optional use-char skip-interactive)
  359. "Initialize *remember* buffer with template, invoke `org-mode'.
  360. This function should be placed into `remember-mode-hook' and in fact requires
  361. to be run from that hook to function properly."
  362. (when (and (boundp 'initial) (stringp initial))
  363. (setq initial (org-no-properties initial))
  364. (remove-text-properties 0 (length initial) '(read-only t) initial))
  365. (if org-remember-templates
  366. (let* ((entry (org-select-remember-template use-char))
  367. (ct (or org-overriding-default-time (org-current-time)))
  368. (dct (decode-time ct))
  369. (ct1
  370. (if (< (nth 2 dct) org-extend-today-until)
  371. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  372. ct))
  373. (tpl (car entry))
  374. (plist-p (if org-store-link-plist t nil))
  375. (file (if (and (nth 1 entry)
  376. (or (and (stringp (nth 1 entry))
  377. (string-match "\\S-" (nth 1 entry)))
  378. (functionp (nth 1 entry))))
  379. (nth 1 entry)
  380. org-default-notes-file))
  381. (headline (nth 2 entry))
  382. (v-c (and (> (length kill-ring) 0) (current-kill 0)))
  383. (v-x (or (org-get-x-clipboard 'PRIMARY)
  384. (org-get-x-clipboard 'CLIPBOARD)
  385. (org-get-x-clipboard 'SECONDARY)))
  386. (v-t (format-time-string (car org-time-stamp-formats) ct))
  387. (v-T (format-time-string (cdr org-time-stamp-formats) ct))
  388. (v-u (concat "[" (substring v-t 1 -1) "]"))
  389. (v-U (concat "[" (substring v-T 1 -1) "]"))
  390. ;; `initial' and `annotation' are bound in `remember'.
  391. ;; But if the property list has them, we prefer those values
  392. (v-i (or (plist-get org-store-link-plist :initial)
  393. (and (boundp 'initial) initial)
  394. ""))
  395. (v-a (or (plist-get org-store-link-plist :annotation)
  396. (and (boundp 'annotation) annotation)
  397. ""))
  398. ;; Is the link empty? Then we do not want it...
  399. (v-a (if (equal v-a "[[]]") "" v-a))
  400. (clipboards (remove nil (list v-i
  401. (org-get-x-clipboard 'PRIMARY)
  402. (org-get-x-clipboard 'CLIPBOARD)
  403. (org-get-x-clipboard 'SECONDARY)
  404. v-c)))
  405. (v-A (if (and v-a
  406. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  407. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  408. v-a))
  409. (v-n user-full-name)
  410. (v-k (if (marker-buffer org-clock-marker)
  411. (org-substring-no-properties org-clock-heading)))
  412. (v-K (if (marker-buffer org-clock-marker)
  413. (org-make-link-string
  414. (buffer-file-name (marker-buffer org-clock-marker))
  415. org-clock-heading)))
  416. v-I
  417. (org-startup-folded nil)
  418. (org-inhibit-startup t)
  419. org-time-was-given org-end-time-was-given x
  420. prompt completions char time pos default histvar)
  421. (when (functionp file)
  422. (setq file (funcall file)))
  423. (when (functionp headline)
  424. (setq headline (funcall headline)))
  425. (when (and file (not (file-name-absolute-p file)))
  426. (setq file (expand-file-name file org-directory)))
  427. (setq org-store-link-plist
  428. (plist-put org-store-link-plist :annotation v-a)
  429. org-store-link-plist
  430. (plist-put org-store-link-plist :initial v-i))
  431. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  432. (erase-buffer)
  433. (insert (substitute-command-keys
  434. (format
  435. "## %s \"%s\" -> \"* %s\"
  436. ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
  437. ## C-0 C-c C-c \"%s\" -> \"* %s\"
  438. ## %s to select file and header location interactively.
  439. ## C-2 C-c C-c as child (C-3: as sibling) of the currently clocked item
  440. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  441. (if org-remember-store-without-prompt " C-c C-c" " C-1 C-c C-c")
  442. (abbreviate-file-name (or file org-default-notes-file))
  443. (or headline "")
  444. (or (car org-remember-previous-location) "???")
  445. (or (cdr org-remember-previous-location) "???")
  446. (if org-remember-store-without-prompt "C-1 C-c C-c" " C-c C-c"))))
  447. (insert tpl)
  448. ;; %[] Insert contents of a file.
  449. (goto-char (point-min))
  450. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  451. (unless (org-remember-escaped-%)
  452. (let ((start (match-beginning 0))
  453. (end (match-end 0))
  454. (filename (expand-file-name (match-string 1))))
  455. (goto-char start)
  456. (delete-region start end)
  457. (condition-case error
  458. (insert-file-contents filename)
  459. (error (insert (format "%%![Couldn't insert %s: %s]"
  460. filename error)))))))
  461. ;; Simple %-escapes
  462. (goto-char (point-min))
  463. (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
  464. (unless (org-remember-escaped-%)
  465. (when (and initial (equal (match-string 0) "%i"))
  466. (save-match-data
  467. (let* ((lead (buffer-substring
  468. (point-at-bol) (match-beginning 0))))
  469. (setq v-i (mapconcat 'identity
  470. (org-split-string initial "\n")
  471. (concat "\n" lead))))))
  472. (replace-match
  473. (or (eval (intern (concat "v-" (match-string 1)))) "")
  474. t t)))
  475. ;; %() embedded elisp
  476. (goto-char (point-min))
  477. (while (re-search-forward "%\\((.+)\\)" nil t)
  478. (unless (org-remember-escaped-%)
  479. (goto-char (match-beginning 0))
  480. (let ((template-start (point)))
  481. (forward-char 1)
  482. (let ((result
  483. (condition-case error
  484. (eval (read (current-buffer)))
  485. (error (format "%%![Error: %s]" error)))))
  486. (delete-region template-start (point))
  487. (insert result)))))
  488. ;; From the property list
  489. (when plist-p
  490. (goto-char (point-min))
  491. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  492. (unless (org-remember-escaped-%)
  493. (and (setq x (or (plist-get org-store-link-plist
  494. (intern (match-string 1))) ""))
  495. (replace-match x t t)))))
  496. ;; Turn on org-mode in the remember buffer, set local variables
  497. (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1))
  498. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  499. (org-set-local 'org-default-notes-file file))
  500. (if headline
  501. (org-set-local 'org-remember-default-headline headline))
  502. (org-set-local 'org-remember-reference-date
  503. (list (nth 4 dct) (nth 3 dct) (nth 5 dct)))
  504. ;; Interactive template entries
  505. (goto-char (point-min))
  506. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
  507. (unless (org-remember-escaped-%)
  508. (setq char (if (match-end 3) (match-string 3))
  509. prompt (if (match-end 2) (match-string 2)))
  510. (goto-char (match-beginning 0))
  511. (replace-match "")
  512. (setq completions nil default nil)
  513. (when prompt
  514. (setq completions (org-split-string prompt "|")
  515. prompt (pop completions)
  516. default (car completions)
  517. histvar (intern (concat
  518. "org-remember-template-prompt-history::"
  519. (or prompt "")))
  520. completions (mapcar 'list completions)))
  521. (cond
  522. ((member char '("G" "g"))
  523. (let* ((org-last-tags-completion-table
  524. (org-global-tags-completion-table
  525. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  526. (org-add-colon-after-tag-completion t)
  527. (ins (org-icompleting-read
  528. (if prompt (concat prompt ": ") "Tags: ")
  529. 'org-tags-completion-function nil nil nil
  530. 'org-tags-history)))
  531. (setq ins (mapconcat 'identity
  532. (org-split-string ins (org-re "[^[:alnum:]_@#%]+"))
  533. ":"))
  534. (when (string-match "\\S-" ins)
  535. (or (equal (char-before) ?:) (insert ":"))
  536. (insert ins)
  537. (or (equal (char-after) ?:) (insert ":")))))
  538. ((equal char "C")
  539. (cond ((= (length clipboards) 1) (insert (car clipboards)))
  540. ((> (length clipboards) 1)
  541. (insert (read-string "Clipboard/kill value: "
  542. (car clipboards) '(clipboards . 1)
  543. (car clipboards))))))
  544. ((equal char "L")
  545. (cond ((= (length clipboards) 1)
  546. (org-insert-link 0 (car clipboards)))
  547. ((> (length clipboards) 1)
  548. (org-insert-link 0 (read-string "Clipboard/kill value: "
  549. (car clipboards)
  550. '(clipboards . 1)
  551. (car clipboards))))))
  552. ((equal char "p")
  553. (let*
  554. ((prop (org-substring-no-properties prompt))
  555. (pall (concat prop "_ALL"))
  556. (allowed
  557. (with-current-buffer
  558. (or (find-buffer-visiting file)
  559. (find-file-noselect file))
  560. (or (cdr (assoc pall org-file-properties))
  561. (cdr (assoc pall org-global-properties))
  562. (cdr (assoc pall org-global-properties-fixed)))))
  563. (existing (with-current-buffer
  564. (or (find-buffer-visiting file)
  565. (find-file-noselect file))
  566. (mapcar 'list (org-property-values prop))))
  567. (propprompt (concat "Value for " prop ": "))
  568. (val (if allowed
  569. (org-completing-read
  570. propprompt
  571. (mapcar 'list (org-split-string allowed "[ \t]+"))
  572. nil 'req-match)
  573. (org-completing-read-no-i propprompt existing nil nil
  574. "" nil ""))))
  575. (org-set-property prop val)))
  576. (char
  577. ;; These are the date/time related ones
  578. (setq org-time-was-given (equal (upcase char) char))
  579. (setq time (org-read-date (equal (upcase char) "U") t nil
  580. prompt))
  581. (org-insert-time-stamp time org-time-was-given
  582. (member char '("u" "U"))
  583. nil nil (list org-end-time-was-given)))
  584. (t
  585. (let (org-completion-use-ido)
  586. (insert (org-without-partial-completion
  587. (org-completing-read-no-i
  588. (concat (if prompt prompt "Enter string")
  589. (if default (concat " [" default "]"))
  590. ": ")
  591. completions nil nil nil histvar default))))))))
  592. (goto-char (point-min))
  593. (if (re-search-forward "%\\?" nil t)
  594. (replace-match "")
  595. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  596. (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1)))
  597. (when (save-excursion
  598. (goto-char (point-min))
  599. (re-search-forward "%&" nil t))
  600. (replace-match "")
  601. (org-set-local 'org-jump-to-target-location t))
  602. (when org-remember-backup-directory
  603. (unless (file-directory-p org-remember-backup-directory)
  604. (make-directory org-remember-backup-directory))
  605. (org-set-local 'auto-save-file-name-transforms nil)
  606. (setq buffer-file-name
  607. (expand-file-name
  608. (format-time-string "remember-%Y-%m-%d-%H-%M-%S")
  609. org-remember-backup-directory))
  610. (save-buffer)
  611. (org-set-local 'auto-save-visited-file-name t)
  612. (auto-save-mode 1))
  613. (when (save-excursion
  614. (goto-char (point-min))
  615. (re-search-forward "%!" nil t))
  616. (replace-match "")
  617. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  618. (defun org-remember-escaped-% ()
  619. (if (equal (char-before (match-beginning 0)) ?\\)
  620. (progn
  621. (delete-region (1- (match-beginning 0)) (match-beginning 0))
  622. t)
  623. nil))
  624. (defun org-remember-finish-immediately ()
  625. "File remember note immediately.
  626. This should be run in `post-command-hook' and will remove itself
  627. from that hook."
  628. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  629. (org-remember-finalize))
  630. (defun org-remember-visit-immediately ()
  631. "File remember note immediately.
  632. This should be run in `post-command-hook' and will remove itself
  633. from that hook."
  634. (org-remember '(16))
  635. (goto-char (or (text-property-any
  636. (point) (save-excursion (org-end-of-subtree t t))
  637. 'org-position-cursor t)
  638. (point)))
  639. (message "%s"
  640. (format
  641. (substitute-command-keys
  642. "Restore window configuration with \\[jump-to-register] %c")
  643. remember-register)))
  644. (defvar org-clock-marker) ; Defined in org.el
  645. (defun org-remember-finalize ()
  646. "Finalize the remember process."
  647. (interactive)
  648. (unless org-remember-mode
  649. (error "This does not seem to be a remember buffer for Org-mode"))
  650. (run-hooks 'org-remember-before-finalize-hook)
  651. (unless (fboundp 'remember-finalize)
  652. (defalias 'remember-finalize 'remember-buffer))
  653. (when (and org-clock-marker
  654. (equal (marker-buffer org-clock-marker) (current-buffer)))
  655. ;; the clock is running in this buffer.
  656. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  657. (or (eq org-remember-clock-out-on-exit t)
  658. (and org-remember-clock-out-on-exit
  659. (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
  660. (let (org-log-note-clock-out) (org-clock-out))))
  661. (when buffer-file-name
  662. (do-auto-save))
  663. (remember-finalize))
  664. (defun org-remember-kill ()
  665. "Abort the current remember process."
  666. (interactive)
  667. (let ((org-note-abort t))
  668. (org-remember-finalize)))
  669. ;;;###autoload
  670. (defun org-remember (&optional goto org-force-remember-template-char)
  671. "Call `remember'. If this is already a remember buffer, re-apply template.
  672. If there is an active region, make sure remember uses it as initial content
  673. of the remember buffer.
  674. When called interactively with a \\[universal-argument] \
  675. prefix argument GOTO, don't remember
  676. anything, just go to the file/headline where the selected template usually
  677. stores its notes. With a double prefix argument \
  678. \\[universal-argument] \\[universal-argument], go to the last
  679. note stored by remember.
  680. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  681. associated with a template in `org-remember-templates'."
  682. (interactive "P")
  683. (org-require-remember)
  684. (cond
  685. ((equal goto '(4)) (org-go-to-remember-target))
  686. ((equal goto '(16)) (org-remember-goto-last-stored))
  687. (t
  688. ;; set temporary variables that will be needed in
  689. ;; `org-select-remember-template'
  690. (setq org-select-template-temp-major-mode major-mode)
  691. (setq org-select-template-original-buffer (current-buffer))
  692. (if org-remember-mode
  693. (progn
  694. (when (< (length org-remember-templates) 2)
  695. (error "No other template available"))
  696. (erase-buffer)
  697. (let ((annotation (plist-get org-store-link-plist :annotation))
  698. (initial (plist-get org-store-link-plist :initial)))
  699. (org-remember-apply-template))
  700. (message "Press C-c C-c to remember data"))
  701. (if (org-region-active-p)
  702. (org-do-remember (buffer-substring (point) (mark)))
  703. (org-do-remember))))))
  704. (defvar org-remember-last-stored-marker (make-marker)
  705. "Marker pointing to the entry most recently stored with `org-remember'.")
  706. (defun org-remember-goto-last-stored ()
  707. "Go to the location where the last remember note was stored."
  708. (interactive)
  709. (org-goto-marker-or-bmk org-remember-last-stored-marker
  710. "org-remember-last-stored")
  711. (message "This is the last note stored by remember"))
  712. (defun org-go-to-remember-target (&optional template-key)
  713. "Go to the target location of a remember template.
  714. The user is queried for the template."
  715. (interactive)
  716. (let* (org-select-template-temp-major-mode
  717. (entry (org-select-remember-template template-key))
  718. (file (nth 1 entry))
  719. (heading (nth 2 entry))
  720. visiting)
  721. (unless (and file (stringp file) (string-match "\\S-" file))
  722. (setq file org-default-notes-file))
  723. (when (and file (not (file-name-absolute-p file)))
  724. (setq file (expand-file-name file org-directory)))
  725. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  726. (setq heading org-remember-default-headline))
  727. (setq visiting (org-find-base-buffer-visiting file))
  728. (if (not visiting) (find-file-noselect file))
  729. (org-pop-to-buffer-same-window (or visiting (get-file-buffer file)))
  730. (widen)
  731. (goto-char (point-min))
  732. (if (re-search-forward
  733. (format org-complex-heading-regexp-format (regexp-quote heading))
  734. nil t)
  735. (goto-char (match-beginning 0))
  736. (error "Target headline not found: %s" heading))))
  737. ;; FIXME (bzg): let's clean up of final empty lines happen only once
  738. ;; (see the org-remember-delete-empty-lines-at-end option below)
  739. ;;;###autoload
  740. (defun org-remember-handler ()
  741. "Store stuff from remember.el into an org file.
  742. When the template has specified a file and a headline, the entry is filed
  743. there, or in the location defined by `org-default-notes-file' and
  744. `org-remember-default-headline'.
  745. \\<org-remember-mode-map>
  746. If no defaults have been defined, or if the current prefix argument
  747. is 1 (using C-1 \\[org-remember-finalize] to exit remember), an interactive
  748. process is used to select the target location.
  749. When the prefix is 0 (i.e. when remember is exited with \
  750. C-0 \\[org-remember-finalize]),
  751. the entry is filed to the same location as the previous note.
  752. When the prefix is 2 (i.e. when remember is exited with \
  753. C-2 \\[org-remember-finalize]),
  754. the entry is filed as a subentry of the entry where the clock is
  755. currently running.
  756. When \\[universal-argument] has been used as prefix argument, the
  757. note is stored and Emacs moves point to the new location of the
  758. note, so that editing can be continued there (similar to
  759. inserting \"%&\" into the template).
  760. Before storing the note, the function ensures that the text has an
  761. org-mode-style headline, i.e. a first line that starts with
  762. a \"*\". If not, a headline is constructed from the current date and
  763. some additional data.
  764. If the variable `org-adapt-indentation' is non-nil, the entire text is
  765. also indented so that it starts in the same column as the headline
  766. \(i.e. after the stars).
  767. See also the variable `org-reverse-note-order'."
  768. (when (and (equal current-prefix-arg 2)
  769. (not (marker-buffer org-clock-marker)))
  770. (error "No running clock"))
  771. (when (org-bound-and-true-p org-jump-to-target-location)
  772. (let* ((end (min (point-max) (1+ (point))))
  773. (beg (point)))
  774. (if (= end beg) (setq beg (1- beg)))
  775. (put-text-property beg end 'org-position-cursor t)))
  776. (goto-char (point-min))
  777. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  778. (replace-match ""))
  779. (when org-remember-delete-empty-lines-at-end
  780. (goto-char (point-max))
  781. (beginning-of-line 1)
  782. (while (and (looking-at "[ \t]*$\\|##.*") (> (point) 1))
  783. (delete-region (1- (point)) (point-max))
  784. (beginning-of-line 1)))
  785. (catch 'quit
  786. (if org-note-abort (throw 'quit t))
  787. (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
  788. (backup-file
  789. (and buffer-file-name
  790. (equal (file-name-directory buffer-file-name)
  791. (file-name-as-directory
  792. (expand-file-name org-remember-backup-directory)))
  793. (string-match "^remember-[0-9]\\{4\\}"
  794. (file-name-nondirectory buffer-file-name))
  795. buffer-file-name))
  796. (dummy
  797. (unless (string-match "\\S-" (buffer-string))
  798. (message "Nothing to remember")
  799. (and backup-file
  800. (ignore-errors
  801. (delete-file backup-file)
  802. (delete-file (concat backup-file "~"))))
  803. (set-buffer-modified-p nil)
  804. (throw 'quit t)))
  805. (reference-date org-remember-reference-date)
  806. (previousp (and (member current-prefix-arg '((16) 0))
  807. org-remember-previous-location))
  808. (clockp (equal current-prefix-arg 2))
  809. (clocksp (equal current-prefix-arg 3))
  810. (fastp (org-xor (equal current-prefix-arg 1)
  811. org-remember-store-without-prompt))
  812. (file (cond
  813. (fastp org-default-notes-file)
  814. ((and (eq org-remember-interactive-interface 'refile)
  815. org-refile-targets)
  816. org-default-notes-file)
  817. ((not previousp)
  818. (org-get-org-file))))
  819. (heading org-remember-default-headline)
  820. (visiting (and file (org-find-base-buffer-visiting file)))
  821. (org-startup-folded nil)
  822. (org-startup-align-all-tables nil)
  823. (org-goto-start-pos 1)
  824. spos exitcmd level reversed txt text-before-node-creation)
  825. (when (equal current-prefix-arg '(4))
  826. (setq visitp t))
  827. (when previousp
  828. (setq file (car org-remember-previous-location)
  829. visiting (and file (org-find-base-buffer-visiting file))
  830. heading (cdr org-remember-previous-location)
  831. fastp t))
  832. (when (or clockp clocksp)
  833. (setq file (buffer-file-name (marker-buffer org-clock-marker))
  834. visiting (and file (org-find-base-buffer-visiting file))
  835. heading org-clock-heading-for-remember
  836. fastp t))
  837. (setq current-prefix-arg nil)
  838. ;; Modify text so that it becomes a nice subtree which can be inserted
  839. ;; into an org tree.
  840. (when org-remember-delete-empty-lines-at-end
  841. (goto-char (point-min))
  842. (if (re-search-forward "[ \t\n]+\\'" nil t)
  843. ;; remove empty lines at end
  844. (replace-match "")))
  845. (goto-char (point-min))
  846. (setq text-before-node-creation (buffer-string))
  847. (unless (looking-at org-outline-regexp)
  848. ;; add a headline
  849. (insert (concat "* " (current-time-string)
  850. " (" (remember-buffer-desc) ")\n"))
  851. (backward-char 1)
  852. (when org-adapt-indentation
  853. (while (re-search-forward "^" nil t)
  854. (insert " "))))
  855. ;; Delete final empty lines
  856. (when org-remember-delete-empty-lines-at-end
  857. (goto-char (point-min))
  858. (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
  859. (replace-match "\n\n")
  860. (if (re-search-forward "[ \t\n]*\\'")
  861. (replace-match "\n"))))
  862. (goto-char (point-min))
  863. (setq txt (buffer-string))
  864. (org-save-markers-in-region (point-min) (point-max))
  865. (set-buffer-modified-p nil)
  866. (when (and (eq org-remember-interactive-interface 'refile)
  867. (not fastp))
  868. (org-refile nil (or visiting (find-file-noselect file)))
  869. (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
  870. (save-excursion
  871. (bookmark-jump "org-refile-last-stored")
  872. (bookmark-set "org-remember-last-stored")
  873. (move-marker org-remember-last-stored-marker (point)))
  874. (throw 'quit t))
  875. ;; Find the file
  876. (with-current-buffer (or visiting (find-file-noselect file))
  877. (unless (or (eq major-mode 'org-mode) (member heading '(top bottom)))
  878. (error "Target files for notes must be in Org-mode if not filing to top/bottom"))
  879. (save-excursion
  880. (save-restriction
  881. (widen)
  882. (setq reversed (org-notes-order-reversed-p))
  883. ;; Find the default location
  884. (when heading
  885. (cond
  886. ((not (eq major-mode 'org-mode))
  887. (if (eq heading 'top)
  888. (goto-char (point-min))
  889. (goto-char (point-max))
  890. (or (bolp) (newline)))
  891. (insert text-before-node-creation)
  892. (when remember-save-after-remembering
  893. (save-buffer)
  894. (if (not visiting) (kill-buffer (current-buffer))))
  895. (throw 'quit t))
  896. ((eq heading 'top)
  897. (goto-char (point-min))
  898. (or (looking-at org-outline-regexp)
  899. (re-search-forward org-outline-regexp nil t))
  900. (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
  901. ((eq heading 'bottom)
  902. (goto-char (point-max))
  903. (or (bolp) (newline))
  904. (setq org-goto-start-pos (point)))
  905. ((eq heading 'date-tree)
  906. (org-datetree-find-date-create reference-date)
  907. (setq reversed nil)
  908. (setq org-goto-start-pos (point)))
  909. ((and (stringp heading) (string-match "\\S-" heading))
  910. (goto-char (point-min))
  911. (if (re-search-forward
  912. (format org-complex-heading-regexp-format
  913. (regexp-quote heading))
  914. nil t)
  915. (setq org-goto-start-pos (match-beginning 0))
  916. (when fastp
  917. (goto-char (point-max))
  918. (unless (bolp) (newline))
  919. (insert "* " heading "\n")
  920. (setq org-goto-start-pos (point-at-bol 0)))))
  921. (t (goto-char (point-min)) (setq org-goto-start-pos (point)
  922. heading 'top))))
  923. ;; Ask the User for a location, using the appropriate interface
  924. (cond
  925. ((and fastp (memq heading '(top bottom)))
  926. (setq spos org-goto-start-pos
  927. exitcmd (if (eq heading 'top) 'left nil)))
  928. (fastp (setq spos org-goto-start-pos
  929. exitcmd 'return))
  930. ((eq org-remember-interactive-interface 'outline)
  931. (setq spos (org-get-location (current-buffer)
  932. org-remember-help)
  933. exitcmd (cdr spos)
  934. spos (car spos)))
  935. ((eq org-remember-interactive-interface 'outline-path-completion)
  936. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  937. (org-refile-use-outline-path t))
  938. (setq spos (org-refile-get-location "Heading")
  939. exitcmd 'return
  940. spos (nth 3 spos))))
  941. (t (error "This should not happen")))
  942. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  943. ; not handle this note
  944. (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
  945. (goto-char spos)
  946. (cond ((org-on-heading-p t)
  947. (org-back-to-heading t)
  948. (setq level (funcall outline-level))
  949. (cond
  950. ((eq exitcmd 'return)
  951. ;; sublevel of current
  952. (setq org-remember-previous-location
  953. (cons (abbreviate-file-name file)
  954. (org-get-heading 'notags)))
  955. (if reversed
  956. (outline-next-heading)
  957. (org-end-of-subtree t)
  958. (if (not (bolp))
  959. (if (looking-at "[ \t]*\n")
  960. (beginning-of-line 2)
  961. (end-of-line 1)
  962. (insert "\n"))))
  963. (org-paste-subtree (if clocksp
  964. level
  965. (org-get-valid-level level 1)) txt)
  966. (and org-auto-align-tags (org-set-tags nil t))
  967. (bookmark-set "org-remember-last-stored")
  968. (move-marker org-remember-last-stored-marker (point)))
  969. ((eq exitcmd 'left)
  970. ;; before current
  971. (org-paste-subtree level txt)
  972. (and org-auto-align-tags (org-set-tags nil t))
  973. (bookmark-set "org-remember-last-stored")
  974. (move-marker org-remember-last-stored-marker (point)))
  975. ((eq exitcmd 'right)
  976. ;; after current
  977. (org-end-of-subtree t)
  978. (org-paste-subtree level txt)
  979. (and org-auto-align-tags (org-set-tags nil t))
  980. (bookmark-set "org-remember-last-stored")
  981. (move-marker org-remember-last-stored-marker (point)))
  982. (t (error "This should not happen"))))
  983. ((eq heading 'bottom)
  984. (org-paste-subtree 1 txt)
  985. (and org-auto-align-tags (org-set-tags nil t))
  986. (bookmark-set "org-remember-last-stored")
  987. (move-marker org-remember-last-stored-marker (point)))
  988. ((and (bobp) (not reversed))
  989. ;; Put it at the end, one level below level 1
  990. (save-restriction
  991. (widen)
  992. (goto-char (point-max))
  993. (if (not (bolp)) (newline))
  994. (org-paste-subtree (org-get-valid-level 1 1) txt)
  995. (and org-auto-align-tags (org-set-tags nil t))
  996. (bookmark-set "org-remember-last-stored")
  997. (move-marker org-remember-last-stored-marker (point))))
  998. ((and (bobp) reversed)
  999. ;; Put it at the start, as level 1
  1000. (save-restriction
  1001. (widen)
  1002. (goto-char (point-min))
  1003. (re-search-forward org-outline-regexp-bol nil t)
  1004. (beginning-of-line 1)
  1005. (org-paste-subtree 1 txt)
  1006. (and org-auto-align-tags (org-set-tags nil t))
  1007. (bookmark-set "org-remember-last-stored")
  1008. (move-marker org-remember-last-stored-marker (point))))
  1009. (t
  1010. ;; Put it right there, with automatic level determined by
  1011. ;; org-paste-subtree or from prefix arg
  1012. (org-paste-subtree
  1013. (if (numberp current-prefix-arg) current-prefix-arg)
  1014. txt)
  1015. (and org-auto-align-tags (org-set-tags nil t))
  1016. (bookmark-set "org-remember-last-stored")
  1017. (move-marker org-remember-last-stored-marker (point))))
  1018. (when remember-save-after-remembering
  1019. (save-buffer)
  1020. (if (and (not visiting)
  1021. (not (equal (marker-buffer org-clock-marker)
  1022. (current-buffer))))
  1023. (kill-buffer (current-buffer))))
  1024. (when org-remember-auto-remove-backup-files
  1025. (when backup-file
  1026. (ignore-errors
  1027. (delete-file backup-file)
  1028. (delete-file (concat backup-file "~"))))
  1029. (when org-remember-backup-directory
  1030. (let ((n (length
  1031. (directory-files
  1032. org-remember-backup-directory nil
  1033. "^remember-.*[0-9]$"))))
  1034. (when (and org-remember-warn-about-backups
  1035. (> n 0))
  1036. (message
  1037. "%d backup files (unfinished remember calls) in %s"
  1038. n org-remember-backup-directory))))))))))
  1039. t) ;; return t to indicate that we took care of this note.
  1040. (defun org-do-remember (&optional initial)
  1041. "Call remember."
  1042. (remember initial))
  1043. (defun org-require-remember ()
  1044. "Make sure remember is loaded, or install our own emergency version of it."
  1045. (condition-case nil
  1046. (require 'remember)
  1047. (error
  1048. ;; Lets install our own micro version of remember
  1049. (defvar remember-register ?R)
  1050. (defvar remember-mode-hook nil)
  1051. (defvar remember-handler-functions nil)
  1052. (defvar remember-buffer "*Remember*")
  1053. (defvar remember-save-after-remembering t)
  1054. (defvar remember-annotation-functions '(buffer-file-name))
  1055. (defun remember-finalize ()
  1056. (run-hook-with-args-until-success 'remember-handler-functions)
  1057. (when (equal remember-buffer (buffer-name))
  1058. (kill-buffer (current-buffer))
  1059. (jump-to-register remember-register)))
  1060. (defun remember-mode ()
  1061. (fundamental-mode)
  1062. (setq mode-name "Remember")
  1063. (run-hooks 'remember-mode-hook))
  1064. (defun remember (&optional initial)
  1065. (window-configuration-to-register remember-register)
  1066. (let* ((annotation (run-hook-with-args-until-success
  1067. 'remember-annotation-functions)))
  1068. (switch-to-buffer-other-window (get-buffer-create remember-buffer))
  1069. (remember-mode)))
  1070. (defun remember-buffer-desc ()
  1071. (buffer-substring (point-min) (save-excursion (goto-char (point-min))
  1072. (point-at-eol)))))))
  1073. (provide 'org-remember)
  1074. ;;; org-remember.el ends here