Skip to content

Instantly share code, notes, and snippets.

@natsukagami
Last active March 13, 2021 08:27
Show Gist options
  • Save natsukagami/176d87fdcf75e696fd5fbe74d1074453 to your computer and use it in GitHub Desktop.
Save natsukagami/176d87fdcf75e696fd5fbe74d1074453 to your computer and use it in GitHub Desktop.
More LaTeX support for kak!
## Author: @natsukagami (https://github.com/natsukagami)
##
## To activate, source the file into kakrc and add:
### require-module latex-kak
##
## NOTE: This overrides <a-o>, so if you don't like it, remove it.
provide-module latex-kak %{
# Create a simple begin block, put the cursors in and remove multi-cursor on exit.
define-command -hidden create-begin-block %{
execute-keys -with-hooks "<esc>i\begin{b0}<ret>\end{b0}<esc>"
execute-keys -with-hooks "<a-/>b0<ret><a-N>c"
hook -once buffer ModeChange .*:normal %{
execute-keys -with-hooks -with-maps "<space>gl"
}
}
# Create a begin block with the given parameter as block name.
define-command -params 1 create-begin-block-with %{
execute-keys "<esc>i\begin{b0}<ret>\end{b0}<esc>"
execute-keys -with-hooks "<a-/>b0<ret><a-N>c%arg{1}<esc><space>gl"
}
# Create a \param{} block and put the cursor in the middle.
define-command -params 2 -hidden create-delims %{
execute-keys -with-hooks "<esc>i%arg{1}<esc>hZa%arg{2}<esc>zli"
}
define-command -params 1 create-block-with %{
create-delims "\%arg{1}{" "}"
}
# The font-menu
declare-user-mode latex-font
## Semantics
map -docstring "Text" global latex-font t ": create-block-with text<ret>"
map -docstring "Emphasize (emph)" global latex-font e ": create-block-with emph<ret>"
## Shape
map -docstring "Italics (textit)" global latex-font i ": create-block-with textit<ret>"
map -docstring "Upright (textup)" global latex-font u ": create-block-with textup<ret>"
# map -docstring "Slanted (textsl)" global latex-font S ": create-block-with textsl<ret>"
# map -docstring "Swash font (textsw)" global latex-font W ": create-block-with textsw<ret>"
# map -docstring "Small caps (textsc)" global latex-font C ": create-block-with textsc<ret>"
# Weight
map -docstring "Bold text (textbf)" global latex-font b ": create-block-with textbf<ret>"
# map -docstring "Medium bold (textmd)" global latex-font M ": create-block-with textmd<ret>"
# map -docstring "Normal (textnormal)" global latex-font N ": create-block-with textnormal<ret>"
## Family
# map -docstring "Serif font (textsf)" global latex-font s ": create-block-with textsf<ret>"
# map -docstring "Roman text (textrm)" global latex-font r ": create-block-with textrm<ret>"
map -docstring "Monospace (texttt)" global latex-font m ": create-block-with texttt<ret>"
## Math styles
map -docstring "Math Calligraphic (mathcal)" global latex-font <a-c> ": create-block-with mathcal<ret>"
map -docstring "Math Blackboard (mathbb)" global latex-font <a-b> ": create-block-with mathbb<ret>"
# map -docstring "Math Fraktur (mathfr)" global latex-font <a-F> ": create-block-with mathfr<ret>"
# map -docstring "Math Roman (mathrm)" global latex-font <a-r> ": create-block-with mathrm<ret>"
# map -docstring "Math Italics (mathit)" global latex-font <a-i> ": create-block-with mathit<ret>"
# map -docstring "Math Bold (mathbf)" global latex-font <a-B> ": create-block-with mathbf<ret>"
# map -docstring "Serif font (mathsf)" global latex-font <a-s> ": create-block-with mathsf<ret>"
map -docstring "Math Monospace (mathtt)" global latex-font <a-m> ": create-block-with mathtt<ret>"
# "Insert block" menu
declare-user-mode latex-insert-block
## Common normal text blocks
map -docstring "Unordered list" global latex-insert-block u ": create-begin-block-with itemize<ret>"
map -docstring "Ordered list" global latex-insert-block o ": create-begin-block-with enumerate<ret>"
## Common math blocks
map -docstring "Theorem" global latex-insert-block t ": create-begin-block-with theorem<ret>"
map -docstring "Definition" global latex-insert-block d ": create-begin-block-with definition<ret>"
map -docstring "Lemma" global latex-insert-block l ": create-begin-block-with lemma<ret>"
map -docstring "Example" global latex-insert-block e ": create-begin-block-with example<ret>"
map -docstring "Proof" global latex-insert-block p ": create-begin-block-with proof<ret>"
## Common environments
map -docstring "align*" global latex-insert-block a ": create-begin-block-with align*<ret>"
map -docstring "Matrix" global latex-insert-block m ": create-begin-block-with bmatrix<ret>"
map -docstring "Cases" global latex-insert-block C ": create-begin-block-with cases<ret>"
map -docstring "Table" global latex-insert-block T ": create-begin-block-with tabular<ret>"
## Custom
map -docstring "Custom" global latex-insert-block c ": create-begin-block<ret>"
# Pairs of delimiters
declare-user-mode latex-insert-delims
map -docstring "Parentheses" global latex-insert-delims p ": create-delims \left( \right)<ret>"
map -docstring "Brackets" global latex-insert-delims b ": create-delims \left[ \right]<ret>"
map -docstring "Sets" global latex-insert-delims s ": create-delims \{ \}<ret>"
map -docstring "Large Sets" global latex-insert-delims S ": create-delims \left\{ \right\}<ret>"
hook global WinSetOption filetype=(latex) %{
## Create inline and display math blocks
map buffer normal <a-3> "i\(\)<esc>hhi"
map buffer insert <a-3> "\(\)<a-;>2h"
map buffer normal <a-4> "i\[\]<esc>hhi"
map buffer insert <a-4> "\[\]<a-;>2h"
map buffer normal <a-5> ": enter-user-mode latex-insert-delims<ret>"
map buffer insert <a-5> "<esc>: enter-user-mode latex-insert-delims<ret>"
## Quickly create begin/end blocks
map buffer normal <c-n> ": create-begin-block<ret>"
map buffer insert <c-n> "<esc>: create-begin-block<ret>"
## Font menu
map buffer normal <c-b> ": enter-user-mode latex-font<ret>"
map buffer insert <c-b> "<esc>: enter-user-mode latex-font<ret>"
## Insert menu
map buffer normal <a-b> ": enter-user-mode latex-insert-block<ret>"
map buffer insert <a-b> "<esc>: enter-user-mode latex-insert-block<ret>"
## Select math equations and environment blocks
map buffer object e -docstring "Inline Math equation \( \)" "c\\\\\\(,\\\\\\)<ret>"
map buffer object E -docstring "Display Math equation \[ \]" "c\\\\\\\[,\\\\\\]<ret>"
map buffer object v -docstring "Simple environment \env{}" "c\\\\\\w+\\{,\\}<ret>"
map buffer object V -docstring "Full environment \begin{env}\end{env}" "c\\\\begin\\{\\w+\\}(?:\\{[\\w\\s]*\\})*(?:\\[[\\w\\s]*\\])*,\\\\end\\{\\w+\\}<ret>"
## Quickly get a new item
map buffer normal <a-o> "o\item "
map buffer insert <a-ret> "<esc>o\item "
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment