;;; crucible-mode.el --- Typeface for texmath -*- lexical-binding:t -*- ;; Copyright (C) 2021 Free Software Foundation ;; Author: Christopher Dimech ;; URL: https://website-here ;; Version: 0.1.0 ;; Package-Requires: ((emacs "27.1")) ;; This file is part of Behistun, a Gnu Behistun. ;; Behistun 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. ;; Behintun 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 Affero General Public ;; License along with Behistun. If not, see ;; . ;;; ------------------------------------------------------------------- ;;; Commentary: ;; * Setting texmath keywords using rx ;; rx uses regular expressions that are easier to read. ;; ;; * Byte Compilation ;; ;; The Byte Compiler translates functions written in Lisp into ;; byte-code, a special representation that makes functions execute ;; faster. The byte compiler also returns very useful messages. ;; ;; To byte-compile a file named crucible-mode.el, launch Emacs and ;; enter the following command. ;; ;; M-x byte-compile-file Ret crucible-mode.el ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; (require 'rx) ; Is this needed (eval-when-compile (setq byte-compile-function-environment (delq (assq 'crucible-mode byte-compile-function-environment) byte-compile-function-environment))) ;; --------------------------------------------------------------------- (defgroup crucible () "Groups tools for assisting with superior & inferior typeface." :prefix "crucible-" :group 'crucible) ;; --------------------------------------------------------------------- (defgroup mtksy-kwd () "Groups tools for typesetting mathematical symbols." :prefix "crucible-mtksy" :group 'crucible) ;; --------------------------------------------------------------------- (defvar crucible-mode-hook nil "Hook called when Crucible Mode is enabled.") ;; --------------------------------------------------------------------- ;;;###autoload (define-minor-mode crucible-mode "Minor mode for assisting with superior & inferior typeface." :init-value nil :global nil :lighter " Crucible" (run-hooks 'crucible-mode-hook)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (multic " * Colour tex keywords") ;; _ _ _ ;; __ ___| |___ _ _ _ _ | |____ __ ___ _ __| | ;; / _/ _ \ / _ \ || | '_| | / /\ V V / '_/ _` | ;; \__\___/_\___/\_,_|_| |_\_\ \_/\_/|_| \__,_| ;; --------------------------------------------------------------------- (defface colour-tex-cruc '( (default :inherit bold) ( ((class color) (min-colors 88) (background light)) :foreground "#FF0000" ) ( ((class color) (min-colors 88) (background dark)) :foreground "#FF0000" ) (t :inherit font-lock-builtin-face) ) "Typeface for tex keywords.") ;; ------------------------------------------------------------------------- (defconst tex-keywords-cruc `( ;; Greek. Use (0 'colour-tex-cruc) to highlight the leading "\". (,(rx "\\" word-start (group (or "alpha" "beta" "chi" "delta" "Delta" "epsilon" "varepsilon" "eta" "gamma" "Gamma" "iota" "kappa" "lambda" "Lambda" "mu" "nu" "omega" "Omega" "phi" "varphi" "Phi" "pi" "varpi" "Pi" "psi" "Psi" "rho" "varrho" "sigma" "varsigma" "Sigma" "tau" "theta" "vartheta" "Theta" "upsilon" "Upsilon" "xi" "Xi" "zeta")) word-end) (1 'colour-tex-cruc)) ;; ;; Mathematical symbols (,(rx "\\" word-start (group (or "infty" "Re" "Im" "angle" "triangle" "backslash" "vert" "Vert" "emptyset" "bot" "top" "exists" "forall" "hbar" "ell" "aleph" "imath" "jmath" "nabla" "neg" "lnot" "prime" "partial" "surd" "wp" "flat" "sharp" "natural" "clubsuit" "diamondsuit" "heartsuit" "spadesuit")) word-end) (1 'colour-tex-cruc)) ;; ;; Binary operations (,(rx "\\" word-start (group (or "vee" "wedge" "amalg" "cap" "cup" "uplus" "sqcap" "sqcup" "dagger" "ddaccer" "land" "lor" "cdot" "diamond" "bullet" "circ" "bigcirc" "odot" "ominus" "oplus" "oslash" "otimes" "pm" "mp" "triangleleft" "triangleright" "bigtriangledown" "bigtriangleup" "ast" "star" "times" "div" "setminus" "wr")) word-end) (1 'colour-tex-cruc)) ;; ;; Mathematical relations (,(rx "\\" word-start (group (or "asymp" "cong" "dashv" "vdash" "perp" "mid" "parallel" "doteq" "equiv" "ge" "geq" "le" "leq" "gg" "ll" "models" "ne" "neq" "notin" "in" "ni" "owns" "prec" "preceq" "succ" "succeq" "bowtie" "propto" "approx" "sim" "simeq" "frown" "smile" "subset" "subseteq" "supset" "supseteq" "sqsubseteq" "sqsupseteq" "cos" "sin" "tan" "cot" "csc" "sec" "arccos" "arcsin" "arctan" "cosh" "coth" "sinh" "tanh" "det" "dim" "exp" "ln" "log" "lg" "arg" "deg" "gcd" "hom" "ker" "inf" "sup" "lim" "liminf" "limsup" "max" "min" "Pr")) word-end) (1 'colour-tex-cruc)) ;; ;; Delimeters (,(rx "\\" word-start (group (or "lbrace" "rbrace" "lbrack" "rbrack " "langle" "rangle" "lceil" "rceil" "lfloor" "rfloor")) word-end) (1 'colour-tex-cruc)) ;; ;; Arrows (,(rx "\\" word-start (group (or "leftarrow" "gets" "Leftarrow" "rightarrow " "to" "Rightarrow" "leftrightarrow" "Leftrightarrow" "longleftarrow" "Longleftarrow" "longrightarrow" "Longrightarrow" "longleftrightarrow" "Longleftrightarrow" "iff" "hookleftarrow" "hookrightarrow" "leftharpoondown" "rightharpoondown" "leftharpoonup" "rightharpoonup" "rightleftharpoons" "mapsto" "longmapsto" "downarrow" "Downarrow" "uparrow" "Uparrow" "updownarrow" "Updownarrow" "nearrow" "searrow" "nwarrow" "swarrow")) word-end) (1 'colour-tex-cruc)) ;; ;; Large mathematical operations (,(rx "\\" word-start (group (or "bigcap" "bigcup" "bigodot" "bigoplus" "bigotimes" "bigsqcup" "biguplus" "bigvee" "bigwedge" "coprod" "smallint" "int" "oint" "prod" "sum" "limits" "nolimits" "displaylimits")) word-end) (1 'colour-tex-cruc)) ;; ;; Fractions and stacking (,(rx "\\" word-start (group (or "over" "atop" "above" "choose" "brace" "cases" "underbrace" "overbrace" "underline" "overline" "overleftarrow" "overrightarrow" "buildrel" "ldots" "cdots" "vdots" "ddots" "lgroup" "rgroup" "left" "right" "delimiter" "delimiterfactor" "delimitershortfall" "matrix" "pmatrix" "bordermatrix" "sqrt" "root" "of")) word-end) (1 'colour-tex-cruc)) ;; ;; Punctuation and style (,(rx "\\" word-start (group (or "cdotp" "ldotp" "colon" "sb" "sp" "textstyle" "scriptstyle" "scriptscriptstyle" "displaystyle")) word-end) (1 'colour-tex-cruc)) ;; ;; Mathematical accents (,(rx "\\" word-start (group (or "acute" "b" "bar" "breve" "check" "ddot" "dot" "grave" "hat" "widehat" "tilde" "widetilde" "vec")) word-end) (1 'colour-tex-cruc)) ;; ;; Mathematical class (,(rx "\\" word-start (group (or "mathord" "mathop" "mathbin" "mathrel " "mathopen" "mathclose" "mathpunct")) word-end) (1 'colour-tex-cruc)) ;; ;; Equations and multiline display (,(rx "\\" word-start (group (or "eqno" "leqno" "displaylines" "eqalign " "eqalignno" "leqalignno")) word-end) (1 'colour-tex-cruc)) ;; ;; Big sizes. Using concat rather than rx. (,(concat "\\\\\\<\\(big\\|bigl\\|bigm\\|bigr\\|Big\\|Bigl\\|" "Bigm\\|Bigr\\|bigg\\|biggl\\|biggm\\|biggr\\|" "Bigg\\|Biggl\\|Biggm\\|Biggr\\|downbracefill\\|" "upbracefill\\|arrowvert\\|Arrowvert\\|lmoustache\\|" "rmoustache\\|bracevert\\)\\>") (1 'colour-tex-cruc)) ) "Fontification for letters and symbols.") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; * Positioning of superscripts and subscripts ;; _ __ ;; ____ _ _ __(_)_ _ / _| _ __ ___ ____ _ ;; (_-< || | '_ \ | ' \| _| | '_ \/ _ (_-< ' \ ;; /__/\_,_| .__/_|_||_|_| | .__/\___/__/_||_| ;; |_| |_| ;; --------------------------------------------------------------------- (defcustom supinf-fontify-signal t "Enables typeface for tex commands." :type 'boolean :group 'supinf) (put 'supinf-fontify-signal 'safe-local-variable 'booleanp) ;; --------------------------------------------------------------------- (defcustom crucible-supinf-vshift '(0.18 -0.18) "Sets vertical shift for superior and inferior glyphs. The first number is negative, whilst the second is positive. Shift is measured relative to the text font size." :group 'supinf :type '(list (float :tag "superior-vshift") (float :tag "inferior-vshift")) ) ;; --------------------------------------------------------------------- (defcustom crucible-height-ratio 0.8 "Sets relative supinf height ratio with the preceding text. For nested expressions, the setting is applied repeatedly, subject to the limit set by `crucible-height-minimum'." :type 'float :group 'supinf) ;; --------------------------------------------------------------------- (defcustom crucible-height-minimum 0.8 "Sets the minimum glyph height. For integer values, the height is in units of 1/10 point. Relative height is used for floating point values." :type '(choice (integer :tag "Font height in 1/10 point units") (float :tag "Fraction of font height")) :group 'supinf) ;; --------------------------------------------------------------------- (defun crucible-supinf-match (limit) "Match superior and inferior glyph patterns up to LIMIT." (when (and supinf-fontify-signal (re-search-forward "[_^] *\\([^\n\\{}]\\|\ \\\\\\([a-zA-Z@]+\\|[^ \t\n]\\)\\|\\({\\)\\)" limit t)) (when (match-end 3) (let ((beg (match-beginning 3)) (end (save-restriction (narrow-to-region (point-min) limit) (condition-case nil (scan-lists (point) 1 1) (error nil))))) ;; body of let (store-match-data (if end (list (match-beginning 0) end beg end) (list beg beg beg beg))))) t)) ;; --------------------------------------------------------------------- (defun crucible-supinf-height (height) "Return integer HEIGHT of superior and inferior glyph. Height is an integer that is 1/10 of point size. Not smaller than the value set by `crucible-height-minimum'." (ceiling (max (if (integerp crucible-height-minimum) crucible-height-minimum (condition-case nil ; For bootstrapping. (* crucible-height-minimum (face-attribute 'default :height)) (error 0))) (* height crucible-height-ratio)))) ; assumes height is integer. ;; --------------------------------------------------------------------- (defface supr-tfc-cruc '((t :height crucible-supinf-height)) "Typography for superior glyphs." :group 'supinf) ;; --------------------------------------------------------------------- (defface infr-tfc-cruc '((t :height crucible-supinf-height)) "Typography for inferior glyphs." :group 'supinf) ;; --------------------------------------------------------------------- (defun supinf-typeface-cruc (pos) "Fontify text at POS." (unless (or (memq (get-text-property pos 'face) '(font-lock-constant-face font-lock-builtin-face font-lock-comment-face)) ;; Check for backslash quoting (let ((odd nil) (pos pos)) (while (eq (char-before pos) ?\\) (setq pos (1- pos) odd (not odd))) odd)) (if (eq (char-after pos) ?^) `(face supr-tfc-cruc display (raise ,(car crucible-supinf-vshift))) `(face infr-tfc-cruc display (raise ,(cadr crucible-supinf-vshift))) ))) ;; --------------------------------------------------------------------- (defconst supinf-font-lock-cruc (append nil '( (crucible-supinf-match (1 (supinf-typeface-cruc (match-beginning 0)) append))) ) "Experimental expressions to highlight in TeX modes.") ;; --------------------------------------------------------------------- (defun supinf-disable-cruc (beg end) "Set typeface for text between BEG and END." (font-lock-default-unfontify-region beg end) (while (< beg end) (let ((next (next-single-property-change beg 'display nil end)) (prop (get-text-property beg 'display))) (if (and (eq (car-safe prop) 'raise) (member (car-safe (cdr prop)) crucible-supinf-vshift) (null (cddr prop))) (put-text-property beg next 'display nil)) (setq beg next)))) ;; --------------------------------------------------------------------- ;;(defun tex-typeface-crucible () ;; "Font-lock setup for mathematical symbols." ;; (font-lock-flush (point-min) (point-max)) ;; (if crucible-supinf-enable ;; (progn ;; (font-lock-add-keywords nil tex-keywords-cruc t) ;; (font-lock-add-keywords nil supinf-font-lock-cruc t)) ;; (font-lock-remove-keywords nil tex-keywords-cruc) ;; (supinf-disable-cruc (point-min) (point-max))) ;; (font-lock-flush (point-min) (point-max))) ;;(defun supinf-typeface-crucible () ;; "Font-lock setup for mathematical symbols." ;; (font-lock-flush (point-min) (point-max)) ;; ;; (if crucible-mode ;; (font-lock-add-keywords nil tex-keywords-cruc t) ;; (font-lock-remove-keywords nil tex-keywords-cruc)) ;; ;; (if crucible-supinf-enable ;; (font-lock-add-keywords nil supinf-font-lock-cruc t) ;; (supinf-disable-cruc (point-min) (point-max))) ;; ;; (font-lock-flush (point-min) (point-max))) (defun supinf-enable () "Enable the supinf typeface" (font-lock-flush (point-min) (point-max)) (font-lock-add-keywords nil supinf-font-lock-cruc t) (font-lock-flush (point-min) (point-max)) ) (defun supinf-disable () "Disable the supinf typeface" (font-lock-flush (point-min) (point-max)) (setq font-lock-defaults '(font-lock-unfontify-region-function . supinf-disable-cruc)) ;;(supinf-disable-cruc (point-min) (point-max)) (font-lock-flush (point-min) (point-max)) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; * Displays texmath glyphs for the corresponding tex commands. ;; _ _ _ _ ;; _ __ __ _| |_| |_ __ _| |_ _ _ __| |_ ___ ;; | ' \/ _` | _| ' \ / _` | | || | '_ \ ' \(_-< ;; |_|_|_\__,_|\__|_||_| \__, |_|\_, | .__/_||_/__/ ;; |___/ |__/|_| ;;(require 'prog-mode) (defun texglyph-predicate (start end _match) "Prettifies mathematical expressions written in tex syntax. Matches start with a backslash and end with a word constituent [a-zA-Z0-9]. Uses the prettify functionality provided by prog-mode." (not (or (memq (char-before start) '(?\\)) (memq (char-syntax (or (char-after end) ?\s)) '(?w)) (nth 8 (syntax-ppss))))) ;; --------------------------------------------------------------------- (defun texglyph-typeface-crucible () (push '("\\alpha" . ?α) prettify-symbols-alist) (push '("\\beta" . ?β) prettify-symbols-alist) (setq prettify-symbols-compose-predicate 'texglyph-predicate)) ;; --------------------------------------------------------------------- (defun texglyph-enable () "Enables typeface for tex glyphs." (texglyph-typeface-crucible) (prettify-symbols-mode)) ;; --------------------------------------------------------------------- (defvar-local texglyph-state-cruc nil) (defun texglyph-cycle-crucible () "Breaks lines when they get beyond fill-column." (interactive) (pcase texglyph-state-cruc ;; ---------------------------------------------- (1 (setq supinf-fontify-signal t) (supinf-enable) (message "Supinf Enabled") (setq-local texglyph-state-cruc (1+ texglyph-state-cruc))) ;; ---------------------------------------------- (2 (texglyph-enable) (message "Supinf and Glyphs Enabled") (setq-local texglyph-state-cruc (1+ texglyph-state-cruc))) ;; ---------------------------------------------- (_ (setq supinf-fontify-signal nil) ;;(supinf-disable) (setq-local supinf-fontify-signal nil) ;;(setq tex-font-script-display '(-0.0 0.0)) ;;(setq tex-suscript-height-ratio 0.0) (font-lock-flush) ;;(font-lock-refresh-defaults) ;;(prettify-symbols-mode 0) (message "Supinf and Glyphs Disabled") (setq-local texglyph-state-cruc 1)) )) ;; ----------------------------------------------------------------------- (defun texglyph-enableo () "Enables a typeface for displaying tex commands." (texglyph-cycle-crucible)) ;; ----------------------------------------------------------------------- (defun crucible-prettify-hook () "defun-todo" (add-hook 'crucible-mode-hook #'supinf-typeface-crucible) (add-hook 'texinfo-mode-hook #'texglyph-typeface-crucible) ) (global-set-key (kbd "H-t") #'texglyph-cycle-crucible) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;(texglyph-typeface-crucible) ;; (texglyph-crucible) ;; (crucible-prettify-hook) (provide 'crucible-mode) ;;; crucible-mode.el ends here ;; crucible-mode Highlight tex keywords with colour scheme ;; crucible-supinf-enable Enable supinf positioning ;; crucible-texglyph-enable Associate glyphs with tex keywords