;;; 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))) ;; --------------------------------------------------------------------- ;;; Code: (defgroup crucible () "Groups tools for assisting with superior & inferior typeface." :prefix "crucible-" :group 'crucible) ;; --------------------------------------------------------------------- (defgroup keyword-tfc () "Groups tools for typesetting mathematical symbols." :prefix "crucible-keyword" :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-signal t "Enables typeface for tex commands." :type 'boolean :group 'supinf) (put 'supinf-signal 'safe-local-variable 'booleanp) ;; --------------------------------------------------------------------- (defcustom deviat-supinf '(0.18 -0.18) "Sets the vertical shift for superior and inferior glyphs. Deviation is measured relative to the text font size." :group 'supinf :type '(list (float :tag "superior-deviation") (float :tag "inferior-deviation")) ) ;; --------------------------------------------------------------------- (defcustom height-scale-supinf 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 height-minimum-supinf 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 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 height-minimum-supinf) height-minimum-supinf (condition-case nil ; For bootstrapping. (* height-minimum-supinf (face-attribute 'default :height)) (error 0))) (* height height-scale-supinf)))) ; assumes height is integer. ;; --------------------------------------------------------------------- (defface supr-tfc '((t :height supinf-height)) "Typography for superior glyphs." :group 'supinf) ;; --------------------------------------------------------------------- (defface infr-tfc '((t :height supinf-height)) "Typography for inferior glyphs." :group 'supinf) ;; --------------------------------------------------------------------- (defun supinf-raise-cruc (pos) "Raise and lower typeface at position POS." (unless (or (memq (get-text-property pos 'face) '(font-lock-constant-face font-lock-builtin-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 display (raise ,(car deviat-supinf))) `(face infr-tfc display (raise ,(cadr deviat-supinf))) ))) ;; --------------------------------------------------------------------- (defun supinf-match (limit) "Match superior and inferior glyph patterns up to LIMIT." (when (and supinf-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)) ;; --------------------------------------------------------------------- (defconst supinf-font-lock-cruc (append '((supinf-match (1 (supinf-raise-cruc (match-beginning 0))) ))) "Experimental expressions to highlight in TeX modes.") ;; --------------------------------------------------------------------- (defun supinf-enable () "Enables a typeface for displaying tex commands." (setq supinf-signal t) (font-lock-add-keywords nil supinf-font-lock-cruc t)) ;; --------------------------------------------------------------------- (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)) deviat-supinf) (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-disable () "Disable supinf functionality." (interactive) (setq-local supinf-signal nil) (font-lock-flush)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; * Displays texmath glyphs for the corresponding tex commands. ;; _ _ _ _ ;; _ __ __ _| |_| |_ __ _| |_ _ _ __| |_ ___ ;; | ' \/ _` | _| ' \ / _` | | || | '_ \ ' \(_-< ;; |_|_|_\__,_|\__|_||_| \__, |_|\_, | .__/_||_/__/ ;; |___/ |__/|_| ;;(require 'prog-mode) (defun texglyph-predicate (start end _match) "Prettify mathematical expressions defined using 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-crucible () "Set keywords and corresponding glyph." (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-crucible) (prettify-symbols-mode)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; --------------------------------------------------------------------- (defvar-local texglyph-state-cruc nil) (defun texglyph-cycle-crucible () "Cycles supinf and texglyph functionality." (interactive) (pcase texglyph-state-cruc ;; ---------------------------------------------- (1 (setq supinf-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))) ;; ---------------------------------------------- (_ (supinf-disable) (prettify-symbols-mode 0) (message "Supinf and Glyphs Disabled") (setq-local texglyph-state-cruc 1)) )) ;; ----------------------------------------------------------------------- (defun crucible-prettify-hook () "Hooks." (add-hook 'crucible-mode-hook #'supinf-enable) (add-hook 'texinfo-mode-hook #'texglyph-crucible)) (global-set-key (kbd "H-t") #'texglyph-cycle-crucible) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (crucible-prettify-hook) (provide 'crucible-mode) ;;; crucible-mode.el ends here