X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/0bacd8d0b26c549094d13db9fdcc638d4f10baa7..4f124fb5287c38e0f8e507a2b8a5edd4d0cfb42c:/lisp/progmodes/cc-styles.el diff --git a/lisp/progmodes/cc-styles.el b/lisp/progmodes/cc-styles.el index 0e8b124752..1a26e54bf0 100644 --- a/lisp/progmodes/cc-styles.el +++ b/lisp/progmodes/cc-styles.el @@ -1,11 +1,12 @@ ;;; cc-styles.el --- support for styles in CC Mode -;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc. +;; Copyright (C) 1985,1987,1992-2003 Free Software Foundation, Inc. -;; Authors: 1992-1997 Barry A. Warsaw +;; Authors: 1998- Martin Stjernholm +;; 1992-1999 Barry A. Warsaw ;; 1987 Dave Detlefs and Stewart Clamen ;; 1985 Richard M. Stallman -;; Maintainer: cc-mode-help@python.org +;; Maintainer: bug-cc-mode@gnu.org ;; Created: 22-Apr-1997 (split from cc-mode.el) ;; Version: See cc-mode.el ;; Keywords: c languages oop @@ -23,28 +24,50 @@ ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License -;; along with GNU Emacs; see the file COPYING. If not, write to the -;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;; Boston, MA 02111-1307, USA. +;;; Commentary: + +;;; Code: + +(eval-when-compile + (let ((load-path + (if (and (boundp 'byte-compile-dest-file) + (stringp byte-compile-dest-file)) + (cons (file-name-directory byte-compile-dest-file) load-path) + load-path))) + (load "cc-bytecomp" nil t))) + +(cc-require 'cc-defs) +(cc-require 'cc-vars) +(cc-require 'cc-align) +;; cc-align is only indirectly required: Styles added with +;; `c-add-style' often contains references to functions defined there. + +;; Silence the compiler. +(cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs -(defconst c-style-alist +(defvar c-style-alist '(("gnu" (c-basic-offset . 2) (c-comment-only-line-offset . (0 . 0)) (c-offsets-alist . ((statement-block-intro . +) (knr-argdecl-intro . 5) (substatement-open . +) + (substatement-label . 0) (label . 0) (statement-case-open . +) (statement-cont . +) (arglist-intro . c-lineup-arglist-intro-after-paren) (arglist-close . c-lineup-arglist) + (inline-open . 0) + (brace-list-open . +) )) (c-special-indent-hook . c-gnu-impose-minimum) - (c-comment-continuation-stars . "") - (c-hanging-comment-ender-p . t) + (c-block-comment-prefix . "") ) ("k&r" (c-basic-offset . 5) @@ -52,18 +75,22 @@ (c-offsets-alist . ((statement-block-intro . +) (knr-argdecl-intro . 0) (substatement-open . 0) + (substatement-label . 0) (label . 0) (statement-cont . +) )) ) ("bsd" - (c-basic-offset . 4) + (c-basic-offset . 8) (c-comment-only-line-offset . 0) (c-offsets-alist . ((statement-block-intro . +) (knr-argdecl-intro . +) (substatement-open . 0) + (substatement-label . 0) (label . 0) (statement-cont . +) + (inline-open . 0) + (inexpr-class . 0) )) ) ("stroustrup" @@ -71,6 +98,7 @@ (c-comment-only-line-offset . 0) (c-offsets-alist . ((statement-block-intro . +) (substatement-open . 0) + (substatement-label . 0) (label . 0) (statement-cont . +) )) @@ -78,21 +106,46 @@ ("whitesmith" (c-basic-offset . 4) (c-comment-only-line-offset . 0) - (c-offsets-alist . ((statement-block-intro . +) - (knr-argdecl-intro . +) - (substatement-open . 0) + (c-offsets-alist . ((knr-argdecl-intro . +) (label . 0) (statement-cont . +) + (substatement-open . +) + (substatement-label . +) + (block-open . +) + (statement-block-intro . c-lineup-whitesmith-in-block) + (block-close . c-lineup-whitesmith-in-block) + (inline-open . +) + (defun-open . +) + (defun-block-intro . c-lineup-whitesmith-in-block) + (defun-close . c-lineup-whitesmith-in-block) + (brace-list-open . +) + (brace-list-intro . c-lineup-whitesmith-in-block) + (brace-entry-open . c-indent-multi-line-block) + (brace-list-close . c-lineup-whitesmith-in-block) + (class-open . +) + (inclass . c-lineup-whitesmith-in-block) + (class-close . +) + (inexpr-class . 0) + (extern-lang-open . +) + (inextern-lang . c-lineup-whitesmith-in-block) + (extern-lang-close . +) + (namespace-open . +) + (innamespace . c-lineup-whitesmith-in-block) + (namespace-close . +) + (module-open . +) + (inmodule . c-lineup-whitesmith-in-block) + (module-close . +) + (composition-open . +) + (incomposition . c-lineup-whitesmith-in-block) + (composition-close . +) )) - ) ("ellemtel" (c-basic-offset . 3) (c-comment-only-line-offset . 0) (c-hanging-braces-alist . ((substatement-open before after))) (c-offsets-alist . ((topmost-intro . 0) - (topmost-intro-cont . 0) - (substatement . +) + (substatement . +) (substatement-open . 0) (case-label . +) (access-label . -) @@ -104,19 +157,21 @@ (c-basic-offset . 8) (c-comment-only-line-offset . 0) (c-hanging-braces-alist . ((brace-list-open) + (brace-entry-open) (substatement-open after) (block-close . c-snug-do-while))) (c-cleanup-list . (brace-else-brace)) (c-offsets-alist . ((statement-block-intro . +) (knr-argdecl-intro . 0) (substatement-open . 0) + (substatement-label . 0) (label . 0) (statement-cont . +) )) ) ("python" (indent-tabs-mode . t) - (fill-column . 72) + (fill-column . 78) (c-basic-offset . 8) (c-offsets-alist . ((substatement-open . 0) (inextern-lang . 0) @@ -126,24 +181,23 @@ (c-hanging-braces-alist . ((brace-list-open) (brace-list-intro) (brace-list-close) + (brace-entry-open) (substatement-open after) (block-close . c-snug-do-while) )) - (c-comment-continuation-stars . "") - (c-hanging-comment-ender-p . nil) - (fill-column . 78) + (c-block-comment-prefix . "") ) ("java" (c-basic-offset . 4) (c-comment-only-line-offset . (0 . 0)) ;; the following preserves Javadoc starter lines - (c-hanging-comment-starter-p . nil) (c-offsets-alist . ((inline-open . 0) (topmost-intro-cont . +) (statement-block-intro . +) (knr-argdecl-intro . 5) - (substatement-open . +) - (label . 0) + (substatement-open . +) + (substatement-label . +) + (label . +) (statement-case-open . +) (statement-cont . +) (arglist-intro . c-lineup-arglist-intro-after-paren) @@ -152,7 +206,6 @@ (inher-cont . c-lineup-java-inher) (func-decl-cont . c-lineup-java-throws) )) - ) ) "Styles of indentation. @@ -166,7 +219,7 @@ for that variable when using the selected style. Optional BASE-STYLE if present, is a string and must follow STYLE-STRING. BASE-STYLE names a style that this style inherits from. -By default, all styles inherit from the \"cc-mode\" style, which is +By default, all styles inherit from the \"user\" style, which is computed at run time. Style loops generate errors. Two variables are treated specially. When VARIABLE is @@ -190,78 +243,143 @@ the existing style.") ;; Functions that manipulate styles -(defun c-set-style-1 (conscell) +(defun c-set-style-1 (conscell dont-override) ;; Set the style for one variable + ;; + ;; This function does not do any hidden buffer changes. (let ((attr (car conscell)) (val (cdr conscell))) (cond ;; first special variable ((eq attr 'c-offsets-alist) - (mapcar - (function - (lambda (langentry) - (let ((langelem (car langentry)) - (offset (cdr langentry))) - (c-set-offset langelem offset) - ))) - val)) + (let ((offsets (cond ((eq dont-override t) + c-offsets-alist) + (dont-override + (default-value 'c-offsets-alist))))) + (mapcar (lambda (langentry) + (let ((langelem (car langentry)) + (offset (cdr langentry))) + (unless (assq langelem offsets) + (c-set-offset langelem offset)))) + val))) ;; second special variable ((eq attr 'c-special-indent-hook) - (if (listp val) - (while val - (add-hook 'c-special-indent-hook (car val)) - (setq val (cdr val))) - (add-hook 'c-special-indent-hook val))) + ;; Maybe we should ignore dont-override here and always add new + ;; hooks? + (unless (cond ((eq dont-override t) + c-special-indent-hook) + (dont-override + (default-value 'c-special-indent-hook))) + (if (listp val) + (mapcar (lambda (func) + (add-hook 'c-special-indent-hook func t t)) + val) + (add-hook 'c-special-indent-hook val t t)))) ;; all other variables - (t (set attr val))) - )) + (t (when (or (not dont-override) + (not (memq attr c-style-variables)) + (eq (if (eq dont-override t) + (symbol-value attr) + (default-value attr)) + 'set-from-style)) + (set attr val) + ;; Must update a number of other variables if + ;; c-comment-prefix-regexp is set. + (if (eq attr 'c-comment-prefix-regexp) + (c-setup-paragraph-variables))))))) + +(defun c-get-style-variables (style basestyles) + ;; Return all variables in a style by resolving inheritances. + ;; + ;; This function does not do any hidden buffer changes. + (if (not style) + (copy-alist c-fallback-style) + (let ((vars (cdr (or (assoc (downcase style) c-style-alist) + (assoc (upcase style) c-style-alist) + (assoc style c-style-alist) + (progn + (c-benign-error "Undefined style: %s" style) + nil))))) + (let ((base (and (stringp (car-safe vars)) + (prog1 + (downcase (car vars)) + (setq vars (cdr vars)))))) + (if (memq base basestyles) + (c-benign-error "Style loop detected: %s in %s" base basestyles) + (nconc (c-get-style-variables base (cons base basestyles)) + (copy-alist vars))))))) -(defun c-set-style-2 (style basestyles) - ;; Recursively set the base style. If no base style is given, the - ;; default base style is "user" (a.k.a. "cc-mode") and the recursion - ;; stops. Be sure to detect loops. - (let ((vars (cdr (or (assoc (downcase style) c-style-alist) - (assoc (upcase style) c-style-alist) - (assoc style c-style-alist) - (error "Undefined style: %s" style))))) - (if (not (string-equal style "user")) - (let ((base (if (stringp (car vars)) - (prog1 - (downcase (car vars)) - (setq vars (cdr vars))) - "user"))) - (if (memq base basestyles) - (error "Style loop detected: %s in %s" base basestyles)) - (c-set-style-2 base (cons base basestyles)))) - (mapcar 'c-set-style-1 vars))) - (defvar c-set-style-history nil) ;;;###autoload -(defun c-set-style (stylename) +(defun c-set-style (stylename &optional dont-override) "Set CC Mode variables to use one of several different indentation styles. STYLENAME is a string representing the desired style from the list of styles described in the variable `c-style-alist'. See that variable for details of setting up styles. The variable `c-indentation-style' always contains the buffer's current -style name." - (interactive (list (let ((completion-ignore-case t) - (prompt (format "Which %s indentation style? " - mode-name))) - (completing-read prompt c-style-alist nil t - (cons c-indentation-style 0) - 'c-set-style-history)))) +style name. + +If the optional argument DONT-OVERRIDE is t, no style variables that +already have values will be overridden. I.e. in the case of +`c-offsets-alist', syntactic symbols will only be added, and in the +case of all other style variables, only those set to `set-from-style' +will be reassigned. + +If DONT-OVERRIDE is neither nil nor t, only those style variables that +have default (i.e. non-buffer local) values will keep their settings +while the rest will be overridden. This is useful to avoid overriding +global settings done in ~/.emacs when setting a style from a mode hook +\(providing the style variables are buffer local, which is the +default). + +Obviously, setting DONT-OVERRIDE to t is useful mainly when the +initial style is chosen for a CC Mode buffer by a major mode. Since +that is done internally by CC Mode, it typically won't have any effect +when used elsewhere." + (interactive + (list (let ((completion-ignore-case t) + (prompt (format "Which %s indentation style? " + mode-name))) + (condition-case nil + ;; The default argument is preferred over + ;; initial-contents, but it only exists in Emacs >= 20 + ;; and XEmacs >= 21. + (completing-read prompt c-style-alist nil t nil + 'c-set-style-history + c-indentation-style) + (wrong-number-of-arguments + ;; If the call above failed, we fall back to the old way + ;; of specifying the default value. + (completing-read prompt c-style-alist nil t + (cons c-indentation-style 0) + 'c-set-style-history)))))) + (or (stringp stylename) + (error "Argument to c-set-style was not a string")) (c-initialize-builtin-style) - (c-set-style-2 stylename nil) + (let ((vars (c-get-style-variables stylename nil))) + (unless dont-override + ;; Since we always add to c-special-indent-hook we must reset it + ;; first, or else the hooks from the preceding style will + ;; remain. This is not necessary for c-offsets-alist, since + ;; c-get-style-variables contains every valid offset type in the + ;; fallback entry. + (setq c-special-indent-hook + (default-value 'c-special-indent-hook))) + (mapcar (lambda (elem) + (c-set-style-1 elem dont-override)) + ;; Need to go through the variables backwards when we + ;; don't override any settings. + (if (eq dont-override t) (nreverse vars) vars))) (setq c-indentation-style stylename) (c-keep-region-active)) ;;;###autoload -(defun c-add-style (style descrip &optional set-p) +(defun c-add-style (style description &optional set-p) "Adds a style to `c-style-alist', or updates an existing one. -STYLE is a string identifying the style to add or update. DESCRIP is -an association list describing the style and must be of the form: +STYLE is a string identifying the style to add or update. DESCRIPTION +is an association list describing the style and must be of the form: ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...]) @@ -271,268 +389,59 @@ STYLE using `c-set-style' if the optional SET-P flag is non-nil." (interactive (let ((stylename (completing-read "Style to add: " c-style-alist nil nil nil 'c-set-style-history)) - (description (eval-minibuffer "Style description: "))) - (list stylename description + (descr (eval-minibuffer "Style description: "))) + (list stylename descr (y-or-n-p "Set the style too? ")))) (setq style (downcase style)) (let ((s (assoc style c-style-alist))) (if s - (setcdr s (copy-alist descrip)) ; replace - (setq c-style-alist (cons (cons style descrip) c-style-alist)))) + (setcdr s (copy-alist description)) ; replace + (setq c-style-alist (cons (cons style description) c-style-alist)))) (and set-p (c-set-style style))) - - -(defconst c-offsets-alist - '((string . -1000) - (c . c-lineup-C-comments) - (defun-open . 0) - (defun-close . 0) - (defun-block-intro . +) - (class-open . 0) - (class-close . 0) - (inline-open . +) - (inline-close . 0) - (func-decl-cont . +) - (knr-argdecl-intro . +) - (knr-argdecl . 0) - (topmost-intro . 0) - (topmost-intro-cont . 0) - (member-init-intro . +) - (member-init-cont . 0) - (inher-intro . +) - (inher-cont . c-lineup-multi-inher) - (block-open . 0) - (block-close . 0) - (brace-list-open . 0) - (brace-list-close . 0) - (brace-list-intro . +) - (brace-list-entry . 0) - (statement . 0) - ;; some people might prefer - ;;(statement . c-lineup-runin-statements) - (statement-cont . +) - ;; some people might prefer - ;;(statement-cont . c-lineup-math) - (statement-block-intro . +) - (statement-case-intro . +) - (statement-case-open . 0) - (substatement . +) - (substatement-open . +) - (case-label . 0) - (access-label . -) - (label . 2) - (do-while-closure . 0) - (else-clause . 0) - (comment-intro . c-lineup-comment) - (arglist-intro . +) - (arglist-cont . 0) - (arglist-cont-nonempty . c-lineup-arglist) - (arglist-close . +) - (stream-op . c-lineup-streamop) - (inclass . +) - (cpp-macro . -1000) - (cpp-macro-cont . c-lineup-dont-change) - (friend . 0) - (objc-method-intro . -1000) - (objc-method-args-cont . c-lineup-ObjC-method-args) - (objc-method-call-cont . c-lineup-ObjC-method-call) - (extern-lang-open . 0) - (extern-lang-close . 0) - (inextern-lang . +) - (namespace-open . 0) - (namespace-close . 0) - (innamespace . +) - (template-args-cont . +) - ) - "Association list of syntactic element symbols and indentation offsets. -As described below, each cons cell in this list has the form: - - (SYNTACTIC-SYMBOL . OFFSET) - -When a line is indented, CC Mode first determines the syntactic -context of the line by generating a list of symbols called syntactic -elements. This list can contain more than one syntactic element and -the global variable `c-syntactic-context' contains the context list -for the line being indented. Each element in this list is actually a -cons cell of the syntactic symbol and a buffer position. This buffer -position is called the relative indent point for the line. Some -syntactic symbols may not have a relative indent point associated with -them. - -After the syntactic context list for a line is generated, CC Mode -calculates the absolute indentation for the line by looking at each -syntactic element in the list. First, it compares the syntactic -element against the SYNTACTIC-SYMBOL's in `c-offsets-alist'. When it -finds a match, it adds the OFFSET to the column of the relative indent -point. The sum of this calculation for each element in the syntactic -list is the absolute offset for line being indented. - -If the syntactic element does not match any in the `c-offsets-alist', -an error is generated if `c-strict-syntax-p' is non-nil, otherwise the -element is ignored. - -Actually, OFFSET can be an integer, a function, a variable, or one of -the following symbols: `+', `-', `++', `--', `*', or `/'. These -latter designate positive or negative multiples of `c-basic-offset', -respectively: 1, -1, 2, -2, 0.5, and -0.5. If OFFSET is a function, it -is called with a single argument containing the cons of the syntactic -element symbol and the relative indent point. The function should -return an integer offset. - -Here is the current list of valid syntactic element symbols: - - string -- inside multi-line string - c -- inside a multi-line C style block comment - defun-open -- brace that opens a function definition - defun-close -- brace that closes a function definition - defun-block-intro -- the first line in a top-level defun - class-open -- brace that opens a class definition - class-close -- brace that closes a class definition - inline-open -- brace that opens an in-class inline method - inline-close -- brace that closes an in-class inline method - func-decl-cont -- the region between a function definition's - argument list and the function opening brace - (excluding K&R argument declarations). In C, you - cannot put anything but whitespace and comments - between them; in C++ and Java, throws declarations - and other things can appear in this context. - knr-argdecl-intro -- first line of a K&R C argument declaration - knr-argdecl -- subsequent lines in a K&R C argument declaration - topmost-intro -- the first line in a topmost construct definition - topmost-intro-cont -- topmost definition continuation lines - member-init-intro -- first line in a member initialization list - member-init-cont -- subsequent member initialization list lines - inher-intro -- first line of a multiple inheritance list - inher-cont -- subsequent multiple inheritance lines - block-open -- statement block open brace - block-close -- statement block close brace - brace-list-open -- open brace of an enum or static array list - brace-list-close -- close brace of an enum or static array list - brace-list-intro -- first line in an enum or static array list - brace-list-entry -- subsequent lines in an enum or static array list - statement -- a C (or like) statement - statement-cont -- a continuation of a C (or like) statement - statement-block-intro -- the first line in a new statement block - statement-case-intro -- the first line in a case \"block\" - statement-case-open -- the first line in a case block starting with brace - substatement -- the first line after an if/while/for/do/else - substatement-open -- the brace that opens a substatement block - case-label -- a `case' or `default' label - access-label -- C++ private/protected/public access label - label -- any ordinary label - do-while-closure -- the `while' that ends a do/while construct - else-clause -- the `else' of an if/else construct - comment-intro -- a line containing only a comment introduction - arglist-intro -- the first line in an argument list - arglist-cont -- subsequent argument list lines when no - arguments follow on the same line as the - arglist opening paren - arglist-cont-nonempty -- subsequent argument list lines when at - least one argument follows on the same - line as the arglist opening paren - arglist-close -- the solo close paren of an argument list - stream-op -- lines continuing a stream operator construct - inclass -- the construct is nested inside a class definition - cpp-macro -- the start of a C preprocessor macro definition - cpp-macro-cont -- the second and subsequent lines in a - multi-line C preprocessor macro definition - friend -- a C++ friend declaration - objc-method-intro -- the first line of an Objective-C method definition - objc-method-args-cont -- lines continuing an Objective-C method definition - objc-method-call-cont -- lines continuing an Objective-C method call - extern-lang-open -- brace that opens an external language block - extern-lang-close -- brace that closes an external language block - inextern-lang -- analogous to `inclass' syntactic symbol, - but used inside, e.g. extern \"C\" constructs - namespace-open -- brace that opens a C++ namespace block - namespace-close -- brace that closes a C++ namespace block - innamespace -- analogous to `inextern-lang' syntactic - symbol, but used inside C++ namespace constructs - template-args-cont -- C++ template argument list continuations -") - -(defun c-get-offset (langelem) - ;; Get offset from LANGELEM which is a cons cell of the form: - ;; (SYMBOL . RELPOS). The symbol is matched against - ;; c-offsets-alist and the offset found there is either returned, - ;; or added to the indentation at RELPOS. If RELPOS is nil, then - ;; the offset is simply returned. - (let* ((symbol (car langelem)) - (relpos (cdr langelem)) - (match (assq symbol c-offsets-alist)) - (offset (cdr-safe match))) - ;; offset can be a number, a function, a variable, or one of the - ;; symbols + or - - (cond - ((not match) - (if c-strict-syntax-p - (error "don't know how to indent a %s" symbol) - (setq offset 0 - relpos 0))) - ((eq offset '+) (setq offset c-basic-offset)) - ((eq offset '-) (setq offset (- c-basic-offset))) - ((eq offset '++) (setq offset (* 2 c-basic-offset))) - ((eq offset '--) (setq offset (* 2 (- c-basic-offset)))) - ((eq offset '*) (setq offset (/ c-basic-offset 2))) - ((eq offset '/) (setq offset (/ (- c-basic-offset) 2))) - ((functionp offset) (setq offset (funcall offset langelem))) - ((not (numberp offset)) (setq offset (symbol-value offset))) - ) - (+ (if (and relpos - (< relpos (c-point 'bol))) - (save-excursion - (goto-char relpos) - (current-column)) - 0) - offset))) - (defvar c-read-offset-history nil) (defun c-read-offset (langelem) ;; read new offset value for LANGELEM from minibuffer. return a ;; legal value only - (let* ((oldoff (cdr-safe (assq langelem c-offsets-alist))) - (defstr (format "(default %s): " oldoff)) - (errmsg (concat "Offset must be int, func, var, " - "or in [+,-,++,--,*,/] " - defstr)) - (prompt (concat "Offset " defstr)) - offset input interned raw) + ;; + ;; This function does not do any hidden buffer changes. + (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist) + (assq langelem (get 'c-offsets-alist + 'c-stylevar-fallback))))) + (symname (symbol-name langelem)) + (defstr (format "(default %s): " oldoff)) + (errmsg (concat "Offset must be int, func, var, vector, list, " + "or [+,-,++,--,*,/] " + defstr)) + (prompt (concat symname " offset " defstr)) + (keymap (make-sparse-keymap)) + (minibuffer-completion-table obarray) + (minibuffer-completion-predicate 'fboundp) + offset input) + ;; In principle completing-read is used here, but SPC is unbound + ;; to make it less annoying to enter lists. + (set-keymap-parent keymap minibuffer-local-completion-map) + (define-key keymap " " 'self-insert-command) (while (not offset) - (setq input (completing-read prompt obarray 'fboundp nil nil - 'c-read-offset-history) - offset (cond ((string-equal "" input) oldoff) ; default - ((string-equal "+" input) '+) - ((string-equal "-" input) '-) - ((string-equal "++" input) '++) - ((string-equal "--" input) '--) - ((string-equal "*" input) '*) - ((string-equal "/" input) '/) - ((string-match "^-?[0-9]+$" input) - (string-to-int input)) - ;; a symbol with a function binding - ((fboundp (setq interned (intern input))) - interned) - ;; a lambda function - ((c-safe (functionp (setq raw (read input)))) - raw) - ;; a symbol with variable binding - ((boundp interned) interned) - ;; error, but don't signal one, keep trying - ;; to read an input value - (t (ding) - (setq prompt errmsg) - nil)))) + (setq input (read-from-minibuffer prompt nil keymap t + 'c-read-offset-history + (format "%s" oldoff))) + (if (c-valid-offset input) + (setq offset input) + ;; error, but don't signal one, keep trying + ;; to read an input value + (ding) + (setq prompt errmsg))) offset)) ;;;###autoload -(defun c-set-offset (symbol offset &optional add-p) +(defun c-set-offset (symbol offset &optional ignored) "Change the value of a syntactic element symbol in `c-offsets-alist'. SYMBOL is the syntactic element symbol to change and OFFSET is the new -offset for that syntactic element. Optional ADD says to add SYMBOL to -`c-offsets-alist' if it doesn't already appear there." +offset for that syntactic element. The optional argument is not used +and exists only for compatibility reasons." (interactive (let* ((langelem (intern (completing-read @@ -542,38 +451,119 @@ offset for that syntactic element. Optional ADD says to add SYMBOL to (mapcar #'(lambda (langelem) (cons (format "%s" (car langelem)) nil)) - c-offsets-alist) + (get 'c-offsets-alist 'c-stylevar-fallback)) nil (not current-prefix-arg) ;; initial contents tries to be the last element ;; on the syntactic analysis list for the current ;; line - (let* ((syntax (c-guess-basic-syntax)) - (len (length syntax)) - (ic (format "%s" (car (nth (1- len) syntax))))) - (cons ic 0)) + (and c-buffer-is-cc-mode + (c-save-buffer-state + ((syntax (c-guess-basic-syntax)) + (len (length syntax)) + (ic (format "%s" (car (nth (1- len) syntax))))) + (cons ic 0))) ))) (offset (c-read-offset langelem))) (list langelem offset current-prefix-arg))) ;; sanity check offset - (or (eq offset '+) - (eq offset '-) - (eq offset '++) - (eq offset '--) - (eq offset '*) - (eq offset '/) - (integerp offset) - (functionp offset) - (boundp offset) - (error "Offset must be int, func, var, or in [+,-,++,--,*,/]: %s" - offset)) - (let ((entry (assq symbol c-offsets-alist))) - (if entry - (setcdr entry offset) - (if add-p - (setq c-offsets-alist (cons (cons symbol offset) c-offsets-alist)) - (error "%s is not a valid syntactic symbol." symbol)))) + (if (c-valid-offset offset) + (let ((entry (assq symbol c-offsets-alist))) + (if entry + (setcdr entry offset) + (if (assq symbol (get 'c-offsets-alist 'c-stylevar-fallback)) + (setq c-offsets-alist (cons (cons symbol offset) + c-offsets-alist)) + (c-benign-error "%s is not a valid syntactic symbol" symbol)))) + (c-benign-error "Invalid indentation setting for symbol %s: %s" + symbol offset)) (c-keep-region-active)) + +(defun c-setup-paragraph-variables () + "Fix things up for paragraph recognition and filling inside comments by +incorporating the value of `c-comment-prefix-regexp' in the relevant +variables." + ;; + ;; This function does not do any hidden buffer changes. + + (interactive) + + (setq c-current-comment-prefix + (if (listp c-comment-prefix-regexp) + (cdr-safe (or (assoc major-mode c-comment-prefix-regexp) + (assoc 'other c-comment-prefix-regexp))) + c-comment-prefix-regexp)) + + (let ((comment-line-prefix + (concat "[ \t]*\\(" c-current-comment-prefix "\\)[ \t]*"))) + + (setq paragraph-start (concat comment-line-prefix + c-paragraph-start + "\\|" + page-delimiter) + paragraph-separate (concat comment-line-prefix + c-paragraph-separate + "\\|" + page-delimiter) + paragraph-ignore-fill-prefix t + adaptive-fill-mode t + adaptive-fill-regexp + (concat comment-line-prefix + (if (default-value 'adaptive-fill-regexp) + (concat "\\(" + (default-value 'adaptive-fill-regexp) + "\\)") + ""))) + + (when (boundp 'adaptive-fill-first-line-regexp) + ;; XEmacs (20.x) adaptive fill mode doesn't have this. + (make-local-variable 'adaptive-fill-first-line-regexp) + (setq adaptive-fill-first-line-regexp + (concat "\\`" comment-line-prefix + ;; Maybe we should incorporate the old value here, + ;; but then we have to do all sorts of kludges to + ;; deal with the \` and \' it probably contains. + "\\'"))))) + + +;; Helper for setting up Filladapt mode. It's not used by CC Mode itself. + +(cc-bytecomp-defvar filladapt-token-table) +(cc-bytecomp-defvar filladapt-token-match-table) +(cc-bytecomp-defvar filladapt-token-conversion-table) + +(defun c-setup-filladapt () + "Convenience function to configure Kyle E. Jones' Filladapt mode for +CC Mode by making sure the proper entries are present on +`filladapt-token-table', `filladapt-token-match-table', and +`filladapt-token-conversion-table'. This is intended to be used on +`c-mode-common-hook' or similar." + ;; This function is intended to be used explicitly by the end user + ;; only. + ;; + ;; This function does not do any hidden buffer changes. + + ;; The default configuration already handles C++ comments, but we + ;; need to add handling of C block comments. A new filladapt token + ;; `c-comment' is added for that. + (let (p) + (setq p filladapt-token-table) + (while (and p (not (eq (car-safe (cdr-safe (car-safe p))) 'c-comment))) + (setq p (cdr-safe p))) + (if p + (setcar (car p) c-current-comment-prefix) + (setq filladapt-token-table + (append (list (car filladapt-token-table) + (list c-current-comment-prefix 'c-comment)) + (cdr filladapt-token-table))))) + (unless (assq 'c-comment filladapt-token-match-table) + (setq filladapt-token-match-table + (append '((c-comment c-comment)) + filladapt-token-match-table))) + (unless (assq 'c-comment filladapt-token-conversion-table) + (setq filladapt-token-conversion-table + (append '((c-comment . exact)) + filladapt-token-conversion-table)))) (defun c-initialize-builtin-style () @@ -581,92 +571,59 @@ offset for that syntactic element. Optional ADD says to add SYMBOL to ;; crucial because future c-set-style calls will always reset the ;; variables first to the `cc-mode' style before instituting the new ;; style. Only do this once! - (c-initialize-cc-mode t) - (or (assoc "cc-mode" c-style-alist) - (assoc "user" c-style-alist) - (let (copyfunc) - ;; use built-in copy-tree if its there. - (if (and (fboundp 'copy-tree) - (functionp (symbol-function 'copy-tree))) - (setq copyfunc (symbol-function 'copy-tree)) - (setq copyfunc (lambda (tree) - (if (consp tree) - (cons (funcall copyfunc (car tree)) - (funcall copyfunc (cdr tree))) - tree)))) - (c-add-style "user" - (mapcar - (function - (lambda (var) - (let ((val (symbol-value var))) - (cons var (if (atom val) - val - (funcall copyfunc val) - )) - ))) - '(c-backslash-column - c-basic-offset - c-cleanup-list - c-comment-only-line-offset - c-electric-pound-behavior - c-hanging-braces-alist - c-hanging-colons-alist - c-hanging-comment-starter-p - c-hanging-comment-ender-p - c-offsets-alist - ))) - (c-add-style "cc-mode" '("user")) - ;; the default style is now GNU. This can be overridden in - ;; c-mode-common-hook or {c,c++,objc,java}-mode-hook. - (c-set-style c-default-style))) - (if c-style-variables-are-local-p - (c-make-styles-buffer-local))) - + ;; + ;; This function does not do any hidden buffer changes. + (unless (get 'c-initialize-builtin-style 'is-run) + (put 'c-initialize-builtin-style 'is-run t) + ;;(c-initialize-cc-mode) + (unless (assoc "user" c-style-alist) + (let ((vars c-style-variables) var val uservars) + (while vars + (setq var (car vars) + val (symbol-value var) + vars (cdr vars)) + (cond ((eq var 'c-offsets-alist) + (or (null val) + (setq uservars (cons (cons 'c-offsets-alist val) + uservars)))) + ((not (eq val 'set-from-style)) + (setq uservars (cons (cons var val) + uservars))))) + (c-add-style "user" uservars))) + (unless (assoc "cc-mode" c-style-alist) + (c-add-style "cc-mode" '("user"))) + (if c-style-variables-are-local-p + (c-make-styles-buffer-local)))) (defun c-make-styles-buffer-local (&optional this-buf-only-p) "Make all CC Mode style variables buffer local. -If you edit primarily one style of C (or C++, Objective-C, Java) code, -you probably want style variables to be global. This is the default. - -If you edit many different styles of C (or C++, Objective-C, Java) at -the same time, you probably want the CC Mode style variables to be -buffer local. If you do, then you will need to set any CC Mode style -variables in a hook function (e.g. off of c-mode-common-hook), instead -of at the top level of your ~/.emacs file. - -This function makes all the CC Mode style variables buffer local. -Call it after CC Mode is loaded into your Emacs environment. -Conversely, set the variable `c-style-variables-are-local-p' to t in -your .emacs file, before CC Mode is loaded, and this function will be -automatically called when CC Mode is loaded. - -Optional argument, when non-nil, means use `make-local-variable' -instead of `make-variable-buffer-local'." +If `this-buf-only-p' is non-nil, the style variables will be made +buffer local only in the current buffer. Otherwise they'll be made +permanently buffer local in any buffer that change their values. + +The buffer localness of the style variables are normally controlled +with the variable `c-style-variables-are-local-p', so there's seldom +any reason to call this function directly." + ;; + ;; This function does not do any hidden buffer changes. + ;; style variables (let ((func (if this-buf-only-p 'make-local-variable 'make-variable-buffer-local)) - (varsyms '(c-offsets-alist - c-basic-offset - c-file-style - c-file-offsets - c-comment-only-line-offset - c-cleanup-list - c-hanging-braces-alist - c-hanging-colons-alist - c-hanging-comment-starter-p - c-hanging-comment-ender-p - c-backslash-column - c-label-minimum-indentation - c-indentation-style))) + (varsyms (cons 'c-indentation-style (copy-alist c-style-variables)))) + (delq 'c-special-indent-hook varsyms) (mapcar func varsyms) ;; Hooks must be handled specially (if this-buf-only-p (make-local-hook 'c-special-indent-hook) - (make-variable-buffer-local 'c-special-indent-hook)) + (make-variable-buffer-local 'c-special-indent-hook) + (setq c-style-variables-are-local-p t)) )) -(provide 'cc-styles) +(cc-provide 'cc-styles) + +;;; arch-tag: c764f61a-96ba-484a-a68f-101c0e9d5d2c ;;; cc-styles.el ends here