🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.emacs.bug
Article #98628

Re: Help on indent-for-comment

#98628
From: Joe Wells
Date: Fri, 12 Apr 1991 18:13
909 lines
31510 bytes
In article <9104102003.AA11010@life.ai.mit.edu> mthome@BBN.COM (Mike Thome) writes:

   Indent-for-comment does very much the wrong thing on lines were there
   are strings containing imbedded comment characters: 

I've got a fix for c-mode, and a partial fix for lisp-mode (preceding a
semicolon in a string with a slash).  Unfortunately, it's buried among a
lot of other fixes.  Maybe someone will like some of those also.  Two
files follow, indent-comment.el and c-fix.el.  Many of these functions are
modified versions of stuff in Emacs, I've tried to add a comment for each
place where I made a change.

-- 
Joe Wells <jbw@cs.bu.edu>
----------------------------------------------------------------------
;; Created by: Joe Wells, jbw@bucsf.bu.edu
;; Created on: 1987?
;; Last modified by: Joe Wells, jbw@bucsf.bu.edu
;; Last modified on: Fri Sep 28 05:28:11 1990
;; Filename: indent-comment.el
;; Purpose: Indent comment to correct column
;; Change log: 
;; 
;; 
;; Fri Sep 28 05:28:13 1990  Joe Wells  (jbw at bucsf.bu.edu)
;; 
;; 	* Now ignores comment start inside string in c-mode.
;; 

;; Mon Jul 30 23:43:31 1990  JBW  (JBW at _CORTEZ)
;; 
;; 	* Now ignores comments preceded by backslash.
;; 

(defmacro update-col (column diff bound)
  (` (if (>= (, column) (, bound))
	 (setq (, column) (+ (, column) (- (, diff) (, bound)))))))

(defun indent-comment ()
  "Indent this line's comment to comment column."
  (interactive "*")
  (if (null comment-start)
      (error "No comment syntax defined"))
  (let ((oldcol (current-column))       ;save-excursion won't work w. delete
	(eolpos (save-excursion (end-of-line) (point)))
	(indent-tabs-mode nil)
	bolpos indent commentpos codepos commentcol codecol)
    (beginning-of-line)
    (setq bolpos (point))
    (while (and (not (eolp))
		(null commentpos)
		(re-search-forward comment-start-skip eolpos 'move))
      (goto-char (match-beginning 0))
      (if (and (not (eq ?\\ (preceding-char)))
	       (not (and (eq 'c-mode major-mode)
			 (fboundp 'c-in-string-or-char)
			 (c-in-string-or-char))))
	  (setq commentpos (point)
		commentcol (current-column)
		indent (save-excursion (funcall comment-indent-hook)))
	(forward-char 1)))
    (and commentpos
	 (cond ((>= indent commentcol)
		(indent-to indent)
		(update-col oldcol indent commentcol))
	       ((eval t)
		(save-excursion
		  (skip-chars-backward " \t")
		  (setq codepos (point)
			codecol (current-column)))
		(cond ((< indent codecol)
		       (delete-region codepos commentpos)
		       (update-col oldcol codecol commentcol))
		      ((eval t)
		       (backward-delete-char-untabify (- commentcol indent))
		       (update-col oldcol indent commentcol)))
		(indent-to indent))))
    (move-to-column oldcol)))

(provide 'indent-comment)
----------------------------------------------------------------------
;; Created by: Joe Wells, jbw@bucsf
;; Created on: 1988?
;; Last modified by: Joe Wells, jbw@bigbird
;; Last modified on: Tue Feb 26 19:58:34 1991
;; Filename: c-fix.el
;; Purpose: Enhancements and fixes to c-mode
;; Change log: 
;; 
;; 
;; Sun Sep 30 01:29:05 1990  Joe Wells  (jbw at bucsf.bu.edu)
;; 
;; 	* Fixed indent-c-exp also for same reasons.
;; 
;; 	* Fixed c-indent-line to handle "} else" correctly.  Also handle
;; 	final "}" of top-level item correctly.
;; 
;; Fri Sep 28 04:26:23 1990  Joe Wells  (jbw at bucsf.bu.edu)
;; 
;; 	* Fixed fix in calculate-c-indent to make it an even more accurate
;; 	guess of whether we are in a function's args.
;; 
;; 	* Added c-lex-type using parse-partial-sexp to determine what type
;; 	of text we are in.  Rewrote several other functions to use this.
;; 
;; Thu Aug 23 21:07:48 1990  JBW  (JBW at _CORTEZ)
;; 
;; 	* Finished correct versions of c-in-comment and
;; 	c-in-string-or-char.  Provided c-forward-to-noncomment.  Kludged
;; 	c-extract-prototypes to fill in default type of "int" for
;; 	functions with no explicitly specified type.
;; 
;; Tue Aug 14 12:29:27 1990  JBW  (JBW at _CORTEZ)
;; 
;; 	* Added support for ... in c-extract-prototypes.  Added
;; 	c-align-max-ident-len to prevent aligning arguments too far to
;; 	right.
;; 
;; Tue Aug  7 13:41:54 1990  JBW  (JBW at _CORTEZ)
;; 
;; 	* Added c-extract-prototypes.
;; 
;; Mon Aug  6 10:04:44 1990  JBW  (JBW at _CORTEZ)
;; 
;; 	* Fixed c-move-to-defined-identifier for VAX C _align(page)
;; 	modifier.
;; 
;; Tue Jul 31 17:40:31 1990  JBW  (JBW at _CORTEZ)
;; 
;; 	* Added support for aligning structs, function parameters in
;; 	c-align-declarations.
;; 
;; Mon Jul 30 23:42:40 1990  JBW  (JBW at _CORTEZ)
;; 
;; 	* Added c-align-declarations.
;; 

(require 'indent-comment)

(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))))
		 ;; jbw: regexp enhanced to handle "} else" plus "else_var"
		 ((and (looking-at "\}?[ \t]*else\\b")
		       (not (looking-at "\}?[ \t]*else\\s_")))
		  (setq indent (save-excursion
				 ;; jbw: skip "}" of "} else"
				 (skip-chars-forward "\}")
				 (c-backward-to-start-of-if)
				 (current-indentation))))
		 ((= (following-char) ?})
		  (setq indent (- indent
				  ;; jbw: make sure the closing brace of a
				  ;; top-level block gets placed in the
				  ;; same column as the opening brace.
				  ;; this corresponds to a special case in
				  ;; calculate-c-indent, see the code there.
				  (if (and (zerop c-indent-level)
					   (save-excursion
					     (forward-char 1)
					     (backward-sexp 1)
					     (bolp)))
				      (+ c-brace-offset
					 c-continued-statement-offset)
				    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))


;;		(+ (if (and (bolp) (zerop c-indent-level))
;;		       (+ c-brace-offset c-continued-statement-offset)
;;		     c-indent-level)

;; Fixed to handle indenting function argument declarations correctly,
;; even when the function name is not at the beginning of a line.

(defun calculate-c-indent (&optional parse-start)
  "Return appropriate indentation for current line as C code.
In usual case returns an integer: the column to indent to.
Returns nil if line starts inside a string, t if in a comment."
  (save-excursion
    (beginning-of-line)
    (let ((indent-point (point))
	  (case-fold-search nil)
	  state
	  containing-sexp)
      (if parse-start
	  (goto-char parse-start)
	(beginning-of-defun))
      (while (< (point) indent-point)
	(setq parse-start (point))
	(setq state (parse-partial-sexp (point) indent-point 0))
	(setq containing-sexp (car (cdr state))))
      (cond ((or (nth 3 state) (nth 4 state))
	     ;; return nil or t if should not change this line
	     (nth 4 state))
	    ((null containing-sexp)
	     ;; Line is at top level.  May be data or function definition,
	     ;; or may be function argument declaration.
	     ;; Indent like the previous top level line
	     ;; unless that ends in a closeparen without semicolon,
	     ;; in which case this line is the first argument decl.
	     (goto-char indent-point)
	     (skip-chars-forward " \t")
	     (if (= (following-char) ?{)
		 0			; Unless it starts a function body
	       (c-backward-to-noncomment (or parse-start (point-min)))
	       ;; Look at previous line that's at column 0
	       ;; to determine whether we are in top-level decls
	       ;; or function's arg decls.  Set basic-indent accordinglu.
	       (let ((basic-indent
		      (save-excursion
			(re-search-backward "^[^ \^L\t\n#]" nil 'move)
			(if (and (looking-at "\\sw\\|\\s_")
				 ;; jbw: enhanced following testing to be
				 ;; more likely to correctly decide if we
				 ;; are in the argument declarations of an
				 ;; old-style function definition.  It's
				 ;; still not completely correct.
				 (parse-partial-sexp (point) parse-start 0)
				 (eq ?\) (preceding-char))
				 (or (skip-chars-forward " \t\f\n") t)
				 (not (memq (following-char) '(?\, ?\;))))
			    c-argdecl-indent
			  0))))
		 ;; Now add a little if this is a continuation line.
		 (+ basic-indent (if (or (bobp)
					 (memq (preceding-char) '(?\) ?\; ?\})))
				     ;; jbw: Should top level statements use
				     ;; c-continued-statement-offset???
				     0 c-continued-statement-offset)))))
	    ((/= (char-after containing-sexp) ?{)
	     ;; line is expression, not statement:
	     ;; indent to just after the surrounding open.
	     (goto-char (1+ containing-sexp))
	     (current-column))
	    (t
	     ;; Statement level.  Is it a continuation or a new statement?
	     ;; Find previous non-comment character.
	     (goto-char indent-point)
	     (c-backward-to-noncomment containing-sexp)
	     ;; Back up over label lines, since they don't
	     ;; affect whether our line is a continuation.
	     (while (or (eq (preceding-char) ?\,)
			(and (eq (preceding-char) ?:)
			     (or (eq (char-after (- (point) 2)) ?\')
				 (memq (char-syntax (char-after (- (point) 2)))
				       '(?w ?_)))))
	       (if (eq (preceding-char) ?\,)
		   (c-backward-to-start-of-continued-exp containing-sexp))
	       (beginning-of-line)
	       (c-backward-to-noncomment containing-sexp))
	     ;; Now we get the answer.
	     (if (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?\{)))
		 ;; This line is continuation of preceding line's statement;
		 ;; indent  c-continued-statement-offset  more than the
		 ;; previous line of the statement.
		 (progn
		   (c-backward-to-start-of-continued-exp containing-sexp)
		   (+ c-continued-statement-offset (current-column)
		      (if (save-excursion (goto-char indent-point)
					  (skip-chars-forward " \t")
					  (eq (following-char) ?{))
			  c-continued-brace-offset 0)))
	       ;; This line starts a new statement.
	       ;; Position following last unclosed open.
	       (goto-char containing-sexp)
	       ;; Is line first statement after an open-brace?
	       (or
		;; If no, find that first statement and indent like it.
		(save-excursion
		  (forward-char 1)
		  (let ((colon-line-end 0))
		    (while (progn (skip-chars-forward " \t\n")
				  (looking-at "#\\|/\\*\\|case[ \t\n].*:\\|[a-zA-Z0-9_$]*:"))
		      ;; Skip over comments and labels following openbrace.
		      (cond ((= (following-char) ?\#)
			     (forward-line 1))
			    ((= (following-char) ?\/)
			     (forward-char 2)
			     (search-forward "*/" nil 'move))
			    ;; case or label:
			    (t
			     (save-excursion (end-of-line)
					     (setq colon-line-end (point)))
			     (search-forward ":"))))
		    ;; The first following code counts
		    ;; if it is before the line we want to indent.
		    (and (< (point) indent-point)
			 (if (> colon-line-end (point))
			     (- (current-indentation) c-label-offset)
			   (current-column)))))
		;; If no previous statement,
		;; indent it relative to line brace is on.
		;; For open brace in column zero, don't let statement
		;; start there too.  If c-indent-level is zero,
		;; use c-brace-offset + c-continued-statement-offset instead.
		;; For open-braces not the first thing in a line,
		;; add in c-brace-imaginary-offset.
		(+ (if (and (bolp) (zerop c-indent-level))
		       (+ c-brace-offset c-continued-statement-offset)
		     c-indent-level)
		   ;; Move back over whitespace before the openbrace.
		   ;; If openbrace is not first nonwhite thing on the line,
		   ;; add the c-brace-imaginary-offset.
		   (progn (skip-chars-backward " \t")
			  (if (bolp) 0 c-brace-imaginary-offset))
		   ;; If the openbrace is preceded by a parenthesized exp,
		   ;; move to the beginning of that;
		   ;; possibly a different line
		   (progn
		     (if (eq (preceding-char) ?\))
			 (forward-sexp -1))
		     ;; Get initial indentation of the line we are on.
		     (current-indentation))))))))))

(defun indent-c-exp ()
  "Indent each line of the C grouping following point."
  (interactive)
  (let ((indent-stack (list nil))
	(contain-stack (list (point)))
	(case-fold-search nil)
	restart outer-loop-done inner-loop-done state ostate
	this-indent last-sexp
	at-else at-brace
	(opoint (point))
	(next-depth 0))
    (save-excursion
      (forward-sexp 1))
    (save-excursion
      (setq outer-loop-done nil)
      (while (and (not (eobp)) (not outer-loop-done))
	(setq last-depth next-depth)
	;; Compute how depth changes over this line
	;; plus enough other lines to get to one that
	;; does not end inside a comment or string.
	;; Meanwhile, do appropriate indentation on comment lines.
	(setq inner-loop-done nil)
	(while (and (not inner-loop-done)
		    (not (and (eobp) (setq outer-loop-done t))))
	  (setq ostate state)
	  (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
					  nil nil state))
	  (setq next-depth (car state))
	  (if (and (car (cdr (cdr state)))
		   (>= (car (cdr (cdr state))) 0))
	      (setq last-sexp (car (cdr (cdr state)))))
	  (if (or (nth 4 ostate))
	      (c-indent-line))
	  (if (or (nth 3 state))
	      (forward-line 1)
	    (setq inner-loop-done t)))
	(if (<= next-depth 0)
	    (setq outer-loop-done t))
	(if outer-loop-done
	    nil
	  ;; If this line had ..))) (((.. in it, pop out of the levels
	  ;; that ended anywhere in this line, even if the final depth
	  ;; doesn't indicate that they ended.
	  (while (> last-depth (nth 6 state))
	    (setq indent-stack (cdr indent-stack)
		  contain-stack (cdr contain-stack)
		  last-depth (1- last-depth)))
	  (if (/= last-depth next-depth)
	      (setq last-sexp nil))
	  ;; Add levels for any parens that were started in this line.
	  (while (< last-depth next-depth)
	    (setq indent-stack (cons nil indent-stack)
		  contain-stack (cons nil contain-stack)
		  last-depth (1+ last-depth)))
	  (if (null (car contain-stack))
	      (setcar contain-stack (or (car (cdr state))
					(save-excursion (forward-sexp -1)
							(point)))))
	  (forward-line 1)
	  (skip-chars-forward " \t")
	  (if (eolp)
	      nil
	    ;; jbw: ?????????
	    (setq at-else nil at-brace nil)
	    (if (and (car indent-stack)
		     (>= (car indent-stack) 0))
		;; Line is on an existing nesting level.
		;; Lines inside parens are handled specially.
		(if (/= (char-after (car contain-stack)) ?{)
		    (setq this-indent (car indent-stack))
		  ;; Line is at statement level.
		  ;; Is it a new statement?  Is it an else?
		  ;; Find last non-comment character before this line
		  (save-excursion
		    ;; jbw: enhanced regexp to handle "} else" plus "else_var"
		    (setq at-else (and (looking-at "\}?[ \t]*else\\b")
				       (not (looking-at "\}?[ \t]*else\\s_"))))
		    (setq at-brace (= (following-char) ?{))
		    (c-backward-to-noncomment opoint)
		    (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?{)))
			;; Preceding line did not end in comma or semi;
			;; indent this line  c-continued-statement-offset
			;; more than previous.
			(progn
			  (c-backward-to-start-of-continued-exp (car contain-stack))
			  (setq this-indent
				(+ c-continued-statement-offset (current-column)
				   (if at-brace c-continued-brace-offset 0))))
		      ;; Preceding line ended in comma or semi;
		      ;; use the standard indent for this level.
		      (if at-else
			  ;; jbw: enhanced to handle "} else"
			  (progn (skip-chars-forward " \t\f\n\}")
				 (c-backward-to-start-of-if opoint)
				 (setq this-indent (current-indentation)))
			(setq this-indent (car indent-stack))))))
	      ;; Just started a new nesting level.
	      ;; Compute the standard indent for this level.
	      (let ((val (calculate-c-indent
			   (if (car indent-stack)
			       (- (car indent-stack))))))
		(setcar indent-stack
			(setq this-indent val))))
	    ;; Adjust line indentation according to its contents
	    (if (or (looking-at "case[ \t]")
		    (and (looking-at "[A-Za-z]")
			 (save-excursion
			   (forward-sexp 1)
			   (looking-at ":"))))
		(setq this-indent (max 1 (+ this-indent c-label-offset))))
	    ;; jbw: handle "} else"
	    (if (and (= (following-char) ?}) (not at-else))
		(setq this-indent (- this-indent
				     ;; jbw: make sure the closing brace of a
				     ;; top-level block gets placed in the
				     ;; same column as the opening brace.
				     ;; this corresponds to a special case in
				     ;; calculate-c-indent, see the code there.
				     (if (and (zerop c-indent-level)
					      (save-excursion
						(forward-char 1)
						(backward-sexp 1)
						(bolp)))
					 (+ c-brace-offset
					    c-continued-statement-offset)
				       c-indent-level))))
	    (if (= (following-char) ?{)
		(setq this-indent (+ this-indent c-brace-offset)))
	    ;; Put chosen indentation into effect.
	    (or (= (current-column) this-indent)
		(= (following-char) ?\#)
		(progn
		  (delete-region (point) (progn (beginning-of-line) (point)))
		  (indent-to this-indent)))
	    ;; Indent any comment following the text.
	    (or (looking-at comment-start-skip)
		(if (re-search-forward comment-start-skip (save-excursion (end-of-line) (point)) t)
		    (progn (indent-for-comment) (beginning-of-line)))))))))
; (message "Indenting C expression...done")
  )

;; This has changed in 18.57, I've got to update my version:

(defun c-backward-to-noncomment (lim)
  (let (opoint stop)
    (while (not stop)
      (skip-chars-backward " \t\n\f" lim)
      (setq opoint (point))
      (if (and (>= (point) (+ 2 lim))
	       (save-excursion
		 (forward-char -2)
		 (looking-at "\\*/")))
	  (search-backward "/*" lim 'move)
	;; jbw: handle # lines continued with \ correctly
	(while (progn
		 (beginning-of-line)
		 (eq ?\\ (char-after (- (point) 2))))
	  (forward-char -1)
	  (beginning-of-line))
	(skip-chars-forward " \t")
	(setq stop (or (not (looking-at "#")) (<= (point) lim)))
	(if stop (goto-char opoint)
	  (beginning-of-line))))))

;; This is used by indent-for-comment
;; to decide how much to indent a comment in C code
;; based on its context.

;; jbw: did I modify this from the 18.55 one or is this based on
;; c-comment-indent from an earlier Emacs?

(defun c-comment-indent ()
  (save-excursion
    (skip-chars-backward " \t")
    (if (bolp)
	(let ((temp (calculate-c-indent))) ; t in commment, nil in string
	  (if (integerp temp) temp 0))
      (max (1+ (current-column))        ;Else indent at comment column
	   comment-column))))	        ; except leave at least one space.

(defun c-indent-command (&optional whole-exp)
  "Indent current line as C code, or in some cases insert a tab character.
If c-tab-always-indent is non-nil (the default), always indent current line.
Otherwise, indent the current line only if point is at the left margin
or in the line's indentation; otherwise insert a tab.

A numeric argument, regardless of its value,
means indent rigidly all the lines of the expression starting after point
so that this line becomes properly indented.
The relative indentation among the lines of the expression are preserved."
  (interactive "P")
  (if whole-exp
      ;; If arg, always indent this line as C
      ;; and shift remaining lines of expression the same amount.
      (let ((shift-amt (c-indent-line))
	    beg end)
	(save-excursion
	  (if c-tab-always-indent
	      (beginning-of-line))
	  (setq beg (point))
	  (forward-sexp 1)
	  (setq end (point))
	  (goto-char beg)
	  (forward-line 1)
	  (setq beg (point)))
	(if (> end beg)
	    (indent-code-rigidly beg end shift-amt "#")))
    (if (and (not c-tab-always-indent)
	     (save-excursion
	       (skip-chars-backward " \t")
	       (not (bolp))))
	(insert-tab)
      (c-indent-line)
      ;; jbw: indent comment also
      (indent-comment))))

(defun fix-c-map (map)
;;  (define-key map "\t"
;;    (function (lambda ()
;;		(interactive)
;;		(c-indent-line)
;;		(indent-comment))))
  (define-key map "\eQ" 'indent-c-exp)
  (define-key map "\C-c\t" 'indent-c-exp)
  (define-key map "\e\t" 'tag-complete-symbol)
  (define-key map "\C-x\t" 'c-align-declarations))

(if c-mode-map (fix-c-map c-mode-map))

(defun c-space-hook ()
  (cond ((and (eq this-command 'self-insert-command)
	      (eq last-command-char ? )
	      (not (c-in-comment))
	      (not (c-in-string-or-char)))
	 (insert ? )
	 (insert-parentheses nil)
	 (setq unread-command-char ?\C-?))))

(defun c-beginning-of-line ()
  (interactive)
  (if (c-in-comment)
      (search-backward "/*"))
  (while (progn
	   (while (progn
		    (beginning-of-line)
		    (c-in-comment))
	     (search-backward "/*"))
	   (eq ?\\ (char-after (- (point) 2))))
    (backward-char 1)))

(define-key esc-map "s" 'test-c-in-scc)
(defun test-c-in-scc ()
  (interactive)
  (message "%s" (c-lex-type)))

(defun c-lex-type (&optional start-point)
  (or (numberp start-point)
      (setq start-point (point)))
  (if (eq ?* (char-after start-point))
      (setq start-point (1+ start-point)))
  (save-excursion
    (save-restriction
      (let* ((state (progn
		      (beginning-of-defun)
		      (parse-partial-sexp (point) start-point)))
	     (stringp (nth 3 state))
	     (commentp (nth 4 state)))
	(and stringp commentp
	     (error "Can't be in string and comment at same time!"))
	(cond ((eq ?\" stringp)
	       'string)
	      ((eq ?\' stringp)
	       'char)
	      (stringp
	       (error "Impossible string type!"))
	      (commentp
	       'comment))))))

(defun c-in-comment ()
  (eq 'comment (c-lex-type))) 

;;(defun c-in-comment ()
;;  (let ((opoint (point)))
;;    (catch 'result
;;      (save-excursion
;;	(save-restriction
;;	  (narrow-to-region (save-excursion
;;			      (beginning-of-defun 1)
;;			      (point))
;;			    (point-max))
;;	  (or (search-backward "/*" nil t)
;;	      (throw 'result nil))
;;	  (while (c-in-string-or-char)
;;	    (or (search-backward "/*" nil t)
;;		(throw 'result nil)))
;;	  (if (search-forward "*/" nil 'move)
;;	      (goto-char (match-beginning 0)))
;;	  (>= (point) opoint))))))

;; (define-key esc-map "s" 'test-c-in-string-or-char)
;;(defun test-c-in-string-or-char ()
;;  (interactive)
;;  (message "%s" (or (c-in-string-or-char)
;;		    (if (c-in-comment)
;;			'comment))))

(defun c-in-string-or-char ()
  (let ((lex-type (c-lex-type)))
    (if (memq lex-type '(string char))
	lex-type)))

;;(defun c-in-string-or-char ()
;;  (let ((opoint (point))
;;	delimiter-type
;;	recursive-result)
;;    (catch 'result
;;      (save-excursion
;;	(save-restriction
;;	  (narrow-to-region (progn
;;			      (c-beginning-of-line)
;;			      (if (c-in-comment)
;;				  (search-forward "*/" nil 'move))
;;			      (point))
;;			    (point-max))
;;	  (if (< opoint (point-min))
;;	      (throw 'result nil))
;;	  (goto-char opoint)
;;	  (or (re-search-backward "['\"]" nil t)
;;	      (throw 'result nil))
;;	  (while (c-in-comment)
;;	    (or (re-search-backward "['\"]" nil t)
;;		(throw 'result nil)))
;;	  (or (setq delimiter-type (cdr (assq (following-char)
;;					      '((?\" . string)
;;						(?' . char)))))
;;	      (throw 'result nil))
;;	  (setq recursive-result (c-in-string-or-char))
;;	  (cond ((null recursive-result)
;;		 delimiter-type)
;;		((and (eq recursive-result delimiter-type)
;;		      (not (eq ?\\ (preceding-char))))
;;		 nil)
;;		(t
;;		 recursive-result)))))))

(defun c-on-cpp-line (&optional start-point)
  (save-excursion
    (if (numberp start-point)
	(goto-char start-point))
    (c-beginning-of-line)
    (looking-at "[ \t\f]*#")))

(defvar c-skip-line-regexp "[ \t]*\\(#\\|/\\*\\|\n\\)")

(defun c-in-or-before-comment ()
  (or (c-in-comment)
      (save-excursion
	(skip-chars-forward " \t\f\n")
	(skip-chars-forward "/")
	(skip-chars-forward "*")
	(c-in-comment))))

(defun c-forward-to-noncomment ()
  (while (and (progn (skip-chars-forward " \t\f\n") t)
	      (not (eobp))
	      (cond ((c-in-or-before-comment)
		     (search-forward "*/" nil 'move))
		    ((c-on-cpp-line)
		     (forward-line 1))))))

(defun c-move-to-defined-identifier ()
  (interactive)
  (let ((keyword-count 0))
    (while (progn
	     (skip-chars-forward " \t\n\f")
	     (cond ((looking-at c-type-keyword-regexp)
		    (setq keyword-count (1+ keyword-count)))
		   ((looking-at c-sc-keyword-regexp))
		   ((looking-at "_align[ \t]*([^\)]*)"))
		   ((looking-at "globaldef[ \t]*{[^\}]*}"))))
      (goto-char (match-end 0)))
    (if (eq 0 keyword-count)
	(skip-chars-forward "A-Za-z0-9_"))
    (skip-chars-forward " \t\n\(*")))

(defvar c-type-keyword-regexp
  "\\b\\(int\\|long\\|short\\|void\\|unsigned\\|char\\)\\b")

(defvar c-sc-keyword-regexp
  "\\b\\(auto\\|register\\|static\\|extern\\|volatile\\|const\\|typedef\\)\\b")

(defvar c-align-max-ident-len 25)

(defun c-align-declarations ()
  (interactive)
  (let* ((old-pos (point-marker))
	 (begin
	  (save-excursion
	    (if (re-search-backward "\\(^\\|\{\\)[ \t]*$" nil 'move)
		(search-forward "\n"))
	    (point)))
	 (indent-level
	  (save-excursion
	    (goto-char begin)
	    (c-indent-line)
	    (beginning-of-line 1)
	    (skip-chars-forward " \t\f")
	    (current-column)))
	 (end
	  (save-excursion
	    (if (re-search-forward "^\\(\{\\|\}\\|[ \t]*$\\)" nil 'move)
		(search-backward "\n"))
	    (point)))
	 (max-col 0)
	 (max-id-len 0)
	 (first-line t)
	 col len spaces)
    (save-restriction
      (narrow-to-region begin end)
      (goto-char (point-min))
      (while (progn
	       (c-forward-to-noncomment)
	       (not (eobp)))
	(beginning-of-line 1)
	(skip-chars-forward " \t\f")
	(setq col (current-column))
	(cond ((< col indent-level)
	       (indent-to indent-level))
	      ((> col indent-level)
	       (move-to-column-force indent-level)
	       (re-search-forward "[ \t]+")
	       (replace-match "")))
	(c-move-to-defined-identifier)
	(while (progn
		 (skip-chars-backward "*\(")
		 (memq (preceding-char) '(?  ?\t)))
	  (delete-char -1))
	(insert ? )
	(skip-chars-forward "*\(")
	(setq col (current-column))
	(if (> col max-col)
	    (setq max-col col))
	(forward-sexp 1)
	(setq len (- (current-column) col))
	(if (> len max-id-len)
	    (setq max-id-len len))
	(if (> max-id-len c-align-max-ident-len)
	    (setq max-id-len c-align-max-ident-len))
	(forward-line 1))
      (goto-char (point-min))
      (while (progn
	       (c-forward-to-noncomment)
	       (not (eobp)))
	(c-move-to-defined-identifier)
	(setq spaces (+ 2 (- max-col (current-column))))
	(skip-chars-backward "*\(")
	(or (eq ?  (preceding-char))
	    (error "impossible"))
	(delete-char -1)
	(indent-to (+ (current-column) spaces))
	(skip-chars-forward " \t\(*")
	(setq len (- (- (point)
			(progn
			  (forward-sexp 1)
			  (point)))))
	(cond ((looking-at "[ \t]*\(")
	       (replace-match "\(")
	       (backward-char 1)
	       (indent-to (+ max-col max-id-len 2) 1)))
	(indent-comment)
	(forward-line 1)))
    (goto-char old-pos)
    (set-marker old-pos nil)))

(defun c-extract-prototypes ()
  (interactive)
  (let (proto-list proto-buffer proto)
    (save-excursion
      (goto-char (point-min))
      (while (beginning-of-defun -1)
	(setq proto-list
	      (cons
	       (buffer-substring
		(save-excursion
		  (skip-chars-backward " \t\f\n")
		  (point))
		(save-excursion
		  (search-backward "\(")
		  (re-search-backward "[\};/]")
		  (forward-char 1)
		  (c-forward-to-noncomment)
		  (point)))
	       proto-list)))
      (setq proto-list (nreverse proto-list))
      (setq proto-buffer (get-buffer-create
			  (concat "*" (buffer-name) " Prototypes*")))
      (set-buffer proto-buffer)
      (erase-buffer)
      (or (eq major-mode 'c-mode)
	  (c-mode))
      (while (consp proto-list)
	(setq proto (car proto-list))
	(narrow-to-region (point) (point))
	(insert "extern " proto)
	(goto-char (point-min))
	(while (re-search-forward "[ \t\n\f]+" nil t)
	  (replace-match " "))
	(goto-char (point-min))
	(while (re-search-forward " \\([\),]\\)" nil t)
	  (replace-match "\\1"))
	(goto-char (point-min))
	(while (re-search-forward "\( " nil t)
	  (replace-match "\("))
	(goto-char (point-min))
	(c-move-to-defined-identifier)
	;; kludge to handle ambiguity: functions with no specified type
	(skip-chars-backward "\(")
	(if (looking-at "\(")
	    (save-excursion
	      (backward-sexp 1)
	      (insert "int "))
	  (forward-sexp 1))
	(while (progn
		 (skip-chars-forward " \)")
		 (not (eobp)))
	  (cond ((or (looking-at "\\[[^\]]\\]")
		     (looking-at "(void)"))
		 (goto-char (match-end 0)))
		((looking-at "([A-Za-z_0-9]+\\(, ?[A-Za-z_0-9]+\\)*)")
		 (replace-match "()"))
		((looking-at "\(")
		 (forward-char 1)
		 (while (not (looking-at "\)"))
		   (c-move-to-defined-identifier)
		   (or (looking-at "\\.\\.\\.\)")
		       (delete-region
			(progn
			  (skip-chars-backward " ")
			  (point))
			(progn
			  (forward-sexp 1)
			  (point))))
		   (re-search-forward "[,\)]" nil t)
		   (skip-chars-backward "\)")))
		(t
		 (delete-region
		  (progn
		    (skip-chars-backward " ")
		    (point))
		  (point-max)))))
	(widen)
	(goto-char (point-max))
	(insert ";\n")
	(setq proto-list (cdr proto-list)))
      (goto-char (point-min))
      (c-align-declarations)
      (set-buffer-modified-p nil))
    (display-buffer proto-buffer)))

(provide 'c-fix)
Technical Details
Message-ID: <JBW.91Apr12141321@bigbird.bu.edu>
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!dg-rtp!rock.concert.net!mcnc!stanford.edu!agate!ucbvax!tut.cis.ohio-state.edu!unreplyable!garbage
References: <9104102003.AA11010@life.ai.mit.edu>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fb2f1,a8da5d0a61b440c4
X-Google-Attributes: gidfb2f1,public
X-Google-ArrivalTime: 1991-04-13 04:01:09 PST
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!dg-rtp!rock.concert.net!mcnc!stanford.edu!agate!ucbvax!tut.cis.ohio-state.edu!unreplyable!garbage
From: jbw@BIGBIRD.BU.EDU (Joe Wells)
Newsgroups: gnu.emacs.bug
Subject: Re: Help on indent-for-comment
Message-ID: <JBW.91Apr12141321@bigbird.bu.edu>
Date: 12 Apr 91 18:13:21 GMT
References: <9104102003.AA11010@life.ai.mit.edu>
Sender: daemon@tut.cis.ohio-state.edu
Distribution: gnu
Organization: Boston University Computer Science Department
Lines: 909
Approved: bug-gnu-emacs@prep.ai.mit.edu