org-remember.el 31 KB

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