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