org-remember.el 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. ;;; org-remember.el --- Fast note taking in Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 6.02b
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 3, or (at your option)
  13. ;; any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file contains the system to take fast notes with Org-mode.
  26. ;; This system is used together with John Wiegleys `remember.el'.
  27. ;;; Code:
  28. (eval-when-compile
  29. (require 'cl))
  30. (require 'org)
  31. (declare-function remember "remember" (&optional initial))
  32. (declare-function remember-buffer-desc "remember" ())
  33. (declare-function remember-finalize "remember" ())
  34. (defvar remember-save-after-remembering)
  35. (defvar remember-data-file)
  36. (defvar remember-register)
  37. (defvar remember-buffer)
  38. (defvar remember-handler-functions)
  39. (defvar remember-annotation-functions)
  40. (defgroup org-remember nil
  41. "Options concerning interaction with remember.el."
  42. :tag "Org Remember"
  43. :group 'org)
  44. (defcustom org-remember-store-without-prompt t
  45. "Non-nil means, `C-c C-c' stores remember note without further promts.
  46. In this case, you need `C-u C-c C-c' to get the prompts for
  47. note file and headline.
  48. When this variable is nil, `C-c C-c' give you the prompts, and
  49. `C-u C-c C-c' trigger the fasttrack."
  50. :group 'org-remember
  51. :type 'boolean)
  52. (defcustom org-remember-interactive-interface 'refile
  53. "The interface to be used for interactive filing of remember notes.
  54. This is only used when the interactive mode for selecting a filing
  55. location is used (see the variable `org-remember-store-without-prompt').
  56. Allowed vaues are:
  57. outline The interface shows an outline of the relevant file
  58. and the correct heading is found by moving through
  59. the outline or by searching with incremental search.
  60. outline-path-completion Headlines in the current buffer are offered via
  61. completion.
  62. refile Use the refile interface, and offer headlines,
  63. possibly from different buffers."
  64. :group 'org-remember
  65. :type '(choice
  66. (const :tag "Refile" refile)
  67. (const :tag "Outline" outline)
  68. (const :tag "Outline-path-completion" outline-path-completion)))
  69. (defcustom org-remember-default-headline ""
  70. "The headline that should be the default location in the notes file.
  71. When filing remember notes, the cursor will start at that position.
  72. You can set this on a per-template basis with the variable
  73. `org-remember-templates'."
  74. :group 'org-remember
  75. :type 'string)
  76. (defcustom org-remember-templates nil
  77. "Templates for the creation of remember buffers.
  78. When nil, just let remember make the buffer.
  79. When not nil, this is a list of 5-element lists. In each entry, the first
  80. element is the name of the template, which should be a single short word.
  81. The second element is a character, a unique key to select this template.
  82. The third element is the template.
  83. The fourth element is optional and can specify a destination file for
  84. remember items created with this template. The default file is given
  85. by `org-default-notes-file'. If the file name is not an absolute path,
  86. it will be interpreted relative to `org-directory'.
  87. An optional fifth element can specify the headline in that file that should
  88. be offered first when the user is asked to file the entry. The default
  89. headline is given in the variable `org-remember-default-headline'.
  90. An optional sixth element specifies the contexts in which the user can
  91. select the template. This element can be either a list of major modes
  92. or a function. `org-remember' will first check whether the function
  93. returns `t' or if we are in any of the listed major modes, and select
  94. the template accordingly.
  95. The template specifies the structure of the remember buffer. It should have
  96. a first line starting with a star, to act as the org-mode headline.
  97. Furthermore, the following %-escapes will be replaced with content:
  98. %^{prompt} Prompt the user for a string and replace this sequence with it.
  99. A default value and a completion table ca be specified like this:
  100. %^{prompt|default|completion2|completion3|...}
  101. %t time stamp, date only
  102. %T time stamp with date and time
  103. %u, %U like the above, but inactive time stamps
  104. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  105. You may define a prompt like %^{Please specify birthday}t
  106. %n user name (taken from `user-full-name')
  107. %a annotation, normally the link created with org-store-link
  108. %i initial content, the region active. If %i is indented,
  109. the entire inserted text will be indented as well.
  110. %c content of the clipboard, or current kill ring head
  111. %^g prompt for tags, with completion on tags in target file
  112. %^G prompt for tags, with completion all tags in all agenda files
  113. %:keyword specific information for certain link types, see below
  114. %[pathname] insert the contents of the file given by `pathname'
  115. %(sexp) evaluate elisp `(sexp)' and replace with the result
  116. %! Store this note immediately after filling the template
  117. %? After completing the template, position cursor here.
  118. Apart from these general escapes, you can access information specific to the
  119. link type that is created. For example, calling `remember' in emails or gnus
  120. will record the author and the subject of the message, which you can access
  121. with %:author and %:subject, respectively. Here is a complete list of what
  122. is recorded for each link type.
  123. Link type | Available information
  124. -------------------+------------------------------------------------------
  125. bbdb | %:type %:name %:company
  126. vm, wl, mh, rmail | %:type %:subject %:message-id
  127. | %:from %:fromname %:fromaddress
  128. | %:to %:toname %:toaddress
  129. | %:fromto (either \"to NAME\" or \"from NAME\")
  130. gnus | %:group, for messages also all email fields
  131. w3, w3m | %:type %:url
  132. info | %:type %:file %:node
  133. calendar | %:type %:date"
  134. :group 'org-remember
  135. :get (lambda (var) ; Make sure all entries have at least 5 elements
  136. (mapcar (lambda (x)
  137. (if (not (stringp (car x))) (setq x (cons "" x)))
  138. (cond ((= (length x) 4) (append x '("")))
  139. ((= (length x) 3) (append x '("" "")))
  140. (t x)))
  141. (default-value var)))
  142. :type '(repeat
  143. :tag "enabled"
  144. (list :value ("" ?a "\n" nil nil nil)
  145. (string :tag "Name")
  146. (character :tag "Selection Key")
  147. (string :tag "Template")
  148. (choice
  149. (file :tag "Destination file")
  150. (const :tag "Prompt for file" nil))
  151. (choice
  152. (string :tag "Destination headline")
  153. (const :tag "Selection interface for heading"))
  154. (choice
  155. (const :tag "Use by default" nil)
  156. (const :tag "Use in all contexts" t)
  157. (repeat :tag "Use only if in major mode"
  158. (symbol :tag "Major mode"))
  159. (function :tag "Perform a check against function")))))
  160. (defcustom org-remember-clock-out-on-exit 'query
  161. "Non-nil means, stop the clock when exiting a clocking remember buffer.
  162. This only applies if the clock is running in the remember buffer. If the
  163. clock is not stopped, it continues to run in the storage location.
  164. Instead of nil or t, this may also be the symbol `query' to prompt the
  165. user each time a remember buffer with a running clock is filed away. "
  166. :group 'org-remember
  167. :type '(choice
  168. (const :tag "Never" nil)
  169. (const :tag "Always" t)
  170. (const :tag "Query user" query)))
  171. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  172. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  173. ;;;###autoload
  174. (defun org-remember-insinuate ()
  175. "Setup remember.el for use wiht Org-mode."
  176. (require 'remember)
  177. (setq remember-annotation-functions '(org-remember-annotation))
  178. (setq remember-handler-functions '(org-remember-handler))
  179. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  180. ;;;###autoload
  181. (defun org-remember-annotation ()
  182. "Return a link to the current location as an annotation for remember.el.
  183. If you are using Org-mode files as target for data storage with
  184. remember.el, then the annotations should include a link compatible with the
  185. conventions in Org-mode. This function returns such a link."
  186. (org-store-link nil))
  187. (defconst org-remember-help
  188. "Select a destination location for the note.
  189. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  190. RET on headline -> Store as sublevel entry to current headline
  191. RET at beg-of-buf -> Append to file as level 2 headline
  192. <left>/<right> -> before/after current headline, same headings level")
  193. (defvar org-remember-previous-location nil)
  194. (defvar org-force-remember-template-char) ;; dynamically scoped
  195. ;; Save the major mode of the buffer we called remember from
  196. (defvar org-select-template-temp-major-mode nil)
  197. ;; Temporary store the buffer where remember was called from
  198. (defvar org-select-template-original-buffer nil)
  199. (defun org-select-remember-template (&optional use-char)
  200. (when org-remember-templates
  201. (let* ((pre-selected-templates
  202. (mapcar
  203. (lambda (tpl)
  204. (let ((ctxt (nth 5 tpl))
  205. (mode org-select-template-temp-major-mode)
  206. (buf org-select-template-original-buffer))
  207. (and (or (not ctxt) (eq ctxt t)
  208. (and (listp ctxt) (memq mode ctxt))
  209. (and (functionp ctxt)
  210. (with-current-buffer buf
  211. ;; Protect the user-defined function from error
  212. (condition-case nil (funcall ctxt) (error nil)))))
  213. tpl)))
  214. org-remember-templates))
  215. ;; If no template at this point, add the default templates:
  216. (pre-selected-templates1
  217. (if (not (delq nil pre-selected-templates))
  218. (mapcar (lambda(x) (if (not (nth 5 x)) x))
  219. org-remember-templates)
  220. pre-selected-templates))
  221. ;; Then unconditionnally add template for any contexts
  222. (pre-selected-templates2
  223. (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
  224. org-remember-templates)
  225. (delq nil pre-selected-templates1)))
  226. (templates (mapcar (lambda (x)
  227. (if (stringp (car x))
  228. (append (list (nth 1 x) (car x)) (cddr x))
  229. (append (list (car x) "") (cdr x))))
  230. (delq nil pre-selected-templates2)))
  231. (char (or use-char
  232. (cond
  233. ((= (length templates) 1)
  234. (caar templates))
  235. ((and (boundp 'org-force-remember-template-char)
  236. org-force-remember-template-char)
  237. (if (stringp org-force-remember-template-char)
  238. (string-to-char org-force-remember-template-char)
  239. org-force-remember-template-char))
  240. (t
  241. (message "Select template: %s"
  242. (mapconcat
  243. (lambda (x)
  244. (cond
  245. ((not (string-match "\\S-" (nth 1 x)))
  246. (format "[%c]" (car x)))
  247. ((equal (downcase (car x))
  248. (downcase (aref (nth 1 x) 0)))
  249. (format "[%c]%s" (car x)
  250. (substring (nth 1 x) 1)))
  251. (t (format "[%c]%s" (car x) (nth 1 x)))))
  252. templates " "))
  253. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  254. (when (equal char0 ?\C-g)
  255. (jump-to-register remember-register)
  256. (kill-buffer remember-buffer))
  257. char0))))))
  258. (cddr (assoc char templates)))))
  259. (defun org-get-x-clipboard (value)
  260. "Get the value of the x clibboard, in a way that also works with XEmacs."
  261. (if (eq window-system 'x)
  262. (let ((x (if org-xemacs-p
  263. (org-no-warnings (get-selection-no-error value))
  264. (and (fboundp 'x-selection-value)
  265. (x-selection-value value)))))
  266. (and (> (length x) 0) (set-text-properties 0 (length x) nil x) x))))
  267. ;;;###autoload
  268. (defun org-remember-apply-template (&optional use-char skip-interactive)
  269. "Initialize *remember* buffer with template, invoke `org-mode'.
  270. This function should be placed into `remember-mode-hook' and in fact requires
  271. to be run from that hook to function properly."
  272. (if org-remember-templates
  273. (let* ((entry (org-select-remember-template use-char))
  274. (tpl (car entry))
  275. (plist-p (if org-store-link-plist t nil))
  276. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  277. (string-match "\\S-" (nth 1 entry)))
  278. (nth 1 entry)
  279. org-default-notes-file))
  280. (headline (nth 2 entry))
  281. (v-c (and (> (length kill-ring) 0) (current-kill 0)))
  282. (v-x (or (org-get-x-clipboard 'PRIMARY)
  283. (org-get-x-clipboard 'CLIPBOARD)
  284. (org-get-x-clipboard 'SECONDARY)))
  285. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  286. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  287. (v-u (concat "[" (substring v-t 1 -1) "]"))
  288. (v-U (concat "[" (substring v-T 1 -1) "]"))
  289. ;; `initial' and `annotation' are bound in `remember'
  290. (v-i (if (boundp 'initial) initial))
  291. (v-a (if (and (boundp 'annotation) annotation)
  292. (if (equal annotation "[[]]") "" annotation)
  293. ""))
  294. (clipboards (remove nil (list v-i
  295. (org-get-x-clipboard 'PRIMARY)
  296. (org-get-x-clipboard 'CLIPBOARD)
  297. (org-get-x-clipboard 'SECONDARY)
  298. v-c)))
  299. (v-A (if (and v-a
  300. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  301. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  302. v-a))
  303. (v-n user-full-name)
  304. (org-startup-folded nil)
  305. org-time-was-given org-end-time-was-given x
  306. prompt completions char time pos default histvar)
  307. (when (and file (not (file-name-absolute-p file)))
  308. (setq file (expand-file-name file org-directory)))
  309. (setq org-store-link-plist
  310. (append (list :annotation v-a :initial v-i)
  311. org-store-link-plist))
  312. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  313. (erase-buffer)
  314. (insert (substitute-command-keys
  315. (format
  316. "## Filing location: Select interactively, default, or last used:
  317. ## %s to select file and header location interactively.
  318. ## %s \"%s\" -> \"* %s\"
  319. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  320. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  321. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  322. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  323. (abbreviate-file-name (or file org-default-notes-file))
  324. (or headline "")
  325. (or (car org-remember-previous-location) "???")
  326. (or (cdr org-remember-previous-location) "???"))))
  327. (insert tpl) (goto-char (point-min))
  328. ;; Simple %-escapes
  329. (while (re-search-forward "%\\([tTuUaiAcx]\\)" nil t)
  330. (when (and initial (equal (match-string 0) "%i"))
  331. (save-match-data
  332. (let* ((lead (buffer-substring
  333. (point-at-bol) (match-beginning 0))))
  334. (setq v-i (mapconcat 'identity
  335. (org-split-string initial "\n")
  336. (concat "\n" lead))))))
  337. (replace-match
  338. (or (eval (intern (concat "v-" (match-string 1)))) "")
  339. t t))
  340. ;; %[] Insert contents of a file.
  341. (goto-char (point-min))
  342. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  343. (let ((start (match-beginning 0))
  344. (end (match-end 0))
  345. (filename (expand-file-name (match-string 1))))
  346. (goto-char start)
  347. (delete-region start end)
  348. (condition-case error
  349. (insert-file-contents filename)
  350. (error (insert (format "%%![Couldn't insert %s: %s]"
  351. filename error))))))
  352. ;; %() embedded elisp
  353. (goto-char (point-min))
  354. (while (re-search-forward "%\\((.+)\\)" nil t)
  355. (goto-char (match-beginning 0))
  356. (let ((template-start (point)))
  357. (forward-char 1)
  358. (let ((result
  359. (condition-case error
  360. (eval (read (current-buffer)))
  361. (error (format "%%![Error: %s]" error)))))
  362. (delete-region template-start (point))
  363. (insert result))))
  364. ;; From the property list
  365. (when plist-p
  366. (goto-char (point-min))
  367. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  368. (and (setq x (or (plist-get org-store-link-plist
  369. (intern (match-string 1))) ""))
  370. (replace-match x t t))))
  371. ;; Turn on org-mode in the remember buffer, set local variables
  372. (org-mode)
  373. (org-set-local 'org-finish-function 'org-remember-finalize)
  374. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  375. (org-set-local 'org-default-notes-file file))
  376. (if (and headline (stringp headline) (string-match "\\S-" headline))
  377. (org-set-local 'org-remember-default-headline headline))
  378. ;; Interactive template entries
  379. (goto-char (point-min))
  380. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtTCL]\\)?" nil t)
  381. (setq char (if (match-end 3) (match-string 3))
  382. prompt (if (match-end 2) (match-string 2)))
  383. (goto-char (match-beginning 0))
  384. (replace-match "")
  385. (setq completions nil default nil)
  386. (when prompt
  387. (setq completions (org-split-string prompt "|")
  388. prompt (pop completions)
  389. default (car completions)
  390. histvar (intern (concat
  391. "org-remember-template-prompt-history::"
  392. (or prompt "")))
  393. completions (mapcar 'list completions)))
  394. (cond
  395. ((member char '("G" "g"))
  396. (let* ((org-last-tags-completion-table
  397. (org-global-tags-completion-table
  398. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  399. (org-add-colon-after-tag-completion t)
  400. (ins (completing-read
  401. (if prompt (concat prompt ": ") "Tags: ")
  402. 'org-tags-completion-function nil nil nil
  403. 'org-tags-history)))
  404. (setq ins (mapconcat 'identity
  405. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  406. ":"))
  407. (when (string-match "\\S-" ins)
  408. (or (equal (char-before) ?:) (insert ":"))
  409. (insert ins)
  410. (or (equal (char-after) ?:) (insert ":")))))
  411. ((equal char "C")
  412. (cond ((= (length clipboards) 1) (insert (car clipboards)))
  413. ((> (length clipboards) 1)
  414. (insert (read-string "Clipboard/kill value: "
  415. (car clipboards) '(clipboards . 1)
  416. (car clipboards))))))
  417. ((equal char "L")
  418. (cond ((= (length clipboards) 1)
  419. (org-insert-link 0 (car clipboards)))
  420. ((> (length clipboards) 1)
  421. (org-insert-link 0 (read-string "Clipboard/kill value: "
  422. (car clipboards)
  423. '(clipboards . 1)
  424. (car clipboards))))))
  425. (char
  426. (setq org-time-was-given (equal (upcase char) char))
  427. (setq time (org-read-date (equal (upcase char) "U") t nil
  428. prompt))
  429. (org-insert-time-stamp time org-time-was-given
  430. (member char '("u" "U"))
  431. nil nil (list org-end-time-was-given)))
  432. (t
  433. (insert (org-completing-read
  434. (concat (if prompt prompt "Enter string")
  435. (if default (concat " [" default "]"))
  436. ": ")
  437. completions nil nil nil histvar default)))))
  438. (goto-char (point-min))
  439. (if (re-search-forward "%\\?" nil t)
  440. (replace-match "")
  441. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  442. (org-mode)
  443. (org-set-local 'org-finish-function 'org-remember-finalize))
  444. (when (save-excursion
  445. (goto-char (point-min))
  446. (re-search-forward "%!" nil t))
  447. (replace-match "")
  448. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  449. (defun org-remember-finish-immediately ()
  450. "File remember note immediately.
  451. This should be run in `post-command-hook' and will remove itself
  452. from that hook."
  453. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  454. (when org-finish-function
  455. (funcall org-finish-function)))
  456. (defvar org-clock-marker) ; Defined in org.el
  457. (defun org-remember-finalize ()
  458. "Finalize the remember process."
  459. (unless (fboundp 'remember-finalize)
  460. (defalias 'remember-finalize 'remember-buffer))
  461. (when (and org-clock-marker
  462. (equal (marker-buffer org-clock-marker) (current-buffer)))
  463. ;; the clock is running in this buffer.
  464. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  465. (or (eq org-remember-clock-out-on-exit t)
  466. (and org-remember-clock-out-on-exit
  467. (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
  468. (let (org-log-note-clock-out) (org-clock-out))))
  469. (when buffer-file-name
  470. (save-buffer)
  471. (setq buffer-file-name nil))
  472. (remember-finalize))
  473. ;;;###autoload
  474. (defun org-remember (&optional goto org-force-remember-template-char)
  475. "Call `remember'. If this is already a remember buffer, re-apply template.
  476. If there is an active region, make sure remember uses it as initial content
  477. of the remember buffer.
  478. When called interactively with a `C-u' prefix argument GOTO, don't remember
  479. anything, just go to the file/headline where the selected template usually
  480. stores its notes. With a double prefix arg `C-u C-u', go to the last
  481. note stored by remember.
  482. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  483. associated with a template in `org-remember-templates'."
  484. (interactive "P")
  485. (cond
  486. ((equal goto '(4)) (org-go-to-remember-target))
  487. ((equal goto '(16)) (org-remember-goto-last-stored))
  488. (t
  489. ;; set temporary variables that will be needed in
  490. ;; `org-select-remember-template'
  491. (setq org-select-template-temp-major-mode major-mode)
  492. (setq org-select-template-original-buffer (current-buffer))
  493. (if (eq org-finish-function 'org-remember-finalize)
  494. (progn
  495. (when (< (length org-remember-templates) 2)
  496. (error "No other template available"))
  497. (erase-buffer)
  498. (let ((annotation (plist-get org-store-link-plist :annotation))
  499. (initial (plist-get org-store-link-plist :initial)))
  500. (org-remember-apply-template))
  501. (message "Press C-c C-c to remember data"))
  502. (if (org-region-active-p)
  503. (org-do-remember (buffer-substring (point) (mark)))
  504. (org-do-remember))))))
  505. (defun org-remember-goto-last-stored ()
  506. "Go to the location where the last remember note was stored."
  507. (interactive)
  508. (bookmark-jump "org-remember-last-stored")
  509. (message "This is the last note stored by remember"))
  510. (defun org-go-to-remember-target (&optional template-key)
  511. "Go to the target location of a remember template.
  512. The user is queried for the template."
  513. (interactive)
  514. (let* (org-select-template-temp-major-mode
  515. (entry (org-select-remember-template template-key))
  516. (file (nth 1 entry))
  517. (heading (nth 2 entry))
  518. visiting)
  519. (unless (and file (stringp file) (string-match "\\S-" file))
  520. (setq file org-default-notes-file))
  521. (when (and file (not (file-name-absolute-p file)))
  522. (setq file (expand-file-name file org-directory)))
  523. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  524. (setq heading org-remember-default-headline))
  525. (setq visiting (org-find-base-buffer-visiting file))
  526. (if (not visiting) (find-file-noselect file))
  527. (switch-to-buffer (or visiting (get-file-buffer file)))
  528. (widen)
  529. (goto-char (point-min))
  530. (if (re-search-forward
  531. (concat "^\\*+[ \t]+" (regexp-quote heading)
  532. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  533. nil t)
  534. (goto-char (match-beginning 0))
  535. (error "Target headline not found: %s" heading))))
  536. ;;;###autoload
  537. (defun org-remember-handler ()
  538. "Store stuff from remember.el into an org file.
  539. First prompts for an org file. If the user just presses return, the value
  540. of `org-default-notes-file' is used.
  541. Then the command offers the headings tree of the selected file in order to
  542. file the text at a specific location.
  543. You can either immediately press RET to get the note appended to the
  544. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  545. find a better place. Then press RET or <left> or <right> in insert the note.
  546. Key Cursor position Note gets inserted
  547. -----------------------------------------------------------------------------
  548. RET buffer-start as level 1 heading at end of file
  549. RET on headline as sublevel of the heading at cursor
  550. RET no heading at cursor position, level taken from context.
  551. Or use prefix arg to specify level manually.
  552. <left> on headline as same level, before current heading
  553. <right> on headline as same level, after current heading
  554. So the fastest way to store the note is to press RET RET to append it to
  555. the default file. This way your current train of thought is not
  556. interrupted, in accordance with the principles of remember.el.
  557. You can also get the fast execution without prompting by using
  558. C-u C-c C-c to exit the remember buffer. See also the variable
  559. `org-remember-store-without-prompt'.
  560. Before being stored away, the function ensures that the text has a
  561. headline, i.e. a first line that starts with a \"*\". If not, a headline
  562. is constructed from the current date and some additional data.
  563. If the variable `org-adapt-indentation' is non-nil, the entire text is
  564. also indented so that it starts in the same column as the headline
  565. \(i.e. after the stars).
  566. See also the variable `org-reverse-note-order'."
  567. (goto-char (point-min))
  568. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  569. (replace-match ""))
  570. (goto-char (point-max))
  571. (beginning-of-line 1)
  572. (while (looking-at "[ \t]*$\\|##.*")
  573. (delete-region (1- (point)) (point-max))
  574. (beginning-of-line 1))
  575. (catch 'quit
  576. (if org-note-abort (throw 'quit nil))
  577. (let* ((fastp (org-xor (equal current-prefix-arg '(4))
  578. org-remember-store-without-prompt))
  579. (file (cond
  580. (fastp org-default-notes-file)
  581. ((and (eq org-remember-interactive-interface 'refile)
  582. org-refile-targets)
  583. org-default-notes-file)
  584. ((not (and (equal current-prefix-arg '(16))
  585. org-remember-previous-location))
  586. (org-get-org-file))))
  587. (heading org-remember-default-headline)
  588. (visiting (and file (org-find-base-buffer-visiting file)))
  589. (org-startup-folded nil)
  590. (org-startup-align-all-tables nil)
  591. (org-goto-start-pos 1)
  592. spos exitcmd level reversed txt)
  593. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  594. (setq file (car org-remember-previous-location)
  595. heading (cdr org-remember-previous-location)
  596. fastp t))
  597. (setq current-prefix-arg nil)
  598. ;; Modify text so that it becomes a nice subtree which can be inserted
  599. ;; into an org tree.
  600. (goto-char (point-min))
  601. (if (re-search-forward "[ \t\n]+\\'" nil t)
  602. ;; remove empty lines at end
  603. (replace-match ""))
  604. (goto-char (point-min))
  605. (unless (looking-at org-outline-regexp)
  606. ;; add a headline
  607. (insert (concat "* " (current-time-string)
  608. " (" (remember-buffer-desc) ")\n"))
  609. (backward-char 1)
  610. (when org-adapt-indentation
  611. (while (re-search-forward "^" nil t)
  612. (insert " "))))
  613. (goto-char (point-min))
  614. (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
  615. (replace-match "\n\n")
  616. (if (re-search-forward "[ \t\n]*\\'")
  617. (replace-match "\n")))
  618. (goto-char (point-min))
  619. (setq txt (buffer-string))
  620. (org-save-markers-in-region (point-min) (point-max))
  621. (when (and (eq org-remember-interactive-interface 'refile)
  622. (not fastp))
  623. (org-refile nil (or visiting (find-file-noselect file)))
  624. (throw 'quit t))
  625. ;; Find the file
  626. (if (not visiting) (find-file-noselect file))
  627. (with-current-buffer (or visiting (get-file-buffer file))
  628. (unless (org-mode-p)
  629. (error "Target files for remember notes must be in Org-mode"))
  630. (save-excursion
  631. (save-restriction
  632. (widen)
  633. (and (goto-char (point-min))
  634. (not (re-search-forward "^\\* " nil t))
  635. (insert "\n* " (or heading "Notes") "\n"))
  636. (setq reversed (org-notes-order-reversed-p))
  637. ;; Find the default location
  638. (when (and heading (stringp heading) (string-match "\\S-" heading))
  639. (goto-char (point-min))
  640. (if (re-search-forward
  641. (concat "^\\*+[ \t]+" (regexp-quote heading)
  642. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  643. nil t)
  644. (setq org-goto-start-pos (match-beginning 0))
  645. (when fastp
  646. (goto-char (point-max))
  647. (unless (bolp) (newline))
  648. (insert "* " heading "\n")
  649. (setq org-goto-start-pos (point-at-bol 0)))))
  650. ;; Ask the User for a location, using the appropriate interface
  651. (cond
  652. (fastp (setq spos org-goto-start-pos
  653. exitcmd 'return))
  654. ((eq org-remember-interactive-interface 'outline)
  655. (setq spos (org-get-location (current-buffer)
  656. org-remember-help)
  657. exitcmd (cdr spos)
  658. spos (car spos)))
  659. ((eq org-remember-interactive-interface 'outline-path-completion)
  660. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  661. (org-refile-use-outline-path t))
  662. (setq spos (org-refile-get-location "Heading: ")
  663. exitcmd 'return
  664. spos (nth 3 spos))))
  665. (t (error "This should not happen")))
  666. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  667. ; not handle this note
  668. (goto-char spos)
  669. (cond ((org-on-heading-p t)
  670. (org-back-to-heading t)
  671. (setq level (funcall outline-level))
  672. (cond
  673. ((eq exitcmd 'return)
  674. ;; sublevel of current
  675. (setq org-remember-previous-location
  676. (cons (abbreviate-file-name file)
  677. (org-get-heading 'notags)))
  678. (if reversed
  679. (outline-next-heading)
  680. (org-end-of-subtree t)
  681. (if (not (bolp))
  682. (if (looking-at "[ \t]*\n")
  683. (beginning-of-line 2)
  684. (end-of-line 1)
  685. (insert "\n"))))
  686. (bookmark-set "org-remember-last-stored")
  687. (org-paste-subtree (org-get-valid-level level 1) txt))
  688. ((eq exitcmd 'left)
  689. ;; before current
  690. (bookmark-set "org-remember-last-stored")
  691. (org-paste-subtree level txt))
  692. ((eq exitcmd 'right)
  693. ;; after current
  694. (org-end-of-subtree t)
  695. (bookmark-set "org-remember-last-stored")
  696. (org-paste-subtree level txt))
  697. (t (error "This should not happen"))))
  698. ((and (bobp) (not reversed))
  699. ;; Put it at the end, one level below level 1
  700. (save-restriction
  701. (widen)
  702. (goto-char (point-max))
  703. (if (not (bolp)) (newline))
  704. (bookmark-set "org-remember-last-stored")
  705. (org-paste-subtree (org-get-valid-level 1 1) txt)))
  706. ((and (bobp) reversed)
  707. ;; Put it at the start, as level 1
  708. (save-restriction
  709. (widen)
  710. (goto-char (point-min))
  711. (re-search-forward "^\\*+ " nil t)
  712. (beginning-of-line 1)
  713. (bookmark-set "org-remember-last-stored")
  714. (org-paste-subtree 1 txt)))
  715. (t
  716. ;; Put it right there, with automatic level determined by
  717. ;; org-paste-subtree or from prefix arg
  718. (bookmark-set "org-remember-last-stored")
  719. (org-paste-subtree
  720. (if (numberp current-prefix-arg) current-prefix-arg)
  721. txt)))
  722. (when remember-save-after-remembering
  723. (save-buffer)
  724. (if (and (not visiting)
  725. (not (equal (marker-buffer org-clock-marker)
  726. (current-buffer))))
  727. (kill-buffer (current-buffer)))))))))
  728. t) ;; return t to indicate that we took care of this note.
  729. (defun org-do-remember (&optional initial)
  730. "Call remember."
  731. (remember initial))
  732. (provide 'org-remember)
  733. ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
  734. ;;; org-remember.el ends here