feat(emacs): some keybindings, snippets, jest & org-babel stuff

This commit is contained in:
Pavel Korytov 2022-04-14 12:34:51 +03:00
parent 53eb9132dc
commit 9b1a3b4b43
8 changed files with 296 additions and 19 deletions

View file

@ -300,6 +300,10 @@ then it takes a second \\[keyboard-quit] to abort the minibuffer."
(my-leader-def "?" 'which-key-show-top-level)
(my-leader-def "E" 'eval-expression)
(my-leader-def
"SPC" '(:wk "second level")
"SPC x" '(:wk "ctl-x")
"SPC x" ctl-x-map)
(my-leader-def
"a" '(:which-key "apps"))
@ -514,6 +518,7 @@ then it takes a second \\[keyboard-quit] to abort the minibuffer."
(setq undo-outer-limit 1006632960))
(use-package yasnippet-snippets
:disabled
:straight t)
(use-package yasnippet
@ -521,9 +526,17 @@ then it takes a second \\[keyboard-quit] to abort the minibuffer."
:config
(setq yas-snippet-dirs
`(,(concat (expand-file-name user-emacs-directory) "snippets")
yasnippet-snippets-dir))
;; yasnippet-snippets-dir
))
(setq yas-triggers-in-field t)
(yas-global-mode 1))
(yas-global-mode 1)
(my-leader-def
:keymaps 'yas-minor-mode-map
:infix "es"
"" '(:wk "yasnippet")
"n" #'yas-new-snippet
"s" #'yas-insert-snippet
"v" #'yas-visit-snippet-file))
(general-imap "M-TAB" 'company-yasnippet)
@ -689,7 +702,7 @@ then it takes a second \\[keyboard-quit] to abort the minibuffer."
:states '(insert normal)
"C-y" 'counsel-yank-pop)
(my-leader-def "SPC" 'ivy-resume)
(my-leader-def "SPC SPC" 'ivy-resume)
(my-leader-def "s" 'swiper-isearch
"S" 'swiper-all)
@ -729,6 +742,15 @@ then it takes a second \\[keyboard-quit] to abort the minibuffer."
(my-leader-def
"h" '(:keymap help-map :which-key "help"))
(my-leader-def
:infix "h"
"" '(:which-key "help")
"h" '(:keymap help-map :which-key "help-map")
"f" 'helpful-function
"k" 'helpful-key
"v" 'helpful-variable
"o" 'helpful-symbol)
(general-define-key
:keymaps 'help-map
"f" 'helpful-function
@ -1481,15 +1503,41 @@ Returns (<buffer> . <workspace-index>) or nil."
(my-leader-def
:keymaps 'jest-test-mode-map
:infix "t"
"t" 'jest-test-run-at-point
"r" 'jest-test-run
"a" 'jest-test-run-all-tests))
"t" #'jest-test-run-at-point
"d" #'jest-test-debug-run-at-point
"r" #'jest-test-run
"a" #'jest-test-run-all-tests)
(defmacro my/jest-test-with-debug-flags (form)
"Execute FORM with debugger flags set."
(declare (indent 0))
`(let ((jest-test-options (seq-concatenate 'list jest-test-options (list "--runInBand") ))
(jest-test-npx-options (seq-concatenate 'list jest-test-npx-options (list "--node-options" "--inspect-brk"))))
,form))
(defun my/jest-test-debug ()
"Run the test with an inline debugger attached."
(interactive)
(my/jest-test-with-debug-flags
(jest-test-run)))
(defun my/jest-test-debug-rerun-test ()
"Run the test with an inline debugger attached."
(interactive)
(my/jest-test-with-debug-flags
(jest-test-rerun-test)))
(defun my/jest-test-debug-run-at-point ()
"Run the test with an inline debugger attached."
(interactive)
(my/jest-test-with-debug-flags
(jest-test-run-at-point)))
(advice-add #'jest-test-debug :override #'my/jest-test-debug)
(advice-add #'jest-test-debug-rerun-test :override #'my/jest-test-debug-rerun-test)
(advice-add #'jest-test-debug-run-at-point
:override #'my/jest-test-debug-run-at-point))
(defun my/jest-test-run-at-point-copy ()
"Run the top level describe block of the current buffer's point."
(interactive)
(let ((filename (jest-test-find-file))
(example (jest-test-example-at-point)))
(example (jest-test-unit-at-point)))
(if (and filename example)
(jest-test-from-project-directory filename
(let ((jest-test-options (seq-concatenate 'list jest-test-options (list "-t" example))))
@ -2357,7 +2405,17 @@ Returns (<buffer> . <workspace-index>) or nil."
;; (hs-minor-mode -1)
;; (electric-indent-local-mode -1)
;; (rainbow-delimiters-mode -1)
(highlight-indent-guides-mode -1))))
(highlight-indent-guides-mode -1)))
(with-eval-after-load 'org-babel
(general-define-key
:keymaps 'org-babel-map
"B" #'my/org-babel-execute-buffer-below
"A" #'my/org-babel-execute-buffer-above)
(my-leader-def
:keymaps 'org-mode-map
"SPC b" '(:wk "org-babel")
"SPC b" org-babel-map)))
(setq my/org-latex-scale 1.75)
(setq org-format-latex-options (plist-put org-format-latex-options :scale my/org-latex-scale))
(setq my/latex-preview-header "\\documentclass{article}
@ -2659,6 +2717,30 @@ Returns (<buffer> . <workspace-index>) or nil."
(add-hook 'org-babel-after-execute-hook #'my/babel-ansi)
(remove-hook 'org-babel-after-execute-hook #'my/babel-ansi)))
(defun my/org-babel-execute-buffer-below (&optional arg)
(interactive "P")
(org-babel-eval-wipe-error-buffer)
(let ((point (point)))
(org-save-outline-visibility t
(org-babel-map-executables nil
(when (>= (point) point)
(if (memq (org-element-type (org-element-context))
'(babel-call inline-babel-call))
(org-babel-lob-execute-maybe)
(org-babel-execute-src-block arg)))))))
(defun my/org-babel-execute-buffer-above (&optional arg)
(interactive "P")
(org-babel-eval-wipe-error-buffer)
(let ((point (point)))
(org-save-outline-visibility t
(org-babel-map-executables nil
(when (<= (point) point)
(if (memq (org-element-type (org-element-context))
'(babel-call inline-babel-call))
(org-babel-lob-execute-maybe)
(org-babel-execute-src-block arg)))))))
(defun my/org-prj-dir (path)
(expand-file-name path (org-entry-get nil "PRJ-DIR" t)))
@ -3263,6 +3345,17 @@ Returns (<buffer> . <workspace-index>) or nil."
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
(add-to-list 'org-latex-classes
'("org-plain-extreport"
"\\documentclass{extreport}
[NO-DEFAULT-PACKAGES]
[PACKAGES]
[EXTRA]"
("\\chapter{%s}" . "\\chapter*{%s}")
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")))
;; Use beamer without the default packages
(add-to-list 'org-latex-classes
'("org-latex-beamer"
@ -4309,6 +4402,15 @@ Returns (<buffer> . <workspace-index>) or nil."
(use-package devdocs
:straight t
:commands (devdocs-install devdocs-lookup)
:config
(general-define-key
:keymaps 'devdocs-mode-map
:states '(normal)
"H" #'devdocs-go-back
"L" #'devdocs-go-forward
"o" #'devdocs-lookup
"[" #'devdocs-previous-page
"]" #'devdocs-next-page)
:init
(my-leader-def
"he" #'devdocs-lookup
@ -4341,8 +4443,8 @@ Returns (<buffer> . <workspace-index>) or nil."
:states '(normal)
:keymaps '(sx-question-mode-map)
"gr" #'sx-question-mode-refresh
"j" #'sx-question-mode-next-section
"k" #'sx-question-mode-previous-section
"J" #'sx-question-mode-next-section
"K" #'sx-question-mode-previous-section
"a" #'sx-answer
"e" #'sx-edit
"D" #'sx-delete
@ -4355,7 +4457,11 @@ Returns (<buffer> . <workspace-index>) or nil."
"k" #'sx-question-list-previous
"S" #'sx-search
"m" #'sx-question-list-mark-read
"O" #'sx-question-list-order-by
"t" #'sx-tab-switch)
(my-leader-def
"hs" #'sx-search
"hS" #'sx-tab-frontpage)
(my/use-doom-colors
(sx-question-mode-accepted :foreground (doom-color 'green)
:weight 'bold)

View file

@ -0,0 +1,9 @@
# key: apiq
# name: nest-api-query
# condition: (string-match-p (rx controller.ts eos) (buffer-file-name))
# --
@ApiQuery({
name: '$1',
required: ${2:$$(yas-choose-value "false" "true")},
description: '$3',
})

View file

@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: private-method
# key: pm
# --
private $1 ($2)${3:$$(if (string-empty-p yas-text) "" (if (string-match-p (rx bos ":") yas-text) yas-text (format ": %s" yas-text)))} {
$0
}

View file

@ -0,0 +1,7 @@
# -*- mode: snippet -*-
# name: public-async-method
# key: pam
# --
public async $1 ($2)${3:$$(if (string-empty-p yas-text) "" (if (string-match-p (rx bos ":") yas-text) yas-text (format ": %s" yas-text)))} {
$0
}

View file

@ -0,0 +1,6 @@
# key: um
# name: public-method
# --
public $1 ($2)${3:$$(if (string-empty-p yas-text) "" (if (string-match-p (rx bos ":") yas-text) yas-text (format ": %s" yas-text)))} {
$0
}

View file

@ -0,0 +1,10 @@
# -*- mode: snippet -*-
# name: switch
# key: sw
# --
switch (${1:condition}) {
case ${2:expression}:
$0
break;
default:
}

View file

@ -0,0 +1,15 @@
# -*- mode: snippet -*-
# name: vue-typescript-component
# condition: (string-match-p (rx ".vue" eos) (buffer-file-name))
# key: tsc
# --
<template>
$0
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
@Component
export default class $1 extends Vue {}
</script>

135
Emacs.org
View file

@ -777,6 +777,10 @@ Using the =SPC= key as a leader key, like in Doom Emacs or Spacemacs.
(my-leader-def "?" 'which-key-show-top-level)
(my-leader-def "E" 'eval-expression)
(my-leader-def
"SPC" '(:wk "second level")
"SPC x" '(:wk "ctl-x")
"SPC x" ctl-x-map)
#+end_src
=general.el= has a nice integration with which-key, so I use that to show more descriptive annotations for certain groups of keybindings (the default annotation is just =prefix=).
@ -1133,11 +1137,14 @@ A snippet system for Emacs and a collection of pre-built snippets.
~yasnippet-snippets~ has to be loaded before ~yasnippet~ for user snippets to override the pre-built ones.
Edit <2022-04-11 Mon> I don't really use ~yasnippet-snippets~, so I'd rather write stuff manually.
References:
- [[http://joaotavora.github.io/yasnippet/][yasnippet documentation]]
#+begin_src emacs-lisp
(use-package yasnippet-snippets
:disabled
:straight t)
(use-package yasnippet
@ -1145,9 +1152,17 @@ References:
:config
(setq yas-snippet-dirs
`(,(concat (expand-file-name user-emacs-directory) "snippets")
yasnippet-snippets-dir))
;; yasnippet-snippets-dir
))
(setq yas-triggers-in-field t)
(yas-global-mode 1))
(yas-global-mode 1)
(my-leader-def
:keymaps 'yas-minor-mode-map
:infix "es"
"" '(:wk "yasnippet")
"n" #'yas-new-snippet
"s" #'yas-insert-snippet
"v" #'yas-visit-snippet-file))
(general-imap "M-TAB" 'company-yasnippet)
#+end_src
@ -1385,7 +1400,7 @@ Setting up quick access to various completions.
:states '(insert normal)
"C-y" 'counsel-yank-pop)
(my-leader-def "SPC" 'ivy-resume)
(my-leader-def "SPC SPC" 'ivy-resume)
(my-leader-def "s" 'swiper-isearch
"S" 'swiper-all)
@ -1445,6 +1460,15 @@ As I use =C-h= to switch buffers, I moved the help to =SPC-h= with the code belo
(my-leader-def
"h" '(:keymap help-map :which-key "help"))
(my-leader-def
:infix "h"
"" '(:which-key "help")
"h" '(:keymap help-map :which-key "help-map")
"f" 'helpful-function
"k" 'helpful-key
"v" 'helpful-variable
"o" 'helpful-symbol)
(general-define-key
:keymaps 'help-map
"f" 'helpful-function
@ -2492,9 +2516,35 @@ My bit of config here:
(my-leader-def
:keymaps 'jest-test-mode-map
:infix "t"
"t" 'jest-test-run-at-point
"r" 'jest-test-run
"a" 'jest-test-run-all-tests))
"t" #'jest-test-run-at-point
"d" #'jest-test-debug-run-at-point
"r" #'jest-test-run
"a" #'jest-test-run-all-tests)
(defmacro my/jest-test-with-debug-flags (form)
"Execute FORM with debugger flags set."
(declare (indent 0))
`(let ((jest-test-options (seq-concatenate 'list jest-test-options (list "--runInBand") ))
(jest-test-npx-options (seq-concatenate 'list jest-test-npx-options (list "--node-options" "--inspect-brk"))))
,form))
(defun my/jest-test-debug ()
"Run the test with an inline debugger attached."
(interactive)
(my/jest-test-with-debug-flags
(jest-test-run)))
(defun my/jest-test-debug-rerun-test ()
"Run the test with an inline debugger attached."
(interactive)
(my/jest-test-with-debug-flags
(jest-test-rerun-test)))
(defun my/jest-test-debug-run-at-point ()
"Run the test with an inline debugger attached."
(interactive)
(my/jest-test-with-debug-flags
(jest-test-run-at-point)))
(advice-add #'jest-test-debug :override #'my/jest-test-debug)
(advice-add #'jest-test-debug-rerun-test :override #'my/jest-test-debug-rerun-test)
(advice-add #'jest-test-debug-run-at-point
:override #'my/jest-test-debug-run-at-point))
#+end_src
#+begin_src emacs-lisp
@ -2502,7 +2552,7 @@ My bit of config here:
"Run the top level describe block of the current buffer's point."
(interactive)
(let ((filename (jest-test-find-file))
(example (jest-test-example-at-point)))
(example (jest-test-unit-at-point)))
(if (and filename example)
(jest-test-from-project-directory filename
(let ((jest-test-options (seq-concatenate 'list jest-test-options (list "-t" example))))
@ -3804,6 +3854,49 @@ A minor mode to apply ANSI color codes after execution.
(add-hook 'org-babel-after-execute-hook #'my/babel-ansi)
(remove-hook 'org-babel-after-execute-hook #'my/babel-ansi)))
#+end_src
*** Executing stuff
A few convinient functions and keybindings to execute things in an org buffer.
First, execute things above and below the point:
#+begin_src emacs-lisp
(defun my/org-babel-execute-buffer-below (&optional arg)
(interactive "P")
(org-babel-eval-wipe-error-buffer)
(let ((point (point)))
(org-save-outline-visibility t
(org-babel-map-executables nil
(when (>= (point) point)
(if (memq (org-element-type (org-element-context))
'(babel-call inline-babel-call))
(org-babel-lob-execute-maybe)
(org-babel-execute-src-block arg)))))))
(defun my/org-babel-execute-buffer-above (&optional arg)
(interactive "P")
(org-babel-eval-wipe-error-buffer)
(let ((point (point)))
(org-save-outline-visibility t
(org-babel-map-executables nil
(when (<= (point) point)
(if (memq (org-element-type (org-element-context))
'(babel-call inline-babel-call))
(org-babel-lob-execute-maybe)
(org-babel-execute-src-block arg)))))))
#+end_src
Some keybindings:
#+begin_src emacs-lisp :tangle no :noweb-ref org-lang-setup
(with-eval-after-load 'org-babel
(general-define-key
:keymaps 'org-babel-map
"B" #'my/org-babel-execute-buffer-below
"A" #'my/org-babel-execute-buffer-above)
(my-leader-def
:keymaps 'org-mode-map
"SPC b" '(:wk "org-babel")
"SPC b" org-babel-map))
#+end_src
*** Managing a literate programming project
A few tricks to do literate programming.
@ -4765,6 +4858,17 @@ Add a custom LaTeX template without default packages. Packages are indented to b
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
(add-to-list 'org-latex-classes
'("org-plain-extreport"
"\\documentclass{extreport}
[NO-DEFAULT-PACKAGES]
[PACKAGES]
[EXTRA]"
("\\chapter{%s}" . "\\chapter*{%s}")
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")))
;; Use beamer without the default packages
(add-to-list 'org-latex-classes
'("org-latex-beamer"
@ -6338,6 +6442,15 @@ Finally, there is also an Emacs plugin for [[https://devdocs.io][devdocs.io]].
(use-package devdocs
:straight t
:commands (devdocs-install devdocs-lookup)
:config
(general-define-key
:keymaps 'devdocs-mode-map
:states '(normal)
"H" #'devdocs-go-back
"L" #'devdocs-go-forward
"o" #'devdocs-lookup
"[" #'devdocs-previous-page
"]" #'devdocs-next-page)
:init
(my-leader-def
"he" #'devdocs-lookup
@ -6374,8 +6487,8 @@ Finally, there is also an Emacs plugin for [[https://devdocs.io][devdocs.io]].
:states '(normal)
:keymaps '(sx-question-mode-map)
"gr" #'sx-question-mode-refresh
"j" #'sx-question-mode-next-section
"k" #'sx-question-mode-previous-section
"J" #'sx-question-mode-next-section
"K" #'sx-question-mode-previous-section
"a" #'sx-answer
"e" #'sx-edit
"D" #'sx-delete
@ -6388,7 +6501,11 @@ Finally, there is also an Emacs plugin for [[https://devdocs.io][devdocs.io]].
"k" #'sx-question-list-previous
"S" #'sx-search
"m" #'sx-question-list-mark-read
"O" #'sx-question-list-order-by
"t" #'sx-tab-switch)
(my-leader-def
"hs" #'sx-search
"hS" #'sx-tab-frontpage)
(my/use-doom-colors
(sx-question-mode-accepted :foreground (doom-color 'green)
:weight 'bold)