GNU bug report logs - #32204
CC Mode 5.33.1 (Dart//l); cc-mode without fontification

Previous Next

Package: cc-mode;

Reported by: Brady Trainor <mail <at> bradyt.com>

Date: Wed, 18 Jul 2018 19:56:02 UTC

Severity: normal

To reply to this bug, email your comments to 32204 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-cc-mode <at> gnu.org:
bug#32204; Package cc-mode. (Wed, 18 Jul 2018 19:56:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Brady Trainor <mail <at> bradyt.com>:
New bug report received and forwarded. Copy sent to bug-cc-mode <at> gnu.org. (Wed, 18 Jul 2018 19:56:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Brady Trainor <mail <at> bradyt.com>
To: submit <at> debbugs.gnu.org
Subject: CC Mode 5.33.1 (Dart//l); cc-mode without fontification
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 "[ 	]*\\(//+\\|\\**\\)[ 	]*\\([ 	]*\\([-–!|#%;>*·•‣⁃◦]+[ 	]*\\)*\\)"
 )




Information forwarded to bug-cc-mode <at> gnu.org:
bug#32204; Package cc-mode. (Sat, 21 Jul 2018 21:23:01 GMT) Full text and rfc822 format available.

Message #8 received at 32204 <at> debbugs.gnu.org (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Brady Trainor <mail <at> bradyt.com>
Cc: 32204 <at> debbugs.gnu.org
Subject: Re: bug#32204: CC Mode 5.33.1 (Dart//l); cc-mode without fontification
Date: Sat, 21 Jul 2018 21:08:48 +0000
Hello, Brady.

On Wed, Jul 18, 2018 at 12:55:25 -0700, Brady Trainor wrote:
> 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?

There is currently no provision in CC Mode for this strategy.  "Should"
is a difficult, philosophical word.  ;-)

I'd normally ask at this point why you'd want to do this, but you
explain lower down.

> 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---

I loaded dart-mode.el and tried it out.  Just before typing that closing
brace, after-change-functions looks like this:

    (jit-lock-after-change c-after-change t)

.  For CC Mode to work, it would need to look like this:

    (c-after-change jit-lock-after-change t)

, with CC Mode's after change hook executing before jit-lock's, so that
c-after-change can adjust certain position variables which later get
used by c-extend-after-change-region.

Normally this rearrangement of after-change-functions is done by the
function c-after-font-lock-init, which is placed on font-lock-mode-hook
by c-font-lock-init.

c-font-lock-init is called from c-common-init, which dart-mode isn't
calling.  It's worth emphasising that c-font-lock-init is initialising
CC Mode for font-lock, just as much as it is initialising font-lock for
CC Mode.

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

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

I think it's just coincidence that that "works".  I also think that
you'll run into a whole sequence of bugs like this while attempting to
remove CC Mode's fontification routines.

> 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.

All the c-lang-defconsts are needed; if you don't specify a value, you
will inherit the value from the base language (here, Java Mode).  The
c-lang-defconts are compile time variables.  From them are built the
c-lang-defvars, which are normal runtime variables (constants, really)
which take on different values for different languages.  In a sense they
_define_ the different CC Mode languages.

> 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.

Yes.  Bear in mind CC Mode is very old code indeed, it's development
extending back more that 30 years.  In that time it's become somewhat
twisted and inelegant (for which I'm partly to blame).

> (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.

This depends on whether you definitely want to build a better mode, or
whether you merely want to have a better mode.  Indentation functions
for C-like languages are difficult.  Correct fontification is not much
easier.  As a matter of interest, Stefan Monnier has done some work
creating a modern C Mode; I'm not entirely sure how far he's got, but
it's certainly at least basically working.  You can reach him on the
Emacs developers' mailing list, emacs-devel <at> gnu.org.

> 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.

Fontification (and indeed indentation) errors tend to be like bubbles of
paste underneath freshly hung wallpaper; you think you've managed to
eliminate an unsightly bubble, but it's just popped up somewhere else.

> 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.

Do make sure they really are improvements.  You might well be interested
in the CC Mode unit tests which test indentation and fontification.
They're in directory "tests" in the standalone CC Mode repository at
SourceForge.

> 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.

CC Mode's indentation is already very configurable, see the CC Mode
manual.  There is no single "CC Mode indentation".

> 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!

Happy hacking!

> --
> Brady

[ CC Mode configuration appreciated, but snipped. ]

-- 
Alan Mackenzie (Nuremberg, Germany).




This bug report was last modified 6 years and 332 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.