AutoYADM commit: 2025-07-21 13:53:39

This commit is contained in:
Daniel Fichtinger 2025-07-21 13:53:39 -04:00
parent 0fb9c25d0a
commit 57f262d4e4
6 changed files with 344 additions and 6 deletions

View file

@ -1,6 +1,6 @@
declare-user-mode git
declare-user-mode ficgit
map -docstring "git" global user g ": enter-user-mode git<ret>"
map -docstring "lazygit kak cwd" global git g ": terminal lazygit<ret>"
map -docstring "lazygit kak cwd" global ficgit g ": terminal lazygit<ret>"
define-command lazygit-buffer-dir %{
evaluate-commands %sh{
@ -8,16 +8,16 @@ define-command lazygit-buffer-dir %{
printf "terminal lazygit -p %s" "$ROOT"
}
}
map -docstring "lazygit buffer directory" global git G ": lazygit-buffer-dir<ret>"
map -docstring "lazygit buffer directory" global ficgit G ": lazygit-buffer-dir<ret>"
define-command git-blame %{
info %sh{
git -C $(dirname $(realpath $kak_buffile)) blame -L $kak_cursor_line,$kak_cursor_line $(realpath $kak_buffile)
}
}
map -docstring "blame" global git b ": git-blame<ret>"
map -docstring "show diff" global git d ": git show-diff<ret>"
map -docstring "hide diff" global git D ": git hide-diff<ret>"
map -docstring "blame" global ficgit b ": git-blame<ret>"
map -docstring "show diff" global ficgit d ": git show-diff<ret>"
map -docstring "hide diff" global ficgit D ": git hide-diff<ret>"
provide-module git-diff %~

View file

@ -0,0 +1,30 @@
Copyright (c) 2023, Dimitri Sabadie <dimitri.sabadie@gmail.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Dimitri Sabadie <dimitri.sabadie@gmail.com> nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View file

@ -0,0 +1,23 @@
# git.kak, a complementary set of Git tools for Kakoune
## Installation
Go in your config path — e.g. `~/.config/kak`, and clone this repository or link it in any
subdirectory of `autoload`. For instance:
```
cd ~/.config/kak
mkdir -p autoload/plugins
cd autoload/plugins
git clone https://git.sr.ht/~hadronized/git.kak
```
## Usage
Most of the tools are available via the `git` user-mode. Typical usage would be to place it under the user
mode, for instance:
```
map global user g ':enter-user-mode git<ret>'
```

View file

@ -0,0 +1,203 @@
# Git branches in buffers.
#
# This small script provides convenience mechanisms to operate on Git branches by altering a buffer.
# It can:
#
# - Switch branches.
# - Create new branches with optional starting point.
# - Rename branches.
# - Delete and force-delete branches.
declare-option str git_bin "git --no-optional-locks"
declare-option str git_list_local_branches "%opt{git_bin} branch"
declare-option str git_branch "xargs %opt{git_bin} branch"
declare-option str git_branch_rename "xargs %opt{git_bin} branch -m"
declare-option str git_branch_switch "xargs %opt{git_bin} switch"
declare-user-mode git-branches
# Create a branch listing buffer.
define-command git-branches %{
edit -scratch "*git-branches*"
set-option buffer filetype git-branches
git-branches-refresh
# highlighters
add-highlighter buffer/ ref git-branches
# mappings
map buffer normal <ret> ':git-branches-switch<ret>' -docstring 'switch branch'
map buffer git-branches <ret> ':git-branches-switch<ret>' -docstring 'switch branch'
map buffer git-branches c ':git-branches-commit-pending<ret>' -docstring 'commit pending changes'
map buffer git-branches d ':git-branches-delete<ret>' -docstring 'delete branch'
map buffer git-branches D ':git-branches-force-delete<ret>' -docstring 'force-delete branch'
map buffer git-branches g ':git-branches-refresh<ret>' -docstring 'refresh'
map buffer git-branches n ':git-branches-new<ret>' -docstring 'new branch'
map buffer git-branches r ':git-branches-rename<ret>' -docstring 'rename branch'
}
# Save the content of the current branch (primary sel) in the "b register.
define-command -hidden git-save-branch-name %{
try %{
execute-keys ';,ghll<a-E>"by'
}
}
# Restore the position of the main selection. Required to have saved something in the "b register.
define-command -hidden git-restore-branch-name %{
try %{
execute-keys '/<c-r>b<ret>;'
}
}
# Refresh the content of the branch buffer. This will lose all information that have been modified in it.
# TODO: support + branches (I dont recall what they are, but I think it means they are checked out in
# working trees?)
define-command -hidden git-branches-refresh %{
evaluate-commands -save-regs b %{
git-save-branch-name
execute-keys -draft "%%|%opt{git_list_local_branches}<ret>"
# replace the * for current branch by c
execute-keys -draft '%s^\*<ret>rc'
git-restore-branch-name
execute-keys 'gh'
}
}
# Delete a branch.
#
# Various situations are possible, and are acted upon by dispatching to git-branches-delete-- based on the
# first charact of every selections line.
define-command -hidden git-branches-delete %{
execute-keys -itersel '<a-s>ghy:git-branches-delete--<c-r>" d<ret>'
}
# Force delete a branch.
#
# Same as the non-force version.
define-command -hidden git-branches-force-delete %{
execute-keys -itersel '<a-s>ghy:git-branches-delete--<c-r>" D<ret>'
}
# Switch the branch as pending deletion.
#
# The argument is used for regular deletion (d) or force-deletion (D).
define-command -hidden git-branches-delete-- -params 1 %{
execute-keys "ghr%arg{1}gh"
}
# Deletion of a rename line simply removes the rename annotation, and delete.
define-command -hidden git-branches-delete--r -params 1 %{
git-branches-unrename
git-branches-delete-- %arg{1}
}
# Undelete.
define-command -hidden git-branches-delete--d -params 1 %{
execute-keys 'ghr gh'
}
# Force delete.
define-command -hidden git-branches-delete--D -params 1 %{
execute-keys 'ghr gh'
}
# Deletion on current branches is simply not allowed for obvious reasons (Git will refuse it anyway).
define-command -hidden git-branches-delete--c -params 1 %{
fail 'cannot delete current branches'
}
# Switch to a given branch (primary selection only).
define-command -hidden git-branches-switch %{
execute-keys -draft ",ghllGl<a-|>%opt{git_branch_switch}<ret>"
git-branches-refresh
}
# Rename branches.
#
# Dispatching is done via git-branches-rename--.
define-command -hidden git-branches-rename %{
execute-keys -itersel '<a-s>ghy:git-branches-rename--<c-r>"<ret>'
}
# Rename branch implementation.
define-command -hidden git-branches-rename-- %{
execute-keys -save-regs b 'ghrrll<a-E>"bya <c-r>b<esc>;<a-i><a-w>'
}
# Renaming a deletion branch.
define-command -hidden git-branches-rename--d %{
git-branches-rename--
}
# Remove renaming annotation.
define-command -hidden git-branches-rename--r %{
git-branches-unrename
}
# We forbid renaming current branches.
define-command -hidden git-branches-rename--c %{
fail 'cannot rename current branches'
}
# Unrename (remove rename annotations).
define-command -hidden git-branches-unrename %{
execute-keys 'ghr <a-E><a-E>dgh'
}
# Create new branches.
#
# This is a handy version that uses the optional second argument by yanking the branches selections are on.
define-command -hidden git-branches-new %{
execute-keys '<a-s>ghll<a-E>ygjo<esc><a-P>a<ret><esc>in new-<c-r>" <esc>hh<a-i><a-w>'
}
# Execute pending deletions. Called on commit.
define-command -hidden git-branches-execute-delete %{
try %{
execute-keys -draft -itersel "%%s^(d|D)y<ret>llGl<a-|>%opt{git_branch} -<c-r>""<ret>"
}
}
# Execute pending renames. Called on commit.
define-command -hidden git-branches-execute-rename %{
try %{
execute-keys -draft "%%s^r<ret>ddx_<a-|>%opt{git_branch_rename}<ret>"
}
}
# Execute pending creations. Called on commit.
define-command -hidden git-branches-execute-new %{
try %{
execute-keys -draft "%%s^n<ret>ddx_<a-|>%opt{git_branch}<ret>"
}
}
# Commit pending modifications in the buffer and refresh.
define-command -hidden git-branches-commit-pending %{
git-branches-execute-delete
git-branches-execute-rename
git-branches-execute-new
git-branches-refresh
}
# Highlighting.
set-face global GitOpsBranchCurrent green
set-face global GitOpsBranchOther default
set-face global GitOpsBranchDeleted red+s
set-face global GitOpsBranchForceDeleted red+bs
set-face global GitOpsBranchRenameSource blue
set-face global GitOpsBranchRenameDest cyan+b
set-face global GitOpsBranchNewName blue
set-face global GitOpsBranchNewStart yellow+i
# Highlighters.
add-highlighter shared/git-branches group
add-highlighter shared/git-branches/current-branch regex '^(c)\s+([^\n]+)$' "1:black" "2:GitOpsBranchCurrent"
add-highlighter shared/git-branches/other-branch regex '^\s+([^\n]+)$' "1:GitOpsBranchOther"
add-highlighter shared/git-branches/deleted-branch regex '^(d)\s+([^\n]+)$' "1:black" "2:GitOpsBranchDeleted"
add-highlighter shared/git-branches/force-deleted-branch regex '^(D)\s+([^\n]+)$' "1:black" "2:GitOpsBranchForceDeleted"
add-highlighter shared/git-branches/renamed-branch regex '^(r)\s+([^\n ]+) ([^\n ]*)$' "1:black" "2:GitOpsBranchRenameSource" "3:GitOpsBranchRenameDest"
add-highlighter shared/git-branches/new-branch regex '^(n)\s+([^\n ]+) ?([^\n ]*)?$' "1:black" "2:GitOpsBranchNewName" "3:GitOpsBranchNewStart"

View file

@ -0,0 +1,77 @@
declare-option str awk_cmd 'awk'
declare-user-mode git
map global git <ret> ':git-show-current-line<ret>' -docstring 'open last commit that touched current line'
map global git b ':git-branches<ret>' -docstring 'list branches'
map global git B ':git-blame-current-line<ret>' -docstring 'blame current line'
map global git n ':git next-hunk<ret>' -docstring 'goto next hunk'
map global git p ':git prev-hunk<ret>' -docstring 'goto previous hunk'
map global git d ':git diff<ret>' -docstring 'diff'
## Blame current line
set-face global GitBlameLineRef red,black
set-face global GitBlameLineSummary green,black
set-face global GitBlameLineAuthor blue,black
set-face global GitBlameLineTime default,black@comment
define-command git-blame-current-line %{
info -markup -style above -anchor "%val{cursor_line}.%val{cursor_column}" -- %sh{
git blame -L$kak_cursor_line,$kak_cursor_line $kak_bufname --incremental | $kak_opt_awk_cmd '\
BEGIN {
ref = ""
author = ""
time = ""
summary = ""
}
/^[a-f0-9]+ [0-9]+ [0-9]+ [0-9]+$/ {
ref = substr($1, 0, 8)
}
/summary/ {
for (i = 2; i < NF; i++) {
summary = summary $i " "
}
summary = summary $NF
}
/author / {
for (i = 2; i < NF; i++) {
author = author $i " "
}
author = author $NF
}
/author-time/ {
time = strftime("%a %d %b %Y, %H:%M:%S", $2)
}
END {
first = sprintf("{GitBlameLineRef}%s {GitBlameLineSummary}%s", ref, summary)
second = sprintf("{GitBlameLineAuthor}%s {GitBlameLineTime}on %s", author, time)
max_len = length(first)
second_len = length(second)
if (second_len > max_len) {
max_len = second_len
}
fmt_string = sprintf("%%-%ds", max_len)
printf fmt_string "\n", first
printf fmt_string, second
}'
}
}
## Show the commit that touched the line under the cursor.
declare-option str git_show_current_line_commit
define-command git-show-current-line %{
set-option global git_show_current_line_commit %sh{ git blame -L$kak_cursor_line,$kak_cursor_line $kak_bufname | cut -d' ' -f1 }
edit -scratch *git*
set-option buffer filetype git-commit
execute-keys '%|git show --pretty=fuller $kak_opt_git_show_current_line_commit<ret>gg'
set-option buffer readonly true
}