🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.emacs.bug
Article #99457

low priority bug in c-mode.el

#99457
From: Geoff Clemm
Date: Thu, 13 Sep 1990 06:18
76 lines
2851 bytes
The indentation of else's where the braces "cuddle up" to the else is not
handled properly when c-brace-imaginary offset is used instead of 
c-indent-level, e.g.
   (setq-default c-indent-level 0)
   (setq-default c-brace-imaginary-offset 3).

For example,
   if (test1) {
      code1;
   } else if (test2) {
      code2;
   } else {
      code 3; }

appears as

   if (test1) {
      code1;
      } else if (test2) {
         code2;
         } else {
            code3; }

The fix is to extend the definition of c-indent-line to be :

(defun c-indent-line ()
  "Indent current line as C code.
Return the amount the indentation changed by."
  (let ((indent (calculate-c-indent nil))
	beg shift-amt
	(case-fold-search nil)
	(pos (- (point-max) (point))))
    (beginning-of-line)
    (setq beg (point))
    (cond ((eq indent nil)
	   (setq indent (current-indentation)))
	  ((eq indent t)
	   (setq indent (calculate-c-indent-within-comment)))
	  ((looking-at "[ \t]*#")
	   (setq indent 0))
	  (t
	   (skip-chars-forward " \t")
	   (if (listp indent) (setq indent (car indent)))
	   (cond ((or (looking-at "case[ \t]")
		      (and (looking-at "[A-Za-z]")
			   (save-excursion
			     (forward-sexp 1)
			     (looking-at ":"))))
		  (setq indent (max 1 (+ indent c-label-offset))))
		 ((and (looking-at "else\\b")
		       (not (looking-at "else\\s_")))
		  (setq indent (save-excursion
				 (c-backward-to-start-of-if)
				 (current-indentation))))
		 ((looking-at "}[ \t]*else")
		  (setq indent (save-excursion
				 (forward-char)
				 (backward-sexp)
				 (current-indentation))))
		 ((= (following-char) ?})
		  (setq indent (- indent c-indent-level)))
		 ((= (following-char) ?{)
		  (setq indent (+ indent c-brace-offset))))))
    (skip-chars-forward " \t")
    (setq shift-amt (- indent (current-column)))
    (if (zerop shift-amt)
	(if (> (- (point-max) pos) (point))
	    (goto-char (- (point-max) pos)))
      (delete-region beg (point))
      (indent-to indent)
      ;; If initial point was within line's indentation,
      ;; position after the indentation.  Else stay at same point in text.
      (if (> (- (point-max) pos) (point))
	  (goto-char (- (point-max) pos))))
    shift-amt))
Technical Details
Message-ID: <9009130618.AA02304@wodehouse.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!tut.cis.ohio-state.edu!eng.sun.com!geoff

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fb2f1,312a868ab8d69744,start
X-Google-Attributes: gidfb2f1,public
X-Google-ArrivalTime: 1990-09-13 15:47:36 PST
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!tut.cis.ohio-state.edu!eng.sun.com!geoff
From: geoff@eng.sun.com (Geoff Clemm)
Newsgroups: gnu.emacs.bug
Subject: low priority bug in c-mode.el
Message-ID: <9009130618.AA02304@wodehouse.Eng.Sun.COM>
Date: 13 Sep 90 06:18:01 GMT
Sender: daemon@tut.cis.ohio-state.edu
Distribution: gnu
Organization: GNUs Not Usenet
Lines: 76
Posted: Thu Sep 13 07:18:01 1990