annotate lisp/progmodes/cc-cmds.el @ 29191:3977c8167022

(handle_invisible_prop): Don't try to skip over invisible text if end of text is already reached.
author Gerd Moellmann <gerd@gnu.org>
date Thu, 25 May 2000 15:53:05 +0000
parents cad48fa4c22d
children 4fe5cb975331
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1 ;;; cc-cmds.el --- user level commands for CC Mode
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
3 ;; Copyright (C) 1985,1987,1992-1999 Free Software Foundation, Inc.
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
5 ;; Authors: 1998-1999 Barry A. Warsaw and Martin Stjernholm
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
6 ;; 1992-1997 Barry A. Warsaw
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7 ;; 1987 Dave Detlefs and Stewart Clamen
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 ;; 1985 Richard M. Stallman
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
9 ;; Maintainer: bug-cc-mode@gnu.org
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10 ;; Created: 22-Apr-1997 (split from cc-mode.el)
20143
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
11 ;; Version: See cc-mode.el
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; Keywords: c languages oop
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14 ;; This file is part of GNU Emacs.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; it under the terms of the GNU General Public License as published by
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; the Free Software Foundation; either version 2, or (at your option)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; any later version.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 ;; GNU Emacs is distributed in the hope that it will be useful,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24 ;; GNU General Public License for more details.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26 ;; You should have received a copy of the GNU General Public License
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 ;; Boston, MA 02111-1307, USA.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
31 (eval-when-compile
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
32 (let ((load-path
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
33 (if (and (boundp 'byte-compile-current-file)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
34 (stringp byte-compile-current-file))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
35 (cons (file-name-directory byte-compile-current-file)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
36 load-path)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
37 load-path)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
38 (load "cc-defs" nil t)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
39 (require 'cc-engine)
18843
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
40
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 (defun c-calculate-state (arg prevstate)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 ;; arg is nil or zero, toggle the state. If arg is negative, turn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 ;; the state off, and if arg is positive, turn the state on
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 (if (or (not arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 (zerop (setq arg (prefix-numeric-value arg))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 (not prevstate)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 (> arg 0)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 ;; Auto-newline and hungry-delete
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 (defun c-toggle-auto-state (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 "Toggle auto-newline feature.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 Optional numeric ARG, if supplied turns on auto-newline when positive,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 turns it off when negative, and just toggles it when zero.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 When the auto-newline feature is enabled (as evidenced by the `/a' or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 `/ah' on the modeline after the mode name) newlines are automatically
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 inserted after special characters such as brace, comma, semi-colon,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 and colon."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 (interactive "P")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 (c-update-modeline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 (defun c-toggle-hungry-state (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 "Toggle hungry-delete-key feature.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 Optional numeric ARG, if supplied turns on hungry-delete when positive,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 turns it off when negative, and just toggles it when zero.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71 When the hungry-delete-key feature is enabled (as evidenced by the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 `/h' or `/ah' on the modeline after the mode name) the delete key
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 gobbles all preceding whitespace in one fell swoop."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 (interactive "P")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76 (c-update-modeline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79 (defun c-toggle-auto-hungry-state (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
80 "Toggle auto-newline and hungry-delete-key features.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 Optional numeric ARG, if supplied turns on auto-newline and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 hungry-delete when positive, turns them off when negative, and just
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 toggles them when zero.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85 See `c-toggle-auto-state' and `c-toggle-hungry-state' for details."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86 (interactive "P")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 (c-update-modeline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93 ;; Electric keys
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 ;; Note: In XEmacs 20.3 the Delete and BackSpace keysyms have been
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 ;; separated and "\177" is no longer an alias for both keys. Also,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 ;; the variable delete-key-deletes-forward controls in which direction
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 ;; the Delete keysym deletes characters. The functions
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 ;; c-electric-delete and c-electric-backspace attempt to deal with
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 ;; this new functionality. For Emacs 19 and XEmacs 19 backwards
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 ;; compatibility, the old behavior has moved to c-electric-backspace
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 ;; and c-backspace-function.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 (defun c-electric-backspace (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 "Deletes preceding character or whitespace.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 \"/ah\" string on the mode line, then all preceding whitespace is
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 consumed. If however an ARG is supplied, or `c-hungry-delete-key' is
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 nil, or point is inside a literal then the function in the variable
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 `c-backspace-function' is called.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 See also \\[c-electric-delete]."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
113 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 (if (or (not c-hungry-delete-key)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 arg
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116 (c-in-literal))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 (funcall c-backspace-function (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 (let ((here (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 (skip-chars-backward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 (if (/= (point) here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 (delete-region (point) here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 (funcall c-backspace-function 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 ))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 (defun c-electric-delete (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 "Deletes preceding or following character or whitespace.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 The behavior of this function depends on the variable
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 `delete-key-deletes-forward'. If this variable is nil (or does not
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 exist, as in older Emacsen), then this function behaves identical to
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131 \\[c-electric-backspace].
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
133 If `delete-key-deletes-forward' is non-nil and is supported in your
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
134 Emacs, then deletion occurs in the forward direction. So if
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
135 `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
136 \"/ah\" string on the mode line, then all following whitespace is
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
137 consumed. If however an ARG is supplied, or `c-hungry-delete-key' is
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
138 nil, or point is inside a literal then the function in the variable
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
139 `c-delete-function' is called."
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
140 (interactive "*P")
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
141 (if (or (and (fboundp 'delete-forward-p) ;XEmacs 21
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
142 (delete-forward-p))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
143 (and (boundp 'delete-key-deletes-forward) ;XEmacs 20
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
144 delete-key-deletes-forward))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 (if (or (not c-hungry-delete-key)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 arg
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 (c-in-literal))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 (funcall c-delete-function (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 (let ((here (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 (skip-chars-forward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 (if (/= (point) here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 (delete-region (point) here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 (funcall c-delete-function 1))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 ;; act just like c-electric-backspace
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 (c-electric-backspace arg)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 (defun c-electric-pound (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 "Electric pound (`#') insertion.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 Inserts a `#' character specially depending on the variable
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 point is inside a literal, nothing special happens."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
162 (interactive "*P")
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
163 (if (or arg
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
164 (not (memq 'alignleft c-electric-pound-behavior))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
165 (save-excursion (skip-chars-backward " \t") (not (bolp)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
166 (c-in-literal))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 ;; do nothing special
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 ;; place the pound character at the left edge
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 (let ((pos (- (point-max) (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 (bolp (bolp)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 (delete-horizontal-space)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 (insert-char last-command-char 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 (and (not bolp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 (goto-char (- (point-max) pos)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 )))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 (defun c-electric-brace (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 "Insert a brace.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 If the auto-newline feature is turned on, as evidenced by the \"/a\"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 or \"/ah\" string on the mode line, newlines are inserted before and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 after braces based on the value of `c-hanging-braces-alist'.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 Also, the line is re-indented unless a numeric ARG is supplied, there
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 are non-whitespace characters present on the line after the brace, or
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
188 the brace is inserted inside a literal.
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
189
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
190 This function does various newline cleanups based on the value of
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
191 `c-cleanup-list'."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
192 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193 (let* ((c-state-cache (c-parse-state))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 (safepos (c-safe-position (point) c-state-cache))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 (literal (c-in-literal safepos)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 ;; if we're in a literal, or we're not at the end of the line, or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 ;; a numeric arg is provided, or auto-newlining is turned off,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 ;; then just insert the character.
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
199 (if (or literal
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
200 arg
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 (not (looking-at "[ \t]*$")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 (self-insert-command (prefix-numeric-value arg))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
203 (let* ((syms
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
204 ;; This is the list of brace syntactic symbols that can
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
205 ;; hang. If any new ones are added to c-offsets-alist,
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
206 ;; they should be added here as well.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
207 '(class-open class-close defun-open defun-close
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
208 inline-open inline-close
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
209 brace-list-open brace-list-close
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
210 brace-list-intro brace-entry-open
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
211 block-open block-close
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
212 substatement-open statement-case-open
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
213 extern-lang-open extern-lang-close
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
214 namespace-open namespace-close
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
215 inexpr-class-open inexpr-class-close
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
216 ))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 ;; we want to inhibit blinking the paren since this will
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 ;; be most disruptive. we'll blink it ourselves later on
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 (old-blink-paren blink-paren-function)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 blink-paren-function
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 (insertion-point (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 delete-temp-newline
19306
974544be1a17 (c-electric-brace): Make preserve-p nil at BOB.
Richard M. Stallman <rms@gnu.org>
parents: 19297
diff changeset
223 (preserve-p (and (not (bobp))
974544be1a17 (c-electric-brace): Make preserve-p nil at BOB.
Richard M. Stallman <rms@gnu.org>
parents: 19297
diff changeset
224 (eq ?\ (char-syntax (char-before)))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 ;; shut this up too
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 (c-echo-syntactic-information-p nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 (syntax (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 ;; only insert a newline if there is
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 ;; non-whitespace behind us
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 (if (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 (skip-chars-backward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 (not (bolp)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 (progn (newline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 (setq delete-temp-newline t)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 ;; state cache doesn't change
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 (c-guess-basic-syntax)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 (newlines (and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 c-auto-newline
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
240 (or (c-lookup-lists
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
241 syms
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
242 ;; Substitute inexpr-class and class-open
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
243 ;; or class-close with inexpr-class-open
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
244 ;; or inexpr-class-close.
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
245 (if (assq 'inexpr-class syntax)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
246 (cond ((assq 'class-open syntax)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
247 '((inexpr-class-open)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
248 ((assq 'class-close syntax)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
249 '((inexpr-class-close)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
250 (t syntax))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
251 syntax)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
252 c-hanging-braces-alist)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 '(ignore before after)))))
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
254 ;; Do not try to insert newlines around a special (Pike-style)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
255 ;; brace list.
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
256 (if (and c-special-brace-lists
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
257 (c-intersect-lists '(brace-list-open brace-list-close
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
258 brace-list-intro brace-entry-open)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
259 syntax)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
260 (save-excursion
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
261 (c-safe (if (= (char-before) ?{)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
262 (forward-char -1)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
263 (c-forward-sexp -1))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
264 (c-looking-at-special-brace-list))))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
265 (setq newlines nil))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 ;; If syntax is a function symbol, then call it using the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 ;; defined semantics.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 (if (and (not (consp (cdr newlines)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 (functionp (cdr newlines)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 (let ((c-syntactic-context syntax))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271 (setq newlines
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 (funcall (cdr newlines) (car newlines) insertion-point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 ;; does a newline go before the open brace?
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 (if (memq 'before newlines)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275 ;; we leave the newline we've put in there before,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 ;; but we need to re-indent the line above
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 (let ((pos (- (point-max) (point)))
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
278 (here (point)))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 (forward-line -1)
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
280 (let ((c-state-cache (c-whack-state (point) c-state-cache)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
281 ;; we may need to update the cache. this should
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
282 ;; still be faster than recalculating the state
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
283 ;; in many cases
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
284 (save-excursion
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
285 (save-restriction
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
286 (narrow-to-region here (point))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
287 (if (and (c-safe (progn (backward-up-list -1) t))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
288 (memq (char-before) '(?\) ?}))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
289 (progn (widen)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
290 (c-safe (progn (c-forward-sexp -1)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
291 t))))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
292 (setq c-state-cache
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
293 (c-hack-state (point) 'open c-state-cache)))))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
294 (c-indent-line))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
295 (setq c-state-cache (c-adjust-state (c-point 'bol) here
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
296 (- (point) (c-point 'bol))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
297 c-state-cache))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298 (goto-char (- (point-max) pos))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299 ;; if the buffer has changed due to the indentation, we
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 ;; need to recalculate syntax for the current line, but
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301 ;; we won't need to update the state cache.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302 (if (/= (point) here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303 (setq syntax (c-guess-basic-syntax))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304 ;; must remove the newline we just stuck in (if we really did it)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
305 (and delete-temp-newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
306 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307 ;; if there is whitespace before point, then preserve
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308 ;; at least one space.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309 (delete-indentation)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 (just-one-space)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311 (if (not preserve-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 (delete-char -1))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313 ;; since we're hanging the brace, we need to recalculate
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314 ;; syntax. Update the state to accurately reflect the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 ;; beginning of the line. We punt if we cross any open or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316 ;; closed parens because its just too hard to modify the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
317 ;; known state. This limitation will be fixed in v5.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319 (let ((bol (c-point 'bol)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 (if (zerop (car (parse-partial-sexp bol (1- (point)))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 (setq c-state-cache (c-whack-state bol c-state-cache)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
322 syntax (c-guess-basic-syntax))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
323 ;; gotta punt. this requires some horrible kludgery
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
325 (makunbound 'c-state-cache)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 (setq c-state-cache (c-parse-state)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327 syntax nil))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328 )
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 ;; now adjust the line's indentation. don't update the state
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 ;; cache since c-guess-basic-syntax isn't called when the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331 ;; syntax is passed to c-indent-line
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
332 (let* ((here (point)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
333 (c-indent-line syntax)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 (setq c-state-cache (c-adjust-state (c-point 'bol) here
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
335 (- (c-point 'boi) (c-point 'bol))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
336 c-state-cache)))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337 ;; Do all appropriate clean ups
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 (let ((here (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 (pos (- (point-max) (point)))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
340 mbeg mend tmp)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 ;; clean up empty defun braces
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342 (if (and c-auto-newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343 (memq 'empty-defun-braces c-cleanup-list)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344 (eq last-command-char ?\})
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 (c-intersect-lists '(defun-close class-close inline-close)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 syntax)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348 (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349 (skip-chars-backward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350 (eq (char-before) ?\{))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 ;; make sure matching open brace isn't in a comment
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352 (not (c-in-literal)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353 (delete-region (point) (1- here)))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
354 ;; clean up brace-else-brace and brace-elseif-brace
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
355 (when (and c-auto-newline
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
356 (eq last-command-char ?\{)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
357 (not (c-in-literal)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
358 (cond
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
359 ((and (memq 'brace-else-brace c-cleanup-list)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360 (re-search-backward "}[ \t\n]*else[ \t\n]*{" nil t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 (setq mbeg (match-beginning 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 mend (match-end 0))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
364 (eq (match-end 0) here)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
365 (delete-region mbeg mend)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
366 (insert "} else {"))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
367 ((and (memq 'brace-elseif-brace c-cleanup-list)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
368 (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
369 (goto-char (1- here))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
370 (setq mend (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
371 (skip-chars-backward " \t\n")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
372 (setq mbeg (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
373 (eq (char-before) ?\)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
374 (= (c-backward-token-1 1 t) 0)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
375 (eq (char-after) ?\()
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
376 (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
377 (setq tmp (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
378 (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*"
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
379 nil t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
380 (eq (match-end 0) tmp))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
381 (delete-region mbeg mend)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
382 (goto-char mbeg)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
383 (insert " "))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
384 (goto-char (- (point-max) pos))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
385 )
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
386 ;; does a newline go after the brace?
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387 (if (memq 'after newlines)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389 (newline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390 ;; update on c-state-cache
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391 (let* ((bufpos (- (point) 2))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392 (which (if (eq (char-after bufpos) ?{) 'open 'close))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 (c-state-cache (c-hack-state bufpos which c-state-cache)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394 (c-indent-line))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395 ;; blink the paren
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
396 (and (eq last-command-char ?\})
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397 old-blink-paren
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
399 (c-backward-syntactic-ws safepos)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400 (funcall old-blink-paren)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 ))))
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
402
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403 (defun c-electric-slash (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 "Insert a slash character.
19297
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
405
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
406 Indent the line as a comment, if:
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
407
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
408 1. The slash is second of a `//' line oriented comment introducing
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
409 token and we are on a comment-only-line, or
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
410
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
411 2. The slash is part of a `*/' token that closes a block oriented
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
412 comment.
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
413
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
414 If numeric ARG is supplied or point is inside a literal, indentation
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
415 is inhibited."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
416 (interactive "*P")
19297
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
417 (let* ((ch (char-before))
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
418 (indentp (and (not arg)
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
419 (eq last-command-char ?/)
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
420 (or (and (eq ch ?/)
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
421 (not (c-in-literal)))
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
422 (and (eq ch ?*)
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
423 (c-in-literal)))
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
424 ))
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
425 ;; shut this up
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
426 (c-echo-syntactic-information-p nil))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
427 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
428 (if indentp
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
429 (c-indent-line))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
430
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
431 (defun c-electric-star (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
432 "Insert a star character.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
433 If the star is the second character of a C style comment introducing
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
434 construct, and we are on a comment-only-line, indent line as comment.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
435 If numeric ARG is supplied or point is inside a literal, indentation
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
436 is inhibited."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
437 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
438 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
439 ;; if we are in a literal, or if arg is given do not re-indent the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
440 ;; current line, unless this star introduces a comment-only line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
441 (if (and (not arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
442 (memq (c-in-literal) '(c))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
443 (eq (char-before) ?*)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
444 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
445 (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
446 (skip-chars-backward "*")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
447 (if (eq (char-before) ?/)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
448 (forward-char -1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
449 (skip-chars-backward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
450 (bolp)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
451 ;; shut this up
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
452 (let (c-echo-syntactic-information-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
453 (c-indent-line))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
454 ))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
455
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
456 (defun c-electric-semi&comma (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
457 "Insert a comma or semicolon.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
458 When the auto-newline feature is turned on, as evidenced by the \"/a\"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
459 or \"/ah\" string on the mode line, a newline might be inserted. See
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
460 the variable `c-hanging-semi&comma-criteria' for how newline insertion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
461 is determined.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
462
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
463 When semicolon is inserted, the line is re-indented unless a numeric
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
464 arg is supplied, point is inside a literal, or there are
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
465 non-whitespace characters on the line following the semicolon.
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
466
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
467 Based on the value of `c-cleanup-list', this function cleans up commas
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
468 following brace lists and semicolons following defuns."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
469 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
470 (let* ((lim (c-most-enclosing-brace (c-parse-state)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
471 (literal (c-in-literal lim))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
472 (here (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
473 ;; shut this up
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
474 (c-echo-syntactic-information-p nil))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
475 (if (or literal
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
476 arg
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
477 (not (looking-at "[ \t]*$")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
478 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
479 ;; do some special stuff with the character
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
480 (self-insert-command (prefix-numeric-value arg))
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
481 ;; do all cleanups and newline insertions if c-auto-newline is
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
482 ;; turned on
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
483 (if (not c-auto-newline)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
484 (c-indent-line)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
485 ;; clean ups
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
486 (let ((pos (- (point-max) (point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
487 (if (and (or (and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
488 (eq last-command-char ?,)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
489 (memq 'list-close-comma c-cleanup-list))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
490 (and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
491 (eq last-command-char ?\;)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
492 (memq 'defun-close-semi c-cleanup-list)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
493 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
494 (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
495 (skip-chars-backward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
496 (eq (char-before) ?}))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
497 ;; make sure matching open brace isn't in a comment
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
498 (not (c-in-literal lim)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
499 (delete-region (point) here))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
500 (goto-char (- (point-max) pos)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
501 ;; re-indent line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
502 (c-indent-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
503 ;; check to see if a newline should be added
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
504 (let ((criteria c-hanging-semi&comma-criteria)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
505 answer add-newline-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
506 (while criteria
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
507 (setq answer (funcall (car criteria)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
508 ;; only nil value means continue checking
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
509 (if (not answer)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
510 (setq criteria (cdr criteria))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
511 (setq criteria nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
512 ;; only 'stop specifically says do not add a newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
513 (setq add-newline-p (not (eq answer 'stop)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
514 ))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
515 (if add-newline-p
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
516 (progn (newline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
517 (c-indent-line)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
518 )))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
519
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
520 (defun c-electric-colon (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
521 "Insert a colon.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
522
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
523 If the auto-newline feature is turned on, as evidenced by the \"/a\"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
524 or \"/ah\" string on the mode line, newlines are inserted before and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
525 after colons based on the value of `c-hanging-colons-alist'.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
526
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
527 Also, the line is re-indented unless a numeric ARG is supplied, there
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
528 are non-whitespace characters present on the line after the colon, or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
529 the colon is inserted inside a literal.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
530
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
531 This function cleans up double colon scope operators based on the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
532 value of `c-cleanup-list'."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
533 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
534 (let* ((bod (c-point 'bod))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
535 (literal (c-in-literal bod))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
536 syntax newlines is-scope-op
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
537 ;; shut this up
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
538 (c-echo-syntactic-information-p nil))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
539 (if (or literal
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
540 arg
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
541 (not (looking-at "[ \t]*$")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
542 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
543 ;; insert the colon, then do any specified cleanups
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
544 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
545 (let ((pos (- (point-max) (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
546 (here (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
547 (if (and c-auto-newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
548 (memq 'scope-operator c-cleanup-list)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
549 (eq (char-before) ?:)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
550 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
551 (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
552 (skip-chars-backward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
553 (eq (char-before) ?:))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
554 (not (c-in-literal))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
555 (not (eq (char-after (- (point) 2)) ?:)))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
556 (progn
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
557 (delete-region (point) (1- here))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
558 (setq is-scope-op t)))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
559 (goto-char (- (point-max) pos)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
560 ;; lets do some special stuff with the colon character
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
561 (setq syntax (c-guess-basic-syntax)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
562 ;; some language elements can only be determined by
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
563 ;; checking the following line. Lets first look for ones
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
564 ;; that can be found when looking on the line with the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
565 ;; colon
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
566 newlines
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
567 (and c-auto-newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
568 (or (c-lookup-lists '(case-label label access-label)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
569 syntax c-hanging-colons-alist)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
570 (c-lookup-lists '(member-init-intro inher-intro)
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
571 (let ((buffer-undo-list t))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
572 (insert "\n")
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
573 (unwind-protect
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
574 (c-guess-basic-syntax)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
575 (delete-char -1)))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
576 c-hanging-colons-alist))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
577 ;; indent the current line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
578 (c-indent-line syntax)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
579 ;; does a newline go before the colon? Watch out for already
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
580 ;; non-hung colons. However, we don't unhang them because that
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
581 ;; would be a cleanup (and anti-social).
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
582 (if (and (memq 'before newlines)
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
583 (not is-scope-op)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
584 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
585 (skip-chars-backward ": \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
586 (not (bolp))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
587 (let ((pos (- (point-max) (point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
588 (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
589 (newline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
590 (c-indent-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
591 (goto-char (- (point-max) pos))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
592 ;; does a newline go after the colon?
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
593 (if (and (memq 'after (cdr-safe newlines))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
594 (not is-scope-op))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
595 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
596 (newline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
597 (c-indent-line)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
598 )))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
599
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
600 (defun c-electric-lt-gt (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
601 "Insert a less-than, or greater-than character.
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
602 The line will be re-indented if the character inserted is the second
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
603 of a C++ style stream operator and the buffer is in C++ mode.
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
604 Exceptions are when a numeric argument is supplied, or point is inside
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
605 a literal, in which case the line will not be re-indented."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
606 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
607 (let ((indentp (and (not arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
608 (eq (char-before) last-command-char)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
609 (not (c-in-literal))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
610 ;; shut this up
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
611 (c-echo-syntactic-information-p nil))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
612 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
613 (if indentp
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
614 (c-indent-line))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
615
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
616 (defun c-electric-paren (arg)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
617 "Insert a parenthesis.
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
618
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
619 If the auto-newline feature is turned on, as evidenced by the \"/a\"
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
620 or \"/ah\" string on the mode line, some newline cleanups are done if
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
621 appropriate; see the variable `c-cleanup-list'.
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
622
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
623 Also, the line is re-indented unless a numeric ARG is supplied, there
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
624 are non-whitespace characters present on the line after the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
625 parenthesis, or the parenthesis is inserted inside a literal."
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
626 (interactive "*P")
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
627 (let (;; shut this up
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
628 (c-echo-syntactic-information-p nil))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
629 (if (or arg
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
630 (not (looking-at "[ \t]*$"))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
631 (c-in-literal (c-point 'bod)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
632 (self-insert-command (prefix-numeric-value arg))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
633 ;; do some special stuff with the character
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
634 (let* (;; We want to inhibit blinking the paren since this will
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
635 ;; be most disruptive. We'll blink it ourselves
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
636 ;; afterwards.
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
637 (old-blink-paren blink-paren-function)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
638 blink-paren-function)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
639 (self-insert-command (prefix-numeric-value arg))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
640 (c-indent-line)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
641 (when c-auto-newline
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
642 ;; Do all appropriate clean ups
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
643 (let ((here (point))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
644 (pos (- (point-max) (point)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
645 mbeg mend)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
646 ;; clean up brace-elseif-brace
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
647 (if (and (memq 'brace-elseif-brace c-cleanup-list)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
648 (eq last-command-char ?\()
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
649 (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*("
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
650 nil t)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
651 (save-excursion
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
652 (setq mbeg (match-beginning 0)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
653 mend (match-end 0))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
654 (= mend here))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
655 (not (c-in-literal)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
656 (progn
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
657 (delete-region mbeg mend)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
658 (insert "} else if (")))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
659 ;; clean up brace-catch-brace
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
660 (if (and (memq 'brace-catch-brace c-cleanup-list)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
661 (eq last-command-char ?\()
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
662 (re-search-backward "}[ \t\n]*catch[ \t\n]*(" nil t)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
663 (save-excursion
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
664 (setq mbeg (match-beginning 0)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
665 mend (match-end 0))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
666 (= mend here))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
667 (not (c-in-literal)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
668 (progn
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
669 (delete-region mbeg mend)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
670 (insert "} catch (")))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
671 (goto-char (- (point-max) pos))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
672 ))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
673 (if old-blink-paren
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
674 (funcall old-blink-paren))))))
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
675
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
676
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
677
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
678 ;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
679 ;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
680 (defun c-forward-into-nomenclature (&optional arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
681 "Move forward to end of a nomenclature section or word.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
682 With arg, to it arg times."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
683 (interactive "p")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
684 (let ((case-fold-search nil))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
685 (if (> arg 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
686 (re-search-forward "\\W*\\([A-Z]*[a-z0-9]*\\)" (point-max) t arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
687 (while (and (< arg 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
688 (re-search-backward
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
689 "\\(\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\W\\w+\\)"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
690 (point-min) 0))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
691 (forward-char 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
692 (setq arg (1+ arg)))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
693 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
694
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
695 (defun c-backward-into-nomenclature (&optional arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
696 "Move backward to beginning of a nomenclature section or word.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
697 With optional ARG, move that many times. If ARG is negative, move
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
698 forward."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
699 (interactive "p")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
700 (c-forward-into-nomenclature (- arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
701 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
702
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
703 (defun c-scope-operator ()
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
704 "Insert a double colon scope operator at point.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
705 No indentation or other \"electric\" behavior is performed."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
706 (interactive "*")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
707 (insert "::"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
708
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
709 (defun c-beginning-of-defun (&optional arg)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
710 "Move backward to the beginning of a defun.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
711 With argument, do it that many times. Negative arg -N
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
712 means move forward to Nth following beginning of defun.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
713 Returns t unless search stops due to beginning or end of buffer.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
714
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
715 Unlike the built-in `beginning-of-defun' this tries to be smarter
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
716 about finding the char with open-parenthesis syntax that starts the
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
717 defun."
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
718 (interactive "p")
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
719 (unless arg (setq arg 1))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
720 (if (< arg 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
721 (c-end-of-defun (- arg))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
722 (while (> arg 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
723 (let ((state (nreverse (c-parse-state)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
724 prevbod bod)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
725 (while (and state (not bod))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
726 (setq bod (car state)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
727 state (cdr state))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
728 (if (consp bod)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
729 (setq prevbod (car bod)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
730 bod nil)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
731 (cond
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
732 (bod (goto-char bod))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
733 (prevbod (goto-char prevbod))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
734 (t (goto-char (point-min))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
735 (setq arg 0)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
736 (setq arg (1- arg))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
737 (c-keep-region-active)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
738 (= arg 0)))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
739
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
740 (defun c-end-of-defun (&optional arg)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
741 "Move forward to next end of defun. With argument, do it that many times.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
742 Negative argument -N means move back to Nth preceding end of defun.
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
743 Returns t unless search stops due to beginning or end of buffer.
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
744
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
745 An end of a defun occurs right after the close-parenthesis that matches
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
746 the open-parenthesis that starts a defun; see `beginning-of-defun'."
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
747 (interactive "p")
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
748 (if (not arg)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
749 (setq arg 1))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
750 (if (< arg 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
751 (c-beginning-of-defun (- arg))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
752 (while (> arg 0)
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
753 (let ((pos (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
754 eol)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
755 (while (and (c-safe (down-list 1) t)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
756 (not (eq (char-before) ?{)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
757 ;; skip down into the next defun-block
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
758 (forward-char -1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
759 (c-forward-sexp))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
760 (c-beginning-of-defun 1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
761 (setq eol (c-point 'eol))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
762 (c-forward-sexp)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
763 (if (< eol (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
764 ;; Don't move to next line for one line defuns.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
765 (forward-line 1))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
766 (when (<= (point) pos)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
767 (goto-char (point-max))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
768 (setq arg 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
769 (setq arg (1- arg))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
770 (c-keep-region-active)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
771 (= arg 0)))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
772
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
773
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
774 (defun c-beginning-of-statement (&optional count lim sentence-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
775 "Go to the beginning of the innermost C statement.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
776 With prefix arg, go back N - 1 statements. If already at the
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
777 beginning of a statement then go to the beginning of the closest
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
778 preceding one, moving into nested blocks if necessary (use
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
779 \\[backward-sexp] to skip over a block). If within a comment, or next
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
780 to a comment (only whitespace between), move by sentences instead of
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
781 statements.
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
782
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
783 When called from a program, this function takes 3 optional args: the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
784 repetition count, a buffer position limit which is the farthest back
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
785 to search, and a flag saying whether to do sentence motion when in a
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
786 comment."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
787 (interactive (list (prefix-numeric-value current-prefix-arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
788 nil t))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
789 (let* ((count (or count 1))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
790 here
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
791 (range (c-collect-line-comments (c-literal-limits lim))))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
792 (while (and (/= count 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
793 (or (not lim) (> (point) lim)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
794 (setq here (point))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
795 (if (and (not range) sentence-flag)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
796 (save-excursion
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
797 ;; Find the comment next to point if we're not in one.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
798 (if (> count 0)
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
799 (setq range (if (c-forward-comment -1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
800 (cons (point)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
801 (progn (c-forward-comment 1) (point)))))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
802 (skip-chars-forward " \t\n")
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
803 (setq range (point))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
804 (setq range (if (c-forward-comment 1)
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
805 (cons range (point))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
806 nil)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
807 (setq range (c-collect-line-comments range))))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
808 (if (and (< count 0) (= here (point-max)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
809 ;; Special case because eob might be in a literal.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
810 (setq range nil))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
811 (if range
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
812 (if (and sentence-flag
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
813 (/= (char-syntax (char-after (car range))) ?\"))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
814 (let* ((lit-type (c-literal-type range))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
815 (beg (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
816 (goto-char (car range))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
817 (looking-at (if (eq lit-type 'c)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
818 comment-start-skip
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
819 (concat "\\("
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
820 c-comment-prefix-regexp
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
821 "\\)[ \t]*")))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
822 (goto-char (match-end 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
823 (point)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
824 (end (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
825 (goto-char (- (cdr range)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
826 (if (eq lit-type 'c) 2 1)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
827 (point))))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
828 ;; move by sentence, but not past the limit of the literal
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
829 (save-restriction
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
830 (narrow-to-region beg end)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
831 (c-safe (forward-sentence (if (< count 0) 1 -1)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
832 (if (and (memq lit-type '(c c++))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
833 ;; Check if we stopped due to a comment
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
834 ;; prefix and not a sentence end.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
835 (/= (point) beg)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
836 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
837 (beginning-of-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
838 (looking-at (concat "[ \t]*\\("
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
839 c-comment-prefix-regexp
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
840 "\\)[ \t]*")))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
841 (>= (point) (match-beginning 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
842 (/= (match-beginning 1) (match-end 1))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
843 (or (< (point) (match-end 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
844 (and
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
845 (= (point) (match-end 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
846 ;; The comment prefix may contain
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
847 ;; characters that is regarded as end
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
848 ;; of sentence.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
849 (or (eolp)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
850 (and
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
851 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
852 (forward-paragraph -1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
853 (< (point) (match-beginning 0)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
854 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
855 (beginning-of-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
856 (or (not (re-search-backward
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
857 sentence-end
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
858 (c-point 'bopl)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
859 t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
860 (< (match-end 0)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
861 (c-point 'eol)))))))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
862 (setq count (+ count (if (< count 0) -1 1)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
863 (if (< count 0)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
864 (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
865 ;; In block comments, if there's only
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
866 ;; horizontal ws between the text and the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
867 ;; comment ender, stop before it. Stop after
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
868 ;; the ender if there's either nothing or
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
869 ;; newlines between.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
870 (when (and (eq lit-type 'c) (eq (point) end))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
871 (widen)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
872 (skip-chars-backward " \t")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
873 (when (or (eq (point) end) (bolp))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
874 (goto-char (cdr range)))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
875 (when (and (eq (point) beg) (looking-at "[ \t]*$"))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
876 ;; Stop before instead of after the comment
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
877 ;; starter if nothing follows it.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
878 (widen)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
879 (goto-char (car range))))))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
880 ;; See if we should escape the literal.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
881 (if (> count 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
882 (if (< (point) here)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
883 (setq count (1- count))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
884 (goto-char (car range))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
885 (setq range nil))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
886 (if (> (point) here)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
887 (setq count (1+ count))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
888 (goto-char (cdr range))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
889 (setq range nil))))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
890 (goto-char (if (> count 0) (car range) (cdr range)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
891 (setq range nil))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
892 ;; Below we do approximately the same as
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
893 ;; c-beginning-of-statement-1 and c-end-of-statement-1, and
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
894 ;; perhaps they should be changed, but that'd likely break a
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
895 ;; lot in cc-engine.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
896 (goto-char here)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
897 (if (> count 0)
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
898 (condition-case nil
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
899 ;; Stop before `{' and after `;', `{', `}' and `};'
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
900 ;; when not followed by `}' or `)', but on the other
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
901 ;; side of the syntactic ws. Move by sexps and move
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
902 ;; into parens. Also stop before `#' when it's first
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
903 ;; on a line.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
904 (let ((comment-pos (not sentence-flag))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
905 (large-enough (- (point-max)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
906 last last-below-line)
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
907 (catch 'done
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
908 (while t
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
909 (setq last (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
910 (when (and (looking-at "{\\|^#") (/= here last))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
911 (unless (and c-special-brace-lists
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
912 (eq (char-after) ?{)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
913 (c-looking-at-special-brace-list))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
914 (if (and (eq (char-after) ?#)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
915 (numberp last-below-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
916 (not (eq last-below-line here)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
917 (goto-char last-below-line))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
918 (throw 'done t)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
919 (if comment-pos
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
920 (c-forward-comment large-enough)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
921 (when (c-forward-comment -1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
922 ;; Record position of first comment.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
923 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
924 (c-forward-comment 1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
925 (setq comment-pos (point)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
926 (c-forward-comment large-enough)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
927 (unless last-below-line
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
928 (if (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
929 (re-search-forward "\\(^\\|[^\\]\\)$" last t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
930 (setq last-below-line last)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
931 (cond ((bobp) ; Must handle bob specially.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
932 (if (= here last)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
933 (throw 'done t)
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
934 (goto-char last)
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
935 (throw 'done t)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
936 ((progn (backward-char)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
937 (looking-at "[;{}]"))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
938 (if (and c-special-brace-lists
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
939 (eq (char-after) ?{)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
940 (c-looking-at-special-brace-list))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
941 (skip-syntax-backward "w_") ; Speedup only.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
942 (if (or (= here last)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
943 (memq (char-after last) '(?\) ?})))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
944 (if (and (eq (char-before) ?})
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
945 (eq (char-after) ?\;))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
946 (backward-char))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
947 (goto-char last)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
948 (throw 'done t))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
949 ((= (char-syntax (char-after)) ?\")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
950 (forward-char)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
951 (c-backward-sexp))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
952 (t (skip-syntax-backward "w_")) ; Speedup only.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
953 )))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
954 (if (and (numberp comment-pos)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
955 (< (point) comment-pos))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
956 ;; We jumped over a comment that should be investigated.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
957 (goto-char comment-pos)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
958 (setq count (1- count))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
959 (error
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
960 (goto-char (point-min))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
961 (setq count 0)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
962 (condition-case nil
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
963 ;; Stop before `{', `}', and `#' when it's first on a
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
964 ;; line, but on the other side of the syntactic ws, and
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
965 ;; after `;', `}' and `};'. Only stop before `{' if at
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
966 ;; top level or inside braces, though. Move by sexps
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
967 ;; and move into parens. Also stop at eol of lines
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
968 ;; starting with `#'.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
969 (let ((comment-pos (not sentence-flag))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
970 (large-enough (point-max))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
971 last)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
972 (catch 'done
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
973 (while t
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
974 (setq last (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
975 (if comment-pos
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
976 (c-forward-comment large-enough)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
977 (if (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
978 (skip-chars-forward " \t\n\r\f")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
979 ;; Record position of first comment.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
980 (setq comment-pos (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
981 (c-forward-comment 1))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
982 (c-forward-comment large-enough)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
983 (setq comment-pos nil)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
984 (cond ((and (eq (char-after) ?{)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
985 (not (and c-special-brace-lists
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
986 (c-looking-at-special-brace-list)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
987 (/= here last)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
988 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
989 (or (not (c-safe (up-list -1) t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
990 (= (char-after) ?{))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
991 (goto-char last)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
992 (throw 'done t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
993 ((and c-special-brace-lists
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
994 (eq (char-after) ?})
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
995 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
996 (and (c-safe (up-list -1) t)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
997 (c-looking-at-special-brace-list))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
998 (forward-char 1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
999 (skip-syntax-forward "w_")) ; Speedup only.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1000 ((and (eq (char-after) ?})
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1001 (/= here last))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1002 (goto-char last)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1003 (throw 'done t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1004 ((looking-at "^#")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1005 (if (= here last)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1006 (or (re-search-forward "\\(^\\|[^\\]\\)$" nil t)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1007 (goto-char (point-max)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1008 (goto-char last))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1009 (throw 'done t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1010 ((looking-at ";\\|};?")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1011 (goto-char (match-end 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1012 (throw 'done t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1013 ((= (char-syntax (char-after)) ?\")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1014 (c-forward-sexp))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1015 (t
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1016 (forward-char 1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1017 (skip-syntax-forward "w_")) ; Speedup only.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1018 )))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1019 (if (and (numberp comment-pos)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1020 (> (point) comment-pos))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1021 ;; We jumped over a comment that should be investigated.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1022 (goto-char comment-pos)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1023 (setq count (1+ count))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1024 (error
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1025 (goto-char (point-max))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1026 (setq count 0)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1027 ))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1028 ;; If we haven't moved we're near a buffer limit.
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1029 (when (and (not (zerop count)) (= (point) here))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1030 (goto-char (if (> count 0) (point-min) (point-max)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1031 (setq count 0)))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1032 ;; its possible we've been left up-buf of lim
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1033 (if lim (goto-char (max (point) lim))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1034 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1035
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1036 (defun c-end-of-statement (&optional count lim sentence-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1037 "Go to the end of the innermost C statement.
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1038 With prefix arg, go forward N - 1 statements. Move forward to the end
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1039 of the next statement if already at end, and move into nested blocks
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1040 \(use \\[forward-sexp] to skip over a block). If within a comment, or
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1041 next to a comment (only whitespace between), move by sentences instead
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1042 of statements.
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1043
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1044 When called from a program, this function takes 3 optional args: the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1045 repetition count, a buffer position limit which is the farthest back
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1046 to search, and a flag saying whether to do sentence motion when in a
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1047 comment."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1048 (interactive (list (prefix-numeric-value current-prefix-arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1049 nil t))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1050 (c-beginning-of-statement (- (or count 1)) lim sentence-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1051 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1052
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1053
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1054 ;; set up electric character functions to work with pending-del,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1055 ;; (a.k.a. delsel) mode. All symbols get the t value except
18843
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
1056 ;; the functions which delete, which gets 'supersede.
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1057 (mapcar
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1058 (function
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1059 (lambda (sym)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1060 (put sym 'delete-selection t) ; for delsel (Emacs)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1061 (put sym 'pending-delete t))) ; for pending-del (XEmacs)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1062 '(c-electric-pound
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1063 c-electric-brace
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1064 c-electric-slash
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1065 c-electric-star
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1066 c-electric-semi&comma
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1067 c-electric-lt-gt
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1068 c-electric-colon
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1069 c-electric-paren))
18843
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
1070 (put 'c-electric-delete 'delete-selection 'supersede) ; delsel
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
1071 (put 'c-electric-delete 'pending-delete 'supersede) ; pending-del
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
1072 (put 'c-electric-backspace 'delete-selection 'supersede) ; delsel
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
1073 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1074
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1075
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1076 ;; This is used by indent-for-comment to decide how much to indent a
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1077 ;; comment in C code based on its context.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1078 (defun c-comment-indent ()
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1079 (if (looking-at (concat "^\\(" c-comment-start-regexp "\\)"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1080 0 ;Existing comment at bol stays there.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1081 (let ((opoint (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1082 placeholder)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1083 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1084 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1085 (cond
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1086 ;; CASE 1: A comment following a solitary close-brace should
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1087 ;; have only one space.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1088 ((looking-at (concat "[ \t]*}[ \t]*\\($\\|"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1089 c-comment-start-regexp
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1090 "\\)"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1091 (search-forward "}")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1092 (1+ (current-column)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1093 ;; CASE 2: 2 spaces after #endif
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1094 ((or (looking-at "^#[ \t]*endif[ \t]*")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1095 (looking-at "^#[ \t]*else[ \t]*"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1096 7)
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1097 ;; CASE 3: when c-indent-comments-syntactically-p is t,
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1098 ;; calculate the offset according to c-offsets-alist.
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1099 ;; E.g. identical to hitting TAB.
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1100 ((and c-indent-comments-syntactically-p
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1101 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1102 (skip-chars-forward " \t")
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1103 (or (looking-at c-comment-start-regexp)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1104 (eolp))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1105 (let ((syntax (c-guess-basic-syntax)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1106 ;; BOGOSITY ALERT: if we're looking at the eol, its
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1107 ;; because indent-for-comment hasn't put the comment-start
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1108 ;; in the buffer yet. this will screw up the syntactic
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1109 ;; analysis so we kludge in the necessary info. Another
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1110 ;; kludge is that if we're at the bol, then we really want
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1111 ;; to ignore any anchoring as specified by
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1112 ;; c-comment-only-line-offset since it doesn't apply here.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1113 (if (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1114 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1115 (skip-chars-forward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1116 (eolp))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1117 (c-add-syntax 'comment-intro))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1118 (let ((c-comment-only-line-offset
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1119 (if (consp c-comment-only-line-offset)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1120 c-comment-only-line-offset
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1121 (cons c-comment-only-line-offset
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1122 c-comment-only-line-offset))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1123 (apply '+ (mapcar 'c-get-offset syntax)))))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1124 ;; CASE 4: If previous line is a comment-only line, use its
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1125 ;; indentation if it's greater than comment-column. Leave at
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1126 ;; least one space between the comment and the last nonblank
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1127 ;; character in any case.
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1128 ((save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1129 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1130 (and (not (bobp))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1131 (forward-line -1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1132 (skip-chars-forward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1133 (prog1
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1134 (looking-at c-comment-start-regexp)
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1135 (setq placeholder (current-column))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1136 (goto-char opoint)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1137 (skip-chars-backward " \t")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1138 (max (if (bolp) 0 (1+ (current-column)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1139 placeholder
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1140 comment-column))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1141 ;; CASE 5: If comment-column is 0, and nothing but space
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1142 ;; before the comment, align it at 0 rather than 1.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1143 ((progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1144 (goto-char opoint)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1145 (skip-chars-backward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1146 (and (= comment-column 0) (bolp)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1147 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1148 ;; CASE 6: indent at comment column except leave at least one
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1149 ;; space.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1150 (t (max (1+ (current-column))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1151 comment-column))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1152 )))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1153
19804
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
1154
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1155 ;; used by outline-minor-mode
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1156 (defun c-outline-level ()
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1157 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1158 (skip-chars-forward "\t ")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1159 (current-column)))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1160
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1161
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1162 (defun c-up-conditional (count)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1163 "Move back to the containing preprocessor conditional, leaving mark behind.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1164 A prefix argument acts as a repeat count. With a negative argument,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1165 move forward to the end of the containing preprocessor conditional.
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1166
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1167 `#elif' is treated like `#else' followed by `#if', so the function
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1168 stops at them when going backward, but not when going forward."
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1169 (interactive "p")
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1170 (c-forward-conditional (- count) -1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1171 (c-keep-region-active))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1172
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1173 (defun c-up-conditional-with-else (count)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1174 "Move back to the containing preprocessor conditional, including `#else'.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1175 Just like `c-up-conditional', except it also stops at `#else'
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1176 directives."
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1177 (interactive "p")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1178 (c-forward-conditional (- count) -1 t)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1179 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1180
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1181 (defun c-down-conditional (count)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1182 "Move forward into the next preprocessor conditional, leaving mark behind.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1183 A prefix argument acts as a repeat count. With a negative argument,
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1184 move backward into the previous preprocessor conditional.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1185
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1186 `#elif' is treated like `#else' followed by `#if', so the function
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1187 stops at them when going forward, but not when going backward."
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1188 (interactive "p")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1189 (c-forward-conditional count 1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1190 (c-keep-region-active))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1191
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1192 (defun c-down-conditional-with-else (count)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1193 "Move forward into the next preprocessor conditional, including `#else'.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1194 Just like `c-down-conditional', except it also stops at `#else'
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1195 directives."
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1196 (interactive "p")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1197 (c-forward-conditional count 1 t)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1198 (c-keep-region-active))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1199
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1200 (defun c-backward-conditional (count &optional target-depth with-else)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1201 "Move back across a preprocessor conditional, leaving mark behind.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1202 A prefix argument acts as a repeat count. With a negative argument,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1203 move forward across a preprocessor conditional."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1204 (interactive "p")
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1205 (c-forward-conditional (- count) target-depth with-else)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1206 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1207
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1208 (defun c-forward-conditional (count &optional target-depth with-else)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1209 "Move forward across a preprocessor conditional, leaving mark behind.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1210 A prefix argument acts as a repeat count. With a negative argument,
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1211 move backward across a preprocessor conditional.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1212
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1213 `#elif' is treated like `#else' followed by `#if', except that the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1214 nesting level isn't changed when tracking subconditionals.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1215
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1216 The optional argument TARGET-DEPTH specifies the wanted nesting depth
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1217 after each scan. I.e. if TARGET-DEPTH is -1, the function will move
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1218 out of the enclosing conditional. A non-integer non-nil TARGET-DEPTH
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1219 counts as -1.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1220
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1221 If the optional argument WITH-ELSE is non-nil, `#else' directives are
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1222 treated as conditional clause limits. Normally they are ignored."
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1223 (interactive "p")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1224 (let* ((forward (> count 0))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1225 (increment (if forward -1 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1226 (search-function (if forward 're-search-forward 're-search-backward))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1227 (new))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1228 (unless (integerp target-depth)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1229 (setq target-depth (if target-depth -1 0)))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1230 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1231 (while (/= count 0)
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1232 (let ((depth 0)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1233 ;; subdepth is the depth in "uninteresting" subtrees,
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1234 ;; i.e. those that takes us farther from the target
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1235 ;; depth instead of closer.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1236 (subdepth 0)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1237 found)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1238 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1239 ;; Find the "next" significant line in the proper direction.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1240 (while (and (not found)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1241 ;; Rather than searching for a # sign that
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1242 ;; comes at the beginning of a line aside from
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1243 ;; whitespace, search first for a string
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1244 ;; starting with # sign. Then verify what
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1245 ;; precedes it. This is faster on account of
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1246 ;; the fastmap feature of the regexp matcher.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1247 (funcall search-function
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1248 "#[ \t]*\\(if\\|elif\\|endif\\|else\\)"
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1249 nil t))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1250 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1251 ;; Now verify it is really a preproc line.
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1252 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\|else\\)")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1253 (let (dchange (directive (match-string 1)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1254 (cond ((string= directive "if")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1255 (setq dchange (- increment)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1256 ((string= directive "endif")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1257 (setq dchange increment))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1258 ((= subdepth 0)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1259 ;; When we're not in an "uninteresting"
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1260 ;; subtree, we might want to act on "elif"
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1261 ;; and "else" too.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1262 (if (cond (with-else
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1263 ;; Always move toward the target depth.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1264 (setq dchange
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1265 (if (> target-depth 0) 1 -1)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1266 ((string= directive "elif")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1267 (setq dchange (- increment))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1268 ;; Ignore the change if it'd take us
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1269 ;; into an "uninteresting" subtree.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1270 (if (eq (> dchange 0) (<= target-depth 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1271 (setq dchange nil)))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1272 (when dchange
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1273 (when (or (/= subdepth 0)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1274 (eq (> dchange 0) (<= target-depth 0)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1275 (setq subdepth (+ subdepth dchange)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1276 (setq depth (+ depth dchange))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1277 ;; If we are trying to move across, and we find an
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1278 ;; end before we find a beginning, get an error.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1279 (if (and (< depth target-depth) (< dchange 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1280 (error (if forward
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1281 "No following conditional at this level"
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1282 "No previous conditional at this level"))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1283 ;; When searching forward, start from next line so
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1284 ;; that we don't find the same line again.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1285 (if forward (forward-line 1))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1286 ;; We found something if we've arrived at the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1287 ;; target depth.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1288 (if (and dchange (= depth target-depth))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1289 (setq found (point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1290 ;; else
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1291 (if forward (forward-line 1)))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1292 (or found
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1293 (error "No containing preprocessor conditional"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1294 (goto-char (setq new found)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1295 (setq count (+ count increment))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1296 (push-mark)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1297 (goto-char new))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1298 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1299
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1300
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1301 ;; commands to indent lines, regions, defuns, and expressions
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1302 (defun c-indent-command (&optional whole-exp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1303 "Indent current line as C code, and/or insert some whitespace.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1304
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1305 If `c-tab-always-indent' is t, always just indent the current line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1306 If nil, indent the current line only if point is at the left margin or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1307 in the line's indentation; otherwise insert some whitespace[*]. If
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1308 other than nil or t, then some whitespace[*] is inserted only within
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1309 literals (comments and strings) and inside preprocessor directives,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1310 but the line is always reindented.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1311
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1312 A numeric argument, regardless of its value, means indent rigidly all
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1313 the lines of the expression starting after point so that this line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1314 becomes properly indented. The relative indentation among the lines
24529
2986e6d0e927 (c-indent-command): Doc fix.
Karl Heuer <kwzh@gnu.org>
parents: 24447
diff changeset
1315 of the expression is preserved.
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1316
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1317 [*] The amount and kind of whitespace inserted is controlled by the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1318 variable `c-insert-tab-function', which is called to do the actual
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1319 insertion of whitespace. Normally the function in this variable
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1320 just inserts a tab character, or the equivalent number of spaces,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1321 depending on the variable `indent-tabs-mode'."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1322
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1323 (interactive "P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1324 (let ((bod (c-point 'bod)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1325 (if whole-exp
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1326 ;; If arg, always indent this line as C
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1327 ;; and shift remaining lines of expression the same amount.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1328 (let ((shift-amt (c-indent-line))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1329 beg end)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1330 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1331 (if (eq c-tab-always-indent t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1332 (beginning-of-line))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1333 (setq beg (point))
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1334 (c-forward-sexp 1)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1335 (setq end (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1336 (goto-char beg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1337 (forward-line 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1338 (setq beg (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1339 (if (> end beg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1340 (indent-code-rigidly beg end (- shift-amt) "#")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1341 ;; No arg supplied, use c-tab-always-indent to determine
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1342 ;; behavior
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1343 (cond
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1344 ;; CASE 1: indent when at column zero or in lines indentation,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1345 ;; otherwise insert a tab
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1346 ((not c-tab-always-indent)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1347 (if (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1348 (skip-chars-backward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1349 (not (bolp)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1350 (funcall c-insert-tab-function)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1351 (c-indent-line)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1352 ;; CASE 2: just indent the line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1353 ((eq c-tab-always-indent t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1354 (c-indent-line))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1355 ;; CASE 3: if in a literal, insert a tab, but always indent the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1356 ;; line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1357 (t
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1358 (if (c-in-literal bod)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1359 (funcall c-insert-tab-function))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1360 (c-indent-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1361 )))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1362
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1363 (defun c-indent-exp (&optional shutup-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1364 "Indent each line in balanced expression following point.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1365 Optional SHUTUP-P if non-nil, inhibits message printing and error checking."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1366 (interactive "*P")
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1367 (let ((here (point-marker))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1368 end progress-p)
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1369 (set-marker-insertion-type here t)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1370 (unwind-protect
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1371 (let ((c-echo-syntactic-information-p nil) ;keep quiet for speed
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1372 (start (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1373 ;; try to be smarter about finding the range of
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1374 ;; lines to indent. skip all following
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1375 ;; whitespace. failing that, try to find any
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1376 ;; opening brace on the current line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1377 (skip-chars-forward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1378 (if (memq (char-after) '(?\( ?\[ ?\{))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1379 (point)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1380 (let ((state (parse-partial-sexp (point)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1381 (c-point 'eol))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1382 (and (nth 1 state)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1383 (goto-char (nth 1 state))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1384 (memq (char-after) '(?\( ?\[ ?\{))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1385 (point)))))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1386 ;; find balanced expression end
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1387 (setq end (and (c-safe (progn (c-forward-sexp 1) t))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1388 (point-marker)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1389 ;; sanity check
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1390 (and (not start)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1391 (not shutup-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1392 (error "Cannot find start of balanced expression to indent."))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1393 (and (not end)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1394 (not shutup-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1395 (error "Cannot find end of balanced expression to indent."))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1396 (c-progress-init start end 'c-indent-exp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1397 (setq progress-p t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1398 (goto-char start)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1399 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1400 (while (< (point) end)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1401 (if (not (looking-at "[ \t]*$"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1402 (c-indent-line))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1403 (c-progress-update)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1404 (forward-line 1)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1405 ;; make sure marker is deleted
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1406 (and end
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1407 (set-marker end nil))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1408 (and progress-p
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1409 (c-progress-fini 'c-indent-exp))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1410 (goto-char here)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1411 (set-marker here nil))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1412
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1413 (defun c-indent-defun ()
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1414 "Re-indents the current top-level function def, struct or class declaration."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1415 (interactive "*")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1416 (let ((here (point-marker))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1417 (c-echo-syntactic-information-p nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1418 (brace (c-least-enclosing-brace (c-parse-state))))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1419 (goto-char (or brace (c-point 'bod)))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1420 ;; if we're sitting at b-o-b, it might be because there was no
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1421 ;; least enclosing brace and we were sitting on the defun's open
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1422 ;; brace.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1423 (if (and (bobp) (not (eq (char-after) ?\{)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1424 (goto-char here))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1425 ;; if defun-prompt-regexp is non-nil, b-o-d might not leave us at
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1426 ;; the open brace. I consider this an Emacs bug.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1427 (and (boundp 'defun-prompt-regexp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1428 defun-prompt-regexp
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1429 (looking-at defun-prompt-regexp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1430 (goto-char (match-end 0)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1431 ;; catch all errors in c-indent-exp so we can 1. give more
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1432 ;; meaningful error message, and 2. restore point
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1433 (unwind-protect
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1434 (c-indent-exp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1435 (goto-char here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1436 (set-marker here nil))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1437
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1438 (defun c-indent-region (start end)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1439 ;; Indent every line whose first char is between START and END inclusive.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1440 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1441 (goto-char start)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1442 ;; Advance to first nonblank line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1443 (skip-chars-forward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1444 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1445 (let (endmark)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1446 (unwind-protect
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1447 (let ((c-tab-always-indent t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1448 ;; shut up any echo msgs on indiv lines
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1449 (c-echo-syntactic-information-p nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1450 fence)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1451 (c-progress-init start end 'c-indent-region)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1452 (setq endmark (copy-marker end))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1453 (while (and (bolp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1454 (not (eobp))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1455 (< (point) endmark))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1456 ;; update progress
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1457 (c-progress-update)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1458 ;; Indent one line as with TAB.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1459 (let (nextline sexpend sexpbeg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1460 ;; skip blank lines
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1461 (skip-chars-forward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1462 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1463 ;; indent the current line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1464 (c-indent-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1465 (setq fence (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1466 (if (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1467 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1468 (looking-at "[ \t]*#"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1469 (forward-line 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1470 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1471 ;; Find beginning of following line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1472 (setq nextline (c-point 'bonl))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1473 ;; Find first beginning-of-sexp for sexp extending past
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1474 ;; this line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1475 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1476 (while (< (point) nextline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1477 (condition-case nil
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1478 (progn
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1479 (c-forward-sexp 1)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1480 (setq sexpend (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1481 (error (setq sexpend nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1482 (goto-char nextline)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1483 (c-forward-syntactic-ws))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1484 (if sexpend
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1485 (progn
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1486 ;; make sure the sexp we found really starts on the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1487 ;; current line and extends past it
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1488 (goto-char sexpend)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1489 (setq sexpend (point-marker))
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1490 (c-safe (c-backward-sexp 1))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1491 (setq sexpbeg (point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1492 (if (and sexpbeg (< sexpbeg fence))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1493 (setq sexpbeg fence)))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1494 ;; Since we move by sexps we might have missed
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1495 ;; comment-only lines.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1496 (if sexpbeg
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1497 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1498 (while (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1499 (forward-line 1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1500 (skip-chars-forward " \t")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1501 (< (point) sexpbeg))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1502 (if (looking-at c-comment-start-regexp)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1503 (setq sexpbeg (c-point 'bol))))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1504 ;; If that sexp ends within the region, indent it all at
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1505 ;; once, fast.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1506 (condition-case nil
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1507 (if (and sexpend
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1508 (> sexpend nextline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1509 (<= sexpend endmark))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1510 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1511 (goto-char sexpbeg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1512 (c-indent-exp 'shutup)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1513 (c-progress-update)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1514 (goto-char sexpend)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1515 (error
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1516 (goto-char sexpbeg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1517 (c-indent-line)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1518 ;; Move to following line and try again.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1519 (and sexpend
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1520 (markerp sexpend)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1521 (set-marker sexpend nil))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1522 (forward-line 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1523 (setq fence (point))))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1524 (set-marker endmark nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1525 (c-progress-fini 'c-indent-region)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1526 (c-echo-parsing-error)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1527 ))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1528
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1529 (defun c-mark-function ()
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1530 "Put mark at end of current top-level defun, point at beginning."
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1531 (interactive)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1532 (let ((here (point))
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1533 (eod (c-point 'eod))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1534 (state (c-parse-state)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1535 ;; Are we sitting at the top level, someplace between either the
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1536 ;; beginning of buffer, or the nearest preceding defun? If so,
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1537 ;; try first to figure out whether we're sitting on the
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1538 ;; introduction to a top-level defun, in which case we want to
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1539 ;; mark the entire defun we're sitting on.
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1540 ;;
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1541 ;; If we're sitting on anything else at the top-level, we want to
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1542 ;; just mark the statement that we're on
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1543 (if (or (and (consp (car state))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1544 (= (length state) 1))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1545 (null state))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1546 ;; Are we in the whitespace after the nearest preceding defun?
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1547 (if (and state
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1548 (looking-at "[ \t]*$")
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1549 (= (save-excursion
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1550 (c-backward-syntactic-ws)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1551 (skip-chars-backward ";")
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1552 (point))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1553 (cdar state)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1554 (progn
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1555 (setq eod (point))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1556 (goto-char (caar state))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1557 (c-beginning-of-statement-1))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1558 (if (= ?{ (save-excursion
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1559 (c-end-of-statement-1)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1560 (char-before)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1561 ;; We must be in a defuns's introduction
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1562 (progn
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1563 (c-end-of-statement-1)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1564 (skip-chars-backward "{")
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1565 (c-beginning-of-statement-1)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1566 (c-forward-syntactic-ws))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1567 ;; Just mark the statement
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1568 (c-end-of-statement-1)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1569 (forward-line 1)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1570 (setq eod (point))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1571 (c-beginning-of-statement-1)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1572 ;; We are inside some enclosing brace structure, so we first
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1573 ;; need to find our way to the least enclosing brace. Then, in
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1574 ;; both cases, we to mark the region from the beginning of the
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1575 ;; current statement, until the end of the next following defun
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1576 (while (and state)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1577 (or (consp (car state))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1578 (goto-char (car state)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1579 (setq state (cdr state)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1580 (c-beginning-of-statement-1))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1581 (push-mark here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1582 (push-mark eod nil t)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1583
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1584 (defun c-indent-line-or-region ()
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1585 "When the region is active, indent it. Otherwise indent the current line."
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1586 ;; Emacs has a variable called mark-active, XEmacs uses region-active-p
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1587 (interactive)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1588 (if (c-region-is-active-p)
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1589 (c-indent-region (region-beginning) (region-end))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1590 (c-indent-command)))
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1591
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1592
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1593 ;; for progress reporting
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1594 (defvar c-progress-info nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1595
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1596 (defun c-progress-init (start end context)
20143
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1597 (cond
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1598 ;; Be silent
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1599 ((not c-progress-interval))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1600 ;; Start the progress update messages. If this Emacs doesn't have
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1601 ;; a built-in timer, just be dumb about it.
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1602 ((not (fboundp 'current-time))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1603 (message "indenting region... (this may take a while)"))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1604 ;; If progress has already been initialized, do nothing. otherwise
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1605 ;; initialize the counter with a vector of:
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1606 ;; [start end lastsec context]
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1607 (c-progress-info)
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1608 (t (setq c-progress-info (vector start
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1609 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1610 (goto-char end)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1611 (point-marker))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1612 (nth 1 (current-time))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1613 context))
20143
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1614 (message "indenting region..."))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1615 ))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1616
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1617 (defun c-progress-update ()
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1618 ;; update progress
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1619 (if (not (and c-progress-info c-progress-interval))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1620 nil
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1621 (let ((now (nth 1 (current-time)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1622 (start (aref c-progress-info 0))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1623 (end (aref c-progress-info 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1624 (lastsecs (aref c-progress-info 2)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1625 ;; should we update? currently, update happens every 2 seconds,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1626 ;; what's the right value?
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1627 (if (< c-progress-interval (- now lastsecs))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1628 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1629 (message "indenting region... (%d%% complete)"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1630 (/ (* 100 (- (point) start)) (- end start)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1631 (aset c-progress-info 2 now)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1632 )))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1633
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1634 (defun c-progress-fini (context)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1635 ;; finished
20143
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1636 (if (not c-progress-interval)
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1637 nil
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1638 (if (or (eq context (aref c-progress-info 3))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1639 (eq context t))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1640 (progn
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1641 (set-marker (aref c-progress-info 1) nil)
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1642 (setq c-progress-info nil)
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1643 (message "indenting region...done")))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1644
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1645
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1646
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1647 ;;; This page handles insertion and removal of backslashes for C macros.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1648
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1649 (defun c-backslash-region (from to delete-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1650 "Insert, align, or delete end-of-line backslashes on the lines in the region.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1651 With no argument, inserts backslashes and aligns existing backslashes.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1652 With an argument, deletes the backslashes.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1653
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1654 This function does not modify blank lines at the start of the region.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1655 If the region ends at the start of a line, it always deletes the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1656 backslash (if any) at the end of the previous line.
24282
5b0864259a4b Installed CC Mode 5.25.
Barry A. Warsaw <barry@zope.org>
parents: 21107
diff changeset
1657
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1658 You can put the region around an entire macro definition and use this
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1659 command to conveniently insert and align the necessary backslashes."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1660 (interactive "*r\nP")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1661 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1662 (goto-char from)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1663 (let ((column c-backslash-column)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1664 (endmark (make-marker)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1665 (move-marker endmark to)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1666 ;; Compute the smallest column number past the ends of all the lines.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1667 (if (not delete-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1668 (while (< (point) to)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1669 (end-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1670 (if (eq (char-before) ?\\)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1671 (progn (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1672 (skip-chars-backward " \t")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1673 (setq column (max column (1+ (current-column))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1674 (forward-line 1)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1675 ;; Adjust upward to a tab column, if that doesn't push past the margin.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1676 (if (> (% column tab-width) 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1677 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1678 (if (< adjusted (window-width))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1679 (setq column adjusted))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1680 ;; Don't modify blank lines at start of region.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1681 (goto-char from)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1682 (while (and (< (point) endmark) (eolp))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1683 (forward-line 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1684 ;; Add or remove backslashes on all the lines.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1685 (while (< (point) endmark)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1686 (if (and (not delete-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1687 ;; Un-backslashify the last line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1688 ;; if the region ends right at the start of the next line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1689 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1690 (forward-line 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1691 (< (point) endmark)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1692 (c-append-backslash column)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1693 (c-delete-backslash))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1694 (forward-line 1))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1695 (move-marker endmark nil))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1696
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1697 (defun c-append-backslash (column)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1698 (end-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1699 (if (eq (char-before) ?\\)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1700 (progn (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1701 (delete-horizontal-space)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1702 (indent-to column))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1703 (indent-to column)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1704 (insert "\\")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1705
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1706 (defun c-delete-backslash ()
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1707 (end-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1708 (or (bolp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1709 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1710 (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1711 (if (looking-at "\\\\")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1712 (delete-region (1+ (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1713 (progn (skip-chars-backward " \t") (point)))))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1714
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1715
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1716
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1717 ;;; Line breaking and paragraph filling.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1718
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1719 ;; The filling code is based on a simple theory; leave the intricacies
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1720 ;; of the text handling to the currently active mode for that
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1721 ;; (e.g. adaptive-fill-mode or filladapt-mode) and do as little as
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1722 ;; possible to make them work correctly wrt the comment and string
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1723 ;; separators, one-line paragraphs etc. Unfortunately, when it comes
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1724 ;; to it, there's quite a lot of special cases to handle which makes
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1725 ;; the code anything but simple. The intention is that it will work
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1726 ;; with any well-written text filling package that preserves a fill
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1727 ;; prefix.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1728 ;;
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1729 ;; We temporarily mask comment starters and enders as necessary for
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1730 ;; the filling code to do its job on a seemingly normal text block.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1731 ;; We do _not_ mask the fill prefix, so it's up to the filling code to
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1732 ;; preserve it correctly (especially important when filling C++ style
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1733 ;; line comments). By default, we set up and use adaptive-fill-mode,
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1734 ;; which is standard in all supported Emacs flavors.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1735
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1736 (defun c-guess-fill-prefix (lit-limits lit-type)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1737 ;; Determine the appropriate comment fill prefix for a block or line
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1738 ;; comment. Return a cons of the prefix string and the column where
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1739 ;; it ends. If fill-prefix is set, it'll override. Note that this
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1740 ;; function also uses the value of point in some heuristics.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1741 (let* ((here (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1742 (prefix-regexp (concat "[ \t]*\\("
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1743 c-comment-prefix-regexp
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1744 "\\)[ \t]*"))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1745 (comment-start-regexp (if (eq lit-type 'c++)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1746 prefix-regexp
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1747 comment-start-skip))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1748 prefix-line comment-prefix res)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1749 (cond
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1750 (fill-prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1751 (setq res (cons fill-prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1752 ;; Ugly way of getting the column after the fill
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1753 ;; prefix; it'd be nice with a current-column
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1754 ;; that works on strings..
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1755 (let ((buffer-modified (buffer-modified-p))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1756 (buffer-undo-list t)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1757 (start (point)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1758 (unwind-protect
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1759 (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1760 (insert ?\n fill-prefix)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1761 (current-column))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1762 (delete-region start (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1763 (set-buffer-modified-p buffer-modified))))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1764 ((eq lit-type 'c++)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1765 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1766 ;; Set fallback for comment-prefix if none is found.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1767 (setq comment-prefix "// ")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1768 (beginning-of-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1769 (if (> (point) (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1770 ;; The current line is not the comment starter, so the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1771 ;; comment has more than one line, and it can therefore be
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1772 ;; used to find the comment fill prefix.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1773 (setq prefix-line (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1774 (goto-char (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1775 (if (and (= (forward-line 1) 0)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1776 (< (point) (cdr lit-limits)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1777 ;; The line after the comment starter is inside the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1778 ;; comment, so we can use it.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1779 (setq prefix-line (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1780 ;; The comment is only one line. Take the comment prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1781 ;; from it and keep the indentation.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1782 (goto-char (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1783 (if (looking-at prefix-regexp)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1784 (goto-char (match-end 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1785 (forward-char 2)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1786 (skip-chars-forward " \t"))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1787 (setq res
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1788 (if (eq (c-point 'boi) (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1789 ;; There is only whitespace before the comment
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1790 ;; starter; take the prefix straight from this
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1791 ;; line.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1792 (cons (buffer-substring-no-properties
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1793 (c-point 'bol) (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1794 (current-column))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1795 ;; There is code before the comment starter, so we
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1796 ;; have to temporarily insert and indent a new
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1797 ;; line to get the right space/tab mix in the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1798 ;; indentation.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1799 (let ((buffer-modified (buffer-modified-p))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1800 (buffer-undo-list t)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1801 (prefix-len (- (point) (car lit-limits)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1802 tmp)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1803 (unwind-protect
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1804 (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1805 (goto-char (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1806 (indent-to (prog1 (current-column)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1807 (insert ?\n)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1808 (setq tmp (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1809 (forward-char prefix-len)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1810 (cons (buffer-substring-no-properties
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1811 (c-point 'bol) (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1812 (current-column)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1813 (delete-region (car lit-limits) tmp)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1814 (set-buffer-modified-p buffer-modified))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1815 )))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1816 (t
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1817 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1818 (beginning-of-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1819 (if (and (> (point) (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1820 (not (and (looking-at "[ \t]*\\*/")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1821 (eq (cdr lit-limits) (match-end 0)))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1822 ;; The current line is not the comment starter and
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1823 ;; contains more than just the ender, so it's good enough
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1824 ;; to be used for the comment fill prefix.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1825 (setq prefix-line (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1826 (goto-char (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1827 (if (or (/= (forward-line 1) 0)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1828 (>= (point) (cdr lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1829 (and (looking-at "[ \t]*\\*/")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1830 (eq (cdr lit-limits) (match-end 0)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1831 (and (looking-at prefix-regexp)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1832 (<= (1- (cdr lit-limits)) (match-end 0)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1833 (and (< here (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1834 (or (not (match-beginning 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1835 (looking-at "[ \t]*$"))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1836 ;; The comment is either one line or the next line
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1837 ;; contains just the comment ender. Also, if point is
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1838 ;; on the comment opener line and the following line is
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1839 ;; empty or doesn't match c-comment-prefix-regexp we
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1840 ;; assume that this is in fact a not yet closed one line
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1841 ;; comment, so we shouldn't look for the comment prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1842 ;; on the next line. In these cases we have no
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1843 ;; information about a suitable comment prefix, so we
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1844 ;; resort to c-block-comment-prefix.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1845 (setq comment-prefix (or c-block-comment-prefix "")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1846 res (let ((buffer-modified (buffer-modified-p))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1847 (buffer-undo-list t)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1848 tmp-pre tmp-post)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1849 ;; The comment doesn't give any information
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1850 ;; about the indentation column. We'll have to
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1851 ;; temporarily insert a new comment line and
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1852 ;; indent it to find the correct column.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1853 (unwind-protect
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1854 (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1855 (goto-char (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1856 (if (looking-at comment-start-regexp)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1857 (goto-char (match-end 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1858 (forward-char 2)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1859 (skip-chars-forward " \t"))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1860 (when (eq (char-syntax (char-before)) ?\ )
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1861 ;; If there's ws on the current
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1862 ;; line, we'll use it instead of
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1863 ;; what's ending comment-prefix.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1864 (setq comment-prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1865 (concat (substring comment-prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1866 0 (string-match
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1867 "\\s *\\'"
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1868 comment-prefix))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1869 (buffer-substring-no-properties
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1870 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1871 (skip-chars-backward " \t")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1872 (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1873 (point)))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1874 (setq tmp-pre (point-marker))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1875 ;; We insert an extra non-whitespace
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1876 ;; character before the line break and
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1877 ;; after comment-prefix in case it's
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1878 ;; "" or ends with whitespace.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1879 (insert "x\n" comment-prefix ?x)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1880 (setq tmp-post (point-marker))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1881 (c-indent-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1882 (goto-char (1- tmp-post))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1883 (cons (buffer-substring-no-properties
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1884 (c-point 'bol) (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1885 (current-column)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1886 (when tmp-post
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1887 (delete-region tmp-pre tmp-post)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1888 (set-marker tmp-pre nil)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1889 (set-marker tmp-post nil))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1890 (set-buffer-modified-p buffer-modified))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1891 ;; Otherwise the line after the comment starter is good
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1892 ;; enough to find the prefix in.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1893 (setq prefix-line (point)))))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1894 (or res
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1895 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1896 ;; prefix-line is the bol of a line on which we should try
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1897 ;; to find the prefix.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1898 (let* (fb-string fb-endpos ; Contains any fallback prefix found.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1899 (test-line
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1900 (lambda ()
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1901 (when (and (looking-at prefix-regexp)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1902 (< (match-end 0) (1- (cdr lit-limits))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1903 (unless fb-string
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1904 (setq fb-string (buffer-substring-no-properties
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1905 (match-beginning 0) (match-end 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1906 fb-endpos (match-end 0)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1907 (unless (eq (match-end 0) (c-point 'eol))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1908 (throw 'found t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1909 t))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1910 (if (catch 'found
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1911 ;; Search for a line which has text after the prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1912 ;; so that we get the proper amount of whitespace
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1913 ;; after it. We start with the current line, then
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1914 ;; search backwards, then forwards.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1915 (goto-char prefix-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1916 (when (and (funcall test-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1917 (/= (match-end 1) (match-end 0)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1918 ;; If the current line doesn't have text but do
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1919 ;; have whitespace after the prefix, we'll use it.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1920 (throw 'found t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1921 (while (and (zerop (forward-line -1))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1922 (> (point) (car lit-limits)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1923 (funcall test-line))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1924 (goto-char prefix-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1925 (while (and (zerop (forward-line 1))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1926 (< (point) (cdr lit-limits)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1927 (funcall test-line))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1928 nil)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1929 ;; A good line with text after the prefix was found.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1930 (cons (buffer-substring-no-properties (point) (match-end 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1931 (progn (goto-char (match-end 0)) (current-column)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1932 (if fb-string
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1933 ;; A good line wasn't found, but at least we have a
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1934 ;; fallback that matches the comment prefix regexp.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1935 (cond ((string-match "\\s \\'" fb-string)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1936 ;; There are ws after the prefix, so let's use it.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1937 (cons fb-string
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1938 (progn (goto-char fb-endpos) (current-column))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1939 ((progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1940 ;; Check if there's any whitespace padding
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1941 ;; on the comment start line that we can
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1942 ;; use after the prefix.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1943 (goto-char (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1944 (if (looking-at comment-start-regexp)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1945 (goto-char (match-end 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1946 (forward-char 2)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1947 (skip-chars-forward " \t"))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1948 (eq (char-syntax (char-before)) ?\ ))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1949 (setq fb-string (buffer-substring-no-properties
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1950 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1951 (skip-chars-backward " \t")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1952 (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1953 (point)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1954 (goto-char fb-endpos)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1955 (skip-chars-backward " \t")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1956 (let ((buffer-modified (buffer-modified-p))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1957 (buffer-undo-list t)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1958 (tmp (point)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1959 ;; Got to mess in the buffer once again to
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1960 ;; ensure the column gets correct. :P
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1961 (unwind-protect
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1962 (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1963 (insert fb-string)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1964 (cons (buffer-substring-no-properties
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1965 (c-point 'bol)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1966 (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1967 (current-column)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1968 (delete-region tmp (point)))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1969 (t
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1970 ;; Last resort: Just add a single space after
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1971 ;; the prefix.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1972 (cons (concat fb-string " ")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1973 (progn (goto-char fb-endpos)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1974 (1+ (current-column))))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1975 ;; The line doesn't match the comment prefix regexp.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1976 (if comment-prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1977 ;; We have a fallback for line comments that we must use.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1978 (cons (concat (buffer-substring-no-properties
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1979 prefix-line (c-point 'boi))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1980 comment-prefix)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1981 (progn (back-to-indentation)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1982 (+ (current-column) (length comment-prefix))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1983 ;; Assume we are dealing with a "free text" block
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1984 ;; comment where the lines doesn't have any comment
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1985 ;; prefix at all and we should just fill it as
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1986 ;; normal text.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1987 '("" . 0)))))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1988 ))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1989
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1990 (defun c-fill-paragraph (&optional arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1991 "Like \\[fill-paragraph] but handles C and C++ style comments.
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1992 If any of the current line is a comment or within a comment, fill the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1993 comment or the paragraph of it that point is in, preserving the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1994 comment indentation or line-starting decorations (see the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1995 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
1996 details).
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1997
21107
d67e858e738b (c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents: 20912
diff changeset
1998 If point is inside multiline string literal, fill it. This currently
d67e858e738b (c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents: 20912
diff changeset
1999 does not respect escaped newlines, except for the special case when it
d67e858e738b (c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents: 20912
diff changeset
2000 is the very first thing in the string. The intended use for this rule
d67e858e738b (c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents: 20912
diff changeset
2001 is in situations like the following:
d67e858e738b (c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents: 20912
diff changeset
2002
d67e858e738b (c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents: 20912
diff changeset
2003 char description[] = \"\\
d67e858e738b (c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents: 20912
diff changeset
2004 A very long description of something that you want to fill to make
d67e858e738b (c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents: 20912
diff changeset
2005 nicely formatted output.\"\;
d67e858e738b (c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents: 20912
diff changeset
2006
d67e858e738b (c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents: 20912
diff changeset
2007 If point is in any other situation, i.e. in normal code, do nothing.
d67e858e738b (c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents: 20912
diff changeset
2008
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2009 Optional prefix ARG means justify paragraph as well."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
2010 (interactive "*P")
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2011 (let (lit-limits lit-type fill
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2012 ;; beg and end limits the region to be filled. end is a marker.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2013 beg end
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2014 ;; tmp-pre and tmp-post marks strings that are temporarily
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2015 ;; inserted at the start and end of the region. tmp-pre is a
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2016 ;; cons of the positions of the prepended string. tmp-post is
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2017 ;; a marker pointing to the single character of the appended
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2018 ;; string.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2019 tmp-pre tmp-post
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2020 hang-ender-stuck)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2021 ;; Restore point on undo. It's necessary since we do a lot of
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2022 ;; hidden inserts and deletes below that should be as transparent
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2023 ;; as possible.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2024 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2025 (setq buffer-undo-list (cons (point) buffer-undo-list)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2026 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2027 (save-restriction
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2028 ;; Widen to catch comment limits correctly.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2029 (widen)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2030 (setq lit-limits (c-collect-line-comments (c-literal-limits nil t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2031 lit-type (c-literal-type lit-limits)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2032 (forward-paragraph)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2033 (setq end (point-marker))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2034 (backward-paragraph)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2035 (setq beg (point)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2036 (when (and (>= (point) beg) (<= (point) end))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2037 (unwind-protect
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2038 (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2039 (cond
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2040 ((eq lit-type 'c++) ; Line comment.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2041 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2042 ;; Fill to the comment or paragraph end, whichever
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2043 ;; comes first.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2044 (set-marker end (min end (cdr lit-limits)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2045 (when (<= beg (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2046 ;; The region to be filled includes the comment
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2047 ;; starter, so we must check it.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2048 (goto-char (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2049 (back-to-indentation)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2050 (if (eq (point) (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2051 ;; Include the first line in the fill.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2052 (setq beg (c-point 'bol))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2053 ;; The first line contains code before the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2054 ;; comment. We must fake a line that doesn't.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2055 (setq tmp-pre t)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2056 ))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2057 ((eq lit-type 'c) ; Block comment.
21107
d67e858e738b (c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents: 20912
diff changeset
2058 (save-excursion
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2059 (when (>= end (cdr lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2060 ;; The region to be filled includes the comment ender.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2061 (goto-char (cdr lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2062 (beginning-of-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2063 (if (and (looking-at (concat "[ \t]*\\("
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2064 c-comment-prefix-regexp
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2065 "\\)\\*/"))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2066 (eq (cdr lit-limits) (match-end 0)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2067 ;; Leave the comment ender on its own line.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2068 (set-marker end (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2069 ;; The comment ender should hang. Replace all
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2070 ;; cruft between it and the last word with a 'x'
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2071 ;; and include it in the fill. We'll change it
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2072 ;; back to a space afterwards.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2073 (let ((ender-start (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2074 (goto-char (cdr lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2075 (skip-syntax-backward "^w ")
27111
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2076 (point)))
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2077 spaces)
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2078 (goto-char (cdr lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2079 (setq tmp-post (point-marker))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2080 (insert ?\n)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2081 (set-marker end (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2082 (forward-line -1)
27111
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2083 (if (and (looking-at (concat "[ \t]*\\(\\("
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2084 c-comment-prefix-regexp
27111
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2085 "\\)[ \t]*\\)"))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2086 (eq ender-start (match-end 0)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2087 ;; The comment ender is prefixed by nothing
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2088 ;; but a comment line prefix. Remove it
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2089 ;; along with surrounding ws.
27111
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2090 (setq spaces (- (match-end 1) (match-end 2)))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2091 (goto-char ender-start))
27111
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2092 (skip-chars-backward " \t\r\n")
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2093 (when (/= (point) ender-start)
27111
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2094 ;; Keep one or two spaces between the text and
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2095 ;; the ender, depending on how many there are now.
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2096 (unless spaces (setq spaces (- ender-start (point))))
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2097 (setq spaces (max (min spaces 2) 1))
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2098 ; Insert the filler first to keep marks right.
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2099 (insert (make-string spaces ?x))
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2100 (delete-region (point) (+ ender-start spaces))
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2101 (setq hang-ender-stuck spaces)))))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2102 (when (<= beg (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2103 ;; The region to be filled includes the comment starter.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2104 (goto-char (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2105 (if (looking-at (concat "\\(" comment-start-skip "\\)$"))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2106 ;; Begin filling with the next line.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2107 (setq beg (c-point 'bonl))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2108 ;; Fake the fill prefix in the first line.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2109 (setq tmp-pre t)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2110 ))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2111 ((eq lit-type 'string) ; String.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2112 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2113 (when (>= end (cdr lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2114 (goto-char (1- (cdr lit-limits)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2115 (setq tmp-post (point-marker))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2116 (insert ?\n)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2117 (set-marker end (point)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2118 (when (<= beg (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2119 (goto-char (1+ (car lit-limits)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2120 (setq beg (if (looking-at "\\\\$")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2121 ;; Leave the start line if it's
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2122 ;; nothing but an escaped newline.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2123 (1+ (match-end 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2124 (point))))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2125 (t (setq beg nil)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2126 (when tmp-pre
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2127 ;; Temporarily insert the fill prefix after the comment
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2128 ;; starter so that the first line looks like any other
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2129 ;; comment line in the narrowed region.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2130 (setq fill (c-guess-fill-prefix lit-limits lit-type))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2131 (unless (string-match (concat "\\`[ \t]*\\("
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2132 c-comment-prefix-regexp
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2133 "\\)[ \t]*\\'")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2134 (car fill))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2135 ;; Oops, the prefix doesn't match the comment prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2136 ;; regexp. This could produce very confusing
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2137 ;; results with adaptive fill packages together with
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2138 ;; the insert prefix magic below, since the prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2139 ;; often doesn't appear at all. So let's warn about
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2140 ;; it.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2141 (message "\
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2142 Warning: `c-comment-prefix-regexp' doesn't match the comment prefix %S"
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2143 (car fill)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2144 ;; Find the right spot on the line, break it, insert
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2145 ;; the fill prefix and make sure we're back in the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2146 ;; same column by temporarily prefixing the first word
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2147 ;; with a number of 'x'.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2148 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2149 (goto-char (car lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2150 (if (looking-at (if (eq lit-type 'c++)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2151 c-comment-prefix-regexp
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2152 comment-start-skip))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2153 (goto-char (match-end 0))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2154 (forward-char 2)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2155 (skip-chars-forward " \t"))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2156 (while (< (current-column) (cdr fill)) (forward-char 1))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2157 (let ((col (current-column)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2158 (setq beg (1+ (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2159 tmp-pre (list (point)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2160 (unwind-protect
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2161 (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2162 (insert ?\n (car fill))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2163 (insert (make-string (- col (current-column)) ?x)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2164 (setcdr tmp-pre (point))))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2165 (when beg
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2166 (let ((fill-paragraph-function
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2167 ;; Avoid infinite recursion.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2168 (if (not (eq fill-paragraph-function 'c-fill-paragraph))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2169 fill-paragraph-function))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2170 (fill-prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2171 (or fill-prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2172 (when (and (eq lit-type 'c++)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2173 (not (string-match
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2174 "\\`[ \t]*//"
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2175 (or (fill-context-prefix beg end)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2176 ""))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2177 ;; Kludge: If the function that adapts the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2178 ;; fill prefix doesn't produce the required
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2179 ;; comment starter for line comments, then
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2180 ;; force it by setting fill-prefix.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2181 (car (or fill (c-guess-fill-prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2182 lit-limits lit-type)))))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2183 ;; Preparations finally done! Now we can call the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2184 ;; real fill function.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2185 (save-restriction
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2186 (narrow-to-region beg end)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2187 (fill-paragraph arg)))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2188 (when (consp tmp-pre)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2189 (delete-region (car tmp-pre) (cdr tmp-pre)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2190 (when tmp-post
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2191 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2192 (goto-char tmp-post)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2193 (delete-char 1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2194 (when hang-ender-stuck
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2195 (skip-syntax-backward "^w ")
27111
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2196 (forward-char (- hang-ender-stuck))
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2197 (insert (make-string hang-ender-stuck ?\ ))
cad48fa4c22d (c-fill-paragraph): Count number of spaces
Gerd Moellmann <gerd@gnu.org>
parents: 27021
diff changeset
2198 (delete-char hang-ender-stuck))
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2199 (set-marker tmp-post nil)))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2200 (set-marker end nil))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2201 ;; Always return t. This has the effect that if filling isn't done
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2202 ;; above, it isn't done at all, and it's therefore effectively
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2203 ;; disabled in normal code.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2204 t)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2205
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2206 (defun c-do-auto-fill ()
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2207 ;; Do automatic filling if not inside a context where it should be
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2208 ;; ignored.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2209 (let ((c-auto-fill-prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2210 ;; The decision whether the line should be broken is actually
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2211 ;; done in c-indent-new-comment-line, which do-auto-fill
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2212 ;; calls to break lines. We just set this special variable
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2213 ;; so that we'll know when we're called from there. It's
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2214 ;; also used to detect whether fill-prefix is user set or
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2215 ;; generated automatically by do-auto-fill.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2216 fill-prefix))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2217 (do-auto-fill)))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2218
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2219 (defun c-indent-new-comment-line (&optional soft)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2220 "Break line at point and indent, continuing comment if within one.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2221 If inside a comment and `comment-multi-line' is non-nil, the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2222 indentation and line prefix are preserved (see the
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2223 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2224 details). If inside a comment and `comment-multi-line' is nil, a new
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2225 comment of the same type is started on the next line and indented as
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2226 appropriate for comments.
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2227
26817
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2228 If a fill prefix is specified, it overrides all the above."
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2229 (interactive)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2230 (let ((fill-prefix fill-prefix)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2231 (do-line-break
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2232 (lambda ()
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2233 (delete-region (progn (skip-chars-backward " \t") (point))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2234 (progn (skip-chars-forward " \t") (point)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2235 (if soft (insert-and-inherit ?\n) (newline 1))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2236 ;; Already know the literal type and limits when called from
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2237 ;; c-context-line-break.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2238 (c-lit-limits (if (boundp 'c-lit-limits) c-lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2239 (c-lit-type (if (boundp 'c-lit-type) c-lit-type)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2240 (when (boundp 'c-auto-fill-prefix)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2241 ;; Called from do-auto-fill.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2242 (unless c-lit-limits
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2243 (setq c-lit-limits (c-literal-limits nil nil t)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2244 (unless c-lit-type
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2245 (setq c-lit-type (c-literal-type c-lit-limits)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2246 (if (memq (cond ((eq c-lit-type 'pound)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2247 ;; Come to think about it, "pound" is a bit
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2248 ;; of a misnomer, so call it "cpp" instead
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2249 ;; in user interaction.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2250 'cpp)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2251 ((null c-lit-type) 'code)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2252 (t c-lit-type))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2253 c-ignore-auto-fill)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2254 (setq fill-prefix t) ; Used as flag in the cond.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2255 (if (null c-auto-fill-prefix)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2256 (setq fill-prefix nil))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2257 (cond ((eq fill-prefix t)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2258 ;; A call from do-auto-fill which should be ignored.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2259 )
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2260 (fill-prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2261 ;; A fill-prefix overrides anything.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2262 (funcall do-line-break)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2263 (insert-and-inherit fill-prefix))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2264 ((progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2265 (unless c-lit-limits
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2266 (setq c-lit-limits (c-literal-limits nil nil t)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2267 (unless c-lit-type
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2268 (setq c-lit-type (c-literal-type c-lit-limits)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2269 (memq c-lit-type '(c c++)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2270 (if comment-multi-line
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2271 ;; Inside a comment that should be continued.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2272 (let ((fill (c-guess-fill-prefix
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2273 (setq c-lit-limits
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2274 (c-collect-line-comments c-lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2275 c-lit-type))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2276 (pos (point)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2277 (if (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2278 (back-to-indentation)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2279 (> (point) (car c-lit-limits))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2280 (looking-at c-comment-prefix-regexp))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2281 (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2282 ;; Skip forward past the fill prefix in case
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2283 ;; we're standing in it.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2284 (while (and (< (current-column) (cdr fill))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2285 (not (eolp)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2286 (forward-char 1))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2287 (if (> (point) (if (and (eq c-lit-type 'c)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2288 (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2289 (forward-char -2)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2290 (looking-at "\\*/")))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2291 (- (cdr c-lit-limits) 2)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2292 (cdr c-lit-limits)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2293 (progn
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2294 ;; The skip takes us out of the comment;
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2295 ;; insert the fill prefix at bol instead
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2296 ;; and keep the position.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2297 (setq pos (copy-marker pos t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2298 (beginning-of-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2299 (insert-and-inherit (car fill))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2300 (if soft (insert-and-inherit ?\n) (newline 1))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2301 (goto-char pos)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2302 (set-marker pos nil))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2303 (funcall do-line-break)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2304 (insert-and-inherit (car fill))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2305 (funcall do-line-break)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2306 (insert-and-inherit (car fill))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2307 ;; Inside a comment that should be broken.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2308 (let ((comment-start comment-start)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2309 (comment-end comment-end)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2310 col)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2311 (if (eq c-lit-type 'c)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2312 (unless (string-match "[ \t]*/\\*" comment-start)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2313 (setq comment-start "/* " comment-end " */"))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2314 (unless (string-match "[ \t]*//" comment-start)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2315 (setq comment-start "// " comment-end "")))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2316 (setq col (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2317 (back-to-indentation)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2318 (current-column)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2319 (funcall do-line-break)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2320 (when (and comment-end (not (equal comment-end "")))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2321 (forward-char -1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2322 (insert-and-inherit comment-end)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2323 (forward-char 1))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2324 ;; c-comment-indent may look at the current
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2325 ;; indentation, so let's start out with the same
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2326 ;; indentation as the previous one.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2327 (indent-to col)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2328 (insert-and-inherit comment-start)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2329 (indent-for-comment))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2330 (t
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2331 ;; Somewhere else in the code.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2332 (let ((col (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2333 (while (progn (back-to-indentation)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2334 (and (looking-at "^\\s *$")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2335 (= (forward-line -1) 0))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2336 (current-column))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2337 (funcall do-line-break)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2338 (indent-to col))))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2339
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2340 (defalias 'c-comment-line-break-function 'c-indent-new-comment-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2341 (make-obsolete 'c-comment-line-break-function 'c-indent-new-comment-line)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2342
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2343 ;; advice for indent-new-comment-line for older Emacsen
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2344 (unless (boundp 'comment-line-break-function)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2345 (defadvice indent-new-comment-line (around c-line-break-advice
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2346 activate preactivate)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2347 "Call `c-indent-new-comment-line' if in CC Mode."
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2348 (if (or (boundp 'c-inside-line-break-advice)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2349 (not c-buffer-is-cc-mode))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2350 ad-do-it
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2351 (let (c-inside-line-break-advice)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2352 (c-indent-new-comment-line (ad-get-arg 0))))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2353
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2354 (defun c-context-line-break ()
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2355 "Do a line break suitable to the context.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2356
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2357 When point is outside a comment, insert a newline and indent according
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2358 to the syntactic context.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2359
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2360 When point is inside a comment, continue it with the appropriate
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2361 comment prefix (see the `c-comment-prefix-regexp' and
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2362 `c-block-comment-prefix' variables for details). The end of a
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2363 C++-style line comment doesn't count as inside the comment, though."
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2364 (interactive "*")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2365 (let* ((c-lit-limits (c-literal-limits nil nil t))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2366 (c-lit-type (c-literal-type c-lit-limits)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2367 (if (or (eq c-lit-type 'c)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2368 (and (eq c-lit-type 'c++)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2369 (< (point)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2370 (1- (cdr (setq c-lit-limits
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2371 (c-collect-line-comments c-lit-limits)))))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2372 (let ((comment-multi-line t)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2373 (fill-prefix nil))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2374 (c-indent-new-comment-line))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2375 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2376 (newline)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2377 ;; c-indent-line may look at the current indentation, so let's
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2378 ;; start out with the same indentation as the previous line.
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2379 (let ((col (save-excursion
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2380 (forward-line -1)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2381 (while (progn (back-to-indentation)
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2382 (and (looking-at "^\\s *$")
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2383 (= (forward-line -1) 0))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2384 (current-column))))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2385 (indent-to col))
03befb219d03 Installed version 5.26
Gerd Moellmann <gerd@gnu.org>
parents: 24529
diff changeset
2386 (c-indent-line))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2387
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2388
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2389 (provide 'cc-cmds)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2390 ;;; cc-cmds.el ends here