(shell-mode): Use define-derived-mode.
[emacs.git] / lisp / progmodes / cc-compat.el
blob44ab7acbcadc187e73d3109e54ef7b8f39cefa20
1 ;;; cc-compat.el --- cc-mode compatibility with c-mode.el confusion
3 ;; Copyright (C) 1985,1987,1992-2000 Free Software Foundation, Inc.
5 ;; Authors: 2000- Martin Stjernholm
6 ;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
7 ;; 1994-1997 Barry A. Warsaw
8 ;; Maintainer: bug-cc-mode@gnu.org
9 ;; Created: August 1994, split from cc-mode.el
10 ;; Version: See cc-mode.el
11 ;; Keywords: c languages oop
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
30 ;;; Commentary:
32 ;; Boring old c-mode.el (BOCM) is confusion and brain melt. cc-mode.el
33 ;; is clarity of thought and purity of chi. If you are still unwilling
34 ;; to accept enlightenment, this might help, or it may prolong your
35 ;; agony.
37 ;; To use, add the following to your c-mode-hook:
39 ;; (require 'cc-compat)
40 ;; (c-set-style "BOCM")
42 ;;; Code:
44 (eval-when-compile
45 (let ((load-path
46 (if (and (boundp 'byte-compile-current-file)
47 (stringp byte-compile-current-file))
48 (cons (file-name-directory byte-compile-current-file)
49 load-path)
50 load-path)))
51 (load "cc-defs" nil t)))
52 (require 'cc-styles)
53 (require 'cc-engine)
56 ;; In case c-mode.el isn't loaded
57 (defvar c-indent-level 2
58 "*Indentation of C statements with respect to containing block.")
59 (defvar c-brace-imaginary-offset 0
60 "*Imagined indentation of a C open brace that actually follows a statement.")
61 (defvar c-brace-offset 0
62 "*Extra indentation for braces, compared with other text in same context.")
63 (defvar c-argdecl-indent 5
64 "*Indentation level of declarations of C function arguments.")
65 (defvar c-label-offset -2
66 "*Offset of C label lines and case statements relative to usual indentation.")
67 (defvar c-continued-statement-offset 2
68 "*Extra indent for lines not starting new statements.")
69 (defvar c-continued-brace-offset 0
70 "*Extra indent for substatements that start with open-braces.
71 This is in addition to c-continued-statement-offset.")
75 ;; these offsets are taken by brute force testing c-mode.el, since
76 ;; there's no logic to what it does.
77 (let* ((offsets '(c-offsets-alist .
78 ((defun-block-intro . cc-block-intro-offset)
79 (statement-block-intro . cc-block-intro-offset)
80 (defun-open . 0)
81 (class-open . 0)
82 (inline-open . c-brace-offset)
83 (block-open . c-brace-offset)
84 (block-close . cc-block-close-offset)
85 (brace-list-open . c-brace-offset)
86 (substatement-open . cc-substatement-open-offset)
87 (substatement . c-continued-statement-offset)
88 (knr-argdecl-intro . c-argdecl-indent)
89 (case-label . c-label-offset)
90 (access-label . c-label-offset)
91 (label . c-label-offset)
92 ))))
93 (c-add-style "BOCM" offsets))
96 (defun cc-block-intro-offset (langelem)
97 ;; taken directly from calculate-c-indent confusion
98 (save-excursion
99 (c-backward-syntactic-ws)
100 (if (eq (char-before) ?{)
101 (forward-char -1)
102 (goto-char (cdr langelem)))
103 (let* ((curcol (save-excursion
104 (goto-char (cdr langelem))
105 (current-column)))
106 (bocm-lossage
107 ;; If no previous statement, indent it relative to line
108 ;; brace is on. For open brace in column zero, don't let
109 ;; statement start there too. If c-indent-level is zero,
110 ;; use c-brace-offset + c-continued-statement-offset
111 ;; instead. For open-braces not the first thing in a line,
112 ;; add in c-brace-imaginary-offset.
113 (+ (if (and (bolp) (zerop c-indent-level))
114 (+ c-brace-offset c-continued-statement-offset)
115 c-indent-level)
116 ;; Move back over whitespace before the openbrace. If
117 ;; openbrace is not first nonwhite thing on the line,
118 ;; add the c-brace-imaginary-offset.
119 (progn (skip-chars-backward " \t")
120 (if (bolp) 0 c-brace-imaginary-offset))
121 ;; If the openbrace is preceded by a parenthesized exp,
122 ;; move to the beginning of that; possibly a different
123 ;; line
124 (progn
125 (if (eq (char-before) ?\))
126 (c-forward-sexp -1))
127 ;; Get initial indentation of the line we are on.
128 (current-indentation)))))
129 (- bocm-lossage curcol))))
132 (defun cc-block-close-offset (langelem)
133 (save-excursion
134 (let* ((here (point))
135 bracep
136 (curcol (progn
137 (goto-char (cdr langelem))
138 (current-column)))
139 (bocm-lossage (progn
140 (goto-char (cdr langelem))
141 (if (eq (char-after) ?{)
142 (setq bracep t)
143 (goto-char here)
144 (beginning-of-line)
145 (backward-up-list 1)
146 (forward-char 1)
147 (c-forward-syntactic-ws))
148 (current-column))))
149 (- bocm-lossage curcol
150 (if bracep 0 c-indent-level)))))
153 (defun cc-substatement-open-offset (langelem)
154 (+ c-continued-statement-offset c-continued-brace-offset))
157 (provide 'cc-compat)
158 ;;; cc-compat.el ends here