org-remember.el 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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.27
  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 "At beginning of file" top)
  169. (const :tag "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. (defcustom org-remember-backup-directory nil
  205. "Directory where to store all remember buffers, for backup purposes.
  206. After a remember buffer has been stored successfully, the backup file
  207. will be removed. However, if you forget to finish the remember process,
  208. the file will remain there.
  209. See also `org-remember-auto-remove-backup-files'."
  210. :group 'org-remember
  211. :type '(choice
  212. (const :tag "No backups" nil)
  213. (directory :tag "Directory")))
  214. (defcustom org-remember-auto-remove-backup-files t
  215. "Non-nil means, remove remember backup files after successfully storage.
  216. When remember is finished successfully, with storing the note at the
  217. desired target, remove the backup files related to this remember process
  218. and show a message about remaining backup files, from previous, unfinished
  219. remember sessions.
  220. Backup files will only be made at all, when `org-remember-backup-directory'
  221. is set."
  222. :group 'org-remember
  223. :type 'boolean)
  224. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  225. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  226. ;;;###autoload
  227. (defun org-remember-insinuate ()
  228. "Setup remember.el for use with Org-mode."
  229. (org-require-remember)
  230. (setq remember-annotation-functions '(org-remember-annotation))
  231. (setq remember-handler-functions '(org-remember-handler))
  232. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  233. ;;;###autoload
  234. (defun org-remember-annotation ()
  235. "Return a link to the current location as an annotation for remember.el.
  236. If you are using Org-mode files as target for data storage with
  237. remember.el, then the annotations should include a link compatible with the
  238. conventions in Org-mode. This function returns such a link."
  239. (org-store-link nil))
  240. (defconst org-remember-help
  241. "Select a destination location for the note.
  242. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  243. RET on headline -> Store as sublevel entry to current headline
  244. RET at beg-of-buf -> Append to file as level 2 headline
  245. <left>/<right> -> before/after current headline, same headings level")
  246. (defvar org-jump-to-target-location nil)
  247. (defvar org-remember-previous-location nil)
  248. (defvar org-force-remember-template-char) ;; dynamically scoped
  249. ;; Save the major mode of the buffer we called remember from
  250. (defvar org-select-template-temp-major-mode nil)
  251. ;; Temporary store the buffer where remember was called from
  252. (defvar org-select-template-original-buffer nil)
  253. (defun org-select-remember-template (&optional use-char)
  254. (when org-remember-templates
  255. (let* ((pre-selected-templates
  256. (mapcar
  257. (lambda (tpl)
  258. (let ((ctxt (nth 5 tpl))
  259. (mode org-select-template-temp-major-mode)
  260. (buf org-select-template-original-buffer))
  261. (and (or (not ctxt) (eq ctxt t)
  262. (and (listp ctxt) (memq mode ctxt))
  263. (and (functionp ctxt)
  264. (with-current-buffer buf
  265. ;; Protect the user-defined function from error
  266. (condition-case nil (funcall ctxt) (error nil)))))
  267. tpl)))
  268. org-remember-templates))
  269. ;; If no template at this point, add the default templates:
  270. (pre-selected-templates1
  271. (if (not (delq nil pre-selected-templates))
  272. (mapcar (lambda(x) (if (not (nth 5 x)) x))
  273. org-remember-templates)
  274. pre-selected-templates))
  275. ;; Then unconditionally add template for any contexts
  276. (pre-selected-templates2
  277. (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
  278. org-remember-templates)
  279. (delq nil pre-selected-templates1)))
  280. (templates (mapcar (lambda (x)
  281. (if (stringp (car x))
  282. (append (list (nth 1 x) (car x)) (cddr x))
  283. (append (list (car x) "") (cdr x))))
  284. (delq nil pre-selected-templates2)))
  285. msg
  286. (char (or use-char
  287. (cond
  288. ((= (length templates) 1)
  289. (caar templates))
  290. ((and (boundp 'org-force-remember-template-char)
  291. org-force-remember-template-char)
  292. (if (stringp org-force-remember-template-char)
  293. (string-to-char org-force-remember-template-char)
  294. org-force-remember-template-char))
  295. (t
  296. (setq msg (format
  297. "Select template: %s"
  298. (mapconcat
  299. (lambda (x)
  300. (cond
  301. ((not (string-match "\\S-" (nth 1 x)))
  302. (format "[%c]" (car x)))
  303. ((equal (downcase (car x))
  304. (downcase (aref (nth 1 x) 0)))
  305. (format "[%c]%s" (car x)
  306. (substring (nth 1 x) 1)))
  307. (t (format "[%c]%s" (car x) (nth 1 x)))))
  308. templates " ")))
  309. (let ((inhibit-quit t) char0)
  310. (while (not char0)
  311. (message msg)
  312. (setq char0 (read-char-exclusive))
  313. (when (and (not (assoc char0 templates))
  314. (not (equal char0 ?\C-g)))
  315. (message "No suche template \"%c\"" char0)
  316. (ding) (sit-for 1)
  317. (setq char0 nil)))
  318. (when (equal char0 ?\C-g)
  319. (jump-to-register remember-register)
  320. (kill-buffer remember-buffer)
  321. (error "Abort"))
  322. char0))))))
  323. (cddr (assoc char templates)))))
  324. (defun org-get-x-clipboard (value)
  325. "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
  326. (if (eq window-system 'x)
  327. (let ((x (org-get-x-clipboard-compat value)))
  328. (if x (org-no-properties x)))))
  329. ;;;###autoload
  330. (defun org-remember-apply-template (&optional use-char skip-interactive)
  331. "Initialize *remember* buffer with template, invoke `org-mode'.
  332. This function should be placed into `remember-mode-hook' and in fact requires
  333. to be run from that hook to function properly."
  334. (when (and (boundp 'initial) (stringp initial))
  335. (setq initial (org-no-properties initial))
  336. (remove-text-properties 0 (length initial) '(read-only t) initial))
  337. (if org-remember-templates
  338. (let* ((entry (org-select-remember-template use-char))
  339. (ct (or org-overriding-default-time (org-current-time)))
  340. (dct (decode-time ct))
  341. (ct1
  342. (if (< (nth 2 dct) org-extend-today-until)
  343. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  344. ct))
  345. (tpl (car entry))
  346. (plist-p (if org-store-link-plist t nil))
  347. (file (if (and (nth 1 entry)
  348. (or (and (stringp (nth 1 entry))
  349. (string-match "\\S-" (nth 1 entry)))
  350. (functionp (nth 1 entry))))
  351. (nth 1 entry)
  352. org-default-notes-file))
  353. (headline (nth 2 entry))
  354. (v-c (and (> (length kill-ring) 0) (current-kill 0)))
  355. (v-x (or (org-get-x-clipboard 'PRIMARY)
  356. (org-get-x-clipboard 'CLIPBOARD)
  357. (org-get-x-clipboard 'SECONDARY)))
  358. (v-t (format-time-string (car org-time-stamp-formats) ct))
  359. (v-T (format-time-string (cdr org-time-stamp-formats) ct))
  360. (v-u (concat "[" (substring v-t 1 -1) "]"))
  361. (v-U (concat "[" (substring v-T 1 -1) "]"))
  362. ;; `initial' and `annotation' are bound in `remember'.
  363. ;; But if the property list has them, we prefer those values
  364. (v-i (or (plist-get org-store-link-plist :initial)
  365. (and (boundp 'initial) initial)
  366. ""))
  367. (v-a (or (plist-get org-store-link-plist :annotation)
  368. (and (boundp 'annotation) annotation)
  369. ""))
  370. ;; Is the link empty? Then we do not want it...
  371. (v-a (if (equal v-a "[[]]") "" v-a))
  372. (clipboards (remove nil (list v-i
  373. (org-get-x-clipboard 'PRIMARY)
  374. (org-get-x-clipboard 'CLIPBOARD)
  375. (org-get-x-clipboard 'SECONDARY)
  376. v-c)))
  377. (v-A (if (and v-a
  378. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  379. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  380. v-a))
  381. (v-n user-full-name)
  382. (v-k (if (marker-buffer org-clock-marker)
  383. (org-substring-no-properties org-clock-heading)))
  384. (v-K (if (marker-buffer org-clock-marker)
  385. (org-make-link-string
  386. (buffer-file-name (marker-buffer org-clock-marker))
  387. org-clock-heading)))
  388. v-I
  389. (org-startup-folded nil)
  390. (org-inhibit-startup t)
  391. org-time-was-given org-end-time-was-given x
  392. prompt completions char time pos default histvar)
  393. (when (functionp file)
  394. (setq file (funcall file)))
  395. (when (and file (not (file-name-absolute-p file)))
  396. (setq file (expand-file-name file org-directory)))
  397. (setq org-store-link-plist
  398. (plist-put org-store-link-plist :annotation v-a)
  399. org-store-link-plist
  400. (plist-put org-store-link-plist :initial v-i))
  401. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  402. (erase-buffer)
  403. (insert (substitute-command-keys
  404. (format
  405. "## %s \"%s\" -> \"* %s\"
  406. ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
  407. ## C-0 C-c C-c \"%s\" -> \"* %s\"
  408. ## %s to select file and header location interactively.
  409. ## C-2 C-c C-c as child of the currently clocked item
  410. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  411. (if org-remember-store-without-prompt " C-c C-c" " C-1 C-c C-c")
  412. (abbreviate-file-name (or file org-default-notes-file))
  413. (or headline "")
  414. (or (car org-remember-previous-location) "???")
  415. (or (cdr org-remember-previous-location) "???")
  416. (if org-remember-store-without-prompt "C-1 C-c C-c" " C-c C-c"))))
  417. (insert tpl)
  418. (goto-char (point-min))
  419. ;; Simple %-escapes
  420. (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
  421. (when (and initial (equal (match-string 0) "%i"))
  422. (save-match-data
  423. (let* ((lead (buffer-substring
  424. (point-at-bol) (match-beginning 0))))
  425. (setq v-i (mapconcat 'identity
  426. (org-split-string initial "\n")
  427. (concat "\n" lead))))))
  428. (replace-match
  429. (or (eval (intern (concat "v-" (match-string 1)))) "")
  430. t t))
  431. ;; %[] Insert contents of a file.
  432. (goto-char (point-min))
  433. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  434. (let ((start (match-beginning 0))
  435. (end (match-end 0))
  436. (filename (expand-file-name (match-string 1))))
  437. (goto-char start)
  438. (delete-region start end)
  439. (condition-case error
  440. (insert-file-contents filename)
  441. (error (insert (format "%%![Couldn't insert %s: %s]"
  442. filename error))))))
  443. ;; %() embedded elisp
  444. (goto-char (point-min))
  445. (while (re-search-forward "%\\((.+)\\)" nil t)
  446. (goto-char (match-beginning 0))
  447. (let ((template-start (point)))
  448. (forward-char 1)
  449. (let ((result
  450. (condition-case error
  451. (eval (read (current-buffer)))
  452. (error (format "%%![Error: %s]" error)))))
  453. (delete-region template-start (point))
  454. (insert result))))
  455. ;; From the property list
  456. (when plist-p
  457. (goto-char (point-min))
  458. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  459. (and (setq x (or (plist-get org-store-link-plist
  460. (intern (match-string 1))) ""))
  461. (replace-match x t t))))
  462. ;; Turn on org-mode in the remember buffer, set local variables
  463. (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1))
  464. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  465. (org-set-local 'org-default-notes-file file))
  466. (if headline
  467. (org-set-local 'org-remember-default-headline headline))
  468. ;; Interactive template entries
  469. (goto-char (point-min))
  470. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
  471. (setq char (if (match-end 3) (match-string 3))
  472. prompt (if (match-end 2) (match-string 2)))
  473. (goto-char (match-beginning 0))
  474. (replace-match "")
  475. (setq completions nil default nil)
  476. (when prompt
  477. (setq completions (org-split-string prompt "|")
  478. prompt (pop completions)
  479. default (car completions)
  480. histvar (intern (concat
  481. "org-remember-template-prompt-history::"
  482. (or prompt "")))
  483. completions (mapcar 'list completions)))
  484. (cond
  485. ((member char '("G" "g"))
  486. (let* ((org-last-tags-completion-table
  487. (org-global-tags-completion-table
  488. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  489. (org-add-colon-after-tag-completion t)
  490. (ins (org-ido-completing-read
  491. (if prompt (concat prompt ": ") "Tags: ")
  492. 'org-tags-completion-function nil nil nil
  493. 'org-tags-history)))
  494. (setq ins (mapconcat 'identity
  495. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  496. ":"))
  497. (when (string-match "\\S-" ins)
  498. (or (equal (char-before) ?:) (insert ":"))
  499. (insert ins)
  500. (or (equal (char-after) ?:) (insert ":")))))
  501. ((equal char "C")
  502. (cond ((= (length clipboards) 1) (insert (car clipboards)))
  503. ((> (length clipboards) 1)
  504. (insert (read-string "Clipboard/kill value: "
  505. (car clipboards) '(clipboards . 1)
  506. (car clipboards))))))
  507. ((equal char "L")
  508. (cond ((= (length clipboards) 1)
  509. (org-insert-link 0 (car clipboards)))
  510. ((> (length clipboards) 1)
  511. (org-insert-link 0 (read-string "Clipboard/kill value: "
  512. (car clipboards)
  513. '(clipboards . 1)
  514. (car clipboards))))))
  515. ((equal char "p")
  516. (let*
  517. ((prop (org-substring-no-properties prompt))
  518. (pall (concat prop "_ALL"))
  519. (allowed
  520. (with-current-buffer
  521. (get-buffer (file-name-nondirectory file))
  522. (or (cdr (assoc pall org-file-properties))
  523. (cdr (assoc pall org-global-properties))
  524. (cdr (assoc pall org-global-properties-fixed)))))
  525. (existing (with-current-buffer
  526. (get-buffer (file-name-nondirectory file))
  527. (mapcar 'list (org-property-values prop))))
  528. (propprompt (concat "Value for " prop ": "))
  529. (val (if allowed
  530. (org-completing-read
  531. propprompt
  532. (mapcar 'list (org-split-string allowed "[ \t]+"))
  533. nil 'req-match)
  534. (org-completing-read-no-ido propprompt existing nil nil
  535. "" nil ""))))
  536. (org-set-property prop val)))
  537. (char
  538. ;; These are the date/time related ones
  539. (setq org-time-was-given (equal (upcase char) char))
  540. (setq time (org-read-date (equal (upcase char) "U") t nil
  541. prompt))
  542. (org-insert-time-stamp time org-time-was-given
  543. (member char '("u" "U"))
  544. nil nil (list org-end-time-was-given)))
  545. (t
  546. (let (org-completion-use-ido)
  547. (insert (org-completing-read-no-ido
  548. (concat (if prompt prompt "Enter string")
  549. (if default (concat " [" default "]"))
  550. ": ")
  551. completions nil nil nil histvar default))))))
  552. (goto-char (point-min))
  553. (if (re-search-forward "%\\?" nil t)
  554. (replace-match "")
  555. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  556. (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1)))
  557. (when (save-excursion
  558. (goto-char (point-min))
  559. (re-search-forward "%&" nil t))
  560. (replace-match "")
  561. (org-set-local 'org-jump-to-target-location t))
  562. (when org-remember-backup-directory
  563. (unless (file-directory-p org-remember-backup-directory)
  564. (make-directory org-remember-backup-directory))
  565. (org-set-local 'auto-save-file-name-transforms nil)
  566. (setq buffer-file-name
  567. (expand-file-name
  568. (format-time-string "remember-%Y-%m-%d-%H-%M-%S")
  569. org-remember-backup-directory))
  570. (save-buffer)
  571. (org-set-local 'auto-save-visited-file-name t)
  572. (auto-save-mode 1))
  573. (when (save-excursion
  574. (goto-char (point-min))
  575. (re-search-forward "%!" nil t))
  576. (replace-match "")
  577. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  578. (defun org-remember-finish-immediately ()
  579. "File remember note immediately.
  580. This should be run in `post-command-hook' and will remove itself
  581. from that hook."
  582. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  583. (org-remember-finalize))
  584. (defun org-remember-visit-immediately ()
  585. "File remember note immediately.
  586. This should be run in `post-command-hook' and will remove itself
  587. from that hook."
  588. (org-remember '(16))
  589. (goto-char (or (text-property-any
  590. (point) (save-excursion (org-end-of-subtree t t))
  591. 'org-position-cursor t)
  592. (point)))
  593. (message "%s"
  594. (format
  595. (substitute-command-keys
  596. "Restore window configuration with \\[jump-to-register] %c")
  597. remember-register)))
  598. (defvar org-clock-marker) ; Defined in org.el
  599. (defun org-remember-finalize ()
  600. "Finalize the remember process."
  601. (interactive)
  602. (unless org-remember-mode
  603. (error "This does not seem to be a remember buffer for Org-mode"))
  604. (run-hooks 'org-remember-before-finalize-hook)
  605. (unless (fboundp 'remember-finalize)
  606. (defalias 'remember-finalize 'remember-buffer))
  607. (when (and org-clock-marker
  608. (equal (marker-buffer org-clock-marker) (current-buffer)))
  609. ;; the clock is running in this buffer.
  610. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  611. (or (eq org-remember-clock-out-on-exit t)
  612. (and org-remember-clock-out-on-exit
  613. (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
  614. (let (org-log-note-clock-out) (org-clock-out))))
  615. (when buffer-file-name
  616. (save-buffer))
  617. (remember-finalize))
  618. (defun org-remember-kill ()
  619. "Abort the current remember process."
  620. (interactive)
  621. (let ((org-note-abort t))
  622. (org-remember-finalize)))
  623. ;;;###autoload
  624. (defun org-remember (&optional goto org-force-remember-template-char)
  625. "Call `remember'. If this is already a remember buffer, re-apply template.
  626. If there is an active region, make sure remember uses it as initial content
  627. of the remember buffer.
  628. When called interactively with a `C-u' prefix argument GOTO, don't remember
  629. anything, just go to the file/headline where the selected template usually
  630. stores its notes. With a double prefix arg `C-u C-u', go to the last
  631. note stored by remember.
  632. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  633. associated with a template in `org-remember-templates'."
  634. (interactive "P")
  635. (org-require-remember)
  636. (cond
  637. ((equal goto '(4)) (org-go-to-remember-target))
  638. ((equal goto '(16)) (org-remember-goto-last-stored))
  639. (t
  640. ;; set temporary variables that will be needed in
  641. ;; `org-select-remember-template'
  642. (setq org-select-template-temp-major-mode major-mode)
  643. (setq org-select-template-original-buffer (current-buffer))
  644. (if org-remember-mode
  645. (progn
  646. (when (< (length org-remember-templates) 2)
  647. (error "No other template available"))
  648. (erase-buffer)
  649. (let ((annotation (plist-get org-store-link-plist :annotation))
  650. (initial (plist-get org-store-link-plist :initial)))
  651. (org-remember-apply-template))
  652. (message "Press C-c C-c to remember data"))
  653. (if (org-region-active-p)
  654. (org-do-remember (buffer-substring (point) (mark)))
  655. (org-do-remember))))))
  656. (defvar org-remember-last-stored-marker (make-marker)
  657. "Marker pointing to the entry most recently stored with `org-remember'.")
  658. (defun org-remember-goto-last-stored ()
  659. "Go to the location where the last remember note was stored."
  660. (interactive)
  661. (org-goto-marker-or-bmk org-remember-last-stored-marker
  662. "org-remember-last-stored")
  663. (message "This is the last note stored by remember"))
  664. (defun org-go-to-remember-target (&optional template-key)
  665. "Go to the target location of a remember template.
  666. The user is queried for the template."
  667. (interactive)
  668. (let* (org-select-template-temp-major-mode
  669. (entry (org-select-remember-template template-key))
  670. (file (nth 1 entry))
  671. (heading (nth 2 entry))
  672. visiting)
  673. (unless (and file (stringp file) (string-match "\\S-" file))
  674. (setq file org-default-notes-file))
  675. (when (and file (not (file-name-absolute-p file)))
  676. (setq file (expand-file-name file org-directory)))
  677. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  678. (setq heading org-remember-default-headline))
  679. (setq visiting (org-find-base-buffer-visiting file))
  680. (if (not visiting) (find-file-noselect file))
  681. (switch-to-buffer (or visiting (get-file-buffer file)))
  682. (widen)
  683. (goto-char (point-min))
  684. (if (re-search-forward
  685. (concat "^\\*+[ \t]+" (regexp-quote heading)
  686. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  687. nil t)
  688. (goto-char (match-beginning 0))
  689. (error "Target headline not found: %s" heading))))
  690. ;;;###autoload
  691. (defun org-remember-handler ()
  692. "Store stuff from remember.el into an org file.
  693. When the template has specified a file and a headline, the entry is filed
  694. there, or in the location defined by `org-default-notes-file' and
  695. `org-remember-default-headline'.
  696. If no defaults have been defined, or if the current prefix argument
  697. is 1 (so you must use `C-1 C-c C-c' to exit remember), an interactive
  698. process is used to select the target location.
  699. When the prefix is 0 (i.e. when remember is exited with `C-0 C-c C-c'),
  700. the entry is filed to the same location as the previous note.
  701. When the prefix is 2 (i.e. when remember is exited with `C-2 C-c C-c'),
  702. the entry is filed as a subentry of the entry where the clock is
  703. currently running.
  704. When `C-u' has been used as prefix argument, the note is stored and emacs
  705. moves point to the new location of the note, so that editing can be
  706. continued there (similar to inserting \"%&\" into the template).
  707. Before storing the note, the function ensures that the text has an
  708. org-mode-style headline, i.e. a first line that starts with
  709. a \"*\". If not, a headline is constructed from the current date and
  710. some additional data.
  711. If the variable `org-adapt-indentation' is non-nil, the entire text is
  712. also indented so that it starts in the same column as the headline
  713. \(i.e. after the stars).
  714. See also the variable `org-reverse-note-order'."
  715. (when (and (equal current-prefix-arg 2)
  716. (not (marker-buffer org-clock-marker)))
  717. (error "No running clock"))
  718. (when (org-bound-and-true-p org-jump-to-target-location)
  719. (let* ((end (min (point-max) (1+ (point))))
  720. (beg (point)))
  721. (if (= end beg) (setq beg (1- beg)))
  722. (put-text-property beg end 'org-position-cursor t)))
  723. (goto-char (point-min))
  724. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  725. (replace-match ""))
  726. (goto-char (point-max))
  727. (beginning-of-line 1)
  728. (while (looking-at "[ \t]*$\\|##.*")
  729. (delete-region (1- (point)) (point-max))
  730. (beginning-of-line 1))
  731. (catch 'quit
  732. (if org-note-abort (throw 'quit nil))
  733. (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
  734. (backup-file
  735. (and buffer-file-name
  736. (equal (file-name-directory buffer-file-name)
  737. (file-name-as-directory
  738. (expand-file-name org-remember-backup-directory)))
  739. (string-match "^remember-[0-9]\\{4\\}"
  740. (file-name-nondirectory buffer-file-name))
  741. buffer-file-name))
  742. (previousp (and (member current-prefix-arg '((16) 0))
  743. org-remember-previous-location))
  744. (clockp (equal current-prefix-arg 2))
  745. (fastp (org-xor (equal current-prefix-arg 1)
  746. org-remember-store-without-prompt))
  747. (file (cond
  748. (fastp org-default-notes-file)
  749. ((and (eq org-remember-interactive-interface 'refile)
  750. org-refile-targets)
  751. org-default-notes-file)
  752. ((not previousp)
  753. (org-get-org-file))))
  754. (heading org-remember-default-headline)
  755. (visiting (and file (org-find-base-buffer-visiting file)))
  756. (org-startup-folded nil)
  757. (org-startup-align-all-tables nil)
  758. (org-goto-start-pos 1)
  759. spos exitcmd level reversed txt text-before-node-creation)
  760. (when (equal current-prefix-arg '(4))
  761. (setq visitp t))
  762. (when previousp
  763. (setq file (car org-remember-previous-location)
  764. visiting (and file (org-find-base-buffer-visiting file))
  765. heading (cdr org-remember-previous-location)
  766. fastp t))
  767. (when clockp
  768. (setq file (buffer-file-name (marker-buffer org-clock-marker))
  769. visiting (and file (org-find-base-buffer-visiting file))
  770. heading org-clock-heading-for-remember
  771. fastp t))
  772. (setq current-prefix-arg nil)
  773. ;; Modify text so that it becomes a nice subtree which can be inserted
  774. ;; into an org tree.
  775. (goto-char (point-min))
  776. (if (re-search-forward "[ \t\n]+\\'" nil t)
  777. ;; remove empty lines at end
  778. (replace-match ""))
  779. (goto-char (point-min))
  780. (unless (looking-at org-outline-regexp)
  781. ;; add a headline
  782. (setq text-before-node-creation (buffer-string))
  783. (insert (concat "* " (current-time-string)
  784. " (" (remember-buffer-desc) ")\n"))
  785. (backward-char 1)
  786. (when org-adapt-indentation
  787. (while (re-search-forward "^" nil t)
  788. (insert " "))))
  789. (goto-char (point-min))
  790. (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
  791. (replace-match "\n\n")
  792. (if (re-search-forward "[ \t\n]*\\'")
  793. (replace-match "\n")))
  794. (goto-char (point-min))
  795. (setq txt (buffer-string))
  796. (org-save-markers-in-region (point-min) (point-max))
  797. (set-buffer-modified-p nil)
  798. (when (and (eq org-remember-interactive-interface 'refile)
  799. (not fastp))
  800. (org-refile nil (or visiting (find-file-noselect file)))
  801. (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
  802. (save-excursion
  803. (bookmark-jump "org-refile-last-stored")
  804. (bookmark-set "org-remember-last-stored")
  805. (move-marker org-remember-last-stored-marker (point)))
  806. (throw 'quit t))
  807. ;; Find the file
  808. (with-current-buffer (or visiting (find-file-noselect file))
  809. (unless (or (org-mode-p) (member heading '(top bottom)))
  810. (error "Target files for notes must be in Org-mode if not filing to top/bottom"))
  811. (save-excursion
  812. (save-restriction
  813. (widen)
  814. (setq reversed (org-notes-order-reversed-p))
  815. ;; Find the default location
  816. (when heading
  817. (cond
  818. ((not (org-mode-p))
  819. (if (eq heading 'top)
  820. (goto-char (point-min))
  821. (goto-char (point-max))
  822. (or (bolp) (newline)))
  823. (insert text-before-node-creation)
  824. (when remember-save-after-remembering
  825. (save-buffer)
  826. (if (not visiting) (kill-buffer (current-buffer))))
  827. (throw 'quit t))
  828. ((eq heading 'top)
  829. (goto-char (point-min))
  830. (or (looking-at org-outline-regexp)
  831. (re-search-forward org-outline-regexp nil t))
  832. (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
  833. ((eq heading 'bottom)
  834. (goto-char (point-max))
  835. (or (bolp) (newline))
  836. (setq org-goto-start-pos (point)))
  837. ((and (stringp heading) (string-match "\\S-" heading))
  838. (goto-char (point-min))
  839. (if (re-search-forward
  840. (concat "^\\*+[ \t]+" (regexp-quote heading)
  841. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  842. nil t)
  843. (setq org-goto-start-pos (match-beginning 0))
  844. (when fastp
  845. (goto-char (point-max))
  846. (unless (bolp) (newline))
  847. (insert "* " heading "\n")
  848. (setq org-goto-start-pos (point-at-bol 0)))))
  849. (t (goto-char (point-min)) (setq org-goto-start-pos (point)
  850. heading 'top))))
  851. ;; Ask the User for a location, using the appropriate interface
  852. (cond
  853. ((and fastp (memq heading '(top bottom)))
  854. (setq spos org-goto-start-pos
  855. exitcmd (if (eq heading 'top) 'left nil)))
  856. (fastp (setq spos org-goto-start-pos
  857. exitcmd 'return))
  858. ((eq org-remember-interactive-interface 'outline)
  859. (setq spos (org-get-location (current-buffer)
  860. org-remember-help)
  861. exitcmd (cdr spos)
  862. spos (car spos)))
  863. ((eq org-remember-interactive-interface 'outline-path-completion)
  864. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  865. (org-refile-use-outline-path t))
  866. (setq spos (org-refile-get-location "Heading: ")
  867. exitcmd 'return
  868. spos (nth 3 spos))))
  869. (t (error "This should not happen")))
  870. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  871. ; not handle this note
  872. (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
  873. (goto-char spos)
  874. (cond ((org-on-heading-p t)
  875. (org-back-to-heading t)
  876. (setq level (funcall outline-level))
  877. (cond
  878. ((eq exitcmd 'return)
  879. ;; sublevel of current
  880. (setq org-remember-previous-location
  881. (cons (abbreviate-file-name file)
  882. (org-get-heading 'notags)))
  883. (if reversed
  884. (outline-next-heading)
  885. (org-end-of-subtree t)
  886. (if (not (bolp))
  887. (if (looking-at "[ \t]*\n")
  888. (beginning-of-line 2)
  889. (end-of-line 1)
  890. (insert "\n"))))
  891. (org-paste-subtree (org-get-valid-level level 1) txt)
  892. (and org-auto-align-tags (org-set-tags nil t))
  893. (bookmark-set "org-remember-last-stored")
  894. (move-marker org-remember-last-stored-marker (point)))
  895. ((eq exitcmd 'left)
  896. ;; before current
  897. (org-paste-subtree level txt)
  898. (and org-auto-align-tags (org-set-tags nil t))
  899. (bookmark-set "org-remember-last-stored")
  900. (move-marker org-remember-last-stored-marker (point)))
  901. ((eq exitcmd 'right)
  902. ;; after current
  903. (org-end-of-subtree t)
  904. (org-paste-subtree level txt)
  905. (and org-auto-align-tags (org-set-tags nil t))
  906. (bookmark-set "org-remember-last-stored")
  907. (move-marker org-remember-last-stored-marker (point)))
  908. (t (error "This should not happen"))))
  909. ((eq heading 'bottom)
  910. (org-paste-subtree 1 txt)
  911. (and org-auto-align-tags (org-set-tags nil t))
  912. (bookmark-set "org-remember-last-stored")
  913. (move-marker org-remember-last-stored-marker (point)))
  914. ((and (bobp) (not reversed))
  915. ;; Put it at the end, one level below level 1
  916. (save-restriction
  917. (widen)
  918. (goto-char (point-max))
  919. (if (not (bolp)) (newline))
  920. (org-paste-subtree (org-get-valid-level 1 1) txt)
  921. (and org-auto-align-tags (org-set-tags nil t))
  922. (bookmark-set "org-remember-last-stored")
  923. (move-marker org-remember-last-stored-marker (point))))
  924. ((and (bobp) reversed)
  925. ;; Put it at the start, as level 1
  926. (save-restriction
  927. (widen)
  928. (goto-char (point-min))
  929. (re-search-forward "^\\*+ " nil t)
  930. (beginning-of-line 1)
  931. (org-paste-subtree 1 txt)
  932. (and org-auto-align-tags (org-set-tags nil t))
  933. (bookmark-set "org-remember-last-stored")
  934. (move-marker org-remember-last-stored-marker (point))))
  935. (t
  936. ;; Put it right there, with automatic level determined by
  937. ;; org-paste-subtree or from prefix arg
  938. (org-paste-subtree
  939. (if (numberp current-prefix-arg) current-prefix-arg)
  940. txt)
  941. (and org-auto-align-tags (org-set-tags nil t))
  942. (bookmark-set "org-remember-last-stored")
  943. (move-marker org-remember-last-stored-marker (point))))
  944. (when remember-save-after-remembering
  945. (save-buffer)
  946. (if (and (not visiting)
  947. (not (equal (marker-buffer org-clock-marker)
  948. (current-buffer))))
  949. (kill-buffer (current-buffer))))
  950. (when org-remember-auto-remove-backup-files
  951. (when backup-file
  952. (ignore-errors
  953. (delete-file backup-file)
  954. (delete-file (concat backup-file "~"))))
  955. (when org-remember-backup-directory
  956. (let ((n (length
  957. (directory-files
  958. org-remember-backup-directory nil
  959. "^remember-.*[0-9]$"))))
  960. (when (> n 0)
  961. (message
  962. "%d backup files (unfinished remember calls) in %s"
  963. n org-remember-backup-directory))))))))))
  964. t) ;; return t to indicate that we took care of this note.
  965. (defun org-do-remember (&optional initial)
  966. "Call remember."
  967. (remember initial))
  968. (defun org-require-remember ()
  969. "Make sure remember is loaded, or install our own emergency version of it."
  970. (condition-case nil
  971. (require 'remember)
  972. (error
  973. ;; Lets install our own micro version of remember
  974. (defvar remember-register ?R)
  975. (defvar remember-mode-hook nil)
  976. (defvar remember-handler-functions nil)
  977. (defvar remember-buffer "*Remember*")
  978. (defvar remember-save-after-remembering t)
  979. (defvar remember-annotation-functions '(buffer-file-name))
  980. (defun remember-finalize ()
  981. (run-hook-with-args-until-success 'remember-handler-functions)
  982. (when (equal remember-buffer (buffer-name))
  983. (kill-buffer (current-buffer))
  984. (jump-to-register remember-register)))
  985. (defun remember-mode ()
  986. (fundamental-mode)
  987. (setq mode-name "Remember")
  988. (run-hooks 'remember-mode-hook))
  989. (defun remember (&optional initial)
  990. (window-configuration-to-register remember-register)
  991. (let* ((annotation (run-hook-with-args-until-success
  992. 'remember-annotation-functions)))
  993. (switch-to-buffer-other-window (get-buffer-create remember-buffer))
  994. (remember-mode)))
  995. (defun remember-buffer-desc ()
  996. (buffer-substring (point-min) (save-excursion (goto-char (point-min))
  997. (point-at-eol)))))))
  998. (provide 'org-remember)
  999. ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
  1000. ;;; org-remember.el ends here