Add function to generate QR codes

This commit is contained in:
Bram Schoenmakers 2024-02-09 20:02:47 +01:00
parent 741f9a98b1
commit 73a87ed767
Signed by: bram
GPG key ID: 0CCD19DFDC63258F
2 changed files with 61 additions and 0 deletions

View file

@ -908,6 +908,40 @@ Finally, I attached this hook in three places:
#+end_src
Which covers my (potential) GPG/SSH usage within Emacs. Now, anytime a I perform a GPG / SSH operation, the $GPG_TTY variable is set properly and if needed, the passhprase prompt shows up in the minibuffer.
* Generate and show QR for the region or minibuffer input :emacs:
#+begin_src elisp :tangle gists/generate-qr.el
(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)
(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)))
(display-buffer (current-buffer))))
#+end_src
* Meta
** License

27
gists/generate-qr.el Normal file
View file

@ -0,0 +1,27 @@
(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)
(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)))
(display-buffer (current-buffer))))