emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Le Wang <l26wang@gmail.com>
To: Orgmode Mailing List <emacs-orgmode@gnu.org>
Subject: [PATCH] exiting source block edit without change should not change buffer-modified sttatus
Date: Sat, 12 Jan 2013 23:05:47 +0800	[thread overview]
Message-ID: <CAM=K+ioB3kqOKq-_u_S1KiC+05ryuntRf8BFV5Z1C-He=38Q+g@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 266 bytes --]

Currently when I press "C-c '", and immediately exit with "C-c '", the
org buffer's modified flag is set, even though I didn't change
anything.

Code changes with testcases included.  I'm not sure if this can be
made generic for other types of special edit.

-- 
Le

[-- Attachment #2: 0001-add-tests-for-round-trip-source-block-without-change.patch --]
[-- Type: application/octet-stream, Size: 1417 bytes --]

From ed9d88fc9a628a99ae1c134dbc18c14bb4f686fd Mon Sep 17 00:00:00 2001
From: Le Wang <le.wang@agworld.com.au>
Date: Sat, 12 Jan 2013 22:58:14 +0800
Subject: [PATCH 1/2] add tests for round-trip source block without change

- When the source block isn't changed in the source buffer, modified
  status should not change.
---
 testing/lisp/test-org-src.el | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/testing/lisp/test-org-src.el b/testing/lisp/test-org-src.el
index 958a7bc..ca0442f 100644
--- a/testing/lisp/test-org-src.el
+++ b/testing/lisp/test-org-src.el
@@ -96,5 +96,34 @@
 #+end_src
 "))))
 
+(ert-deftest test-org-src/no-change ()
+  "Not changing contents should not change buffer-modified status."
+  (org-test-with-temp-text
+   "
+#+begin_src emacs-lisp
+  (message hello)
+#+end_src
+"
+   (goto-line 2)
+   (set-buffer-modified-p nil)
+   (org-edit-special)
+   (org-edit-src-exit)
+   (should (equal (buffer-modified-p) nil))))
+
+(ert-deftest test-org-src/no-change-empty-block ()
+  "Not changing contents should change buffer-modified status
+when newline has to be added."
+
+  (org-test-with-temp-text
+   "
+#+begin_src emacs-lisp
+#+end_src
+"
+   (goto-line 2)
+   (set-buffer-modified-p nil)
+   (org-edit-special)
+   (org-edit-src-exit)
+   (should (equal (buffer-modified-p) t))))
+
 (provide 'test-org-src)
 ;;; test-org-src.el ends here
-- 
1.7.11.4


[-- Attachment #3: 0002-Fix-unmodified-source-blocking-editing-setting-buffe.patch --]
[-- Type: application/octet-stream, Size: 4983 bytes --]

From c059719772f83fd5b51319367e1a5cca3bcf7751 Mon Sep 17 00:00:00 2001
From: Le Wang <le.wang@agworld.com.au>
Date: Sat, 12 Jan 2013 23:01:15 +0800
Subject: [PATCH 2/2] Fix unmodified source blocking editing setting
 buffer-modified.

- `org-edit-src-exit' should not change buffer-modified status when the
  source block didn't actually change.
---
 lisp/org-src.el | 61 +++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 23 deletions(-)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index 501d30a..2deb39a 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -182,6 +182,7 @@ For example, there is no ocaml-mode in Emacs, but the mode to use is
 (defvar org-edit-src-picture nil)
 (defvar org-edit-src-beg-marker nil)
 (defvar org-edit-src-end-marker nil)
+(defvar org-edit-src-md5 nil)
 (defvar org-edit-src-overlay nil)
 (defvar org-edit-src-block-indentation nil)
 (defvar org-edit-src-saved-temp-window-config nil)
@@ -216,7 +217,8 @@ the display of windows containing the Org buffer and the code buffer."
       (user-error "Not in a source code or example block")
     (unless (eq context 'save)
       (setq org-edit-src-saved-temp-window-config (current-window-configuration)))
-    (let* ((mark (and (org-region-active-p) (mark)))
+    (let* (md5
+	   (mark (and (org-region-active-p) (mark)))
 	   (case-fold-search t)
 	   (info
 	    ;; If the src region consists in no lines, we insert a blank
@@ -224,8 +226,14 @@ the display of windows containing the Org buffer and the code buffer."
 	    (let* ((temp (org-edit-src-find-region-and-lang))
 		   (beg (nth 0 temp))
 		   (end (nth 1 temp)))
-	      (if (>= end beg) temp
-		(goto-char beg)
+	      (if (>= end beg)
+		  (progn
+		    (setq md5 (md5 (current-buffer) beg end))
+		    temp)
+                (setq md5 (md5 (current-buffer)
+			       (point)
+			       (point)))
+                (goto-char beg)
 		(insert "\n")
 		(org-edit-src-find-region-and-lang))))
 	   (full-info (org-babel-get-src-block-info 'light))
@@ -263,6 +271,7 @@ the display of windows containing the Org buffer and the code buffer."
 	       ,(or (nth 4 info) org-coderef-label-format))
 	      (org-edit-src-beg-marker ,beg)
 	      (org-edit-src-end-marker ,end)
+              (org-edit-src-md5 ,md5)
 	      (org-edit-src-block-indentation ,block-nindent)))
       (if (and mark (>= mark beg) (<= mark (1+ end)))
 	  (save-excursion (goto-char (min mark end))
@@ -645,6 +654,7 @@ with \",*\", \",#+\", \",,*\" and \",,#+\"."
 			   org-edit-src-content-indentation))
 	 (preserve-indentation org-src-preserve-indentation)
 	 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p))
+	 (orig-md5 org-edit-src-md5)
 	 (delta 0) code line col indent)
     (when allow-write-back-p
       (unless preserve-indentation (untabify (point-min) (point-max)))
@@ -698,27 +708,32 @@ with \",*\", \",#+\", \",,*\" and \",,#+\"."
 	(insert bufstr))
       (set-buffer-modified-p nil))
     (org-src-switch-to-buffer (marker-buffer beg) (or context 'exit))
-    (if (eq context 'save) (save-buffer)
-      (kill-buffer buffer))
-    (goto-char beg)
-    (when allow-write-back-p
-      (delete-region beg (max beg end))
-      (unless (string-match "\\`[ \t]*\\'" code)
-	(insert code))
+    (let ((orig-buffer-modified-p (buffer-modified-p)))
+      (if (eq context 'save) (save-buffer)
+	(kill-buffer buffer))
       (goto-char beg)
-      (if single (just-one-space)))
-    (if (memq t (mapcar (lambda (overlay)
-			  (eq (overlay-get overlay 'invisible)
-			      'org-hide-block))
-			(overlays-at (point))))
-	;; Block is hidden; put point at start of block
-	(beginning-of-line 0)
-      ;; Block is visible, put point where it was in the code buffer
-      (org-goto-line (1- (+ (org-current-line) line)))
-      (org-move-to-column (if preserve-indentation col (+ col total-nindent delta))))
-    (unless (eq context 'save)
-      (move-marker beg nil)
-      (move-marker end nil)))
+      (when allow-write-back-p
+	(delete-region beg (max beg end))
+	(unless (string-match "\\`[ \t]*\\'" code)
+	  (insert code))
+	(goto-char beg)
+	(if single (just-one-space)))
+      (when (and (not orig-buffer-modified-p)
+		 (equal orig-md5
+			(md5 (current-buffer) beg end)))
+	(set-buffer-modified-p nil))
+      (if (memq t (mapcar (lambda (overlay)
+			    (eq (overlay-get overlay 'invisible)
+				'org-hide-block))
+			  (overlays-at (point))))
+	  ;; Block is hidden; put point at start of block
+	  (beginning-of-line 0)
+	;; Block is visible, put point where it was in the code buffer
+	(org-goto-line (1- (+ (org-current-line) line)))
+	(org-move-to-column (if preserve-indentation col (+ col total-nindent delta))))
+      (unless (eq context 'save)
+	(move-marker beg nil)
+	(move-marker end nil))))
   (unless (eq context 'save)
     (when org-edit-src-saved-temp-window-config
       (set-window-configuration org-edit-src-saved-temp-window-config)
-- 
1.7.11.4


                 reply	other threads:[~2013-01-12 15:05 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAM=K+ioB3kqOKq-_u_S1KiC+05ryuntRf8BFV5Z1C-He=38Q+g@mail.gmail.com' \
    --to=l26wang@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.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).