org-remember.el 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. ;;; org-remember.el --- Fast note taking in Org-mode
  2. ;; Copyright (C) 2004-2012 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-at-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