org-remember.el 39 KB

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