* Continuing existing comment, with proper identation
@ 2023-10-21 21:57 Spencer Baugh
2023-10-22 10:38 ` Nikolay Kudryavtsev
0 siblings, 1 reply; 2+ messages in thread
From: Spencer Baugh @ 2023-10-21 21:57 UTC (permalink / raw)
To: help-gnu-emacs
I'm writing a function to manipulate an existing commnet. It should
insert a blank line, and then some data, at the end of a comment,
without creating a new comment. I want it to work in a
major-mode-independent way, but I'm having a bit of trouble figuring out
what functions to call to get the same behavior across major-mode.
In Lisp, I'd like:
;; ...existing comment...
;; ...existing comment...
to be turned into
;; ...existing comment...
;; ...existing comment...
;;
;; new data
and in C:
/* ...existing comment...
...existing comment... */
to be turned into
/* ...existing comment...
...existing comment...
new data */
(Keeping the same indentation level as the previous lines, of course, if
it's already in an indented block.)
The obvious thing to do is to put point at the end of the comment and
comment-indent-new-line twice.
But this behaves differently in Lisp and C - it's a one character
difference, but that's enough to break my major-mode-independence.
In C, I get
/* ...existing comment...
...existing comment...
<point> */
so I can just go ahead and (insert "new data")
In Lisp though, I get:
;; ...existing comment...
;; ...existing comment...
;;
;;<point>
Note the lack of a space before <point>. So if I (insert "new data"), I get
;; ...existing comment...
;; ...existing comment...
;;
;;new data
which is not what I want. I want a space before ";;" and "new data", as
is normal in Lisp.
What should I do to get the behavior that I see in C, in Lisp
major-modes?
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Continuing existing comment, with proper identation
2023-10-21 21:57 Continuing existing comment, with proper identation Spencer Baugh
@ 2023-10-22 10:38 ` Nikolay Kudryavtsev
0 siblings, 0 replies; 2+ messages in thread
From: Nikolay Kudryavtsev @ 2023-10-22 10:38 UTC (permalink / raw)
To: Spencer Baugh, help-gnu-emacs
Honestly, your best bet is probably implementing literally all of the
logic yourself, maybe reusing some code from whatever mode you're
willing to support.
In a perfect world Emacs would understand comments as proper syntax tree
expressions and have some kind of high level API to deal with those. But
it does not and there isn't one.
Also, extending comments is not your only problem. Indentation is a
factor too. Again, there's no unified API. Some major modes use homebrew
indentation logic, few use SMIE, now there's tree-sitter too.
For example elisp-mode won't indent comments:
;; The next line should indent up to here(according to the OP
rules)
;; But it won't!
Then there's also the elephant in the room - comments can be code too.
Lets say you want to comment out a very basic C-like if:
/* if(){
* whatever
*/
How would your code know that offset is warranted here? You'd have to
run the major mode's normal indentation logic to see it, right? That's
why code that comments out stuff generally tries to avoid messing with
indentation.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-10-22 10:38 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-21 21:57 Continuing existing comment, with proper identation Spencer Baugh
2023-10-22 10:38 ` Nikolay Kudryavtsev
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).