In Org one can use Org Source blocks to illustrate Org Syntax, as
follows:
#+begin_src org
* Test
** Test 2
#+end_src
If one invokes `org-comment-dwim` on the source code, the text is
commented out, as follows:
#+begin_src org
# * Test
# ** COMMENT Test 2
#+end_src
A second invocation adds the necessary escapes:
#+begin_src org
,* Test
,** COMMENT Test 2
#+end_src
However, to re-invoke `org-comment-dwim` one has to then reselect the
text. Is this by design?
It would be helpful if selection remains as in this sample code, which
could be better implemented I am sure:
```
(defun org-comment-dwim-keep-selection (_arg)
"Call the comment command while preserving selection.
Call `org-toggle-comment' if on a heading, otherwise call
`comment-dwim', within a source edit buffer if needed."
(interactive "*P")
(let ((region-active (region-active-p))
(rbeg (when (region-active-p) (region-beginning)))
(rend (when (region-active-p) (region-end))))
;; Call comment function
(cond ((org-at-heading-p)
(call-interactively #'org-toggle-comment))
((org-in-src-block-p)
(org-babel-do-in-edit-buffer (call-interactively #'comment-dwim)))
(t (call-interactively #'comment-dwim)))
;; Simply restore the exact original selection
(when (and region-active rbeg rend)
(set-mark rbeg)
(goto-char rend)
(setq deactivate-mark nil))))
(define-key org-mode-map (kbd "M-;") 'org-comment-dwim-keep-selection)
```
--
Raoul Comninos