From d40195126b33840bf4d198ffff5e2996e472e1c9 Mon Sep 17 00:00:00 2001 From: Gaurav Date: Sun, 13 Feb 2022 19:23:51 -0500 Subject: [PATCH] =?UTF-8?q?Remove=20direct=20keybindings=20of=20=C3=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.org | 7 +++-- litex-mode.el | 81 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 56 insertions(+), 32 deletions(-) diff --git a/README.org b/README.org index 6497f23..ef93ebf 100644 --- a/README.org +++ b/README.org @@ -191,11 +191,12 @@ y &=& 2 + 3 + 6 x \\ #+begin_src emacs-lisp :tangle yes (use-package litex-mode - :load-path "~/.emacs.d/myfunc/litex-mode/" + ;; :load-path "~/.emacs.d/myfunc/litex-mode/" :commands litex-mode :hook text-mode - :config (local-set-key (kbd "C-e") litex-key-map) - ) + :config + (local-set-key (kbd "C-e") litex-key-map) + (local-set-key (kbd "×") 'litex-insert-or-replace-x)) #+end_src * Contributing diff --git a/litex-mode.el b/litex-mode.el index 8deb34f..a1bb4a2 100644 --- a/litex-mode.el +++ b/litex-mode.el @@ -25,10 +25,12 @@ ;;; Commentary: ;; converts valid mathematical Lisp expressions to LaTeX snippets -;; improved from https://emacs.stackexchange.com/a/70360 -;; modified with help from https://gist.github.com/bpanthi977/4b8ece0eeff3bc05bb82275a23cbb56d +;; improved from https://emacs.stackexchange.com/a/70360 modified with +;; help from +;; https://gist.github.com/bpanthi977/4b8ece0eeff3bc05bb82275a23cbb56d -;; For detailed help visit github page: https://github.com/Atreyagaurav/litex-mode +;; For detailed help visit github page: +;; https://github.com/Atreyagaurav/litex-mode ;;; Code: (eval-when-compile (require 'pcase)) @@ -80,10 +82,12 @@ (defun litex-format-float (val) "Function that defines how float VAL is formatted in lisp2latex." - (if (or (< val litex-format-float-lower-limit) (> val litex-format-float-upper-limit)) + (if (or (< val litex-format-float-lower-limit) + (> val litex-format-float-upper-limit)) (let* ((exponent (floor (log val 10))) (front (/ val (expt 10 exponent)))) - (format (concat litex-format-float-string " \\times 10^{%d}") front exponent)) + (format (concat litex-format-float-string + " \\times 10^{%d}") front exponent)) (format litex-format-float-string val))) @@ -157,8 +161,10 @@ (and (symbolp next) (> (length (prin1-to-string next)) 1)) (numberp next))) - (princ (concat (format arg1-format - (litex-latex-maybe-enclose me)) " \\times ")) + (princ (concat + (format arg1-format + (litex-latex-maybe-enclose me)) + " \\times ")) (princ (format arg1-format (litex-latex-maybe-enclose me)))))))) @@ -189,8 +195,12 @@ (let ((base (car args)) (power (cadr args))) (if (listp base) - (format "(%s)^{%s}" (litex-lisp2latex-all base) (litex-lisp2latex-all power)) - (format "%s^{%s}" (litex-lisp2latex-all base) (litex-lisp2latex-all power))))) + (format "(%s)^{%s}" + (litex-lisp2latex-all base) + (litex-lisp2latex-all power)) + (format "%s^{%s}" + (litex-lisp2latex-all base) + (litex-lisp2latex-all power))))) (defun litex-format-args-sqrt (args) @@ -202,7 +212,9 @@ "Formatting function for setq function called with ARGS." (with-output-to-string (cl-loop for (a b . rest) on args by #'cddr do - (princ (format "%s = %s" (litex-lisp2latex-all a) (litex-lisp2latex-all b))) + (princ (format "%s = %s" + (litex-lisp2latex-all a) + (litex-lisp2latex-all b))) (when rest (princ "; "))))) @@ -222,7 +234,9 @@ (defun litex-format-args-default (func args) - "Default Formatting function, Call corresponding called with ARGS formatting function if available for FUNC passing ARGS as argument, else make a general format." + "Default Formatting function, Call corresponding called with +ARGS formatting function if available for FUNC passing ARGS as +argument, else make a general format." (let ((func-symbol (intern (format "litex-format-args-%s" func)))) (if (functionp func-symbol) (let ((litex-latex-maybe-enclose? @@ -234,7 +248,8 @@ (litex-latex-enclose-check-args args))) (format-string (concat (if known? "\\%s" "\\mathrm{%s}") (if enclose? "(%s)" " %s")))) - (format format-string func (mapconcat #'litex-lisp2latex-all args ",")))))) + (format format-string func + (mapconcat #'litex-lisp2latex-all args ",")))))) (defun litex-lisp2latex-all (form) @@ -282,7 +297,8 @@ (cond ((listp form) (if (cl-every #'numberp (cl-rest form)) (eval form) - (cons (cl-first form) (mapcar #'litex-solve-single-step (cl-rest form))))) + (cons (cl-first form) + (mapcar #'litex-solve-single-step (cl-rest form))))) ((functionp form) form) @@ -293,7 +309,8 @@ (defun litex-solve-all-steps (form) - "Solves all the steps of calculations in FORM expression and retuns a list of steps." + "Solves all the steps of calculations in FORM expression and +retuns a list of steps." (let ((solution (list form))) ;given expression (if @@ -318,11 +335,17 @@ (pcase expression (`(setq ,var ,exp) (concat - (format "%s%s" (litex-format-variable var) litex-steps-join-string) - (mapconcat format-func (litex-solve-all-steps exp) - (concat litex-steps-end-string litex-steps-join-string)))) - (_ (mapconcat format-func (litex-solve-all-steps expression) - (concat litex-steps-end-string litex-steps-join-string))))) + (format "%s%s" + (litex-format-variable var) + litex-steps-join-string) + (mapconcat format-func + (litex-solve-all-steps exp) + (concat litex-steps-end-string + litex-steps-join-string)))) + (_ (mapconcat format-func + (litex-solve-all-steps expression) + (concat litex-steps-end-string + litex-steps-join-string))))) (defun litex-eval-and-replace () @@ -431,7 +454,8 @@ Argument END end position of region." (defun litex-format-region-last (beg end) - "Format selected region as per format of last call to `litex-format-region`,BEG and END are region bounds." + "Format selected region as per format of last call to +`litex-format-region`,BEG and END are region bounds." (interactive (if (use-region-p) (list (region-beginning) (region-end)) (let ((bnd (bounds-of-thing-at-point 'sexp))) @@ -481,7 +505,8 @@ Argument END end position of region." (defun litex-insert-or-replace-x (beg end) - "If a region (BEG to END) is selected, replace * by \times otherwise insert \times instead of ×." + "If a region (BEG to END) is selected, replace * by \times +otherwise insert \times instead of ×." (interactive (if (use-region-p) (list (region-beginning) (region-end)) (list (point) (point)))) @@ -491,13 +516,10 @@ Argument END end position of region." (when (re-search-backward "*" beg) (replace-match "\\\\times")))) -(define-minor-mode litex-mode - "Minor mode for Calculations on lisp, and formatting on LaTeX." - :lighter " LiTeX" - :keymap (make-sparse-keymap)) - -;; you can choose to apply this keymap to some other key. +;; I'm not making a litex-mode-map because I don't want it to come by +;; default, user can choose to apply this keymap to some other key as +;; prefix key, like C-e in readme, or just map individual functions. (defvar litex-key-map (let ((keymap (make-sparse-keymap))) (define-key keymap (kbd "F") 'litex-format-region) @@ -514,8 +536,9 @@ Argument END end position of region." (define-key keymap (kbd "a") 'litex-solve-all-steps-eqnarray) keymap)) -;; can be used directly -(define-key litex-mode-map (kbd "×") 'litex-insert-or-replace-x) +(define-minor-mode litex-mode + "Minor mode for Calculations on lisp, and formatting on LaTeX." + :lighter " LiTeX") (provide 'litex-mode)