docs(Emacs scripts): reorganize file

This commit is contained in:
Lucien Cartier-Tilet 2024-02-07 06:35:15 +01:00
parent 9c1fe06d26
commit 2ef0f4fc89
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA

View File

@ -23,102 +23,6 @@ buffer in new Emacs buffers.
(file-list)))) (file-list))))
#+end_src #+end_src
** Switch between buffers
Two default shortcuts I really like from Spacemacs are ~SPC b m~ and ~SPC
b s~, which bring the user directly to the ~*Messages*~ buffer and the
~*scratch*~ buffer respectively. These functions do exactly this.
#+begin_src emacs-lisp
(defun switch-to-messages-buffer ()
"Switch to Messages buffer."
(interactive)
(switch-to-buffer (messages-buffer)))
(defun switch-to-scratch-buffer ()
"Switch to Messages buffer."
(interactive)
(switch-to-buffer "*scratch*"))
#+end_src
** Screenshots
Since Emacs27, it is possible for Emacs to take screenshots of itself
in various formats. Im mainly interested in the SVG and PNG format,
so Ill only write functions for these. It isnt really redundant with
the ~screenshot.el~ package used [[file:./packages/applications.md#screenshot][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]]. I modified it
to make it possible to pass as an argument the format the screenshot
will be taken as or ask the user which format they would like to save
it as.
#+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 wield 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 (list
(intern (completing-read "Screenshot type: "
'(png svg pdf postscript)))))
(let* ((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
I used this function to take the screenshots you can see in this
document.
** Handle new windows
The two functions below allow the user to not only create a new window
to the right or below the current window (respectively), but also to
focus the new window immediately.
#+begin_src emacs-lisp
(defun split-window-right-and-focus ()
"Spawn a new window right of the current one and focus it."
(interactive)
(split-window-right)
(windmove-right))
(defun split-window-below-and-focus ()
"Spawn a new window below the current one and focus it."
(interactive)
(split-window-below)
(windmove-down))
(defun kill-buffer-and-delete-window ()
"Kill the current buffer and delete its window."
(interactive)
(progn
(kill-this-buffer)
(delete-window)))
#+end_src
** Resize windows
#+begin_src emacs-lisp
(with-eval-after-load 'hydra
(defhydra windows-adjust-size ()
"
_s_: enlarge
_c_: enlarge _r_: right
_t_: shrink
"
("c" enlarge-window-horizontally)
("t" shrink-window)
("s" enlarge-window)
("r" shrink-window-horizontally)))
#+end_src
** Extend ~add-to-list~ ** Extend ~add-to-list~
One function I find missing regarding ~add-to-list~ is ~add-all-to-list~ One function I find missing regarding ~add-to-list~ is ~add-all-to-list~
which enables the user to add multiple elements to a list at once. which enables the user to add multiple elements to a list at once.
@ -164,3 +68,99 @@ APPEND and COMPARE-FN, see `add-to-list'."
nil)) nil))
32)))) 32))))
#+end_src #+end_src
** Handle new windows
The two functions below allow the user to not only create a new window
to the right or below the current window (respectively), but also to
focus the new window immediately.
#+begin_src emacs-lisp
(defun split-window-right-and-focus ()
"Spawn a new window right of the current one and focus it."
(interactive)
(split-window-right)
(windmove-right))
(defun split-window-below-and-focus ()
"Spawn a new window below the current one and focus it."
(interactive)
(split-window-below)
(windmove-down))
(defun kill-buffer-and-delete-window ()
"Kill the current buffer and delete its window."
(interactive)
(progn
(kill-this-buffer)
(delete-window)))
#+end_src
** Resize windows
#+begin_src emacs-lisp
(with-eval-after-load 'hydra
(defhydra windows-adjust-size ()
"
_s_: enlarge
_c_: enlarge _r_: right
_t_: shrink
"
("c" enlarge-window-horizontally)
("t" shrink-window)
("s" enlarge-window)
("r" shrink-window-horizontally)))
#+end_src
** Screenshots
Since Emacs27, it is possible for Emacs to take screenshots of itself
in various formats. Im mainly interested in the SVG and PNG format,
so Ill only write functions for these. It isnt really redundant with
the ~screenshot.el~ package used [[file:./packages/applications.md#screenshot][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]]. I modified it
to make it possible to pass as an argument the format the screenshot
will be taken as or ask the user which format they would like to save
it as.
#+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 wield 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 (list
(intern (completing-read "Screenshot type: "
'(png svg pdf postscript)))))
(let* ((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
I used this function to take the screenshots you can see in this
document.
** Switch between buffers
Two default shortcuts I really like from Spacemacs are ~SPC b m~ and ~SPC
b s~, which bring the user directly to the ~*Messages*~ buffer and the
~*scratch*~ buffer respectively. These functions do exactly this.
#+begin_src emacs-lisp
(defun switch-to-messages-buffer ()
"Switch to Messages buffer."
(interactive)
(switch-to-buffer (messages-buffer)))
(defun switch-to-scratch-buffer ()
"Switch to Messages buffer."
(interactive)
(switch-to-buffer "*scratch*"))
#+end_src