From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#41897: 28.0.50; JavaScript comment filling with mhtml-mode Date: Mon, 22 Jun 2020 19:17:50 +0000 Message-ID: <20200622191750.GA11506@ACM> References: <20200620171827.7855.qmail@mail.muc.de> <87d05ta8z9.fsf@simenheg@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="120204"; mail-complaints-to="usenet@ciao.gmane.io" Cc: acm@muc.de, 41897@debbugs.gnu.org To: Simen =?UTF-8?Q?Heggest=C3=B8yl?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Jun 22 21:18:30 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jnRxd-000V3b-T3 for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 22 Jun 2020 21:18:30 +0200 Original-Received: from localhost ([::1]:53262 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jnRxZ-00076X-CO for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 22 Jun 2020 15:18:25 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51556) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jnRxD-000736-OE for bug-gnu-emacs@gnu.org; Mon, 22 Jun 2020 15:18:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:51252) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jnRxD-0006bG-EA for bug-gnu-emacs@gnu.org; Mon, 22 Jun 2020 15:18:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jnRxD-0007zs-9o for bug-gnu-emacs@gnu.org; Mon, 22 Jun 2020 15:18:03 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Alan Mackenzie Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 22 Jun 2020 19:18:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 41897 X-GNU-PR-Package: emacs Original-Received: via spool by 41897-submit@debbugs.gnu.org id=B41897.159285348230725 (code B ref 41897); Mon, 22 Jun 2020 19:18:03 +0000 Original-Received: (at 41897) by debbugs.gnu.org; 22 Jun 2020 19:18:02 +0000 Original-Received: from localhost ([127.0.0.1]:34563 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jnRxB-0007zP-JS for submit@debbugs.gnu.org; Mon, 22 Jun 2020 15:18:02 -0400 Original-Received: from colin.muc.de ([193.149.48.1]:63478 helo=mail.muc.de) by debbugs.gnu.org with smtp (Exim 4.84_2) (envelope-from ) id 1jnRx8-0007zB-Vw for 41897@debbugs.gnu.org; Mon, 22 Jun 2020 15:18:00 -0400 Original-Received: (qmail 72029 invoked by uid 3782); 22 Jun 2020 19:17:51 -0000 Original-Received: from acm.muc.de (p4fe15b7f.dip0.t-ipconnect.de [79.225.91.127]) by localhost.muc.de (tmda-ofmipd) with ESMTP; Mon, 22 Jun 2020 21:17:50 +0200 Original-Received: (qmail 11548 invoked by uid 1000); 22 Jun 2020 19:17:50 -0000 Content-Disposition: inline In-Reply-To: <87d05ta8z9.fsf@simenheg@gmail.com> X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:182291 Archived-At: Hello, Simen. We're gradually converging on working code. Since yesterday, the improvements are: (i) (slightly) better handling of auto-fill-mode, which is no longer automatically enabled; (ii) An enhancement to CC Mode, used by mhtml-mode and js-mode, which invalidates a CC Mode cache used by the filling code. This gets rid of a few strange looking bugs; (iii) The inclusion of "paragraph-" in the "crucial variables" thing, so that paragraph-start and paragraph-separate from js-mode get into mhtml-mode. On Sat, Jun 20, 2020 at 20:27:22 +0200, Simen Heggestøyl wrote: > Hi Alan, thanks for working on this. > Alan Mackenzie writes: > > The patch below fixes both these errors, and seems to allow your test > > case to work. > Yes, this seems to fix it in both emacs-27 and master, thanks. > However in emacs-27 with the patch applied I now get a strange > side-effect: After filling the comment, mhtml-mode seems to lose track > of where the JavaScript portion of the buffer is. Please see the > attached image. The position where point is should still be recognized > as HTML+JS, but it's recognized as plain HTML+ instead after > filling. This doesn't happen on master. I think this is now properly fixed. Would you please try the latest patch, which might, just might, be fully working code. As always, this is a diff on the code _without_ previous patches. I haven't tested bits of enclosed css-mode source - you're certainly better equipped to do this than I am. diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el index 8c8296fd6d..d8279ba4f0 100644 --- a/lisp/progmodes/cc-engine.el +++ b/lisp/progmodes/cc-engine.el @@ -3209,6 +3209,24 @@ c-truncate-lit-pos-cache c-semi-near-cache-limit (min c-semi-near-cache-limit pos) c-full-near-cache-limit (min c-full-near-cache-limit pos))) +(defun c-foreign-truncate-lit-pos-cache (beg _end) + "Truncate CC Mode's literal cache. + +This function should be added to the `before-change-functions' +hook by major modes that use CC Mode's filling functionality +without initializing CC Mode. Currently (2020-06) these are +js-mode and mhtml-mode." + (c-truncate-lit-pos-cache beg)) + +(defun c-foreign-init-lit-pos-cache () + "Initialize CC Mode's literal cache. + +This function should be called from the mode functions of major +modes which use CC Mode's filling functionality without +initializing CC Mode. Currently (2020-06) these are js-mode and +mhtml-mode." + (c-truncate-lit-pos-cache 1)) + ;; A system for finding noteworthy parens before the point. diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 04b449ecd2..5c50e2accd 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -4570,7 +4570,7 @@ js-mode ;; Comments (setq-local comment-start "// ") - (setq-local comment-start-skip "\\(//+\\|/\\*+\\)\\s *") + (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *") (setq-local comment-end "") (setq-local fill-paragraph-function #'js-fill-paragraph) (setq-local normal-auto-fill-function #'js-do-auto-fill) @@ -4591,6 +4591,8 @@ js-mode (setq imenu-create-index-function #'js--imenu-create-index) ;; for filling, pretend we're cc-mode + (c-foreign-init-lit-pos-cache) + (add-hook 'before-change-functions #'c-foreign-truncate-lit-pos-cache nil t) (setq-local comment-line-break-function #'c-indent-new-comment-line) (setq-local comment-multi-line t) (setq-local electric-indent-chars diff --git a/lisp/textmodes/mhtml-mode.el b/lisp/textmodes/mhtml-mode.el index 1ae07c0a30..bed0ced618 100644 --- a/lisp/textmodes/mhtml-mode.el +++ b/lisp/textmodes/mhtml-mode.el @@ -73,7 +73,9 @@ mhtml-tag-relative-indent (defconst mhtml--crucial-variable-prefix (regexp-opt '("comment-" "uncomment-" "electric-indent-" - "smie-" "forward-sexp-function" "completion-" "major-mode")) + "smie-" "forward-sexp-function" "completion-" "major-mode" + "adaptive-fill-" "fill-" "normal-auto-fill-function" + "paragraph-")) "Regexp matching the prefix of \"crucial\" buffer-locals we want to capture.") (defconst mhtml--variable-prefix @@ -94,6 +96,7 @@ mhtml--construct-submode (unless (variable-binding-locus 'font-lock-fontify-region-function) (setq-local font-lock-fontify-region-function #'font-lock-default-fontify-region)) + (dolist (iter (buffer-local-variables)) (when (string-match mhtml--crucial-variable-prefix (symbol-name (car iter))) @@ -255,17 +258,14 @@ mhtml--syntax-propertize sgml-syntax-propertize-rules)) (defun mhtml-syntax-propertize (start end) - ;; First remove our special settings from the affected text. They - ;; will be re-applied as needed. - (remove-list-of-text-properties start end - '(syntax-table local-map mhtml-submode)) - (goto-char start) - ;; Be sure to look back one character, because START won't yet have - ;; been propertized. - (unless (bobp) - (let ((submode (get-text-property (1- (point)) 'mhtml-submode))) - (if submode - (mhtml--syntax-propertize-submode submode end)))) + (let ((submode (get-text-property start 'mhtml-submode))) + ;; First remove our special settings from the affected text. They + ;; will be re-applied as needed. + (remove-list-of-text-properties start end + '(syntax-table local-map mhtml-submode)) + (goto-char start) + (if submode + (mhtml--syntax-propertize-submode submode end))) (sgml-syntax-propertize (point) end mhtml--syntax-propertize)) (defun mhtml-indent-line () @@ -333,6 +333,18 @@ mhtml-mode ;: Hack (js--update-quick-match-re) + ;; Setup the appropriate js-mode value of auto-fill-function. + (setf (mhtml--submode-crucial-captured-locals mhtml--js-submode) + (push (cons 'auto-fill-function + (if (and (boundp 'auto-fill-function) auto-fill-function) + #'js-do-auto-fill + nil)) + (mhtml--submode-crucial-captured-locals mhtml--js-submode))) + + ;; This mode might be using CC Mode's filling functionality. + (c-foreign-init-lit-pos-cache) + (add-hook 'before-change-functions #'c-foreign-truncate-lit-pos-cache nil t) + ;; This is sort of a prog-mode as well as a text mode. (run-hooks 'prog-mode-hook)) > -- Simen -- Alan Mackenzie (Nuremberg, Germany).