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