org-remember.el 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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: 5.24
  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. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  161. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  162. ;;;###autoload
  163. (defun org-remember-insinuate ()
  164. "Setup remember.el for use wiht Org-mode."
  165. (require 'remember)
  166. (setq remember-annotation-functions '(org-remember-annotation))
  167. (setq remember-handler-functions '(org-remember-handler))
  168. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  169. ;;;###autoload
  170. (defun org-remember-annotation ()
  171. "Return a link to the current location as an annotation for remember.el.
  172. If you are using Org-mode files as target for data storage with
  173. remember.el, then the annotations should include a link compatible with the
  174. conventions in Org-mode. This function returns such a link."
  175. (org-store-link nil))
  176. (defconst org-remember-help
  177. "Select a destination location for the note.
  178. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  179. RET on headline -> Store as sublevel entry to current headline
  180. RET at beg-of-buf -> Append to file as level 2 headline
  181. <left>/<right> -> before/after current headline, same headings level")
  182. (defvar org-remember-previous-location nil)
  183. (defvar org-force-remember-template-char) ;; dynamically scoped
  184. ;; Save the major mode of the buffer we called remember from
  185. (defvar org-select-template-temp-major-mode nil)
  186. ;; Temporary store the buffer where remember was called from
  187. (defvar org-select-template-original-buffer nil)
  188. (defun org-select-remember-template (&optional use-char)
  189. (when org-remember-templates
  190. (let* ((pre-selected-templates
  191. (mapcar
  192. (lambda (tpl)
  193. (let ((ctxt (nth 5 tpl))
  194. (mode org-select-template-temp-major-mode)
  195. (buf org-select-template-original-buffer))
  196. (and (or (not ctxt) (eq ctxt t)
  197. (and (listp ctxt) (memq mode ctxt))
  198. (and (functionp ctxt)
  199. (with-current-buffer buf
  200. ;; Protect the user-defined function from error
  201. (condition-case nil (funcall ctxt) (error nil)))))
  202. tpl)))
  203. org-remember-templates))
  204. ;; If no template at this point, add the default templates:
  205. (pre-selected-templates1
  206. (if (not (delq nil pre-selected-templates))
  207. (mapcar (lambda(x) (if (not (nth 5 x)) x))
  208. org-remember-templates)
  209. pre-selected-templates))
  210. ;; Then unconditionnally add template for any contexts
  211. (pre-selected-templates2
  212. (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
  213. org-remember-templates)
  214. (delq nil pre-selected-templates1)))
  215. (templates (mapcar (lambda (x)
  216. (if (stringp (car x))
  217. (append (list (nth 1 x) (car x)) (cddr x))
  218. (append (list (car x) "") (cdr x))))
  219. (delq nil pre-selected-templates2)))
  220. (char (or use-char
  221. (cond
  222. ((= (length templates) 1)
  223. (caar templates))
  224. ((and (boundp 'org-force-remember-template-char)
  225. org-force-remember-template-char)
  226. (if (stringp org-force-remember-template-char)
  227. (string-to-char org-force-remember-template-char)
  228. org-force-remember-template-char))
  229. (t
  230. (message "Select template: %s"
  231. (mapconcat
  232. (lambda (x)
  233. (cond
  234. ((not (string-match "\\S-" (nth 1 x)))
  235. (format "[%c]" (car x)))
  236. ((equal (downcase (car x))
  237. (downcase (aref (nth 1 x) 0)))
  238. (format "[%c]%s" (car x)
  239. (substring (nth 1 x) 1)))
  240. (t (format "[%c]%s" (car x) (nth 1 x)))))
  241. templates " "))
  242. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  243. (when (equal char0 ?\C-g)
  244. (jump-to-register remember-register)
  245. (kill-buffer remember-buffer))
  246. char0))))))
  247. (cddr (assoc char templates)))))
  248. (defvar x-last-selected-text)
  249. (defvar x-last-selected-text-primary)
  250. ;;;###autoload
  251. (defun org-remember-apply-template (&optional use-char skip-interactive)
  252. "Initialize *remember* buffer with template, invoke `org-mode'.
  253. This function should be placed into `remember-mode-hook' and in fact requires
  254. to be run from that hook to function properly."
  255. (if org-remember-templates
  256. (let* ((entry (org-select-remember-template use-char))
  257. (tpl (car entry))
  258. (plist-p (if org-store-link-plist t nil))
  259. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  260. (string-match "\\S-" (nth 1 entry)))
  261. (nth 1 entry)
  262. org-default-notes-file))
  263. (headline (nth 2 entry))
  264. (v-c (or (and (eq window-system 'x)
  265. (fboundp 'x-cut-buffer-or-selection-value)
  266. (x-cut-buffer-or-selection-value))
  267. (org-bound-and-true-p x-last-selected-text)
  268. (org-bound-and-true-p x-last-selected-text-primary)
  269. (and (> (length kill-ring) 0) (current-kill 0))))
  270. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  271. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  272. (v-u (concat "[" (substring v-t 1 -1) "]"))
  273. (v-U (concat "[" (substring v-T 1 -1) "]"))
  274. ;; `initial' and `annotation' are bound in `remember'
  275. (v-i (if (boundp 'initial) initial))
  276. (v-a (if (and (boundp 'annotation) annotation)
  277. (if (equal annotation "[[]]") "" annotation)
  278. ""))
  279. (v-A (if (and v-a
  280. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  281. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  282. v-a))
  283. (v-n user-full-name)
  284. (org-startup-folded nil)
  285. org-time-was-given org-end-time-was-given x
  286. prompt completions char time pos default histvar)
  287. (when (and file (not (file-name-absolute-p file)))
  288. (setq file (expand-file-name file org-directory)))
  289. (setq org-store-link-plist
  290. (append (list :annotation v-a :initial v-i)
  291. org-store-link-plist))
  292. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  293. (erase-buffer)
  294. (insert (substitute-command-keys
  295. (format
  296. "## Filing location: Select interactively, default, or last used:
  297. ## %s to select file and header location interactively.
  298. ## %s \"%s\" -> \"* %s\"
  299. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  300. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  301. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  302. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  303. (abbreviate-file-name (or file org-default-notes-file))
  304. (or headline "")
  305. (or (car org-remember-previous-location) "???")
  306. (or (cdr org-remember-previous-location) "???"))))
  307. (insert tpl) (goto-char (point-min))
  308. ;; Simple %-escapes
  309. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  310. (when (and initial (equal (match-string 0) "%i"))
  311. (save-match-data
  312. (let* ((lead (buffer-substring
  313. (point-at-bol) (match-beginning 0))))
  314. (setq v-i (mapconcat 'identity
  315. (org-split-string initial "\n")
  316. (concat "\n" lead))))))
  317. (replace-match
  318. (or (eval (intern (concat "v-" (match-string 1)))) "")
  319. t t))
  320. ;; %[] Insert contents of a file.
  321. (goto-char (point-min))
  322. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  323. (let ((start (match-beginning 0))
  324. (end (match-end 0))
  325. (filename (expand-file-name (match-string 1))))
  326. (goto-char start)
  327. (delete-region start end)
  328. (condition-case error
  329. (insert-file-contents filename)
  330. (error (insert (format "%%![Couldn't insert %s: %s]"
  331. filename error))))))
  332. ;; %() embedded elisp
  333. (goto-char (point-min))
  334. (while (re-search-forward "%\\((.+)\\)" nil t)
  335. (goto-char (match-beginning 0))
  336. (let ((template-start (point)))
  337. (forward-char 1)
  338. (let ((result
  339. (condition-case error
  340. (eval (read (current-buffer)))
  341. (error (format "%%![Error: %s]" error)))))
  342. (delete-region template-start (point))
  343. (insert result))))
  344. ;; From the property list
  345. (when plist-p
  346. (goto-char (point-min))
  347. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  348. (and (setq x (or (plist-get org-store-link-plist
  349. (intern (match-string 1))) ""))
  350. (replace-match x t t))))
  351. ;; Turn on org-mode in the remember buffer, set local variables
  352. (org-mode)
  353. (org-set-local 'org-finish-function 'org-remember-finalize)
  354. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  355. (org-set-local 'org-default-notes-file file))
  356. (if (and headline (stringp headline) (string-match "\\S-" headline))
  357. (org-set-local 'org-remember-default-headline headline))
  358. ;; Interactive template entries
  359. (goto-char (point-min))
  360. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  361. (setq char (if (match-end 3) (match-string 3))
  362. prompt (if (match-end 2) (match-string 2)))
  363. (goto-char (match-beginning 0))
  364. (replace-match "")
  365. (setq completions nil default nil)
  366. (when prompt
  367. (setq completions (org-split-string prompt "|")
  368. prompt (pop completions)
  369. default (car completions)
  370. histvar (intern (concat
  371. "org-remember-template-prompt-history::"
  372. (or prompt "")))
  373. completions (mapcar 'list completions)))
  374. (cond
  375. ((member char '("G" "g"))
  376. (let* ((org-last-tags-completion-table
  377. (org-global-tags-completion-table
  378. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  379. (org-add-colon-after-tag-completion t)
  380. (ins (completing-read
  381. (if prompt (concat prompt ": ") "Tags: ")
  382. 'org-tags-completion-function nil nil nil
  383. 'org-tags-history)))
  384. (setq ins (mapconcat 'identity
  385. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  386. ":"))
  387. (when (string-match "\\S-" ins)
  388. (or (equal (char-before) ?:) (insert ":"))
  389. (insert ins)
  390. (or (equal (char-after) ?:) (insert ":")))))
  391. (char
  392. (setq org-time-was-given (equal (upcase char) char))
  393. (setq time (org-read-date (equal (upcase char) "U") t nil
  394. prompt))
  395. (org-insert-time-stamp time org-time-was-given
  396. (member char '("u" "U"))
  397. nil nil (list org-end-time-was-given)))
  398. (t
  399. (insert (org-completing-read
  400. (concat (if prompt prompt "Enter string")
  401. (if default (concat " [" default "]"))
  402. ": ")
  403. completions nil nil nil histvar default)))))
  404. (goto-char (point-min))
  405. (if (re-search-forward "%\\?" nil t)
  406. (replace-match "")
  407. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  408. (org-mode)
  409. (org-set-local 'org-finish-function 'org-remember-finalize))
  410. (when (save-excursion
  411. (goto-char (point-min))
  412. (re-search-forward "%!" nil t))
  413. (replace-match "")
  414. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  415. (defun org-remember-finish-immediately ()
  416. "File remember note immediately.
  417. This should be run in `post-command-hook' and will remove itself
  418. from that hook."
  419. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  420. (when org-finish-function
  421. (funcall org-finish-function)))
  422. (defvar org-clock-marker) ; Defined below
  423. (defun org-remember-finalize ()
  424. "Finalize the remember process."
  425. (unless (fboundp 'remember-finalize)
  426. (defalias 'remember-finalize 'remember-buffer))
  427. (when (and org-clock-marker
  428. (equal (marker-buffer org-clock-marker) (current-buffer)))
  429. ;; FIXME: test this, this is w/o notetaking!
  430. (let (org-log-note-clock-out) (org-clock-out)))
  431. (when buffer-file-name
  432. (save-buffer)
  433. (setq buffer-file-name nil))
  434. (remember-finalize))
  435. ;;;###autoload
  436. (defun org-remember (&optional goto org-force-remember-template-char)
  437. "Call `remember'. If this is already a remember buffer, re-apply template.
  438. If there is an active region, make sure remember uses it as initial content
  439. of the remember buffer.
  440. When called interactively with a `C-u' prefix argument GOTO, don't remember
  441. anything, just go to the file/headline where the selected template usually
  442. stores its notes. With a double prefix arg `C-u C-u', go to the last
  443. note stored by remember.
  444. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  445. associated with a template in `org-remember-templates'."
  446. (interactive "P")
  447. (cond
  448. ((equal goto '(4)) (org-go-to-remember-target))
  449. ((equal goto '(16)) (org-remember-goto-last-stored))
  450. (t
  451. ;; set temporary variables that will be needed in
  452. ;; `org-select-remember-template'
  453. (setq org-select-template-temp-major-mode major-mode)
  454. (setq org-select-template-original-buffer (current-buffer))
  455. (if (eq org-finish-function 'org-remember-finalize)
  456. (progn
  457. (when (< (length org-remember-templates) 2)
  458. (error "No other template available"))
  459. (erase-buffer)
  460. (let ((annotation (plist-get org-store-link-plist :annotation))
  461. (initial (plist-get org-store-link-plist :initial)))
  462. (org-remember-apply-template))
  463. (message "Press C-c C-c to remember data"))
  464. (if (org-region-active-p)
  465. (org-do-remember (buffer-substring (point) (mark)))
  466. (org-do-remember))))))
  467. (defun org-remember-goto-last-stored ()
  468. "Go to the location where the last remember note was stored."
  469. (interactive)
  470. (bookmark-jump "org-remember-last-stored")
  471. (message "This is the last note stored by remember"))
  472. (defun org-go-to-remember-target (&optional template-key)
  473. "Go to the target location of a remember template.
  474. The user is queried for the template."
  475. (interactive)
  476. (let* (org-select-template-temp-major-mode
  477. (entry (org-select-remember-template template-key))
  478. (file (nth 1 entry))
  479. (heading (nth 2 entry))
  480. visiting)
  481. (unless (and file (stringp file) (string-match "\\S-" file))
  482. (setq file org-default-notes-file))
  483. (when (and file (not (file-name-absolute-p file)))
  484. (setq file (expand-file-name file org-directory)))
  485. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  486. (setq heading org-remember-default-headline))
  487. (setq visiting (org-find-base-buffer-visiting file))
  488. (if (not visiting) (find-file-noselect file))
  489. (switch-to-buffer (or visiting (get-file-buffer file)))
  490. (widen)
  491. (goto-char (point-min))
  492. (if (re-search-forward
  493. (concat "^\\*+[ \t]+" (regexp-quote heading)
  494. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  495. nil t)
  496. (goto-char (match-beginning 0))
  497. (error "Target headline not found: %s" heading))))
  498. ;;;###autoload
  499. (defun org-remember-handler ()
  500. "Store stuff from remember.el into an org file.
  501. First prompts for an org file. If the user just presses return, the value
  502. of `org-default-notes-file' is used.
  503. Then the command offers the headings tree of the selected file in order to
  504. file the text at a specific location.
  505. You can either immediately press RET to get the note appended to the
  506. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  507. find a better place. Then press RET or <left> or <right> in insert the note.
  508. Key Cursor position Note gets inserted
  509. -----------------------------------------------------------------------------
  510. RET buffer-start as level 1 heading at end of file
  511. RET on headline as sublevel of the heading at cursor
  512. RET no heading at cursor position, level taken from context.
  513. Or use prefix arg to specify level manually.
  514. <left> on headline as same level, before current heading
  515. <right> on headline as same level, after current heading
  516. So the fastest way to store the note is to press RET RET to append it to
  517. the default file. This way your current train of thought is not
  518. interrupted, in accordance with the principles of remember.el.
  519. You can also get the fast execution without prompting by using
  520. C-u C-c C-c to exit the remember buffer. See also the variable
  521. `org-remember-store-without-prompt'.
  522. Before being stored away, the function ensures that the text has a
  523. headline, i.e. a first line that starts with a \"*\". If not, a headline
  524. is constructed from the current date and some additional data.
  525. If the variable `org-adapt-indentation' is non-nil, the entire text is
  526. also indented so that it starts in the same column as the headline
  527. \(i.e. after the stars).
  528. See also the variable `org-reverse-note-order'."
  529. (goto-char (point-min))
  530. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  531. (replace-match ""))
  532. (goto-char (point-max))
  533. (beginning-of-line 1)
  534. (while (looking-at "[ \t]*$\\|##.*")
  535. (delete-region (1- (point)) (point-max))
  536. (beginning-of-line 1))
  537. (catch 'quit
  538. (if org-note-abort (throw 'quit nil))
  539. (let* ((txt (buffer-substring (point-min) (point-max)))
  540. (fastp (org-xor (equal current-prefix-arg '(4))
  541. org-remember-store-without-prompt))
  542. (file (cond
  543. (fastp org-default-notes-file)
  544. ((and (eq org-remember-interactive-interface 'refile)
  545. org-refile-targets)
  546. org-default-notes-file)
  547. ((not (and (equal current-prefix-arg '(16))
  548. org-remember-previous-location))
  549. (org-get-org-file))))
  550. (heading org-remember-default-headline)
  551. (visiting (and file (org-find-base-buffer-visiting file)))
  552. (org-startup-folded nil)
  553. (org-startup-align-all-tables nil)
  554. (org-goto-start-pos 1)
  555. spos exitcmd level indent reversed)
  556. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  557. (setq file (car org-remember-previous-location)
  558. heading (cdr org-remember-previous-location)
  559. fastp t))
  560. (setq current-prefix-arg nil)
  561. (if (string-match "[ \t\n]+\\'" txt)
  562. (setq txt (replace-match "" t t txt)))
  563. ;; Modify text so that it becomes a nice subtree which can be inserted
  564. ;; into an org tree.
  565. (let* ((lines (split-string txt "\n"))
  566. first)
  567. (setq first (car lines) lines (cdr lines))
  568. (if (string-match "^\\*+ " first)
  569. ;; Is already a headline
  570. (setq indent nil)
  571. ;; We need to add a headline: Use time and first buffer line
  572. (setq lines (cons first lines)
  573. first (concat "* " (current-time-string)
  574. " (" (remember-buffer-desc) ")")
  575. indent " "))
  576. (if (and org-adapt-indentation indent)
  577. (setq lines (mapcar
  578. (lambda (x)
  579. (if (string-match "\\S-" x)
  580. (concat indent x) x))
  581. lines)))
  582. (setq txt (concat first "\n"
  583. (mapconcat 'identity lines "\n"))))
  584. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  585. (setq txt (replace-match "\n\n" t t txt))
  586. (if (string-match "[ \t\n]*\\'" txt)
  587. (setq txt (replace-match "\n" t t txt))))
  588. ;; Put the modified text back into the remember buffer, for refile.
  589. (erase-buffer)
  590. (insert txt)
  591. (goto-char (point-min))
  592. (when (and (eq org-remember-interactive-interface 'refile)
  593. (not fastp))
  594. (org-refile nil (or visiting (find-file-noselect file)))
  595. (throw 'quit t))
  596. ;; Find the file
  597. (if (not visiting) (find-file-noselect file))
  598. (with-current-buffer (or visiting (get-file-buffer file))
  599. (unless (org-mode-p)
  600. (error "Target files for remember notes must be in Org-mode"))
  601. (save-excursion
  602. (save-restriction
  603. (widen)
  604. (and (goto-char (point-min))
  605. (not (re-search-forward "^\\* " nil t))
  606. (insert "\n* " (or heading "Notes") "\n"))
  607. (setq reversed (org-notes-order-reversed-p))
  608. ;; Find the default location
  609. (when (and heading (stringp heading) (string-match "\\S-" heading))
  610. (goto-char (point-min))
  611. (if (re-search-forward
  612. (concat "^\\*+[ \t]+" (regexp-quote heading)
  613. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  614. nil t)
  615. (setq org-goto-start-pos (match-beginning 0))
  616. (when fastp
  617. (goto-char (point-max))
  618. (unless (bolp) (newline))
  619. (insert "* " heading "\n")
  620. (setq org-goto-start-pos (point-at-bol 0)))))
  621. ;; Ask the User for a location, using the appropriate interface
  622. (cond
  623. (fastp (setq spos org-goto-start-pos
  624. exitcmd 'return))
  625. ((eq org-remember-interactive-interface 'outline)
  626. (setq spos (org-get-location (current-buffer)
  627. org-remember-help)
  628. exitcmd (cdr spos)
  629. spos (car spos)))
  630. ((eq org-remember-interactive-interface 'outline-path-completion)
  631. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  632. (org-refile-use-outline-path t))
  633. (setq spos (org-refile-get-location "Heading: ")
  634. exitcmd 'return
  635. spos (nth 3 spos))))
  636. (t (error "this should not hapen")))
  637. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  638. ; not handle this note
  639. (goto-char spos)
  640. (cond ((org-on-heading-p t)
  641. (org-back-to-heading t)
  642. (setq level (funcall outline-level))
  643. (cond
  644. ((eq exitcmd 'return)
  645. ;; sublevel of current
  646. (setq org-remember-previous-location
  647. (cons (abbreviate-file-name file)
  648. (org-get-heading 'notags)))
  649. (if reversed
  650. (outline-next-heading)
  651. (org-end-of-subtree t)
  652. (if (not (bolp))
  653. (if (looking-at "[ \t]*\n")
  654. (beginning-of-line 2)
  655. (end-of-line 1)
  656. (insert "\n"))))
  657. (bookmark-set "org-remember-last-stored")
  658. (org-paste-subtree (org-get-valid-level level 1) txt))
  659. ((eq exitcmd 'left)
  660. ;; before current
  661. (bookmark-set "org-remember-last-stored")
  662. (org-paste-subtree level txt))
  663. ((eq exitcmd 'right)
  664. ;; after current
  665. (org-end-of-subtree t)
  666. (bookmark-set "org-remember-last-stored")
  667. (org-paste-subtree level txt))
  668. (t (error "This should not happen"))))
  669. ((and (bobp) (not reversed))
  670. ;; Put it at the end, one level below level 1
  671. (save-restriction
  672. (widen)
  673. (goto-char (point-max))
  674. (if (not (bolp)) (newline))
  675. (bookmark-set "org-remember-last-stored")
  676. (org-paste-subtree (org-get-valid-level 1 1) txt)))
  677. ((and (bobp) reversed)
  678. ;; Put it at the start, as level 1
  679. (save-restriction
  680. (widen)
  681. (goto-char (point-min))
  682. (re-search-forward "^\\*+ " nil t)
  683. (beginning-of-line 1)
  684. (bookmark-set "org-remember-last-stored")
  685. (org-paste-subtree 1 txt)))
  686. (t
  687. ;; Put it right there, with automatic level determined by
  688. ;; org-paste-subtree or from prefix arg
  689. (bookmark-set "org-remember-last-stored")
  690. (org-paste-subtree
  691. (if (numberp current-prefix-arg) current-prefix-arg)
  692. txt)))
  693. (when remember-save-after-remembering
  694. (save-buffer)
  695. (if (not visiting) (kill-buffer (current-buffer)))))))))
  696. t) ;; return t to indicate that we took care of this note.
  697. (defun org-do-remember (&optional initial)
  698. "Call remember."
  699. (remember initial))
  700. (provide 'org-remember)
  701. ;;; org-remember.el ends here