Function: c-basic-common-init

c-basic-common-init is a byte-compiled function defined in cc-mode.el.gz.

Signature

(c-basic-common-init MODE DEFAULT-STYLE)

Documentation

Initialize the syntax handling routines and the line breaking/filling code.

Intended to be used by other packages that embed CC Mode.

MODE is the CC Mode flavor to set up, e.g. c-mode or java-mode. DEFAULT-STYLE tells which indentation style to install. It has the same format as c-default-style.

Note that c-init-language-vars must be called before this function. This function cannot do that since c-init-language-vars is a macro that requires a literal mode spec at compile time.

Probably introduced at or before Emacs version 22.1.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/cc-mode.el.gz
(defun c-basic-common-init (mode default-style)
  "Initialize the syntax handling routines and the line breaking/filling code.
Intended to be used by other packages that embed CC Mode.

MODE is the CC Mode flavor to set up, e.g. `c-mode' or `java-mode'.
DEFAULT-STYLE tells which indentation style to install.  It has the
same format as `c-default-style'.

Note that `c-init-language-vars' must be called before this function.
This function cannot do that since `c-init-language-vars' is a macro
that requires a literal mode spec at compile time."

  (setq c-buffer-is-cc-mode mode)

  (c-populate-abbrev-table)

  ;; these variables should always be buffer local; they do not affect
  ;; indentation style.
  (make-local-variable 'comment-start)
  (make-local-variable 'comment-end)
  (make-local-variable 'comment-start-skip)

  (make-local-variable 'paragraph-start)
  (make-local-variable 'paragraph-separate)
  (make-local-variable 'paragraph-ignore-fill-prefix)
  (make-local-variable 'adaptive-fill-mode)
  (make-local-variable 'adaptive-fill-regexp)
  (make-local-variable 'fill-paragraph-handle-comment)

  (setq c-buffer-is-cc-mode mode)

  ;; Prepare for the use of `electric-pair-mode'.  Note: if this mode is not
  ;; yet loaded, `electric-pair-inhibit-predicate' will get set from an
  ;; `eval-after-load' form in `c-initialize-cc-mode' when elec-pair.elc is
  ;; loaded.
  (when (boundp 'electric-pair-inhibit-predicate)
    (make-local-variable 'electric-pair-inhibit-predicate)
    (setq electric-pair-inhibit-predicate
	  #'c-electric-pair-inhibit-predicate))

  ;; now set their values
  (set (make-local-variable 'parse-sexp-ignore-comments) t)
  (set (make-local-variable 'indent-line-function) 'c-indent-line)
  (set (make-local-variable 'indent-region-function) 'c-indent-region)
  (set (make-local-variable 'normal-auto-fill-function) 'c-do-auto-fill)
  (set (make-local-variable 'comment-multi-line) t)
  (set (make-local-variable 'comment-line-break-function)
       'c-indent-new-comment-line)

  ;; Prevent time-wasting activity on C-y.
  (when (boundp 'yank-handled-properties)
    (make-local-variable 'yank-handled-properties)
    (let ((yank-cat-handler (assq 'category yank-handled-properties)))
      (when yank-cat-handler
	(setq yank-handled-properties (remq yank-cat-handler
					    yank-handled-properties)))))

  ;; For the benefit of adaptive fill, which otherwise mis-fills.
  (setq fill-paragraph-handle-comment nil)

  ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
  ;; direct call to `fill-paragraph' behaves better.  This still
  ;; doesn't work with filladapt but it's better than nothing.
  (set (make-local-variable 'fill-paragraph-function) 'c-fill-paragraph)

  ;; Initialize the cache for `c-looking-at-or-maybe-in-bracelist'.
  (setq c-laomib-cache nil)
  ;; Initialize the three literal sub-caches.
  (c-truncate-lit-pos-cache 1)
  ;; Initialize the cache of brace pairs, and opening braces/brackets/parens.
  (c-state-cache-init)
  ;; Initialize the "brace stack" cache.
  (c-init-bs-cache)

  ;; Keep track of where `c-fl-syn-tab' text properties are set.
  (setq c-min-syn-tab-mkr nil)
  (setq c-max-syn-tab-mkr nil)

  (when (or c-recognize-<>-arglists
	    (c-major-mode-is 'awk-mode)
	    (c-major-mode-is '(java-mode c-mode c++-mode objc-mode pike-mode)))
    ;; We'll use the syntax-table text property to change the syntax
    ;; of some chars for this language, so do the necessary setup for
    ;; that.
    ;;
    ;; Note to other package developers: It's ok to turn this on in CC
    ;; Mode buffers when CC Mode doesn't, but it's not ok to turn it
    ;; off if CC Mode has turned it on.

    ;; Emacs.
    (when (boundp 'parse-sexp-lookup-properties)
      (set (make-local-variable 'parse-sexp-lookup-properties) t))

    ;; Same as above for XEmacs.
    (when (boundp 'lookup-syntax-properties)
      (set (make-local-variable 'lookup-syntax-properties) t)))

  ;; Use this in Emacs 21+ to avoid meddling with the rear-nonsticky
  ;; property on each character.
  (when (boundp 'text-property-default-nonsticky)
    (make-local-variable 'text-property-default-nonsticky)
    (mapc (lambda (tprop)
	    (unless (assq tprop text-property-default-nonsticky)
	      (setq text-property-default-nonsticky
                    (cons `(,tprop . t) text-property-default-nonsticky))))
	  '(syntax-table c-fl-syn-tab category c-type)))

  ;; In Emacs 21 and later it's possible to turn off the ad-hoc
  ;; heuristic that open parens in column 0 are defun starters.  Since
  ;; we have c-state-cache, that heuristic isn't useful and only causes
  ;; trouble, so turn it off.
;; 2006/12/17: This facility is somewhat confused, and doesn't really seem
;; helpful.  Comment it out for now.
;;   (when (memq 'col-0-paren c-emacs-features)
;;     (make-local-variable 'open-paren-in-column-0-is-defun-start)
;;     (setq open-paren-in-column-0-is-defun-start nil))

  (c-clear-found-types)

  ;; now set the mode style based on default-style
  (let ((style (cc-choose-style-for-mode mode default-style)))
    ;; Override style variables if `c-old-style-variable-behavior' is
    ;; set.  Also override if we are using global style variables,
    ;; have already initialized a style once, and are switching to a
    ;; different style.  (It's doubtful whether this is desirable, but
    ;; the whole situation with nonlocal style variables is a bit
    ;; awkward.  It's at least the most compatible way with the old
    ;; style init procedure.)
    (c-set-style style (not (or c-old-style-variable-behavior
				(and (not c-style-variables-are-local-p)
				     c-indentation-style
				     (not (string-equal c-indentation-style
							style)))))))
  (c-setup-paragraph-variables)

  ;; we have to do something special for c-offsets-alist so that the
  ;; buffer local value has its own alist structure.
  (setq c-offsets-alist (copy-alist c-offsets-alist))

  ;; setup the comment indent variable in an Emacs version portable way
  (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
  ;; What sort of comments are default for M-;?
  (setq c-block-comment-flag c-block-comment-is-default)

  ;; In Emacs 24.4 onwards, prevent Emacs's built in electric indentation from
  ;; messing up CC Mode's, and set `c-electric-flag' if `electric-indent-mode'
  ;; has been called by the user.
  (when (boundp 'electric-indent-inhibit) (setq electric-indent-inhibit t))
  ;; CC-mode should obey Emacs's generic preferences, tho only do it if
  ;; Emacs's generic preferences can be set per-buffer (Emacs>=24.4).
  (when (fboundp 'electric-indent-local-mode)
    (setq c-electric-flag electric-indent-mode))

;;   ;; Put submode indicators onto minor-mode-alist, but only once.
;;   (or (assq 'c-submode-indicators minor-mode-alist)
;;       (setq minor-mode-alist
;;	    (cons '(c-submode-indicators c-submode-indicators)
;;		  minor-mode-alist)))
  (c-update-modeline)

  ;; Install the functions that ensure that various internal caches
  ;; don't become invalid due to buffer changes.
  (when (featurep 'xemacs)
    (make-local-hook 'before-change-functions)
    (make-local-hook 'after-change-functions))
  (add-hook 'before-change-functions 'c-before-change nil t)
  (setq c-just-done-before-change nil)
  ;; FIXME: We should use the new `depth' arg in Emacs-27 (e.g. a depth of -10
  ;; would do since font-lock uses a(n implicit) depth of 0) so we don't need
  ;; c-after-font-lock-init.
  (add-hook 'after-change-functions 'c-after-change nil t)
  (add-hook 'post-command-hook 'c-post-command)

  (when (boundp 'font-lock-extend-after-change-region-function)
    (set (make-local-variable 'font-lock-extend-after-change-region-function)
         'c-extend-after-change-region))) ; Currently (2009-05) used by all