annotate lisp/progmodes/c-mode.el @ 4043:6228decc1353

(ispell): Call deactivate-mark instead of setting mark-active directly. Do this regardless of transient-mark-mode.
author Roland McGrath <roland@gnu.org>
date Thu, 08 Jul 1993 22:38:59 +0000
parents a03d6cac1cd1
children 8b53e0fa08b4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
662
8a533acedb77 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 472
diff changeset
1 ;;; c-mode.el --- C code editing commands for Emacs
909
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
2 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
846
20674ae6bf52 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 837
diff changeset
3
807
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 759
diff changeset
4 ;; Maintainer: FSF
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 759
diff changeset
5 ;; Keywords: c
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 759
diff changeset
6
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
7 ;; This file is part of GNU Emacs.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
8
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
10 ;; it under the terms of the GNU General Public License as published by
807
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 759
diff changeset
11 ;; the Free Software Foundation; either version 2, or (at your option)
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
12 ;; any later version.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
13
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
14 ;; GNU Emacs is distributed in the hope that it will be useful,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
17 ;; GNU General Public License for more details.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
18
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
19 ;; You should have received a copy of the GNU General Public License
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
22
2307
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2084
diff changeset
23 ;;; Commentary:
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2084
diff changeset
24
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2084
diff changeset
25 ;; A smart editing mode for C code. It knows a lot about C syntax and tries
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2084
diff changeset
26 ;; to position the curser according to C layout conventions. You can
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2084
diff changeset
27 ;; change the details of the layout style with option variables. Load it
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2084
diff changeset
28 ;; and do M-x describe-mode for details.
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2084
diff changeset
29
807
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 759
diff changeset
30 ;;; Code:
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
31
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
32 (defvar c-mode-abbrev-table nil
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
33 "Abbrev table in use in C mode.")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
34 (define-abbrev-table 'c-mode-abbrev-table ())
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
35
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
36 (defvar c-mode-map ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
37 "Keymap used in C mode.")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
38 (if c-mode-map
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
39 ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
40 (setq c-mode-map (make-sparse-keymap))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
41 (define-key c-mode-map "{" 'electric-c-brace)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
42 (define-key c-mode-map "}" 'electric-c-brace)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
43 (define-key c-mode-map ";" 'electric-c-semi)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
44 (define-key c-mode-map "#" 'electric-c-sharp-sign)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
45 (define-key c-mode-map ":" 'electric-c-terminator)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
46 (define-key c-mode-map "\e\C-h" 'mark-c-function)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
47 (define-key c-mode-map "\e\C-q" 'indent-c-exp)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
48 (define-key c-mode-map "\ea" 'c-beginning-of-statement)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
49 (define-key c-mode-map "\ee" 'c-end-of-statement)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
50 (define-key c-mode-map "\eq" 'c-fill-paragraph)
3906
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
51 (define-key c-mode-map "\C-c\C-n" 'c-forward-conditional)
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
52 (define-key c-mode-map "\C-c\C-p" 'c-backward-conditional)
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
53 (define-key c-mode-map "\C-c\C-u" 'c-up-conditional)
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
54 (define-key c-mode-map "\177" 'backward-delete-char-untabify)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
55 (define-key c-mode-map "\t" 'c-indent-command))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
56
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
57 ;; cmacexp is lame because it uses no preprocessor symbols.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
58 ;; It isn't very extensible either -- hardcodes /lib/cpp.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
59 (autoload 'c-macro-expand "cmacexp"
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
60 "Display the result of expanding all C macros occurring in the region.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
61 The expansion is entirely correct because it uses the C preprocessor."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
62 t)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
63
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
64 (defvar c-mode-syntax-table nil
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
65 "Syntax table in use in C-mode buffers.")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
66
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
67 (if c-mode-syntax-table
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
68 ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
69 (setq c-mode-syntax-table (make-syntax-table))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
70 (modify-syntax-entry ?\\ "\\" c-mode-syntax-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
71 (modify-syntax-entry ?/ ". 14" c-mode-syntax-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
72 (modify-syntax-entry ?* ". 23" c-mode-syntax-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
73 (modify-syntax-entry ?+ "." c-mode-syntax-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
74 (modify-syntax-entry ?- "." c-mode-syntax-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
75 (modify-syntax-entry ?= "." c-mode-syntax-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
76 (modify-syntax-entry ?% "." c-mode-syntax-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
77 (modify-syntax-entry ?< "." c-mode-syntax-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
78 (modify-syntax-entry ?> "." c-mode-syntax-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
79 (modify-syntax-entry ?& "." c-mode-syntax-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
80 (modify-syntax-entry ?| "." c-mode-syntax-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
81 (modify-syntax-entry ?\' "\"" c-mode-syntax-table))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
82
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
83 (defconst c-indent-level 2
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
84 "*Indentation of C statements with respect to containing block.")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
85 (defconst c-brace-imaginary-offset 0
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
86 "*Imagined indentation of a C open brace that actually follows a statement.")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
87 (defconst c-brace-offset 0
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
88 "*Extra indentation for braces, compared with other text in same context.")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
89 (defconst c-argdecl-indent 5
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
90 "*Indentation level of declarations of C function arguments.")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
91 (defconst c-label-offset -2
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
92 "*Offset of C label lines and case statements relative to usual indentation.")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
93 (defconst c-continued-statement-offset 2
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
94 "*Extra indent for lines not starting new statements.")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
95 (defconst c-continued-brace-offset 0
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
96 "*Extra indent for substatements that start with open-braces.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
97 This is in addition to c-continued-statement-offset.")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
98 (defconst c-style-alist
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
99 '(("GNU"
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
100 (c-indent-level . 2)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
101 (c-argdecl-indent . 5)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
102 (c-brace-offset . 0)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
103 (c-label-offset . -2)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
104 (c-continued-statement-offset . 2))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
105 ("K&R"
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
106 (c-indent-level . 5)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
107 (c-argdecl-indent . 0)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
108 (c-brace-offset . -5)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
109 (c-label-offset . -5)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
110 (c-continued-statement-offset . 5))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
111 ("BSD"
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
112 (c-indent-level . 4)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
113 (c-argdecl-indent . 4)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
114 (c-brace-offset . -4)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
115 (c-label-offset . -4)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
116 (c-continued-statement-offset . 4))
1661
26d8a65a6e32 * c-mode.el (c-style-alist): Add quotes around C++ style name.
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1205
diff changeset
117 ("C++"
472
e6b49c51a9bb *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 453
diff changeset
118 (c-indent-level . 4)
e6b49c51a9bb *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 453
diff changeset
119 (c-continued-statement-offset . 4)
e6b49c51a9bb *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 453
diff changeset
120 (c-brace-offset . -4)
e6b49c51a9bb *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 453
diff changeset
121 (c-argdecl-indent . 0)
e6b49c51a9bb *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 453
diff changeset
122 (c-label-offset . -4)
e6b49c51a9bb *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 453
diff changeset
123 (c-auto-newline . t))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
124 ("Whitesmith"
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
125 (c-indent-level . 4)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
126 (c-argdecl-indent . 4)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
127 (c-brace-offset . 0)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
128 (c-label-offset . -4)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
129 (c-continued-statement-offset . 4))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
130
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
131 (defconst c-auto-newline nil
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
132 "*Non-nil means automatically newline before and after braces,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
133 and after colons and semicolons, inserted in C code.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
134 If you do not want a leading newline before braces then use:
1159
14a07c6ffb04 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 1086
diff changeset
135 (define-key c-mode-map \"{\" 'electric-c-semi)")
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
136
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
137 (defconst c-tab-always-indent t
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
138 "*Non-nil means TAB in C mode should always reindent the current line,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
139 regardless of where in the line point is when the TAB command is used.")
1804
fe2bebf150c7 * c-mode.el (c-switch-label-regexp): New constant.
Jim Blandy <jimb@redhat.com>
parents: 1701
diff changeset
140
fe2bebf150c7 * c-mode.el (c-switch-label-regexp): New constant.
Jim Blandy <jimb@redhat.com>
parents: 1701
diff changeset
141 ;;; Regular expression used internally to recognize labels in switch
fe2bebf150c7 * c-mode.el (c-switch-label-regexp): New constant.
Jim Blandy <jimb@redhat.com>
parents: 1701
diff changeset
142 ;;; statements.
fe2bebf150c7 * c-mode.el (c-switch-label-regexp): New constant.
Jim Blandy <jimb@redhat.com>
parents: 1701
diff changeset
143 (defconst c-switch-label-regexp "case[ \t'/(]\\|default\\(\\S_\\|'\\)")
fe2bebf150c7 * c-mode.el (c-switch-label-regexp): New constant.
Jim Blandy <jimb@redhat.com>
parents: 1701
diff changeset
144
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
145
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
146 (defun c-mode ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
147 "Major mode for editing C code.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
148 Expression and list commands understand all C brackets.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
149 Tab indents for C code.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
150 Comments are delimited with /* ... */.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
151 Paragraphs are separated by blank lines only.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
152 Delete converts tabs to spaces as it moves back.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
153 \\{c-mode-map}
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
154 Variables controlling indentation style:
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
155 c-tab-always-indent
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
156 Non-nil means TAB in C mode should always reindent the current line,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
157 regardless of where in the line point is when the TAB command is used.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
158 c-auto-newline
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
159 Non-nil means automatically newline before and after braces,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
160 and after colons and semicolons, inserted in C code.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
161 c-indent-level
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
162 Indentation of C statements within surrounding block.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
163 The surrounding block's indentation is the indentation
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
164 of the line on which the open-brace appears.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
165 c-continued-statement-offset
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
166 Extra indentation given to a substatement, such as the
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
167 then-clause of an if or body of a while.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
168 c-continued-brace-offset
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
169 Extra indentation given to a brace that starts a substatement.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
170 This is in addition to c-continued-statement-offset.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
171 c-brace-offset
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
172 Extra indentation for line if it starts with an open brace.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
173 c-brace-imaginary-offset
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
174 An open brace following other text is treated as if it were
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
175 this far to the right of the start of its line.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
176 c-argdecl-indent
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
177 Indentation level of declarations of C function arguments.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
178 c-label-offset
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
179 Extra indentation for line that is a label, or case or default.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
180
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
181 Settings for K&R and BSD indentation styles are
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
182 c-indent-level 5 8
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
183 c-continued-statement-offset 5 8
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
184 c-brace-offset -5 -8
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
185 c-argdecl-indent 0 8
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
186 c-label-offset -5 -8
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
187
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
188 Turning on C mode calls the value of the variable c-mode-hook with no args,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
189 if that value is non-nil."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
190 (interactive)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
191 (kill-all-local-variables)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
192 (use-local-map c-mode-map)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
193 (setq major-mode 'c-mode)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
194 (setq mode-name "C")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
195 (setq local-abbrev-table c-mode-abbrev-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
196 (set-syntax-table c-mode-syntax-table)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
197 (make-local-variable 'paragraph-start)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
198 (setq paragraph-start (concat "^$\\|" page-delimiter))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
199 (make-local-variable 'paragraph-separate)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
200 (setq paragraph-separate paragraph-start)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
201 (make-local-variable 'paragraph-ignore-fill-prefix)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
202 (setq paragraph-ignore-fill-prefix t)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
203 (make-local-variable 'indent-line-function)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
204 (setq indent-line-function 'c-indent-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
205 (make-local-variable 'indent-region-function)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
206 (setq indent-region-function 'c-indent-region)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
207 (make-local-variable 'require-final-newline)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
208 (setq require-final-newline t)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
209 (make-local-variable 'comment-start)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
210 (setq comment-start "/* ")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
211 (make-local-variable 'comment-end)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
212 (setq comment-end " */")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
213 (make-local-variable 'comment-column)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
214 (setq comment-column 32)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
215 (make-local-variable 'comment-start-skip)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
216 (setq comment-start-skip "/\\*+ *")
2307
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2084
diff changeset
217 (make-local-variable 'comment-indent-function)
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2084
diff changeset
218 (setq comment-indent-function 'c-comment-indent)
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
219 (make-local-variable 'parse-sexp-ignore-comments)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
220 (setq parse-sexp-ignore-comments t)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
221 (run-hooks 'c-mode-hook))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
222
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
223 ;; This is used by indent-for-comment
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
224 ;; to decide how much to indent a comment in C code
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
225 ;; based on its context.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
226 (defun c-comment-indent ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
227 (if (looking-at "^/\\*")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
228 0 ;Existing comment at bol stays there.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
229 (let ((opoint (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
230 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
231 (beginning-of-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
232 (cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\)")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
233 ;; A comment following a solitary close-brace
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
234 ;; should have only one space.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
235 (search-forward "}")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
236 (1+ (current-column)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
237 ((or (looking-at "^#[ \t]*endif[ \t]*")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
238 (looking-at "^#[ \t]*else[ \t]*"))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
239 7) ;2 spaces after #endif
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
240 ((progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
241 (goto-char opoint)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
242 (skip-chars-backward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
243 (and (= comment-column 0) (bolp)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
244 ;; If comment-column is 0, and nothing but space
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
245 ;; before the comment, align it at 0 rather than 1.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
246 0)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
247 (t
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
248 (max (1+ (current-column)) ;Else indent at comment column
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
249 comment-column))))))) ; except leave at least one space.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
250
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
251 (defun c-fill-paragraph (&optional arg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
252 "Like \\[fill-paragraph] but handle C comments.
1081
4e7d09b779eb *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1064
diff changeset
253 If any of the current line is a comment or within a comment,
4e7d09b779eb *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1064
diff changeset
254 fill the comment or the paragraph of it that point is in,
4e7d09b779eb *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1064
diff changeset
255 preserving the comment indentation or line-starting decorations."
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
256 (interactive "P")
1086
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
257 (let* (comment-start-place
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
258 (first-line
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
259 ;; Check for obvious entry to comment.
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
260 (save-excursion
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
261 (beginning-of-line)
1701
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
262 (skip-chars-forward " \t\n")
1086
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
263 (and (looking-at comment-start-skip)
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
264 (setq comment-start-place (point))))))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
265 (if (or first-line
1064
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
266 ;; t if we enter a comment between start of function and this line.
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
267 (eq (calculate-c-indent) t)
1081
4e7d09b779eb *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1064
diff changeset
268 ;; t if this line contains a comment starter.
1181
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
269 (setq first-line
1701
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
270 (save-excursion
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
271 (beginning-of-line)
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
272 (prog1
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
273 (re-search-forward comment-start-skip
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
274 (save-excursion (end-of-line)
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
275 (point))
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
276 t)
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
277 (setq comment-start-place (point))))))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
278 ;; Inside a comment: fill one comment paragraph.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
279 (let ((fill-prefix
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
280 ;; The prefix for each line of this paragraph
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
281 ;; is the appropriate part of the start of this line,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
282 ;; up to the column at which text should be indented.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
283 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
284 (beginning-of-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
285 (if (looking-at "[ \t]*/\\*.*\\*/")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
286 (progn (re-search-forward comment-start-skip)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
287 (make-string (current-column) ?\ ))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
288 (if first-line (forward-line 1))
1701
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
289
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
290 (let ((line-width (progn (end-of-line) (current-column))))
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
291 (beginning-of-line)
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
292 (prog1
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
293 (buffer-substring
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
294 (point)
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
295
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
296 ;; How shall we decide where the end of the
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
297 ;; fill-prefix is?
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
298 ;; calculate-c-indent-within-comment bases its value
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
299 ;; on the indentation of previous lines; if they're
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
300 ;; indented specially, it could return a column
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
301 ;; that's well into the current line's text. So
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
302 ;; we'll take at most that many space, tab, or *
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
303 ;; characters, and use that as our fill prefix.
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
304 (let ((max-prefix-end
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
305 (progn
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
306 (move-to-column
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
307 (calculate-c-indent-within-comment t)
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
308 t)
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
309 (point))))
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
310 (beginning-of-line)
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
311 (skip-chars-forward " \t*" max-prefix-end)
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
312 (point)))
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
313
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
314 ;; If the comment is only one line followed by a blank
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
315 ;; line, calling move-to-column above may have added
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
316 ;; some spaces and tabs to the end of the line; the
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
317 ;; fill-paragraph function will then delete it and the
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
318 ;; newline following it, so we'll lose a blank line
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
319 ;; when we shouldn't. So delete anything
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
320 ;; move-to-column added to the end of the line. We
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
321 ;; record the line width instead of the position of the
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
322 ;; old line end because move-to-column might break a
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
323 ;; tab into spaces, and the new characters introduced
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
324 ;; there shouldn't be deleted.
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
325
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
326 ;; If you can see a better way to do this, please make
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
327 ;; the change. This seems very messy to me.
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
328 (delete-region (progn (move-to-column line-width)
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
329 (point))
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
330 (progn (end-of-line) (point))))))))
e0463f021e2f * c-mode.el (c-fill-paragraph): Calculating the proper fill-prefix
Jim Blandy <jimb@redhat.com>
parents: 1662
diff changeset
331
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
332 (paragraph-start
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
333 ;; Lines containing just a comment start or just an end
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
334 ;; should not be filled into paragraphs they are next to.
1821
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1804
diff changeset
335 (concat
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1804
diff changeset
336 paragraph-start
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1804
diff changeset
337 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$"))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
338 (paragraph-separate
1821
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1804
diff changeset
339 (concat
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1804
diff changeset
340 paragraph-separate
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1804
diff changeset
341 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$"))
1181
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
342 (chars-to-delete 0))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
343 (save-restriction
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
344 ;; Don't fill the comment together with the code following it.
1086
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
345 ;; So temporarily exclude everything before the comment start,
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
346 ;; and everything after the line where the comment ends.
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
347 ;; If comment-start-place is non-nil, the comment starter is there.
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
348 ;; Otherwise, point is inside the comment.
1181
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
349 (narrow-to-region (save-excursion
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
350 (if comment-start-place
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
351 (goto-char comment-start-place)
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
352 (search-backward "/*"))
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
353 ;; Protect text before the comment start
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
354 ;; by excluding it. Add spaces to bring back
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
355 ;; proper indentation of that point.
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
356 (let ((column (current-column)))
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
357 (prog1 (point)
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
358 (setq chars-to-delete column)
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
359 (insert-char ?\ column))))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
360 (save-excursion
1086
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
361 (if comment-start-place
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
362 (goto-char (+ comment-start-place 2)))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
363 (search-forward "*/" nil 'move)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
364 (forward-line 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
365 (point)))
1181
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
366
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
367 (fill-paragraph arg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
368 (save-excursion
1181
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
369 ;; Delete the chars we inserted to avoid clobbering
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
370 ;; the stuff before the comment start.
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
371 (goto-char (point-min))
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
372 (if (> chars-to-delete 0)
5897b220ad8e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1177
diff changeset
373 (delete-region (point) (+ (point) chars-to-delete)))
1086
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
374 ;; Find the comment ender (should be on last line of buffer,
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
375 ;; given the narrowing) and don't leave it on its own line.
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
376 (goto-char (point-max))
273918bf0a95 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1081
diff changeset
377 (forward-line -1)
1662
ee7f627ef26e Fri Dec 4 00:31:30 1992 Jim Blandy (jimb@totoro.cs.oberlin.edu)
Jim Blandy <jimb@redhat.com>
parents: 1661
diff changeset
378 (search-forward "*/" nil 'move)
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
379 (beginning-of-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
380 (if (looking-at "[ \t]*\\*/")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
381 (delete-indentation)))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
382 ;; Outside of comments: do ordinary filling.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
383 (fill-paragraph arg))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
384
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
385 (defun electric-c-brace (arg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
386 "Insert character and correct line's indentation."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
387 (interactive "P")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
388 (let (insertpos)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
389 (if (and (not arg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
390 (eolp)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
391 (or (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
392 (skip-chars-backward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
393 (bolp))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
394 (if c-auto-newline (progn (c-indent-line) (newline) t) nil)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
395 (progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
396 (insert last-command-char)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
397 (c-indent-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
398 (if c-auto-newline
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
399 (progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
400 (newline)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
401 ;; (newline) may have done auto-fill
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
402 (setq insertpos (- (point) 2))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
403 (c-indent-line)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
404 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
405 (if insertpos (goto-char (1+ insertpos)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
406 (delete-char -1))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
407 (if insertpos
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
408 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
409 (goto-char insertpos)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
410 (self-insert-command (prefix-numeric-value arg)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
411 (self-insert-command (prefix-numeric-value arg)))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
412
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
413 (defun electric-c-sharp-sign (arg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
414 "Insert character and correct line's indentation."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
415 (interactive "P")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
416 (if (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
417 (skip-chars-backward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
418 (bolp))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
419 (let ((c-auto-newline nil))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
420 (electric-c-terminator arg))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
421 (self-insert-command (prefix-numeric-value arg))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
422
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
423 (defun electric-c-semi (arg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
424 "Insert character and correct line's indentation."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
425 (interactive "P")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
426 (if c-auto-newline
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
427 (electric-c-terminator arg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
428 (self-insert-command (prefix-numeric-value arg))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
429
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
430 (defun electric-c-terminator (arg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
431 "Insert character and correct line's indentation."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
432 (interactive "P")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
433 (let (insertpos (end (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
434 (if (and (not arg) (eolp)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
435 (not (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
436 (beginning-of-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
437 (skip-chars-forward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
438 (or (= (following-char) ?#)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
439 ;; Colon is special only after a label, or case ....
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
440 ;; So quickly rule out most other uses of colon
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
441 ;; and do no indentation for them.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
442 (and (eq last-command-char ?:)
1804
fe2bebf150c7 * c-mode.el (c-switch-label-regexp): New constant.
Jim Blandy <jimb@redhat.com>
parents: 1701
diff changeset
443 (not (looking-at c-switch-label-regexp))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
444 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
445 (skip-chars-forward "a-zA-Z0-9_$")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
446 (skip-chars-forward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
447 (< (point) end)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
448 (progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
449 (beginning-of-defun)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
450 (let ((pps (parse-partial-sexp (point) end)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
451 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
452 (progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
453 (insert last-command-char)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
454 (c-indent-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
455 (and c-auto-newline
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
456 (not (c-inside-parens-p))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
457 (progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
458 (newline)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
459 ;; (newline) may have done auto-fill
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
460 (setq insertpos (- (point) 2))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
461 (c-indent-line)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
462 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
463 (if insertpos (goto-char (1+ insertpos)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
464 (delete-char -1))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
465 (if insertpos
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
466 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
467 (goto-char insertpos)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
468 (self-insert-command (prefix-numeric-value arg)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
469 (self-insert-command (prefix-numeric-value arg)))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
470
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
471 (defun c-inside-parens-p ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
472 (condition-case ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
473 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
474 (save-restriction
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
475 (narrow-to-region (point)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
476 (progn (beginning-of-defun) (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
477 (goto-char (point-max))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
478 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
479 (error nil)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
480
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
481 (defun c-indent-command (&optional whole-exp)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
482 "Indent current line as C code, or in some cases insert a tab character.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
483 If `c-tab-always-indent' is non-nil (the default), always indent current line.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
484 Otherwise, indent the current line only if point is at the left margin or
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
485 in the line's indentation; otherwise insert a tab.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
486
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
487 A numeric argument, regardless of its value, means indent rigidly all the
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
488 lines of the expression starting after point so that this line becomes
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
489 properly indented. The relative indentation among the lines of the
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
490 expression are preserved."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
491 (interactive "P")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
492 (if whole-exp
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
493 ;; If arg, always indent this line as C
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
494 ;; and shift remaining lines of expression the same amount.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
495 (let ((shift-amt (c-indent-line))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
496 beg end)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
497 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
498 (if c-tab-always-indent
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
499 (beginning-of-line))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
500 ;; Find beginning of following line.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
501 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
502 (forward-line 1) (setq beg (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
503 ;; Find first beginning-of-sexp for sexp extending past this line.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
504 (while (< (point) beg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
505 (forward-sexp 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
506 (setq end (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
507 (skip-chars-forward " \t\n")))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
508 (if (> end beg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
509 (indent-code-rigidly beg end shift-amt "#")))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
510 (if (and (not c-tab-always-indent)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
511 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
512 (skip-chars-backward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
513 (not (bolp))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
514 (insert-tab)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
515 (c-indent-line))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
516
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
517 (defun c-indent-line ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
518 "Indent current line as C code.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
519 Return the amount the indentation changed by."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
520 (let ((indent (calculate-c-indent nil))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
521 beg shift-amt
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
522 (case-fold-search nil)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
523 (pos (- (point-max) (point))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
524 (beginning-of-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
525 (setq beg (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
526 (cond ((eq indent nil)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
527 (setq indent (current-indentation)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
528 ((eq indent t)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
529 (setq indent (calculate-c-indent-within-comment)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
530 ((looking-at "[ \t]*#")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
531 (setq indent 0))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
532 (t
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
533 (skip-chars-forward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
534 (if (listp indent) (setq indent (car indent)))
1804
fe2bebf150c7 * c-mode.el (c-switch-label-regexp): New constant.
Jim Blandy <jimb@redhat.com>
parents: 1701
diff changeset
535 (cond ((or (looking-at c-switch-label-regexp)
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
536 (and (looking-at "[A-Za-z]")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
537 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
538 (forward-sexp 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
539 (looking-at ":"))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
540 (setq indent (max 1 (+ indent c-label-offset))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
541 ((and (looking-at "else\\b")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
542 (not (looking-at "else\\s_")))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
543 (setq indent (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
544 (c-backward-to-start-of-if)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
545 (current-indentation))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
546 ((looking-at "}[ \t]*else")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
547 (setq indent (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
548 (forward-char)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
549 (backward-sexp)
3716
c8af07496d54 (c-indent-line): Call c-backward-to-start-of-if
Richard M. Stallman <rms@gnu.org>
parents: 3591
diff changeset
550 (c-backward-to-start-of-if)
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
551 (current-indentation))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
552 ((and (looking-at "while\\b")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
553 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
554 (c-backward-to-start-of-do)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
555 ;; This is a `while' that ends a do-while.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
556 (setq indent (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
557 (c-backward-to-start-of-do)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
558 (current-indentation))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
559 ((= (following-char) ?})
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
560 (setq indent (- indent c-indent-level)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
561 ((= (following-char) ?{)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
562 (setq indent (+ indent c-brace-offset))))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
563 (skip-chars-forward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
564 (setq shift-amt (- indent (current-column)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
565 (if (zerop shift-amt)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
566 (if (> (- (point-max) pos) (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
567 (goto-char (- (point-max) pos)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
568 (delete-region beg (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
569 (indent-to indent)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
570 ;; If initial point was within line's indentation,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
571 ;; position after the indentation. Else stay at same point in text.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
572 (if (> (- (point-max) pos) (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
573 (goto-char (- (point-max) pos))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
574 shift-amt))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
575
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
576 (defun calculate-c-indent (&optional parse-start)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
577 "Return appropriate indentation for current line as C code.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
578 In usual case returns an integer: the column to indent to.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
579 Returns nil if line starts inside a string, t if in a comment."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
580 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
581 (beginning-of-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
582 (let ((indent-point (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
583 (case-fold-search nil)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
584 state
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
585 containing-sexp)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
586 (if parse-start
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
587 (goto-char parse-start)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
588 (beginning-of-defun))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
589 (while (< (point) indent-point)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
590 (setq parse-start (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
591 (setq state (parse-partial-sexp (point) indent-point 0))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
592 (setq containing-sexp (car (cdr state))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
593 (cond ((or (nth 3 state) (nth 4 state))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
594 ;; return nil or t if should not change this line
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
595 (nth 4 state))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
596 ((null containing-sexp)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
597 ;; Line is at top level. May be data or function definition,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
598 ;; or may be function argument declaration.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
599 ;; Indent like the previous top level line
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
600 ;; unless that ends in a closeparen without semicolon,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
601 ;; in which case this line is the first argument decl.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
602 (goto-char indent-point)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
603 (skip-chars-forward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
604 (if (= (following-char) ?{)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
605 0 ; Unless it starts a function body
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
606 (c-backward-to-noncomment (or parse-start (point-min)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
607 ;; Look at previous line that's at column 0
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
608 ;; to determine whether we are in top-level decls
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
609 ;; or function's arg decls. Set basic-indent accordingly.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
610 (let ((basic-indent
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
611 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
612 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
759
58b7fc91b74a *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 662
diff changeset
613 (let (comment lim)
1064
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
614 ;; Recognize the DEFUN macro in Emacs.
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
615 (if (save-excursion
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
616 ;; Move down to the (putative) argnames line.
1184
5f4300ccc93f entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 1181
diff changeset
617 (while (and (not (eobp))
5f4300ccc93f entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 1181
diff changeset
618 (not (looking-at " *[({}#/]")))
1064
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
619 (forward-line 1))
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
620 ;; Go back to the DEFUN, if it is one.
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
621 (condition-case nil
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
622 (backward-sexp 1)
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
623 (error))
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
624 (beginning-of-line)
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
625 (looking-at "DEFUN\\b"))
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
626 c-argdecl-indent
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
627 (if (and (looking-at "\\sw\\|\\s_")
1205
56d315ca10ee (calculate-c-indent): When testing for function-start line,
Richard M. Stallman <rms@gnu.org>
parents: 1184
diff changeset
628 ;; This is careful to stop at the first
56d315ca10ee (calculate-c-indent): When testing for function-start line,
Richard M. Stallman <rms@gnu.org>
parents: 1184
diff changeset
629 ;; paren if we have
56d315ca10ee (calculate-c-indent): When testing for function-start line,
Richard M. Stallman <rms@gnu.org>
parents: 1184
diff changeset
630 ;; int foo Proto ((int, int));
56d315ca10ee (calculate-c-indent): When testing for function-start line,
Richard M. Stallman <rms@gnu.org>
parents: 1184
diff changeset
631 (looking-at "[^\"\n=(]*(")
1064
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
632 (progn
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
633 (goto-char (1- (match-end 0)))
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
634 (setq lim (point))
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
635 (condition-case nil
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
636 (forward-sexp 1)
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
637 (error))
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
638 (skip-chars-forward " \t\f")
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
639 (and (< (point) indent-point)
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
640 (not (memq (following-char)
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
641 '(?\, ?\;)))))
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
642 ;; Make sure the "function decl" we found
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
643 ;; is not inside a comment.
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
644 (progn
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
645 (beginning-of-line)
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
646 (while (and (not comment)
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
647 (search-forward "/*" lim t))
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
648 (setq comment
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
649 (not (search-forward "*/" lim t))))
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
650 (not comment)))
e699ce19609f *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 1054
diff changeset
651 c-argdecl-indent 0))))))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
652 basic-indent)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
653
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
654 ;; ;; Now add a little if this is a continuation line.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
655 ;; (+ basic-indent (if (or (bobp)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
656 ;; (memq (preceding-char) '(?\) ?\; ?\}))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
657 ;; ;; Line with zero indentation
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
658 ;; ;; is probably the return-type
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
659 ;; ;; of a function definition,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
660 ;; ;; so following line is function name.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
661 ;; (= (current-indentation) 0))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
662 ;; 0 c-continued-statement-offset))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
663
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
664 ((/= (char-after containing-sexp) ?{)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
665 ;; line is expression, not statement:
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
666 ;; indent to just after the surrounding open.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
667 (goto-char (1+ containing-sexp))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
668 (current-column))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
669 (t
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
670 ;; Statement level. Is it a continuation or a new statement?
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
671 ;; Find previous non-comment character.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
672 (goto-char indent-point)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
673 (c-backward-to-noncomment containing-sexp)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
674 ;; Back up over label lines, since they don't
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
675 ;; affect whether our line is a continuation.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
676 (while (or (eq (preceding-char) ?\,)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
677 (and (eq (preceding-char) ?:)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
678 (or (eq (char-after (- (point) 2)) ?\')
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
679 (memq (char-syntax (char-after (- (point) 2)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
680 '(?w ?_)))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
681 (if (eq (preceding-char) ?\,)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
682 (progn (forward-char -1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
683 (c-backward-to-start-of-continued-exp containing-sexp)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
684 (beginning-of-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
685 (c-backward-to-noncomment containing-sexp))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
686 ;; Check for a preprocessor statement or its continuation lines.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
687 ;; Move back to end of previous non-preprocessor line.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
688 (let ((found (point)) stop)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
689 (while (not stop)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
690 (cond ((save-excursion (end-of-line 0)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
691 (= (preceding-char) ?\\))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
692 (end-of-line 0))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
693 ;; This line is not preceded by a backslash.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
694 ;; So either it starts a preprocessor command
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
695 ;; or any following continuation lines
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
696 ;; should not be skipped.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
697 ((progn (beginning-of-line) (= (following-char) ?#))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
698 (end-of-line 0)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
699 (setq found (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
700 (t (setq stop t))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
701 (goto-char found))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
702 ;; Now we get the answer.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
703 (if (and (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?\{)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
704 ;; But don't treat a line with a close-brace
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
705 ;; as a continuation. It is probably the
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
706 ;; end of an enum type declaration.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
707 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
708 (goto-char indent-point)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
709 (skip-chars-forward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
710 (not (= (following-char) ?}))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
711 ;; This line is continuation of preceding line's statement;
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
712 ;; indent c-continued-statement-offset more than the
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
713 ;; previous line of the statement.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
714 (progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
715 (c-backward-to-start-of-continued-exp containing-sexp)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
716 (+ c-continued-statement-offset (current-column)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
717 (if (save-excursion (goto-char indent-point)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
718 (skip-chars-forward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
719 (eq (following-char) ?{))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
720 c-continued-brace-offset 0)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
721 ;; This line starts a new statement.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
722 ;; Position following last unclosed open.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
723 (goto-char containing-sexp)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
724 ;; Is line first statement after an open-brace?
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
725 (or
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
726 ;; If no, find that first statement and indent like it.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
727 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
728 (forward-char 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
729 (let ((colon-line-end 0))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
730 (while (progn (skip-chars-forward " \t\n")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
731 (looking-at "#\\|/\\*\\|case[ \t\n'/(].*:\\|[a-zA-Z0-9_$]*:"))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
732 ;; Skip over comments and labels following openbrace.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
733 (cond ((= (following-char) ?\#)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
734 (forward-line 1))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
735 ((= (following-char) ?\/)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
736 (forward-char 2)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
737 (search-forward "*/" nil 'move))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
738 ;; case or label:
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
739 (t
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
740 (save-excursion (end-of-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
741 (setq colon-line-end (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
742 (search-forward ":"))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
743 ;; The first following code counts
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
744 ;; if it is before the line we want to indent.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
745 (and (< (point) indent-point)
1054
f6f13367d93c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 909
diff changeset
746 (-
f6f13367d93c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 909
diff changeset
747 (if (> colon-line-end (point))
f6f13367d93c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 909
diff changeset
748 (- (current-indentation) c-label-offset)
f6f13367d93c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 909
diff changeset
749 (current-column))
f6f13367d93c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 909
diff changeset
750 ;; If prev stmt starts with open-brace, that
f6f13367d93c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 909
diff changeset
751 ;; open brace was offset by c-brace-offset.
f6f13367d93c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 909
diff changeset
752 ;; Compensate to get the column where
f6f13367d93c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 909
diff changeset
753 ;; an ordinary statement would start.
f6f13367d93c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 909
diff changeset
754 (if (= (following-char) ?\{) c-brace-offset 0)))))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
755 ;; If no previous statement,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
756 ;; indent it relative to line brace is on.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
757 ;; For open brace in column zero, don't let statement
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
758 ;; start there too. If c-indent-level is zero,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
759 ;; use c-brace-offset + c-continued-statement-offset instead.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
760 ;; For open-braces not the first thing in a line,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
761 ;; add in c-brace-imaginary-offset.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
762 (+ (if (and (bolp) (zerop c-indent-level))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
763 (+ c-brace-offset c-continued-statement-offset)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
764 c-indent-level)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
765 ;; Move back over whitespace before the openbrace.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
766 ;; If openbrace is not first nonwhite thing on the line,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
767 ;; add the c-brace-imaginary-offset.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
768 (progn (skip-chars-backward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
769 (if (bolp) 0 c-brace-imaginary-offset))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
770 ;; If the openbrace is preceded by a parenthesized exp,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
771 ;; move to the beginning of that;
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
772 ;; possibly a different line
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
773 (progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
774 (if (eq (preceding-char) ?\))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
775 (forward-sexp -1))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
776 ;; Get initial indentation of the line we are on.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
777 (current-indentation))))))))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
778
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
779 (defun calculate-c-indent-within-comment (&optional after-star)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
780 "Return the indentation amount for line inside a block comment.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
781 Optional arg AFTER-STAR means, if lines in the comment have a leading star,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
782 return the indentation of the text that would follow this star."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
783 (let (end star-start)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
784 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
785 (beginning-of-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
786 (skip-chars-forward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
787 (setq star-start (= (following-char) ?\*))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
788 (skip-chars-backward " \t\n")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
789 (setq end (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
790 (beginning-of-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
791 (skip-chars-forward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
792 (if after-star
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
793 (and (looking-at "\\*")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
794 (re-search-forward "\\*[ \t]*")))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
795 (and (re-search-forward "/\\*[ \t]*" end t)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
796 star-start
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
797 (not after-star)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
798 (goto-char (1+ (match-beginning 0))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
799 (if (and (looking-at "[ \t]*$") (= (preceding-char) ?\*))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
800 (1+ (current-column))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
801 (current-column)))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
802
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
803
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
804 (defun c-backward-to-noncomment (lim)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
805 (let (opoint stop)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
806 (while (not stop)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
807 (skip-chars-backward " \t\n\f" lim)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
808 (setq opoint (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
809 (if (and (>= (point) (+ 2 lim))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
810 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
811 (forward-char -2)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
812 (looking-at "\\*/")))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
813 (search-backward "/*" lim 'move)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
814 (setq stop (or (<= (point) lim)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
815 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
816 (beginning-of-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
817 (skip-chars-forward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
818 (not (looking-at "#")))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
819 (or stop (beginning-of-line))))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
820
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
821 (defun c-backward-to-start-of-continued-exp (lim)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
822 (if (memq (preceding-char) '(?\) ?\"))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
823 (forward-sexp -1))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
824 (beginning-of-line)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
825 (if (<= (point) lim)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
826 (goto-char (1+ lim)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
827 (skip-chars-forward " \t"))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
828
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
829 (defun c-backward-to-start-of-if (&optional limit)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
830 "Move to the start of the last \"unbalanced\" `if'."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
831 (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
832 (let ((if-level 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
833 (case-fold-search nil))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
834 (while (and (not (bobp)) (not (zerop if-level)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
835 (backward-sexp 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
836 (cond ((looking-at "else\\b")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
837 (setq if-level (1+ if-level)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
838 ((looking-at "if\\b")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
839 (setq if-level (1- if-level)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
840 ((< (point) limit)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
841 (setq if-level 0)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
842 (goto-char limit))))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
843
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
844 (defun c-backward-to-start-of-do (&optional limit)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
845 "If point follows a `do' statement, move to beginning of it and return t.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
846 Otherwise return nil and don't move point."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
847 (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
848 (let ((first t)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
849 (startpos (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
850 (done nil))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
851 (while (not done)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
852 (let ((next-start (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
853 (condition-case nil
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
854 ;; Move back one token or one brace or paren group.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
855 (backward-sexp 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
856 ;; If we find an open-brace, we lose.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
857 (error (setq done 'fail)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
858 (if done
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
859 nil
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
860 ;; If we reached a `do', we win.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
861 (if (looking-at "do\\b")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
862 (setq done 'succeed)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
863 ;; Otherwise, if we skipped a semicolon, we lose.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
864 ;; (Exception: we can skip one semicolon before getting
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
865 ;; to a the last token of the statement, unless that token
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
866 ;; is a close brace.)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
867 (if (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
868 (forward-sexp 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
869 (or (and (not first) (= (preceding-char) ?}))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
870 (search-forward ";" next-start t
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
871 (if (and first
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
872 (/= (preceding-char) ?}))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
873 2 1))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
874 (setq done 'fail)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
875 (setq first nil)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
876 ;; If we go too far back in the buffer, we lose.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
877 (if (< (point) limit)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
878 (setq done 'fail)))))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
879 (if (eq done 'succeed)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
880 t
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
881 (goto-char startpos)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
882 nil)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
883
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
884 (defun c-beginning-of-statement (count)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
885 "Go to the beginning of the innermost C statement.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
886 With prefix arg, go back N - 1 statements. If already at the beginning of a
3591
507f64624555 Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents: 3256
diff changeset
887 statement then go to the beginning of the preceding one.
2084
9561d2584cbb (c-beginning-of-statement): If next to a comment, use sentence motion.
Richard M. Stallman <rms@gnu.org>
parents: 1821
diff changeset
888 If within a string or comment, or next to a comment (only whitespace between),
9561d2584cbb (c-beginning-of-statement): If next to a comment, use sentence motion.
Richard M. Stallman <rms@gnu.org>
parents: 1821
diff changeset
889 move by sentences instead of statements."
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
890 (interactive "p")
871
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
891 (let ((here (point)) state)
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
892 (save-excursion
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
893 (beginning-of-defun)
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
894 (setq state (parse-partial-sexp (point) here nil nil)))
2084
9561d2584cbb (c-beginning-of-statement): If next to a comment, use sentence motion.
Richard M. Stallman <rms@gnu.org>
parents: 1821
diff changeset
895 (if (or (nth 3 state) (nth 4 state)
9561d2584cbb (c-beginning-of-statement): If next to a comment, use sentence motion.
Richard M. Stallman <rms@gnu.org>
parents: 1821
diff changeset
896 (looking-at (concat "[ \t]*" comment-start-skip))
9561d2584cbb (c-beginning-of-statement): If next to a comment, use sentence motion.
Richard M. Stallman <rms@gnu.org>
parents: 1821
diff changeset
897 (save-excursion (skip-chars-backward " \t")
9561d2584cbb (c-beginning-of-statement): If next to a comment, use sentence motion.
Richard M. Stallman <rms@gnu.org>
parents: 1821
diff changeset
898 (goto-char (- (point) 2))
9561d2584cbb (c-beginning-of-statement): If next to a comment, use sentence motion.
Richard M. Stallman <rms@gnu.org>
parents: 1821
diff changeset
899 (looking-at "\\*/")))
871
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
900 (forward-sentence (- count))
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
901 (while (> count 0)
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
902 (c-beginning-of-statement-1)
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
903 (setq count (1- count)))
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
904 (while (< count 0)
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
905 (c-end-of-statement-1)
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
906 (setq count (1+ count))))))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
907
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
908 (defun c-end-of-statement (count)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
909 "Go to the end of the innermost C statement.
871
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
910 With prefix arg, go forward N - 1 statements.
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
911 Move forward to end of the next statement if already at end.
34b6b37092c7 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 861
diff changeset
912 If within a string or comment, move by sentences instead of statements."
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
913 (interactive "p")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
914 (c-beginning-of-statement (- count)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
915
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
916 (defun c-beginning-of-statement-1 ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
917 (let ((last-begin (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
918 (first t))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
919 (condition-case ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
920 (progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
921 (while (and (not (bobp))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
922 (progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
923 (backward-sexp 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
924 (or first
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
925 (not (re-search-forward "[;{}]" last-begin t)))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
926 (setq last-begin (point) first nil))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
927 (goto-char last-begin))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
928 (error (if first (backward-up-list 1) (goto-char last-begin))))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
929
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
930 (defun c-end-of-statement-1 ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
931 (condition-case ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
932 (progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
933 (while (and (not (eobp))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
934 (let ((beg (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
935 (forward-sexp 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
936 (let ((end (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
937 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
938 (goto-char beg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
939 (not (re-search-forward "[;{}]" end t)))))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
940 (re-search-backward "[;}]")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
941 (forward-char 1))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
942 (error
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
943 (let ((beg (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
944 (backward-up-list -1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
945 (let ((end (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
946 (goto-char beg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
947 (search-forward ";" end 'move))))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
948
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
949 (defun mark-c-function ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
950 "Put mark at end of C function, point at beginning."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
951 (interactive)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
952 (push-mark (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
953 (end-of-defun)
2829
688e57a7ade8 (mark-c-function): Activate the mark.
Richard M. Stallman <rms@gnu.org>
parents: 2307
diff changeset
954 (push-mark (point) nil t)
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
955 (beginning-of-defun)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
956 (backward-paragraph))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
957
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
958 (defun indent-c-exp (&optional endpos)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
959 "Indent each line of the C grouping following point.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
960 If optional arg ENDPOS is given, indent each line, stopping when
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
961 ENDPOS is encountered."
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
962 (interactive)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
963 (let* ((indent-stack (list nil))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
964 (opoint (point)) ;; May be altered below.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
965 (contain-stack
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
966 (list (if endpos
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
967 (let (funbeg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
968 ;; Find previous fcn-start.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
969 (save-excursion (forward-char 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
970 (beginning-of-defun)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
971 (setq funbeg (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
972 ;; Try to find containing open,
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
973 ;; but don't scan past that fcn-start.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
974 (save-restriction
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
975 (narrow-to-region funbeg (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
976 (condition-case nil
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
977 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
978 (backward-up-list 1) (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
979 ;; We gave up: must be between fcns.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
980 ;; Set opoint to beg of prev fcn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
981 ;; since otherwise calculate-c-indent
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
982 ;; will get wrong answers.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
983 (error (setq opoint funbeg)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
984 (point)))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
985 (point))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
986 (case-fold-search nil)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
987 restart outer-loop-done inner-loop-done state ostate
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
988 this-indent last-sexp
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
989 at-else at-brace at-while
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
990 last-depth
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
991 (next-depth 0))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
992 ;; If the braces don't match, get an error right away.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
993 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
994 (forward-sexp 1))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
995 ;; Realign the comment on the first line, even though we don't reindent it.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
996 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
997 (let ((beg (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
998 (and (re-search-forward
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
999 comment-start-skip
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1000 (save-excursion (end-of-line) (point)) t)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1001 ;; Make sure the comment starter we found
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1002 ;; is not actually in a string or quoted.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1003 (let ((new-state
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1004 (parse-partial-sexp beg (point)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1005 nil nil state)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1006 (and (not (nth 3 new-state)) (not (nth 5 new-state))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1007 (progn (indent-for-comment) (beginning-of-line)))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1008 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1009 (setq outer-loop-done nil)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1010 (while (and (not (eobp))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1011 (if endpos (< (point) endpos)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1012 (not outer-loop-done)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1013 (setq last-depth next-depth)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1014 ;; Compute how depth changes over this line
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1015 ;; plus enough other lines to get to one that
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1016 ;; does not end inside a comment or string.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1017 ;; Meanwhile, do appropriate indentation on comment lines.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1018 (setq inner-loop-done nil)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1019 (while (and (not inner-loop-done)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1020 (not (and (eobp) (setq outer-loop-done t))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1021 (setq ostate state)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1022 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1023 nil nil state))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1024 (setq next-depth (car state))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1025 (if (and (car (cdr (cdr state)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1026 (>= (car (cdr (cdr state))) 0))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1027 (setq last-sexp (car (cdr (cdr state)))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1028 (if (or (nth 4 ostate))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1029 (c-indent-line))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1030 (if (or (nth 3 state))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1031 (forward-line 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1032 (setq inner-loop-done t)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1033 (and endpos
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1034 (while (< next-depth 0)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1035 (setq indent-stack (append indent-stack (list nil)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1036 (setq contain-stack (append contain-stack (list nil)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1037 (setq next-depth (1+ next-depth))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1038 (setq last-depth (1+ last-depth))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1039 (setcar (nthcdr 6 state) (1+ (nth 6 state)))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1040 (setq outer-loop-done (and (not endpos) (<= next-depth 0)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1041 (if outer-loop-done
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1042 nil
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1043 ;; If this line had ..))) (((.. in it, pop out of the levels
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1044 ;; that ended anywhere in this line, even if the final depth
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1045 ;; doesn't indicate that they ended.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1046 (while (> last-depth (nth 6 state))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1047 (setq indent-stack (cdr indent-stack)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1048 contain-stack (cdr contain-stack)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1049 last-depth (1- last-depth)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1050 (if (/= last-depth next-depth)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1051 (setq last-sexp nil))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1052 ;; Add levels for any parens that were started in this line.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1053 (while (< last-depth next-depth)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1054 (setq indent-stack (cons nil indent-stack)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1055 contain-stack (cons nil contain-stack)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1056 last-depth (1+ last-depth)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1057 (if (null (car contain-stack))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1058 (setcar contain-stack (or (car (cdr state))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1059 (save-excursion (forward-sexp -1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1060 (point)))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1061 (forward-line 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1062 (skip-chars-forward " \t")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1063 (if (eolp)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1064 nil
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1065 (if (and (car indent-stack)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1066 (>= (car indent-stack) 0))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1067 ;; Line is on an existing nesting level.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1068 ;; Lines inside parens are handled specially.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1069 (if (/= (char-after (car contain-stack)) ?{)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1070 (setq this-indent (car indent-stack))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1071 ;; Line is at statement level.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1072 ;; Is it a new statement? Is it an else?
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1073 ;; Find last non-comment character before this line
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1074 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1075 (setq at-else (looking-at "else\\W"))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1076 (setq at-brace (= (following-char) ?{))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1077 (setq at-while (looking-at "while\\b"))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1078 (c-backward-to-noncomment opoint)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1079 (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?{)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1080 ;; Preceding line did not end in comma or semi;
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1081 ;; indent this line c-continued-statement-offset
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1082 ;; more than previous.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1083 (progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1084 (c-backward-to-start-of-continued-exp (car contain-stack))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1085 (setq this-indent
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1086 (+ c-continued-statement-offset (current-column)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1087 (if at-brace c-continued-brace-offset 0))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1088 ;; Preceding line ended in comma or semi;
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1089 ;; use the standard indent for this level.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1090 (cond (at-else (progn (c-backward-to-start-of-if opoint)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1091 (setq this-indent
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1092 (current-indentation))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1093 ((and at-while (c-backward-to-start-of-do opoint))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1094 (setq this-indent (current-indentation)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1095 (t (setq this-indent (car indent-stack)))))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1096 ;; Just started a new nesting level.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1097 ;; Compute the standard indent for this level.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1098 (let ((val (calculate-c-indent
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1099 (if (car indent-stack)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1100 (- (car indent-stack))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1101 opoint))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1102 (setcar indent-stack
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1103 (setq this-indent val))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1104 ;; Adjust line indentation according to its contents
1804
fe2bebf150c7 * c-mode.el (c-switch-label-regexp): New constant.
Jim Blandy <jimb@redhat.com>
parents: 1701
diff changeset
1105 (if (or (looking-at c-switch-label-regexp)
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1106 (and (looking-at "[A-Za-z]")
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1107 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1108 (forward-sexp 1)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1109 (looking-at ":"))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1110 (setq this-indent (max 1 (+ this-indent c-label-offset))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1111 (if (= (following-char) ?})
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1112 (setq this-indent (- this-indent c-indent-level)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1113 (if (= (following-char) ?{)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1114 (setq this-indent (+ this-indent c-brace-offset)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1115 ;; Don't leave indentation in empty lines.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1116 (if (eolp) (setq this-indent 0))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1117 ;; Put chosen indentation into effect.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1118 (or (= (current-column) this-indent)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1119 (= (following-char) ?\#)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1120 (progn
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1121 (delete-region (point) (progn (beginning-of-line) (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1122 (indent-to this-indent)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1123 ;; Indent any comment following the text.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1124 (or (looking-at comment-start-skip)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1125 (let ((beg (point)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1126 (and (re-search-forward
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1127 comment-start-skip
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1128 (save-excursion (end-of-line) (point)) t)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1129 ;; Make sure the comment starter we found
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1130 ;; is not actually in a string or quoted.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1131 (let ((new-state
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1132 (parse-partial-sexp beg (point)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1133 nil nil state)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1134 (and (not (nth 3 new-state)) (not (nth 5 new-state))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1135 (progn (indent-for-comment) (beginning-of-line)))))))))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1136
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1137 ;; Look at all comment-start strings in the current line after point.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1138 ;; Return t if one of them starts a real comment.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1139 ;; This is not used yet, because indent-for-comment
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1140 ;; isn't smart enough to handle the cases this can find.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1141 (defun indent-c-find-real-comment ()
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1142 (let (win)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1143 (while (and (not win)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1144 (re-search-forward comment-start-skip
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1145 (save-excursion (end-of-line) (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1146 t))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1147 ;; Make sure the comment start is not quoted.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1148 (let ((state-1
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1149 (parse-partial-sexp
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1150 (save-excursion (beginning-of-line) (point))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1151 (point) nil nil state)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1152 (setq win (and (null (nth 3 state-1)) (null (nth 5 state-1))))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1153 win))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1154
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1155 ;; Indent every line whose first char is between START and END inclusive.
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1156 (defun c-indent-region (start end)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1157 (save-excursion
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1158 (goto-char start)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1159 (let ((endmark (copy-marker end)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1160 (and (bolp) (not (eolp))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1161 (c-indent-line))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1162 (indent-c-exp endmark)
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1163 (set-marker endmark nil))))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1164
837
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1165 (defun set-c-style (style &optional global)
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1166 "Set C-mode variables to use one of several different indentation styles.
837
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1167 The arguments are a string representing the desired style
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1168 and a flag which, if non-nil, means to set the style globally.
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1169 \(Interactively, the flag comes from the prefix argument.)
861
345296f94a1e *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 846
diff changeset
1170 Available styles are GNU, K&R, BSD and Whitesmith."
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1171 (interactive (list (completing-read "Use which C indentation style? "
837
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1172 c-style-alist nil t)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1173 current-prefix-arg))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1174 (let ((vars (cdr (assoc style c-style-alist))))
837
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1175 (or vars
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1176 (error "Invalid C indentation style `%s'" style))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1177 (while vars
837
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1178 (or global
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1179 (make-local-variable (car (car vars))))
453
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1180 (set (car (car vars)) (cdr (car vars)))
7c8c17669828 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1181 (setq vars (cdr vars)))))
837
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1182
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1183 ;;; This page handles insertion and removal of backslashes for C macros.
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1184
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1185 (defvar c-backslash-column 48
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1186 "*Minimum column for end-of-line backslashes of macro definitions.")
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1187
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1188 (defun c-backslash-region (from to delete-flag)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1189 "Insert, align, or delete end-of-line backslashes on the lines in the region.
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1190 With no argument, inserts backslashes and aligns existing backslashes.
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1191 With an argument, deletes the backslashes.
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1192
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1193 This function does not modify the last line of the region if the region ends
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1194 right at the start of the following line; it does not modify blank lines
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1195 at the start of the region. So you can put the region around an entire macro
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1196 definition and conveniently use this command."
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1197 (interactive "r\nP")
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1198 (save-excursion
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1199 (goto-char from)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1200 (let ((column c-backslash-column)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1201 (endmark (make-marker)))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1202 (move-marker endmark to)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1203 ;; Compute the smallest column number past the ends of all the lines.
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1204 (if (not delete-flag)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1205 (while (< (point) to)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1206 (end-of-line)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1207 (if (= (preceding-char) ?\\)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1208 (progn (forward-char -1)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1209 (skip-chars-backward " \t")))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1210 (setq column (max column (1+ (current-column))))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1211 (forward-line 1)))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1212 ;; Adjust upward to a tab column, if that doesn't push past the margin.
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1213 (if (> (% column tab-width) 0)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1214 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width)))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1215 (if (< adjusted (window-width))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1216 (setq column adjusted))))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1217 ;; Don't modify blank lines at start of region.
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1218 (goto-char from)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1219 (while (and (< (point) endmark) (eolp))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1220 (forward-line 1))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1221 ;; Add or remove backslashes on all the lines.
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1222 (while (and (< (point) endmark)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1223 ;; Don't backslashify the last line
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1224 ;; if the region ends right at the start of the next line.
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1225 (save-excursion
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1226 (forward-line 1)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1227 (< (point) endmark)))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1228 (if (not delete-flag)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1229 (c-append-backslash column)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1230 (c-delete-backslash))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1231 (forward-line 1))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1232 (move-marker endmark nil))))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1233
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1234 (defun c-append-backslash (column)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1235 (end-of-line)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1236 ;; Note that "\\\\" is needed to get one backslash.
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1237 (if (= (preceding-char) ?\\)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1238 (progn (forward-char -1)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1239 (delete-horizontal-space)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1240 (indent-to column))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1241 (indent-to column)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1242 (insert "\\")))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1243
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1244 (defun c-delete-backslash ()
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1245 (end-of-line)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1246 (forward-char -1)
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1247 (if (looking-at "\\\\")
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1248 (delete-region (1+ (point))
a8aef92e0025 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 807
diff changeset
1249 (progn (skip-chars-backward " \t") (point)))))
909
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1250
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1251 (defun c-up-conditional (count)
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1252 "Move back to the containing preprocessor conditional, leaving mark behind.
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1253 A prefix argument acts as a repeat count. With a negative argument,
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1254 move forward to the end of the containing preprocessor conditional.
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1255 When going backwards, `#elif' is treated like `#else' followed by `#if'.
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1256 When going forwards, `#elif' is ignored."
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1257 (interactive "p")
3906
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1258 (c-forward-conditional (- count) t))
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1259
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1260 (defun c-backward-conditional (count &optional up-flag)
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1261 "Move back across a preprocessor conditional, leaving mark behind.
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1262 A prefix argument acts as a repeat count. With a negative argument,
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1263 move forward across a preprocessor conditional."
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1264 (interactive "p")
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1265 (c-forward-conditional (- count) up-flag))
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1266
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1267 (defun c-forward-conditional (count &optional up-flag)
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1268 "Move forward across a preprocessor conditional, leaving mark behind.
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1269 A prefix argument acts as a repeat count. With a negative argument,
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1270 move backward across a preprocessor conditional."
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1271 (interactive "p")
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1272 (let* ((forward (> count 0))
909
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1273 (increment (if forward -1 1))
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1274 (search-function (if forward 're-search-forward 're-search-backward))
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1275 (opoint (point))
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1276 (new))
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1277 (save-excursion
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1278 (while (/= count 0)
3906
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1279 (let ((depth (if up-flag 0 -1)) found)
909
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1280 (save-excursion
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1281 ;; Find the "next" significant line in the proper direction.
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1282 (while (and (not found)
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1283 ;; Rather than searching for a # sign that comes
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1284 ;; at the beginning of a line aside from whitespace,
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1285 ;; search first for a string starting with # sign.
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1286 ;; Then verify what precedes it.
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1287 ;; This is faster on account of the fastmap feature of
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1288 ;; the regexp matcher.
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1289 (funcall search-function
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1290 "#[ \t]*\\(if\\|elif\\|endif\\)"
3256
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1291 nil t))
909
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1292 (beginning-of-line)
3256
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1293 ;; Now verify it is really a preproc line.
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1294 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)")
3906
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1295 (let ((prev depth))
3256
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1296 ;; Update depth according to what we found.
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1297 (beginning-of-line)
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1298 (cond ((looking-at "[ \t]*#[ \t]*endif")
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1299 (setq depth (+ depth increment)))
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1300 ((looking-at "[ \t]*#[ \t]*elif")
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1301 (if (and forward (= depth 0))
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1302 (setq found (point))))
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1303 (t (setq depth (- depth increment))))
3906
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1304 ;; If we are trying to move across, and we find
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1305 ;; an end before we find a beginning, get an error.
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1306 (if (and (< prev 0) (< depth prev))
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1307 (error (if forward
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1308 "No following conditional at this level"
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1309 "No previous conditional at this level")))
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1310 ;; When searching forward, start from next line
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1311 ;; so that we don't find the same line again.
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1312 (if forward (forward-line 1))
3256
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1313 ;; If this line exits a level of conditional, exit inner loop.
14eb227cad41 (c-up-conditional): Handle commented-out #-cmds properly.
Richard M. Stallman <rms@gnu.org>
parents: 2829
diff changeset
1314 (if (< depth 0)
3906
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1315 (setq found (point)))))))
909
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1316 (or found
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1317 (error "No containing preprocessor conditional"))
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1318 (goto-char (setq new found)))
3906
a03d6cac1cd1 (c-forward-conditional): New function.
Richard M. Stallman <rms@gnu.org>
parents: 3716
diff changeset
1319 (setq count (+ count increment))))
909
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1320 (push-mark)
4c6cdb66c74c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 871
diff changeset
1321 (goto-char new)))
662
8a533acedb77 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 472
diff changeset
1322
8a533acedb77 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 472
diff changeset
1323 ;;; c-mode.el ends here