Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

inserting code based on syntactic information in cc-mode

0 views
Skip to first unread message

SameerDS

unread,
Nov 23, 2009, 4:38:19 AM11/23/09
to
Hello,

I want to write elisp functions that can insert code based on the
syntactic information at the point where they are invoked. For
example, typing an opening brace at the start of a function definition
should automatically insert the closing brace and put point on a new
line between the two. But when an opening brace is typed at the start
of a class description, it should also insert a semi-colon after the
closing brace. Such a function might be easily written as a skeleton.
What I need is a way to inspect the local syntactic information when
the '{' key is pressed and then call the appropriate function.

For this, I've been going through the documentation for CC-mode
looking for a function that returns the syntax information for the
current line. Basically a function that is equivalent to c-show-
syntactic-information, but which can be used in elisp code directly. I
couldn't find such a function ... is there a way to do this at all in
CC-mode?

Sameer.

Joost Kremers

unread,
Nov 23, 2009, 4:44:17 AM11/23/09
to
SameerDS wrote:
> I want to write elisp functions that can insert code based on the
> syntactic information at the point where they are invoked.

this is not entirely what you describe, but it may be more useful even:

http://code.google.com/p/yasnippet/


--
Joost Kremers joostk...@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)

SameerDS

unread,
Nov 23, 2009, 4:58:10 AM11/23/09
to
On Nov 23, 2:44 pm, Joost Kremers <joostkrem...@yahoo.com> wrote:
> SameerDS wrote:
> > I want to write elisp functions that can insert code based on the
> > syntactic information at the point where they are invoked.
>
> this is not entirely what you describe, but it may be more useful even:
>
> http://code.google.com/p/yasnippet/

Indeed, that seems like something I definitely want to use ... thanks
for the pointer! But it is only half the solution. Rather than
remembering abbrevs for code snippets or even binding them to keys, I
would like to write glue logic that selects the correct snippet based
on what I am typing! Hence the need to inspect local syntactic
information ...

-- Sameer.
http://sameer.sbuddhe.net/

Pascal J. Bourguignon

unread,
Nov 23, 2009, 5:42:58 AM11/23/09
to
SameerDS <same...@gmail.com> writes:

Type: C-h f c-show-syntactic-information RET
then: C-x o TAB RET

Read the source, and learn what lisp function is called to get the syntax.

It is actually wrapped by this form:

(let* ((c-parsing-error nil)
(syntax (if (boundp 'c-syntactic-context)
;; Use `c-syntactic-context' in the same way as
;; `c-indent-line', to be consistent.
c-syntactic-context
(c-save-buffer-state nil
(c-guess-basic-syntax)))))
...)

so you could define your own wrapper function:

(defun get-syntax ()
(let ((c-parsing-error nil))
(if (boundp 'c-syntactic-context)
;; Use `c-syntactic-context' in the same way as
;; `c-indent-line', to be consistent.
c-syntactic-context
(c-save-buffer-state nil
(c-guess-basic-syntax)))))

--
__Pascal Bourguignon__

SameerDS

unread,
Nov 23, 2009, 6:03:59 AM11/23/09
to
On Nov 23, 3:42 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:

>
> Type: C-h f c-show-syntactic-information RET
> then: C-x o TAB RET
>
> Read the source, and learn what lisp function is called to get the syntax.
** snip **

> so you could define your own wrapper function:

I didn't know one can reach the source this way. Will have to go
through the code to be really sure I know what I am doing, though!

-- Sameer.
http://sameer.sbuddhe.net/

SameerDS

unread,
Dec 11, 2009, 6:16:18 AM12/11/09
to
On Nov 23, 3:42 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
>
> Type: C-h f c-show-syntactic-information RET
> then: C-x o TAB RET
>
> Read the source, and learn what lisp function is called to get the syntax.
>

Well I finally found some time to try this out ... but C-x o TAB
simply takes me to the [back] link at the bottom of the *Help* buffer
(mainly because that is the only link in the buffer). What I expected
was to be taken to the elisp file that actually contains the function
definition!

--
Sameer.
http://sameer.sbuddhe.net/

Lennart Borgman

unread,
Dec 11, 2009, 11:42:49 AM12/11/09
to SameerDS, help-gn...@gnu.org


That does the help buffer show?


harven

unread,
Dec 11, 2009, 12:09:32 PM12/11/09
to
SameerDS <same...@gmail.com> writes:

I guess that the elisp sources are not installed. What is your system ?
On debian, the sources are in a separate package called emacs.el.

Try to locate files ending with .el or .el.gz. on your system.
(e.g. by typing M-x locate misc.el in emacs)
If you only find files ending with .elc (sources are compiled),
that explains why you can't access them through the *help* buffer.

Alan Mackenzie

unread,
Dec 11, 2009, 1:38:32 PM12/11/09
to
SameerDS <same...@gmail.com> wrote:
> Hello,

The function you need is `c-guess-basic-syntax', which is in the file
cc-engine.el. The innards of that function aren't for the faint hearted.

> Sameer.

--
Alan Mackenzie (Nuremberg, Germany).

SameerDS

unread,
Dec 11, 2009, 10:14:39 PM12/11/09
to

harven, that explains it. I don't have the lisp sources installed!
Thanks!

Alan, from the lisp snippet posted earlier it seems the evaluation of
`c-guess-basic-syntax' is usually stored in the variable `c-syntactic-
context', _if_ it is currently bound. This might have been done for
performance reasons, and perhaps because the side-effects are
important here. Repeating the "probably correct" way of using this,
that was posted earlier on this thread. This is basically a guess
based on how other functions in cc-mode do it ... which is why I was
trying to reach the source for cc-mode.

(defun get-syntax ()
(let ((c-parsing-error nil))
(if (boundp 'c-syntactic-context)
;; Use `c-syntactic-context' in the same way as
;; `c-indent-line', to be consistent.
c-syntactic-context
(c-save-buffer-state nil
(c-guess-basic-syntax)))))

BTW, yasnippets rocks! :)
--
Sameer.
http://sameer.sbuddhe.net/

0 new messages