all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Auto-Insertion of C/C++ #include-statements upon use of their symbols
@ 2008-09-17 15:04 Nordlöw
  2008-09-17 16:18 ` David
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Nordlöw @ 2008-09-17 15:04 UTC (permalink / raw)
  To: help-gnu-emacs

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 <string.h>" before
calling memcpy().

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

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.


Many Thanks in advance,
Nordlöw


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

* Re: Auto-Insertion of C/C++ #include-statements upon use of their symbols
  2008-09-17 15:04 Auto-Insertion of C/C++ #include-statements upon use of their symbols Nordlöw
@ 2008-09-17 16:18 ` David
       [not found] ` <mailman.19414.1221668342.18990.help-gnu-emacs@gnu.org>
  2008-09-19 16:13 ` Nikolaj Schumacher
  2 siblings, 0 replies; 6+ messages in thread
From: David @ 2008-09-17 16:18 UTC (permalink / raw)
  To: help-gnu-emacs

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





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

* Re: Auto-Insertion of C/C++ #include-statements upon use of their symbols
       [not found] ` <mailman.19414.1221668342.18990.help-gnu-emacs@gnu.org>
@ 2008-09-17 18:39   ` Chetan
  2008-09-17 21:17     ` Peter Milliken
  2008-09-17 21:54     ` David
  0 siblings, 2 replies; 6+ messages in thread
From: Chetan @ 2008-09-17 18:39 UTC (permalink / raw)
  To: help-gnu-emacs

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


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

* Re: Auto-Insertion of C/C++ #include-statements upon use of their symbols
  2008-09-17 18:39   ` Chetan
@ 2008-09-17 21:17     ` Peter Milliken
  2008-09-17 21:54     ` David
  1 sibling, 0 replies; 6+ messages in thread
From: Peter Milliken @ 2008-09-17 21:17 UTC (permalink / raw)
  To: Chetan; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 5519 bytes --]

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
>

[-- Attachment #2: Type: text/html, Size: 6547 bytes --]

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

* Re: Auto-Insertion of C/C++ #include-statements upon use of their symbols
  2008-09-17 18:39   ` Chetan
  2008-09-17 21:17     ` Peter Milliken
@ 2008-09-17 21:54     ` David
  1 sibling, 0 replies; 6+ messages in thread
From: David @ 2008-09-17 21:54 UTC (permalink / raw)
  To: help-gnu-emacs

>> 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?

> David <de_bb@arcor.de> 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?

Chetan <Chet.xspam@xspam.sbcglobal.net> writes:
> 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.

He wants to automatically add proper #include statements as soon as you
use external functions. In practice, this would mean you wouldn't have
to write any #include statements at all, since they would be
automatically added as you write your code (and also removed again when
not needed anymore, although this would be more difficult to do). By
using CEDET's templating system SRecode, this could even be made
language independent. I don't think the usual templating systems can do
that.

-David





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

* Re: Auto-Insertion of C/C++ #include-statements upon use of their symbols
  2008-09-17 15:04 Auto-Insertion of C/C++ #include-statements upon use of their symbols Nordlöw
  2008-09-17 16:18 ` David
       [not found] ` <mailman.19414.1221668342.18990.help-gnu-emacs@gnu.org>
@ 2008-09-19 16:13 ` Nikolaj Schumacher
  2 siblings, 0 replies; 6+ messages in thread
From: Nikolaj Schumacher @ 2008-09-19 16:13 UTC (permalink / raw)
  To: Nordlöw; +Cc: help-gnu-emacs

Nordlöw <per.nordlow@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 <string.h>" 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))))

(defun import-class-maybe ()
  "Add an import statement for the class name before point if there is any."
  (let (case-fold-search)
    (and (looking-back class-name-regexp)
         (not (comment-or-string-p))
         (ignore-errors
           (profi-import-class (match-string-no-properties 1) t)))))

> My suggestion is to make Emacs call a function each time a character
> is inserted into buffer

`pre-abbrev-expand-hook' is only called at the end of words, which might
even be better.  `after-change-functions' is called after every character.

> If any one is interested in using this add-ons I will gladly send it
> to you.

I hope you'll release it eventually, though.

regards,
Nikolaj Schumacher




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

end of thread, other threads:[~2008-09-19 16:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-09-17 15:04 Auto-Insertion of C/C++ #include-statements upon use of their symbols Nordlöw
2008-09-17 16:18 ` David
     [not found] ` <mailman.19414.1221668342.18990.help-gnu-emacs@gnu.org>
2008-09-17 18:39   ` Chetan
2008-09-17 21:17     ` Peter Milliken
2008-09-17 21:54     ` David
2008-09-19 16:13 ` Nikolaj Schumacher

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.