unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: inserting code based on syntactic information in cc-mode
Date: Mon, 23 Nov 2009 11:42:58 +0100	[thread overview]
Message-ID: <877hthed3h.fsf@galatea.local> (raw)
In-Reply-To: af0b487e-69c2-4252-b4d3-8c5f25cf772b@o9g2000prg.googlegroups.com

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.

\f

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__


  parent reply	other threads:[~2009-11-23 10:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-23  9:38 inserting code based on syntactic information in cc-mode SameerDS
2009-11-23  9:44 ` Joost Kremers
2009-11-23  9:58   ` SameerDS
2009-11-23 10:42 ` Pascal J. Bourguignon [this message]
2009-11-23 11:03   ` SameerDS
2009-12-11 11:16   ` SameerDS
2009-12-11 16:42     ` Lennart Borgman
2009-12-11 17:09     ` harven
2009-12-11 18:38 ` Alan Mackenzie
2009-12-12  3:14   ` SameerDS

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877hthed3h.fsf@galatea.local \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).