From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Rahul Martim Juliato Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] feat: add markdown-ts-mode Date: Fri, 28 Jun 2024 23:09:57 -0300 Message-ID: <8734owcxe2.fsf@gmail.com> References: <877cgs7m4e.fsf@gmail.com> <86frvga5zc.fsf@gnu.org> <87edazgt7v.fsf@gmail.com> <87h6ftvpzz.fsf@posteo.net> <87plugzxgx.fsf@gmail.com> <87frv7qfb6.fsf@posteo.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="12190"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Rahul Martim Juliato , Eli Zaretskii , Jostein =?utf-8?Q?Kj=C3=B8nigsen?= , Yuan Fu , "Ergus via Emacs development discussions." To: Philip Kaludercic Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Jun 29 08:19:08 2024 Return-path: Envelope-to: ged-emacs-devel@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 1sNRQR-0002xk-5n for ged-emacs-devel@m.gmane-mx.org; Sat, 29 Jun 2024 08:19:07 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sNRPa-0006iE-8i; Sat, 29 Jun 2024 02:18:14 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sNNXY-0003kD-BM for emacs-devel@gnu.org; Fri, 28 Jun 2024 22:10:12 -0400 Original-Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sNNXU-0007ez-B5; Fri, 28 Jun 2024 22:10:12 -0400 Original-Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1f65a3abd01so8835625ad.3; Fri, 28 Jun 2024 19:10:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719627003; x=1720231803; darn=gnu.org; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=HPz/GNSpnWqeBW4WG7pF6Q2s3QG2pE7IajBwE/wxjpk=; b=OpRGkGN/m0bzlHPNerSyMnK3IcSeHzxeKDFiS7/mHQ3Yyq9nVzG/vam0VvKZvcxJTQ MVzmMrlLfT5jqlc35yq+q8mX7vJnQuhdx9VVhDkoM+8TFhuX8nzxIJBHDhim3pWiVMgx mtfQWVPrNOd9QOEnYntvkGnGszYqzt63CaHoxlEs89oLaFij9GcwI2Y17FzbpObdb8GD wLTl/FV4LkWsaei4xWtjri8fGAUtzYUp3MkoJk2afBSxPVZEWCUiIzmt4FGpJIB9y2hM PlPcj7cKnCL3gnRC7puL1EAuIssTtBsyZKMaUoog5Csf+Qk6ieh+9FF9Yn9eZT0/DGIT KWew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719627003; x=1720231803; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=HPz/GNSpnWqeBW4WG7pF6Q2s3QG2pE7IajBwE/wxjpk=; b=hSTdMoh9QzVYhAArUi3CW4SBAfDq7taznW1bFAxm++nU/Qv4OhiIeIq4OWUPgok3Hk dqfi3g0FaaqGINMrBxL/CV4btymBB+ZvEkmBeD85cxY0zbl95FqPaRTySFAPKuaUQoTM Q4xlVq9ayOtW8twYDf44awB/1G3q4hAFgoyHBlVQHodauqEhzr8qyLxbjQgwVhdKJO+6 HIceHzM04/0l6cKUBTKyEqNzj6APxc4U6Cwl16kapT3CONOLt45BAE6QwOYNGRWKHkqE YpFG45/KzKbI7jeHEw5rWC6/79cVvm5RRdXckOJju1K3rzdO8aeDxMZ1WYwLoNmlUREY /D8Q== X-Forwarded-Encrypted: i=1; AJvYcCU9ZvJ5N9r/AvRnzAS949x5USHKtOM/k8kSjCgUMG4uLe5XarFzsB0E7bJo++6m4CJKKUAPKq70m98JDFQnn0ZFbmwtHIPPe8S6Zrzov4yyMzw= X-Gm-Message-State: AOJu0YzdQJLuD8XihQtN78a83oFpUp7hw6Kj7BhoQrkv6OkMyV2VzYAs /nfrrHkXP7o/ksm7IC59Hooe4FO0nTLjqTIzBtqrH4/I+hh/beyQR9I6ru+lyhg= X-Google-Smtp-Source: AGHT+IGDvit1sAZldhBceGoQYbS30PHSKuOx8dm476mPKpbhZWjSn9QzdAePaUCHly6wHn9kIlb44Q== X-Received: by 2002:a17:902:c406:b0:1f6:e11e:640e with SMTP id d9443c01a7336-1fa23add606mr210614045ad.4.1719627001815; Fri, 28 Jun 2024 19:10:01 -0700 (PDT) Original-Received: from debian ([2804:7f0:9182:e44f:f74f:7779:2dfd:daaa]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10d14b5sm22578915ad.27.2024.06.28.19.09.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jun 2024 19:10:01 -0700 (PDT) X-Google-Original-From: Rahul Martim Juliato In-Reply-To: <87frv7qfb6.fsf@posteo.net> (Philip Kaludercic's message of "Sat, 27 Apr 2024 10:18:37 +0000") Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=rahuljuliato@gmail.com; helo=mail-pl1-x632.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sat, 29 Jun 2024 02:18:12 -0400 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 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-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:320840 Archived-At: --=-=-= Content-Type: text/plain Philip Kaludercic writes: > Rahul Martim Juliato writes: > >>> So if I understand correctly, there is no keymap or any convenience >>> features that markdown-mode provides, because this is just the >>> beginning, right? A large part of major modes for me is not just the >>> syntax highlighting and integration into Emacs systems, but also >>> bindings that in the case of Markdown would insert links or add/remove >>> emphasis. I think it would be nice, if we could add these features in >>> the future, and re-use bindings from a package like AucTeX to build on >>> existing intuition (org-mode would be an alternative, but I am not a fan >>> or their choice of bindings). Some commands to "compile" and preview a >>> document would also be nice. >> >> >> Hello there again! >> >> Please find attached the current version of this patch. >> >> It now works with the "official" tree-sitter grammar for markdown: >> https://github.com/tree-sitter-grammars/tree-sitter-markdown >> >> Please note (as explained above) the `main branch' is not the default >> anymore, but `split_parser' is. >> >> They splitted the parser into two, one for the body of the document and >> another to inline. >> >> So, in order to test this patch, configure it with: >> >> (use-package markdown-ts-mode >> :ensure nil >> :defer t >> :mode ("\\.md\\'" . markdown-ts-mode) >> :config >> (add-to-list 'treesit-language-source-alist '(markdown >> "https://github.com/tree-sitter-grammars/tree-sitter-markdown" >> "split_parser" "tree-sitter-markdown/src")) >> (add-to-list 'treesit-language-source-alist '(markdown-inline >> "https://github.com/tree-sitter-grammars/tree-sitter-markdown" >> "split_parser" "tree-sitter-markdown-inline/src"))) > > I have said this before, and will say it again, > `treesit-language-source-alist' shouldn't clone moving git repositories > but download tarballs (which basically every git host provides as a > feature, since git archive is built-in) and use those to build the > library. These tarballs should be pinned in the -ts-mode.el files > themselves to make using these major modes more reliable. > This is a problem with all TS modes, isnt it? Or did I do something wrong on my patch? >> If you visit a markdown file treesit issues an error, as always, asking >> for the grammars. >> >> Then, install BOTH grammars: >> >> M-x treesit-install-language-grammar RET markdown RET >> M-x treesit-install-language-grammar RET markdown-inline RET >> >> Reload the `markdown-ts-mode'. >> >> And everything should be working fine :) >> >> >> @Philip, thanks for bringing that up! I completely agree with you; >> aiming for a fully-featured mode is our long-term objective. >> >> The idea here is to establish a foundation for supporting markdown files >> within Emacs, without relying on external packages. > > That sounds good. > >> From there, we can delve into discussions about editing and exporting >> features. For instance, we could consider mimicking bindings and prompts >> from org-mode, as well as exploring integrations if users opt to also >> utilize third-party markdown-mode. > > I would be interested in helping out or at the very least to comment on > the development going on, since I had started writing the same mode > myself a while back, but never got around to it since because I don't > understand how `treesit-font-lock-rules' works. > >> However, simply having the mode built-in reduces friction significantly >> for new ideas and collaborations :) > > And makes it easier for external packages to rely on the code. Usually > I'd also say that it makes Emacs more usable in offline environments, > but the fact that the tree-sitter grammars have to for now be downloaded > stands in the way of that :/ I agree with you Philip it is more usable in offline environments, but as all tree-sitter modes, they depend on those external libraries. But I have to say, I've been looking some distros including those libraries on their package repositories, so it should reduce (hopefully) some friction with it. --- Sorry for the long hiatus on this thread. I was actually waiting for more discussions but many cooler topics (and also life) got in the way :) I haven't made any changes on my last patch, but I am attaching it here to sync with this message. So is there anything else I could do about it? --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-feat-add-markdown-ts-mode.patch Content-Description: markdown-ts-mode >From a1d071285b3ea989f8722b0b2915cf881b59b84c Mon Sep 17 00:00:00 2001 From: Rahul Martim Juliato Date: Fri, 19 Apr 2024 23:21:20 -0300 Subject: [PATCH] feat: add markdown-ts-mode * lisp/textmodes/markdown-ts-mode.el: New file * doc/emacs/emacs.texi: Add Markdown to the manual * etc/NEWS: Announce markdown-ts-mode --- doc/emacs/emacs.texi | 1 + etc/NEWS | 5 ++ lisp/textmodes/markdown-ts-mode.el | 113 +++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 lisp/textmodes/markdown-ts-mode.el diff --git a/doc/emacs/emacs.texi b/doc/emacs/emacs.texi index 7d77f13ab21..244c822ee04 100644 --- a/doc/emacs/emacs.texi +++ b/doc/emacs/emacs.texi @@ -618,6 +618,7 @@ Top * Enriched Text:: Editing text enriched with fonts, colors, etc. * Text Based Tables:: Commands for editing text-based tables. * Two-Column:: Splitting text columns into separate windows. +* Markdown:: Major mode for editing Markdown files. Filling Text diff --git a/etc/NEWS b/etc/NEWS index 8ad1e78ca60..06fbaa03b55 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1688,6 +1688,11 @@ A major mode based on the tree-sitter library for editing Elixir files. *** New major mode 'lua-ts-mode'. A major mode based on the tree-sitter library for editing Lua files. ++++ +*** New major mode 'markdown-ts-mode'. +A major mode based on the tree-sitter library for editing Markdown files. + + ** Minibuffer and Completions +++ diff --git a/lisp/textmodes/markdown-ts-mode.el b/lisp/textmodes/markdown-ts-mode.el new file mode 100644 index 00000000000..49fe1baf011 --- /dev/null +++ b/lisp/textmodes/markdown-ts-mode.el @@ -0,0 +1,113 @@ +;;; markdown-ts-mode.el --- tree sitter support for Markdown -*- lexical-binding: t; -*- + +;; Copyright (C) 2024 Free Software Foundation, Inc. + +;; Author : Rahul Martim Juliato +;; Maintainer : Rahul Martim Juliato +;; Created : April 2024 +;; Keywords : markdown md languages tree-sitter + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Commentary: +;; + +;;; Code: + +(require 'treesit) +(require 'subr-x) + +(declare-function treesit-node-parent "treesit.c") +(declare-function treesit-node-type "treesit.c") +(declare-function treesit-parser-create "treesit.c") + +(defvar markdown-ts--treesit-settings + (treesit-font-lock-rules + :language 'markdown + :override t + :feature 'delimiter + '([ "[" "]" "(" ")" ] @shadow) + + + :language 'markdown + :feature 'paragraph + '([((setext_heading) @font-lock-keyword-face) + ((atx_heading) @font-lock-keyword-face) + ((thematic_break) @shadow) + ((indented_code_block) @font-lock-string-face) + (list_item (list_marker_star) @font-lock-keyword-face) + (list_item (list_marker_plus) @font-lock-keyword-face) + (list_item (list_marker_minus) @font-lock-keyword-face) + (list_item (list_marker_dot) @font-lock-keyword-face) + (fenced_code_block (fenced_code_block_delimiter) @font-lock-doc-face) + (fenced_code_block (code_fence_content) @font-lock-string-face) + ((block_quote_marker) @font-lock-string-face) + (block_quote (paragraph) @font-lock-string-face) + (block_quote (block_quote_marker) @font-lock-string-face) + ]) + + :language 'markdown-inline + :feature 'paragraph-inline + '([ + ((image_description) @link) + ((link_destination) @font-lock-string-face) + ((code_span) @font-lock-string-face) + ((emphasis) @underline) + ((strong_emphasis) @bold) + (inline_link (link_text) @link) + (inline_link (link_destination) @font-lock-string-face) + (shortcut_link (link_text) @link)]))) + +(defun markdown-ts-imenu-node-p (node) + "Check if NODE is a valid entry to imenu." + (equal (treesit-node-type (treesit-node-parent node)) + "atx_heading")) + +(defun markdown-ts-imenu-name-function (node) + "Return an imenu entry if NODE is a valid header." + (let ((name (treesit-node-text node))) + (if (markdown-ts-imenu-node-p node) + (thread-first (treesit-node-parent node)(treesit-node-text)) + name))) + +(defun markdown-ts-setup () + "Setup treesit for `markdown-ts-mode'." + (setq-local treesit-font-lock-settings markdown-ts--treesit-settings) + (treesit-major-mode-setup)) + +;;;###autoload +(define-derived-mode markdown-ts-mode text-mode "Markdown" + "Major mode for editing Markdown using tree-sitter grammar." + (setq-local font-lock-defaults nil + treesit-font-lock-feature-list '((delimiter) + (paragraph) + (paragraph-inline))) + + (setq-local treesit-simple-imenu-settings + `(("Headings" markdown-ts-imenu-node-p nil markdown-ts-imenu-name-function))) + + (when (treesit-ready-p 'markdown) + (treesit-parser-create 'markdown-inline) + (treesit-parser-create 'markdown) + (markdown-ts-setup))) + +(derived-mode-add-parents 'markdown-ts-mode '(markdown-mode)) + +(if (treesit-ready-p 'markdown) + (add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-ts-mode))) + +(provide 'markdown-ts-mode) +;;; markdown-ts-mode.el ends here -- 2.39.2 --=-=-=--