From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tom Tromey Newsgroups: gmane.emacs.devel Subject: Re: html, css, and js modes working together Date: Thu, 09 Feb 2017 16:45:44 -0700 Message-ID: <877f4z6i8n.fsf@tromey.com> References: <87o9ynarz3.fsf@tromey.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1486684864 21288 195.159.176.226 (10 Feb 2017 00:01:04 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 10 Feb 2017 00:01:04 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1.91 (gnu/linux) Cc: Emacs discussions To: Tom Tromey Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Feb 10 01:00:55 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cbydy-0004yk-64 for ged-emacs-devel@m.gmane.org; Fri, 10 Feb 2017 01:00:54 +0100 Original-Received: from localhost ([::1]:40911 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cbye4-0008Ql-0C for ged-emacs-devel@m.gmane.org; Thu, 09 Feb 2017 19:01:00 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:38059) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cbyUs-0000Zm-O6 for emacs-devel@gnu.org; Thu, 09 Feb 2017 18:51:33 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cbyUo-0005fO-Ox for emacs-devel@gnu.org; Thu, 09 Feb 2017 18:51:30 -0500 Original-Received: from qproxy5-pub.mail.unifiedlayer.com ([69.89.21.30]:47200) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1cbyUn-0005cy-T6 for emacs-devel@gnu.org; Thu, 09 Feb 2017 18:51:26 -0500 Original-Received: (qmail 7379 invoked by uid 0); 9 Feb 2017 23:50:54 -0000 Original-Received: from unknown (HELO cmgw3) (10.0.90.84) by qproxy5.mail.unifiedlayer.com with SMTP; 9 Feb 2017 23:50:54 -0000 Original-Received: from box522.bluehost.com ([74.220.219.122]) by cmgw3 with id inll1u00T2f2jeq01nloV1; Thu, 09 Feb 2017 16:45:54 -0700 X-Authority-Analysis: v=2.1 cv=WOnsABcR c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=n2v9WMKugxEA:10 a=zstS-IiYAAAA:8 a=mDV3o1hIAAAA:8 a=C2l8gPeqm5nK_jSfH3EA:9 a=WyacwRpqJ6QA:10 a=L03L2QfmqWoA:10 a=NWVoK91CQyQA:10 a=4G6NA9xxw8l3yy4pmD5M:22 a=_FVE-zBwftR9WsbkzFJk:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Type:MIME-Version:Message-ID:In-Reply-To:Date: References:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=nbKGTMxyGhHogCoDkjOQsSESPM4ZJ91quecEqbkvGHA=; b=gcacV0tt1xRaCm6zKpW/0dNkcC lzWIveFRX35eYpZ8v3zFXyXCrSGI/OkZ3jOc7AGp+sYI5CaBI4Y2RP5WwH8GFYUqy1bYHY81OyqJn C/DcVKMnEGgyET5wB4I/LP3AY; Original-Received: from 174-16-146-181.hlrn.qwest.net ([174.16.146.181]:46162 helo=bapiya) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1cbyPJ-0006ul-Kp; Thu, 09 Feb 2017 16:45:45 -0700 X-Attribution: Tom In-Reply-To: <87o9ynarz3.fsf@tromey.com> (Tom Tromey's message of "Tue, 31 Jan 2017 13:34:40 -0700") X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box522.bluehost.com X-AntiAbuse: Original Domain - gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 174.16.146.181 X-Exim-ID: 1cbyPJ-0006ul-Kp X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 174-16-146-181.hlrn.qwest.net (bapiya) [174.16.146.181]:46162 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 69.89.21.30 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:212190 Archived-At: >>>>> "Tom" == Tom Tromey writes: Tom> This patch changes the html, css, and js modes to work together a bit. Here's the second version of this patch. I'd appreciate comments once again. A big thanks to everyone for their help so far. This version addresses most of the review comments. It also cleans up the implementation quite a bit (IMO anyway) and adds a couple new features: a mode-line highlighter (e.g., it says "HTML+JS" in a " + :syntax-table js-mode-syntax-table + :propertize #'js-syntax-propertize + :indenter #'js-indent-line + :keymap js-mode-map)) + +(defun mhtml--submode-lighter () + "Mode-line lighter indicating the current submode." + (let ((submode (get-text-property (point) 'mhtml-submode))) + (if submode + (mhtml--submode-name submode) + ""))) + +(defun mhtml--syntax-propertize-submode (submode end) + (save-excursion + (when (search-forward (mhtml--submode-end-tag submode) end t) + (setq end (match-beginning 0)))) + (set-text-properties (point) end + (list 'mhtml-submode submode + 'syntax-table (mhtml--submode-syntax-table submode) + ;; We want local-map here so that we act + ;; more like the sub-mode and don't + ;; override minor mode maps. + 'local-map (mhtml--submode-keymap submode) + 'cursor-sensor-functions + (list (lambda (_window _old-point _action) + (force-mode-line-update))))) + (funcall (mhtml--submode-propertize submode) (point) end) + (goto-char end)) + +(defun mhtml-syntax-propertize (start end) + (goto-char start) + (when (get-text-property (point) 'mhtml-submode) + (mhtml--syntax-propertize-submode (get-text-property (point) 'mhtml-submode) + end)) + (funcall + (syntax-propertize-rules + ("" + (0 (ignore + (goto-char (match-end 0)) + (mhtml--syntax-propertize-submode mhtml--css-submode end)))) + ("" + (0 (ignore + (goto-char (match-end 0)) + (mhtml--syntax-propertize-submode mhtml--js-submode end)))) + sgml-syntax-propertize-rules) + ;; Make sure to handle the situation where + ;; mhtml--syntax-propertize-submode moved point. + (point) end)) + +(defun mhtml-indent-line () + "Indent the current line as HTML, JS, or CSS, according to its context." + (interactive) + (let ((submode (save-excursion + (back-to-indentation) + (get-text-property (point) 'mhtml-submode)))) + (if submode + (save-restriction + (let* ((region-start (previous-single-property-change (point) + 'mhtml-submode)) + (base-indent (save-excursion + (goto-char region-start) + (sgml-calculate-indent)))) + (narrow-to-region region-start (point-max)) + (let ((prog-indentation-context (list base-indent + (cons (point-min) nil) + nil))) + (funcall (mhtml--submode-indenter submode))))) + ;; HTML. + (sgml-indent-line)))) + +;;;###autoload +(define-derived-mode mhtml-mode html-mode + '((sgml-xml-mode "XHTML+" "HTML+") (:eval (mhtml--submode-lighter))) + "Major mode based on `html-mode', but works with embedded JS and CSS. + +Code inside a