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: Nikolaj Schumacher
Subject: Re: Auto-Insertion of C/C++ #include-statements upon use of their symbols
Date: Fri, 19 Sep 2008 18:13:28 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2.50 (darwin)

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




reply via email to

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