all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* inserting code based on syntactic information in cc-mode
@ 2009-11-23  9:38 SameerDS
  2009-11-23  9:44 ` Joost Kremers
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: SameerDS @ 2009-11-23  9:38 UTC (permalink / raw
  To: help-gnu-emacs

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.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: inserting code based on syntactic information in cc-mode
  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
  2009-12-11 18:38 ` Alan Mackenzie
  2 siblings, 1 reply; 10+ messages in thread
From: Joost Kremers @ 2009-11-23  9:44 UTC (permalink / raw
  To: help-gnu-emacs

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                                      joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: inserting code based on syntactic information in cc-mode
  2009-11-23  9:44 ` Joost Kremers
@ 2009-11-23  9:58   ` SameerDS
  0 siblings, 0 replies; 10+ messages in thread
From: SameerDS @ 2009-11-23  9:58 UTC (permalink / raw
  To: help-gnu-emacs

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/


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: inserting code based on syntactic information in cc-mode
  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 10:42 ` Pascal J. Bourguignon
  2009-11-23 11:03   ` SameerDS
  2009-12-11 11:16   ` SameerDS
  2009-12-11 18:38 ` Alan Mackenzie
  2 siblings, 2 replies; 10+ messages in thread
From: Pascal J. Bourguignon @ 2009-11-23 10:42 UTC (permalink / raw
  To: help-gnu-emacs

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__


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: inserting code based on syntactic information in cc-mode
  2009-11-23 10:42 ` Pascal J. Bourguignon
@ 2009-11-23 11:03   ` SameerDS
  2009-12-11 11:16   ` SameerDS
  1 sibling, 0 replies; 10+ messages in thread
From: SameerDS @ 2009-11-23 11:03 UTC (permalink / raw
  To: help-gnu-emacs

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/


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: inserting code based on syntactic information in cc-mode
  2009-11-23 10:42 ` Pascal J. Bourguignon
  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
  1 sibling, 2 replies; 10+ messages in thread
From: SameerDS @ 2009-12-11 11:16 UTC (permalink / raw
  To: help-gnu-emacs

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/


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: inserting code based on syntactic information in cc-mode
  2009-12-11 11:16   ` SameerDS
@ 2009-12-11 16:42     ` Lennart Borgman
  2009-12-11 17:09     ` harven
  1 sibling, 0 replies; 10+ messages in thread
From: Lennart Borgman @ 2009-12-11 16:42 UTC (permalink / raw
  To: SameerDS; +Cc: help-gnu-emacs

On Fri, Dec 11, 2009 at 12:16 PM, SameerDS <sameerds@gmail.com> wrote:
> 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!


That does the help buffer show?




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: inserting code based on syntactic information in cc-mode
  2009-12-11 11:16   ` SameerDS
  2009-12-11 16:42     ` Lennart Borgman
@ 2009-12-11 17:09     ` harven
  1 sibling, 0 replies; 10+ messages in thread
From: harven @ 2009-12-11 17:09 UTC (permalink / raw
  To: help-gnu-emacs

SameerDS <sameerds@gmail.com> writes:

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

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.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: inserting code based on syntactic information in cc-mode
  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 10:42 ` Pascal J. Bourguignon
@ 2009-12-11 18:38 ` Alan Mackenzie
  2009-12-12  3:14   ` SameerDS
  2 siblings, 1 reply; 10+ messages in thread
From: Alan Mackenzie @ 2009-12-11 18:38 UTC (permalink / raw
  To: help-gnu-emacs

SameerDS <sameerds@gmail.com> wrote:
> 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?

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).



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: inserting code based on syntactic information in cc-mode
  2009-12-11 18:38 ` Alan Mackenzie
@ 2009-12-12  3:14   ` SameerDS
  0 siblings, 0 replies; 10+ messages in thread
From: SameerDS @ 2009-12-12  3:14 UTC (permalink / raw
  To: help-gnu-emacs


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/


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2009-12-12  3:14 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.