Skip to content

Instantly share code, notes, and snippets.

@rhaps0dy
Created October 12, 2020 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhaps0dy/03333c764f4180b540d58fc66505f6a3 to your computer and use it in GitHub Desktop.
Save rhaps0dy/03333c764f4180b540d58fc66505f6a3 to your computer and use it in GitHub Desktop.
LaTeX -> unicode substitution as used in Julia
;;; packages.el --- latexsub layer packages file for Spacemacs.
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Adrià Garriga Alonso <adria@k9>
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defconst latexsub-packages
'(julia-mode
company))
;; obtained using the following, and inspecting:
; (apply #'string (sort (delete-dups (string-to-list
; (apply #'concat (hash-table-keys julia-mode-latexsubs)))) #'<))
(defconst latexsub//chars "()+/=\\^\\-_[:alnum:]")
(defconst latexsub//keys nil)
(defconst latexsub//reverse-lookup nil)
(defcustom latexsub/company-major-modes
'(python-mode org-mode julia-mode fundamental-mode text-mode)
"Major modes where latexsub/company will be an active backend"
:group 'latexsub)
(defun latexsub/add-capf ()
(add-hook 'completion-at-point-functions #'latexsub/completion-at-point nil t))
(defun latexsub/remove-capf ()
(remove-hook 'completion-at-point-functions #'latexsub/completion-at-point t))
(defun latexsub/info ()
(interactive)
(message "completion-at-point-functions: %s, company-backends: %s"
completion-at-point-functions company-backends))
(define-minor-mode latexsub-mode
"substitute your LaTeX expressions for Unicode characters"
nil ; init-value
" TeXsub" ; lighter
nil ; keymap
(progn
(message "Calling latexsub-mode %s" latexsub-mode)
(if latexsub-mode (latexsub/add-capf) (latexsub/remove-capf))))
(defun latexsub/init-julia-mode ()
(use-package julia-mode)
; populate lookup tables and keys
(setq latexsub//keys (hash-table-keys julia-mode-latexsubs))
(setq latexsub//reverse-lookup
(make-hash-table :test 'equal :size (length latexsub//keys)))
(maphash
(lambda (k v) (puthash (aref v 0) k latexsub//reverse-lookup))
julia-mode-latexsubs))
(defun latexsub//key-until-point ()
"Get bounds for latexsub key (e.g. \\lambda). nil if key does not start with \\"
(let ((beg (save-excursion
(skip-chars-backward latexsub//chars (- (point) 25))
(backward-char)
(point))))
(when (eq ?\\ (char-after beg))
(list beg (point)))))
(defun latexsub/completion-at-point ()
"Fake completion-at-point latexsub function. Never actually
returns completions. However, if point is at the end of a valid
LaTeX key, it substitutes it for the corresponding
unicode (\\lambda -> λ)"
(interactive)
(let ((bounds (latexsub//key-until-point)))
(when bounds
(let* ((key (apply #'buffer-substring-no-properties bounds))
(value (gethash key julia-mode-latexsubs)))
(when value
(apply #'delete-region bounds)
(insert value)))))
nil)
(defun latexsub/company (command &optional arg &rest ignored)
"Company backend for latexsub"
(interactive (list 'interactive))
(pcase command
(`interactive (company-begin-backend 'latexsub/company))
(`prefix (let ((bounds (latexsub//key-until-point)))
(when bounds
(apply #'buffer-substring-no-properties bounds))))
(`candidates (remove-if-not
(lambda (c) (string-prefix-p arg c))
latexsub//keys))
(`annotation (format " %s" (gethash arg julia-mode-latexsubs)))
(`post-completion (latexsub/completion-at-point))))
(defun latexsub/help-symbol-at-point (char)
(interactive (list (char-after (point))))
(let ((key (gethash char latexsub//reverse-lookup)))
(if key
(message "To write \"%c\", use \"%s\"." char key)
(message "No LaTeX key for \"%c\" found." char)
)))
(defun latexsub/post-init-company ()
(spacemacs/set-leader-keys-for-minor-mode 'latexsub-mode
"hs" 'latexsub/help-symbol-at-point)
(eval
`(spacemacs|add-company-backends
:backends latexsub/company
:modes ,@latexsub/company-major-modes))
(add-hook 'prog-mode-hook #'latexsub-mode t)
(add-hook 'text-mode-hook #'latexsub-mode t))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment