all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* abbrevs, skeletons, and comments
@ 2007-06-02 14:34 nobrowser
  2007-06-03  3:24 ` Tim X
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: nobrowser @ 2007-06-02 14:34 UTC (permalink / raw
  To: help-gnu-emacs

In a module I'm writing, I'd like to bind abbrevs to code skeletons,
similar to what is done e.g. by sml-mode.  But expanding the abbrevs
makes no sense when the point is inside a comment (or a string
literal).  So I'd like to conditionally stop expansion.  The main
opportunity for that seems to be pre-abbrev-expand-hook, but there are
only two ways it can stop an expansion: 1, throw an error, 2, change
the abbrev bindings (either by making changes in the current table or
by swapping in a whole new table). 1 is unacceptable - how many errors
before user gets mad and disables abbrevs altogether? 2, how to undo
the changes when the point leaves the comment or string?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: abbrevs, skeletons, and comments
  2007-06-02 14:34 abbrevs, skeletons, and comments nobrowser
@ 2007-06-03  3:24 ` Tim X
  2007-06-03  4:44   ` nobrowser
  2007-06-11  9:20 ` Johan Bockgård
  2007-06-12 13:50 ` Stefan Monnier
  2 siblings, 1 reply; 9+ messages in thread
From: Tim X @ 2007-06-03  3:24 UTC (permalink / raw
  To: help-gnu-emacs

nobrowser@gmail.com writes:

> In a module I'm writing, I'd like to bind abbrevs to code skeletons,
> similar to what is done e.g. by sml-mode.  But expanding the abbrevs
> makes no sense when the point is inside a comment (or a string
> literal).  So I'd like to conditionally stop expansion.  The main
> opportunity for that seems to be pre-abbrev-expand-hook, but there are
> only two ways it can stop an expansion: 1, throw an error, 2, change
> the abbrev bindings (either by making changes in the current table or
> by swapping in a whole new table). 1 is unacceptable - how many errors
> before user gets mad and disables abbrevs altogether? 2, how to undo
> the changes when the point leaves the comment or string?
>

maybe use an error handler that catches that specific error and ignores it?

Tim
-- 
tcross (at) rapttech dot com dot au

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: abbrevs, skeletons, and comments
  2007-06-03  3:24 ` Tim X
@ 2007-06-03  4:44   ` nobrowser
  2007-06-03 10:32     ` Tim X
  2007-06-03 11:43     ` Andreas Roehler
  0 siblings, 2 replies; 9+ messages in thread
From: nobrowser @ 2007-06-03  4:44 UTC (permalink / raw
  To: help-gnu-emacs


Tim X wrote:
> nobrowser@gmail.com writes:
>
> > In a module I'm writing, I'd like to bind abbrevs to code skeletons,
> > similar to what is done e.g. by sml-mode.  But expanding the abbrevs
> > makes no sense when the point is inside a comment (or a string
> > literal).  So I'd like to conditionally stop expansion.  The main
> > opportunity for that seems to be pre-abbrev-expand-hook, but there are
> > only two ways it can stop an expansion: 1, throw an error, 2, change
> > the abbrev bindings (either by making changes in the current table or
> > by swapping in a whole new table). 1 is unacceptable - how many errors
> > before user gets mad and disables abbrevs altogether? 2, how to undo
> > the changes when the point leaves the comment or string?
> >
>
> maybe use an error handler that catches that specific error and ignores it?
>

Well, there's no point where I get control, that's the problem.
Abbrev expansion can be triggered just by the user typing a space or
period, if they have Abbrev minor mode on.

I see a way to do what I want.  It involves hooking both pre-abbrev-
expand-hook and post-command-hook.  I can restore the abbrev bindings
in the latter.  But I am afraid what this could do to Emacs speed or
even stability.

What I've decided on so far is I won't do anything by default (so
users who avoid Abbrev minor mode are not penalized) and have a
customize flag that turns on the above solution.

Other suggestions still welcome.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: abbrevs, skeletons, and comments
  2007-06-03  4:44   ` nobrowser
@ 2007-06-03 10:32     ` Tim X
  2007-06-03 13:31       ` nobrowser
  2007-06-03 11:43     ` Andreas Roehler
  1 sibling, 1 reply; 9+ messages in thread
From: Tim X @ 2007-06-03 10:32 UTC (permalink / raw
  To: help-gnu-emacs

nobrowser@gmail.com writes:

> Tim X wrote:
>> nobrowser@gmail.com writes:
>>
>> > In a module I'm writing, I'd like to bind abbrevs to code skeletons,
>> > similar to what is done e.g. by sml-mode.  But expanding the abbrevs
>> > makes no sense when the point is inside a comment (or a string
>> > literal).  So I'd like to conditionally stop expansion.  The main
>> > opportunity for that seems to be pre-abbrev-expand-hook, but there are
>> > only two ways it can stop an expansion: 1, throw an error, 2, change
>> > the abbrev bindings (either by making changes in the current table or
>> > by swapping in a whole new table). 1 is unacceptable - how many errors
>> > before user gets mad and disables abbrevs altogether? 2, how to undo
>> > the changes when the point leaves the comment or string?
>> >
>>
>> maybe use an error handler that catches that specific error and ignores it?
>>
>
> Well, there's no point where I get control, that's the problem.
> Abbrev expansion can be triggered just by the user typing a space or
> period, if they have Abbrev minor mode on.
>
> I see a way to do what I want.  It involves hooking both pre-abbrev-
> expand-hook and post-command-hook.  I can restore the abbrev bindings
> in the latter.  But I am afraid what this could do to Emacs speed or
> even stability.
>
> What I've decided on so far is I won't do anything by default (so
> users who avoid Abbrev minor mode are not penalized) and have a
> customize flag that turns on the above solution.
>
> Other suggestions still welcome.
>

OK, so if I understand, the problem with using the pre-abbrev-expand-hook is
that once you use that hook to modify the table to prevent the expansion if
this would occur inside a comment/string or whatever, you have no easy way of
re-establishing that expansion, so text encountered later which is not in a
comment/string won't be expanded as it should. 

How about changing perspective. Why not make a hook function that looks at the
text that may be expanded/replaced and if it is text that should be expanded,
make the appropriate entry into the abbrev table and if its not already there.
If it is there, but he text sould not be expanded, then remove it. 

I think this would eliminate the problem of how to re=establish the corect
expansion entry in the table after it has been removed/changed because the text
being considered is either in a comment or a string. 

Another possibility is to use the abbrev hook function. This function runs
after the abbrev has been done. I guess it would be possible to use it to
'undo' the expansion if the text is in a comment/string. Not very nice, but
probably less impact than having something run via post-command-hook that would
run more often.

Tim

-- 
tcross (at) rapttech dot com dot au

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: abbrevs, skeletons, and comments
  2007-06-03  4:44   ` nobrowser
  2007-06-03 10:32     ` Tim X
@ 2007-06-03 11:43     ` Andreas Roehler
  1 sibling, 0 replies; 9+ messages in thread
From: Andreas Roehler @ 2007-06-03 11:43 UTC (permalink / raw
  To: nobrowser; +Cc: help-gnu-emacs

nobrowser@gmail.com schrieb:
> Tim X wrote:
>   
>> nobrowser@gmail.com writes:
>>
>>     
>>> In a module I'm writing, I'd like to bind abbrevs to code skeletons,
>>> similar to what is done e.g. by sml-mode.  But expanding the abbrevs
>>> makes no sense when the point is inside a comment (or a string
>>> literal).  So I'd like to conditionally stop expansion.  The main
>>> opportunity for that seems to be pre-abbrev-expand-hook, but there are
>>> only two ways it can stop an expansion: 1, throw an error, 2, change
>>> the abbrev bindings (either by making changes in the current table or
>>> by swapping in a whole new table). 1 is unacceptable - how many errors
>>> before user gets mad and disables abbrevs altogether? 2, how to undo
>>> the changes when the point leaves the comment or string?
>>>
>>>       
>> maybe use an error handler that catches that specific error and ignores it?
>>
>>     
>
> Well, there's no point where I get control, that's the problem.
> Abbrev expansion can be triggered just by the user typing a space or
> period, if they have Abbrev minor mode on.
>
> I see a way to do what I want.  It involves hooking both pre-abbrev-
> expand-hook and post-command-hook.  I can restore the abbrev bindings
> in the latter.  But I am afraid what this could do to Emacs speed or
> even stability.
>
> What I've decided on so far is I won't do anything by default (so
> users who avoid Abbrev minor mode are not penalized) and have a
> customize flag that turns on the above solution.
>
> Other suggestions still welcome.
>   

You could write

(unless (in-string-p)
    (expand-abbrev

Concerning comments, you may use

,----
| (defun comment-beginning ()
|   "Find the beginning of the enclosing comment.
| Returns nil if not inside a comment, else moves point and returns
| the same as `comment-search-backward'."
`----


If `font-lock' at point is reliable, another (probably faster)
approach could be

(get-text-property (point) 'face)

to determine if inside a comment.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: abbrevs, skeletons, and comments
  2007-06-03 10:32     ` Tim X
@ 2007-06-03 13:31       ` nobrowser
  0 siblings, 0 replies; 9+ messages in thread
From: nobrowser @ 2007-06-03 13:31 UTC (permalink / raw
  To: help-gnu-emacs


Tim X wrote:
> How about changing perspective. Why not make a hook function that looks at the
> text that may be expanded/replaced and if it is text that should be expanded,
> make the appropriate entry into the abbrev table and if its not already there.
> If it is there, but he text sould not be expanded, then remove it.

Yes, this would work, though ATM it clashes with my stylized code to
establish the bindings.  If I find a nice way to refactor that code
I'll use this suggestion.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: abbrevs, skeletons, and comments
  2007-06-02 14:34 abbrevs, skeletons, and comments nobrowser
  2007-06-03  3:24 ` Tim X
@ 2007-06-11  9:20 ` Johan Bockgård
  2007-06-12 13:50 ` Stefan Monnier
  2 siblings, 0 replies; 9+ messages in thread
From: Johan Bockgård @ 2007-06-11  9:20 UTC (permalink / raw
  To: help-gnu-emacs

nobrowser@gmail.com writes:

> In a module I'm writing, I'd like to bind abbrevs to code skeletons,
> similar to what is done e.g. by sml-mode.  But expanding the abbrevs
> makes no sense when the point is inside a comment (or a string
> literal).  So I'd like to conditionally stop expansion.

C-h f define-abbrev

-- 
Johan Bockgård

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: abbrevs, skeletons, and comments
  2007-06-02 14:34 abbrevs, skeletons, and comments nobrowser
  2007-06-03  3:24 ` Tim X
  2007-06-11  9:20 ` Johan Bockgård
@ 2007-06-12 13:50 ` Stefan Monnier
  2007-06-14 18:30   ` Johan Bockgård
  2 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2007-06-12 13:50 UTC (permalink / raw
  To: help-gnu-emacs

> In a module I'm writing, I'd like to bind abbrevs to code skeletons,
> similar to what is done e.g. by sml-mode.  But expanding the abbrevs
> makes no sense when the point is inside a comment (or a string
> literal).  So I'd like to conditionally stop expansion.

That's a good point.  An easy way to do that is instead of defining the
abbrev as

    (define-abbrev sml-mode-abbrev-table ,name "" ',fsym nil 'system)

where `fsym' is the name of the skeleton function, you can define it as

    (define-abbrev sml-mode-abbrev-table ,name ""
                   (lambda () (unless (nth 8 (syntax-ppss))
                                (call-interactively ',fsym)))
                   nil 'system)

Hmm... ah no, then it just erases the abbrev's name, so you have to adjust
the function so it re-inserts the text.  And then you have the issue of
whether or not the space that follows the abbrev name was/is inserted,
....  Yuck!

I guess this deserves a feature-request via M-x report-emacs-bug.


        Stefan

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: abbrevs, skeletons, and comments
  2007-06-12 13:50 ` Stefan Monnier
@ 2007-06-14 18:30   ` Johan Bockgård
  0 siblings, 0 replies; 9+ messages in thread
From: Johan Bockgård @ 2007-06-14 18:30 UTC (permalink / raw
  To: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> you can define it as
>
>     (define-abbrev sml-mode-abbrev-table ,name ""
>                    (lambda () (unless (nth 8 (syntax-ppss))
>                                 (call-interactively ',fsym)))
>                    nil 'system)
>
> Hmm... ah no, then it just erases the abbrev's name, so you have to
> adjust the function so it re-inserts the text.

    The argument EXPANSION is normally the desired expansion [...]. If
    it is anything but a string or `nil', then the abbreviation
    "expands" solely by running HOOK.

-- 
Johan Bockgård

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2007-06-14 18:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-06-02 14:34 abbrevs, skeletons, and comments nobrowser
2007-06-03  3:24 ` Tim X
2007-06-03  4:44   ` nobrowser
2007-06-03 10:32     ` Tim X
2007-06-03 13:31       ` nobrowser
2007-06-03 11:43     ` Andreas Roehler
2007-06-11  9:20 ` Johan Bockgård
2007-06-12 13:50 ` Stefan Monnier
2007-06-14 18:30   ` Johan Bockgård

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.