annotate lisp/modes/old-c-mode.el @ 9:6f2bbbbbe05a

Added tag r19-15b5 for changeset 4b173ad71786
author cvs
date Mon, 13 Aug 2007 08:47:36 +0200
parents 376386a54a3c
children 0293115a14e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; c-mode.el --- C code editing commands for Emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;; Copyright (C) 1985, 86, 87, 92, 94, 95 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;; Maintainer: FSF
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Keywords: c
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;; along with XEmacs; see the file COPYING. If not, write to the Free
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;;; Synched up with: FSF 19.30.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;; A smart editing mode for C code. It knows a lot about C syntax and tries
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;; to position the cursor according to C layout conventions. You can
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; change the details of the layout style with option variables. Load it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;; and do M-x describe-mode for details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 (defvar c-mode-abbrev-table nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 "Abbrev table in use in C mode.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 (define-abbrev-table 'c-mode-abbrev-table ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 (defvar c-mode-map ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 "Keymap used in C mode.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;; XEmacs change: if cc-mode is loaded, then override its keymap.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;; otherwise things will be really screwed up.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 (if (or (null c-mode-map) (featurep 'cc-mode))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 (setq c-mode-map (make-sparse-keymap))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 (set-keymap-name c-mode-map 'c-mode-map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (define-key c-mode-map "{" 'electric-c-brace)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 (define-key c-mode-map "}" 'electric-c-brace)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (define-key c-mode-map ";" 'electric-c-semi)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (define-key c-mode-map "#" 'electric-c-sharp-sign)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 (define-key c-mode-map ":" 'electric-c-terminator)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (define-key c-mode-map "\e{" 'c-insert-braces)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ;; Commented out electric square brackets because nobody likes them.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;;(define-key c-mode-map "[" 'c-insert-brackets)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 (define-key c-mode-map "\e\C-h" 'mark-c-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 (define-key c-mode-map "\e\C-q" 'indent-c-exp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (define-key c-mode-map "\ea" 'c-beginning-of-statement)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (define-key c-mode-map "\ee" 'c-end-of-statement)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 (define-key c-mode-map "\C-c\C-n" 'c-forward-conditional)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 (define-key c-mode-map "\C-c\C-p" 'c-backward-conditional)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 (define-key c-mode-map "\C-c\C-u" 'c-up-conditional)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (define-key c-mode-map "\177" 'backward-delete-char-untabify)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (define-key c-mode-map "\t" 'c-indent-command)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (defvar c-mode-popup-menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 (purecopy '("C Menu"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 ["Comment Out Region" comment-region (region-exists-p)]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 ["Macro Expand Region" c-macro-expand (region-exists-p)]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 ["Backslashify" c-backslash-region (region-exists-p)]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 "---"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 ["Indent Expression" indent-c-exp t]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 ["Indent Line" c-indent-command t]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 ["Fill Comment Paragraph" c-fill-paragraph t]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 "---"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 ["Highlight Conditionals" cpp-highlight-buffer t]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 ["Up Conditional" c-up-conditional t]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 ["Backward Conditional" c-backward-conditional t]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 ["Forward Conditional" c-forward-conditional t]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 "---"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 ["Backward Statement" c-beginning-of-statement t]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 ["Forward Statement" c-end-of-statement t]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 )))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (defvar c-mode-menubar-menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (purecopy (cons "C" (cdr c-mode-popup-menu))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 ;; cmacexp is lame because it uses no preprocessor symbols.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 ;; It isn't very extensible either -- hardcodes /lib/cpp.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (autoload 'c-macro-expand "cmacexp"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 "Display the result of expanding all C macros occurring in the region.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 The expansion is entirely correct because it uses the C preprocessor."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (defvar c-mode-syntax-table nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 "Syntax table in use in C-mode buffers.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (if c-mode-syntax-table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (setq c-mode-syntax-table (make-syntax-table))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (modify-syntax-entry ?\\ "\\" c-mode-syntax-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (modify-syntax-entry ?/ ". 14" c-mode-syntax-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (modify-syntax-entry ?* ". 23" c-mode-syntax-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (modify-syntax-entry ?+ "." c-mode-syntax-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (modify-syntax-entry ?- "." c-mode-syntax-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (modify-syntax-entry ?= "." c-mode-syntax-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (modify-syntax-entry ?% "." c-mode-syntax-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (modify-syntax-entry ?< "." c-mode-syntax-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (modify-syntax-entry ?> "." c-mode-syntax-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (modify-syntax-entry ?& "." c-mode-syntax-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (modify-syntax-entry ?| "." c-mode-syntax-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (modify-syntax-entry ?\' "\"" c-mode-syntax-table))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (defvar c-indent-level 2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 "*Indentation of C statements with respect to containing block.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (defvar c-brace-imaginary-offset 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 "*Imagined indentation of a C open brace that actually follows a statement.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (defvar c-brace-offset 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 "*Extra indentation for braces, compared with other text in same context.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (defvar c-argdecl-indent 5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 "*Indentation level of declarations of C function arguments.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (defvar c-label-offset -2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 "*Offset of C label lines and case statements relative to usual indentation.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (defvar c-continued-statement-offset 2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 "*Extra indent for lines not starting new statements.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (defvar c-continued-brace-offset 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 "*Extra indent for substatements that start with open-braces.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 This is in addition to `c-continued-statement-offset'.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (defconst c-style-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 '(("GNU"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (c-indent-level . 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (c-argdecl-indent . 5)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (c-brace-offset . 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (c-continued-brace-offset . 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (c-label-offset . -2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (c-continued-statement-offset . 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 ("K&R"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (c-indent-level . 5)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (c-argdecl-indent . 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (c-brace-offset . 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (c-continued-brace-offset . -5)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (c-label-offset . -5)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (c-continued-statement-offset . 5))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 ("BSD"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (c-indent-level . 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (c-argdecl-indent . 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (c-brace-offset . 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (c-continued-brace-offset . -4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (c-label-offset . -4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (c-continued-statement-offset . 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 ("C++"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (c-indent-level . 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (c-argdecl-indent . 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (c-brace-offset . 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (c-continued-brace-offset . -4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (c-label-offset . -4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (c-continued-statement-offset . 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (c-auto-newline . t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 ("Whitesmith"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (c-indent-level . 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (c-argdecl-indent . 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (c-brace-offset . 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 (c-continued-brace-offset . 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (c-label-offset . -4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (c-continued-statement-offset . 4))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (defvar c-auto-newline nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 "*Non-nil means automatically newline before and after braces,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 and after colons and semicolons, inserted in C code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 If you do not want a leading newline before braces then use:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (define-key c-mode-map \"{\" 'electric-c-semi)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (defvar c-tab-always-indent t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 "*Non-nil means TAB in C mode should always reindent the current line,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 regardless of where in the line point is when the TAB command is used.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 ;;; Regular expression used internally to recognize labels in switch
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 ;;; statements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (defvar c-switch-label-regexp (purecopy "case[ \t'/(]\\|default[ \t]*:"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 ;;; This is actually the expression for C++ mode, but it's used for C too.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 ;(defvar c-imenu-generic-expression
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 ; (`
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 ; ((nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 ; (,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 ; (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 ; "^" ; beginning of line is required
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 ; "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 ; "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; type specs; there can be no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 ; "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; more than 3 tokens, right?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 ; "\\(" ; last type spec including */&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 ; "[a-zA-Z0-9_:]+"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 ; "\\([ \t]*[*&]+[ \t]*\\|[ \t]+\\)" ; either pointer/ref sign or whitespace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 ; "\\)?" ; if there is a last type spec
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 ; "\\(" ; name; take that into the imenu entry
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 ; "[a-zA-Z0-9_:~]+" ; member function, ctor or dtor...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 ; ; (may not contain * because then
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 ; ; "a::operator char*" would become "char*"!)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 ; "\\|"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 ; "\\([a-zA-Z0-9_:~]*::\\)?operator"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 ; "[^a-zA-Z1-9_][^(]*" ; ...or operator
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 ; " \\)"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 ; "[ \t]*([^)]*)[ \t\n]*[^ ;]" ; require something other than a ; after
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 ; ; the (...) to avoid prototypes. Can't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 ; ; catch cases with () inside the parentheses
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 ; ; surrounding the parameters
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 ; ; (like "int foo(int a=bar()) {...}"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 ; )) 6)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 ; ("Class"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 ; (, (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 ; "^" ; beginning of line is required
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 ; "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 ; "class[ \t]+"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 ; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 ; "[ \t]*[:{]"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 ; )) 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 ;;; Example of generic expression for finding prototypes, structs, unions, enums.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 ;;; Uncomment if you want to find these too. It will be a bit slower gathering
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 ;;; the indexes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 ;; ("Prototypes"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 ;; (,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 ;; (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 ;; "^" ; beginning of line is required
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 ;; "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 ;; "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; type specs; there can be no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 ;; "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; more than 3 tokens, right?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 ;; "\\(" ; last type spec including */&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 ;; "[a-zA-Z0-9_:]+"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 ;; "\\([ \t]*[*&]+[ \t]*\\|[ \t]+\\)" ; either pointer/ref sign or whitespace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 ;; "\\)?" ; if there is a last type spec
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 ;; "\\(" ; name; take that into the imenu entry
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 ;; "[a-zA-Z0-9_:~]+" ; member function, ctor or dtor...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 ;; ; (may not contain * because then
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 ;; ; "a::operator char*" would become "char*"!)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 ;; "\\|"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 ;; "\\([a-zA-Z0-9_:~]*::\\)?operator"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 ;; "[^a-zA-Z1-9_][^(]*" ; ...or operator
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 ;; " \\)"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 ;; "[ \t]*([^)]*)[ \t\n]*;" ; require ';' after
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 ;; ; the (...) Can't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 ;; ; catch cases with () inside the parentheses
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 ;; ; surrounding the parameters
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 ;; ; (like "int foo(int a=bar());"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 ;; )) 6)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 ;; ("Struct"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 ;; (, (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 ;; "^" ; beginning of line is required
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 ;; "\\(static[ \t]+\\)?" ; there may be static or const.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 ;; "\\(const[ \t]+\\)?"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 ;; "struct[ \t]+"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 ;; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 ;; "[ \t]*[{]"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 ;; )) 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 ;; ("Enum"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 ;; (, (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 ;; "^" ; beginning of line is required
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 ;; "\\(static[ \t]+\\)?" ; there may be static or const.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 ;; "\\(const[ \t]+\\)?"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 ;; "enum[ \t]+"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 ;; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 ;; "[ \t]*[{]"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 ;; )) 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 ;; ("Union"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 ;; (, (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 ;; "^" ; beginning of line is required
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 ;; "\\(static[ \t]+\\)?" ; there may be static or const.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 ;; "\\(const[ \t]+\\)?"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 ;; "union[ \t]+"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 ;; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 ;; "[ \t]*[{]"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 ;; )) 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 ; ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 ; "Imenu generic expression for C mode. See `imenu-generic-expression'.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 (defun c-mode ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 "Major mode for editing C code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 Expression and list commands understand all C brackets.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 Tab indents for C code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 Comments are delimited with /* ... */.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 Paragraphs are separated by blank lines only.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 Delete converts tabs to spaces as it moves back.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 \\{c-mode-map}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 Variables controlling indentation style:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 c-tab-always-indent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 Non-nil means TAB in C mode should always reindent the current line,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 regardless of where in the line point is when the TAB command is used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 c-auto-newline
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 Non-nil means automatically newline before and after braces,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 and after colons and semicolons, inserted in C code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 c-indent-level
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 Indentation of C statements within surrounding block.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 The surrounding block's indentation is the indentation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 of the line on which the open-brace appears.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 c-continued-statement-offset
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 Extra indentation given to a substatement, such as the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 then-clause of an if or body of a while.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 c-continued-brace-offset
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 Extra indentation given to a brace that starts a substatement.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 This is in addition to c-continued-statement-offset.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 c-brace-offset
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 Extra indentation for line if it starts with an open brace.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 c-brace-imaginary-offset
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 An open brace following other text is treated as if it were
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 this far to the right of the start of its line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 c-argdecl-indent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 Indentation level of declarations of C function arguments.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 c-label-offset
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 Extra indentation for line that is a label, or case or default.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 Settings for K&R and BSD indentation styles are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 c-indent-level 5 8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 c-continued-statement-offset 5 8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 c-brace-offset -5 -8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 c-argdecl-indent 0 8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 c-label-offset -5 -8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 Turning on C mode calls the value of the variable c-mode-hook with no args,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 if that value is non-nil."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 (kill-all-local-variables)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 (use-local-map c-mode-map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 (setq major-mode 'c-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 (setq mode-popup-menu c-mode-popup-menu)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 (setq mode-name "C")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 (setq local-abbrev-table c-mode-abbrev-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 (set-syntax-table c-mode-syntax-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 (make-local-variable 'paragraph-start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 (setq paragraph-start (concat "$\\|" page-delimiter))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 (make-local-variable 'paragraph-separate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 (setq paragraph-separate paragraph-start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 (make-local-variable 'paragraph-ignore-fill-prefix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 (setq paragraph-ignore-fill-prefix t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 (make-local-variable 'fill-paragraph-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 (setq fill-paragraph-function 'c-fill-paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 (make-local-variable 'indent-line-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 (setq indent-line-function 'c-indent-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 (make-local-variable 'indent-region-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 (setq indent-region-function 'c-indent-region)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 (make-local-variable 'require-final-newline)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 (setq require-final-newline t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 (make-local-variable 'outline-regexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 (setq outline-regexp "[^#\n\^M]")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 (make-local-variable 'outline-level)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 (setq outline-level 'c-outline-level)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 (make-local-variable 'comment-start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 (setq comment-start "/* ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 (make-local-variable 'comment-end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 (setq comment-end " */")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 (make-local-variable 'comment-column)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 (setq comment-column 32)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 (make-local-variable 'comment-start-skip)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 (setq comment-start-skip "/\\*+ *")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 (make-local-variable 'comment-indent-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 (setq comment-indent-function 'c-comment-indent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 (make-local-variable 'comment-multi-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 (setq comment-multi-line t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 (make-local-variable 'parse-sexp-ignore-comments)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 (setq parse-sexp-ignore-comments t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 (if (featurep 'menubar)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 ;; make a local copy of the menubar, so our modes don't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 ;; change the global menubar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 (set-buffer-menubar current-menubar)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 (add-submenu nil c-mode-menubar-menu)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 ; (make-local-variable 'imenu-generic-expression)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 ; (setq imenu-generic-expression c-imenu-generic-expression)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 (run-hooks 'c-mode-hook))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 (defun c-outline-level ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 (skip-chars-forward "\t ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 (current-column)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 ^L
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 ;; This is used by indent-for-comment
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 ;; to decide how much to indent a comment in C code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 ;; based on its context.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 (defun c-comment-indent ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 (if (looking-at "^/\\*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 0 ;Existing comment at bol stays there.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 (let ((opoint (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 (cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 ;; A comment following a solitary close-brace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 ;; should have only one space.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 (search-forward "}")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 (1+ (current-column)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 ((or (looking-at "^#[ \t]*endif[ \t]*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 (looking-at "^#[ \t]*else[ \t]*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 7) ;2 spaces after #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 ((progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 (goto-char opoint)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 (skip-chars-backward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 (and (= comment-column 0) (bolp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 ;; If comment-column is 0, and nothing but space
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 ;; before the comment, align it at 0 rather than 1.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 (max (1+ (current-column)) ;Else indent at comment column
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 comment-column))))))) ; except leave at least one space.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 (defun c-fill-paragraph (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 "Like \\[fill-paragraph] but handle C comments.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 If any of the current line is a comment or within a comment,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 fill the comment or the paragraph of it that point is in,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 preserving the comment indentation or line-starting decorations."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 (let* (comment-start-place
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 (first-line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 ;; Check for obvious entry to comment.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 (skip-chars-forward " \t\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 (and (looking-at comment-start-skip)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 (setq comment-start-place (point))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 (if (and (eq major-mode 'c++-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 (looking-at ".*//")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 (let (fill-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 (paragraph-start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 ;; Lines containing just a comment start or just an end
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 ;; should not be filled into paragraphs they are next to.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 paragraph-start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 (paragraph-separate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 paragraph-separate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 ;; Move up to first line of this comment.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 (while (and (not (bobp)) (looking-at "[ \t]*//"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 (forward-line -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 (if (not (looking-at ".*//"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 (forward-line 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 ;; Find the comment start in this line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 (re-search-forward "[ \t]*//[ \t]*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 ;; Set the fill-prefix to be what all lines except the first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 ;; should start with.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 (let ((endcol (current-column)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 (skip-chars-backward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 (setq fill-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 (concat (make-string (- (current-column) 2) ?\ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 "//"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 (make-string (- endcol (current-column)) ?\ ))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 ;; Narrow down to just the lines of this comment.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 (narrow-to-region (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 (forward-line 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 (while (looking-at "[ \t]*//")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 (forward-line 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 (insert fill-prefix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 (fill-paragraph arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 (delete-region (point-min)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 (+ (point-min) (length fill-prefix))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 (if (or first-line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 ;; t if we enter a comment between start of function and this line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 (eq (calculate-c-indent) t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 ;; t if this line contains a comment starter.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 (setq first-line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 (prog1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 (re-search-forward comment-start-skip
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 (save-excursion (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 (setq comment-start-place (point))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 ;; Inside a comment: fill one comment paragraph.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 (let ((fill-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 ;; The prefix for each line of this paragraph
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 ;; is the appropriate part of the start of this line,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 ;; up to the column at which text should be indented.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 (if (looking-at "[ \t]*/\\*.*\\*/")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 (progn (re-search-forward comment-start-skip)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 (make-string (current-column) ?\ ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 (if first-line (forward-line 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 (let ((line-width (progn (end-of-line) (current-column))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 (prog1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 (buffer-substring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 ;; How shall we decide where the end of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 ;; fill-prefix is?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 ;; calculate-c-indent-within-comment bases its value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 ;; on the indentation of previous lines; if they're
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 ;; indented specially, it could return a column
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 ;; that's well into the current line's text. So
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 ;; we'll take at most that many space, tab, or *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 ;; characters, and use that as our fill prefix.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 (let ((max-prefix-end
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 (move-to-column
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 (calculate-c-indent-within-comment t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 (skip-chars-forward " \t*" max-prefix-end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 ;; Don't include part of comment terminator
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 ;; in the fill-prefix.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 (and (eq (following-char) ?/)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 (eq (preceding-char) ?*)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 (backward-char 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 ;; If the comment is only one line followed by a blank
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 ;; line, calling move-to-column above may have added
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 ;; some spaces and tabs to the end of the line; the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 ;; fill-paragraph function will then delete it and the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 ;; newline following it, so we'll lose a blank line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 ;; when we shouldn't. So delete anything
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 ;; move-to-column added to the end of the line. We
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 ;; record the line width instead of the position of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 ;; old line end because move-to-column might break a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 ;; tab into spaces, and the new characters introduced
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 ;; there shouldn't be deleted.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 ;; If you can see a better way to do this, please make
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 ;; the change. This seems very messy to me.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 (delete-region (progn (move-to-column line-width)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 (progn (end-of-line) (point))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 (paragraph-start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 ;; Lines containing just a comment start or just an end
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 ;; should not be filled into paragraphs they are next to.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 (concat paragraph-start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 ;; This:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 ;; "\\|[ \t]*/\\*[ \t]*$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 ;; "\\|[ \t]*\\*/[ \t]*$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 ;; "\\|[ \t/*]*$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 ;; is just this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 "\\|[ \t/*]*$"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 (paragraph-separate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 (concat paragraph-separate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 ;; This:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 ;; "\\|[ \t]*/\\*[ \t]*$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 ;; "\\|[ \t]*\\*/[ \t]*$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 ;; "\\|[ \t/*]*$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 ;; is just this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 "\\|[ \t/*]*$"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 (chars-to-delete 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 ;; Don't fill the comment together with the code following it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 ;; So temporarily exclude everything before the comment start,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 ;; and everything after the line where the comment ends.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 ;; If comment-start-place is non-nil, the comment starter is there.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 ;; Otherwise, point is inside the comment.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 (narrow-to-region (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 (if comment-start-place
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 (goto-char comment-start-place)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 (search-backward "/*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 ;; Protect text before the comment start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 ;; by excluding it. Add spaces to bring back
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 ;; proper indentation of that point.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 (let ((column (current-column)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 (prog1 (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 (setq chars-to-delete column)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 (insert-char ?\ column))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 (if comment-start-place
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 (goto-char (+ comment-start-place 2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 (search-forward "*/" nil 'move)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 (forward-line 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 (goto-char (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 (forward-line -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 ;; And comment terminator was on a separate line before,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 ;; keep it that way.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 ;; This also avoids another problem:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 ;; if the fill-prefix ends in a *, it could eat up
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 ;; the * of the comment terminator.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 (if (looking-at "[ \t]*\\*/")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 (narrow-to-region (point-min) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 (fill-paragraph arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 ;; Delete the chars we inserted to avoid clobbering
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 ;; the stuff before the comment start.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 (if (> chars-to-delete 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 (delete-region (point) (+ (point) chars-to-delete)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 ;; Find the comment ender (should be on last line of buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 ;; given the narrowing) and don't leave it on its own line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 ;; Do this with a fill command, so as to preserve sentence
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 ;; boundaries.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 (goto-char (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 (forward-line -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 (search-forward "*/" nil 'move)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 (if (looking-at "[ \t]*\\*/")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 (let ((fill-column (+ fill-column 9999)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 (forward-line -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 (fill-region-as-paragraph (point) (point-max)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 ;; Outside of comments: do ordinary filling.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 (fill-paragraph arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 (defun electric-c-brace (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 "Insert character and correct line's indentation."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 (let ((insertpos nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 (if (and (not arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 (eolp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 (or (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 (skip-chars-backward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 (bolp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 (if c-auto-newline (progn (c-indent-line) (newline) t) nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 (insert last-command-char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 (c-indent-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 (if c-auto-newline
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 (newline)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 ;; (newline) may have done auto-fill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 (setq insertpos (- (point) 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 (c-indent-line)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 (if insertpos (goto-char (1+ insertpos)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 (delete-char -1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 (if insertpos
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 (goto-char insertpos)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 (self-insert-command (prefix-numeric-value arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 (self-insert-command (prefix-numeric-value arg)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 (defun c-insert-brackets ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 (insert ?[)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 (insert ?])))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 (defun c-insert-braces ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 (setq last-command-char ?{)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 (electric-c-brace 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 (newline)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 (c-indent-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 (newline)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 (insert ?})
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 (c-indent-line)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 (defun electric-c-sharp-sign (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 "Insert character and correct line's indentation."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 (if (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 (skip-chars-backward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 (bolp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 (let ((c-auto-newline nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 (electric-c-terminator arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 (self-insert-command (prefix-numeric-value arg))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 (defun electric-c-semi (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 "Insert character and correct line's indentation."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 (if c-auto-newline
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 (electric-c-terminator arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 (self-insert-command (prefix-numeric-value arg))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 (defun electric-c-terminator (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 "Insert character and correct line's indentation."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 (let (insertpos (end (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 (if (and (not arg) (eolp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 (not (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 (skip-chars-forward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677 (or (= (following-char) ?#)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 ;; Colon is special only after a label, or case ....
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 ;; So quickly rule out most other uses of colon
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 ;; and do no indentation for them.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 (and (eq last-command-char ?:)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 (not (looking-at c-switch-label-regexp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 (skip-chars-forward "a-zA-Z0-9_$")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 (skip-chars-forward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 (< (point) end)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 (beginning-of-defun)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 (let ((pps (parse-partial-sexp (point) end)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 (insert last-command-char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 (c-indent-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 (and c-auto-newline
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 (not (c-inside-parens-p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 (newline)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 ;; (newline) may have done auto-fill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 (setq insertpos (- (point) 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 (c-indent-line)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 (if insertpos (goto-char (1+ insertpos)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 (delete-char -1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 (if insertpos
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 (goto-char insertpos)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 (self-insert-command (prefix-numeric-value arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 (self-insert-command (prefix-numeric-value arg)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 (defun c-inside-parens-p ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711 (condition-case ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 (narrow-to-region (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 (progn (beginning-of-defun) (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 (goto-char (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 (error nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 (defun c-indent-command (&optional whole-exp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721 "Indent current line as C code, or in some cases insert a tab character.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 If `c-tab-always-indent' is non-nil (the default), always indent current line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723 Otherwise, indent the current line only if point is at the left margin
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 or in the line's indentation; otherwise insert a tab.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 A numeric argument, regardless of its value, means indent rigidly all
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727 the lines of the expression starting after point so that this line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 becomes properly indented. The relative indentation among the lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 of the expression are preserved."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 (if whole-exp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 ;; If arg, always indent this line as C
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 ;; and shift remaining lines of expression the same amount.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 (let ((shift-amt (c-indent-line))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 beg end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 (if c-tab-always-indent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 (beginning-of-line))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739 ;; Find beginning of following line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741 (forward-line 1) (setq beg (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 ;; Find first beginning-of-sexp for sexp extending past this line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 (while (< (point) beg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744 (forward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 (setq end (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746 (skip-chars-forward " \t\n")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747 (if (> end beg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 (indent-code-rigidly beg end shift-amt "#")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 (if (and (not c-tab-always-indent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 (skip-chars-backward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 (not (bolp))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 (insert-tab)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754 (c-indent-line))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 (defun c-indent-line ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 "Indent current line as C code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 Return the amount the indentation changed by."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 (let ((indent (calculate-c-indent nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 beg shift-amt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761 (case-fold-search nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762 (pos (- (point-max) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764 (setq beg (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 (cond ((eq indent nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 (setq indent (current-indentation)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 ((eq indent t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 (setq indent (calculate-c-indent-within-comment)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 ((looking-at "[ \t]*#")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 (setq indent 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 (skip-chars-forward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773 (if (listp indent) (setq indent (car indent)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 (cond ((or (looking-at c-switch-label-regexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775 (and (looking-at "[A-Za-z]")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777 (forward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 (looking-at ":"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 (setq indent (max 1 (+ indent c-label-offset))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780 ((and (looking-at "else\\b")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781 (not (looking-at "else\\s_")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 (setq indent (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 (c-backward-to-start-of-if)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 (current-indentation))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 ((and (looking-at "}[ \t]*else\\b")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 (not (looking-at "}[ \t]*else\\s_")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 (setq indent (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788 (forward-char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 (backward-sexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790 (c-backward-to-start-of-if)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 (current-indentation))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 ((and (looking-at "while\\b")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 (not (looking-at "while\\s_"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795 (c-backward-to-start-of-do)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 ;; This is a `while' that ends a do-while.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 (setq indent (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 (c-backward-to-start-of-do)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799 (current-indentation))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 ((= (following-char) ?})
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801 (setq indent (- indent c-indent-level)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 ((= (following-char) ?{)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 (setq indent (+ indent c-brace-offset))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 (skip-chars-forward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805 (setq shift-amt (- indent (current-column)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806 (if (zerop shift-amt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 (if (> (- (point-max) pos) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808 (goto-char (- (point-max) pos)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 (delete-region beg (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810 (indent-to indent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811 ;; If initial point was within line's indentation,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
812 ;; position after the indentation. Else stay at same point in text.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813 (if (> (- (point-max) pos) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814 (goto-char (- (point-max) pos))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 shift-amt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
817 (defun calculate-c-indent (&optional parse-start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
818 "Return appropriate indentation for current line as C code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
819 In usual case returns an integer: the column to indent to.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
820 Returns nil if line starts inside a string, t if in a comment."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
821 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
822 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
823 (let ((indent-point (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
824 (case-fold-search nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
825 state
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
826 containing-sexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
827 (if parse-start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
828 (goto-char parse-start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
829 (beginning-of-defun))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
830 (while (< (point) indent-point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
831 (setq parse-start (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
832 (setq state (parse-partial-sexp (point) indent-point 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
833 (setq containing-sexp (car (cdr state))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
834 (cond ((or (nth 3 state) (nth 4 state))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
835 ;; return nil or t if should not change this line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
836 (nth 4 state))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
837 ((null containing-sexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
838 ;; Line is at top level. May be data or function definition,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
839 ;; or may be function argument declaration.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
840 ;; Indent like the previous top level line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
841 ;; unless that ends in a closeparen without semicolon,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
842 ;; in which case this line is the first argument decl.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
843 (goto-char indent-point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
844 (skip-chars-forward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
845 (if (= (following-char) ?{)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
846 0 ; Unless it starts a function body
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
847 (c-backward-to-noncomment (or parse-start (point-min)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
848 ;; Look at previous line that's at column 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
849 ;; to determine whether we are in top-level decls
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
850 ;; or function's arg decls. Set basic-indent accordingly.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
851 (let ((basic-indent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
852 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
853 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
854 (let (comment lim)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
855 ;; Recognize the DEFUN macro in Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
856 (if (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
857 ;; Move down to the (putative) argnames line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
858 (while (and (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
859 (not (looking-at " *[({}#/]")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
860 (forward-line 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
861 ;; Go back to the DEFUN, if it is one.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
862 (condition-case nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
863 (backward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
864 (error))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
865 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
866 (looking-at "DEFUN\\b"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
867 c-argdecl-indent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
868 (if (and (looking-at "\\sw\\|\\s_")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
869 ;; This is careful to stop at the first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
870 ;; paren if we have
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
871 ;; int foo Proto ((int, int));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
872 (looking-at "[^\"\n=(]*(")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
873 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
874 (goto-char (1- (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
875 ;; Skip any number of paren-groups.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
876 ;; Consider typedef int (*fcn) (int);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
877 (while (= (following-char) ?\()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
878 (setq lim (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
879 (condition-case nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
880 (forward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
881 (error))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
882 (skip-chars-forward " \t\f"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
883 ;; Have we reached something
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
884 ;; that shows this isn't a function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
885 ;; definition?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
886 (and (< (point) indent-point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
887 (not (memq (following-char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
888 '(?\, ?\;)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
889 ;; Make sure the "function decl" we found
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
890 ;; is not inside a comment.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
891 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
892 ;; Move back to the `(' starting arglist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
893 (goto-char lim)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
894 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
895 (while (and (not comment)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
896 (search-forward "/*" lim t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
897 (setq comment
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
898 (not (search-forward "*/" lim t))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
899 (not comment)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
900 c-argdecl-indent 0))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
901 basic-indent)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
902
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
903 ;; ;; Now add a little if this is a continuation line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
904 ;; (+ basic-indent (if (or (bobp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
905 ;; (memq (preceding-char) '(?\) ?\; ?\}))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
906 ;; ;; Line with zero indentation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
907 ;; ;; is probably the return-type
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
908 ;; ;; of a function definition,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
909 ;; ;; so following line is function name.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
910 ;; (= (current-indentation) 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
911 ;; 0 c-continued-statement-offset))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
912
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
913 ((/= (char-after containing-sexp) ?{)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
914 ;; line is expression, not statement:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
915 ;; indent to just after the surrounding open.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
916 (goto-char (1+ containing-sexp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
917 (current-column))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
918 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
919 ;; Statement level. Is it a continuation or a new statement?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
920 ;; Find previous non-comment character.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
921 (goto-char indent-point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
922 (c-backward-to-noncomment containing-sexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
923 ;; Back up over label lines, since they don't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
924 ;; affect whether our line is a continuation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
925 (while (or (eq (preceding-char) ?\,)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
926 (and (eq (preceding-char) ?:)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
927 (or (eq (char-after (- (point) 2)) ?\')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
928 (memq (char-syntax (char-after (- (point) 2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
929 '(?w ?_)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
930 (if (eq (preceding-char) ?\,)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
931 (progn (forward-char -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
932 (c-backward-to-start-of-continued-exp containing-sexp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
933 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
934 (c-backward-to-noncomment containing-sexp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
935 ;; Check for a preprocessor statement or its continuation lines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
936 ;; Move back to end of previous non-preprocessor line,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
937 ;; or possibly beginning of buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
938 (let ((found (point)) stop)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
939 (while (not stop)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
940 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
941 (cond ((bobp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
942 (setq found (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
943 stop t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
944 ((save-excursion (forward-char -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
945 (= (preceding-char) ?\\))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
946 (forward-char -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
947 ;; This line is not preceded by a backslash.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
948 ;; So either it starts a preprocessor command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
949 ;; or any following continuation lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
950 ;; should not be skipped.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
951 ((= (following-char) ?#)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
952 (forward-char -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
953 (setq found (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
954 (t (setq stop t))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
955 (goto-char found))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
956 ;; Now we get the answer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
957 (if (and (not (memq (preceding-char) '(0 ?\, ?\; ?\} ?\{)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
958 ;; But don't treat a line with a close-brace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
959 ;; as a continuation. It is probably the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
960 ;; end of an enum type declaration.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
961 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
962 (goto-char indent-point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
963 (skip-chars-forward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
964 (not (= (following-char) ?}))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
965 ;; This line is continuation of preceding line's statement;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
966 ;; indent c-continued-statement-offset more than the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
967 ;; previous line of the statement.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
968 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
969 (c-backward-to-start-of-continued-exp containing-sexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
970 (+ c-continued-statement-offset (current-column)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
971 (if (save-excursion (goto-char indent-point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
972 (skip-chars-forward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
973 (eq (following-char) ?{))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
974 c-continued-brace-offset 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
975 ;; This line starts a new statement.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
976 ;; Position following last unclosed open.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
977 (goto-char containing-sexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
978 ;; Is line first statement after an open-brace?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
979 (or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
980 ;; If no, find that first statement and indent like it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
981 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
982 (forward-char 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
983 (let ((colon-line-end 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
984 (while (progn (skip-chars-forward " \t\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
985 (looking-at "#\\|/\\*\\|case[ \t\n'/(].*:\\|[a-zA-Z0-9_$]*:"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
986 ;; Skip over comments and labels following openbrace.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
987 (cond ((= (following-char) ?\#)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
988 (forward-line 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
989 ((= (following-char) ?\/)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
990 (forward-char 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
991 (search-forward "*/" nil 'move))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
992 ;; case or label:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
993 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
994 (save-excursion (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
995 (setq colon-line-end (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
996 (search-forward ":"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
997 ;; The first following code counts
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
998 ;; if it is before the line we want to indent.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
999 (and (< (point) indent-point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1000 (-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1001 (if (> colon-line-end (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1002 (- (current-indentation) c-label-offset)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1003 (current-column))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1004 ;; If prev stmt starts with open-brace, that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1005 ;; open brace was offset by c-brace-offset.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1006 ;; Compensate to get the column where
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1007 ;; an ordinary statement would start.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1008 (if (= (following-char) ?\{) c-brace-offset 0)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1009 ;; If no previous statement,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1010 ;; indent it relative to line brace is on.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1011 (calculate-c-indent-after-brace))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1012
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1013 (defun calculate-c-indent-after-brace ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1014 "Return the proper C indent for the first line after an open-brace.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1015 This function is called with point before the brace."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1016 ;; For open brace in column zero, don't let statement
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1017 ;; start there too. If c-indent-level is zero,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1018 ;; use c-brace-offset + c-continued-statement-offset instead.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1019 ;; For open-braces not the first thing in a line,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1020 ;; add in c-brace-imaginary-offset.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1021 (+ (if (and (bolp) (zerop c-indent-level))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1022 (+ c-brace-offset c-continued-statement-offset)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1023 c-indent-level)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1024 ;; Move back over whitespace before the openbrace.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1025 ;; If openbrace is not first nonwhite thing on the line,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1026 ;; add the c-brace-imaginary-offset.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1027 (progn (skip-chars-backward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1028 (if (bolp) 0 c-brace-imaginary-offset))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1029 ;; If the openbrace is preceded by a parenthesized exp,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1030 ;; move to the beginning of that;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1031 ;; possibly a different line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1032 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1033 (if (eq (preceding-char) ?\))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1034 (forward-sexp -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1035 ;; Get initial indentation of the line we are on.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1036 (current-indentation))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1037
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1038 (defun calculate-c-indent-within-comment (&optional after-star)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1039 "Return the indentation amount for line inside a block comment.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1040 Optional arg AFTER-STAR means, if lines in the comment have a leading star,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1041 return the indentation of the text that would follow this star."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1042 (let (end star-start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1043 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1044 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1045 (skip-chars-forward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1046 (setq star-start (= (following-char) ?\*))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1047 (skip-chars-backward " \t\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1048 (setq end (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1049 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1050 (skip-chars-forward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1051 (if after-star
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1052 (and (looking-at "\\*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1053 (re-search-forward "\\*[ \t]*")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1054 (and (re-search-forward "/\\*[ \t]*" end t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1055 star-start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1056 (not after-star)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1057 (goto-char (1+ (match-beginning 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1058 (if (and (looking-at "[ \t]*$") (= (preceding-char) ?\*))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1059 (1+ (current-column))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1060 (current-column)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1061
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1062
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1063 (defun c-backward-to-noncomment (lim)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1064 (let (opoint stop)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1065 (while (not stop)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1066 (skip-chars-backward " \t\n\f" lim)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1067 (setq opoint (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1068 (if (and (>= (point) (+ 2 lim))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1069 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1070 (forward-char -2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1071 (looking-at "\\*/")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1072 (search-backward "/*" lim 'move)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1073 (setq stop (or (<= (point) lim)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1074 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1075 (while (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1076 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1077 (eq ?\\ (char-after (- (point) 2))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1078 (forward-char -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1079 (beginning-of-line))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1080 (skip-chars-forward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1081 (not (looking-at "#")))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1082 (or stop (beginning-of-line))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1083
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1084 (defun c-backward-to-start-of-continued-exp (lim)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1085 (if (memq (preceding-char) '(?\) ?\"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1086 (forward-sexp -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1087 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1088 (if (<= (point) lim)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1089 (goto-char (1+ lim)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1090 (skip-chars-forward " \t"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1091
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1092 (defun c-backward-to-start-of-if (&optional limit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1093 "Move to the start of the last \"unbalanced\" `if'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1094 (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1095 (let ((if-level 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1096 (case-fold-search nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1097 (while (and (not (bobp)) (not (zerop if-level)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1098 (backward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1099 (cond ((and (looking-at "else\\b")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1100 (not (looking-at "else\\s_")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1101 (setq if-level (1+ if-level)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1102 ((and (looking-at "if\\b")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1103 (not (looking-at "if\\s_")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1104 (setq if-level (1- if-level)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1105 ((< (point) limit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1106 (setq if-level 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1107 (goto-char limit))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1108
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1109 (defun c-backward-to-start-of-do (&optional limit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1110 "If point follows a `do' statement, move to beginning of it and return t.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1111 Otherwise return nil and don't move point."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1112 (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1113 (let ((first t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1114 (startpos (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1115 (done nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1116 (while (not done)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1117 (let ((next-start (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1118 (condition-case nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1119 ;; Move back one token or one brace or paren group.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1120 (backward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1121 ;; If we find an open-brace, we lose.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1122 (error (setq done 'fail)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1123 (if done
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1124 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1125 ;; If we reached a `do', we win.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1126 (if (looking-at "do\\b")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1127 (setq done 'succeed)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1128 ;; Otherwise, if we skipped a semicolon, we lose.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1129 ;; (Exception: we can skip one semicolon before getting
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1130 ;; to a the last token of the statement, unless that token
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1131 ;; is a close brace.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1132 (if (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1133 (forward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1134 (or (and (not first) (= (preceding-char) ?}))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1135 (search-forward ";" next-start t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1136 (if (and first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1137 (/= (preceding-char) ?}))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1138 2 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1139 (setq done 'fail)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1140 (setq first nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1141 ;; If we go too far back in the buffer, we lose.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1142 (if (< (point) limit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1143 (setq done 'fail)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1144 (if (eq done 'succeed)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1145 t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1146 (goto-char startpos)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1147 nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1148
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1149 (defun c-beginning-of-statement (count)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1150 "Go to the beginning of the innermost C statement.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1151 With prefix arg, go back N - 1 statements. If already at the beginning of a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1152 statement then go to the beginning of the preceding one.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1153 If within a string or comment, or next to a comment (only whitespace between),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1154 move by sentences instead of statements."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1155 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1156 (let ((here (point)) state)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1157 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1158 (beginning-of-defun)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1159 (setq state (parse-partial-sexp (point) here nil nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1160 (if (or (nth 3 state) (nth 4 state)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1161 (looking-at (concat "[ \t]*" comment-start-skip))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1162 (save-excursion (skip-chars-backward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1163 (goto-char (- (point) 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1164 (looking-at "\\*/")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1165 (forward-sentence (- count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1166 (while (> count 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1167 (c-beginning-of-statement-1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1168 (setq count (1- count)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1169 (while (< count 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1170 (c-end-of-statement-1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1171 (setq count (1+ count))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1172
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1173 (defun c-end-of-statement (count)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1174 "Go to the end of the innermost C statement.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1175 With prefix arg, go forward N - 1 statements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1176 Move forward to end of the next statement if already at end.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1177 If within a string or comment, move by sentences instead of statements."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1178 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1179 (c-beginning-of-statement (- count)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1180
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1181 (defun c-beginning-of-statement-1 ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1182 (let ((last-begin (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1183 (first t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1184 (condition-case ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1185 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1186 (while (and (not (bobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1187 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1188 (backward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1189 (or first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1190 (not (re-search-forward "[;{}]" last-begin t)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1191 (setq last-begin (point) first nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1192 (goto-char last-begin))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1193 (error (if first (backward-up-list 1) (goto-char last-begin))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1194
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1195 (defun c-end-of-statement-1 ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1196 (condition-case ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1197 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1198 (while (and (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1199 (let ((beg (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1200 (forward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1201 (let ((end (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1202 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1203 (goto-char beg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1204 (not (re-search-forward "[;{}]" end t)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1205 (re-search-backward "[;}]")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1206 (forward-char 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1207 (error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1208 (let ((beg (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1209 (backward-up-list -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1210 (let ((end (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1211 (goto-char beg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1212 (search-forward ";" end 'move))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1213
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1214 (defun mark-c-function ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1215 "Put mark at end of C function, point at beginning."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1216 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1217 (push-mark (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1218 (end-of-defun)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1219 (push-mark (point) nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1220 (beginning-of-defun)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1221 (backward-paragraph))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1222
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1223 ;; Idea of ENDPOS is, indent each line, stopping when
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1224 ;; ENDPOS is encountered. But it's too much of a pain to make that work.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1225 (defun indent-c-exp (&optional endpos)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1226 "Indent each line of the C grouping following point."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1227 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1228 (let* ((indent-stack (list nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1229 (opoint (point)) ;; May be altered below.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1230 (contain-stack
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1231 (list (if endpos
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1232 (let (funbeg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1233 ;; Find previous fcn-start.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1234 (save-excursion (forward-char 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1235 (beginning-of-defun)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1236 (setq funbeg (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1237 (setq opoint funbeg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1238 ;; Try to find containing open,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1239 ;; but don't scan past that fcn-start.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1240 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1241 (narrow-to-region funbeg (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1242 (condition-case nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1243 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1244 (backward-up-list 1) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1245 ;; We gave up: must be between fcns.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1246 ;; Set opoint to beg of prev fcn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1247 ;; since otherwise calculate-c-indent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1248 ;; will get wrong answers.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1249 (error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1250 ;;;#### Commented-out in XEmacs, present in FSFmacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1251 ; (setq opoint funbeg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1252 (point)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1253 (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1254 (case-fold-search nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1255 restart outer-loop-done inner-loop-done state ostate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1256 this-indent last-sexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1257 at-else at-brace at-while
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1258 last-depth this-point
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1259 (next-depth 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1260 ;; If the braces don't match, get an error right away.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1261 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1262 (forward-sexp 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1263 ;; Realign the comment on the first line, even though we don't reindent it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1264 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1265 (let ((beg (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1266 (and (re-search-forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1267 comment-start-skip
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1268 (save-excursion (end-of-line) (point)) t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1269 ;; Make sure this isn't a comment alone on a line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1270 ;; (which should be indented like code instead).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1271 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1272 (goto-char (match-beginning 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1273 (skip-chars-backward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1274 (not (bolp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1275 ;; Make sure the comment starter we found
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1276 ;; is not actually in a string or quoted.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1277 (let ((new-state
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1278 (parse-partial-sexp beg (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1279 nil nil state)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1280 (and (not (nth 3 new-state)) (not (nth 5 new-state))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1281 (progn (indent-for-comment) (beginning-of-line)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1282 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1283 (setq outer-loop-done nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1284 (while (and (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1285 (if endpos
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1286 (< (point) endpos)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1287 (not outer-loop-done)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1288 (setq last-depth next-depth)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1289 ;; Compute how depth changes over this line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1290 ;; plus enough other lines to get to one that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1291 ;; does not end inside a comment or string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1292 ;; Meanwhile, do appropriate indentation on comment lines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1293 (setq inner-loop-done nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1294 (while (and (not inner-loop-done)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1295 (not (and (eobp) (setq outer-loop-done t))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1296 (setq ostate state)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1297 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1298 nil nil state))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1299 (setq next-depth (car state))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1300 (if (and (car (cdr (cdr state)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1301 (>= (car (cdr (cdr state))) 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1302 (setq last-sexp (car (cdr (cdr state)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1303 ;; If this line started within a comment, indent it as such.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1304 (if (or (nth 4 ostate) (nth 7 ostate))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1305 (c-indent-line))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1306 ;; If it ends outside of comments or strings, exit the inner loop.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1307 ;; Otherwise move on to next line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1308 (if (or (nth 3 state) (nth 4 state) (nth 7 state))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1309 (forward-line 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1310 (setq inner-loop-done t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1311 (and endpos
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1312 (while (< next-depth 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1313 (setq indent-stack (append indent-stack (list nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1314 (setq contain-stack (append contain-stack (list nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1315 (setq next-depth (1+ next-depth))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1316 (setq last-depth (1+ last-depth))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1317 (setcar (nthcdr 6 state) (1+ (nth 6 state)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1318 (setq outer-loop-done (and (not endpos) (<= next-depth 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1319 (if outer-loop-done
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1320 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1321 ;; If this line had ..))) (((.. in it, pop out of the levels
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1322 ;; that ended anywhere in this line, even if the final depth
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1323 ;; doesn't indicate that they ended.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1324 (while (> last-depth (nth 6 state))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1325 (setq indent-stack (cdr indent-stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1326 contain-stack (cdr contain-stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1327 last-depth (1- last-depth)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1328 (if (/= last-depth next-depth)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1329 (setq last-sexp nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1330 ;; Add levels for any parens that were started in this line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1331 (while (< last-depth next-depth)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1332 (setq indent-stack (cons nil indent-stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1333 contain-stack (cons nil contain-stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1334 last-depth (1+ last-depth)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1335 (if (null (car contain-stack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1336 (setcar contain-stack (or (car (cdr state))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1337 (save-excursion (forward-sexp -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1338 (point)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1339 (forward-line 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1340 (skip-chars-forward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1341 ;; Don't really reindent if the line is just whitespace,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1342 ;; or if it is past the endpos.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1343 ;; (The exit test in the outer while
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1344 ;; does not exit until we have passed the first line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1345 ;; past the region.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1346 (if (or (eolp) (and endpos (>= (point) endpos)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1347 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1348 ;; Is this line in a new nesting level?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1349 ;; In other words, is this the first line that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1350 ;; starts in the new level?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1351 (if (and (car indent-stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1352 (>= (car indent-stack) 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1353 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1354 ;; Yes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1355 ;; Compute the standard indent for this level.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1356 (let (val)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1357 (if (= (char-after (car contain-stack)) ?{)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1358 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1359 (goto-char (car contain-stack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1360 (setq val (calculate-c-indent-after-brace)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1361 (setq val (calculate-c-indent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1362 (if (car indent-stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1363 (- (car indent-stack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1364 opoint))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1365 ;; t means we are in a block comment and should
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1366 ;; calculate accordingly.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1367 (if (eq val t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1368 (setq val (calculate-c-indent-within-comment)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1369 (setcar indent-stack val)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1370 ;; Adjust indent of this individual line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1371 ;; based on its predecessor.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1372 ;; Handle continuation lines, if, else, while, and so on.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1373 (if (/= (char-after (car contain-stack)) ?{)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1374 (setq this-indent (car indent-stack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1375 ;; Line is at statement level.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1376 ;; Is it a new statement? Is it an else?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1377 ;; Find last non-comment character before this line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1378 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1379 (setq this-point (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1380 (setq at-else (and (looking-at "else\\b")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1381 (not (looking-at "else\\s_"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1382 (setq at-brace (= (following-char) ?{))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1383 (setq at-while (and (looking-at "while\\b")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1384 (not (looking-at "while\\s_"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1385 (if (= (following-char) ?})
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1386 (setq this-indent (car indent-stack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1387 (c-backward-to-noncomment opoint)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1388 (if (not (memq (preceding-char) '(0 ?\, ?\; ?} ?: ?{)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1389 ;; Preceding line did not end in comma or semi;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1390 ;; indent this line c-continued-statement-offset
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1391 ;; more than previous.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1392 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1393 (c-backward-to-start-of-continued-exp (car contain-stack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1394 (setq this-indent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1395 (+ c-continued-statement-offset (current-column)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1396 (if at-brace c-continued-brace-offset 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1397 ;; Preceding line ended in comma or semi;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1398 ;; use the standard indent for this level.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1399 (cond (at-else (progn (c-backward-to-start-of-if opoint)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1400 (setq this-indent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1401 (current-indentation))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1402 ((and at-while (c-backward-to-start-of-do opoint))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1403 (setq this-indent (current-indentation)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1404 ((eq (preceding-char) ?\,)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1405 (goto-char this-point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1406 (setq this-indent (calculate-c-indent)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1407 (t (setq this-indent (car indent-stack))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1408 ;; Adjust line indentation according to its contents
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1409 (if (or (looking-at c-switch-label-regexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1410 (and (looking-at "[A-Za-z]")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1411 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1412 (forward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1413 (looking-at ":"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1414 (setq this-indent (max 1 (+ this-indent c-label-offset))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1415 (if (= (following-char) ?})
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1416 (setq this-indent (- this-indent c-indent-level)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1417 (if (= (following-char) ?{)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1418 ;; Don't move an open-brace in column 0.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1419 ;; This is good when constructs such as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1420 ;; `extern "C" {' surround a function definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1421 ;; that should be indented as usual.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1422 ;; It is also good for nested functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1423 ;; It is bad when an open-brace is indented at column 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1424 ;; and you want to fix that, but we can't win 'em all.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1425 (if (zerop (current-column))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1426 (setq this-indent 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1427 (setq this-indent (+ this-indent c-brace-offset))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1428 ;; Don't leave indentation in empty lines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1429 (if (eolp) (setq this-indent 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1430 ;; Put chosen indentation into effect.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1431 (or (= (current-column) this-indent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1432 (= (following-char) ?\#)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1433 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1434 (delete-region (point) (progn (beginning-of-line) (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1435 (indent-to this-indent)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1436 ;; Indent any comment following the text.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1437 (or (looking-at comment-start-skip)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1438 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1439 (let ((beg (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1440 (and (re-search-forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1441 comment-start-skip
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1442 (save-excursion (end-of-line) (point)) t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1443 ;; Make sure the comment starter we found
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1444 ;; is not actually in a string or quoted.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1445 (let ((new-state
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1446 (parse-partial-sexp beg (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1447 nil nil state)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1448 (and (not (nth 3 new-state))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1449 (not (nth 5 new-state))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1450 (indent-for-comment)))))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1451
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1452 ;; Look at all comment-start strings in the current line after point.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1453 ;; Return t if one of them starts a real comment.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1454 ;; This is not used yet, because indent-for-comment
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1455 ;; isn't smart enough to handle the cases this can find.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1456 (defun indent-c-find-real-comment ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1457 (let (win)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1458 (while (and (not win)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1459 (re-search-forward comment-start-skip
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1460 (save-excursion (end-of-line) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1461 t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1462 ;; Make sure the comment start is not quoted.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1463 (let ((state-1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1464 (parse-partial-sexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1465 (save-excursion (beginning-of-line) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1466 (point) nil nil state)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1467 (setq win (and (null (nth 3 state-1)) (null (nth 5 state-1))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1468 win))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1469
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1470 ;; Indent every line whose first char is between START and END inclusive.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1471 (defun c-indent-region (start end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1472 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1473 (goto-char start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1474 ;; Advance to first nonblank line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1475 (skip-chars-forward " \t\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1476 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1477 (let ((endmark (copy-marker end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1478 (c-tab-always-indent t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1479 (while (and (bolp) (not (eobp)) (< (point) endmark))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1480 ;; Indent one line as with TAB.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1481 (let ((shift-amt (c-indent-line))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1482 nextline sexpbeg sexpend)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1483 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*#"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1484 (forward-line 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1485 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1486 ;; Find beginning of following line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1487 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1488 (forward-line 1) (setq nextline (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1489 ;; Find first beginning-of-sexp for sexp extending past this line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1490 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1491 (while (< (point) nextline)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1492 (condition-case nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1493 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1494 (forward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1495 (setq sexpend (point-marker)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1496 (error (setq sexpend nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1497 (goto-char nextline)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1498 (skip-chars-forward " \t\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1499 (if sexpend
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1500 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1501 ;; Make sure the sexp we found really starts on the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1502 ;; current line and extends past it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1503 (goto-char sexpend)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1504 (backward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1505 (setq sexpbeg (point)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1506 ;; If that sexp ends within the region,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1507 ;; indent it all at once, fast.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1508 (if (and sexpend (> sexpend nextline) (<= sexpend endmark)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1509 (< sexpbeg nextline))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1510 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1511 (indent-c-exp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1512 (goto-char sexpend)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1513 ;; Move to following line and try again.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1514 (and sexpend (set-marker sexpend nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1515 (forward-line 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1516 (set-marker endmark nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1517
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1518 (defun set-c-style (style &optional global)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1519 "Set C-mode variables to use one of several different indentation styles.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1520 The arguments are a string representing the desired style
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1521 and a flag which, if non-nil, means to set the style globally.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1522 \(Interactively, the flag comes from the prefix argument.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1523 Available styles are GNU, K&R, BSD and Whitesmith."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1524 (interactive (list (let ((completion-ignore-case t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1525 (completing-read "Use which C indentation style? "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1526 c-style-alist nil t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1527 current-prefix-arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1528 (let ((vars (cdr (assoc style c-style-alist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1529 (or vars
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1530 (error "Invalid C indentation style `%s'" style))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1531 (while vars
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1532 (or global
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1533 (make-local-variable (car (car vars))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1534 (set (car (car vars)) (cdr (car vars)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1535 (setq vars (cdr vars)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1536
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1537 ;;; This page handles insertion and removal of backslashes for C macros.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1538
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1539 (defvar c-backslash-column 48
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1540 "*Minimum column for end-of-line backslashes of macro definitions.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1541
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1542 (defun c-backslash-region (from to delete-flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1543 "Insert, align, or delete end-of-line backslashes on the lines in the region.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1544 With no argument, inserts backslashes and aligns existing backslashes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1545 With an argument, deletes the backslashes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1546
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1547 This function does not modify the last line of the region if the region ends
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1548 right at the start of the following line; it does not modify blank lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1549 at the start of the region. So you can put the region around an entire macro
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1550 definition and conveniently use this command."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1551 (interactive "r\nP")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1552 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1553 (goto-char from)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1554 (let ((column c-backslash-column)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1555 (endmark (make-marker)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1556 (move-marker endmark to)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1557 ;; Compute the smallest column number past the ends of all the lines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1558 (if (not delete-flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1559 (while (< (point) to)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1560 (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1561 (if (= (preceding-char) ?\\)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1562 (progn (forward-char -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1563 (skip-chars-backward " \t")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1564 (setq column (max column (1+ (current-column))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1565 (forward-line 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1566 ;; Adjust upward to a tab column, if that doesn't push past the margin.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1567 (if (> (% column tab-width) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1568 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1569 (if (< adjusted (window-width))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1570 (setq column adjusted))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1571 ;; Don't modify blank lines at start of region.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1572 (goto-char from)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1573 (while (and (< (point) endmark) (eolp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1574 (forward-line 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1575 ;; Add or remove backslashes on all the lines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1576 (while (and (< (point) endmark)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1577 ;; Don't backslashify the last line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1578 ;; if the region ends right at the start of the next line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1579 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1580 (forward-line 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1581 (< (point) endmark)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1582 (if (not delete-flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1583 (c-append-backslash column)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1584 (c-delete-backslash))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1585 (forward-line 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1586 (move-marker endmark nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1587
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1588 (defun c-append-backslash (column)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1589 (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1590 ;; Note that "\\\\" is needed to get one backslash.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1591 (if (= (preceding-char) ?\\)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1592 (progn (forward-char -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1593 (delete-horizontal-space)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1594 (indent-to column))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1595 (indent-to column)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1596 (insert "\\")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1597
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1598 (defun c-delete-backslash ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1599 (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1600 (or (bolp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1601 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1602 (forward-char -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1603 (if (looking-at "\\\\")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1604 (delete-region (1+ (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1605 (progn (skip-chars-backward " \t") (point)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1606
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1607 (defun c-up-conditional (count)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1608 "Move back to the containing preprocessor conditional, leaving mark behind.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1609 A prefix argument acts as a repeat count. With a negative argument,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1610 move forward to the end of the containing preprocessor conditional.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1611 When going backwards, `#elif' is treated like `#else' followed by `#if'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1612 When going forwards, `#elif' is ignored."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1613 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1614 (c-forward-conditional (- count) t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1615
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1616 (defun c-backward-conditional (count &optional up-flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1617 "Move back across a preprocessor conditional, leaving mark behind.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1618 A prefix argument acts as a repeat count. With a negative argument,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1619 move forward across a preprocessor conditional."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1620 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1621 (c-forward-conditional (- count) up-flag))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1622
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1623 (defun c-forward-conditional (count &optional up-flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1624 "Move forward across a preprocessor conditional, leaving mark behind.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1625 A prefix argument acts as a repeat count. With a negative argument,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1626 move backward across a preprocessor conditional."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1627 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1628 (let* ((forward (> count 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1629 (increment (if forward -1 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1630 (search-function (if forward 're-search-forward 're-search-backward))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1631 (opoint (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1632 (new))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1633 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1634 (while (/= count 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1635 (let ((depth (if up-flag 0 -1)) found)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1636 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1637 ;; Find the "next" significant line in the proper direction.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1638 (while (and (not found)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1639 ;; Rather than searching for a # sign that comes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1640 ;; at the beginning of a line aside from whitespace,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1641 ;; search first for a string starting with # sign.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1642 ;; Then verify what precedes it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1643 ;; This is faster on account of the fastmap feature of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1644 ;; the regexp matcher.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1645 (funcall search-function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1646 "#[ \t]*\\(if\\|elif\\|endif\\)"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1647 nil t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1648 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1649 ;; Now verify it is really a preproc line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1650 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1651 (let ((prev depth))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1652 ;; Update depth according to what we found.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1653 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1654 (cond ((looking-at "[ \t]*#[ \t]*endif")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1655 (setq depth (+ depth increment)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1656 ((looking-at "[ \t]*#[ \t]*elif")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1657 (if (and forward (= depth 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1658 (setq found (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1659 (t (setq depth (- depth increment))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1660 ;; If we are trying to move across, and we find
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1661 ;; an end before we find a beginning, get an error.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1662 (if (and (< prev 0) (< depth prev))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1663 (error (if forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1664 "No following conditional at this level"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1665 "No previous conditional at this level")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1666 ;; When searching forward, start from next line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1667 ;; so that we don't find the same line again.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1668 (if forward (forward-line 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1669 ;; If this line exits a level of conditional, exit inner loop.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1670 (if (< depth 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1671 (setq found (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1672 ;; If the line is not really a conditional, skip past it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1673 (if forward (end-of-line)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1674 (or found
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1675 (error "No containing preprocessor conditional"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1676 (goto-char (setq new found)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1677 (setq count (+ count increment))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1678 (push-mark)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1679 (goto-char new)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1680
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1681 ;;; c-mode.el ends here