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