cc-compat.el 5.6 KB

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