Auto-Insertion of C/C++ #include-statements upon use of their symbols
  Home FAQ Contact Sign in
gnu.emacs.help only
 
Advanced search
POPULAR GROUPS

more...

gnu.emacs.help Profile…
 Up
Auto-Insertion of C/C++ #include-statements upon use of their symbols         


Author: Nordlöw
Date: Sep 17, 2008 08:04

I am currently writing some logic for automatically looking up a
required header file when the programmer completes the use of a symbol
defined in this header file. This to relieve especially C/C++
programmers from the cumbersome process of doing for example "man
memcpy" to figure out that we need to "include " before
calling memcpy().

In C++ this shall also work for Standard Template Library (STL)
Headers, that is "#include " is auto-(query-)included when the
user completes the expression "std::vector".

The logic for these lookups and insertions already exists. What now
remains is the logic for sensing when this is needed.

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?

If any one is interested in using this add-ons I will gladly send it
to you.
Show full article (1.10Kb)
5 Comments
Re: Auto-Insertion of C/C++ #include-statements upon use of their symbols         


Author: David
Date: Sep 17, 2008 09:18

Nordlöw 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
no comments
Re: Auto-Insertion of C/C++ #include-statements upon use of their symbols         


Author: Chetan
Date: Sep 17, 2008 11:39

David writes:
> Nordlöw 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...
Show full article (1.20Kb)
no comments
Re: Auto-Insertion of C/C++ #include-statements upon use of their symbols         


Author: Peter Milliken
Date: Sep 17, 2008 14:17

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" 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!
:-)
Show full article (10.59Kb)
no comments
Re: Auto-Insertion of C/C++ #include-statements upon use of their symbols         


Author: David
Date: Sep 17, 2008 14:54

>> Nordlöw 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?
> David writes:
>> 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?
Show full article (1.64Kb)
no comments
Re: Auto-Insertion of C/C++ #include-statements upon use of their symbols         


Author: Nikolaj Schumacher
Date: Sep 19, 2008 09:13

Nordlöw gmail.com> wrote:
> I am currently writing some logic for automatically looking up a
> required header file when the programmer completes the use of a symbol
> defined in this header file. This to relieve especially C/C++
> programmers from the cumbersome process of doing for example "man
> memcpy" to figure out that we need to "include " before
> calling memcpy().

I've hacked together something similar for Java. If you're interested,
I could send you some of it (GPLed).
> The logic for these lookups and insertions already exists. What now
> remains is the logic for sensing when this is needed.

I use this:

(add-hook 'pre-abbrev-expand-hook import-class-maybe nil t)

(defun comment-or-string-p ()
(let ((pos (syntax-ppss)))
(or (nth 3 pos) (nth 4 pos))))
Show full article (1.53Kb)
no comments