From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Xah Newsgroups: gmane.emacs.help Subject: Re: how to deal with comment in a new lang mode Date: Fri, 31 Oct 2008 13:04:17 -0700 (PDT) Organization: http://groups.google.com Message-ID: <78961dbc-b748-4a52-bd2d-b793b61d3212@d36g2000prf.googlegroups.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1225485672 20110 80.91.229.12 (31 Oct 2008 20:41:12 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 31 Oct 2008 20:41:12 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Oct 31 21:42:13 2008 connect(): Connection refused Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Kw0ou-0004KS-RH for geh-help-gnu-emacs@m.gmane.org; Fri, 31 Oct 2008 21:42:13 +0100 Original-Received: from localhost ([127.0.0.1]:34729 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Kw0no-0004rO-1L for geh-help-gnu-emacs@m.gmane.org; Fri, 31 Oct 2008 16:41:04 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!d36g2000prf.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.emacs Original-Lines: 139 Original-NNTP-Posting-Host: 24.6.185.159 Original-X-Trace: posting.google.com 1225483457 10911 127.0.0.1 (31 Oct 2008 20:04:17 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Fri, 31 Oct 2008 20:04:17 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: d36g2000prf.googlegroups.com; posting-host=24.6.185.159; posting-account=bRPKjQoAAACxZsR8_VPXCX27T2YcsyMA User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_4_11; en) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.22, gzip(gfe), gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:163965 comp.emacs:97300 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:59308 Archived-At: 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: =E2=80=9C;; this and that.=E2=80=9D 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 =E2=80=9Cnewcomment.el=E2=80=9D. I don't like comme= nt-dwim's behavior. Its too =E2=80=9Csmart/complex=E2=80=9D. In particular, when ther= e'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 =E2=80=9Cnewcomment.el=E2=80=9D. (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 =E2=80=9C//=E2=80=9D 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 =E2=80=9C//=E2=80=9D to the beginning of each line of selected text.= " (interactive "r") (let ((deactivate-mark nil)) (save-excursion (goto-char p2) (while (>=3D (point) p1) (xlsl-comment-current-line) (previous-line) ) ) ) ) (defun xlsl-uncomment-region (p1 p2) "Remove =E2=80=9C//=E2=80=9D in the beginning of each line of selected te= xt." (interactive "r") (let ((deactivate-mark nil)) (save-excursion (goto-char p2) (while (>=3D (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 =E2=80=9Cnewcomment.el= =E2=80=9D. PS is there a command to compact the ending parens? Xah =E2=88=91 http://xahlee.org/ =E2=98=84