Subject: CC Mode 5.33.1 (Dart//l); cc-mode without fontification

From: Brady Trainor <[email-masked]>
Date: Wed, 18 Jul 2018 12:55:25 -0700

Should it be possible for a language mode author to use only the
indentation functions from cc-mode, and write the font-lock-defaults
independently of cc-mode?

I attempted to do this with by using c-basic-common-init, instead of
c-common-init.

Here is the contents of my dart-mode.el

--8<---------------cut here---------------start------------->8---
;;; dart-mode.el --- Major mode for editing Dart files -*- lexical-binding: t; -*-

;;; Code:

(require 'cc-mode)
(eval-when-compile
  (require 'cc-langs)
  (require 'cc-fonts))

(eval-and-compile (c-add-language 'dart-mode 'java-mode))

(c-lang-defconst c-multiline-string-start-char
  dart ?@)

(defconst dart-c-style
  '("java")
  "The default Dart styles.")

(c-add-style "dart" dart-c-style)

(defvar dart-mode-map (c-make-inherited-keymap)
  "Keymap used in dart-mode buffers.")

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.dart\\'" . dart-mode))

;;;###autoload
(define-derived-mode dart-mode prog-mode "Dart"
  "Major mode for editing Dart files.

The hook `c-mode-common-hook' is run with no args at mode
initialization, then `dart-mode-hook'.

Key bindings:
\\{dart-mode-map}"
  (c-initialize-cc-mode t)
  (c-init-language-vars dart-mode)
  (c-basic-common-init 'dart-mode '((dart-mode . "dart")))
  (setq font-lock-defaults '(nil)))

(provide 'dart-mode)

;;; dart-mode.el ends here
--8<---------------cut here---------------end--------------->8---

I then open emacs with:

emacs -Q tmp.dart -l dart-mode.el --eval "(progn (toggle-debug-on-error) (dart-mode))"

However, I get errors when for example tmp.dart has the contents:

class SpaceCraft {

I put point after opening curly, hit RET, and type the closing curly, I
get the following stack trace:

--8<---------------cut here---------------end--------------->8---
Debugger entered--Lisp error: (args-out-of-range 20 40)
  put-text-property(20 40 fontified nil)
  c-extend-after-change-region(20 20 4)
  font-lock-extend-jit-lock-region-after-change(20 20 4)
  run-hook-with-args(font-lock-extend-jit-lock-region-after-change 20 20 4)
  jit-lock-after-change(20 20 4)
  c-shift-line-indentation(-4)
  c-indent-line(((class-close 1)))
  c-electric-brace(nil)
  funcall-interactively(c-electric-brace nil)
  call-interactively(c-electric-brace nil nil)
  command-execute(c-electric-brace)
--8<---------------cut here---------------end--------------->8---

If I remove the following expression, then the issue is resolved.

(c-lang-defconst c-multiline-string-start-char
  dart ?@)

But there are many c-lang-defconst expressions in current dart-mode
master, and I have not understood cc-mode enough to understand why each
should be there, and which should not. If I remove the above line, I can
continue to find such errors, so it seems more about having removed the
cc-mode fontification from the major mode.

(Why would I want to remove only the cc fontification? dart-mode may
become easier to maintain and deal with edge cases once we remove
cc-mode framework from its implementation. I would like to do this
piecemeal. It may take some time to create an indentation function from
scratch that is strictly as good as the one implemented via cc-mode
framework. But there are several issues on issue tracker related to
fontification, which are quite tractable to solve simply using the
font-lock framework standing alone. So I would like to add my
fontification improvements to the master branch, leaving cc-mode
indentation intact, so that I am not creating regressions on master
branch. In fact, inspired by haskell-mode, I'd like to offer multiple
choices in indentation functions, so that potentially, contributors can
improve their choice of either the cc-mode indentation or the "from
scratch" indentation function. I have more ideas for choices in
indentation, like being able to call `dartfmt` on regions, or have it
only look at region between point and point-min (to avoid dartfmt's
predilection to choke and spew errors).)

Thank you for reading!

--
Brady

Emacs  : GNU Emacs 27.0.50 (build 1, x86_64-apple-darwin17.4.0, NS appkit-1561.20 Version 10.13.3 (Build 17D102))
 of 2018-07-03
Package: CC Mode 5.33.1 (Dart//l)
Buffer Style: dart
c-emacs-features: (pps-extended-state col-0-paren posix-char-classes gen-string-delim gen-comment-delim syntax-properties 1-bit)

current state:
==============
(setq
 c-basic-offset 4
 c-comment-only-line-offset '(0 . 0)
 c-indent-comment-alist '((anchored-comment column . 0) (end-block space . 1)
			  (cpp-end-block space . 2))
 c-indent-comments-syntactically-p nil
 c-block-comment-prefix "* "
 c-comment-prefix-regexp '((pike-mode . "//+!?\\|\\**") (awk-mode . "#+")
			   (other . "//+\\|\\**"))
 c-doc-comment-style '((java-mode . javadoc) (pike-mode . autodoc)
		       (c-mode . gtkdoc))
 c-cleanup-list '(scope-operator)
 c-hanging-braces-alist '((brace-list-open) (brace-entry-open) (statement-cont)
			  (substatement-open after)
			  (block-close . c-snug-do-while)
			  (extern-lang-open after) (namespace-open after)
			  (module-open after) (composition-open after)
			  (inexpr-class-open after) (inexpr-class-close before)
			  (arglist-cont-nonempty))
 c-hanging-colons-alist nil
 c-hanging-semi&comma-criteria '(c-semi&comma-inside-parenlist)
 c-backslash-column 48
 c-backslash-max-column 72
 c-special-indent-hook nil
 c-label-minimum-indentation 1
 c-offsets-alist '((inexpr-class . +)
		   (inexpr-statement . +)
		   (lambda-intro-cont . +)
		   (inlambda . c-lineup-inexpr-block)
		   (template-args-cont c-lineup-template-args +)
		   (incomposition . +)
		   (inmodule . +)
		   (innamespace . +)
		   (inextern-lang . +)
		   (composition-close . 0)
		   (module-close . 0)
		   (namespace-close . 0)
		   (extern-lang-close . 0)
		   (composition-open . 0)
		   (module-open . 0)
		   (namespace-open . 0)
		   (extern-lang-open . 0)
		   (objc-method-call-cont
		    c-lineup-ObjC-method-call-colons
		    c-lineup-ObjC-method-call
		    +
		    )
		   (objc-method-args-cont . c-lineup-ObjC-method-args)
		   (objc-method-intro . [0])
		   (friend . 0)
		   (cpp-define-intro c-lineup-cpp-define +)
		   (cpp-macro-cont . +)
		   (cpp-macro . [0])
		   (inclass . +)
		   (stream-op . c-lineup-streamop)
		   (arglist-cont-nonempty
		    c-lineup-gcc-asm-reg
		    c-lineup-arglist
		    )
		   (arglist-cont c-lineup-gcc-asm-reg 0)
		   (comment-intro c-lineup-knr-region-comment c-lineup-comment)
		   (catch-clause . 0)
		   (else-clause . 0)
		   (do-while-closure . 0)
		   (case-label . 0)
		   (substatement . +)
		   (statement-case-intro . +)
		   (statement . 0)
		   (brace-entry-open . 0)
		   (brace-list-entry . c-lineup-under-anchor)
		   (brace-list-intro . +)
		   (brace-list-close . 0)
		   (brace-list-open . 0)
		   (block-close . 0)
		   (block-open . 0)
		   (inher-intro . +)
		   (member-init-cont . c-lineup-multi-inher)
		   (member-init-intro . +)
		   (annotation-var-cont . +)
		   (annotation-top-cont . 0)
		   (topmost-intro . 0)
		   (knr-argdecl . 0)
		   (inline-close . 0)
		   (class-close . 0)
		   (class-open . 0)
		   (defun-block-intro . +)
		   (defun-close . 0)
		   (defun-open . 0)
		   (c . c-lineup-C-comments)
		   (string . c-lineup-dont-change)
		   (func-decl-cont . c-lineup-java-throws)
		   (inher-cont . c-lineup-java-inher)
		   (access-label . 0)
		   (arglist-close . c-lineup-arglist)
		   (arglist-intro . c-lineup-arglist-intro-after-paren)
		   (statement-cont . +)
		   (statement-case-open . +)
		   (label . +)
		   (substatement-label . +)
		   (substatement-open . +)
		   (knr-argdecl-intro . 5)
		   (statement-block-intro . +)
		   (topmost-intro-cont . +)
		   (inline-open . 0)
		   )
 c-buffer-is-cc-mode 'dart-mode
 c-tab-always-indent t
 c-syntactic-indentation t
 c-syntactic-indentation-in-macros t
 c-ignore-auto-fill '(string cpp code)
 c-auto-align-backslashes t
 c-backspace-function 'backward-delete-char-untabify
 c-delete-function 'delete-char
 c-electric-pound-behavior nil
 c-default-style '((java-mode . "java") (awk-mode . "awk") (other . "gnu"))
 c-enable-xemacs-performance-kludge-p nil
 c-old-style-variable-behavior nil
 defun-prompt-regexp nil
 tab-width 8
 comment-column 32
 parse-sexp-ignore-comments t
 parse-sexp-lookup-properties t
 auto-fill-function nil
 comment-multi-line t
 comment-start-skip "\\(//+\\|/\\*+\\)\\s *"
 fill-prefix nil
 fill-column 70
 paragraph-start "[ 	]*\\(//+\\|\\**\\)[ 	]*\\(@[a-zA-Z]+\\>\\|$\\)\\|^\f"
 adaptive-fill-mode t
 adaptive-fill-regexp "[ 	]*\\(//+\\|\\**\\)[ 	]*\\([ 	]*\\([-–!|#%;>*·•‣⁃◦]+[ 	]*\\)*\\)"
 )