From: Joe Bloggs <who@cares.invalid>
To: help-gnu-emacs@gnu.org
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 [thread overview]
Message-ID: <87abgl1c2t.fsf@DEBLAP1.BeNet> (raw)
In-Reply-To: yoijfxqdehe6.fsf@remote5.student.chalmers.se
bojohan+news@dd.chalmers.se (Johan Bockgård) writes:
> Joe Bloggs <who@cares.invalid> writes:
>
>> bojohan+news@dd.chalmers.se (Johan Bockgård) writes:
>>
>>> sunway <sunwayforever@gmail.com> 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?
next prev parent reply other threads:[~2008-07-14 1:36 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-07-13 5:06 Is there any elisp functions to tell whether the cursor is in a comment block? sunway
2008-07-13 9:53 ` Johan Bockgård
2008-07-13 17:55 ` Joe Bloggs
2008-07-13 19:03 ` Johan Bockgård
2008-07-14 1:36 ` Joe Bloggs [this message]
2008-07-14 1:49 ` Johan Bockgård
2008-07-14 1:36 ` sunway
2008-07-14 15:05 ` Joe Bloggs
2008-07-14 15:44 ` Lennart Borgman (gmail)
[not found] ` <mailman.14707.1216050306.18990.help-gnu-emacs@gnu.org>
2008-07-14 20:10 ` Joe Bloggs
2008-07-14 23:20 ` Lennart Borgman (gmail)
2008-07-14 23:43 ` Drew Adams
2008-07-14 23:49 ` Lennart Borgman (gmail)
2008-07-15 6:02 ` Drew Adams
[not found] ` <mailman.14727.1216079418.18990.help-gnu-emacs@gnu.org>
2008-07-15 22:27 ` David Kastrup
2008-07-20 13:59 ` Eli Zaretskii
2008-07-15 1:27 ` Tom Tromey
2008-07-15 7:31 ` Lennart Borgman (gmail)
2008-07-15 14:15 ` Drew Adams
2008-07-15 20:50 ` Lennart Borgman (gmail)
2008-07-15 21:21 ` Drew Adams
2008-07-15 21:34 ` Lennart Borgman (gmail)
2008-07-15 22:06 ` Drew Adams
2008-07-15 22:19 ` Lennart Borgman (gmail)
2008-07-15 22:32 ` Drew Adams
2008-07-15 22:47 ` Lennart Borgman (gmail)
2008-07-15 22:53 ` Drew Adams
[not found] ` <mailman.14800.1216160423.18990.help-gnu-emacs@gnu.org>
2008-07-17 17:40 ` Giorgos Keramidas
2008-07-17 17:30 ` Giorgos Keramidas
2008-07-19 15:17 ` Joe Bloggs
2008-07-20 0:10 ` Giorgos Keramidas
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87abgl1c2t.fsf@DEBLAP1.BeNet \
--to=who@cares.invalid \
--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.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.