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

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

Re: Auto-Insertion of C/C++ #include-statements upon use of their symbol


From: Peter Milliken
Subject: Re: Auto-Insertion of C/C++ #include-statements upon use of their symbols
Date: Thu, 18 Sep 2008 07:17:50 +1000

Personally I would suggest that you don't want it to be "intrusive" on the programmer i.e. if you devise a system that automatically attempts to offer suggestions/completions then I would suggest it could easily be painful/intrusive for a programmer to use. For example, I would consider it really annoying if every time I typed memcpy that your add-on "jumped" up into my face.

So my suggestion would be to link the behaviour to a key sequence - with perhaps some auto-completion involved i.e. typing "mem<F3>" could result in a menu of all functions that have "mem" as the first three letters, selecting the appropriate completion from the menu would then result in the rest of the function "template" being fleshed out for the programmer. This allows the programmer to "activate" the behaviour at will - rather than having it "on" all the time - trust me, that will become "old" really fast! :-)

I did something similar (or started too :-)) for Python programming several years ago - which I never completed. I integrated it with my code template system (ELSE - Emacs Sensitive Language Editor - http://home.exetel.com.au/peterm/). From memory I used the index scanning logic that most major modes incorporate to derive the names of functions/variables in the current buffer (I never extended it to treat libraries/external files as you are attempting) and then created coding templates "on the fly" for use within ELSE i.e. Python is a good language for this as it uses named parameters, so, for example, I would generate a piece of code in the buffer that looked like this:

CalcVoltagesForTempRange ([start = {text}], [end = {text}], [stepsize = {text}], [Vp = {Vcc}], [R = {Rb}])

ELSE allows the programmer  to easily move to the next "placeholder" (items surrounded by []'s or {}'s above) and deletes the text within the braces when the user starts typing - automatically as the user starts typing (ELSE code can be used as an example of how to use the pre-/post-command hooks to have the editor make changes to the buffer based upon what the user is/has typed).

Each item above in []'s is "optional" i.e. if you don't want to use the naming parameter approach then you can just start typing whilst the cursor is within the []'s, otherwise you can "expand" the placeholder and (using the first placeholder as an example) you would have this:

CalcVoltagesForTempRange (start = {text}, [end = {text}], [stepsize = {text}], [Vp = {Vcc}], [R = {Rb}])

and point would be automatically positioned within the (first occurence of) "{text}" placeholder - {}'s mean that it is a "mandatory entry i.e. by this stage the "template" is telling you that you have to supply a value for the parameter - pretty obvious :-) Note that the last two parameters recognise that there is already a "default" value - but the template allows you to see what the values are and you can either "expand" the placeholder to provide different values or you can delete the placeholder altogether - accepting the default values.

So with my "system" (and integration with ELSE) "CalcVoltagesForTempRange" became a "token" - which meant I could type "Calc<F3>" and ELSE would automatically search all of it's "tokens" and attempt to auto-complete for me - if there was more than one possible match, it offers a menu of selections. Once the individual "token" is uniquely identified it then inserted the rest of the code template for me to fill out - a lot of typing saved with just several keystrokes :-)

So two things:

1. Don't make it too intrusive to the programmer - most people hate this sort of stuff anyway. Code template systems have been around for years and extremely few programmers use them! I guess people just like typing :-)

2. Consider integrating it with a "template" system - there are several out there and it wouldn't/shouldn't be too hard to do. I think the work I did "integrating" what I did for Python took me a couple of hours - most of that was spent working out how the indexing code worked and where to hook into it for what I wanted :-)

Hope this helps
Peter


On Thu, Sep 18, 2008 at 4:39 AM, Chetan <Chet.xspam@xspam.sbcglobal.net> wrote:
David <de_bb@arcor.de> writes:

> Nordlöw <per.nordlow@gmail.com> writes:
>> My suggestion is to make Emacs call a function each time a character
>> is inserted into buffer and then check if we are currently standing
>> behind such a use pattern. I believe looking-back() is usable here.
>
>> We also need to check that the cursor is *NOT* currently standing
>> inside a comment or string; How do I query such a context?
>
> Quick&Dirty "solution": use font-lock information, i.e. look at the face
> of the current symbol.
>
> Better solution: Since you are using semantic anyway, you could override
> `semantic-ctxt-current-symbol' for C/C++, so that no symbol is returned
> when the point is in a comment or string. Eric can surely say if this
> would be reasonable to do.
>
> I'm not sure all of this will be fast enough for calling it in something
> like 'post-command-hook, though. Maybe this should just be an extra
> command, which inserts the correct header for the current symbol, if
> necessary?
>
> -David
This looks like something that abbrev-mode, expand and other template
packages do already. I am not sure what you are trying to do and how it
is differs from any of those.

Chetan


reply via email to

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