annotate lisp/modes/old-c-mode.el @ 18:d95e72db5c07 r19-15b92

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