emacs-devel
[Top][All Lists]
Advanced

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

Re: [RFC] Added an option to store content of the *scratch* buffer in a


From: Michal Nazarewicz
Subject: Re: [RFC] Added an option to store content of the *scratch* buffer in a file.
Date: Sat, 08 Jun 2013 00:42:04 +0200
User-agent: Notmuch/0.15.2+55~geb6e9d8 (http://notmuchmail.org) Emacs/24.3.50.10 (x86_64-unknown-linux-gnu)

On Fri, Jun 07 2013, Glenn Morris wrote:
> Michal Nazarewicz wrote:
>> +** `initial-scratch-message' can now be 'file which makes content of
>> +the *scratch* buffer to be kept in a file.  This makes it possible to
>> +keep notes which will persist even when Emacs restarts in that buffer.
>
> Seems like a bad UI to (ab)use initial-scratch-message for this.
> Also, it's not really a scratch buffer if it gets saved, is it?
> Maybe extending `initial-buffer-choice' would be better?

How does the below look like:

-------------------- >8 --------------------------------------------------------
Subject: [PATCH] Add `notes' function to store random notes across Emacs
 restarts.

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 'notes an
`notes-buffer-name' to "*scratch*".  Without the second
change, *scratch* buffer will still be there for notes that do not
need to be preserved.

* list/startup.el (notes): New function creating notes buffer.
(notes-file, notes-recover-from-auto-save-file, notes-buffer-name)
(initial-notes-major-mode): New customize variables for customizing
behaviour of the notes buffer.
(notes--bury-on-kill-buffer, notes--insert-content): New helper
functions for `notes' function.
(notes--buffer): New helper variable for `notes' function.
(notes--initial-message): New helper constant for `notes' function.
---
 etc/NEWS        |   8 +++
 lisp/ChangeLog  |  12 +++++
 lisp/startup.el | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 166 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index 380c934..16e8943 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -87,6 +87,14 @@ simply disabling Transient Mark mode does the same thing.
 ** `initial-buffer-choice' can now specify a function to set up the
 initial buffer.
 
+** `notes' function 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 'notes an
+`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 665e656..b7d87a1 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,15 @@
+2013-06-08  Michal Nazarewicz  <address@hidden>
+
+       Add `notes' function to store random notes across Emacs restarts.
+       * list/startup.el (notes): New function creating notes buffer.
+       (notes-file, notes-recover-from-auto-save-file, notes-buffer-name)
+       (initial-notes-major-mode): New customize variables for customizing
+       behaviour of the notes buffer.
+       (notes--bury-on-kill-buffer, notes--insert-content): New helper
+       functions for `notes' function.
+       (notes--buffer): New helper variable for `notes' function.
+       (notes--initial-message): New helper constant for `notes' function.
+
 2013-06-07  Leo Liu  <address@hidden>
 
        * progmodes/octave.el (octave-mode): Set comment-use-global-state
diff --git a/lisp/startup.el b/lisp/startup.el
index b7b4c15..7d7c2da 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" notes)
+         (function  :tag "Function")
          (const     :tag "Lisp scratch buffer" t))
   :version "24.4"
   :group 'initialization)
@@ -2383,4 +2384,148 @@ A fancy display is used on graphic displays, normal 
otherwise."
        (setq file (replace-match "/" t t file))))
     file))
 
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; Notes which are preserved across restarts
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(defvar notes--buffer nil
+  "The notes buffer.")
+
+
+(defcustom notes-file (concat user-emacs-directory "notes")
+  "File to save notes in.
+When set via customize `buffer-file-name' variable of the notes buffer
+\(if it exists) will be changed."
+  :type 'string
+  :set (lambda (symbol value)
+        (set-default symbol value)
+        (when (buffer-live-p notes--buffer)
+          (with-current-buffer notes--buffer
+            (setq buffer-file-name value))))
+  :group 'initialization)
+
+(defcustom notes-recover-from-auto-save-file 'ask
+  "What to do if notes autosave file is newer than the notes file.
+
+t means to always recover content from auto-save file, 'ask means
+to ask user, and nil means never to recover auto-save file (which
+also disables `auto-save-mode' in the notes buffer.
+
+When set via customize, `auto-save-mode' will be enabled or disabled
+in the notes buffer according to this variable's value."
+  :type '(choice (const :tag "Always recover auto-save" t)
+                 (const :tag "Never recover auto-save" nil)
+                 (const :tag "Ask whether to recover auto-save" ask))
+  :set (lambda (symbol value)
+        (set-default symbol value)
+        (when (buffer-live-p notes--buffer)
+          (with-current-buffer notes--buffer
+            (auto-save-mode (if value 1 -1)))))
+  :group 'initialization)
+
+(defcustom 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 'initialization)
+
+(defcustom initial-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)))
+
+(defcustom bury-notes-on-kill t
+  "Whether to bury notes buffer instead of killing."
+  :type 'boolean)
+
+(defconst notes--initial-message (purecopy "\
+;; This buffer is for notes and for Lisp evaluation.
+;; If you want to create a file, visit that file with C-x C-f,
+;; then enter the text in that file's own buffer.
+;; Contents of this buffer will be saved across restarts.
+
+"))
+
+
+(defun notes ()
+  "Creates notes buffer and switches to it if called interactively.
+
+Name of the created buffer is taken from `notes-buffer-name' variable
+and if buffer with that name already exist (but was not created by
+`notes' function)), its content will be overwritten.  Returns notes
+buffer.
+
+Notes buffer is meant for keeping random notes which you'd like to
+preserve across Emacs restarts."
+  (interactive)
+  (unless (buffer-live-p notes--buffer)
+    (setq notes--buffer (get-buffer-create notes-buffer-name))
+    (with-current-buffer notes--buffer
+      (funcall (if (eq initial-notes-major-mode t)
+                  initial-major-mode
+                initial-notes-major-mode))
+      (setq buffer-file-name notes-file
+           buffer-save-without-query t)
+      (auto-save-mode (if notes-recover-from-auto-save-file 1 -1))
+      ;; We don't want a "Buffer modified" prompt from kill-buffer so
+      ;; we have to use advice rather than a hook.
+      (advice-add 'kill-buffer :around 'notes--kill-buffer)
+      (notes--insert-content)))
+  (when (called-interactively-p 'all)
+    (switch-to-buffer notes--buffer))
+  notes--buffer)
+
+(defun notes--insert-content ()
+  (let* ((have-file (file-readable-p buffer-file-name))
+        (have-auto-save (and buffer-auto-save-file-name
+                             (file-readable-p buffer-auto-save-file-name))))
+    ;; If autosave is older, pretend it does not exist.
+    (and have-file
+        have-auto-save
+        (not (file-newer-than-file-p buffer-auto-save-file-name
+                                      buffer-file-name))
+        (setq have-auto-save nil))
+    ;; If user wants us to always recover, pretend there's no base file.
+    (and have-auto-save
+        (eq t notes-recover-from-auto-save-file)
+        (setq have-file nil))
+    ;; Ask user what to do.
+    (and have-file
+        have-auto-save
+        (if (y-or-n-p "Recover notes file? ")
+            (setq have-file nil)
+          (setq have-auto-save nil)))
+    (let ((file (cond (have-file buffer-file-name)
+                     (have-auto-save buffer-auto-save-file-name))))
+      (cond (file
+            (insert-file-contents file nil nil nil t)
+            (set-buffer-modified-p nil))
+           ((zerop (buffer-size))
+            (insert notes--initial-message))
+            (t
+            (set-buffer-modified-p t))))))
+
+(defun notes--kill-buffer (func &optional buffer)
+  (if (null notes--buffer)
+      (funcall func buffer)
+    (setq buffer (cond ((null    buffer) (current-buffer))
+                      ((stringp buffer) (get-buffer buffer))
+                      (buffer)))
+    (and (buffer-live-p buffer)
+        (not (when (eq buffer notes--buffer)
+               (when (buffer-modified-p buffer)
+                 (with-current-buffer buffer
+                   (save-buffer)))
+               (when bury-notes-on-kill
+                 (bury-buffer (unless (eq buffer (current-buffer)) buffer))
+                 t)))
+        (funcall func buffer)
+        (progn
+          (when (eq buffer notes--buffer)
+            (setq notes--buffer nil))
+          t))))
+
 ;;; startup.el ends here



reply via email to

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