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: [PATCH] feat: add markdown-ts-mode Date: Sat, 20 Apr 2024 00:23:45 -0300 Message-ID: <877cgs7m4e.fsf@gmail.com> 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="21121"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Apr 20 07:52:12 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 1ry3dz-0005J1-NO for ged-emacs-devel@m.gmane-mx.org; Sat, 20 Apr 2024 07:52:11 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ry3cn-0005dY-Ex; Sat, 20 Apr 2024 01:50:57 -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 1ry1KU-0001v2-Gs for emacs-devel@gnu.org; Fri, 19 Apr 2024 23:23:54 -0400 Original-Received: from mail-oo1-xc35.google.com ([2607:f8b0:4864:20::c35]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ry1KQ-00019o-SH for emacs-devel@gnu.org; Fri, 19 Apr 2024 23:23:53 -0400 Original-Received: by mail-oo1-xc35.google.com with SMTP id 006d021491bc7-5aa362cc2ccso1848500eaf.3 for ; Fri, 19 Apr 2024 20:23:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713583428; x=1714188228; darn=gnu.org; h=mime-version:user-agent:message-id:date:subject:to:from:from:to:cc :subject:date:message-id:reply-to; bh=BrQ8lZOq2SiHO+R8R9sw2d2YFhV2apXlp1J/P8QZEMg=; b=biDhESZqmrujMLWfCpOxm2kYzgSqWxAcVFQUscEnpAU3xdEYI1v2xfzsezfuWrk5vP Pa16KVtczVpfDMTGXtxAjDGR8h3gyQ9JZkfU+Zc15i5emi7k29DProA08hkEAEJzuVtN HXRn8JpjlhsRJmPPz2eox+pxoY6HtO9kPLu1F8v9KL9mT/SME8D4Eg/yPUf3F5kDhOin 5AwNnz0k+zS1Gw6NJ1iJg+bopD1nQapWFSlOunK3uMvU9tDMHCbliltT4aNZVqCldHx3 orWj1D+3ZPOJk/XISuNjrajnDBcG1CkHtc5hDeGn+ILGhXS1HJLHQ6MxLvH7bb8HP76e 5k7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713583428; x=1714188228; h=mime-version:user-agent:message-id:date:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BrQ8lZOq2SiHO+R8R9sw2d2YFhV2apXlp1J/P8QZEMg=; b=CxMscecLK6GhaAwza04Kg3I5fj/F0sLoIupGPjidUOVt31LeJJvY/SzgcBSa+4Y9fP Fwd7LWHqrsqe787Vm/y1TzWf+Lgv8djMoQeSSxim2oBif62DQAbASM10ITXGKd4q/47x /UPdo9wHBzqEvviFng+1m1ykpNOdu8kf1r2LiM/Y3Ch7F7dsEThbwMeZ0vyBe7gkjCSv GTvbAMC4Pjo/sqJaT7ls7BAk3m77+FfuI/TizcwG3Dq400x+nZN4m79Wpwk6rxmJKGeX mwlLkosbQ4WwX6nkEIBucJgUt49FxfArHZD2kXVWGF3sRUzguilrF/Bs738VSiQ/wLIg n0Og== X-Gm-Message-State: AOJu0YyTgLhnIxGukp7jFSBCAoo+Zthb/AVlk/kcV6Qnhga91OVkIdFK 1tGgMbDrXvoNYk7Br+GDDPnS8vFHjlBM48lB5riKOjIKyRuhQR2UuXXssw== X-Google-Smtp-Source: AGHT+IHdVRUFHKg8XF0HLFN82yLIR5BjjcvQc+8poC0nDgcJSeNNEg2Gq0cfhuixIwC/gvpt8KPGnQ== X-Received: by 2002:a05:6358:793:b0:186:1193:8ccd with SMTP id n19-20020a056358079300b0018611938ccdmr5062148rwj.23.1713583428500; Fri, 19 Apr 2024 20:23:48 -0700 (PDT) Original-Received: from debian ([2804:7f0:9182:dfe8:a75:b0ca:67e7:6a46]) by smtp.gmail.com with ESMTPSA id a22-20020a17090acb9600b002a4ce78e3e8sm5604426pju.35.2024.04.19.20.23.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 20:23:47 -0700 (PDT) X-Google-Original-From: Rahul Martim Juliato Received-SPF: pass client-ip=2607:f8b0:4864:20::c35; envelope-from=rahuljuliato@gmail.com; helo=mail-oo1-xc35.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, 20 Apr 2024 01:50:56 -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:317860 Archived-At: --=-=-= Content-Type: text/plain Hello there! I've been using Emacs without any extra packages as an educational experiment after years of package hording. One of the few things I've been missing is a way of displaying some sort of syntax highlight for markdown documents. It feels a bit frustrating opening a README.md file with a single face color, since Emacs from the box can handle so much. I am sure others might have shared this feeling before. This patch is a modified version of a package I've recently published on MELPA, screenshots are available here: https://github.com/LionyxML/markdown-ts-mode It is a very basic mode that provides syntax highlight and imenu support using treesitter grammar from https://github.com/tree-sitter-grammars/tree-sitter-markdown The idea here is to provide minimal support to Emacs and continue building up features in the future. It is the first time I contribute to Emacs devel, so please let me know if I did something wrong or anything is not at the expected standards. ----- Rahul --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-feat-add-markdown-ts-mode.patch >From 364f61b03d601d2cb3aeb1687da2d1b2a232474c 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 --- etc/NEWS | 5 ++ lisp/progmodes/markdown-ts-mode.el | 106 +++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 lisp/progmodes/markdown-ts-mode.el diff --git a/etc/NEWS b/etc/NEWS index 8ad1e78ca60..ea0640337fb 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/progmodes/markdown-ts-mode.el b/lisp/progmodes/markdown-ts-mode.el new file mode 100644 index 00000000000..a20d754f83e --- /dev/null +++ b/lisp/progmodes/markdown-ts-mode.el @@ -0,0 +1,106 @@ +;;; 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 + '([((atx_heading) @font-lock-keyword-face) + ((block_quote_marker) @font-lock-string-face) + ((code_span) @font-lock-string-face) + ((emphasis) @underline) + ((image_description) @link) + ((indented_code_block) @font-lock-string-face) + ((link_destination) @font-lock-string-face) + ((setext_heading) @font-lock-keyword-face) + ((strong_emphasis) @bold) + ((thematic_break) @shadow) + (block_quote (block_quote_marker) @font-lock-string-face) + (block_quote (paragraph) @font-lock-string-face) + (fenced_code_block (code_fence_content) @font-lock-string-face) + (fenced_code_block (fenced_code_block_delimiter) @font-lock-doc-face) + (inline_link (link_destination) @font-lock-string-face) + (inline_link (link_text) @link) + (list_item (list_marker_dot) @font-lock-keyword-face) + (list_item (list_marker_minus) @font-lock-keyword-face) + (list_item (list_marker_plus) @font-lock-keyword-face) + (list_item (list_marker_star) @font-lock-keyword-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 fundamental-mode "markdown[ts]" + "Major mode for editing Markdown using tree-sitter grammar." + (setq-local font-lock-defaults nil + treesit-font-lock-feature-list '((delimiter) + (paragraph))) + + (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) + (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 --=-=-=--