help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: inserting code based on syntactic information in cc-mode


From: Pascal J. Bourguignon
Subject: Re: inserting code based on syntactic information in cc-mode
Date: Mon, 23 Nov 2009 11:42:58 +0100
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

SameerDS <sameerds@gmail.com> writes:

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

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__


reply via email to

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