org-remember.el 45 KB

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