From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs,gmane.emacs.pretest.bugs Subject: bug#5209: 23.1.90; CC Mode version 5.31.8 does not indent properly when writing new C files Date: Sun, 13 Dec 2009 20:33:11 +0000 Message-ID: <20091213203311.GB3389@muc.de> References: <20091213162210.GC376@srevilak.net> Reply-To: Alan Mackenzie , 5209@emacsbugs.donarmstrong.com NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1260737332 15082 80.91.229.12 (13 Dec 2009 20:48:52 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 13 Dec 2009 20:48:52 +0000 (UTC) Cc: emacs-pretest-bug@gnu.org To: Steve Revilak , 5209@emacsbugs.donarmstrong.com Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Dec 13 21:48:45 2009 Return-path: Envelope-to: geb-bug-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 1NJvMx-0001nI-2y for geb-bug-gnu-emacs@m.gmane.org; Sun, 13 Dec 2009 21:48:43 +0100 Original-Received: from localhost ([127.0.0.1]:35974 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NJvMw-00089h-Uw for geb-bug-gnu-emacs@m.gmane.org; Sun, 13 Dec 2009 15:48:43 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NJvLu-0007sP-Er for bug-gnu-emacs@gnu.org; Sun, 13 Dec 2009 15:47:38 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NJvLq-0007r3-69 for bug-gnu-emacs@gnu.org; Sun, 13 Dec 2009 15:47:38 -0500 Original-Received: from [199.232.76.173] (port=52670 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NJvLq-0007qs-0z for bug-gnu-emacs@gnu.org; Sun, 13 Dec 2009 15:47:34 -0500 Original-Received: from rzlab.ucr.edu ([138.23.92.77]:50514) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1NJvLp-00070E-F5 for bug-gnu-emacs@gnu.org; Sun, 13 Dec 2009 15:47:33 -0500 Original-Received: from rzlab.ucr.edu (rzlab.ucr.edu [127.0.0.1]) by rzlab.ucr.edu (8.14.3/8.14.3/Debian-5) with ESMTP id nBDKlSNI007905; Sun, 13 Dec 2009 12:47:29 -0800 Original-Received: (from debbugs@localhost) by rzlab.ucr.edu (8.14.3/8.14.3/Submit) id nBDKZ6Em006539; Sun, 13 Dec 2009 12:35:06 -0800 Resent-Date: Sun, 13 Dec 2009 12:35:06 -0800 X-Loop: owner@emacsbugs.donarmstrong.com Resent-From: Alan Mackenzie Resent-To: bug-submit-list@donarmstrong.com Resent-CC: Emacs Bugs 2Resent-Date: Sun, 13 Dec 2009 20:35:06 +0000 Resent-Message-ID: Resent-Sender: owner@emacsbugs.donarmstrong.com X-Emacs-PR-Message: followup 5209 X-Emacs-PR-Package: emacs X-Emacs-PR-Keywords: Original-Received: via spool by 5209-submit@emacsbugs.donarmstrong.com id=B5209.12607360565811 (code B ref 5209); Sun, 13 Dec 2009 20:35:06 +0000 Original-Received: (at 5209) by emacsbugs.donarmstrong.com; 13 Dec 2009 20:27:36 +0000 X-Spam-Bayes: score:0.5 Bayes not run. spammytokens:Tokens not available. hammytokens:Tokens not available. Original-Received: from mail.muc.de (qmailr@colin.muc.de [193.149.48.1]) by rzlab.ucr.edu (8.14.3/8.14.3/Debian-5) with ESMTP id nBDKRWOD005807 for <5209@emacsbugs.donarmstrong.com>; Sun, 13 Dec 2009 12:27:35 -0800 Original-Received: (qmail 46309 invoked by uid 3782); 13 Dec 2009 20:27:31 -0000 Original-Received: from acm.muc.de (pD9E53AA3.dip.t-dialin.net [217.229.58.163]) by colin2.muc.de (tmda-ofmipd) with ESMTP; Sun, 13 Dec 2009 21:27:29 +0100 Original-Received: (qmail 7900 invoked by uid 1000); 13 Dec 2009 20:33:11 -0000 Content-Disposition: inline In-Reply-To: <20091213162210.GC376@srevilak.net> User-Agent: Mutt/1.5.9i X-Delivery-Agent: TMDA/1.1.5 (Fettercairn) X-Primary-Address: acm@muc.de X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) Resent-Date: Sun, 13 Dec 2009 15:47:38 -0500 X-BeenThere: bug-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:33556 gmane.emacs.pretest.bugs:25316 Archived-At: Hi, Steve, On Sun, Dec 13, 2009 at 11:22:10AM -0500, Steve Revilak wrote: > STEPS TO REPRODUCE > ------------------ > (1) Start emacs with the command line "emacs -nw -Q foo.c". > foo.c should be a new file, which does not exist prior to starting > emacs. > (2) Type "#include " and RETURN > (3) Type RETURN to leave a blank line > (4) type "static void add_one(int * x) {" and RETURN > (5) type "*x += 1;" and RETURN > (6) type "}" and RETURN (to close the function definition) > NOTE: the statement written in step (5) is left-aligned to column > zero. The statement should be indented. > (7) Place point in column zero of the line "*x += 1;". Press TAB. > Before pressing TAB, "*x += 1;" is aligned to column zero. After > pressing TAB, "*x += 1;" is still aligned to column zero. TAB did > not indent the statement. > (8) Type "C-x h TAB" (i.e., to select and re-indent the entire > buffer). > The minibuffer displays "Indenting region... done". However, the > statement "*x += 1;" is still aligned to column zero. > After step 8, buffer "foo.c" looks like this: > --------------------------------- > #include > static void add_one(int * x) { > *x += 1; > } > --------------------------------- > NOTE: the fourth line is not indented (but it should be). First thing, thanks for such a simple and clear bug report. There is a bug in `c-parse-state', one of the low-level functions in CC mode, which generates and changes a cache of brace and paren positions. Would you apply the following patch, and test it, please. If anything is still wrong, please let me know again. *** cc-engine.orig.el 2009-12-13 19:43:27.268817800 +0000 --- cc-engine.el 2009-12-13 20:06:47.819901672 +0000 *************** *** 2231,2236 **** --- 2231,2249 ---- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Defuns which analyse the buffer, yet don't change `c-state-cache'. + (defun c-p1-not-in-p0-macro (p0 p1) + ;; Is P0 in a CPP construct and p1 not in it? + (save-restriction + (widen) + (save-excursion + (let ((p0-macro-begin + (progn (goto-char p0) (and (c-beginning-of-macro) (point))))) + (and p0-macro-begin + (not + (eq p0-macro-begin + (progn (goto-char p1) (and (c-beginning-of-macro) (point))))) + ))))) + (defun c-get-fallback-scan-pos (here) ;; Return a start position for building `c-state-cache' from ;; scratch. This will be at the top level, 2 defuns back. *************** *** 2587,2593 **** ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair ;; preceding POS which needs to be recorded in `c-state-cache'. It is a ;; position to scan backwards from. ! ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT. (save-restriction (narrow-to-region 1 (point-max)) (save-excursion --- 2600,2607 ---- ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair ;; preceding POS which needs to be recorded in `c-state-cache'. It is a ;; position to scan backwards from. ! ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT or nil if we ! ;; don't scan past PPS-POINT. (save-restriction (narrow-to-region 1 (point-max)) (save-excursion *************** *** 2624,2630 **** (< (point-max) c-state-old-cpp-end))) (point-max) (min (point-max) c-state-old-cpp-beg))) ! (while (and c-state-cache (> (c-state-cache-top-lparen) upper-lim)) (setq c-state-cache (cdr c-state-cache))) ;; If `upper-lim' is inside the last recorded brace pair, remove its ;; RBrace and indicate we'll need to search backwards for a previous --- 2638,2645 ---- (< (point-max) c-state-old-cpp-end))) (point-max) (min (point-max) c-state-old-cpp-beg))) ! (while (and c-state-cache ! (>= (c-state-cache-top-lparen) upper-lim)) (setq c-state-cache (cdr c-state-cache))) ;; If `upper-lim' is inside the last recorded brace pair, remove its ;; RBrace and indicate we'll need to search backwards for a previous *************** *** 2641,2647 **** ;; (car c-state-cache). There can be no open parens/braces/brackets ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max), ;; due to the interface spec to this function. ! (setq pos (if good-pos-actual-macro-end (1+ good-pos-actual-macro-end) ; get outside the macro as ; marked by a `category' text property. good-pos)) --- 2656,2663 ---- ;; (car c-state-cache). There can be no open parens/braces/brackets ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max), ;; due to the interface spec to this function. ! (setq pos (if (and good-pos-actual-macro-start ! (not (eq good-pos-actual-macro-start in-macro-start))) (1+ good-pos-actual-macro-end) ; get outside the macro as ; marked by a `category' text property. good-pos)) *************** *** 2701,2707 **** (setq c-state-cache (cons (cons pair-beg pos) c-state-cache))) ! (list pos scan-back-pos pps-state))))) (defun c-remove-stale-state-cache-backwards (here cache-pos) ;; Strip stale elements of `c-state-cache' by moving backwards through the --- 2717,2723 ---- (setq c-state-cache (cons (cons pair-beg pos) c-state-cache))) ! (list pos scan-back-pos pps-point-state))))) (defun c-remove-stale-state-cache-backwards (here cache-pos) ;; Strip stale elements of `c-state-cache' by moving backwards through the *************** *** 2769,2777 **** (list (1+ pos) pos t)) ; return value. We've just converted a brace ; pair entry into a { entry, so the caller ; needs to search for a brace pair before the ! ; {. ! ;; ;; `here' might be inside a literal. Check for this. (setq lit (c-state-literal-at here) here-lit-start (or (car lit) here) here-lit-end (or (cdr lit) here)) --- 2785,2793 ---- (list (1+ pos) pos t)) ; return value. We've just converted a brace ; pair entry into a { entry, so the caller ; needs to search for a brace pair before the ! ; {, hence the `pos' in second last place. ! ;; `here' might be inside a literal. Check for this. (setq lit (c-state-literal-at here) here-lit-start (or (car lit) here) here-lit-end (or (cdr lit) here)) *************** *** 2801,2812 **** (setq pos pa)))) ; might signal (if (setq ren (c-safe-scan-lists pos -1 -1 too-far-back)) ;; CASE 3: After a }/)/] before `here''s BOL. ! (list (1+ ren) (and dropped-cons pos) nil) ; Return value ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of ;; literal containing it. (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol))) ! (list good-pos (and dropped-cons good-pos) nil)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; --- 2817,2836 ---- (setq pos pa)))) ; might signal (if (setq ren (c-safe-scan-lists pos -1 -1 too-far-back)) ;; CASE 3: After a }/)/] before `here''s BOL. ! (list (1+ ren) ! (or (and (c-p1-not-in-p0-macro here c-state-cache-good-pos) ! here) ! (and dropped-cons pos)) ! nil) ; Return value ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of ;; literal containing it. (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol))) ! (list good-pos ! (or (and (c-p1-not-in-p0-macro here c-state-cache-good-pos) ! here) ! (and dropped-cons good-pos)) ! nil)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- Alan Mackenzie (Nuremberg, Germany).