org-remember.el 34 KB

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