org-remember.el 45 KB

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