[Emacs] Add code for taking screenshots of Emacs

This commit is contained in:
Lucien Cartier-Tilet 2021-11-22 16:52:15 +01:00
parent e004e7d0c0
commit 0d24b4a056
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA

View File

@ -549,6 +549,63 @@ b s~, which bring the user directly to the ~*Messages*~ buffer and the
(switch-to-buffer "*scratch*"))
#+end_src
** Screenshots
:PROPERTIES:
:CUSTOM_ID: Custom-Elisp-Screenshots-l9bkib013aj0
:END:
Since Emacs27, it is possible for Emacs to take screenshots of itself
in various formats. Im mainly interested by the SVG and PNG format,
so Ill only write functions for these. It isnt really redundant with
the ~screenshot.el~ package used [[#Packages-Configuration-Applications-Screenshot96d1fl6184j0][here]] since these functions take a
screenshot of Emacs as a whole rather than of a code snippet.
First, we have a general function which is a slight modification of
the function shared by Alphapapa in [[https://www.reddit.com/r/emacs/comments/idz35e/emacs_27_can_take_svg_screenshots_of_itself/g2c2c6y/][this Reddit comment]]. It has been
modified so it is possible to pass the function an argument for the
format the screenshot will be taken as, and if ~type~ is ~nil~ the user
can still chose it.
#+begin_src emacs-lisp
(defun self-screenshot (&optional type)
"Save a screenshot of type TYPE of the current Emacs frame.
As shown by the function `', type can weild the value `svg',
`png', `pdf'.
This function will output in /tmp a file beginning with \"Emacs\"
and ending with the extension of the requested TYPE."
(interactive)
(let* ((type (if type type
(intern (completing-read "Screenshot Type: "
'(png svg pdf postscript)))))
(extension (pcase type
('png ".png")
('svg ".svg")
('pdf ".pdf")
('postscript ".ps")
(otherwise (error "Cannot export screenshot of type %s" otherwise))))
(filename (make-temp-file "Emacs-" nil extension))
(data (x-export-frames nil type)))
(with-temp-file filename
(insert data))
(kill-new filename)
(message filename)))
#+end_src
For conveniences sake, Ill also write two functions dedicated to
taking a screenshot in the SVG format and the PNG format respectively.
#+begin_src emacs-lisp
(defun self-screenshot-svg ()
"Save an SVG screenshot of Emacs.
See `self-screenshot'."
(interactive)
(self-screenshot 'svg))
(defun self-screenshot-png ()
"Save a PNG screenshot of Emacs.
See `self-screenshot'."
(interactive)
(self-screenshot 'png))
#+end_src
** Org Functions
:PROPERTIES:
:CUSTOM_ID: Custom-Elisp-Org-Functionsyshkel6184j0