unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCHv5 0/4] Add remember-notes function, take four
@ 2013-06-28 14:40 Michal Nazarewicz
  2013-06-28 14:40 ` [PATCHv5 1/4] `kill-buffer' runs query functions before checking buffer modification Michal Nazarewicz
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Michal Nazarewicz @ 2013-06-28 14:40 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

From: Michal Nazarewicz <mina86@mina86.com>

Compared to v5, just a small change in the first patch suggested by
Martin Rudalics (early returnd and documentation fix).

Michal Nazarewicz (4):
  `kill-buffer' runs query functions before checking buffer
    modification.
  Add `remember-notes' function to store notes across Emacs restarts.
  `remember-append-to-file' appending to buffer bug fix
  Simplify conditional expression in `find-file-noselect'.

 doc/lispref/buffers.texi   |   2 +-
 etc/NEWS                   |   8 +++
 lisp/ChangeLog             |  31 +++++++++++
 lisp/files.el              |  13 +++--
 lisp/startup.el            |   3 +-
 lisp/textmodes/remember.el | 129 ++++++++++++++++++++++++++++++++++++++++-----
 lisp/window.el             |   8 +++
 src/ChangeLog              |   8 +++
 src/buffer.c               |  28 +++++-----
 9 files changed, 197 insertions(+), 33 deletions(-)

-- 
1.8.3.1




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCHv5 1/4] `kill-buffer' runs query functions before checking buffer modification.
  2013-06-28 14:40 [PATCHv5 0/4] Add remember-notes function, take four Michal Nazarewicz
@ 2013-06-28 14:40 ` Michal Nazarewicz
  2013-06-28 14:40 ` [PATCHv5 2/4] Add `remember-notes' function to store notes across Emacs restarts Michal Nazarewicz
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Michal Nazarewicz @ 2013-06-28 14:40 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: martin rudalics, emacs-devel

From: Michal Nazarewicz <mina86@mina86.com>

* buffer.c (FKill_buffer): Run `kill-buffer-query-functions'
before checking whether buffer is modified.  This lets
`kill-buffer-query-functions' cancel killing of the buffer or save
its content before `kill-buffer' asks user the "Buffer %s
modified; kill anyway?" question.
---
 doc/lispref/buffers.texi |  2 +-
 src/ChangeLog            |  8 ++++++++
 src/buffer.c             | 28 ++++++++++++++++------------
 3 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/doc/lispref/buffers.texi b/doc/lispref/buffers.texi
index 7ed1876..e79c86d 100644
--- a/doc/lispref/buffers.texi
+++ b/doc/lispref/buffers.texi
@@ -1064,7 +1064,7 @@ Buffer foo.changed modified; kill anyway? (yes or no) @kbd{yes}
 @end deffn
 
 @defvar kill-buffer-query-functions
-After confirming unsaved changes, @code{kill-buffer} calls the functions
+Before confirming unsaved changes, @code{kill-buffer} calls the functions
 in the list @code{kill-buffer-query-functions}, in order of appearance,
 with no arguments.  The buffer being killed is the current buffer when
 they are called.  The idea of this feature is that these functions will
diff --git a/src/ChangeLog b/src/ChangeLog
index e404cdb..a3af8d1 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,11 @@
+2013-06-28  Michal Nazarewicz  <mina86@mina86.com>
+
+	* buffer.c (FKill_buffer): Run `kill-buffer-query-functions'
+	before checking whether buffer is modified.  This lets
+	`kill-buffer-query-functions' cancel killing of the buffer or save
+	its content before `kill-buffer' asks user the "Buffer %s
+	modified; kill anyway?" question.
+
 2013-06-28  Paul Eggert  <eggert@cs.ucla.edu>
 
 	* image.c (x_from_xcolors): Remove unused local.
diff --git a/src/buffer.c b/src/buffer.c
index 08299da..ad670e0 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -1734,18 +1734,6 @@ cleaning up all windows currently displaying the buffer to be killed. */)
   if (!BUFFER_LIVE_P (b))
     return Qnil;
 
-  /* Query if the buffer is still modified.  */
-  if (INTERACTIVE && !NILP (BVAR (b, filename))
-      && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
-    {
-      GCPRO1 (buffer);
-      tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
-				     BVAR (b, name), make_number (0)));
-      UNGCPRO;
-      if (NILP (tem))
-	return Qnil;
-    }
-
   /* Run hooks with the buffer to be killed the current buffer.  */
   {
     ptrdiff_t count = SPECPDL_INDEX ();
@@ -1761,6 +1749,22 @@ cleaning up all windows currently displaying the buffer to be killed. */)
     if (NILP (tem))
       return unbind_to (count, Qnil);
 
+    /* Query if the buffer is still modified.  */
+    if (INTERACTIVE && !NILP (BVAR (b, filename))
+	&& BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
+      {
+        GCPRO1 (buffer);
+        tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ",
+				       BVAR (b, name), make_number (0)));
+	UNGCPRO;
+	if (NILP (tem))
+	  return unbind_to (count, Qnil);
+      }
+
+    /* If the hooks have killed the buffer, exit now.  */
+    if (!BUFFER_LIVE_P (b))
+      return unbind_to (count, Qt);
+
     /* Then run the hooks.  */
     Frun_hooks (1, &Qkill_buffer_hook);
     unbind_to (count, Qnil);
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCHv5 2/4] Add `remember-notes' function to store notes across Emacs restarts.
  2013-06-28 14:40 [PATCHv5 0/4] Add remember-notes function, take four Michal Nazarewicz
  2013-06-28 14:40 ` [PATCHv5 1/4] `kill-buffer' runs query functions before checking buffer modification Michal Nazarewicz
@ 2013-06-28 14:40 ` Michal Nazarewicz
  2013-06-28 14:40 ` [PATCHv5 3/4] `remember-append-to-file' appending to buffer bug fix Michal Nazarewicz
  2013-06-28 14:40 ` [PATCHv5 4/4] Simplify conditional expression in `find-file-noselect' Michal Nazarewicz
  3 siblings, 0 replies; 5+ messages in thread
From: Michal Nazarewicz @ 2013-06-28 14:40 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

From: Michal Nazarewicz <mina86@mina86.com>

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  <mina86@mina86.com>
+
+	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  <ivan@kanis.fr>
 
 	* 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




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCHv5 3/4] `remember-append-to-file' appending to buffer bug fix
  2013-06-28 14:40 [PATCHv5 0/4] Add remember-notes function, take four Michal Nazarewicz
  2013-06-28 14:40 ` [PATCHv5 1/4] `kill-buffer' runs query functions before checking buffer modification Michal Nazarewicz
  2013-06-28 14:40 ` [PATCHv5 2/4] Add `remember-notes' function to store notes across Emacs restarts Michal Nazarewicz
@ 2013-06-28 14:40 ` Michal Nazarewicz
  2013-06-28 14:40 ` [PATCHv5 4/4] Simplify conditional expression in `find-file-noselect' Michal Nazarewicz
  3 siblings, 0 replies; 5+ messages in thread
From: Michal Nazarewicz @ 2013-06-28 14:40 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

From: Michal Nazarewicz <mina86@mina86.com>

* remember.el (remember-append-to-file): Function used
`find-buffer-visiting' to check whether a file visiting
`remember-data-file` existed but then `get-buffer-visiting' to
retrieve it.  However, the latter requires exact string match so
it could return nil even though the former found a buffer.
Storing result of the `find-buffer-visiting' fixes the issue (and
saves one lookup).
---
 lisp/ChangeLog             | 10 ++++++++++
 lisp/textmodes/remember.el | 25 ++++++++++++++-----------
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 678f040..983e0a4 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,13 @@
+2013-06-28  Michal Nazarewicz  <mina86@mina86.com>
+
+	* remember.el (remember-append-to-file): Function used
+	`find-buffer-visiting' to check whether a file visiting
+	`remember-data-file` existed but then `get-buffer-visiting' to
+	retrieve it.  However, the latter requires exact string match so
+	it could return nil even though the former found a buffer.
+	Storing result of the `find-buffer-visiting' fixes the issue (and
+	saves one lookup).
+
 2013-06-28  Michal Nazarewicz  <mina86@mina86.com>
 
 	Add `remember-notes' function to store random notes across Emacs
diff --git a/lisp/textmodes/remember.el b/lisp/textmodes/remember.el
index 0fe36f8..aa10aeb 100644
--- a/lisp/textmodes/remember.el
+++ b/lisp/textmodes/remember.el
@@ -408,19 +408,22 @@ exists) might be changed.  This is only of importance if you are using
   "Remember, with description DESC, the given TEXT."
   (let ((text (buffer-string))
         (desc (remember-buffer-desc)))
-    (with-temp-buffer
-      (insert "\n" remember-leader-text (current-time-string)
-              " (" desc ")\n\n" text)
-      (if (not (bolp))
-          (insert "\n"))
-      (if (find-buffer-visiting remember-data-file)
-          (let ((remember-text (buffer-string)))
-            (set-buffer (get-file-buffer remember-data-file))
+    (let ((buf (find-buffer-visiting remember-data-file)))
+      (if buf
+          (with-current-buffer buf
             (save-excursion
               (goto-char (point-max))
-              (insert remember-text)
-              (when remember-save-after-remembering (save-buffer))))
-        (append-to-file (point-min) (point-max) remember-data-file)))))
+              (insert "\n" remember-leader-text (current-time-string)
+                      " (" desc ")\n\n" text)
+              (unless (bolp)
+                (insert "\n")))
+            (if remember-save-after-remembering (save-buffer)))
+        (with-temp-buffer
+          (insert "\n" remember-leader-text (current-time-string)
+                  " (" desc ")\n\n" text)
+          (unless (bolp)
+            (insert "\n"))
+          (append-to-file (point-min) (point-max) remember-data-file))))))
 
 (defun remember-region (&optional beg end)
   "Remember the data from BEG to END.
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCHv5 4/4] Simplify conditional expression in `find-file-noselect'.
  2013-06-28 14:40 [PATCHv5 0/4] Add remember-notes function, take four Michal Nazarewicz
                   ` (2 preceding siblings ...)
  2013-06-28 14:40 ` [PATCHv5 3/4] `remember-append-to-file' appending to buffer bug fix Michal Nazarewicz
@ 2013-06-28 14:40 ` Michal Nazarewicz
  3 siblings, 0 replies; 5+ messages in thread
From: Michal Nazarewicz @ 2013-06-28 14:40 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

From: Michal Nazarewicz <mina86@mina86.com>

First of all, (null foo) always returns nil or t, and not is synonym
of null anywaya, so (eq (not (null foo)) (not (null bar))) can be
simplified to (eq (null foo) (null bar)).

Second of all, (and (not foo) (not bar) (not baz)) is the same as (not
(or foo bar baz)).

And then, finally, (when (not foo) bar...) is the same as (unless foo
bar...).
---
 lisp/ChangeLog |  4 ++++
 lisp/files.el  | 13 ++++++-------
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 983e0a4..efcd679 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -10,6 +10,10 @@
 
 2013-06-28  Michal Nazarewicz  <mina86@mina86.com>
 
+	* files.el (find-file-noselect): Simplify conditional expression.
+
+2013-06-28  Michal Nazarewicz  <mina86@mina86.com>
+
 	Add `remember-notes' function to store random notes across Emacs
 	restarts.
 	* remember.el (remember-data-file): Added :set callback to affect
diff --git a/lisp/files.el b/lisp/files.el
index 871a4b0..e59a9ac 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -1859,13 +1859,12 @@ the various files."
 		      (setq buffer-read-only read-only)))
 		  (setq buffer-file-read-only read-only))
 
-		(when (and (not (eq (not (null rawfile))
-				    (not (null find-file-literally))))
-			   (not nonexistent)
-			   ;; It is confusing to ask whether to visit
-			   ;; non-literally if they have the file in
-			   ;; hexl-mode or image-mode.
-			   (not (memq major-mode '(hexl-mode image-mode))))
+		(unless (or (eq (null rawfile) (null find-file-literally))
+			    nonexistent
+			    ;; It is confusing to ask whether to visit
+			    ;; non-literally if they have the file in
+			    ;; hexl-mode or image-mode.
+			    (memq major-mode '(hexl-mode image-mode)))
 		  (if (buffer-modified-p)
 		      (if (y-or-n-p
 			   (format
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-06-28 14:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-06-28 14:40 [PATCHv5 0/4] Add remember-notes function, take four Michal Nazarewicz
2013-06-28 14:40 ` [PATCHv5 1/4] `kill-buffer' runs query functions before checking buffer modification Michal Nazarewicz
2013-06-28 14:40 ` [PATCHv5 2/4] Add `remember-notes' function to store notes across Emacs restarts Michal Nazarewicz
2013-06-28 14:40 ` [PATCHv5 3/4] `remember-append-to-file' appending to buffer bug fix Michal Nazarewicz
2013-06-28 14:40 ` [PATCHv5 4/4] Simplify conditional expression in `find-file-noselect' Michal Nazarewicz

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).