Skip to content

Commit f661e10

Browse files
committed
Add mapping to yank current commit hash
As you can't enter a Vim popup window, you can't easily yank the current commit hash, and that seems a natural thing to want to do (I certainly found myself wanting to do it soon after starting to use git-messenger). To address this, map 'c' to yank the current commit hash to v:register (kind of, while the code behaves the same as using v:register, it doesn't actually use v:register due to some clash with vim-cutlass). While this is added primarily to aid Vim popup users, it doesn't break anything for Neovim floating win or preview window users as the buffer is not modifiable, and it seems a handy mapping for all users anyway.
1 parent ef785fb commit f661e10

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Following mappings are defined within popup window.
139139
| `D` | Toggle all unified diff hunks of the commit |
140140
| `r` | Toggle word diff hunks only in current file of the commit |
141141
| `R` | Toggle all word diff hunks of current commit |
142+
| `c` | Yank/copy the current commit hash to `v:register` |
142143
| `?` | Show mappings help |
143144

144145
### Mappings

autoload/gitmessenger/blame.vim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ function! s:blame__forward() dict abort
6363
endfunction
6464
let s:blame.forward = funcref('s:blame__forward')
6565

66+
function! s:blame__yank_hash() dict abort
67+
" Note: v:register is blackhole here when vim-cutlass plugin used
68+
" TODO: investigate further, it should be possible to use v:register
69+
let register = '"'
70+
if has('clipboard')
71+
if stridx(&clipboard, 'unnamedplus') != -1
72+
let register = '+'
73+
elseif stridx(&clipboard, 'unnamed') != -1
74+
let register = '*'
75+
endif
76+
endif
77+
call setreg(register, self.state.commit)
78+
echo 'git-messenger: yanked commit hash ' . self.state.commit
79+
endfunction
80+
let s:blame.yank_hash = funcref('s:blame__yank_hash')
81+
6682
function! s:blame__open_popup() dict abort
6783
if has_key(self, 'popup') && has_key(self.popup, 'bufnr')
6884
" Already popup is open. It means that now older commit is showing up.
@@ -80,6 +96,7 @@ function! s:blame__open_popup() dict abort
8096
\ 'q': [{-> execute('close', '')}, 'Close popup window'],
8197
\ 'o': [funcref(self.back, [], self), 'Back to older commit'],
8298
\ 'O': [funcref(self.forward, [], self), 'Forward to newer commit'],
99+
\ 'c': [funcref(self.yank_hash), self, 'Yank current commit hash'],
83100
\ 'd': [funcref(self.reveal_diff, [v:false, v:false], self), "Toggle current file's diffs"],
84101
\ 'D': [funcref(self.reveal_diff, [v:true, v:false], self), 'Toggle all diffs'],
85102
\ 'r': [funcref(self.reveal_diff, [v:false, v:true], self), "Toggle current file's word diffs"],

doc/git-messenger.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ COMMANDS *git-messenger-commands*
137137
| D | Toggle all unified diff hunks of the commit |
138138
| r | Toggle word diff hunks only in current file of the commit |
139139
| R | Toggle all word diff hunks of the commit |
140+
| c | Yank/copy the current commit hash to |v:register| |
140141
| ? | Show mappings help |
141142

142143
*:GitMessengerClose*

0 commit comments

Comments
 (0)