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