Initial attempt

This commit is contained in:
TEC 2022-08-12 01:35:42 +08:00
commit c74a011d0e
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
2 changed files with 124 additions and 0 deletions

13
README.org Normal file
View File

@ -0,0 +1,13 @@
#+title: Simple Comment Markup
#+author: tecosaur
#+html: <p><img src="https://img.shields.io/badge/Emacs-26.3+-blueviolet.svg?style=flat-square&logo=GNU%20Emacs&logoColor=white">
#+html: <img src="https://img.shields.io/badge/stage-%CE%B1,%20experimental-red?style=flat-square">
#+html: <a href="https://www.buymeacoffee.com/tecosaur"><img src="https://img.shields.io/badge/Buy_me_a_coffee-FFDD00?style=flat-square&logo=buy-me-a-coffee&logoColor=black"></a></p>
I feel like it's pretty natural to sprinkle a little bit of text markup into
code comments. It's unfortunate that they don't benefit from any extra
fontification.
For those that would appreciate a light bit of markup fontification, one can now
~(require 'simple-comment-markup)~.

111
simple-comment-markup.el Normal file
View File

@ -0,0 +1,111 @@
;;; simple-comment-markup.el --- Simple markup fontification in comments -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022 TEC
;;
;; Author: TEC <https://git.tecosaur.net/tec>
;; Maintainer: TEC <contact@tecosaur.net>
;; Created: August 11, 2022
;; Modified: August 11, 2022
;; Version: 0.0.1
;; Keywords: faces
;; Homepage: https://github.com/tecosaur/simple-comment-markup
;; Package-Requires: ((emacs "26.3"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Simple markup fontification in comments
;;
;;; Code:
(defgroup simple-comment-markup nil
"Simple markup fontification in comments."
:group 'text
:prefix "simple-comment-markup-")
(defcustom simple-comment-markup-patterns
'((org
("*" "*" t bold)
("/" "/" t italic)
("_" "_" t underline)
("~" "~" t font-lock-doc-markup-face)
("=" "=" t font-lock-doc-markup-face))
(markdown
("*" "*" nil italic)
("_" "_" nil italic)
("**" "**" nil bold)
("__" "__" nil bold)
("***" "***" nil (bold italic))
("`" "`" nil font-lock-doc-markup-face))
(markdown-code
("`" "`" nil font-lock-doc-markup-face)))
"dostring"
:type '(alist :key-type (symbol :tag "Name")
:value-type
(repeat
(list
(string :tag "Start")
(string :tag "End")
(boolean :tag "Hide markers")
(choice face (set sexpr) :tag "Face")))))
(defcustom simple-comment-markup-pre-rx "[ \t\\-({'\"]"
"TODO"
:type 'regexp)
(defcustom simple-comment-markup-post-rx "\\(?:[ \t\\-.,;:!?'\")\\[}]\\|$\\)"
"TODO"
:type 'regexp)
(defcustom simple-comment-markup-set 'org
"docstring"
:type '(choice symbol (repeat symbol)))
(defun simple-comment-markup--get-patterns (&optional set)
(let ((set (or set simple-comment-markup-set)))
(apply #'append
(mapcar
(lambda (pset)
(alist-get pset simple-comment-markup-patterns))
(if (consp set) set (list set))))))
(defun simple-comment-markup--construct-font-lock (&optional prefix-rx set)
(let ((prefix-rx (or prefix-rx
(concat "^[[:blank:]]*" (regexp-quote comment-start)))))
(list
(apply #'append
(list prefix-rx)
(mapcar
(lambda (pattern-spec)
(cl-destructuring-bind (start end hide-markers face) pattern-spec
`((,(format "%s\\(%s\\)\\([^[:blank:]][^\n]*[^[:blank:]]\\)\\(%s\\)%s"
simple-comment-markup-pre-rx
(regexp-quote start) (regexp-quote end)
simple-comment-markup-post-rx)
(save-excursion (up-list) (point))
t
,@(and hide-markers '((1 '(face nil invisible t))))
(2 ',face t)
,@(and hide-markers '((3 '(face nil invisible t))))))))
(simple-comment-markup--get-patterns set))))))
(defvar simple-comment-markup--font-lock-keywords nil)
;;;###autoload
(define-minor-mode simple-comment-markup-mode
"Simple markup fontification in comments."
:global nil
:group 'simple-comment-markup
(cond
(simple-comment-markup-mode
(setq simple-comment-markup--font-lock-keywords
(simple-comment-markup--construct-font-lock))
(font-lock-add-keywords nil simple-comment-markup--font-lock-keywords 'append))
(t (font-lock-remove-keywords nil simple-comment-markup--font-lock-keywords)))
(save-restriction
(widen)
(font-lock-flush)))
(provide 'simple-comment-markup)
;;; simple-comment-markup.el ends here