Browse Source

Fix bug when commenting source code.

* org.el (org-babel-do-in-edit-buffer): Declare.
(org-babel-load-file, org-insert-comment)
(org-comment-or-uncomment-region): Don't require 'ob-core.
(org-mode-map): Remap `comment-dwim' to `org-comment-dwim'.
(org-comment-dwim): New command.

* ob-core.el (org-babel-do-in-edit-buffer): Make an autoload.

for a discussion about this bug.
Bastien Guerry 7 years ago
2 changed files with 12 additions and 7 deletions
  1. 1 0
  2. 11 7

+ 1 - 0

@@ -972,6 +972,7 @@ with a prefix argument then this is passed on to
     (funcall swap-windows)))
 (defmacro org-babel-do-in-edit-buffer (&rest body)
   "Evaluate BODY in edit buffer if there is a code block at point.
 Return t if a code block was found at point, nil otherwise."

+ 11 - 7

@@ -124,6 +124,7 @@ Stars are put in group 1 and the trimmed body in group 2.")
 (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
 (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
+(declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
 (declare-function orgtbl-mode "org-table" (&optional arg))
 (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
 (declare-function org-beamer-mode "ox-beamer" ())
@@ -201,7 +202,6 @@ and then loads the resulting file using `load-file'.  With prefix
 arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
 file to byte-code before it is loaded."
   (interactive "fFile to load: \nP")
-  (require 'ob-core)
   (let* ((age (lambda (file)
 		 (time-subtract (current-time)
@@ -19215,6 +19215,7 @@ boundaries."
 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
 (org-defkey org-mode-map "\C-c#"    'org-update-statistics-cookies)
 (org-defkey org-mode-map [remap open-line] 'org-open-line)
+(org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
 (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
 (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
 (org-defkey org-mode-map "\C-m"     'org-return)
@@ -20502,6 +20503,13 @@ If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
     (open-line n))))
+(defun org-comment-dwim (arg)
+  (interactive "*P")
+  "Call `comment-dwim' within a source edit buffer if needed"
+  (if (org-in-src-block-p)
+      (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim))
+    (call-interactively #'comment-dwim)))
 (defun org-return (&optional indent)
   "Goto next table row or insert a newline.
 Calls `org-table-next-row' or `newline', depending on context.
@@ -22602,9 +22610,7 @@ major mode."
 		  (skip-chars-backward " \r\t\n")
-      (progn
-	(require 'ob-core)
-	(org-babel-do-in-edit-buffer (call-interactively #'comment-dwim)))
+      (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim))
     (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
       (open-line 1))
@@ -22628,9 +22634,7 @@ strictly within a source block, use appropriate comment syntax."
 		   (skip-chars-backward " \r\t\n")
-      (progn
-	(require 'ob-core)
-	(org-babel-do-in-edit-buffer (call-interactively #'comment-dwim)))
+      (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim))
       ;; Restrict region
       (narrow-to-region (save-excursion (goto-char beg)