Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!decwrl!pa.dec.com!decprl!decprl!weikart From: weikart@prl.dec.com (Chris Weikart) Newsgroups: comp.lang.c++ Subject: Re: c++-mode.el (for GNU emacs)... by popular request Keywords: c++-mode Message-ID: <1991Mar7.160509.20328@prl.dec.com> Date: 7 Mar 91 16:05:09 GMT Sender: news@prl.dec.com (USENET News System) Reply-To: weikart@prl.dec.com (Chris Weikart) Organization: Digital Equipment Corporation - Paris Research Laboratory Lines: 467 Apologies to all, this is a repost of the c++-mode diffs sent in #5002, for which long lines were wrapped by the xrn poster (fixed with lineLength = breakLength = 0, by the way). The original message follows... Here's the diffs between the posted Detlefs c++-mode (comp.lang.c++ #4749) and the most recent version (that either I or Detlefs is aware of), after my sig. I'm quite satisfied with it. One suggestion, (setq c-auto-newline nil) will avoid unpleasantries with "::". ------------------------------------------------------------------------------- Chris Weikart .............. DEC Paris Research Lab (PRL) *** /udir/weikart/tmp/c++-mode.el.old Tue Mar 5 21:03:08 1991 --- /proj/packages/gnuemacs/gnuemacs-18.55/lisp/c++-mode.el Wed Jan 23 21:22:00 1991 *************** *** 4,9 **** --- 4,20 ---- ;; Done by fairly faithful modification of: ;; c-mode.el, Copyright (C) 1985 Richard M. Stallman. ;; + ;; Last modified on Thu Mar 7 16:35:17 1991 by weikart + ;; + ;; Jan, 1991 (Chris Weikart, weikart@prl.dec.com) + ;; In function calculate-c++-indent, added check for adding + ;; c-continued-brace-offset after c-continued-statement-offset + ;; (change is marked with the string "[weikart]"). + ;; + ;; Jun, 1990 (Dave Detlefs, dld@cs.cmu.edu) + ;; Incorporated stylistic changes from David Lawrence at FSF; + ;; I think I've finally fixed C-style comments. + ;; ;; Feb, 1990 (Dave Detlefs, dld@cs.cmu.edu) ;; Fixed electric-c++-terminator to handle double colons, at the ;; request of John Hagerman. *************** *** 186,217 **** (interactive) (kill-all-local-variables) (use-local-map c++-mode-map) - (setq major-mode 'c++-mode) - (setq mode-name "C++") - (setq local-abbrev-table c++-mode-abbrev-table) (set-syntax-table c++-mode-syntax-table) ! (make-local-variable 'paragraph-start) ! (setq paragraph-start (concat "^$\\|" page-delimiter)) ! (make-local-variable 'paragraph-separate) ! (setq paragraph-separate paragraph-start) ! (make-local-variable 'paragraph-ignore-fill-prefix) ! (setq paragraph-ignore-fill-prefix t) ! (make-local-variable 'indent-line-function) ! (setq indent-line-function 'c++-indent-line) ! (make-local-variable 'require-final-newline) ! (setq require-final-newline t) ! (make-local-variable 'comment-start) ! (setq comment-start "// ") ! (make-local-variable 'comment-end) ! (setq comment-end "") ! (make-local-variable 'comment-column) ! (setq comment-column 32) ! (make-local-variable 'comment-start-skip) ! (setq comment-start-skip "/\\*+ *\\|// *") ! (make-local-variable 'comment-indent-hook) ! (setq comment-indent-hook 'c++-comment-indent) ! (make-local-variable 'parse-sexp-ignore-comments) ! (setq parse-sexp-ignore-comments nil) (run-hooks 'c++-mode-hook) (if c++-electric-colon (define-key c++-mode-map ":" 'electric-c++-terminator))) --- 197,217 ---- (interactive) (kill-all-local-variables) (use-local-map c++-mode-map) (set-syntax-table c++-mode-syntax-table) ! (setq major-mode 'c++-mode ! mode-name "C++" ! indent-line-function 'c++-indent-line ! comment-start "// " ! comment-end "" ! comment-column 32 ! comment-start-skip "/\\*+ *\\|// *" ! comment-indent-hook 'c++-comment-indent ! local-abbrev-table c++-mode-abbrev-table) ! (set (make-local-variable 'paragraph-start) (concat "^$\\|" page-delimiter)) ! (set (make-local-variable 'paragraph-separate) paragraph-start) ! (set (make-local-variable 'paragraph-ignore-fill-prefix) t) ! (set (make-local-variable 'require-final-newline) t) ! (set (make-local-variable 'parse-sexp-ignore-comments) nil) (run-hooks 'c++-mode-hook) (if c++-electric-colon (define-key c++-mode-map ":" 'electric-c++-terminator))) *************** *** 221,227 **** ;; based on its context. (defun c++-comment-indent () (if (looking-at "^\\(/\\*\\|//\\)") ! 0 ;Existing comment at bol stays there. (save-excursion (skip-chars-backward " \t") (max --- 221,227 ---- ;; based on its context. (defun c++-comment-indent () (if (looking-at "^\\(/\\*\\|//\\)") ! 0 ; Existing comment at bol stays there. (save-excursion (skip-chars-backward " \t") (max *************** *** 234,240 **** (progn (goto-char (match-beginning 0)) (current-column)) ! comment-column)))))) ;otherwise indent at comment column. (defun electric-c++-brace (arg) "Insert character and correct line's indentation." --- 234,240 ---- (progn (goto-char (match-beginning 0)) (current-column)) ! comment-column)))))) ; otherwise indent at comment column. (defun electric-c++-brace (arg) "Insert character and correct line's indentation." *************** *** 245,251 **** (or (save-excursion (skip-chars-backward " \t") (bolp)) ! (if c-auto-newline (progn (c++-indent-line) (newline) t) nil))) (progn (insert last-command-char) (c++-indent-line) --- 245,251 ---- (or (save-excursion (skip-chars-backward " \t") (bolp)) ! (if c-auto-newline (progn (c++-indent-line) (newline) t)))) (progn (insert last-command-char) (c++-indent-line) *************** *** 404,409 **** --- 404,416 ---- (goto-char (- (point-max) pos)))) shift-amt)) + + (defun c++-in-c-comment-p () + (save-excursion + (and (re-search-backward "\\(/\\*\\|\\*/\\)" (point-min) t) + (looking-at "/\\*")))) + + (defun calculate-c++-indent (&optional parse-start) "Return appropriate indentation for current line as C++ code. In usual case returns an integer: the column to indent to. *************** *** 421,429 **** (setq parse-start (point)) (setq state (parse-partial-sexp (point) indent-point 0)) (setq containing-sexp (car (cdr state)))) ! (cond ((or (nth 3 state) (nth 4 state)) ! ;; return nil or t if should not change this line ! (nth 4 state)) ((null containing-sexp) ;; Line is at top level. May be data or function definition, or ;; may be function argument declaration or member initialization. --- 428,439 ---- (setq parse-start (point)) (setq state (parse-partial-sexp (point) indent-point 0)) (setq containing-sexp (car (cdr state)))) ! (cond ((nth 3 state) ! ;; in a string. ! nil) ! ((c++-in-c-comment-p) ! ;; in a C comment. ! t) ((null containing-sexp) ;; Line is at top level. May be data or function definition, or ;; may be function argument declaration or member initialization. *************** *** 501,507 **** ;; previous line of the statement. (progn (c-backward-to-start-of-continued-exp containing-sexp) ! (+ c-continued-statement-offset (current-column))) ;; This line starts a new statement. ;; Position following last unclosed open. (goto-char containing-sexp) --- 511,522 ---- ;; previous line of the statement. (progn (c-backward-to-start-of-continued-exp containing-sexp) ! (+ c-continued-statement-offset (current-column) ! ;; Add continued-brace-offset? [weikart] ! (if (save-excursion (goto-char indent-point) ! (skip-chars-forward " \t") ! (eq (following-char) ?{)) ! c-continued-brace-offset 0))) ;; This line starts a new statement. ;; Position following last unclosed open. (goto-char containing-sexp) *************** *** 654,662 **** ;; indent this line c-continued-statement-offset ;; more than previous. (progn ! (c-backward-to-start-of-continued-exp (car contain-stack)) (setq this-indent ! (+ c-continued-statement-offset (current-column) (if at-brace c-continued-brace-offset 0)))) ;; Preceding line ended in comma or semi; ;; use the standard indent for this level. --- 669,679 ---- ;; indent this line c-continued-statement-offset ;; more than previous. (progn ! (c-backward-to-start-of-continued-exp ! (car contain-stack)) (setq this-indent ! (+ c-continued-statement-offset ! (current-column) (if at-brace c-continued-brace-offset 0)))) ;; Preceding line ended in comma or semi; ;; use the standard indent for this level. *************** *** 695,702 **** ;; Indent any comment following the text. (or (looking-at comment-start-skip) (if (re-search-forward comment-start-skip (save-excursion (end-of-line) (point)) t) ! (progn (indent-for-comment) (beginning-of-line))))))))) ! ) (defun fill-C-comment () (interactive) --- 712,718 ---- ;; Indent any comment following the text. (or (looking-at comment-start-skip) (if (re-search-forward comment-start-skip (save-excursion (end-of-line) (point)) t) ! (progn (indent-for-comment) (beginning-of-line)))))))))) (defun fill-C-comment () (interactive) *************** *** 734,740 **** "-*- C++ -*-" "\n\n"))) - (defun within-string-p (point1 point2) "Returns true if number of double quotes between two points is odd." (let ((s (buffer-substring point1 point2))) --- 750,755 ---- *************** *** 747,764 **** (setq count (+ count (if (\= (aref s pos) c) 1 0))) (setq pos (1+ pos))) count)) ! ! ! ! ! ! ! ! ! ! ! ! ;;; This page covers "macroization;" making C++ parameterized types ;;; via macros. --- 762,768 ---- (setq count (+ count (if (\= (aref s pos) c) 1 0))) (setq pos (1+ pos))) count)) ! ;;; This page covers "macroization;" making C++ parameterized types ;;; via macros. *************** *** 801,824 **** (progn (skip-chars-backward " \t") (kill-line)))))) ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ;;; This page covers commenting out multiple lines. (defun c++-comment-region () --- 805,811 ---- (progn (skip-chars-backward " \t") (kill-line)))))) ! ;;; This page covers commenting out multiple lines. (defun c++-comment-region () *************** *** 835,844 **** (beginning-of-line) (insert comment-start) (beginning-of-line) ! (next-line 1) ! )))) - (defun c++-uncomment-region () "Uncomment all lines in region between mark and current point by deleting the leading \"// \" from each line, if any." --- 822,829 ---- (beginning-of-line) (insert comment-start) (beginning-of-line) ! (next-line 1))))) (defun c++-uncomment-region () "Uncomment all lines in region between mark and current point by deleting the leading \"// \" from each line, if any." *************** *** 848,855 **** (end (if (> (point) m) (point) m)) (mymark (copy-marker end)) (len (length comment-start)) ! (char (string-to-char comment-start)) ! ) (save-excursion (goto-char start) (while (< (point) (marker-position mymark)) --- 833,839 ---- (end (if (> (point) m) (point) m)) (mymark (copy-marker end)) (len (length comment-start)) ! (char (string-to-char comment-start))) (save-excursion (goto-char start) (while (< (point) (marker-position mymark)) *************** *** 859,884 **** (zap-to-char 1 char) (delete-char len))) (beginning-of-line) ! (next-line 1) ! )))) ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ;;; Below are two regular expressions that attempt to match defuns ;;; "strongly" and "weakly." The strong one almost reconstructs the ;;; grammar of C++; the weak one just figures anything id or curly on --- 843,850 ---- (zap-to-char 1 char) (delete-char len))) (beginning-of-line) ! (next-line 1))))) ! ;;; Below are two regular expressions that attempt to match defuns ;;; "strongly" and "weakly." The strong one almost reconstructs the ;;; grammar of C++; the weak one just figures anything id or curly on *************** *** 886,892 **** ;;; determines which to use; the default is the weak one. (defvar c++-match-header-strongly nil ! "*If NIL, use c++-defun-header-weak to identify beginning of definitions,\ if nonNIL, use c++-defun-header-strong") (defvar c++-defun-header-strong-struct-equivs "\\(class\\|struct\\|enum\\)" --- 852,858 ---- ;;; determines which to use; the default is the weak one. (defvar c++-match-header-strongly nil ! "*If NIL, use c++-defun-header-weak to identify beginning of definitions, if nonNIL, use c++-defun-header-strong") (defvar c++-defun-header-strong-struct-equivs "\\(class\\|struct\\|enum\\)" *************** *** 937,947 **** wh-nec id "\\)")) (cs-header (concat c++-defun-header-strong-struct-equivs ! wh-nec id wh-opt inherit "?" wh-opt "{")) ! ) (concat "^\\(" func-header "\\|" cs-header "\\)")) ! "Strongly-defined regexp to match beginning of structure \ ! or function definition. ") ;; This part has to do with recognizing defuns. --- 903,912 ---- wh-nec id "\\)")) (cs-header (concat c++-defun-header-strong-struct-equivs ! wh-nec id wh-opt inherit "?" wh-opt "{"))) (concat "^\\(" func-header "\\|" cs-header "\\)")) ! "Strongly-defined regexp to match beginning of structure or ! function definition.") ;; This part has to do with recognizing defuns. *************** *** 954,961 **** ;; strong scheme shown above) are welcomed. (defconst c++-defun-header-weak "^{\\|^[_a-zA-Z].*{" ! "Weakly-defined regexp to match beginning of structure \ ! or function definition. ") (defun c++-beginning-of-defun (arg) --- 919,925 ---- ;; strong scheme shown above) are welcomed. (defconst c++-defun-header-weak "^{\\|^[_a-zA-Z].*{" ! "Weakly-defined regexp to match beginning of structure or function definition.") (defun c++-beginning-of-defun (arg) *************** *** 1008,1016 **** (if (and (= pos (point)) (re-search-forward c++-defun-header nil 'move)) (c++-end-of-defun 1)))) ! t ! )) ! ) (defun c++-indent-defun () "Indents the current function def, struct or class decl." --- 972,978 ---- (if (and (= pos (point)) (re-search-forward c++-defun-header nil 'move)) (c++-end-of-defun 1)))) ! t))) (defun c++-indent-defun () "Indents the current function def, struct or class decl."