cc-compat.el 5.6 KB

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