help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is there any elisp functions to tell whether the cursor is in a comm


From: Joe Bloggs
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
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux)

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?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]