From d625bb2c1504990fa334ab2744884bdae64863f5 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Wed, 12 Jan 2022 17:21:36 -0500 Subject: [PATCH] More efficient kakoune editing integration --- home/kakoune/kak.nix | 5 +- home/kakoune/kaktex | 28 +++++++++ home/kakoune/kaktex.nix | 22 +++++++ home/modules/programs/my-kakoune/default.nix | 2 +- .../programs/my-kakoune/fish-session.nix | 57 +++++++++++++++++++ 5 files changed, 111 insertions(+), 3 deletions(-) create mode 100755 home/kakoune/kaktex create mode 100644 home/kakoune/kaktex.nix create mode 100644 home/modules/programs/my-kakoune/fish-session.nix diff --git a/home/kakoune/kak.nix b/home/kakoune/kak.nix index 93ca582..23e7c66 100644 --- a/home/kakoune/kak.nix +++ b/home/kakoune/kak.nix @@ -19,10 +19,11 @@ let }; in { - imports = [ ../modules/programs/my-kakoune ]; - + imports = [ ../modules/programs/my-kakoune ./kaktex.nix ]; + # Enable the kakoune package. programs.my-kakoune.enable = true; + programs.my-kakoune.enable-fish-session = true; programs.kak-lsp.enable = true; programs.my-kakoune.package = kakounePkg; diff --git a/home/kakoune/kaktex b/home/kakoune/kaktex new file mode 100755 index 0000000..3abb961 --- /dev/null +++ b/home/kakoune/kaktex @@ -0,0 +1,28 @@ +#!/usr/bin/env fish + +function usage + echo "Usage: " + echo " kaktex set [client] [session]" + echo " kaktex jump [file] [line]" + exit 1 +end + +if test (count $argv) -ne 3 + usage +end + +switch $argv[1] + case "set" + set -U _kaktex_client $argv[2] + set -U _kaktex_session $argv[3] + case "jump" + echo " + evaluate-commands -client $_kaktex_client %{ + evaluate-commands -try-client $_kaktex_client %{ + edit -- $argv[2] $argv[3] + } + } + " | kak -p $_kaktex_session + case '*' + usage +end diff --git a/home/kakoune/kaktex.nix b/home/kakoune/kaktex.nix new file mode 100644 index 0000000..cf5c9db --- /dev/null +++ b/home/kakoune/kaktex.nix @@ -0,0 +1,22 @@ +{ config, pkgs, lib, ... }: + +let + kaktexScript = ./kaktex; +in +{ + # Create kak-tex executable + home.file.kaktex = { + source = kaktexScript; + executable = true; + target = ".bin/kaktex"; + }; + + # Source kaktex whenever we have a tex file + programs.my-kakoune.rc = '' + hook global WinSetOption filetype=(tex|latex) %{ + eval %sh{ + ${kaktexScript} set $kak_client $kak_session + } + } + ''; +} diff --git a/home/modules/programs/my-kakoune/default.nix b/home/modules/programs/my-kakoune/default.nix index 5da6e6e..19f9aa5 100644 --- a/home/modules/programs/my-kakoune/default.nix +++ b/home/modules/programs/my-kakoune/default.nix @@ -18,7 +18,7 @@ let }; in { - imports = [ ./kak-lsp.nix ]; + imports = [ ./kak-lsp.nix ./fish-session.nix ]; options.programs.my-kakoune = { enable = mkEnableOption "My version of the kakoune configuration"; diff --git a/home/modules/programs/my-kakoune/fish-session.nix b/home/modules/programs/my-kakoune/fish-session.nix new file mode 100644 index 0000000..5b167f0 --- /dev/null +++ b/home/modules/programs/my-kakoune/fish-session.nix @@ -0,0 +1,57 @@ +{ config, pkgs, lib, ... }: + +with lib; +let + cfg = config.programs.my-kakoune; +in +{ + options.programs.my-kakoune.enable-fish-session = mkEnableOption "Enable fish integration script"; + config = mkIf cfg.enable-fish-session { + programs.fish.functions = { + kak-session = '' + if set -q kak_session + echo "Another kakoune session ($kak_session) is currently running. Do `kill-kak-session` to stop it." >&2 + return 1 + end + + if test (count $argv) -ge 1 + set -gx kak_session $argv[1] + else + set -gx kak_session "kak-"(random) + end + + # Start a new kakoune session + kak -s $kak_session -d & + echo "New kakoune session started (pid = $last_pid, session name = $kak_session)." + + # Rebind $VISUAL, $EDITOR and e command + set -gx VISUAL "kak -s $kak_session" + set -gx EDITOR "kak -s $kak_session" + alias e="kak -c $kak_session" + ''; + + kill-kak-session = '' + if not set -q kak_session + echo "No kakoune session found on this terminal instance." >&2 + return 1 + end + + echo kill | kak -p $kak_session + set -eg kak_session + + # Rebind $VISUAL, $EDITOR and e command + set -gx VISUAL "kak" + set -gx EDITOR "kak" + alias e="kak" + ''; + + _tide_item_kakoune = '' + if set -q kak_session + set -U tide_kakoune_color FFA500 + set -U tide_kakoune_bg_color normal + _tide_print_item kakoune " " "e[$kak_session]" + end + ''; + }; + }; +}