kakoune: set up package with plugins

This commit is contained in:
Natsu Kagami 2024-09-28 16:18:16 +02:00
parent 2c54a0a9a1
commit 0e320f1c1a
Signed by: nki
GPG key ID: 55A032EB38B49ADB
5 changed files with 375 additions and 0 deletions

View file

@ -2,4 +2,5 @@ final: prev: {
epfl-cups-drivers = final.callPackage ./epfl-cups-drivers { };
ttaenc = final.callPackage ./ttaenc.nix { };
suwako-cursors = final.callPackage ./suwako-cursors { };
nki-kakoune = final.callPackage ./nki-kakoune { };
}

View file

@ -0,0 +1,133 @@
## 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.
# Create a simple begin block, put the cursors in and remove multi-cursor on exit.
define-command -hidden create-begin-block %{
execute-keys "<esc>i\begin{b0}<ret>\end{b0}<esc>"
execute-keys "<a-/>b0<ret><a-N>"
execute-keys -with-hooks -with-maps "c"
hook -once buffer ModeChange .*:normal %{
execute-keys -with-maps ",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 "<a-/>b0<ret><a-N>c%arg{1}<esc>,"
execute-keys "o<esc>k"
execute-keys -with-maps "gl"
}
# Create a \param{} block and put the cursor in the middle.
define-command -params 2 -hidden create-delims %{
execute-keys "<esc>i%arg{1}<esc>hZa%arg{2}<esc>zl"
execute-keys -with-hooks -with-maps "i"
}
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>"
map -docstring "Math Fraktur (mathfrak)" global latex-font f ": create-block-with mathfrak<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>"
map -docstring "Remark" global latex-insert-block r ": create-begin-block-with remark<ret>"
map -docstring "Proposition" global latex-insert-block <a-p> ": create-begin-block-with proposition<ret>"
map -docstring "Corollary" global latex-insert-block C ": create-begin-block-with corollary<ret>"
## Common environments
map -docstring "align*" global latex-insert-block a ": create-begin-block-with align*<ret>"
map -docstring "align" global latex-insert-block <a-a> ": create-begin-block-with align<ret>"
map -docstring "equation" global latex-insert-block E ": create-begin-block-with equation<ret>"
map -docstring "equation*" global latex-insert-block <a-e> ": create-begin-block-with equation*<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 "Grouping" global latex-insert-delims g ": create-delims { }<ret>"
map -docstring "Parentheses" global latex-insert-delims p ": create-delims ( )<ret>"
map -docstring "Large 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=(tex|latex) %{
## Create delims (shortcuts)
map buffer normal <a-1> ": enter-user-mode latex-insert-delims<ret>p"
map buffer insert <a-1> "<esc>: enter-user-mode latex-insert-delims<ret>p"
map buffer normal <a-2> ": enter-user-mode latex-insert-delims<ret>g"
map buffer insert <a-2> "<esc>: enter-user-mode latex-insert-delims<ret>g"
map buffer normal <a-3> "i\(\)<esc>Zhhi"
map buffer insert <a-3> "\(\)<a-;>Z<a-;>2h"
map buffer normal <a-4> "i\[\]<esc>Zhhi"
map buffer insert <a-4> "\[\]<a-;>Z<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 "
}

View file

@ -0,0 +1,29 @@
hook global WinSetOption filetype=(markdown) %{
map buffer normal <c-b> ": enter-user-mode markdown-menu<ret>"
}
# A menu for common markdown actions
declare-user-mode markdown-menu
map -docstring "Toggle the checkboxes on the same line" global markdown-menu t ": markdown-toggle-checkbox<ret>"
define-command -hidden markdown-toggle-checkbox %{
try %{
execute-keys -draft "xs^\s*- \[( |x)\]<ret>h: markdown-toggle-checkbox-selections<ret>"
}
}
define-command -hidden markdown-toggle-checkbox-selections %{
try %{
execute-keys -draft -itersel ": markdown-toggle-checkbox-one<ret>"
}
}
define-command -hidden markdown-toggle-checkbox-one %{
try %{
execute-keys -draft "sx<ret>r "
} catch %{
execute-keys -draft "s <ret>rx"
}
}

View file

@ -0,0 +1,3 @@
{ callPackage, kakoune, ... }: kakoune.override {
plugins = (callPackage ./plugins.nix { }).plugins;
}

View file

@ -0,0 +1,209 @@
{ pkgs, symlinkJoin, writeTextDir, kakouneUtils, ... }:
with {
inherit (kakouneUtils) buildKakounePluginFrom2Nix;
};
let
toDir = name: file: writeTextDir name (builtins.readFile file);
writeActivationScript = script: writeTextDir "on-load.kak" ''
hook global KakBegin .* %{
${script}
}
'';
writeModuleWrapper = name: script: writeTextDir "module.kak" ''
provide-module ${name} %
${script}
'';
kakounePlugin = { name, src, wrapAsModule ? false, activationScript ? null, ... }@attrs:
let
module = if wrapAsModule then writeModuleWrapper name (builtins.readFile src) else src;
in
buildKakounePluginFrom2Nix {
pname = name;
version = attrs.version or "latest";
src = if activationScript == null then module else
symlinkJoin {
name = "${name}-src";
paths = [
module
(writeActivationScript activationScript)
];
};
};
in
{
plugins = builtins.map kakounePlugin
[
# My own scripts
{
name = "latex.kak";
src = toDir "latex.kak" ./autoload/latex.kak;
}
{
name = "markdown.kak";
src = toDir "markdown.kak" ./autoload/markdown.kak;
}
# Plugins
{
name = "luar";
src = pkgs.fetchFromGitHub {
owner = "gustavo-hms";
repo = "luar";
rev = "2f430316f8fc4d35db6c93165e2e77dc9f3d0450";
sha256 = "sha256-vHn/V3sfzaxaxF8OpA5jPEuPstOVwOiQrogdSGtT6X4=";
};
activationScript = ''
# Enable luar
require-module luar
# Use luajit
set-option global luar_interpreter ${pkgs.luajit}/bin/luajit
'';
}
{
name = "peneira";
src = pkgs.fetchFromGitHub {
owner = "natsukagami";
repo = "peneira";
rev = "743b9971472853a752475e7c070ce99089c6840c";
sha256 = "sha256-E4ndbF9YC1p0KrvSuGgwmG1Y2IGTuGKJo/AuMixhzlM=";
};
activationScript = ''
require-module peneira
# Change selection color
set-face global PeneiraSelected @PrimarySelection
# Buffers list
define-command -hidden peneira-buffers %{
peneira 'buffers: ' %{ printf '%s\n' $kak_quoted_buflist } %{
buffer %arg{1}
}
}
# Grep in the current location
define-command peneira-grep %{
peneira 'line: ' "rg -n ." %{
lua %arg{1} %{
local file, line = arg[1]:match("([^:]+):(%d+):")
kak.edit(file, line)
}
}
}
# A peneira menu
declare-user-mode fuzzy-match-menu
map -docstring "Switch to buffer" global fuzzy-match-menu b ": peneira-buffers<ret>"
map -docstring "Symbols" global fuzzy-match-menu s ": peneira-symbols<ret>"
map -docstring "Lines" global fuzzy-match-menu l ": peneira-lines<ret>"
map -docstring "Lines in the current directory" global fuzzy-match-menu g ": peneira-grep<ret>"
map -docstring "Files in project" global fuzzy-match-menu f ": peneira-files<ret>"
map -docstring "Files in currently opening file's directory" global fuzzy-match-menu F ": peneira-local-files<ret>"
# Bind menu to user mode
map -docstring "Fuzzy matching" global user f ": enter-user-mode fuzzy-match-menu<ret>"
'';
}
{
name = "kakoune-focus";
src = pkgs.fetchFromGitHub {
owner = "caksoylar";
repo = "kakoune-focus";
rev = "949c0557cd4c476822acfa026ca3c50f3d38a3c0";
sha256 = "sha256-ZV7jlLJQyL420YG++iC9rq1SMjo3WO5hR9KVvJNUiCs=";
};
activationScript = ''
map global user <space> ': focus-toggle<ret>' -docstring "toggle selections focus"
'';
}
{
name = "kakoune-inc-dec";
src = pkgs.fetchFromGitLab {
owner = "Screwtapello";
repo = "kakoune-inc-dec";
rev = "7bfe9c51";
sha256 = "0f33wqxqbfygxypf348jf1fiscac161wf2xvnh8zwdd3rq5yybl0";
};
}
{
name = "racket.kak";
src = (builtins.fetchTree {
type = "git";
url = "https://bitbucket.org/KJ_Duncan/kakoune-racket.kak.git";
rev = "e397042009b46916ff089d79166ec0e8ca813a18";
narHash = "sha256-IcxFmvG0jqpMCG/dT9crVRgPgMGKkic6xwrnW5z4+bc=";
}) + "/rc";
}
# {
# name = "kakoune-discord";
# src = (builtins.getFlake "github:natsukagami/kakoune-discord/03f95e40d6efd8fd3de7bca31653d43de2dcfc5f").packages.${pkgs.system}.kakoune-discord-rc + "/rc";
# }
rec {
name = "kakoune-mirror";
src = pkgs.fetchFromGitHub
{
owner = "Delapouite";
repo = "kakoune-mirror";
rev = "5710635f440bcca914d55ff2ec1bfcba9efe0f15";
sha256 = "sha256-uslx4zZhvjUylrPWvTOugsKYKKpF0EEz1drc1Ckrpjk=";
} + "/mirror.kak";
wrapAsModule = true;
activationScript = ''
require-module ${name}
# Bind <a-w> to ${name}
map global normal <a-w> ': enter-user-mode -lock mirror<ret>'
'';
}
{
name = "unicode-math";
src = pkgs.fetchFromGitHub {
owner = "natsukagami";
repo = "kakoune-unicode-math";
rev = "08dff25da2b86ee0b0777091992bc7fb28c3cb1d";
# sha256 = lib.fakeSha256;
sha256 = "sha256-j0L1ARex1i2ma8sGLYwgkfAbh0jWKh/6QGHFaxPXIKc=";
fetchSubmodules = true;
};
activationScript = ''
require-module unicode-math
# Bind <c-s> to the menu
map global insert <c-s> '<a-;>: insert-unicode '
'';
}
{
name = "kakoune-buffers";
src = pkgs.fetchFromGitHub {
owner = "Delapouite";
repo = "kakoune-buffers";
rev = "6b2081f5b7d58c72de319a5cba7bf628b6802881";
sha256 = "sha256-jOSrzGcLJjLK1GiTSsl2jLmQMPbPxjycR0pwF5t/eV0=";
};
activationScript = ''
# Suggested hook
hook global WinDisplay .* info-buffers
# Suggested mappings
map global user b ':enter-buffers-mode<ret>' -docstring 'buffers'
map global normal ^ ':enter-buffers-mode<ret>' -docstring 'buffers'
map global user B ':enter-user-mode -lock buffers<ret>' -docstring 'buffers (lock)'
# Suggested aliases
alias global bd delete-buffer
alias global bf buffer-first
alias global bl buffer-last
alias global bo buffer-only
alias global bo! buffer-only-force
'';
}
];
}