gists/gists/generate-qr.el

30 lines
1.1 KiB
EmacsLisp

(defun my/generate-qr (text &optional img)
"Generate a QR code from the region or given TEXT.
If no region is active, the TEXT defaults to the clipboard content.
When IMG is non-nil, generate an image instead of the default
UTF-8 representation.
This function relies on qrencode(1) being present in your $PATH."
(interactive
(let ((default-text (if (use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(my/clipboard-get))))
(list
(read-string (format-prompt "Text" default-text) nil nil default-text)
current-prefix-arg)))
(with-current-buffer (get-buffer-create "*qr*")
;; in case the buffer still exists and image-mode is active
(fundamental-mode)
(read-only-mode -1)
(erase-buffer)
(insert text)
(let ((file-format
(if (equal current-prefix-arg '(4)) "PNG" "UTF8")))
(call-process-region nil nil "qrencode" t t nil "-o" "-" "-t" file-format)
(when (and (display-graphic-p) (string= file-format "PNG"))
(image-mode)))
(read-only-mode)
(display-buffer (current-buffer))))