From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Joe Bloggs Newsgroups: gmane.emacs.help Subject: Re: Is there any elisp functions to tell whether the cursor is in a comment block? Date: Mon, 14 Jul 2008 02:36:26 +0100 Message-ID: <87abgl1c2t.fsf@DEBLAP1.BeNet> References: <87tzetis7w.fsf@DEBLAP1.BeNet> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1216043300 19185 80.91.229.12 (14 Jul 2008 13:48:20 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 14 Jul 2008 13:48:20 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Jul 14 15:49:08 2008 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 1KIOQ4-00034M-62 for geh-help-gnu-emacs@m.gmane.org; Mon, 14 Jul 2008 15:48:48 +0200 Original-Received: from localhost ([127.0.0.1]:41364 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KIOPB-0007ZF-0i for geh-help-gnu-emacs@m.gmane.org; Mon, 14 Jul 2008 09:47:53 -0400 Original-Path: news.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local02.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail Original-NNTP-Posting-Date: Sun, 13 Jul 2008 20:36:26 -0500 Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:888q+llYMF+1DR5fxp8NgJVQGMs= Original-Lines: 57 X-Usenet-Provider: http://www.giganews.com Original-X-Trace: sv3-u4bEHe6jI24gjQuCDosx1rJLd3Nwn9WXmgzS6Ek+WTlglpVhAB3j0uiwDd9T7llstDJPelOD5pGvJlM!EtHNUElD4mU9zheQtS9nMY25NuQ5j99FkCivvtlqeWvMLB/qWX0k Original-X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.39 Original-Xref: news.stanford.edu gnu.emacs.help:160176 X-Mailman-Approved-At: Mon, 14 Jul 2008 09:47:30 -0400 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:55551 Archived-At: bojohan+news@dd.chalmers.se (Johan Bockgård) writes: > Joe Bloggs writes: > >> bojohan+news@dd.chalmers.se (Johan Bockgård) writes: >> >>> sunway writes: >>> >>>> for example, if the cursor is in a normal c code block, I want 'if ' >>>> to be expanded to 'if () {}', if it is in the comment block, I prefer >>>> 'if ' not be expanded. >>> >>> ;; Non-nil when inside comment or string >>> (nth 8 (syntax-ppss (point))) >>> >>> -- >>> Johan Bockgård >> >> I want to do that too, how do I do a conditional abbrev? >> Currently I have: >> >> (define-abbrev c-mode-abbrev-table "for" >> "" 'c-style-for-loop) >> (define-abbrev c++-mode-abbrev-table "for" >> "" 'c-style-for-loop) >> >> how would I make it only expand in uncommented code? > > > ;; Emacs 22 has `looking-back' and `syntax-ppss'. > ;; Emacs 23 has much more powerful abbrevs; we could simply use the > ;; `:enable-function' property. > > (defmacro define-expander (name predicate expander) > `(progn (put ',name 'no-self-insert t) > (defun ,name () > (when (and ,predicate > (re-search-backward "\\<\\w+\\=" nil t)) > (delete-region (match-beginning 0) (match-end 0)) > ,expander > t)))) > > (define-expander > FOR-LOOP > (not (nth 8 (parse-partial-sexp > (save-excursion (beginning-of-defun) (point)) > (point)))) > (c-style-for-loop)) > > > (define-abbrev c-mode-abbrev-table "for" t 'FOR-LOOP) > > > -- > Johan Bockgård any possibilities with emacs 21?