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.
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)
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/
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__
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/
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/
That does the help buffer show?
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.
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).
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/