24 |
|
|
25 |
;;; Commentary: |
;;; Commentary: |
26 |
|
|
27 |
;; 1987 Dave Detlefs <dld@cs.cmu.edu> |
;; 1987 Dave Detlefs <dld@cs.cmu.edu> |
28 |
;; and Stewart Clamen <clamen@cs.cmu.edu>. |
;; and Stewart Clamen <clamen@cs.cmu.edu>. |
29 |
;; Done by fairly faithful modification of: |
;; Done by fairly faithful modification of: |
30 |
|
|
33 |
;; Feb, 1990 (Dave Detlefs, dld@cs.cmu.edu) |
;; Feb, 1990 (Dave Detlefs, dld@cs.cmu.edu) |
34 |
;; Fixed electric-c++-terminator to handle double colons, at the |
;; Fixed electric-c++-terminator to handle double colons, at the |
35 |
;; request of John Hagerman. |
;; request of John Hagerman. |
36 |
;; |
;; |
37 |
;; Jan, 1990 (Doug Lea, dl@oswego.edu) |
;; Jan, 1990 (Doug Lea, dl@oswego.edu) |
38 |
;; Replaced c++-comment-region and c++-uncomment-region with |
;; Replaced c++-comment-region and c++-uncomment-region with |
39 |
;; versions from Igor Metz that avoid potential infinite loops. |
;; versions from Igor Metz that avoid potential infinite loops. |
40 |
;; |
;; |
41 |
;; Oct, 1989 (Dave Detlefs, dld@cs.cmu.edu) |
;; Oct, 1989 (Dave Detlefs, dld@cs.cmu.edu) |
42 |
;; Added contribution from Igor Metz <metz@iam.unibe.ch>: |
;; Added contribution from Igor Metz <metz@iam.unibe.ch>: |
43 |
;; functions c++-comment-region and c++-uncomment-region and |
;; functions c++-comment-region and c++-uncomment-region and |
44 |
;; corresponding key-binding. |
;; corresponding key-binding. |
45 |
;; Also fixed bug in indentation of second line after an empty |
;; Also fixed bug in indentation of second line after an empty |
46 |
;; arglist with empty-arglist non-null. |
;; arglist with empty-arglist non-null. |
47 |
;; |
;; |
48 |
;; Sept, 1989 (Glen Ditchfield, gjditchfield@violet.uwaterloo.ca): |
;; Sept, 1989 (Glen Ditchfield, gjditchfield@violet.uwaterloo.ca): |
49 |
;; Textual changes to more closely imitate Emacs 18.55's c-mode. |
;; Textual changes to more closely imitate Emacs 18.55's c-mode. |
50 |
;; Fixed handling of "default:", where ":" was the last character in the |
;; Fixed handling of "default:", where ":" was the last character in the |
180 |
"\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>" |
"\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>" |
181 |
"\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; type specs; there can be no |
"\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; type specs; there can be no |
182 |
"\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; more than 3 tokens, right? |
"\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; more than 3 tokens, right? |
183 |
|
|
184 |
"\\(" ; last type spec including */& |
"\\(" ; last type spec including */& |
185 |
"[a-zA-Z0-9_:]+" |
"[a-zA-Z0-9_:]+" |
186 |
"\\([ \t]*[*&]+[ \t]*\\|[ \t]+\\)" ; either pointer/ref sign or whitespace |
"\\([ \t]*[*&]+[ \t]*\\|[ \t]+\\)" ; either pointer/ref sign or whitespace |
187 |
"\\)?" ; if there is a last type spec |
"\\)?" ; if there is a last type spec |
188 |
"\\(" ; name; take that into the imenu entry |
"\\(" ; name; take that into the imenu entry |
189 |
"[a-zA-Z0-9_:~]+" ; member function, ctor or dtor... |
"[a-zA-Z0-9_:~]+" ; member function, ctor or dtor... |
190 |
; (may not contain * because then |
; (may not contain * because then |
191 |
; "a::operator char*" would become "char*"!) |
; "a::operator char*" would become "char*"!) |
192 |
"\\|" |
"\\|" |
193 |
"\\([a-zA-Z0-9_:~]*::\\)?operator" |
"\\([a-zA-Z0-9_:~]*::\\)?operator" |
198 |
; catch cases with () inside the parentheses |
; catch cases with () inside the parentheses |
199 |
; surrounding the parameters |
; surrounding the parameters |
200 |
; (like "int foo(int a=bar()) {...}" |
; (like "int foo(int a=bar()) {...}" |
201 |
|
|
202 |
) 6) |
) 6) |
203 |
("Class" |
("Class" |
204 |
,(concat |
,(concat |
205 |
"^" ; beginning of line is required |
"^" ; beginning of line is required |
206 |
"\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>" |
"\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>" |
207 |
"class[ \t]+" |
"class[ \t]+" |
212 |
;; Uncomment if you want to find these too. It will be a bit slower gathering |
;; Uncomment if you want to find these too. It will be a bit slower gathering |
213 |
;; the indexes. |
;; the indexes. |
214 |
; ("Prototypes" |
; ("Prototypes" |
215 |
; (, |
; (, |
216 |
; (concat |
; (concat |
217 |
; "^" ; beginning of line is required |
; "^" ; beginning of line is required |
218 |
; "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>" |
; "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>" |
219 |
; "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; type specs; there can be no |
; "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; type specs; there can be no |
220 |
; "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; more than 3 tokens, right? |
; "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; more than 3 tokens, right? |
221 |
|
|
222 |
; "\\(" ; last type spec including */& |
; "\\(" ; last type spec including */& |
223 |
; "[a-zA-Z0-9_:]+" |
; "[a-zA-Z0-9_:]+" |
224 |
; "\\([ \t]*[*&]+[ \t]*\\|[ \t]+\\)" ; either pointer/ref sign or whitespace |
; "\\([ \t]*[*&]+[ \t]*\\|[ \t]+\\)" ; either pointer/ref sign or whitespace |
225 |
; "\\)?" ; if there is a last type spec |
; "\\)?" ; if there is a last type spec |
226 |
; "\\(" ; name; take that into the imenu entry |
; "\\(" ; name; take that into the imenu entry |
227 |
; "[a-zA-Z0-9_:~]+" ; member function, ctor or dtor... |
; "[a-zA-Z0-9_:~]+" ; member function, ctor or dtor... |
228 |
; ; (may not contain * because then |
; ; (may not contain * because then |
229 |
; ; "a::operator char*" would become "char*"!) |
; ; "a::operator char*" would become "char*"!) |
230 |
; "\\|" |
; "\\|" |
231 |
; "\\([a-zA-Z0-9_:~]*::\\)?operator" |
; "\\([a-zA-Z0-9_:~]*::\\)?operator" |
235 |
; ; the (...) Can't |
; ; the (...) Can't |
236 |
; ; catch cases with () inside the parentheses |
; ; catch cases with () inside the parentheses |
237 |
; ; surrounding the parameters |
; ; surrounding the parameters |
238 |
; ; (like "int foo(int a=bar());" |
; ; (like "int foo(int a=bar());" |
239 |
; )) 6) |
; )) 6) |
240 |
; ("Struct" |
; ("Struct" |
241 |
; (, (concat |
; (, (concat |
242 |
; "^" ; beginning of line is required |
; "^" ; beginning of line is required |
530 |
(current-indentation)))) |
(current-indentation)))) |
531 |
((looking-at "friend\[ \t]") |
((looking-at "friend\[ \t]") |
532 |
(setq indent (+ indent c++-friend-offset))) |
(setq indent (+ indent c++-friend-offset))) |
533 |
((= (following-char) ?}) |
((= (following-char) ?\}) |
534 |
(setq indent (- indent c-indent-level))) |
(setq indent (- indent c-indent-level))) |
535 |
((= (following-char) ?{) |
((= (following-char) ?\{) |
536 |
(setq indent (+ indent c-brace-offset)))))) |
(setq indent (+ indent c-brace-offset)))))) |
537 |
(skip-chars-forward " \t") |
(skip-chars-forward " \t") |
538 |
(setq shift-amt (- indent (current-column))) |
(setq shift-amt (- indent (current-column))) |
579 |
;; It is assumed that arg decls and member inits are not mixed. |
;; It is assumed that arg decls and member inits are not mixed. |
580 |
(goto-char indent-point) |
(goto-char indent-point) |
581 |
(skip-chars-forward " \t") |
(skip-chars-forward " \t") |
582 |
(if (= (following-char) ?{) |
(if (= (following-char) ?\{) |
583 |
0 ; Unless it starts a function body |
0 ; Unless it starts a function body |
584 |
(c++-backward-to-noncomment (or parse-start (point-min))) |
(c++-backward-to-noncomment (or parse-start (point-min))) |
585 |
(if (= (preceding-char) ?\)) |
(if (= (preceding-char) ?\)) |
591 |
c-argdecl-indent)) |
c-argdecl-indent)) |
592 |
(if (= (preceding-char) ?\;) |
(if (= (preceding-char) ?\;) |
593 |
(backward-char 1)) |
(backward-char 1)) |
594 |
(if (= (preceding-char) ?}) |
(if (= (preceding-char) ?\}) |
595 |
0 |
0 |
596 |
(if (= (preceding-char) ?\)) |
(if (= (preceding-char) ?\)) |
597 |
(forward-list -1)) |
(forward-list -1)) |
607 |
(current-column))) |
(current-column))) |
608 |
(current-indentation))) |
(current-indentation))) |
609 |
))) |
))) |
610 |
((/= (char-after containing-sexp) ?{) |
((/= (char-after containing-sexp) ?\{) |
611 |
;; line is expression, not statement: |
;; line is expression, not statement: |
612 |
;; indent to just after the surrounding open -- unless |
;; indent to just after the surrounding open -- unless |
613 |
;; empty arg list, in which case we do what |
;; empty arg list, in which case we do what |
647 |
(save-excursion |
(save-excursion |
648 |
(goto-char indent-point) |
(goto-char indent-point) |
649 |
(skip-chars-forward " \t") |
(skip-chars-forward " \t") |
650 |
(not (= (following-char) ?})))) |
(not (= (following-char) ?\})))) |
651 |
;; This line is continuation of preceding line's statement; |
;; This line is continuation of preceding line's statement; |
652 |
;; indent c-continued-statement-offset more than the |
;; indent c-continued-statement-offset more than the |
653 |
;; previous line of the statement. |
;; previous line of the statement. |
656 |
(+ c-continued-statement-offset (current-column) |
(+ c-continued-statement-offset (current-column) |
657 |
(if (save-excursion (goto-char indent-point) |
(if (save-excursion (goto-char indent-point) |
658 |
(skip-chars-forward " \t") |
(skip-chars-forward " \t") |
659 |
(eq (following-char) ?{)) |
(eq (following-char) ?\{)) |
660 |
c-continued-brace-offset 0))) |
c-continued-brace-offset 0))) |
661 |
;; This line starts a new statement. |
;; This line starts a new statement. |
662 |
;; Position following last unclosed open. |
;; Position following last unclosed open. |
688 |
;; The first following code counts |
;; The first following code counts |
689 |
;; if it is before the line we want to indent. |
;; if it is before the line we want to indent. |
690 |
(and (< (point) indent-point) |
(and (< (point) indent-point) |
691 |
(- |
(- |
692 |
(if (> colon-line-end (point)) |
(if (> colon-line-end (point)) |
693 |
(- (current-indentation) c-label-offset) |
(- (current-indentation) c-label-offset) |
694 |
(current-column)) |
(current-column)) |
719 |
(if (eq (preceding-char) ?\)) |
(if (eq (preceding-char) ?\)) |
720 |
(forward-sexp -1)) |
(forward-sexp -1)) |
721 |
;; Get initial indentation of the line we are on. |
;; Get initial indentation of the line we are on. |
722 |
(current-indentation))))))))))) |
(current-indentation)))))))))) |
723 |
|
|
724 |
(defun c++-backward-to-noncomment (lim) |
(defun c++-backward-to-noncomment (lim) |
725 |
(let (opoint stop) |
(let (opoint stop) |
815 |
;; Just started a new nesting level. |
;; Just started a new nesting level. |
816 |
;; Compute the standard indent for this level. |
;; Compute the standard indent for this level. |
817 |
(let (val) |
(let (val) |
818 |
(if (= (char-after (car contain-stack)) ?{) |
(if (= (char-after (car contain-stack)) ?\{) |
819 |
(save-excursion |
(save-excursion |
820 |
(goto-char (car contain-stack)) |
(goto-char (car contain-stack)) |
821 |
(setq val (calculate-c-indent-after-brace))) |
(setq val (calculate-c-indent-after-brace))) |
878 |
(point)) t) |
(point)) t) |
879 |
(progn |
(progn |
880 |
(indent-for-comment) |
(indent-for-comment) |
881 |
(beginning-of-line))))))))))) |
(beginning-of-line)))))))))) |
882 |
|
|
883 |
(defun fill-c++-comment () |
(defun fill-c++-comment () |
884 |
"Fill a comment contained in consecutive lines containing point. |
"Fill a comment contained in consecutive lines containing point. |
941 |
;; (defvar c++-match-header-strongly nil |
;; (defvar c++-match-header-strongly nil |
942 |
;; "*If nil, use `c++-defun-header-weak' to identify beginning of definitions. |
;; "*If nil, use `c++-defun-header-weak' to identify beginning of definitions. |
943 |
;; If non-nil, use `c++-defun-header-strong'.") |
;; If non-nil, use `c++-defun-header-strong'.") |
944 |
;; |
;; |
945 |
;; (defvar c++-defun-header-strong-struct-equivs "\\(class\\|struct\\|enum\\)" |
;; (defvar c++-defun-header-strong-struct-equivs "\\(class\\|struct\\|enum\\)" |
946 |
;; "Regexp to match names of structure declaration blocks in C++.") |
;; "Regexp to match names of structure declaration blocks in C++.") |
947 |
;; |
;; |
948 |
;; (defconst c++-defun-header-strong |
;; (defconst c++-defun-header-strong |
949 |
;; (let* |
;; (let* |
950 |
;; (; valid identifiers |
;; (; valid identifiers |
953 |
;; ;; to be |
;; ;; to be |
954 |
;; ;; (id "\\(_\\|\\w\\)+") |
;; ;; (id "\\(_\\|\\w\\)+") |
955 |
;; ;; things no longer work right. Try it and see! |
;; ;; things no longer work right. Try it and see! |
956 |
;; |
;; |
957 |
;; ; overloadable operators |
;; ; overloadable operators |
958 |
;; (op-sym1 |
;; (op-sym1 |
959 |
;; "[-+*/%^&|~!=<>]\\|[-+*/%^&|<>=!]=\\|<<=?\\|>>=?") |
;; "[-+*/%^&|~!=<>]\\|[-+*/%^&|<>=!]=\\|<<=?\\|>>=?") |
960 |
;; (op-sym2 |
;; (op-sym2 |
961 |
;; "&&\\|||\\|\\+\\+\\|--\\|()\\|\\[\\]") |
;; "&&\\|||\\|\\+\\+\\|--\\|()\\|\\[\\]") |
962 |
;; (op-sym (concat "\\(" op-sym1 "\\|" op-sym2 "\\)")) |
;; (op-sym (concat "\\(" op-sym1 "\\|" op-sym2 "\\)")) |
963 |
;; ; whitespace |
;; ; whitespace |
964 |
;; (middle "[^\\*]*\\(\\*+[^/\\*][^\\*]*\\)*") |
;; (middle "[^\\*]*\\(\\*+[^/\\*][^\\*]*\\)*") |
993 |
;; wh-nec id wh-opt inherit "?" wh-opt "{"))) |
;; wh-nec id wh-opt inherit "?" wh-opt "{"))) |
994 |
;; (concat "^\\(" func-header "\\|" cs-header "\\)")) |
;; (concat "^\\(" func-header "\\|" cs-header "\\)")) |
995 |
;; "Strongly-defined regexp to match beginning of structure or function def.") |
;; "Strongly-defined regexp to match beginning of structure or function def.") |
996 |
;; |
;; |
997 |
;; |
;; |
998 |
;; ;; This part has to do with recognizing defuns. |
;; ;; This part has to do with recognizing defuns. |
999 |
;; |
;; |
1000 |
;; ;; The weak convention we will use is that a defun begins any time |
;; ;; The weak convention we will use is that a defun begins any time |
1001 |
;; ;; there is a left curly brace, or some identifier on the left margin, |
;; ;; there is a left curly brace, or some identifier on the left margin, |
1002 |
;; ;; followed by a left curly somewhere on the line. (This will also |
;; ;; followed by a left curly somewhere on the line. (This will also |
1003 |
;; ;; incorrectly match some continued strings, but this is after all |
;; ;; incorrectly match some continued strings, but this is after all |
1004 |
;; ;; just a weak heuristic.) Suggestions for improvement (short of the |
;; ;; just a weak heuristic.) Suggestions for improvement (short of the |
1005 |
;; ;; strong scheme shown above) are welcomed. |
;; ;; strong scheme shown above) are welcomed. |
1006 |
;; |
;; |
1007 |
;; (defconst c++-defun-header-weak "^{\\|^[_a-zA-Z].*{" |
;; (defconst c++-defun-header-weak "^{\\|^[_a-zA-Z].*{" |
1008 |
;; "Weakly-defined regexp to match beginning of structure or function def.") |
;; "Weakly-defined regexp to match beginning of structure or function def.") |
1009 |
;; |
;; |
1010 |
;; (defun c++-beginning-of-defun (arg) |
;; (defun c++-beginning-of-defun (arg) |
1011 |
;; (interactive "p") |
;; (interactive "p") |
1012 |
;; (let ((c++-defun-header (if c++-match-header-strongly |
;; (let ((c++-defun-header (if c++-match-header-strongly |
1033 |
;; (forward-char (if (< arg 0) 1 -1))) |
;; (forward-char (if (< arg 0) 1 -1))) |
1034 |
;; (and (re-search-backward c++-defun-header nil 'move (or arg 1)) |
;; (and (re-search-backward c++-defun-header nil 'move (or arg 1)) |
1035 |
;; (goto-char (match-beginning 0))))))) |
;; (goto-char (match-beginning 0))))))) |
1036 |
;; |
;; |
1037 |
;; |
;; |
1038 |
;; (defun c++-end-of-defun (arg) |
;; (defun c++-end-of-defun (arg) |
1039 |
;; (interactive "p") |
;; (interactive "p") |
1040 |
;; (let ((c++-defun-header (if c++-match-header-strongly |
;; (let ((c++-defun-header (if c++-match-header-strongly |
1044 |
;; nil |
;; nil |
1045 |
;; (if (and (> arg 0) (looking-at c++-defun-header)) (forward-char 1)) |
;; (if (and (> arg 0) (looking-at c++-defun-header)) (forward-char 1)) |
1046 |
;; (let ((pos (point))) |
;; (let ((pos (point))) |
1047 |
;; (c++-beginning-of-defun |
;; (c++-beginning-of-defun |
1048 |
;; (if (< arg 0) |
;; (if (< arg 0) |
1049 |
;; (- (- arg (if (eobp) 0 1))) |
;; (- (- arg (if (eobp) 0 1))) |
1050 |
;; arg)) |
;; arg)) |
1054 |
;; (progn (forward-char -1) |
;; (progn (forward-char -1) |
1055 |
;; (forward-sexp) |
;; (forward-sexp) |
1056 |
;; (beginning-of-line 2))) |
;; (beginning-of-line 2))) |
1057 |
;; (if (and (= pos (point)) |
;; (if (and (= pos (point)) |
1058 |
;; (re-search-forward c++-defun-header nil 'move)) |
;; (re-search-forward c++-defun-header nil 'move)) |
1059 |
;; (c++-end-of-defun 1)))) |
;; (c++-end-of-defun 1)))) |
1060 |
;; t))) |
;; t))) |
1061 |
;; |
;; |
1062 |
;; (defun c++-indent-defun () |
;; (defun c++-indent-defun () |
1063 |
;; "Indents the current function definition, struct or class declaration." |
;; "Indents the current function definition, struct or class declaration." |
1064 |
;; (interactive) |
;; (interactive) |