Browse Source

org-src: Fix org-edit-src-code for empty blocks and block with just an empty line

* lisp/org-src.el (org-edit-src-code): Use marker with insertion type
t to track end and remove hack requiring delete from beg to (1- end).
Le Wang 5 years ago
parent
commit
8d22b11978
1 changed files with 15 additions and 5 deletions
  1. 15 5
      lisp/org-src.el

+ 15 - 5
lisp/org-src.el

@@ -215,11 +215,22 @@ buffer."
     (setq org-edit-src-saved-temp-window-config (current-window-configuration)))
   (let* ((mark (and (org-region-active-p) (mark)))
 	 (case-fold-search t)
-	 (info (org-edit-src-find-region-and-lang))
+	 (info
+	  ;; If the src region consists in no lines, we insert a blank
+	  ;; line.
+	  (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))))
 	 (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))
-	 (end (make-marker))
+	 ;; Move marker with inserted text for case when src block is
+	 ;; just one empty line, i.e. beg == end.
+	 (end (copy-marker nil t))
 	 (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)
@@ -689,10 +700,9 @@ with \",*\", \",#+\", \",,*\" and \",,#+\"."
       (kill-buffer buffer))
     (goto-char beg)
     (when allow-write-back-p
-      (delete-region beg (max beg (1- end)))
+      (delete-region beg (max beg end))
       (unless (string-match "\\`[ \t]*\\'" code)
-	(insert code)
-	(delete-char 1))
+	(insert code))
       (goto-char beg)
       (if single (just-one-space)))
     (if (memq t (mapcar (lambda (overlay)