emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Le Wang <l26wang@gmail.com>
To: Bastien <bzg@altern.org>
Cc: Bernt Hansen <bernt@norang.ca>, emacs-orgmode@gnu.org
Subject: Re: bug#12905: 24.2.50; org: edit source block causes data loss
Date: Fri, 14 Dec 2012 07:40:15 +0800	[thread overview]
Message-ID: <CAM=K+ioHiom-PNz5fV83MtA6JY1pVLG8a8YUvi08MiSUvZbyXw@mail.gmail.com> (raw)
In-Reply-To: <87mwxhyl5l.fsf@bzg.ath.cx>

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

On Fri, Dec 14, 2012 at 12:25 AM, Bastien <bzg@altern.org> wrote:
> I superseded the first one with my patch, and just applied the
> third one.
>
> Can you explain the bug behind the second one?


If I press C-' before on a line BEFORE "+begin_src ...", then doing
comparison with ">=" with beg==nil and end==nil causes
"wrong-type-argument" error.

"test-org-src/point-outside-block" test case should fail, but because
"wrong-type-argument" is also an error, it is passing.  :(

I've changed "(error ..." to raise an org-src specific error to
facilitate testing.  Commits rebased to master are attached.

A note:

I did change this line:

    (end (copy-marker (make-marker) t))

(make-marker) makes a temporary new marker that's never used.  ;)


-- 
Le

[-- Attachment #2: 0001-don-t-use-copy-marker-nil-t-for-Emacs-23-compat.patch --]
[-- Type: application/octet-stream, Size: 889 bytes --]

From 1b55d713a60a3b61b4af6bb314c0bcfb780d3b34 Mon Sep 17 00:00:00 2001
From: Le Wang <le.wang@agworld.com.au>
Date: Thu, 13 Dec 2012 23:40:59 +0800
Subject: [PATCH 1/5] don't use (copy-marker nil t) for Emacs 23 compat

---
 lisp/org-src.el | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index 188f9d2..6515987 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -230,7 +230,9 @@ buffer."
 	 (beg (make-marker))
 	 ;; Move marker with inserted text for case when src block is
 	 ;; just one empty line, i.e. beg == end.
-	 (end (copy-marker (make-marker) t))
+	 (end (let ((marker (make-marker)))
+		(set-marker-insertion-type marker t)
+		marker))
 	 (allow-write-back-p (null code))
 	 block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
 	 begline markline markcol line col transmitted-variables)
-- 
1.7.11.4


[-- Attachment #3: 0002-fix-org-edit-src-code-invoked-outside-src-block.patch --]
[-- Type: application/octet-stream, Size: 1027 bytes --]

From 2e19391476b76263dcbc0cf73e5ee1d0b260ddf3 Mon Sep 17 00:00:00 2001
From: Le Wang <le.wang@agworld.com.au>
Date: Thu, 13 Dec 2012 23:46:00 +0800
Subject: [PATCH 2/5] fix org-edit-src-code invoked outside src block

---
 lisp/org-src.el | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index 6515987..97f473a 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -221,10 +221,13 @@ 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)
-	      (insert "\n")
-	      (org-edit-src-find-region-and-lang))))
+	    (if (and beg end)
+		(if (>= end beg)
+		    temp
+		  (goto-char beg)
+		  (insert "\n")
+		  (org-edit-src-find-region-and-lang))
+	      (error "Point not in src block."))))
 	 (full-info (org-babel-get-src-block-info 'light))
 	 (org-mode-p (derived-mode-p 'org-mode)) ;; derived-mode-p is reflexive
 	 (beg (make-marker))
-- 
1.7.11.4


[-- Attachment #4: 0003-test-org-src-checks-for-org-src-generated-error.patch --]
[-- Type: application/octet-stream, Size: 960 bytes --]

From a4cf5825f49a30664ca95b6794200f32e94424d8 Mon Sep 17 00:00:00 2001
From: Le Wang <le.wang@agworld.com.au>
Date: Fri, 14 Dec 2012 06:51:31 +0800
Subject: [PATCH 3/5] test-org-src checks for org-src generated error

- We need to check the error raised is not a generic error, but the one
  we expect our code to generate.
---
 testing/lisp/test-org-src.el | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/testing/lisp/test-org-src.el b/testing/lisp/test-org-src.el
index 8205fd1..65d52d3 100644
--- a/testing/lisp/test-org-src.el
+++ b/testing/lisp/test-org-src.el
@@ -56,9 +56,9 @@
 #+end_src
 "
     (goto-line 1)
-    (should-error (org-edit-special))
+    (should-error (org-edit-special) :type '(org-src-error))
     (goto-char (point-max))
-    (should-error (org-edit-special))))
+    (should-error (org-edit-special) :type '(org-src-error))))
 
 (ert-deftest test-org-src/empty-block ()
   "Editing empty block."
-- 
1.7.11.4


[-- Attachment #5: 0004-add-org-src-error-error-condition.patch --]
[-- Type: application/octet-stream, Size: 2793 bytes --]

From f8c4bee3a4223d77f44c961c4eab9afa4cb1345b Mon Sep 17 00:00:00 2001
From: Le Wang <le.wang@agworld.com.au>
Date: Fri, 14 Dec 2012 06:46:15 +0800
Subject: [PATCH 4/5] add org-src-error error-condition

- This makes code more testable, because we can expect org specific
  errors instead of the generic error.
---
 lisp/org-src.el | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index 97f473a..ad6ec60 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -192,6 +192,11 @@ the existing edit buffer.")
 
 (defvar org-src-babel-info nil)
 
+(put 'org-src-error
+     'error-conditions
+     '(error org-src-error))
+(put 'org-src-error 'error-message "Org Src Error")
+
 (define-minor-mode org-src-mode
   "Minor mode for language major mode buffers generated by org.
 This minor mode is turned on in two situations:
@@ -227,7 +232,7 @@ buffer."
 		  (goto-char beg)
 		  (insert "\n")
 		  (org-edit-src-find-region-and-lang))
-	      (error "Point not in src block."))))
+	      (signal 'org-src-error '("Point not in src block.")))))
 	 (full-info (org-babel-get-src-block-info 'light))
 	 (org-mode-p (derived-mode-p 'org-mode)) ;; derived-mode-p is reflexive
 	 (beg (make-marker))
@@ -280,7 +285,7 @@ buffer."
 			 (table-recognize)
 			 (org-set-local 'org-edit-src-content-indentation 0))))
       (unless (functionp lang-f)
-	(error "No such language mode: %s" lang-f))
+	(signal 'org-src-error "No such language mode: %s" lang-f))
       (save-excursion
 	(if (> (point) end) (goto-char end))
 	(setq line (org-current-line)
@@ -322,7 +327,7 @@ buffer."
 	  (condition-case e
 	      (funcall lang-f)
 	    (error
-	     (error "Language mode `%s' fails with: %S" lang-f (nth 1 e)))))
+	     (signal 'org-src-error "Language mode `%s' fails with: %S" lang-f (nth 1 e)))))
 	(dolist (pair transmitted-variables)
 	  (org-set-local (car pair) (cadr pair)))
 	;; Remove protecting commas from visible part of buffer.
@@ -353,7 +358,7 @@ buffer."
   (mouse-set-point e)
   (let ((buf (get-char-property (point) 'edit-buffer)))
     (if buf (org-src-switch-to-buffer buf 'continue)
-      (error "Something is wrong here"))))
+      (signal 'org-src-error "Something is wrong here"))))
 
 (defun org-src-switch-to-buffer (buffer context)
   (case org-src-window-setup
@@ -636,7 +641,7 @@ with \",*\", \",#+\", \",,*\" and \",,#+\"."
   "Exit special edit and protect problematic lines."
   (interactive)
   (unless (org-bound-and-true-p org-edit-src-from-org-mode)
-    (error "This is not a sub-editing buffer, something is wrong"))
+    (signal 'org-src-error "This is not a sub-editing buffer, something is wrong"))
   (widen)
   (let* ((beg org-edit-src-beg-marker)
 	 (end org-edit-src-end-marker)
-- 
1.7.11.4


[-- Attachment #6: 0005-remove-extraneous-if-condition.patch --]
[-- Type: application/octet-stream, Size: 9852 bytes --]

From 949b2cd4c378872345f3f3a026beb5d1a228288d Mon Sep 17 00:00:00 2001
From: Le Wang <le.wang@agworld.com.au>
Date: Fri, 14 Dec 2012 07:31:15 +0800
Subject: [PATCH 5/5] remove extraneous if condition

- We signal org-src-error before ever reaching here.
---
 lisp/org-src.el | 210 ++++++++++++++++++++++++++++----------------------------
 1 file changed, 104 insertions(+), 106 deletions(-)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index ad6ec60..d4747ab 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -244,113 +244,111 @@ buffer."
 	 (allow-write-back-p (null code))
 	 block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
 	 begline markline markcol line col transmitted-variables)
-    (if (not info)
-	nil
-      (setq beg (move-marker beg (nth 0 info))
-	    end (move-marker end (nth 1 info))
-	    msg (if allow-write-back-p
-		    (substitute-command-keys
-		     "Edit, then exit with C-c ' (C-c and single quote)")
-		  "Exit with C-c ' (C-c and single quote)")
-	    code (or code (buffer-substring-no-properties beg end))
-	    lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
-                     (nth 2 info))
-	    lang (if (symbolp lang) (symbol-name lang) lang)
-	    single (nth 3 info)
-	    block-nindent (nth 5 info)
-	    lang-f (intern (concat lang "-mode"))
-	    begline (save-excursion (goto-char beg) (org-current-line))
-	    transmitted-variables
-	    `((org-edit-src-content-indentation
-	       ,org-edit-src-content-indentation)
-	      (org-edit-src-force-single-line ,single)
-	      (org-edit-src-from-org-mode ,org-mode-p)
-	      (org-edit-src-allow-write-back-p ,allow-write-back-p)
-	      (org-src-preserve-indentation ,org-src-preserve-indentation)
-	      (org-src-babel-info ,(org-babel-get-src-block-info 'light))
-	      (org-coderef-label-format
-	       ,(or (nth 4 info) org-coderef-label-format))
-	      (org-edit-src-beg-marker ,beg)
-	      (org-edit-src-end-marker ,end)
-	      (org-edit-src-block-indentation ,block-nindent)))
-      (if (and mark (>= mark beg) (<= mark (1+ end)))
-	  (save-excursion (goto-char (min mark end))
-			  (setq markline (org-current-line)
-				markcol (current-column))))
-      (if (equal lang-f 'table.el-mode)
-	  (setq lang-f (lambda ()
-			 (text-mode)
-			 (if (org-bound-and-true-p flyspell-mode)
-			     (flyspell-mode -1))
-			 (table-recognize)
-			 (org-set-local 'org-edit-src-content-indentation 0))))
-      (unless (functionp lang-f)
-	(signal 'org-src-error "No such language mode: %s" lang-f))
-      (save-excursion
-	(if (> (point) end) (goto-char end))
-	(setq line (org-current-line)
-	      col (current-column)))
-      (if (and (setq buffer (org-edit-src-find-buffer beg end))
-	       (or (eq context 'save)
-		   (if org-src-ask-before-returning-to-edit-buffer
-		       (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? ") t)))
-	  (org-src-switch-to-buffer buffer 'return)
-	(when buffer
-	  (with-current-buffer buffer
-	    (if (boundp 'org-edit-src-overlay)
-		(delete-overlay org-edit-src-overlay)))
-	  (kill-buffer buffer))
-	(setq buffer (generate-new-buffer
-		      (or edit-buffer-name
-			  (org-src-construct-edit-buffer-name (buffer-name) lang))))
-	(setq ovl (make-overlay beg end))
-	(overlay-put ovl 'edit-buffer buffer)
-	(overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
-	(overlay-put ovl 'face 'secondary-selection)
-	(overlay-put ovl
-		     'keymap
-		     (let ((map (make-sparse-keymap)))
-		       (define-key map [mouse-1] 'org-edit-src-continue)
-		       map))
-	(overlay-put ovl :read-only "Leave me alone")
-	(setq transmitted-variables
-	      (append transmitted-variables `((org-edit-src-overlay ,ovl))))
-	(org-src-switch-to-buffer buffer 'edit)
-	(if (eq single 'macro-definition)
-	    (setq code (replace-regexp-in-string "\\\\n" "\n" code t t)))
-	(insert code)
-	(remove-text-properties (point-min) (point-max)
-				'(display nil invisible nil intangible nil))
-	(unless (cadr (assq 'org-src-preserve-indentation transmitted-variables))
-	  (setq total-nindent (or (org-do-remove-indentation) 0)))
-	(let ((org-inhibit-startup t))
-	  (condition-case e
-	      (funcall lang-f)
-	    (error
-	     (signal 'org-src-error "Language mode `%s' fails with: %S" lang-f (nth 1 e)))))
-	(dolist (pair transmitted-variables)
-	  (org-set-local (car pair) (cadr pair)))
-	;; Remove protecting commas from visible part of buffer.
-	(org-unescape-code-in-region (point-min) (point-max))
-	(when markline
-	  (org-goto-line (1+ (- markline begline)))
-	  (org-move-to-column
-	   (if org-src-preserve-indentation markcol
-	     (max 0 (- markcol total-nindent))))
-	  (push-mark (point) 'no-message t)
-	  (setq deactivate-mark nil))
-	(org-goto-line (1+ (- line begline)))
+    (setq beg (move-marker beg (nth 0 info))
+	  end (move-marker end (nth 1 info))
+	  msg (if allow-write-back-p
+		  (substitute-command-keys
+		   "Edit, then exit with C-c ' (C-c and single quote)")
+		"Exit with C-c ' (C-c and single quote)")
+	  code (or code (buffer-substring-no-properties beg end))
+	  lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
+		   (nth 2 info))
+	  lang (if (symbolp lang) (symbol-name lang) lang)
+	  single (nth 3 info)
+	  block-nindent (nth 5 info)
+	  lang-f (intern (concat lang "-mode"))
+	  begline (save-excursion (goto-char beg) (org-current-line))
+	  transmitted-variables
+	  `((org-edit-src-content-indentation
+	     ,org-edit-src-content-indentation)
+	    (org-edit-src-force-single-line ,single)
+	    (org-edit-src-from-org-mode ,org-mode-p)
+	    (org-edit-src-allow-write-back-p ,allow-write-back-p)
+	    (org-src-preserve-indentation ,org-src-preserve-indentation)
+	    (org-src-babel-info ,(org-babel-get-src-block-info 'light))
+	    (org-coderef-label-format
+	     ,(or (nth 4 info) org-coderef-label-format))
+	    (org-edit-src-beg-marker ,beg)
+	    (org-edit-src-end-marker ,end)
+	    (org-edit-src-block-indentation ,block-nindent)))
+    (if (and mark (>= mark beg) (<= mark (1+ end)))
+	(save-excursion (goto-char (min mark end))
+			(setq markline (org-current-line)
+			      markcol (current-column))))
+    (if (equal lang-f 'table.el-mode)
+	(setq lang-f (lambda ()
+		       (text-mode)
+		       (if (org-bound-and-true-p flyspell-mode)
+			   (flyspell-mode -1))
+		       (table-recognize)
+		       (org-set-local 'org-edit-src-content-indentation 0))))
+    (unless (functionp lang-f)
+      (signal 'org-src-error "No such language mode: %s" lang-f))
+    (save-excursion
+      (if (> (point) end) (goto-char end))
+      (setq line (org-current-line)
+	    col (current-column)))
+    (if (and (setq buffer (org-edit-src-find-buffer beg end))
+	     (or (eq context 'save)
+		 (if org-src-ask-before-returning-to-edit-buffer
+		     (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? ") t)))
+	(org-src-switch-to-buffer buffer 'return)
+      (when buffer
+	(with-current-buffer buffer
+	  (if (boundp 'org-edit-src-overlay)
+	      (delete-overlay org-edit-src-overlay)))
+	(kill-buffer buffer))
+      (setq buffer (generate-new-buffer
+		    (or edit-buffer-name
+			(org-src-construct-edit-buffer-name (buffer-name) lang))))
+      (setq ovl (make-overlay beg end))
+      (overlay-put ovl 'edit-buffer buffer)
+      (overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
+      (overlay-put ovl 'face 'secondary-selection)
+      (overlay-put ovl
+		   'keymap
+		   (let ((map (make-sparse-keymap)))
+		     (define-key map [mouse-1] 'org-edit-src-continue)
+		     map))
+      (overlay-put ovl :read-only "Leave me alone")
+      (setq transmitted-variables
+	    (append transmitted-variables `((org-edit-src-overlay ,ovl))))
+      (org-src-switch-to-buffer buffer 'edit)
+      (if (eq single 'macro-definition)
+	  (setq code (replace-regexp-in-string "\\\\n" "\n" code t t)))
+      (insert code)
+      (remove-text-properties (point-min) (point-max)
+			      '(display nil invisible nil intangible nil))
+      (unless (cadr (assq 'org-src-preserve-indentation transmitted-variables))
+	(setq total-nindent (or (org-do-remove-indentation) 0)))
+      (let ((org-inhibit-startup t))
+	(condition-case e
+	    (funcall lang-f)
+	  (error
+	   (signal 'org-src-error "Language mode `%s' fails with: %S" lang-f (nth 1 e)))))
+      (dolist (pair transmitted-variables)
+	(org-set-local (car pair) (cadr pair)))
+      ;; Remove protecting commas from visible part of buffer.
+      (org-unescape-code-in-region (point-min) (point-max))
+      (when markline
+	(org-goto-line (1+ (- markline begline)))
 	(org-move-to-column
-	 (if org-src-preserve-indentation col (max 0 (- col total-nindent))))
-	(org-src-mode)
-	(set-buffer-modified-p nil)
-	(setq buffer-file-name nil)
-	(and org-edit-src-persistent-message
-	     (org-set-local 'header-line-format msg))
-	(let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang))))
-	  (when (fboundp edit-prep-func)
-	    (funcall edit-prep-func full-info))))
-      t)))
+	 (if org-src-preserve-indentation markcol
+	   (max 0 (- markcol total-nindent))))
+	(push-mark (point) 'no-message t)
+	(setq deactivate-mark nil))
+      (org-goto-line (1+ (- line begline)))
+      (org-move-to-column
+       (if org-src-preserve-indentation col (max 0 (- col total-nindent))))
+      (org-src-mode)
+      (set-buffer-modified-p nil)
+      (setq buffer-file-name nil)
+      (and org-edit-src-persistent-message
+	   (org-set-local 'header-line-format msg))
+      (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang))))
+	(when (fboundp edit-prep-func)
+	  (funcall edit-prep-func full-info))))
+    t))
 
 (defun org-edit-src-continue (e)
   "Continue editing source blocks." ;; Fixme: be more accurate
-- 
1.7.11.4


  reply	other threads:[~2012-12-13 23:40 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <50A62865.8010904@gmail.com>
2012-12-07 15:36 ` bug#12905: 24.2.50; org: edit source block causes data loss Chong Yidong
     [not found] ` <87sj7han8a.fsf__16482.9143243425$1354894646$gmane$org@gnu.org>
2012-12-12 16:55   ` Bastien
2012-12-12 21:05     ` Bernt Hansen
2012-12-12 21:34       ` Jonathan Leech-Pepin
2012-12-12 21:51         ` Bernt Hansen
2012-12-12 23:18       ` Bastien
2012-12-13  3:27         ` Bernt Hansen
2012-12-13  3:37           ` Bernt Hansen
2012-12-13  4:50             ` Nick Dokos
2012-12-13 12:45               ` Bernt Hansen
2012-12-13 13:41                 ` Bernt Hansen
2012-12-13 10:36             ` Bastien
2012-12-13 13:42               ` Bernt Hansen
2012-12-13 14:36                 ` Le Wang
2012-12-13 15:17                   ` Bastien
2012-12-13 16:06                     ` Le Wang
2012-12-13 16:25                       ` Bastien
2012-12-13 23:40                         ` Le Wang [this message]
2012-12-13 23:50                           ` Le Wang
2012-12-14  9:38                           ` Bastien
2012-12-14  9:39                           ` Bastien
2012-12-16 14:39                             ` Le Wang
2012-12-14  9:40                           ` Bastien
2012-12-13 13:48               ` Bernt Hansen
2012-12-13 16:05                 ` Bastien
2012-12-14  0:04                   ` Bernt Hansen
2012-12-14  9:35                     ` Bastien
2012-12-14  9:57                     ` Le Wang
     [not found]   ` <87wqwnyzud.fsf@bzg.ath.cx>
2012-12-12 17:39     ` Andy Moreton

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+ioHiom-PNz5fV83MtA6JY1pVLG8a8YUvi08MiSUvZbyXw@mail.gmail.com' \
    --to=l26wang@gmail.com \
    --cc=bernt@norang.ca \
    --cc=bzg@altern.org \
    --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).