From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.help Subject: Re: How to indent on LOCK(); macro??? Date: Thu, 1 Sep 2005 16:28:14 +0000 Organization: muc.de e.V. -- private internet access Message-ID: References: <6mo3fd.46.ln@acm.acm> NNTP-Posting-Host: main.gmane.org X-Trace: sea.gmane.org 1125593308 26966 80.91.229.2 (1 Sep 2005 16:48:28 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 1 Sep 2005 16:48:28 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Sep 01 18:48:19 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1EAsCL-0007kM-Hl for geh-help-gnu-emacs@m.gmane.org; Thu, 01 Sep 2005 18:45:57 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1EAsGa-0008VD-Ih for geh-help-gnu-emacs@m.gmane.org; Thu, 01 Sep 2005 12:50:20 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!newsfeed.gamma.ru!Gamma.RU!ecngs!feeder.ecngs.de!news.germany.com!news.space.net!news.muc.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 89 Original-NNTP-Posting-Host: acm.muc.de Original-X-Trace: marvin.muc.de 1125592541 78802 193.149.49.134 (1 Sep 2005 16:35:41 GMT) Original-X-Complaints-To: news-admin@muc.de Original-NNTP-Posting-Date: 1 Sep 2005 16:35:41 GMT User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (Linux/2.0.35 (i686)) Original-Xref: shelby.stanford.edu gnu.emacs.help:133622 Original-To: help-gnu-emacs@gnu.org 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:29162 Archived-At: Roy Smith wrote on Wed, 31 Aug 2005 19:50:01 -0400: > In article , Alan Mackenzie wrote: >> And if you're not a Lisp hacker (or can't be bothered), say so, and I'll >> throw some code together for you. > Wow. An offer I can't refuse. Thanks! OK, here goes! Put the code into your .emacs. Both the functions here get byte compiled (they'll be getting called a _lot_). To hook up to your CC Mode, do one of: (a) If you're a hooker: (add-hook 'c-special-indent-hook 'rs-LOCK-reindent) (b) If you're a touch more stylish: Put a line like this into your style definition: (c-special-indent-hook . rs-LOCK-reindent) Here's the code: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun rs-back-to-LOCK-p () "Search backwards for the LOCK keyword. If we find it, leave point there and return non-`nil'. If we don't find it, return nil, and point is left undefined." (let (in-lit target) (while (and (search-backward-regexp "^[ \t]*\\(LOCK[ \t]*(\\)" nil t) (setq target (match-beginning 1)) (setq in-lit (c-in-literal)))) (when (and target (not in-lit)) (goto-char target) t))) (byte-compile 'rs-back-to-LOCK-p) (defun rs-LOCK-reindent () "Function to reindent lines inside and around blocks of lines delimited by the macros LOCK and END_LOCK, as follows: 1 LOCK (mydatalock) 2 *myData++ = foo; 3 Data_count += 1; 4 END_LOCK 5 do_something_else (); NOTE: There are no semicolons terminating the LOCK/END_LOCK lines." ;; Another note: c-syntactic-context is bound to the s.c. for the current ;; line. (let (org-indent new-indent stmt-dpair) (save-excursion (save-match-data (setq org-indent (progn (back-to-indentation) (current-column)) new-indent org-indent) (cond ;; L4? ((looking-at "END_LOCK") (if (rs-back-to-LOCK-p) (setq new-indent (current-column)))) ((setq stmt-dpair (assq 'statement-cont c-syntactic-context)) (goto-char (if (consp (cdr stmt-dpair)) (cadr stmt-dpair) ; cc-mode >= 5.30 (cdr stmt-dpair))) ; cc-mode 5.28 (cond ;; L5? ((looking-at "END_LOCK\\>\\([^_]\\|$\\)") (if (rs-back-to-LOCK-p) (setq new-indent (current-column)))) ;; L2? ((looking-at "LOCK[ \t]") (setq new-indent (+ (current-column) c-basic-offset))))) ;; L3? ((and (assq 'statement c-syntactic-context) ;; (eq (c-beginning-of-statement-1) 'previous) ; Needs >= 5.30 (progn (c-beginning-of-statement-1) (looking-at "LOCK[ \t]+("))) (setq new-indent (+ (current-column) c-basic-offset)))))) (c-shift-line-indentation (- new-indent org-indent)))) (byte-compile 'rs-LOCK-reindent) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Just one thing - the code as it stands might not handle labels properly. I mean, you've not got any goto's in the C code, have you? Certainly not in critical sections. ;-) Let me know how you get on! -- Alan Mackenzie (Munich, Germany) Email: aacm@muuc.dee; to decode, wherever there is a repeated letter (like "aa"), remove half of them (leaving, say, "a").