From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: inserting code based on syntactic information in cc-mode Date: Mon, 23 Nov 2009 11:42:58 +0100 Organization: Informatimago Message-ID: <877hthed3h.fsf@galatea.local> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1258976682 23073 80.91.229.12 (23 Nov 2009 11:44:42 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 23 Nov 2009 11:44:42 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Nov 23 12:44:35 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1NCXLM-0001nD-D8 for geh-help-gnu-emacs@m.gmane.org; Mon, 23 Nov 2009 12:44:33 +0100 Original-Received: from localhost ([127.0.0.1]:48650 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NCXLL-0007qw-Os for geh-help-gnu-emacs@m.gmane.org; Mon, 23 Nov 2009 06:44:31 -0500 Original-Path: news.stanford.edu!usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 52 Original-X-Trace: individual.net Ybr8QDDi40AKTiPPGwvTpwtoBuWopHfpqY/cQFzN6SMeg09yww Cancel-Lock: sha1:MzBlZTQzYThkYzYwMmIwMDY3MTk5YTIwOTg1Y2EzYjk3ZDE4YTlhMQ== sha1:B3do+bIw/0sqKC8fIIG1lBlKY3c= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin) Original-Xref: news.stanford.edu gnu.emacs.help:174971 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:70045 Archived-At: SameerDS 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__