-
Notifications
You must be signed in to change notification settings - Fork 34
/
extras.nix
202 lines (170 loc) · 6.57 KB
/
extras.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
{
config,
lib,
pkgs,
...
}:
let
inherit (lib)
mkIf
mkOption
optionalString
types
;
cfg = config.programs.neovim.extras;
nvr = "${pkgs.neovim-remote}/bin/nvr";
shellConfig =
with cfg.nvrAliases;
''
# START programs.neovim.extras config ----------------------------------------------------------
''
+ optionalString cfg.termBufferAutoChangeDir ''
# If shell is running in a Neovim terminal buffer, set the PWD of the buffer to `$PWD`.
if test -n "$NVIM"; nvim-sync-term-buffer-pwd; end
''
+ optionalString cfg.nvrAliases.enable ''
# Neovim Remote aliases
if test -n "$NVIM"
alias ${edit} "${nvr}"
alias ${split} "${nvr} -o"
alias ${vsplit} "${nvr} -O"
alias ${tabedit} "${nvr} --remote-tab"
alias ${nvim} "command nvim"
alias nvim "echo 'This shell is running in a Neovim termainal buffer. Use \'${nvim}\' to a nested instance of Neovim, otherwise use ${edit}, ${split}, ${vsplit}, or ${tabedit} to open files in the this Neovim instance.'"
else
alias ${edit} "nvim"
end
''
+ ''
# END programs.neovim.extras config ------------------------------------------------------------
'';
in
{
options.programs.neovim.extras = {
termBufferAutoChangeDir = mkOption {
type = types.bool;
default = false;
description = ''
When enabled, the <literal>pwd</literal> of terminal buffers in Neovim are automatically
updated to match <literal>$PWD</literal> of the shell running inside them.
Note that you cannot use this option if you are using
<option>programs.neovim.configure</option>, use <option>programs.neovim.extraConfig</option>
and <option>programs.neovim.plugins</option> instead.
(Currently only works with Fish shell.)
'';
};
nvrAliases = {
enable = mkOption {
type = types.bool;
default = false;
description = ''
When enabled, shell aliases for helpful Neovim Remote commands are created if the shell is
running inside a Neovim terminal buffer. Additionally, running <command>nvim</command>
won't open a nested Neovim instance but instead print a message listing the available
<command>nvr</command> aliases, as well as the command to run if you actually want to open
a nested Neovim instance.
Note that you cannot use this option if you are using
<option>programs.neovim.configure</option>, use
<option>programs.neovim.extraConfig</option> and <option>programs.neovim.plugins</option>
instead.
(Currently only works with Fish shell.)
'';
};
edit = mkOption {
type = types.str;
default = "n";
description = ''
Equivalent to Neovim's <command>:edit</command> command, i.e., running
<command>n [file]</command> will open the file in the current window.
When not in Neovim this also acts as an alias for <command>nvim</command>.
(Alias for <command>nvr</command>.)
'';
};
split = mkOption {
type = types.str;
default = "ns";
description = ''
Equivalent to Neovim's <command>:split/command> command.
(Alias for <command>nvr -o</command>.)
'';
};
vsplit = mkOption {
type = types.str;
default = "nv";
description = ''
Equivalent to Neovim's <command>:vsplit/command> command.
(Alias for <command>nvr -O</command>.)
'';
};
tabedit = mkOption {
type = types.str;
default = "nt";
description = ''
Equivalent to Neovim's <command>:tabedit</command> command.
(Alias for <command>nvr --remote-tab</command>.)
'';
};
nvim = mkOption {
type = types.str;
default = "neovim";
description = ''
Opens a nested Neovim instance.
'';
};
};
defaultEditor = mkOption {
type = types.bool;
default = false;
description = ''
When enabled, the <literal>EDITOR</literal> and <literal>VISUAL</literal> environment
variable are set to <command>nvr --remote-wait-silent</command>, and
<command>${nvr} -cc split --remote-wait +'set bufhidden=delete'</command> if the shell is
running inside a Neovim.
'';
};
};
config = mkIf config.programs.neovim.enable {
home.sessionVariables = mkIf cfg.defaultEditor {
EDITOR = "${nvr} --remote-wait-silent";
VISUAL = "${nvr} --remote-wait-silent";
};
programs.fish.functions.nvim-sync-term-buffer-pwd = mkIf cfg.termBufferAutoChangeDir {
body = ''
if test -n "$NVIM"
${nvr} -c "let g:term_buffer_pwds.$fish_pid = '$PWD' | call Set_term_buffer_pwd() "
end
'';
onVariable = "PWD";
};
programs.neovim.extraConfig = mkIf (cfg.termBufferAutoChangeDir || cfg.defaultEditor) (
optionalString cfg.termBufferAutoChangeDir ''
" START programs.neovim.extras.termBufferAutoChangeDir config ------------------------------
" Dictionary used to track the PWD of terminal buffers. Keys should be PIDs and values are
" is PWD of the shell with that PID. These values are updated from the shell using `nvr`.
let g:term_buffer_pwds = {}
" Function to call to update the PWD of the current terminal buffer.
function Set_term_buffer_pwd() abort
if &buftype == 'terminal' && exists('g:term_buffer_pwds[b:terminal_job_pid]')
execute 'lchd ' . g:term_buffer_pwds[b:terminal_job_pid]
endif
endfunction
" Sometimes the PWD the shell in a terminal buffer will change when in another buffer, so
" when entering a terminal buffer we update try to update it's PWD.
augroup NvimTermPwd
au!
au BufEnter * if &buftype == 'terminal' | call Set_term_buffer_pwd() | endif
augroup END
" END programs.neovim.extras.termBufferAutoChangeDir config --------------------------------
''
+ optionalString cfg.defaultEditor ''
" START programs.neovim.extras.defaultEditor config ----------------------------------------
let $EDITOR = '${nvr} -cc split -c "set bufhidden=delete" --remote-wait'
let $VISUAL = $EDITOR
" END programs.neovim.extras.defaultEditor config ------------------------------------------
''
);
programs.fish.interactiveShellInit = mkIf (
cfg.termBufferAutoChangeDir || cfg.nvrAliases.enable
) shellConfig;
};
}