annotate lisp/progmodes/c-mode.el @ 23462:aaad8461ff34

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