Pārlūkot izejas kodu

Small refactoring in comment functions

* lisp/org.el (org-insert-comment, org-comment-or-uncomment-region):
  Use `org-element-at-point' instead of `org-in-src-block-p' to check
  if we're within a source block.
* testing/lisp/test-org.el: Add test.
Nicolas Goaziou 11 gadi atpakaļ
vecāks
revīzija
28f4b89830
2 mainītis faili ar 99 papildinājumiem un 71 dzēšanām
  1. 80 70
      lisp/org.el
  2. 19 1
      testing/lisp/test-org.el

+ 80 - 70
lisp/org.el

@@ -22496,15 +22496,25 @@ non-nil."
 
 (defun org-insert-comment ()
   "Insert an empty comment above current line.
-If the line is empty, insert comment at its beginning."
-  (if (org-in-src-block-p t)
+If the line is empty, insert comment at its beginning.  When
+point is within a source block, comment according to the related
+major mode."
+  (if (let ((element (org-element-at-point)))
+	(and (eq (org-element-type element) 'src-block)
+	     (< (save-excursion
+		  (goto-char (org-element-property :post-affiliated element))
+		  (line-end-position))
+		(point))
+	     (> (save-excursion
+		  (goto-char (org-element-property :end element))
+		  (skip-chars-backward " \r\t\n")
+		  (line-beginning-position))
+		(point))))
       (progn
 	(require 'ob-core)
-	(org-babel-do-in-edit-buffer
-	 (call-interactively #'comment-dwim)))
+	(org-babel-do-in-edit-buffer (call-interactively #'comment-dwim)))
     (beginning-of-line)
-    (if (looking-at "\\s-*$")
-	(delete-region (point) (point-at-eol))
+    (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
       (open-line 1))
     (org-indent-line)
     (insert "# ")))
@@ -22513,70 +22523,70 @@ If the line is empty, insert comment at its beginning."
 (defun org-comment-or-uncomment-region (beg end &rest ignore)
   "Comment or uncomment each non-blank line in the region.
 Uncomment each non-blank line between BEG and END if it only
-contains commented lines.  Otherwise, comment them."
-  (let* ((pt (point-marker))
-	 (head (and (org-in-src-block-p t)
-		    (require 'ob-core)
-		    (org-babel-where-is-src-block-head)))
-	 (foot (and head
-		    (save-excursion
-		      (goto-char head)
-		      (looking-at org-babel-src-block-regexp)
-		      (goto-char (match-end 0))
-		      (point-at-bol)))))
-    (if (and head foot
-	     (> beg head)
-	     (< end foot))
-	(org-babel-do-in-edit-buffer
-	 (call-interactively #'comment-dwim))
-      (save-restriction
-	;; Restrict region
-	(narrow-to-region (save-excursion (goto-char beg)
-					  (skip-chars-forward " \r\t\n" end)
-					  (line-beginning-position))
-			  (save-excursion (goto-char end)
-					  (skip-chars-backward " \r\t\n" beg)
-					  (line-end-position)))
-	(let ((uncommentp
-	       ;; UNCOMMENTP is non-nil when every non blank line between
-	       ;; BEG and END is a comment.
-	       (save-excursion
-		 (goto-char (point-min))
-		 (while (and (not (eobp))
-			     (let ((element (org-element-at-point)))
-			       (and (eq (org-element-type element) 'comment)
-				    (goto-char (min (point-max)
-						    (org-element-property
-						     :end element)))))))
-		 (eobp))))
-	  (if uncommentp
-	      ;; Only blank lines and comments in region: uncomment it.
-	      (save-excursion
-		(goto-char (point-min))
-		(while (not (eobp))
-		  (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
-		    (replace-match "" nil nil nil 1))
-		  (forward-line)))
-	    ;; Comment each line in region.
-	    (let ((min-indent (point-max)))
-	      ;; First find the minimum indentation across all lines.
-	      (save-excursion
-		(goto-char (point-min))
-		(while (and (not (eobp)) (not (zerop min-indent)))
-		  (unless (looking-at "[ \t]*$")
-		    (setq min-indent (min min-indent (current-indentation))))
-		  (forward-line)))
-	      ;; Then loop over all lines.
-	      (save-excursion
-		(goto-char (point-min))
-		(while (not (eobp))
-		  (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
-		    ;; Don't get fooled by invisible text (e.g. link path)
-		    ;; when moving to column MIN-INDENT.
-		    (let ((buffer-invisibility-spec nil))
-		      (org-move-to-column min-indent t))
-		    (insert comment-start))
-		  (forward-line))))))))))
+contains commented lines.  Otherwise, comment them.  If region is
+strictly within a source block, use appropriate comment syntax."
+  (if (let ((element (org-element-at-point)))
+	(and (eq (org-element-type element) 'src-block)
+	     (< (save-excursion
+		  (goto-char (org-element-property :post-affiliated element))
+		  (line-end-position))
+		beg)
+	     (>= (save-excursion
+		   (goto-char (org-element-property :end element))
+		   (skip-chars-backward " \r\t\n")
+		   (line-beginning-position))
+		 end)))
+      (progn
+	(require 'ob-core)
+	(org-babel-do-in-edit-buffer (call-interactively #'comment-dwim)))
+    (save-restriction
+      ;; Restrict region
+      (narrow-to-region (save-excursion (goto-char beg)
+					(skip-chars-forward " \r\t\n" end)
+					(line-beginning-position))
+			(save-excursion (goto-char end)
+					(skip-chars-backward " \r\t\n" beg)
+					(line-end-position)))
+      (let ((uncommentp
+	     ;; UNCOMMENTP is non-nil when every non blank line between
+	     ;; BEG and END is a comment.
+	     (save-excursion
+	       (goto-char (point-min))
+	       (while (and (not (eobp))
+			   (let ((element (org-element-at-point)))
+			     (and (eq (org-element-type element) 'comment)
+				  (goto-char (min (point-max)
+						  (org-element-property
+						   :end element)))))))
+	       (eobp))))
+	(if uncommentp
+	    ;; Only blank lines and comments in region: uncomment it.
+	    (save-excursion
+	      (goto-char (point-min))
+	      (while (not (eobp))
+		(when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
+		  (replace-match "" nil nil nil 1))
+		(forward-line)))
+	  ;; Comment each line in region.
+	  (let ((min-indent (point-max)))
+	    ;; First find the minimum indentation across all lines.
+	    (save-excursion
+	      (goto-char (point-min))
+	      (while (and (not (eobp)) (not (zerop min-indent)))
+		(unless (looking-at "[ \t]*$")
+		  (setq min-indent (min min-indent (current-indentation))))
+		(forward-line)))
+	    ;; Then loop over all lines.
+	    (save-excursion
+	      (goto-char (point-min))
+	      (while (not (eobp))
+		(unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
+		  ;; Don't get fooled by invisible text (e.g. link path)
+		  ;; when moving to column MIN-INDENT.
+		  (let ((buffer-invisibility-spec nil))
+		    (org-move-to-column min-indent t))
+		  (insert comment-start))
+		(forward-line)))))))))
 
 
 ;;; Planning

+ 19 - 1
testing/lisp/test-org.el

@@ -96,7 +96,25 @@
    (equal "# \n#+KEYWORD: value"
 	  (org-test-with-temp-text "#+KEYWORD: value"
 	    (progn (call-interactively 'comment-dwim)
-		   (buffer-string))))))
+		   (buffer-string)))))
+  ;; In a source block, use appropriate syntax.
+  (should
+   (equal "  ;; "
+	  (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
+	    (forward-line)
+	    (let ((org-edit-src-content-indentation 2))
+	      (call-interactively 'comment-dwim))
+	    (buffer-substring-no-properties (line-beginning-position) (point)))))
+  (should
+   (equal "#+BEGIN_SRC emacs-lisp\n  ;; a\n  ;; b\n#+END_SRC"
+	  (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
+	    (forward-line)
+	    (transient-mark-mode 1)
+	    (push-mark (point) t t)
+	    (forward-line 2)
+	    (let ((org-edit-src-content-indentation 2))
+	      (call-interactively 'comment-dwim))
+	    (buffer-string)))))