unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Xah <xahlee@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: how to deal with comment in a new lang mode
Date: Fri, 31 Oct 2008 13:04:17 -0700 (PDT)	[thread overview]
Message-ID: <78961dbc-b748-4a52-bd2d-b793b61d3212@d36g2000prf.googlegroups.com> (raw)
In-Reply-To: mailman.2542.1225421198.25473.help-gnu-emacs@gnu.org


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/

  parent reply	other threads:[~2008-10-31 20:04 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-30 20:59 how to deal with comment in a new lang mode Xah
2008-10-31  2:46 ` Kevin Rodgers
2008-10-31 14:10 ` rgb
     [not found] ` <mailman.2542.1225421198.25473.help-gnu-emacs@gnu.org>
2008-10-31 20:04   ` Xah [this message]
2008-11-01 12:53     ` Rupert Swarbrick
2008-11-01 13:47     ` Kevin Rodgers
     [not found]     ` <mailman.2630.1225547412.25473.help-gnu-emacs@gnu.org>
2008-11-01 14:41       ` Xah
2008-11-01 15:24         ` Andreas Politz
2008-11-01 20:55           ` Xah
2008-11-01 21:23             ` Andreas Politz
2008-11-01 21:48               ` Xah
2008-11-01 18:59         ` Rupert Swarbrick
2008-11-03 14:08     ` rgb
2008-11-03 14:19     ` rgb
2008-11-03 15:02       ` Seweryn Kokot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=78961dbc-b748-4a52-bd2d-b793b61d3212@d36g2000prf.googlegroups.com \
    --to=xahlee@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).