emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCHv5 2/4] Add `remember-notes' function to store notes across Emacs


From: Michal Nazarewicz
Subject: [PATCHv5 2/4] Add `remember-notes' function to store notes across Emacs restarts.
Date: Fri, 28 Jun 2013 16:40:53 +0200

From: Michal Nazarewicz <address@hidden>

You may think of it as a *scratch* buffer whose content is preserved.
In fact, it was designed as a replacement for *scratch* buffer and can
be used that way by setting `initial-buffer-choice' to 'remember-notes
an `remember-notes-buffer-name' to "*scratch*".  Without the second
change, *scratch* buffer will still be there for notes that do not
need to be preserved.

Add `remember-notes' function to store random notes across Emacs
restarts.
* remember.el (remember-data-file): Added :set callback to affect
notes buffer (if any).
(remember-notes, toggle-remember-notes): New functions for showing
the notes buffer.
(remember-notes-buffer-name, bury-remember-notes-on-kill): New
defcustoms for the `remember-notes' function.
(remember-notes--buffer, remember-notes-map): New variables for
the `remember-notes' function.
(remember-notes--kill-buffer-query): New helper functions.
* startup.el (initial-buffer-choice): Added notes to custom type.
* window.el (save-and-bury-buffer): New function doing what the
name says.
---
 etc/NEWS                   |   8 ++++
 lisp/ChangeLog             |  17 ++++++++
 lisp/startup.el            |   3 +-
 lisp/textmodes/remember.el | 104 ++++++++++++++++++++++++++++++++++++++++++++-
 lisp/window.el             |   8 ++++
 5 files changed, 138 insertions(+), 2 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index f5ab7c6..3789c17 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -90,6 +90,14 @@ simply disabling Transient Mark mode does the same thing.
 ** `initial-buffer-choice' can now specify a function to set up the
 initial buffer.
 
+** `remember-notes' creates a buffer whose content is saved on kill-emacs.
+You may think of it as a *scratch* buffer whose content is preserved.
+In fact, it was designed as a replacement for *scratch* buffer and can
+be used that way by setting `initial-buffer-choice' to 'remember-notes
+an `remember-notes-buffer-name' to "*scratch*".  Without the second
+change, *scratch* buffer will still be there for notes that do not
+need to be preserved.
+
 ** `write-region-inhibit-fsync' now defaults to t in batch mode.
 
 ** ACL support has been added.
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index e5d200d..678f040 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,20 @@
+2013-06-25  Michal Nazarewicz  <address@hidden>
+
+       Add `remember-notes' function to store random notes across Emacs
+       restarts.
+       * remember.el (remember-data-file): Added :set callback to affect
+       notes buffer (if any).
+       (remember-notes, toggle-remember-notes): New functions for showing
+       the notes buffer.
+       (remember-notes-buffer-name, bury-remember-notes-on-kill): New
+       defcustoms for the `remember-notes' function.
+       (remember-notes--buffer, remember-notes-map): New variables for
+       the `remember-notes' function.
+       (remember-notes--kill-buffer-query): New helper functions.
+       * startup.el (initial-buffer-choice): Added notes to custom type.
+       * window.el (save-and-bury-buffer): New function doing what the
+       name says.
+
 2013-06-28  Ivan Kanis  <address@hidden>
 
        * net/shr.el (shr-render-region): New function.
diff --git a/lisp/startup.el b/lisp/startup.el
index 77b2bce..44eea77 100644
--- a/lisp/startup.el
+++ b/lisp/startup.el
@@ -53,7 +53,8 @@ or directory when no target file is specified."
          (const     :tag "Startup screen" nil)
          (directory :tag "Directory" :value "~/")
          (file      :tag "File" :value "~/.emacs")
-          (function  :tag "Function")
+         (const     :tag "Notes buffer" remember-notes)
+         (function  :tag "Function")
          (const     :tag "Lisp scratch buffer" t))
   :version "24.4"
   :group 'initialization)
diff --git a/lisp/textmodes/remember.el b/lisp/textmodes/remember.el
index 5782f25..0fe36f8 100644
--- a/lisp/textmodes/remember.el
+++ b/lisp/textmodes/remember.el
@@ -381,9 +381,22 @@ Subject: %s\n\n"
 
 ;; Remembering to plain files
 
+(defvar remember-notes--buffer nil
+  "The notes buffer.")
+
 (defcustom remember-data-file (locate-user-emacs-file "notes" ".notes")
-  "The file in which to store unprocessed data."
+  "The file in which to store unprocessed data.
+When set via customize, visited file of the notes buffer (if it
+exists) might be changed.  This is only of importance if you are using
+`remember-notes'."
   :type 'file
+  :set (lambda (symbol value)
+        (set-default symbol value)
+        (when (buffer-live-p remember-notes--buffer)
+          (with-current-buffer remember-notes--buffer
+            (setq buffer-file-name
+                  (expand-file-name remember-data-file)))))
+  :initialize 'custom-initialize-default
   :group 'remember)
 
 (defcustom remember-leader-text "** "
@@ -551,4 +564,93 @@ the data away for latter retrieval, and possible indexing.
 \\{remember-mode-map}"
   (set-keymap-parent remember-mode-map nil))
 
+;; Notes buffer showing the notes:
+
+(defcustom remember-notes-buffer-name "*notes*"
+  "Name of the notes buffer.
+Setting it to *scratch* will hijack the *scratch* buffer for the
+purpose of storing notes."
+  :type 'string
+  :group 'remember)
+
+(defcustom initial-remember-notes-major-mode t
+  "Major mode to set to notes buffer when it's created.
+If set to t will use the same mode as `initial-major-mode'."
+  :type '(choice (const    :tag "Same as `initial-major-mode'" t)
+                (function :tag "Major mode" text-mode))
+  :group 'remember)
+
+(defcustom bury-remember-notes-on-kill t
+  "Whether to bury notes buffer instead of killing."
+  :type 'boolean
+  :group 'remember)
+
+(defvar remember-notes-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "\C-c\C-c" 'save-and-bury-buffer)
+    map)
+  "A keymap used in notes buffer.")
+
+;;;###autoload
+(defun remember-notes ()
+  "Creates notes buffer and switches to it if called interactively.
+
+If a notes buffer created by a previous invocation of this
+function already exist, it will be returned.  Otherwise a new
+buffer will be created whose content will be read from file
+pointed by `remember-data-file'.  If a buffer visiting this file
+already exist, that buffer will be used instead of creating a new
+one (see `find-file-noselect' function for more details).
+
+Name of the created buffer is taken from `remember-notes-buffer-name'
+variable and if a buffer with that name already exist (but was not
+created by this function), it will be first killed.
+\\<remember-notes-map>
+`remember-notes-map' is active in the notes buffer which by default
+contains only one \\[save-and-bury-buffer] binding which saves and
+buries the buffer.
+
+Function returns notes buffer.  When called interactively,
+switches to it as well.
+
+Notes buffer is meant for keeping random notes which you'd like to
+preserve across Emacs restarts.  The notes will be stored in the
+`remember-data-file'."
+  (interactive)
+  (unless (buffer-live-p remember-notes--buffer)
+    (setq remember-notes--buffer (find-file-noselect remember-data-file))
+    (with-current-buffer remember-notes--buffer
+      (let ((buf (get-buffer remember-notes-buffer-name)))
+       (if (or (not buf) (kill-buffer buf))
+           (rename-buffer remember-notes-buffer-name)))
+      (funcall (if (eq initial-remember-notes-major-mode t)
+                  initial-major-mode
+                initial-remember-notes-major-mode))
+      (setq buffer-save-without-query t)
+      (add-hook 'kill-buffer-query-functions 
'remember-notes--kill-buffer-query)
+      (setq minor-mode-overriding-map-alist
+           (cons (cons 'remember-notes--buffer remember-notes-map)
+                 minor-mode-overriding-map-alist))))
+  (when (called-interactively-p 'all)
+    (switch-to-buffer remember-notes--buffer))
+  remember-notes--buffer)
+
+;;;###autoload
+(defun toggle-remember-notes ()
+  "Switches to notes buffer unless already there in which case buries it.
+For more information about notes buffer see `remember-notes' function."
+  (interactive)
+  (if (eq (current-buffer) remember-notes--buffer)
+      (bury-buffer)
+    (switch-to-buffer (remember-notes))))
+
+(defun remember-notes--kill-buffer-query ()
+  (if (not (eq (current-buffer) remember-notes--buffer))
+      t
+    (when (buffer-modified-p)
+      (save-buffer))
+    (if bury-remember-notes-on-kill
+       (bury-buffer)
+      t)))
+
 ;;; remember.el ends here
diff --git a/lisp/window.el b/lisp/window.el
index fc50bbb..296cca7 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -3429,6 +3429,14 @@ displayed there."
     ;; Always return nil.
     nil))
 
+(defun save-and-bury-buffer ()
+  "Saves and buries current buffer.
+Buffer is saved only if `buffer-modified-p' returns non-nil."
+  (interactive)
+  (when (buffer-modified-p)
+    (save-buffer))
+  (bury-buffer))
+
 (defun unbury-buffer ()
   "Switch to the last buffer in the buffer list."
   (interactive)
-- 
1.8.3.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]