From 2af840342c242fe1f40f033936d3eaa9e7a9aab5 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sat, 1 Jul 2023 23:55:05 +1000 Subject: Adding more faces to wiki mode, as well as rendering the links like in org mode --- emacs/.emacs.d/lisp/my/wiki-faces.el | 31 +++++++++++++++ emacs/.emacs.d/lisp/my/wiki.el | 74 ++++++++++++++++++++++++++++++++---- 2 files changed, 97 insertions(+), 8 deletions(-) (limited to 'emacs') diff --git a/emacs/.emacs.d/lisp/my/wiki-faces.el b/emacs/.emacs.d/lisp/my/wiki-faces.el index 2c35722..00a346a 100644 --- a/emacs/.emacs.d/lisp/my/wiki-faces.el +++ b/emacs/.emacs.d/lisp/my/wiki-faces.el @@ -73,5 +73,36 @@ (defvar wiki-level-6 'wiki-level-6 "Face used for level 6 headlines.") +(defface wiki-hr-face '((t :inherit font-lock-builtin-face)) + "Wiki hrule face") + +(defvar wiki-hr-face 'wiki-hr-face + "Wiki hrule face") + +(defface wiki-bold-italic '((t :inherit bold-italic)) + "Wiki bold-italic face") + +(defvar wiki-bold-italic 'wiki-bold-italic + "Wiki bold-italic face") + +(defface wiki-bold '((t :inherit bold)) + "Wiki bold face") + +(defvar wiki-bold 'wiki-bold + "Wiki bold face") + +(defface wiki-italic '((t :inherit italic)) + "Wiki italic face") + +(defvar wiki-italic 'wiki-italic "Wiki italic face") + +(defface wiki-pre-face '((t :inherit org-code)) "Wiki pre face") + +(defvar wiki-pre-face 'wiki-pre-face "Wiki pre face") + +(defface wiki-link '((t :inherit org-link)) "Wiki link face") + +(defvar wiki-link 'wiki-link "Wiki link face") + (provide 'wiki-faces) ;;; wiki-faces.el ends here diff --git a/emacs/.emacs.d/lisp/my/wiki.el b/emacs/.emacs.d/lisp/my/wiki.el index 2aace73..468244c 100644 --- a/emacs/.emacs.d/lisp/my/wiki.el +++ b/emacs/.emacs.d/lisp/my/wiki.el @@ -27,23 +27,81 @@ ;;; Code: (require 'wiki-faces) +(defvar wiki-link-re + (rx (seq "[[" + ;; Target + (group + (one-or-more + (not (any "[|]")))) + ;; Label (optional) + (opt (seq "|" (group (+? anything)))) + "]]")) + ;; "\\(\\[\\[\\)\\([^][|]+\\)\\(|\\)\\([^][|]+\\)\\(\\]\\]\\)" + ) + (defvar wiki-font-lock-keywords (list - (cons "^======.*======$" 'wiki-level-6) - (cons "^=====.*=====$" 'wiki-level-5) - (cons "^====.*====$" 'wiki-level-4) - (cons "^===.*===$" 'wiki-level-3) - (cons "^==.*==$" 'wiki-level-2) - (cons "^=.*=$" 'wiki-level-1) + (cons "^======.*======\\ *$" 'wiki-level-6) + (cons "^=====.*=====\\ *$" 'wiki-level-5) + (cons "^====.*====\\ *$" 'wiki-level-4) + (cons "^===.*===\\ *$" 'wiki-level-3) + (cons "^==.*==\\ *$" 'wiki-level-2) + (cons "^=.*=\\ *$" 'wiki-level-1) + (cons "^----+\\ *$" 'wiki-hr-face) + (cons "'''''[^ \t\n].*[^ \t\n]'''''" 'wiki-bold-italic) + (cons "'''[^ \t\n'].*[^ \t\n']'''" 'wiki-bold) + (cons "''[^ \t\n'].*[^ \t\n']''" 'wiki-italic) + (cons "^ .*$" 'wiki-pre-face) + ;; FIXME: not working, need to do something like `org-activate-links' + (cons "\\(\\[\\[\\)\\([^][|]+\\)\\(|\\)\\([^][|]+\\)\\(\\]\\]\\)" + '((1 invisible) + (2 invisible) + (3 invisible) + (4 wiki-link) + (5 invisible))) )) +(defvar wiki-font-lock-keywords nil) + +(defvar wiki-outline-regexp "=+.*=+\ *$") + +(defun wiki-outline-level () + (when (looking-at "\\(=+\\).*\\(=+\\)\ *$") + (min (length (match-string 1)) + (length (match-string 2)) + 6))) + +;; `org-activate-links' +(defun wiki-activate-links () + (save-excursion + (goto-char (point-min)) + (while (re-search-forward wiki-link-re nil t) + (let ((matched (match-data)) + (target (match-string 1)) + (maybe-label (match-string 2))) + (put-text-property + (nth 0 matched) + (nth (if maybe-label 4 2) matched) + 'invisible t) + (put-text-property + (nth (if maybe-label 5 3) matched) + (nth 1 matched) + 'invisible t) + (put-text-property + (nth (if maybe-label 4 2) matched) + (nth (if maybe-label 5 3) matched) + 'font-lock-face 'wiki-link))))) -(define-derived-mode wiki-mode text-mode "Wiki" +(define-derived-mode wiki-mode outline-mode "Wiki" "A wikitext mode." (setq-local comment-start "") - (setq-local font-lock-defaults '(wiki-font-lock-keywords t nil nil nil))) + ;; FIXME: this should not be necessary in outline mode + (setq-local font-lock-defaults '(wiki-font-lock-keywords t nil nil nil)) + (setq-local outline-regexp wiki-outline-regexp) + (setq-local outline-level 'wiki-outline-level) + (wiki-activate-links)) (provide 'wiki) ;;; wiki.el ends here -- cgit v1.2.3