Skip to content

Commit

Permalink
Merge pull request #3 from Atreyagaurav/bug-fixes
Browse files Browse the repository at this point in the history
Remove direct keybindings of ×
  • Loading branch information
Atreyagaurav authored Feb 14, 2022
2 parents be2f8a4 + d401951 commit 68ef7fc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
7 changes: 4 additions & 3 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 52 additions & 29 deletions litex-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)))


Expand Down Expand Up @@ -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))))))))

Expand Down Expand Up @@ -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)
Expand All @@ -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 "; ")))))


Expand All @@ -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?
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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 ()
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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))))
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 68ef7fc

Please sign in to comment.