Fix use-package and straight interaction loading and mathpix recipe

This commit is contained in:
Dimitri Lozeve 2021-06-03 16:47:18 +02:00
parent 8316896f09
commit f613773b9c
2 changed files with 17 additions and 107 deletions

34
init.el
View file

@ -74,6 +74,22 @@
(let ((fill-column (point-max)))
(fill-region (region-beginning) (region-end) nil)))
;; Straight.el setup
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 5))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(load bootstrap-file nil 'nomessage))
(straight-use-package 'use-package)
;;; Built-in packages
(use-package paren
:config
@ -94,22 +110,6 @@
;; don't muck with special buffers
(setq uniquify-ignore-buffers-re "^\\*"))
;; Straight.el setup
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 5))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(load bootstrap-file nil 'nomessage))
(straight-use-package 'use-package)
;; Theme
(use-package base16-theme
:straight t
@ -510,7 +510,7 @@
(setq twittering-reverse-mode nil))
(use-package mathpix.el
:load-path "site-lisp"
:straight (:host github :repo "jethrokuan/mathpix.el")
;; You also need to configure `mathpix-app-id` and
;; `mathpix-app-key`, for instance in secrets.el
:bind

View file

@ -1,90 +0,0 @@
;;; mathpix.el --- Mathpix API from Emacs
;;; Commentary:
;; This package provides an interface similar to the Mathpix snipping tool.
;;
;; mathpix-screenshot prompts for a screenshot, which is sent for processing
;; via Mathpix's API.
;;
;; Heavily adapted from org-download.
(require 'json)
(defcustom mathpix-app-id nil
"App ID for Mathpix.")
(defcustom mathpix-app-key nil
"App key for Mathpix.")
;; From org-download
(defcustom mathpix-screenshot-method "gnome-screenshot -a -f %s"
"The tool to capture screenshots."
:type '(choice
(const :tag "gnome-screenshot" "gnome-screenshot -a -f %s")
(const :tag "scrot" "scrot -s %s")
(const :tag "gm" "gm import %s")
(const :tag "imagemagick/import" "import %s")
(const :tag "imagemagick/import + xclip to save to clipboard"
"export filename=\"%s\"; import png:\"$filename\" ;xclip -selection clipboard -target image/png -filter < \"$filename\" &>/dev/null")
(const :tag "xfce4-screenshooter" "xfce4-screenshooter -r -o cat > %s")
;; screenshot method in ms-windows, /capture=4 stands for interactive.
(const :tag "IrfanView" "i_view64 /capture=4 /convert=\"%s\"")
;; screenshot script in osx, -i stands for interactive,
;; press space key to toggle between selection and
;; window/application mode.
(const :tag "screencapture" "screencapture -i %s")
;; take an image that is already on the clipboard, for Linux
(const :tag "xclip"
"xclip -selection clipboard -t image/png -o > %s")
;; take an image that is already on the clipboard, for Windows
(const :tag "imagemagick/convert" "convert clipboard: %s")
(function :tag "Custom function")))
(defcustom mathpix-screenshot-file
(expand-file-name "mathpix.png" temporary-file-directory)
"The file to capture mathpix screenshots"
:type 'string)
(defconst mathpix-api-curl-command
"curl -s https://api.mathpix.com/v3/latex -X POST -H \"app_id: %s\" -H \"app_key: %s\" -H \"Content-Type: application/json\" --data \"{\\\"src\\\":\\\"%s\\\",\\\"formats\\\": [\\\"latex_styled\\\"],\\\"format_options\\\":{\\\"latex_styled\\\": {\\\"transforms\\\": [\\\"rm_spaces\\\"]}}}\""
"The shell executable command to retrieve the results.")
;; screenshot programs have exit-code of 0 even when screenshotting is cancelled.
;; To save API calls, we use the existence of the file as a check if the user
;; wants to continue. Hence, we must delete the file after each function call.
(defun mathpix-screenshot ()
"Capture screenshot and send result to Mathpix API."
(interactive)
(let ((default-directory "~"))
(make-directory (file-name-directory mathpix-screenshot-file) t)
(if (functionp mathpix-screenshot-method)
(funcall mathpix-screenshot-file mathpix-screenshot-file)
(shell-command-to-string
(format mathpix-screenshot-method mathpix-screenshot-file)))
(when (file-exists-p mathpix-screenshot-file)
(insert (mathpix-get-result mathpix-screenshot-file))
(delete-file mathpix-screenshot-file))))
(defun mathpix-get-b64-image (file)
"Returns the base-64 image string from file."
(with-temp-buffer
(insert-file-contents file)
(base64-encode-string (buffer-string) t)))
(defun mathpix-get-result (file)
"Sends the image to Mathpix API."
(let* ((file-extension (file-name-extension file))
(image-data (format "data:image/%s;base64,%s" file-extension (mathpix-get-b64-image file)))
(command (format mathpix-api-curl-command mathpix-app-id mathpix-app-key image-data))
(result (json-read-from-string (shell-command-to-string command))))
(let ((error (assoc-default 'error result)))
(if error
(progn
(message "%s" error)
"")
(assoc-default 'latex_styled result)))))
(provide 'mathpix)
;;; mathpix.el ends here