Git Product home page Git Product logo

kakoune-focus's Introduction

kakoune-focus

kakoune-focus is a plugin to let you work on multiple selections more efficiently by hiding lines that are far from your selections using Kakoune's replace-ranges highlighter (:doc highlighters specs-highlighters).

Note This plugin requires Kakoune release 2022.10.31 or a revision after the breaking change at mawww/kakoune@ef8a11b.

demo

Installation

You can copy focus.kak to your autoload folder, e.g. into ~/.config/kak/autoload.

If you are using plug.kak:

plug "caksoylar/kakoune-focus" config %{
     # configuration here
}

Usage

Once you have the (ideally multiple) selections you want to focus on, use focus-selections to hide the surrounding lines. You can use focus-clear to disable this or use focus-toggle to toggle it on and off.

I recommend assigning a mapping for easy access, for example ,<space> to toggle:

map global user <space> ': focus-toggle<ret>' -docstring "toggle selections focus"

You can also extend the focus area to include your current selections using focus-extend, which can be called multiple times. Using this feature you can even create a live-updating focus area that extends itself to lines you have visited. For example you can define and use commands like below:

define-command focus-live-enable %{
    focus-selections
    hook -group focus window NormalIdle .* %{ focus-extend }
}
define-command focus-live-disable %{
    remove-hooks window focus
    focus-clear
}

Configuration

There are a couple of options you can configure:

  • focus_separator (str): What string to use as the separator to replace hidden lines, can use markup strings :doc faces markup-strings
  • focus_context_lines (int): How many lines of context to preserve above and below selections (default: 1)

Caveats

Due to a Kakoune shortcoming with the replace-ranges highlighter, some lines can be visually cut off from the bottom after focusing. This seems to be an issue especially when long spans of lines are hidden and soft line wrapping is enabled with the wrap highlighter.

License

MIT

kakoune-focus's People

Contributors

caksoylar avatar vbauerster avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

vbauerster

kakoune-focus's Issues

Improve Startup Performance

Kakoune was taking ~1.0s to open a blank buffer so I used this script to profile my config and see what was causing the delay.

kak -debug 'profile|shell' -e 'b *debug*'

and I found this:

----
shell execution took 456969 us (spawn: 88, wait: 456844)
hook 'GlobalSetOption(plug_plugins=... caksoylar/kakoune-focus )' took 0 us
command set-option took 11 us
hook 'GlobalSetOption(focus_separator={Whitespace}────────────────────────────────────────────────────────────────────────────────)' took 0 us
command declare-option took 9 us
hook 'GlobalSetOption(focus_context_lines=1)' took 0 us
command declare-option took 5 us
command declare-option took 1 us
command declare-option took 1 us
command declare-option took 1 us
command define-command took 4 us
command define-command took 1 us
command define-command took 1 us
command define-command took 1 us
command define-command took 3 us
command alias took 1 us
command alias took 0 us
sourcing '/home/sugimoto/.config/kak/plugins/plug.kak/..//kakoune-focus/focus.kak' took 95 us
command source took 102 us
command map took 4 us
command try took 10 us
sourcing '/home/sugimoto/.config/kak/plugins/plug.kak/..//.build/kakoune-focus/config' took 38 us
command source took 43 us
hook 'GlobalSetOption(plug_loaded_plugins=andreyorst/plug.kak JacobTravers/kampliment nojhan/kalolo evanrelf/number-toggle.kak JacobTravers/kak-rainbower kak-lsp/kak-lsp alexherbo2/window.kak alexherbo2/indent.kak andreyorst/smarttab.kak alexherbo2/alacritty.kak alexherbo2/auto-pairs.kak lePerdu/kakboard eburghar/kakpipe https://gitlab.com/Screwtapello/kakoune-repl-buffer JacobTravers/kakoune-sudo-write delapouite/kakoune-auto-percent eburghar/kakship natasky/kakoune-multi-file delapouite/kakoune-buffers delapouite/kakoune-cd caksoylar/kakoune-focus )' took 0 us
command set-option took 5 us
command evaluate-commands took 186 us
command try took 457186 us
command plug took 457190 us
shell:

        # $kak_client
        # $kak_config
        # $kak_opt_plug_always_ensure
        # $kak_opt_plug_git_domain
        # $kak_opt_plug_install_dir
        # $kak_opt_plug_loaded_plugins
        # $kak_opt_plug_max_active_downloads
        # $kak_opt_plug_plugin
        # $kak_opt_plug_plugins
        # $kak_opt_plug_profile
        # $kak_opt_plug_block_ui
        # $kak_opt_plug_report_conf_errors
        # $kak_opt_plug_conf_errors
        # $kak_session

        . "${kak_opt_plug_sh_source}"
        plug "$@"
    
----

this is considerably longer than any other plugin I have installed, for example here is kak-lsp

----
shell execution took 14917 us (spawn: 124, wait: 14737)
hook 'GlobalSetOption(plug_plugins=... kak-lsp/kak-lsp )' took 0 us
command set-option took 6 us
hook 'GlobalSetOption(lsp_cmd=kak-lsp -s 2748945)' took 0 us
command declare-option took 6 us
command set-face took 2 us
command set-face took 1 us
command set-face took 0 us
command set-face took 0 us
command set-face took 0 us
command set-face took 5 us
command set-face took 0 us
command set-face took 0 us
command set-face took 0 us
command set-face took 0 us
command set-face took 0 us
command set-face took 0 us
command set-face took 0 us
command set-face took 0 us
command set-face took 0 us
command declare-option took 1 us
hook 'GlobalSetOption(lsp_completion_trigger=execute-keys '<a-h><a-k>\S.\z<ret>')' took 0 us
command declare-option took 5 us
hook 'GlobalSetOption(lsp_completion_fragment_start=execute-keys "<esc><a-h>s\$?[\w%opt{lsp_extra_word_chars}]+.\z<ret>")' took 0 us
command declare-option took 5 us
command declare-option took 1 us
command hook took 5 us
hook 'GlobalSetOption(lsp_hover_insert_mode_trigger=execute-keys '<a-f>(s\A[^)]+[)]?\z<ret>')' took 0 us
command declare-option took 5 us
hook 'GlobalSetOption(lsp_insert_spaces=true)' took 0 us
command declare-option took 4 us
command declare-option took 1 us
command declare-option took 7 us
hook 'GlobalSetOption(lsp_hover_max_lines=20)' took 0 us
command declare-option took 8 us
command declare-option took 1 us
command add-highlighter took 12 us
command try took 17 us
command declare-option took 2 us
command declare-option took 1 us
hook 'GlobalSetOption(lsp_diagnostic_line_error_sign=*)' took 0 us
command declare-option took 5 us
hook 'GlobalSetOption(lsp_diagnostic_line_hint_sign=-)' took 0 us
command declare-option took 4 us
hook 'GlobalSetOption(lsp_diagnostic_line_info_sign=i)' took 0 us
command declare-option took 13 us
hook 'GlobalSetOption(lsp_diagnostic_line_warning_sign=!)' took 0 us
command declare-option took 4 us
hook 'GlobalSetOption(lsp_code_lens_sign=>)' took 0 us
command declare-option took 4 us
hook 'GlobalSetOption(lsp_inlay_diagnostic_sign=■)' took 0 us
command declare-option took 4 us
hook 'GlobalSetOption(lsp_inlay_diagnostic_gap=     )' took 0 us
command declare-option took 4 us
hook 'GlobalSetOption(lsp_show_hover_format=
info=$lsp_info \
    diagnostics=$lsp_diagnostics \
    code_lenses=$lsp_code_lenses \
    awk 'BEGIN {
        info = ENVIRON["info"]
        diagnostics = ENVIRON["diagnostics"];
        code_lenses = ENVIRON["code_lenses"];
        max_lines = ENVIRON["kak_opt_lsp_hover_max_lines"];

        r = ""
        lines = 0
        if (diagnostics) {
            r = r "{+b@InfoDefault}Diagnostics{InfoDefault} (shortcut e):\n" diagnostics "\n"
            diagnostics_lines = split(diagnostics, _, /\n/)
            lines += 1 + diagnostics_lines
        }
        if (code_lenses) {
            r = r "Code Lenses available (shortcut l)\n"
            lines++
        }
        if (ENVIRON["kak_opt_lsp_modeline_code_actions"]) {
            r = r "Code Actions available (shortcut a)\n"
            lines++
        }

        info_lines = split(info, info_line, /\n/)
        for (i = 1; i <= info_lines && (max_lines <= 0 || i+lines+2 <= max_lines); i++)
            print info_line[i]
        if (i < info_lines || r)
            printf "\n"
        if (i < info_lines)
            printf "{+i@InfoDefault}Hover info truncated, use lsp-hover-buffer (shortcut H) for full info\n"
        printf "%s", r
    }'
)' took 0 us
command declare-option took 11 us
hook 'GlobalSetOption(lsp_location_format=...)' took 0 us
command declare-option took 13 us
command define-command took 6 us
command declare-option took 1 us
command declare-option took 0 us
command set-register took 0 us
command evaluate-commands took 23 us
shell:

        buffer_basename="${kak_buffile##*/}"
        buffer_dirname=$(dirname "${kak_buffile}")

        if [ -f "${kak_buffile}" ]; then
            newer=$(find "${buffer_dirname}"/".${buffer_basename}.kak."* -newer "${kak_buffile}" -exec ls -1t {} + 2>/dev/null | head -n 1)
            older=$(find "${buffer_dirname}"/".${buffer_basename}.kak."* \! -newer "${kak_buffile}" -exec ls -1t {} + 2>/dev/null | head -n 1)
        else
            # New buffers that were never written to disk.
            newer=$(ls -1t "${buffer_dirname}"/".${buffer_basename}.kak."* 2>/dev/null | head -n 1)
            older=""
        fi

        if [ -z "${newer}" ]; then
            if [ -n "${older}" ]; then
                printf %s\\n "
                    echo -debug Old backup file(s) found: will not restore ${older} .
                "
            fi
            exit
        fi

        printf %s\\n "
            ## Replace the content of the buffer with the content of the backup file
            echo -debug Restoring file: ${newer}

            execute-keys -draft %{%d!cat<space>\"${newer}\"<ret>jd}

            ## If the backup file has to be removed, issue the command once
            ## the current buffer has been saved
            ## If the autorestore_purge_restored option has been unset right after the
            ## buffer was restored, do not remove the backup
            hook -group autorestore buffer BufWritePost '${kak_buffile}' %{
                nop %sh{
                    if [ \"\${kak_opt_autorestore_purge_restored}\" = true ];
                    then
                        rm -f \"${buffer_dirname}/.${buffer_basename}.kak.\"*
                    fi
                }
            }
        "
    
----

here is how I have kakoune-focus in my config

plug "caksoylar/kakoune-focus" branch "kak-master" config %{
  map global normal : -docstring "narrow"   ':focus-toggle <ret>'
}

Commenting this section gives me ~700ms reduction in startup time when tested with this script.

hyperfine -i "kak -e \"exec 'gj:q<ret>'\""

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.