annotate lisp/cc-mode/cc-cmds.el @ 175:2d532a89d707 r20-3b14

Import from CVS: tag r20-3b14
author cvs
date Mon, 13 Aug 2007 09:50:14 +0200
parents 8eaf7971accc
children 6075d714658b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1 ;;; cc-cmds.el --- user level commands for CC Mode
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
2
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97 Free Software Foundation, Inc.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
4
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
5 ;; Authors: 1992-1997 Barry A. Warsaw
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
6 ;; 1987 Dave Detlefs and Stewart Clamen
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
7 ;; 1985 Richard M. Stallman
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
8 ;; Maintainer: cc-mode-help@python.org
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
9 ;; Created: 22-Apr-1997 (split from cc-mode.el)
175
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 173
diff changeset
10 ;; Version: 5.13
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
11 ;; Keywords: c languages oop
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
12
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
13 ;; This file is part of GNU Emacs.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
14
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
16 ;; it under the terms of the GNU General Public License as published by
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
17 ;; the Free Software Foundation; either version 2, or (at your option)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
18 ;; any later version.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
19
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
20 ;; GNU Emacs is distributed in the hope that it will be useful,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
23 ;; GNU General Public License for more details.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
24
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
25 ;; You should have received a copy of the GNU General Public License
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
28 ;; Boston, MA 02111-1307, USA.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
29
175
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 173
diff changeset
30 (eval-when-compile
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 173
diff changeset
31 (require 'cc-defs))
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 173
diff changeset
32
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
33
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
34 (defun c-calculate-state (arg prevstate)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
35 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
36 ;; arg is nil or zero, toggle the state. If arg is negative, turn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
37 ;; the state off, and if arg is positive, turn the state on
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
38 (if (or (not arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
39 (zerop (setq arg (prefix-numeric-value arg))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
40 (not prevstate)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
41 (> arg 0)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
42
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
43 ;; Auto-newline and hungry-delete
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
44 (defun c-toggle-auto-state (arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
45 "Toggle auto-newline feature.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
46 Optional numeric ARG, if supplied turns on auto-newline when positive,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
47 turns it off when negative, and just toggles it when zero.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
48
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
49 When the auto-newline feature is enabled (as evidenced by the `/a' or
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
50 `/ah' on the modeline after the mode name) newlines are automatically
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
51 inserted after special characters such as brace, comma, semi-colon,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
52 and colon."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
53 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
54 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
55 (c-update-modeline)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
56 (c-keep-region-active))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
57
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
58 (defun c-toggle-hungry-state (arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
59 "Toggle hungry-delete-key feature.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
60 Optional numeric ARG, if supplied turns on hungry-delete when positive,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
61 turns it off when negative, and just toggles it when zero.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
62
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
63 When the hungry-delete-key feature is enabled (as evidenced by the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
64 `/h' or `/ah' on the modeline after the mode name) the delete key
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
65 gobbles all preceding whitespace in one fell swoop."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
66 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
67 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
68 (c-update-modeline)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
69 (c-keep-region-active))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
70
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
71 (defun c-toggle-auto-hungry-state (arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
72 "Toggle auto-newline and hungry-delete-key features.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
73 Optional numeric ARG, if supplied turns on auto-newline and
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
74 hungry-delete when positive, turns them off when negative, and just
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
75 toggles them when zero.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
76
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
77 See `c-toggle-auto-state' and `c-toggle-hungry-state' for details."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
78 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
79 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
80 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
81 (c-update-modeline)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
82 (c-keep-region-active))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
83
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
84
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
85 ;; Electric keys
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
86
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
87 ;; Note: In XEmacs 20.3 the Delete and BackSpace keysyms have been
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
88 ;; separated and "\177" is no longer an alias for both keys. Also,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
89 ;; the variable delete-key-deletes-forward controls in which direction
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
90 ;; the Delete keysym deletes characters. The functions
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
91 ;; c-electric-delete and c-electric-backspace attempt to deal with
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
92 ;; this new functionality. For Emacs 19 and XEmacs 19 backwards
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
93 ;; compatibility, the old behavior has moved to c-electric-backspace
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
94 ;; and c-backspace-function.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
95
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
96 (defun c-electric-backspace (arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
97 "Deletes preceding character or whitespace.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
98 If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
99 \"/ah\" string on the mode line, then all preceding whitespace is
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
100 consumed. If however an ARG is supplied, or `c-hungry-delete-key' is
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
101 nil, or point is inside a literal then the function in the variable
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
102 `c-backspace-function' is called.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
103
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
104 See also \\[c-electric-delete]."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
105 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
106 (if (or (not c-hungry-delete-key)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
107 arg
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
108 (c-in-literal))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
109 (funcall c-backspace-function (prefix-numeric-value arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
110 (let ((here (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
111 (skip-chars-backward " \t\n")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
112 (if (/= (point) here)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
113 (delete-region (point) here)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
114 (funcall c-backspace-function 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
115 ))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
116
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
117 (defun c-electric-delete (arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
118 "Deletes preceding or following character or whitespace.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
119
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
120 The behavior of this function depends on the variable
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
121 `delete-key-deletes-forward'. If this variable is nil (or does not
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
122 exist, as in older Emacsen), then this function behaves identical to
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
123 \\[c-electric-backspace].
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
124
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
125 If `delete-key-deletes-forward' is non-nil, then deletion occurs in
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
126 the forward direction. So if `c-hungry-delete-key' is non-nil, as
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
127 evidenced by the \"/h\" or \"/ah\" string on the mode line, then all
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
128 following whitespace is consumed. If however an ARG is supplied, or
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
129 `c-hungry-delete-key' is nil, or point is inside a literal then the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
130 function in the variable `c-delete-function' is called."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
131 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
132 (if (and (boundp 'delete-key-deletes-forward)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
133 delete-key-deletes-forward)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
134 (if (or (not c-hungry-delete-key)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
135 arg
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
136 (c-in-literal))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
137 (funcall c-delete-function (prefix-numeric-value arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
138 (let ((here (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
139 (skip-chars-forward " \t\n")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
140 (if (/= (point) here)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
141 (delete-region (point) here)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
142 (funcall c-delete-function 1))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
143 ;; act just like c-electric-backspace
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
144 (c-electric-backspace arg)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
145
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
146 (defun c-electric-pound (arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
147 "Electric pound (`#') insertion.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
148 Inserts a `#' character specially depending on the variable
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
149 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
150 point is inside a literal, nothing special happens."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
151 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
152 (if (or (c-in-literal)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
153 arg
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
154 (not (memq 'alignleft c-electric-pound-behavior)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
155 ;; do nothing special
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
156 (self-insert-command (prefix-numeric-value arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
157 ;; place the pound character at the left edge
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
158 (let ((pos (- (point-max) (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
159 (bolp (bolp)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
160 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
161 (delete-horizontal-space)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
162 (insert-char last-command-char 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
163 (and (not bolp)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
164 (goto-char (- (point-max) pos)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
165 )))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
166
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
167 (defun c-electric-brace (arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
168 "Insert a brace.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
169
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
170 If the auto-newline feature is turned on, as evidenced by the \"/a\"
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
171 or \"/ah\" string on the mode line, newlines are inserted before and
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
172 after braces based on the value of `c-hanging-braces-alist'.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
173
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
174 Also, the line is re-indented unless a numeric ARG is supplied, there
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
175 are non-whitespace characters present on the line after the brace, or
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
176 the brace is inserted inside a literal."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
177 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
178 (let* ((c-state-cache (c-parse-state))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
179 (safepos (c-safe-position (point) c-state-cache))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
180 (literal (c-in-literal safepos)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
181 ;; if we're in a literal, or we're not at the end of the line, or
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
182 ;; a numeric arg is provided, or auto-newlining is turned off,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
183 ;; then just insert the character.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
184 (if (or literal arg
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
185 ; (not c-auto-newline)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
186 (not (looking-at "[ \t]*$")))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
187 (self-insert-command (prefix-numeric-value arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
188 (let* ((syms '(class-open class-close defun-open defun-close
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
189 inline-open inline-close brace-list-open brace-list-close
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
190 brace-list-intro brace-list-entry block-open block-close
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
191 substatement-open statement-case-open
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
192 extern-lang-open extern-lang-close))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
193 ;; we want to inhibit blinking the paren since this will
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
194 ;; be most disruptive. we'll blink it ourselves later on
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
195 (old-blink-paren blink-paren-function)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
196 blink-paren-function
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
197 (insertion-point (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
198 delete-temp-newline
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 171
diff changeset
199 (preserve-p (eq ? (char-syntax (char-before))))
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
200 ;; shut this up too
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
201 (c-echo-syntactic-information-p nil)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
202 (syntax (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
203 ;; only insert a newline if there is
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
204 ;; non-whitespace behind us
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
205 (if (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
206 (skip-chars-backward " \t")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
207 (not (bolp)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
208 (progn (newline)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
209 (setq delete-temp-newline t)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
210 (self-insert-command (prefix-numeric-value arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
211 ;; state cache doesn't change
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
212 (c-guess-basic-syntax)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
213 (newlines (and
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
214 c-auto-newline
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
215 (or (c-lookup-lists syms syntax c-hanging-braces-alist)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
216 '(ignore before after)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
217 ;; If syntax is a function symbol, then call it using the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
218 ;; defined semantics.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
219 (if (and (not (consp (cdr newlines)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
220 (functionp (cdr newlines)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
221 (let ((c-syntactic-context syntax))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
222 (setq newlines
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
223 (funcall (cdr newlines) (car newlines) insertion-point))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
224 ;; does a newline go before the open brace?
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
225 (if (memq 'before newlines)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
226 ;; we leave the newline we've put in there before,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
227 ;; but we need to re-indent the line above
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
228 (let ((pos (- (point-max) (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
229 (here (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
230 (c-state-cache c-state-cache))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
231 (forward-line -1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
232 ;; we may need to update the cache. this should still be
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
233 ;; faster than recalculating the state in many cases
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
234 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
235 (save-restriction
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
236 (narrow-to-region here (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
237 (if (and (c-safe (progn (backward-up-list -1) t))
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
238 (memq (char-before) '(?\) ?}))
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
239 (progn (widen)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
240 (c-safe (progn (forward-sexp -1) t))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
241 (setq c-state-cache
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
242 (c-hack-state (point) 'open c-state-cache))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
243 (if (and (car c-state-cache)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
244 (not (consp (car c-state-cache)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
245 (<= (point) (car c-state-cache)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
246 (setq c-state-cache (cdr c-state-cache))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
247 ))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
248 (let ((here (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
249 (shift (c-indent-line)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
250 (setq c-state-cache (c-adjust-state (c-point 'bol) here
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
251 (- shift) c-state-cache)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
252 (goto-char (- (point-max) pos))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
253 ;; if the buffer has changed due to the indentation, we
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
254 ;; need to recalculate syntax for the current line, but
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
255 ;; we won't need to update the state cache.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
256 (if (/= (point) here)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
257 (setq syntax (c-guess-basic-syntax))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
258 ;; must remove the newline we just stuck in (if we really did it)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
259 (and delete-temp-newline
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
260 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
261 ;; if there is whitespace before point, then preserve
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
262 ;; at least one space.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
263 (delete-indentation)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
264 (just-one-space)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
265 (if (not preserve-p)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
266 (delete-char -1))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
267 ;; since we're hanging the brace, we need to recalculate
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
268 ;; syntax. Update the state to accurately reflect the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
269 ;; beginning of the line. We punt if we cross any open or
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
270 ;; closed parens because its just too hard to modify the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
271 ;; known state. This limitation will be fixed in v5.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
272 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
273 (let ((bol (c-point 'bol)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
274 (if (zerop (car (parse-partial-sexp bol (1- (point)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
275 (setq c-state-cache (c-whack-state bol c-state-cache)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
276 syntax (c-guess-basic-syntax))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
277 ;; gotta punt. this requires some horrible kludgery
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
278 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
279 (makunbound 'c-state-cache)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
280 (setq c-state-cache (c-parse-state)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
281 syntax nil))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
282 )
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
283 ;; now adjust the line's indentation. don't update the state
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
284 ;; cache since c-guess-basic-syntax isn't called when the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
285 ;; syntax is passed to c-indent-line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
286 (let ((here (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
287 (shift (c-indent-line syntax)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
288 (setq c-state-cache (c-adjust-state (c-point 'bol) here
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
289 (- shift) c-state-cache)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
290 ;; Do all appropriate clean ups
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
291 (let ((here (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
292 (pos (- (point-max) (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
293 mbeg mend)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
294 ;; clean up empty defun braces
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
295 (if (and c-auto-newline
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
296 (memq 'empty-defun-braces c-cleanup-list)
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
297 (eq last-command-char ?\})
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
298 (c-intersect-lists '(defun-close class-close inline-close)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
299 syntax)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
300 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
301 (forward-char -1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
302 (skip-chars-backward " \t\n")
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
303 (eq (char-before) ?\{))
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
304 ;; make sure matching open brace isn't in a comment
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
305 (not (c-in-literal)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
306 (delete-region (point) (1- here)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
307 ;; clean up brace-else-brace
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
308 (if (and c-auto-newline
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
309 (memq 'brace-else-brace c-cleanup-list)
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
310 (eq last-command-char ?\{)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
311 (re-search-backward "}[ \t\n]*else[ \t\n]*{" nil t)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
312 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
313 (setq mbeg (match-beginning 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
314 mend (match-end 0))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
315 (= mend here))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
316 (not (c-in-literal)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
317 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
318 (delete-region mbeg mend)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
319 (insert "} else {")))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
320 ;; clean up brace-elseif-brace
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
321 (if (and c-auto-newline
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
322 (memq 'brace-elseif-brace c-cleanup-list)
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
323 (eq last-command-char ?\{)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
324 (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*" nil t)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
325 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
326 (goto-char (match-end 0))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
327 (c-safe (forward-sexp 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
328 (skip-chars-forward " \t\n")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
329 (setq mbeg (match-beginning 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
330 mend (match-end 0))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
331 (= here (1+ (point))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
332 (not (c-in-literal)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
333 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
334 (delete-region mbeg mend)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
335 (insert "} else if ")))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
336 (goto-char (- (point-max) pos))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
337 )
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
338 ;; does a newline go after the brace?
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
339 (if (memq 'after newlines)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
340 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
341 (newline)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
342 ;; update on c-state-cache
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
343 (let* ((bufpos (- (point) 2))
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
344 (which (if (eq (char-after bufpos) ?{) 'open 'close))
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
345 (c-state-cache (c-hack-state bufpos which c-state-cache)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
346 (c-indent-line))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
347 ;; blink the paren
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
348 (and (eq last-command-char ?\})
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
349 old-blink-paren
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
350 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
351 (c-backward-syntactic-ws safepos)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
352 (funcall old-blink-paren)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
353 ))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
354
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
355 (defun c-electric-slash (arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
356 "Insert a slash character.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
357 If slash is second of a double-slash C++ style comment introducing
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
358 construct, and we are on a comment-only-line, indent line as comment.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
359 If numeric ARG is supplied or point is inside a literal, indentation
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
360 is inhibited."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
361 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
362 (let ((indentp (and (not arg)
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
363 (eq (char-before) ?/)
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
364 (eq last-command-char ?/)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
365 (not (c-in-literal))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
366 ;; shut this up
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
367 (c-echo-syntactic-information-p nil))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
368 (self-insert-command (prefix-numeric-value arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
369 (if indentp
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
370 (c-indent-line))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
371
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
372 (defun c-electric-star (arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
373 "Insert a star character.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
374 If the star is the second character of a C style comment introducing
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
375 construct, and we are on a comment-only-line, indent line as comment.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
376 If numeric ARG is supplied or point is inside a literal, indentation
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
377 is inhibited."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
378 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
379 (self-insert-command (prefix-numeric-value arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
380 ;; if we are in a literal, or if arg is given do not re-indent the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
381 ;; current line, unless this star introduces a comment-only line.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
382 (if (and (not arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
383 (memq (c-in-literal) '(c))
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
384 (eq (char-before) ?*)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
385 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
386 (forward-char -1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
387 (skip-chars-backward "*")
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
388 (if (eq (char-before) ?/)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
389 (forward-char -1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
390 (skip-chars-backward " \t")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
391 (bolp)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
392 ;; shut this up
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
393 (let (c-echo-syntactic-information-p)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
394 (c-indent-line))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
395 ))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
396
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
397 (defun c-electric-semi&comma (arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
398 "Insert a comma or semicolon.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
399 When the auto-newline feature is turned on, as evidenced by the \"/a\"
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
400 or \"/ah\" string on the mode line, a newline might be inserted. See
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
401 the variable `c-hanging-semi&comma-criteria' for how newline insertion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
402 is determined.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
403
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
404 When semicolon is inserted, the line is re-indented unless a numeric
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
405 arg is supplied, point is inside a literal, or there are
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
406 non-whitespace characters on the line following the semicolon."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
407 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
408 (let* ((lim (c-most-enclosing-brace (c-parse-state)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
409 (literal (c-in-literal lim))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
410 (here (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
411 ;; shut this up
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
412 (c-echo-syntactic-information-p nil))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
413 (if (or literal
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
414 arg
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
415 (not (looking-at "[ \t]*$")))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
416 (self-insert-command (prefix-numeric-value arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
417 ;; do some special stuff with the character
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
418 (self-insert-command (prefix-numeric-value arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
419 ;; do all cleanups, reindentations, and newline insertions, but
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
420 ;; only if c-auto-newline is turned on
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
421 (if (not c-auto-newline) nil
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
422 ;; clean ups
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
423 (let ((pos (- (point-max) (point))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
424 (if (and (or (and
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
425 (eq last-command-char ?,)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
426 (memq 'list-close-comma c-cleanup-list))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
427 (and
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
428 (eq last-command-char ?\;)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
429 (memq 'defun-close-semi c-cleanup-list)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
430 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
431 (forward-char -1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
432 (skip-chars-backward " \t\n")
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
433 (eq (char-before) ?}))
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
434 ;; make sure matching open brace isn't in a comment
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
435 (not (c-in-literal lim)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
436 (delete-region (point) here))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
437 (goto-char (- (point-max) pos)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
438 ;; re-indent line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
439 (c-indent-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
440 ;; check to see if a newline should be added
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
441 (let ((criteria c-hanging-semi&comma-criteria)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
442 answer add-newline-p)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
443 (while criteria
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
444 (setq answer (funcall (car criteria)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
445 ;; only nil value means continue checking
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
446 (if (not answer)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
447 (setq criteria (cdr criteria))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
448 (setq criteria nil)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
449 ;; only 'stop specifically says do not add a newline
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
450 (setq add-newline-p (not (eq answer 'stop)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
451 ))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
452 (if add-newline-p
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
453 (progn (newline)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
454 (c-indent-line)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
455 )))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
456
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
457 (defun c-electric-colon (arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
458 "Insert a colon.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
459
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
460 If the auto-newline feature is turned on, as evidenced by the \"/a\"
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
461 or \"/ah\" string on the mode line, newlines are inserted before and
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
462 after colons based on the value of `c-hanging-colons-alist'.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
463
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
464 Also, the line is re-indented unless a numeric ARG is supplied, there
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
465 are non-whitespace characters present on the line after the colon, or
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
466 the colon is inserted inside a literal.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
467
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
468 This function cleans up double colon scope operators based on the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
469 value of `c-cleanup-list'."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
470 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
471 (let* ((bod (c-point 'bod))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
472 (literal (c-in-literal bod))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
473 syntax newlines
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
474 ;; shut this up
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
475 (c-echo-syntactic-information-p nil))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
476 (if (or literal
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
477 arg
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
478 (not (looking-at "[ \t]*$")))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
479 (self-insert-command (prefix-numeric-value arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
480 ;; insert the colon, then do any specified cleanups
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
481 (self-insert-command (prefix-numeric-value arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
482 (let ((pos (- (point-max) (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
483 (here (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
484 (if (and c-auto-newline
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
485 (memq 'scope-operator c-cleanup-list)
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
486 (eq (char-before) ?:)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
487 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
488 (forward-char -1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
489 (skip-chars-backward " \t\n")
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
490 (eq (char-before) ?:))
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
491 (not (c-in-literal))
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
492 (not (eq (char-after (- (point) 2)) ?:)))
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
493 (delete-region (point) (1- here)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
494 (goto-char (- (point-max) pos)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
495 ;; lets do some special stuff with the colon character
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
496 (setq syntax (c-guess-basic-syntax)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
497 ;; some language elements can only be determined by
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
498 ;; checking the following line. Lets first look for ones
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
499 ;; that can be found when looking on the line with the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
500 ;; colon
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
501 newlines
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
502 (and c-auto-newline
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
503 (or (c-lookup-lists '(case-label label access-label)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
504 syntax c-hanging-colons-alist)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
505 (c-lookup-lists '(member-init-intro inher-intro)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
506 (prog2
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
507 (insert "\n")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
508 (c-guess-basic-syntax)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
509 (delete-char -1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
510 c-hanging-colons-alist))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
511 ;; indent the current line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
512 (c-indent-line syntax)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
513 ;; does a newline go before the colon? Watch out for already
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
514 ;; non-hung colons. However, we don't unhang them because that
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
515 ;; would be a cleanup (and anti-social).
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
516 (if (and (memq 'before newlines)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
517 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
518 (skip-chars-backward ": \t")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
519 (not (bolp))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
520 (let ((pos (- (point-max) (point))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
521 (forward-char -1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
522 (newline)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
523 (c-indent-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
524 (goto-char (- (point-max) pos))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
525 ;; does a newline go after the colon?
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
526 (if (memq 'after (cdr-safe newlines))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
527 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
528 (newline)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
529 (c-indent-line)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
530 )))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
531
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
532 (defun c-electric-lt-gt (arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
533 "Insert a less-than, or greater-than character.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
534 When the auto-newline feature is turned on, as evidenced by the \"/a\"
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
535 or \"/ah\" string on the mode line, the line will be re-indented if
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
536 the character inserted is the second of a C++ style stream operator
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
537 and the buffer is in C++ mode.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
538
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
539 The line will also not be re-indented if a numeric argument is
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
540 supplied, or point is inside a literal."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
541 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
542 (let ((indentp (and (not arg)
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
543 (eq (char-before) last-command-char)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
544 (not (c-in-literal))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
545 ;; shut this up
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
546 (c-echo-syntactic-information-p nil))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
547 (self-insert-command (prefix-numeric-value arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
548 (if indentp
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
549 (c-indent-line))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
550
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
551
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
552
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
553 ;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
554 ;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
555 (defun c-forward-into-nomenclature (&optional arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
556 "Move forward to end of a nomenclature section or word.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
557 With arg, to it arg times."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
558 (interactive "p")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
559 (let ((case-fold-search nil))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
560 (if (> arg 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
561 (re-search-forward "\\W*\\([A-Z]*[a-z0-9]*\\)" (point-max) t arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
562 (while (and (< arg 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
563 (re-search-backward
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
564 "\\(\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\W\\w+\\)"
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
565 (point-min) 0))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
566 (forward-char 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
567 (setq arg (1+ arg)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
568 (c-keep-region-active))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
569
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
570 (defun c-backward-into-nomenclature (&optional arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
571 "Move backward to beginning of a nomenclature section or word.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
572 With optional ARG, move that many times. If ARG is negative, move
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
573 forward."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
574 (interactive "p")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
575 (c-forward-into-nomenclature (- arg))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
576 (c-keep-region-active))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
577
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
578 (defun c-scope-operator ()
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
579 "Insert a double colon scope operator at point.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
580 No indentation or other \"electric\" behavior is performed."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
581 (interactive)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
582 (insert "::"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
583
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
584
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
585 (defun c-beginning-of-statement (&optional count lim sentence-flag)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
586 "Go to the beginning of the innermost C statement.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
587 With prefix arg, go back N - 1 statements. If already at the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
588 beginning of a statement then go to the beginning of the preceding
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
589 one. If within a string or comment, or next to a comment (only
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
590 whitespace between), move by sentences instead of statements.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
591
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
592 When called from a program, this function takes 3 optional args: the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
593 repetition count, a buffer position limit which is the farthest back
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
594 to search, and a flag saying whether to do sentence motion when in a
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
595 comment."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
596 (interactive (list (prefix-numeric-value current-prefix-arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
597 nil t))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
598 (let ((here (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
599 (count (or count 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
600 (lim (or lim (c-point 'bod)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
601 state)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
602 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
603 (goto-char lim)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
604 (setq state (parse-partial-sexp (point) here nil nil)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
605 (if (and sentence-flag
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
606 (or (nth 3 state)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
607 (nth 4 state)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
608 ; (looking-at (concat "[ \t]*" comment-start-skip))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
609 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
610 (skip-chars-backward " \t")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
611 (goto-char (- (point) 2))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
612 (looking-at "\\*/"))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
613 (forward-sentence (- count))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
614 (while (> count 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
615 (c-beginning-of-statement-1 lim)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
616 (setq count (1- count)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
617 (while (< count 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
618 (c-end-of-statement-1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
619 (setq count (1+ count))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
620 ;; its possible we've been left up-buf of lim
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
621 (goto-char (max (point) lim))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
622 )
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
623 (c-keep-region-active))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
624
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
625 (defun c-end-of-statement (&optional count lim sentence-flag)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
626 "Go to the end of the innermost C statement.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
627
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
628 With prefix arg, go forward N - 1 statements. Move forward to end of
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
629 the next statement if already at end. If within a string or comment,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
630 move by sentences instead of statements.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
631
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
632 When called from a program, this function takes 3 optional args: the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
633 repetition count, a buffer position limit which is the farthest back
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
634 to search, and a flag saying whether to do sentence motion when in a
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
635 comment."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
636 (interactive (list (prefix-numeric-value current-prefix-arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
637 nil t))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
638 (c-beginning-of-statement (- (or count 1)) lim sentence-flag)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
639 (c-keep-region-active))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
640
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
641
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
642 ;; set up electric character functions to work with pending-del,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
643 ;; (a.k.a. delsel) mode. All symbols get the t value except
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 171
diff changeset
644 ;; the functions which delete, which gets 'supersede.
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
645 (mapcar
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
646 (function
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
647 (lambda (sym)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
648 (put sym 'delete-selection t) ; for delsel (Emacs)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
649 (put sym 'pending-delete t))) ; for pending-del (XEmacs)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
650 '(c-electric-pound
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
651 c-electric-brace
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
652 c-electric-slash
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
653 c-electric-star
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
654 c-electric-semi&comma
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
655 c-electric-lt-gt
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
656 c-electric-colon))
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 171
diff changeset
657 (put 'c-electric-delete 'delete-selection 'supersede) ; delsel
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 171
diff changeset
658 (put 'c-electric-delete 'pending-delete 'supersede) ; pending-del
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 171
diff changeset
659 (put 'c-electric-backspace 'delete-selection 'supersede) ; delsel
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 171
diff changeset
660 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
661
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
662
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
663 ;; This is used by indent-for-comment to decide how much to indent a
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
664 ;; comment in C code based on its context.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
665 (defun c-comment-indent ()
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
666 (if (looking-at (concat "^\\(" c-comment-start-regexp "\\)"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
667 0 ;Existing comment at bol stays there.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
668 (let ((opoint (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
669 placeholder)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
670 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
671 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
672 (cond
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
673 ;; CASE 1: A comment following a solitary close-brace should
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
674 ;; have only one space.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
675 ((looking-at (concat "[ \t]*}[ \t]*\\($\\|"
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
676 c-comment-start-regexp
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
677 "\\)"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
678 (search-forward "}")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
679 (1+ (current-column)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
680 ;; CASE 2: 2 spaces after #endif
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
681 ((or (looking-at "^#[ \t]*endif[ \t]*")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
682 (looking-at "^#[ \t]*else[ \t]*"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
683 7)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
684 ;; CASE 3: when comment-column is nil, calculate the offset
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
685 ;; according to c-offsets-alist. E.g. identical to hitting
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
686 ;; TAB.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
687 ((and c-indent-comments-syntactically-p
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
688 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
689 (skip-chars-forward " \t")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
690 (or (looking-at comment-start)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
691 (eolp))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
692 (let ((syntax (c-guess-basic-syntax)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
693 ;; BOGOSITY ALERT: if we're looking at the eol, its
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
694 ;; because indent-for-comment hasn't put the comment-start
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
695 ;; in the buffer yet. this will screw up the syntactic
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
696 ;; analysis so we kludge in the necessary info. Another
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
697 ;; kludge is that if we're at the bol, then we really want
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
698 ;; to ignore any anchoring as specified by
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
699 ;; c-comment-only-line-offset since it doesn't apply here.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
700 (if (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
701 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
702 (skip-chars-forward " \t")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
703 (eolp))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
704 (c-add-syntax 'comment-intro))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
705 (let ((c-comment-only-line-offset
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
706 (if (consp c-comment-only-line-offset)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
707 c-comment-only-line-offset
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
708 (cons c-comment-only-line-offset
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
709 c-comment-only-line-offset))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
710 (apply '+ (mapcar 'c-get-offset syntax)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
711 ;; CASE 4: use comment-column if previous line is a
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
712 ;; comment-only line indented to the left of comment-column
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
713 ((save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
714 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
715 (and (not (bobp))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
716 (forward-line -1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
717 (skip-chars-forward " \t")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
718 (prog1
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
719 (looking-at c-comment-start-regexp)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
720 (setq placeholder (point))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
721 (goto-char placeholder)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
722 (if (< (current-column) comment-column)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
723 comment-column
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
724 (current-column)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
725 ;; CASE 5: If comment-column is 0, and nothing but space
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
726 ;; before the comment, align it at 0 rather than 1.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
727 ((progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
728 (goto-char opoint)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
729 (skip-chars-backward " \t")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
730 (and (= comment-column 0) (bolp)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
731 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
732 ;; CASE 6: indent at comment column except leave at least one
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
733 ;; space.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
734 (t (max (1+ (current-column))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
735 comment-column))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
736 )))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
737
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
738 ;; used by outline-minor-mode
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
739 (defun c-outline-level ()
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
740 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
741 (skip-chars-forward "\t ")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
742 (current-column)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
743
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
744
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
745 (defun c-up-conditional (count)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
746 "Move back to the containing preprocessor conditional, leaving mark behind.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
747 A prefix argument acts as a repeat count. With a negative argument,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
748 move forward to the end of the containing preprocessor conditional.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
749 When going backwards, `#elif' is treated like `#else' followed by
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
750 `#if'. When going forwards, `#elif' is ignored."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
751 (interactive "p")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
752 (c-forward-conditional (- count) t)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
753 (c-keep-region-active))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
754
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
755 (defun c-backward-conditional (count &optional up-flag)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
756 "Move back across a preprocessor conditional, leaving mark behind.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
757 A prefix argument acts as a repeat count. With a negative argument,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
758 move forward across a preprocessor conditional."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
759 (interactive "p")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
760 (c-forward-conditional (- count) up-flag)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
761 (c-keep-region-active))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
762
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
763 (defun c-forward-conditional (count &optional up-flag)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
764 "Move forward across a preprocessor conditional, leaving mark behind.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
765 A prefix argument acts as a repeat count. With a negative argument,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
766 move backward across a preprocessor conditional."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
767 (interactive "p")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
768 (let* ((forward (> count 0))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
769 (increment (if forward -1 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
770 (search-function (if forward 're-search-forward 're-search-backward))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
771 (new))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
772 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
773 (while (/= count 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
774 (let ((depth (if up-flag 0 -1)) found)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
775 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
776 ;; Find the "next" significant line in the proper direction.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
777 (while (and (not found)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
778 ;; Rather than searching for a # sign that
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
779 ;; comes at the beginning of a line aside from
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
780 ;; whitespace, search first for a string
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
781 ;; starting with # sign. Then verify what
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
782 ;; precedes it. This is faster on account of
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
783 ;; the fastmap feature of the regexp matcher.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
784 (funcall search-function
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
785 "#[ \t]*\\(if\\|elif\\|endif\\)"
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
786 nil t))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
787 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
788 ;; Now verify it is really a preproc line.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
789 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
790 (let ((prev depth))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
791 ;; Update depth according to what we found.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
792 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
793 (cond ((looking-at "[ \t]*#[ \t]*endif")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
794 (setq depth (+ depth increment)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
795 ((looking-at "[ \t]*#[ \t]*elif")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
796 (if (and forward (= depth 0))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
797 (setq found (point))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
798 (t (setq depth (- depth increment))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
799 ;; If we are trying to move across, and we find an
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
800 ;; end before we find a beginning, get an error.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
801 (if (and (< prev 0) (< depth prev))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
802 (error (if forward
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
803 "No following conditional at this level"
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
804 "No previous conditional at this level")))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
805 ;; When searching forward, start from next line so
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
806 ;; that we don't find the same line again.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
807 (if forward (forward-line 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
808 ;; If this line exits a level of conditional, exit
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
809 ;; inner loop.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
810 (if (< depth 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
811 (setq found (point))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
812 ;; else
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
813 (if forward (forward-line 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
814 )))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
815 (or found
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
816 (error "No containing preprocessor conditional"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
817 (goto-char (setq new found)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
818 (setq count (+ count increment))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
819 (push-mark)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
820 (goto-char new))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
821 (c-keep-region-active))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
822
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
823
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
824 ;; commands to indent lines, regions, defuns, and expressions
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
825 (defun c-indent-command (&optional whole-exp)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
826 "Indent current line as C code, and/or insert some whitespace.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
827
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
828 If `c-tab-always-indent' is t, always just indent the current line.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
829 If nil, indent the current line only if point is at the left margin or
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
830 in the line's indentation; otherwise insert some whitespace[*]. If
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
831 other than nil or t, then some whitespace[*] is inserted only within
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
832 literals (comments and strings) and inside preprocessor directives,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
833 but the line is always reindented.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
834
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
835 A numeric argument, regardless of its value, means indent rigidly all
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
836 the lines of the expression starting after point so that this line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
837 becomes properly indented. The relative indentation among the lines
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
838 of the expression are preserved.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
839
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
840 [*] The amount and kind of whitespace inserted is controlled by the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
841 variable `c-insert-tab-function', which is called to do the actual
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
842 insertion of whitespace. Normally the function in this variable
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
843 just inserts a tab character, or the equivalent number of spaces,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
844 depending on the variable `indent-tabs-mode'."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
845
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
846 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
847 (let ((bod (c-point 'bod)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
848 (if whole-exp
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
849 ;; If arg, always indent this line as C
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
850 ;; and shift remaining lines of expression the same amount.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
851 (let ((shift-amt (c-indent-line))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
852 beg end)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
853 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
854 (if (eq c-tab-always-indent t)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
855 (beginning-of-line))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
856 (setq beg (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
857 (forward-sexp 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
858 (setq end (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
859 (goto-char beg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
860 (forward-line 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
861 (setq beg (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
862 (if (> end beg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
863 (indent-code-rigidly beg end (- shift-amt) "#")))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
864 ;; No arg supplied, use c-tab-always-indent to determine
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
865 ;; behavior
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
866 (cond
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
867 ;; CASE 1: indent when at column zero or in lines indentation,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
868 ;; otherwise insert a tab
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
869 ((not c-tab-always-indent)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
870 (if (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
871 (skip-chars-backward " \t")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
872 (not (bolp)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
873 (funcall c-insert-tab-function)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
874 (c-indent-line)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
875 ;; CASE 2: just indent the line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
876 ((eq c-tab-always-indent t)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
877 (c-indent-line))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
878 ;; CASE 3: if in a literal, insert a tab, but always indent the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
879 ;; line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
880 (t
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
881 (if (c-in-literal bod)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
882 (funcall c-insert-tab-function))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
883 (c-indent-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
884 )))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
885
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
886 (defun c-indent-exp (&optional shutup-p)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
887 "Indent each line in balanced expression following point.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
888 Optional SHUTUP-P if non-nil, inhibits message printing and error checking."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
889 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
890 (let ((here (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
891 end progress-p)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
892 (unwind-protect
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
893 (let ((c-echo-syntactic-information-p nil) ;keep quiet for speed
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
894 (start (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
895 ;; try to be smarter about finding the range of
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
896 ;; lines to indent. skip all following
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
897 ;; whitespace. failing that, try to find any
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
898 ;; opening brace on the current line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
899 (skip-chars-forward " \t\n")
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
900 (if (memq (char-after) '(?\( ?\[ ?\{))
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
901 (point)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
902 (let ((state (parse-partial-sexp (point)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
903 (c-point 'eol))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
904 (and (nth 1 state)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
905 (goto-char (nth 1 state))
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
906 (memq (char-after) '(?\( ?\[ ?\{))
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
907 (point)))))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
908 ;; find balanced expression end
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
909 (setq end (and (c-safe (progn (forward-sexp 1) t))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
910 (point-marker)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
911 ;; sanity check
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
912 (and (not start)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
913 (not shutup-p)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
914 (error "Cannot find start of balanced expression to indent."))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
915 (and (not end)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
916 (not shutup-p)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
917 (error "Cannot find end of balanced expression to indent."))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
918 (c-progress-init start end 'c-indent-exp)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
919 (setq progress-p t)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
920 (goto-char start)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
921 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
922 (while (< (point) end)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
923 (if (not (looking-at "[ \t]*$"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
924 (c-indent-line))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
925 (c-progress-update)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
926 (forward-line 1)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
927 ;; make sure marker is deleted
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
928 (and end
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
929 (set-marker end nil))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
930 (and progress-p
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
931 (c-progress-fini 'c-indent-exp))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
932 (goto-char here))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
933
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
934 (defun c-indent-defun ()
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
935 "Re-indents the current top-level function def, struct or class declaration."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
936 (interactive)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
937 (let ((here (point-marker))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
938 (c-echo-syntactic-information-p nil)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
939 (brace (c-least-enclosing-brace (c-parse-state))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
940 (if brace
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
941 (goto-char brace)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
942 (beginning-of-defun))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
943 ;; if we're sitting at b-o-b, it might be because there was no
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
944 ;; least enclosing brace and we were sitting on the defun's open
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
945 ;; brace.
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
946 (if (and (bobp) (not (eq (char-after) ?\{)))
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
947 (goto-char here))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
948 ;; if defun-prompt-regexp is non-nil, b-o-d might not leave us at
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
949 ;; the open brace. I consider this an Emacs bug.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
950 (and (boundp 'defun-prompt-regexp)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
951 defun-prompt-regexp
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
952 (looking-at defun-prompt-regexp)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
953 (goto-char (match-end 0)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
954 ;; catch all errors in c-indent-exp so we can 1. give more
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
955 ;; meaningful error message, and 2. restore point
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
956 (unwind-protect
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
957 (c-indent-exp)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
958 (goto-char here)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
959 (set-marker here nil))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
960
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
961 (defun c-indent-region (start end)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
962 ;; Indent every line whose first char is between START and END inclusive.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
963 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
964 (goto-char start)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
965 ;; Advance to first nonblank line.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
966 (skip-chars-forward " \t\n")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
967 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
968 (let (endmark)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
969 (unwind-protect
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
970 (let ((c-tab-always-indent t)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
971 ;; shut up any echo msgs on indiv lines
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
972 (c-echo-syntactic-information-p nil)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
973 fence)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
974 (c-progress-init start end 'c-indent-region)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
975 (setq endmark (copy-marker end))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
976 (while (and (bolp)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
977 (not (eobp))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
978 (< (point) endmark))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
979 ;; update progress
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
980 (c-progress-update)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
981 ;; Indent one line as with TAB.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
982 (let (nextline sexpend sexpbeg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
983 ;; skip blank lines
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
984 (skip-chars-forward " \t\n")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
985 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
986 ;; indent the current line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
987 (c-indent-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
988 (setq fence (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
989 (if (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
990 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
991 (looking-at "[ \t]*#"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
992 (forward-line 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
993 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
994 ;; Find beginning of following line.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
995 (setq nextline (c-point 'bonl))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
996 ;; Find first beginning-of-sexp for sexp extending past
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
997 ;; this line.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
998 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
999 (while (< (point) nextline)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1000 (condition-case nil
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1001 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1002 (forward-sexp 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1003 (setq sexpend (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1004 (error (setq sexpend nil)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1005 (goto-char nextline)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1006 (c-forward-syntactic-ws))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1007 (if sexpend
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1008 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1009 ;; make sure the sexp we found really starts on the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1010 ;; current line and extends past it
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1011 (goto-char sexpend)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1012 (setq sexpend (point-marker))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1013 (c-safe (backward-sexp 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1014 (setq sexpbeg (point))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1015 (if (and sexpbeg (< sexpbeg fence))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1016 (setq sexpbeg fence)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1017 ;; check to see if the next line starts a
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1018 ;; comment-only line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1019 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1020 (forward-line 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1021 (skip-chars-forward " \t")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1022 (if (looking-at c-comment-start-regexp)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1023 (setq sexpbeg (c-point 'bol))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1024 ;; If that sexp ends within the region, indent it all at
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1025 ;; once, fast.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1026 (condition-case nil
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1027 (if (and sexpend
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1028 (> sexpend nextline)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1029 (<= sexpend endmark))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1030 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1031 (goto-char sexpbeg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1032 (c-indent-exp 'shutup)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1033 (c-progress-update)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1034 (goto-char sexpend)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1035 (error
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1036 (goto-char sexpbeg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1037 (c-indent-line)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1038 ;; Move to following line and try again.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1039 (and sexpend
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1040 (markerp sexpend)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1041 (set-marker sexpend nil))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1042 (forward-line 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1043 (setq fence (point))))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1044 (set-marker endmark nil)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1045 (c-progress-fini 'c-indent-region)
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
1046 (c-echo-parsing-error)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1047 ))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1048
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1049 (defun c-mark-function ()
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1050 "Put mark at end of a C, C++, or Objective-C defun, point at beginning."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1051 (interactive)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1052 (let ((here (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1053 ;; there should be a c-point position for 'eod
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1054 (eod (save-excursion (end-of-defun) (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1055 (state (c-parse-state))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1056 brace)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1057 (while state
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1058 (setq brace (car state))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1059 (if (consp brace)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1060 (goto-char (cdr brace))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1061 (goto-char brace))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1062 (setq state (cdr state)))
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
1063 (if (eq (char-after) ?{)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1064 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1065 (forward-line -1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1066 (while (not (or (bobp)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1067 (looking-at "[ \t]*$")))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1068 (forward-line -1)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1069 (forward-line 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1070 (skip-chars-forward " \t\n"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1071 (push-mark here)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1072 (push-mark eod nil t)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1073
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1074
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1075 ;; for progress reporting
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1076 (defvar c-progress-info nil)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1077
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1078 (defun c-progress-init (start end context)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1079 ;; start the progress update messages. if this emacs doesn't have a
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1080 ;; built-in timer, just be dumb about it
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1081 (if (not (fboundp 'current-time))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1082 (message "indenting region... (this may take a while)")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1083 ;; if progress has already been initialized, do nothing. otherwise
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1084 ;; initialize the counter with a vector of:
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1085 ;; [start end lastsec context]
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1086 (if c-progress-info
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1087 ()
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1088 (setq c-progress-info (vector start
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1089 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1090 (goto-char end)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1091 (point-marker))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1092 (nth 1 (current-time))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1093 context))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1094 (message "indenting region..."))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1095
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1096 (defun c-progress-update ()
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1097 ;; update progress
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1098 (if (not (and c-progress-info c-progress-interval))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1099 nil
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1100 (let ((now (nth 1 (current-time)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1101 (start (aref c-progress-info 0))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1102 (end (aref c-progress-info 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1103 (lastsecs (aref c-progress-info 2)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1104 ;; should we update? currently, update happens every 2 seconds,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1105 ;; what's the right value?
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1106 (if (< c-progress-interval (- now lastsecs))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1107 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1108 (message "indenting region... (%d%% complete)"
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1109 (/ (* 100 (- (point) start)) (- end start)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1110 (aset c-progress-info 2 now)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1111 )))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1112
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1113 (defun c-progress-fini (context)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1114 ;; finished
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1115 (if (or (eq context (aref c-progress-info 3))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1116 (eq context t))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1117 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1118 (set-marker (aref c-progress-info 1) nil)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1119 (setq c-progress-info nil)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1120 (message "indenting region...done"))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1121
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1122
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1123
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1124 ;;; This page handles insertion and removal of backslashes for C macros.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1125
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1126 (defun c-backslash-region (from to delete-flag)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1127 "Insert, align, or delete end-of-line backslashes on the lines in the region.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1128 With no argument, inserts backslashes and aligns existing backslashes.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1129 With an argument, deletes the backslashes.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1130
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1131 This function does not modify blank lines at the start of the region.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1132 If the region ends at the start of a line, it always deletes the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1133 backslash (if any) at the end of the previous line.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1134
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1135 You can put the region around an entire macro definition and use this
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1136 command to conveniently insert and align the necessary backslashes."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1137 (interactive "r\nP")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1138 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1139 (goto-char from)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1140 (let ((column c-backslash-column)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1141 (endmark (make-marker)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1142 (move-marker endmark to)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1143 ;; Compute the smallest column number past the ends of all the lines.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1144 (if (not delete-flag)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1145 (while (< (point) to)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1146 (end-of-line)
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
1147 (if (eq (char-before) ?\\)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1148 (progn (forward-char -1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1149 (skip-chars-backward " \t")))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1150 (setq column (max column (1+ (current-column))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1151 (forward-line 1)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1152 ;; Adjust upward to a tab column, if that doesn't push past the margin.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1153 (if (> (% column tab-width) 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1154 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1155 (if (< adjusted (window-width))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1156 (setq column adjusted))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1157 ;; Don't modify blank lines at start of region.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1158 (goto-char from)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1159 (while (and (< (point) endmark) (eolp))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1160 (forward-line 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1161 ;; Add or remove backslashes on all the lines.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1162 (while (< (point) endmark)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1163 (if (and (not delete-flag)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1164 ;; Un-backslashify the last line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1165 ;; if the region ends right at the start of the next line.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1166 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1167 (forward-line 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1168 (< (point) endmark)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1169 (c-append-backslash column)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1170 (c-delete-backslash))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1171 (forward-line 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1172 (move-marker endmark nil)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1173 (c-keep-region-active))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1174
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1175 (defun c-append-backslash (column)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1176 (end-of-line)
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
1177 (if (eq (char-before) ?\\)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1178 (progn (forward-char -1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1179 (delete-horizontal-space)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1180 (indent-to column))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1181 (indent-to column)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1182 (insert "\\")))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1183
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1184 (defun c-delete-backslash ()
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1185 (end-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1186 (or (bolp)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1187 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1188 (forward-char -1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1189 (if (looking-at "\\\\")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1190 (delete-region (1+ (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1191 (progn (skip-chars-backward " \t") (point)))))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1192
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1193
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1194 (defun c-fill-paragraph (&optional arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1195 "Like \\[fill-paragraph] but handles C and C++ style comments.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1196 If any of the current line is a comment or within a comment,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1197 fill the comment or the paragraph of it that point is in,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1198 preserving the comment indentation or line-starting decorations.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1199
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1200 Optional prefix ARG means justify paragraph as well."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1201 (interactive "P")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1202 (let* (comment-start-place
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1203 (first-line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1204 ;; Check for obvious entry to comment.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1205 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1206 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1207 (skip-chars-forward " \t\n")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1208 (and (looking-at comment-start-skip)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1209 (setq comment-start-place (point)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1210 (re1 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1211 (if (and c-double-slash-is-comments-p
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1212 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1213 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1214 (looking-at ".*//")))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1215 (let ((fill-prefix fill-prefix)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1216 ;; Lines containing just a comment start or just an end
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1217 ;; should not be filled into paragraphs they are next
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1218 ;; to.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1219 (paragraph-start (concat paragraph-start re1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1220 (paragraph-separate (concat paragraph-separate re1)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1221 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1222 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1223 ;; Move up to first line of this comment.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1224 (while (and (not (bobp))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1225 (looking-at "[ \t]*//[ \t]*[^ \t\n]"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1226 (forward-line -1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1227 (if (not (looking-at ".*//[ \t]*[^ \t\n]"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1228 (forward-line 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1229 ;; Find the comment start in this line.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1230 (re-search-forward "[ \t]*//[ \t]*")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1231 ;; Set the fill-prefix to be what all lines except the first
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1232 ;; should start with. But do not alter a user set fill-prefix.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1233 (if (null fill-prefix)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1234 (setq fill-prefix (buffer-substring (match-beginning 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1235 (match-end 0))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1236 (save-restriction
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1237 ;; Narrow down to just the lines of this comment.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1238 (narrow-to-region (c-point 'bol)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1239 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1240 (forward-line 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1241 (while (looking-at fill-prefix)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1242 (forward-line 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1243 (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1244 (fill-paragraph arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1245 t)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1246 ;; else C style comments
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1247 (if (or first-line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1248 ;; t if we enter a comment between start of function and
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1249 ;; this line.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1250 (eq (c-in-literal) 'c)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1251 ;; t if this line contains a comment starter.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1252 (setq first-line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1253 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1254 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1255 (prog1
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1256 (re-search-forward comment-start-skip
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1257 (save-excursion (end-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1258 (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1259 t)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1260 (setq comment-start-place (point))))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1261 ;; Inside a comment: fill one comment paragraph.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1262 (let ((fill-prefix
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1263 ;; The prefix for each line of this paragraph
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1264 ;; is the appropriate part of the start of this line,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1265 ;; up to the column at which text should be indented.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1266 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1267 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1268 (if (looking-at "[ \t]*/\\*.*\\*/")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1269 (progn (re-search-forward comment-start-skip)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1270 (make-string (current-column) ?\ ))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1271 (if first-line (forward-line 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1272
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1273 (let ((line-width (progn (end-of-line) (current-column))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1274 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1275 (prog1
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1276 (buffer-substring
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1277 (point)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1278
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1279 ;; How shall we decide where the end of the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1280 ;; fill-prefix is?
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1281 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1282 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1283 (skip-chars-forward " \t*" (c-point 'eol))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1284 ;; kludge alert, watch out for */, in
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1285 ;; which case fill-prefix should *not*
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1286 ;; be "*"!
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
1287 (if (and (eq (char-after) ?/)
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 165
diff changeset
1288 (eq (char-before) ?*))
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1289 (forward-char -1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1290 (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1291
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1292 ;; If the comment is only one line followed
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1293 ;; by a blank line, calling move-to-column
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1294 ;; above may have added some spaces and tabs
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1295 ;; to the end of the line; the fill-paragraph
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1296 ;; function will then delete it and the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1297 ;; newline following it, so we'll lose a
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1298 ;; blank line when we shouldn't. So delete
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1299 ;; anything move-to-column added to the end
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1300 ;; of the line. We record the line width
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1301 ;; instead of the position of the old line
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1302 ;; end because move-to-column might break a
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1303 ;; tab into spaces, and the new characters
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1304 ;; introduced there shouldn't be deleted.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1305
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1306 ;; If you can see a better way to do this,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1307 ;; please make the change. This seems very
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1308 ;; messy to me.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1309 (delete-region (progn (move-to-column line-width)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1310 (point))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1311 (progn (end-of-line) (point))))))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1312
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1313 ;; Lines containing just a comment start or just an end
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1314 ;; should not be filled into paragraphs they are next
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1315 ;; to.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1316 (paragraph-start (concat paragraph-start re1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1317 (paragraph-separate (concat paragraph-separate re1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1318 (chars-to-delete 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1319 )
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1320 (save-restriction
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1321 ;; Don't fill the comment together with the code
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1322 ;; following it. So temporarily exclude everything
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1323 ;; before the comment start, and everything after the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1324 ;; line where the comment ends. If comment-start-place
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1325 ;; is non-nil, the comment starter is there. Otherwise,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1326 ;; point is inside the comment.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1327 (narrow-to-region (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1328 (if comment-start-place
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1329 (goto-char comment-start-place)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1330 (search-backward "/*"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1331 (if (and (not c-hanging-comment-starter-p)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1332 (looking-at
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1333 (concat c-comment-start-regexp
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1334 "[ \t]*$")))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1335 (forward-line 1))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1336 ;; Protect text before the comment
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1337 ;; start by excluding it. Add
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1338 ;; spaces to bring back proper
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1339 ;; indentation of that point.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1340 (let ((column (current-column)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1341 (prog1 (point)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1342 (setq chars-to-delete column)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1343 (insert-char ?\ column))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1344 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1345 (if comment-start-place
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1346 (goto-char (+ comment-start-place 2)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1347 (search-forward "*/" nil 'move)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1348 (forward-line 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1349 (point)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1350 (fill-paragraph arg)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1351 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1352 ;; Delete the chars we inserted to avoid clobbering
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1353 ;; the stuff before the comment start.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1354 (goto-char (point-min))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1355 (if (> chars-to-delete 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1356 (delete-region (point) (+ (point) chars-to-delete)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1357 ;; Find the comment ender (should be on last line of
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1358 ;; buffer, given the narrowing) and don't leave it on
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1359 ;; its own line, unless that's the style that's desired.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1360 (goto-char (point-max))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1361 (forward-line -1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1362 (search-forward "*/" nil 'move)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1363 (beginning-of-line)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1364 (if (and c-hanging-comment-ender-p
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1365 (looking-at "[ \t]*\\*/"))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1366 ;(delete-indentation)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1367 (let ((fill-column (+ fill-column 9999)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1368 (forward-line -1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1369 (fill-region-as-paragraph (point) (point-max))))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1370 t)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1371
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1372
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1373 (provide 'cc-cmds)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
1374 ;;; cc-cmds.el ends here