[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how to deal with comment in a new lang mode
From: |
Xah |
Subject: |
Re: how to deal with comment in a new lang mode |
Date: |
Fri, 31 Oct 2008 13:04:17 -0700 (PDT) |
User-agent: |
G2/1.0 |
Xah Lee wrote:
> > when writing a new language mode, how can one make comment-dwim work?
>
> > i can of course code my own functions dealing with comments, but i
> > think it is better to use the facilities provided in newcomment.el?
> > ...
> > any advice apprecated for dealing with comments in a new lang mode.
Kevin Rodgers wrote:
> I can't address your question, but I can suggest a minor simplification
> of your current implementation:
>
> (defun xlsl-comment-dwim (arg)
> (interactive "*P")
> (let ((comment-start "// ")
> (comment-end ""))
> (comment-dwim arg)))
>
> This is also has the advantage of restoring the original binding of
> the comment-start and comment-end variables, even if comment-dwim
> signals an error.
Thanks, it did help.
i haven't tested extensively, but looks like the above is all i need.
My previous problem of comment-dwim not doing any commenting/
uncommenting when on a line such as:
“;; this and that.”
while the cursor on the line, is the consistant behavior of comment-
dwim.
Not sure i liked the behavior of comment-dwim... i haven't decided
yet, but it seems too smart or quite complex. For now i decided to
experiment since it's not too difficult.
;; implementation using “newcomment.el”. I don't like comment-dwim's
behavior. Its too “smart/complex”. In particular, when there's no
active region and current line is a comment, it doesn't do anything
except moving cursor.
;; (defun xlsl-comment-dwim (arg)
;; (interactive "*P")
;; ;; (require 'newcomment)
;; (let ((comment-start "// ")
;; (comment-end ""))
;; (comment-dwim arg)))
;; implementation not using “newcomment.el”.
(defun xlsl-comment-dwim ()
"Comment or uncomment the current line or text selection."
(interactive)
;; If there's no text selection, comment or uncomment the line
depending whether the WHOLE line is a comment. If there is a text
selection, using the first line to determine whether to comment/
uncomment.
(let (p1 p2)
(if (and transient-mark-mode mark-active)
(save-excursion
(setq p1 (region-beginning) p2 (region-end))
(goto-char p1)
(if (wholeLineIsCmt-p)
(xlsl-uncomment-region p1 p2)
(xlsl-comment-region p1 p2)
))
(progn
(if (wholeLineIsCmt-p)
(xlsl-uncomment-current-line)
(xlsl-comment-current-line)
))
)
))
(defun wholeLineIsCmt-p ()
(save-excursion
(move-beginning-of-line 1)
(looking-at "[ \t]*//")
)
)
(defun xlsl-comment-current-line ()
(interactive)
(move-beginning-of-line 1)
(insert "//")
)
(defun xlsl-uncomment-current-line ()
"Remove the string “//” in the beginning of current line."
(interactive)
(when (wholeLineIsCmt-p)
(move-beginning-of-line 1)
(search-forward "//")
(delete-backward-char 2)
)
)
(defun xlsl-comment-region (p1 p2)
"Add “//” to the beginning of each line of selected text."
(interactive "r")
(let ((deactivate-mark nil))
(save-excursion
(goto-char p2)
(while (>= (point) p1)
(xlsl-comment-current-line)
(previous-line)
)
)
)
)
(defun xlsl-uncomment-region (p1 p2)
"Remove “//” in the beginning of each line of selected text."
(interactive "r")
(let ((deactivate-mark nil))
(save-excursion
(goto-char p2)
(while (>= (point) p1)
(xlsl-uncomment-current-line)
(previous-line)
)
)
)
)
took me about 3 hours. Coding this i realized there's some details,
more complex than i originally thought but still not much work.
Possibly down the road i'll go back to basing it on “newcomment.el”.
PS is there a command to compact the ending parens?
Xah
∑ http://xahlee.org/
☄