From: liberte@cs.uiuc.edu
Subject: Re: help with C-mode variables
Organization: Gatewayed from the GNU Project mailing list help-gnu-emacs@prep.ai.mit.edu
Date: Tue, 17 Nov 1992 06:11:02 GMT

	From: ksampath@magnus.acs.ohio-state.edu (Krishna S Sampath)
	 
	is there any way to set these variables automatically? like if you are 
	editing two c programs with different indentations, it would be very
	convenient to not fight with these variables every single time....

Time to repost c-style.el.

dan
------

;; c-style.el --- sets c-style control variables.
;;
;; LCD Archive Entry:
;; c-style|Daniel LaLiberte|liberte@cs.uiuc.edu
;; |sets c-style control variables
;; |Thu Feb 27 13:42:57 CST 1992|Version: 2.1|~/as-is/c-src-doc.el.Z
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; There are several ways to call set-c-style described below.
;;; None of these methods reindent your program - they only affect
;;; new indentation.
;;;
;;; - Just call set-c-style in your c-mode-hook.
;;;    Without style argument, default-c-style will be used.
;;;    With style argument, this will set the style for every 
;;;    c-mode buffer the same.
;;;
;;; - Call set-c-style from the Local Variables list.
;;;    e.g. "eval:(set-c-style 'C++)"
;;;
;;; - Call set-c-style interactively.  It prompts for the style name
;;;    with completion using default-c-style.
;;;
;;; For convenience, put one of the following in your .emacs:
;;;    (autoload 'set-c-style "c-style" nil t)
;;; or (load "c-style")
;;; =====================================================

(defvar default-c-style 'GNU
  "*The default value of c-style.  Set this in your .emacs.")

;; The following predefined styles are all I know about.
;; If you learn of another style that has a "big" following, please
;; send me the parameters.

(defvar c-style-alist 
  '((GNU 
     (c-indent-level 2)
     (c-continued-statement-offset 2)
     (c-brace-offset 0)
     (c-argdecl-indent 5)
     (c-label-offset -2))

    (BSD
     (c-indent-level 8)
     (c-continued-statement-offset 8)
     (c-brace-offset -8)
     (c-argdecl-indent 8)
     (c-label-offset -8))

    (K&R 
     (c-indent-level 5)
     (c-continued-statement-offset 5)
     (c-brace-offset -5)
     (c-argdecl-indent 0)
     (c-label-offset -5))

    (BS   ; was C++ 
     (c-indent-level 4)
     (c-continued-statement-offset 4)
     (c-brace-offset -4)
     (c-argdecl-indent 4)
     (c-label-offset -4))

    ;; From Lynn Slater
    (LRS
     (c-indent-level 4)
     (c-continued-statement-offset 4)
     (c-brace-offset 0)
     (c-argdecl-indent 4)
     (c-label-offset -2)
     (c-auto-newline nil))

    (Plauger
     (c-indent-level 0)
     (c-continued-statement-offset 8)
     (c-continued-brace-offset -8)
     (c-brace-offset 8)
     (c-brace-imaginary-offset 0)
     (c-argdecl-indent 0)
     (c-label-offset -8)
     (c-auto-newline t)
     (c-tab-always-indent t))

    ;; From Jozsef A Toth <jtoth+@pitt.edu>
    ;; Is this really the Whitesmith style?
    (Alman				
     (c-argdecl-indent 0)
     (c-brace-imaginary-offset 2)  ;;; ????
     (c-brace-offset 0)
     (c-continued-statement-offset 2)
     (c-indent-level 0)
     (c-label-offset -2)
     (c-auto-newline t)
     (comment-column 40)
     (tab-width 2)
     (fill-column '79))

    (Gould
     (c-indent-level 4)
     (c-continued-statement-offset 4)
     (c-brace-offset -4)
     (c-argdecl-indent 8)
     (c-label-offset -2)
     (c-brace-imaginary-offset 0))
     
    ;; From Joan Eslinger <wombat@kilimanjaro.key.amdahl.com>
    (WRS 
     (c-indent-level 0)
     (c-continued-statement-offset 4)
     (c-brace-offset 0)
     (c-argdecl-indent 4)
     (c-label-offset -2)
     (c-brace-imaginary-offset 4)
     (c-continued-brace-offset -4))
    ))
  
(defvar c-style nil
  "The buffer local c-mode indentation style.")

;; Add style name to mode line.  Assumes minor-mode-alist is not buffer local.
;; Thanks to Joan Eslinger.

(defvar c-style-name nil
  "The style name for a c-mode indentation style.
This is to be set by set-c-style, and used by the mode line.")

(or (assq 'c-style-name minor-mode-alist)
    (nconc minor-mode-alist
	   '((c-style-name c-style-name))))  ; use undocumented feature

(defun set-c-style (&optional style)
  "Set up the c-mode style variables from STYLE if it is given, or
default-c-style otherwise.  It makes the c indentation style variables
buffer local."

  (interactive)

  (let ((c-styles (mapcar 'car c-style-alist))) ; for completion
    (if (interactive-p)
	(setq style
	      (let ((style-string	; Get style name with completion.
		     (completing-read
		      (format "Set c-mode indentation style to (default %s): "
			      default-c-style)
		      (vconcat c-styles)
		      (function (lambda (arg) (memq arg c-styles)))
		      )))
		(if (string-equal "" style-string)
		    default-c-style
		  (intern style-string))
		)))
    
    ;; If style is nil, use default-c-style.
    (setq style (or style default-c-style))
    
    (make-local-variable 'c-style)
    (if (memq style c-styles)
	(setq c-style style)
      (error "Undefined c style: %s" style)
      )
    (message "c-style: %s" c-style)
    
    ;; Set the c-style-name
    (make-local-variable 'c-style-name)
    (setq c-style-name (format " %s" c-style))

    ;; Finally, set the indentation style variables making each one local.
    (mapcar (function (lambda (c-style-pair)
			(make-local-variable (car c-style-pair))
			(set (car c-style-pair)
			     (car (cdr c-style-pair)))))
	    (cdr (assq c-style c-style-alist)))
    c-style
    ))