* Comments within Org src block move point unexpectedly
@ 2021-04-09 13:13 excalamus--- via Users list for the GNU Emacs text editor
2021-04-09 13:27 ` excalamus--- via Users list for the GNU Emacs text editor
2021-04-09 14:27 ` Jean Louis
0 siblings, 2 replies; 5+ messages in thread
From: excalamus--- via Users list for the GNU Emacs text editor @ 2021-04-09 13:13 UTC (permalink / raw)
To: Help Gnu Emacs
The usual behavior of comment-line is to move point to the next line.When commenting code within an Org source block, however, the point will jump to some previous line.
For example, say that point is at |:
#+begin_src emacs-lisp (defun hello () "Say hi." (interactive)| (message "Hello, world!"))#+end_src
When comment-line is called, the current line is commented, but point also moves to the indent of the previous line:
#+begin_src emacs-lisp (defun hello () |"Say hi." ;; (interactive) (message "Hello, world!"))#+end_src
The point will jump near the top of the block when the block contains more code (i.e. will jump entire screen heights).
Ideally, I would like point to stay put (relative to the adjacent characters prior to (un)commenting). I would at least expect comment-line to behave in a source block like it does outside of one.
I've tried a handful of related solutions (see links below). They all move point similarly. It looks like comment-or-uncomment-region is the typical entry point. AFAIK, it ultimately hinges on the comment-region-function which is comment-region-default-1 by default. Stepping through, it's not clear to me which part moves point to a previous line. Does anyone have some insight?
https://stackoverflow.com/questions/9688748/emacs-comment-uncomment-current-line
https://stackoverflow.com/questions/20041904/eclipse-like-line-commenting-in-emacs/20064658#20064658
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Comments within Org src block move point unexpectedly
2021-04-09 13:13 Comments within Org src block move point unexpectedly excalamus--- via Users list for the GNU Emacs text editor
@ 2021-04-09 13:27 ` excalamus--- via Users list for the GNU Emacs text editor
2021-04-09 14:27 ` Jean Louis
1 sibling, 0 replies; 5+ messages in thread
From: excalamus--- via Users list for the GNU Emacs text editor @ 2021-04-09 13:27 UTC (permalink / raw)
To: Help Gnu Emacs
Sigh, sorry about the formatting...
Also, make sure org-mode is enabled.
Toggle comment-line with point after 'interactive':
#+begin_src emacs-lisp
(defun hello ()
"Say hi."
(interactive)|
(message "Hello, world!"))
#+end_src
Point jumps to a previous line ("Say hi"):
#+begin_src emacs-lisp
(defun hello ()
|"Say hi."
(interactive)
(message "Hello, world!"))
#+end_src
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Comments within Org src block move point unexpectedly
2021-04-09 13:13 Comments within Org src block move point unexpectedly excalamus--- via Users list for the GNU Emacs text editor
2021-04-09 13:27 ` excalamus--- via Users list for the GNU Emacs text editor
@ 2021-04-09 14:27 ` Jean Louis
2021-04-10 20:31 ` excalamus--- via Users list for the GNU Emacs text editor
1 sibling, 1 reply; 5+ messages in thread
From: Jean Louis @ 2021-04-09 14:27 UTC (permalink / raw)
To: excalamus; +Cc: Help Gnu Emacs
* excalamus--- via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2021-04-09 16:14]:
>
> The usual behavior of comment-line is to move point to the next line.When commenting code within an Org source block, however, the point will jump to some previous line.
>
>
> For example, say that point is at |:
>
> #+begin_src emacs-lisp (defun hello () "Say hi." (interactive)| (message "Hello, world!"))#+end_src
Well, isn't your formatting somehow misaligned?
I have placed this in Org:
#+begin_src emacs-lisp
(defun hello ()
"Say hi."
(interactive)
(message "Hello, world!"))
#+end_src
Then I have marked the Lisp inside and have press TAB, here is what I get:
#+begin_src emacs-lisp
(defun hello ()
"Say hi."
(interactive)
(message "Hello, world!"))
#+end_src
Then if I wish to comment out some line I mark it first, like on (interactive) and press M-;
#+begin_src emacs-lisp
(defun hello ()
"Say hi."
;; (interactive)
(message "Hello, world!"))
#+end_src
And if I call M-x comment-line with cursor after (interactive), cursor jumps up on the "S" in Say hi.
#+begin_src emacs-lisp
(defun hello ()
"Say hi."
;; (interactive)
(message "Hello, world!"))
#+end_src
> When comment-line is called, the current line is commented, but
> point also moves to the indent of the previous line:
But if I put cursor after () and do M-x comment-line, it jumps down to first quote of "Say".
#+begin_src emacs-lisp
;; (defun hello ()
"Say hi."
;; (interactive)
(message "Hello, world!"))
#+end_src
> The point will jump near the top of the block when the block
> contains more code (i.e. will jump entire screen heights).
That I cannot replicate.
> Ideally, I would like point to stay put (relative to the adjacent
> characters prior to (un)commenting). I would at least expect
> comment-line to behave in a source block like it does outside of
> one.
Try this:
(defun my-comment-line ()
(interactive)
(let ((point (point)))
(comment-region (point-at-bol) (point-at-eol))))
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
Sign an open letter in support of Richard M. Stallman
https://rms-support-letter.github.io/
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Comments within Org src block move point unexpectedly
2021-04-09 14:27 ` Jean Louis
@ 2021-04-10 20:31 ` excalamus--- via Users list for the GNU Emacs text editor
2021-04-10 21:34 ` Jean Louis
0 siblings, 1 reply; 5+ messages in thread
From: excalamus--- via Users list for the GNU Emacs text editor @ 2021-04-10 20:31 UTC (permalink / raw)
To: Jean Louis; +Cc: Help Gnu Emacs
Jean,
> (defun my-comment-line ()
> (interactive)
> (let ((point (point)))
> (comment-region (point-at-bol) (point-at-eol))))
>
Thank you for your reply. My apologies about the formatting. I noticed that the code appeared scrambled and quickly sent a follow-up to correct that. I'm sorry you didn't see that and had to correct it manually!
Unfortunately, your suggestion produces the same jumping behavior. I believe this is because it relies on comment-region which is also called by comment-line (comment-line does basically the exact same thing you suggest). It looks like the behavior occurs somewhere within comment-region-default-1 .
I'm surprised to hear that comment-line, and that the my-comment-line, function do not produce the jumping behavior. When I asked on emacs.stackexchange, it was verified by another. I have reproduced the behavior with emacs --no-init on both Windows (GNU Emacs 27.1 (build (1, x86_64-mingw32) of 2020-08-21
) and GNU Guix (GNU Emacs 27.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.24, cairo version 1.16.0).
Are you executing it with org-mode enabled? (I failed to mention that in my original email and specified it in my follow up. Please excuse me!)
What version of Emacs are you using?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Comments within Org src block move point unexpectedly
2021-04-10 20:31 ` excalamus--- via Users list for the GNU Emacs text editor
@ 2021-04-10 21:34 ` Jean Louis
0 siblings, 0 replies; 5+ messages in thread
From: Jean Louis @ 2021-04-10 21:34 UTC (permalink / raw)
To: excalamus; +Cc: Help Gnu Emacs
* excalamus@tutanota.com <excalamus@tutanota.com> [2021-04-10 23:31]:
(defun my-comment-line ()
(interactive)
(comment-region (point-at-bol) (point-at-eol)))
It should be as above. To me it just comments the line and cursor
remains where it is on specific letter. I am using Emacs development version.
> Are you executing it with org-mode enabled? (I failed to mention that in my original email and specified it in my follow up. Please excuse me!)
Good question, in Org mode it does not work as expected, cursor
jumps. But then, I have this workaround:
(defun my-comment-line ()
(interactive)
(major-mode-suspend)
(emacs-lisp-mode)
(comment-region (point-at-bol) (point-at-eol))
(major-mode-restore))
As that way, when you are in Org mode and wish to comment Emacs Lisp
mode, Org mode will be suspended, you finish commenting, and it will
be restored in a breeze withou cursor jumping.
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
Sign an open letter in support of Richard M. Stallman
https://rms-support-letter.github.io/
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2021-04-10 21:34 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-09 13:13 Comments within Org src block move point unexpectedly excalamus--- via Users list for the GNU Emacs text editor
2021-04-09 13:27 ` excalamus--- via Users list for the GNU Emacs text editor
2021-04-09 14:27 ` Jean Louis
2021-04-10 20:31 ` excalamus--- via Users list for the GNU Emacs text editor
2021-04-10 21:34 ` Jean Louis
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).