Mercurial > emacs
comparison lisp/progmodes/c-mode.el @ 837:a8aef92e0025
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Wed, 22 Jul 1992 01:36:20 +0000 |
parents | 4f28bd14272c |
children | 20674ae6bf52 |
comparison
equal
deleted
inserted
replaced
836:a7cc5061c4de | 837:a8aef92e0025 |
---|---|
127 (define-key c-mode-map "{" 'electric-c-semi)") | 127 (define-key c-mode-map "{" 'electric-c-semi)") |
128 | 128 |
129 (defconst c-tab-always-indent t | 129 (defconst c-tab-always-indent t |
130 "*Non-nil means TAB in C mode should always reindent the current line, | 130 "*Non-nil means TAB in C mode should always reindent the current line, |
131 regardless of where in the line point is when the TAB command is used.") | 131 regardless of where in the line point is when the TAB command is used.") |
132 | |
133 (defun set-c-style (&optional style) | |
134 "Set up the c-mode style variables from the c-style variable or if | |
135 STYLE argument is given, use that. It makes the c indentation style | |
136 variables buffer local." | |
137 (interactive) | |
138 (let ((c-styles (mapcar 'car c-style-alist))) | |
139 (if (interactive-p) | |
140 (setq style | |
141 (let ((style-string ; get style name with completion | |
142 (completing-read | |
143 (format "Set c mode indentation style to (default %s): " | |
144 default-c-style) | |
145 (vconcat c-styles) | |
146 (function (lambda (arg) (memq arg c-styles))) | |
147 ))) | |
148 (if (string-equal "" style-string) | |
149 default-c-style | |
150 (intern style-string)) | |
151 ))) | |
152 (setq style (or style c-style)) ; use c-style if style is nil | |
153 (make-local-variable 'c-style) | |
154 (if (memq style c-styles) | |
155 (setq c-style style) | |
156 (error "Bad c style: %s" style) | |
157 ) | |
158 (message "c-style: %s" c-style) | |
159 ; finally, set the indentation style variables making each one local | |
160 (mapcar (function (lambda (c-style-pair) | |
161 (make-local-variable (car c-style-pair)) | |
162 (set (car c-style-pair) | |
163 (cdr c-style-pair)))) | |
164 (cdr (assq c-style c-style-alist))) | |
165 c-style | |
166 ) | |
167 ) | |
168 | 132 |
169 (defun c-mode () | 133 (defun c-mode () |
170 "Major mode for editing C code. | 134 "Major mode for editing C code. |
171 Expression and list commands understand all C brackets. | 135 Expression and list commands understand all C brackets. |
172 Tab indents for C code. | 136 Tab indents for C code. |
1064 (and (bolp) (not (eolp)) | 1028 (and (bolp) (not (eolp)) |
1065 (c-indent-line)) | 1029 (c-indent-line)) |
1066 (indent-c-exp endmark) | 1030 (indent-c-exp endmark) |
1067 (set-marker endmark nil)))) | 1031 (set-marker endmark nil)))) |
1068 | 1032 |
1069 (defun set-c-style (style) | 1033 (defun set-c-style (style &optional global) |
1070 "Set C-mode variables to use one of several different indentation styles. | 1034 "Set C-mode variables to use one of several different indentation styles. |
1071 Takes one argument, a string representing the desired style. | 1035 The arguments are a string representing the desired style |
1072 Available styles are GNU, K&R, BSD and Whitesmith." | 1036 and a flag which, if non-nil, means to set the style globally. |
1037 \(Interactively, the flag comes from the prefix argument.) | |
1038 Available styles are GNU, K&R, BSD and Whitesmith. | |
1073 (interactive (list (completing-read "Use which C indentation style? " | 1039 (interactive (list (completing-read "Use which C indentation style? " |
1074 c-style-alist nil t))) | 1040 c-style-alist nil t) |
1041 current-prefix-arg)) | |
1075 (let ((vars (cdr (assoc style c-style-alist)))) | 1042 (let ((vars (cdr (assoc style c-style-alist)))) |
1076 (if vars | 1043 (or vars |
1077 (if (interactive-p) (message "Using %s C indentation style" style)) | 1044 (error "Invalid C indentation style `%s'" style)) |
1078 (error "Bogus style type, \"%s\"" style)) | |
1079 (while vars | 1045 (while vars |
1046 (or global | |
1047 (make-local-variable (car (car vars)))) | |
1080 (set (car (car vars)) (cdr (car vars))) | 1048 (set (car (car vars)) (cdr (car vars))) |
1081 (setq vars (cdr vars))))) | 1049 (setq vars (cdr vars))))) |
1050 | |
1051 ;;; This page handles insertion and removal of backslashes for C macros. | |
1052 | |
1053 (defvar c-backslash-column 48 | |
1054 "*Minimum column for end-of-line backslashes of macro definitions.") | |
1055 | |
1056 (defun c-backslash-region (from to delete-flag) | |
1057 "Insert, align, or delete end-of-line backslashes on the lines in the region. | |
1058 With no argument, inserts backslashes and aligns existing backslashes. | |
1059 With an argument, deletes the backslashes. | |
1060 | |
1061 This function does not modify the last line of the region if the region ends | |
1062 right at the start of the following line; it does not modify blank lines | |
1063 at the start of the region. So you can put the region around an entire macro | |
1064 definition and conveniently use this command." | |
1065 (interactive "r\nP") | |
1066 (save-excursion | |
1067 (goto-char from) | |
1068 (let ((column c-backslash-column) | |
1069 (endmark (make-marker))) | |
1070 (move-marker endmark to) | |
1071 ;; Compute the smallest column number past the ends of all the lines. | |
1072 (if (not delete-flag) | |
1073 (while (< (point) to) | |
1074 (end-of-line) | |
1075 (if (= (preceding-char) ?\\) | |
1076 (progn (forward-char -1) | |
1077 (skip-chars-backward " \t"))) | |
1078 (setq column (max column (1+ (current-column)))) | |
1079 (forward-line 1))) | |
1080 ;; Adjust upward to a tab column, if that doesn't push past the margin. | |
1081 (if (> (% column tab-width) 0) | |
1082 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width))) | |
1083 (if (< adjusted (window-width)) | |
1084 (setq column adjusted)))) | |
1085 ;; Don't modify blank lines at start of region. | |
1086 (goto-char from) | |
1087 (while (and (< (point) endmark) (eolp)) | |
1088 (forward-line 1)) | |
1089 ;; Add or remove backslashes on all the lines. | |
1090 (while (and (< (point) endmark) | |
1091 ;; Don't backslashify the last line | |
1092 ;; if the region ends right at the start of the next line. | |
1093 (save-excursion | |
1094 (forward-line 1) | |
1095 (< (point) endmark))) | |
1096 (if (not delete-flag) | |
1097 (c-append-backslash column) | |
1098 (c-delete-backslash)) | |
1099 (forward-line 1)) | |
1100 (move-marker endmark nil)))) | |
1101 | |
1102 (defun c-append-backslash (column) | |
1103 (end-of-line) | |
1104 ;; Note that "\\\\" is needed to get one backslash. | |
1105 (if (= (preceding-char) ?\\) | |
1106 (progn (forward-char -1) | |
1107 (delete-horizontal-space) | |
1108 (indent-to column)) | |
1109 (indent-to column) | |
1110 (insert "\\"))) | |
1111 | |
1112 (defun c-delete-backslash () | |
1113 (end-of-line) | |
1114 (forward-char -1) | |
1115 (if (looking-at "\\\\") | |
1116 (delete-region (1+ (point)) | |
1117 (progn (skip-chars-backward " \t") (point))))) | |
1082 | 1118 |
1083 ;;; c-mode.el ends here | 1119 ;;; c-mode.el ends here |