-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RDY] New event: DirChanged #5928
Conversation
4b2d8b0
to
24b9d50
Compare
Might be worth referencing vim/vim#888 for ideas, e.g. the changes to Alternatively I wonder if application of this event should live in |
Okay, I looked at the vim PR and added the commit that prevents expanding filenames during the execution of the autocmd. About the handholding code in |
fea80a7
to
5d0d350
Compare
Okay, now Tests and linters are green. How do you like it so far? |
if (os_chdir((char *)dir) != 0) { | ||
return FAIL; | ||
} | ||
apply_autocmd_dirchanged(dir, kCdScopeWindow); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's confusing that this is done in vim_chdirfile but not vim_chdir.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly because I'm not sure how to access the scope from within vim_chdir()
.
end) | ||
|
||
-- TODO(mhinz): Add more tests here if we start to make | ||
-- use of the autocmd pattern. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To test the API, could use request("nvim_set_current_dir", ...)
, e.g. https://github.com/justinmk/neovim/blob/a85d116b1c4360da05ef1d6bc75e322b0b0b8e0d/test/functional/autocmd/bufenter_spec.lua#L16
case kCdScopeWindow: | ||
snprintf(buf, sizeof buf, "window"); | ||
break; | ||
default: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Omit default case, compiler will catch it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added it because clang gave me this warning:
warning: enumeration value 'kCdScopeInvalid' not handled in switch [-Wswitch]
switch (scope) {
^
Other places in the code do this:
case kCdScopeInvalid:
// Should never happen.
assert(false);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They do that so that if new values for the enum get added you'll see the warning about it being unhandled instead of it being swallowed by the default
case.
} | ||
|
||
dict_add_nr_str(dict, "scope", 0L, (char_u *)buf); | ||
dict_add_nr_str(dict, "path", 0L, new_dir); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's name this "cwd". for consistency with jobstart ().
snprintf(buf, sizeof buf, "tab"); | ||
break; | ||
case kCdScopeWindow: | ||
snprintf(buf, sizeof buf, "window"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we always use parens for size of.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still not a fan of sizeof(foo)
, because sizeof
is not a function, even if its usage is similar. But I'll be nice. ;]
5d0d350
to
8b60767
Compare
Okay, I addressed all mentioned issues apart from the bit that |
local request = h.request | ||
|
||
describe('DirChanged ->', function() | ||
local wd = lfs.currentdir() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curdir
would be more familiar name.
end) | ||
|
||
-- TODO(mhinz): Add more tests here if we start to make | ||
-- use of the autocmd pattern. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment probably isn't needed, it's implied that changes should be accompanied by tests :)
|
||
describe('"autocmd DirChanged', function() | ||
it('*" sets v:event for all :cd variants', function() | ||
execute('autocmd DirChanged * let event = copy(v:event)') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Globals should be prefixed with g:
.
execute('autocmd DirChanged * let event = copy(v:event)') | ||
|
||
execute('lcd '..dirs[1]) | ||
eq({cwd=dirs[1], scope='window'}, eval('event')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
g:
end) | ||
end) | ||
|
||
describe('API ->', function() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need nested describe blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it would be nicer for later additions to group everything logically right away.. maybe a bit overkill. :>
end) | ||
|
||
describe("'autochdir'", function() | ||
it('triggers DirChanged', function() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could just be
it("'autochdir' triggers DirChanged", ...
and then the nested describe isn't needed. The setup() should be in before_each()
then.
8b60767
to
07a4d49
Compare
|
||
if (vim_chdir(new_dir, scope)) { | ||
EMSG(_(e_failed)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need an else{} block wrapping the stuff below, before the xfree call.
kCdScopeGlobal, ///< Affects the entire instance of Neovim. | ||
} CdScope; | ||
#define MIN_CD_SCOPE kCdScopeWindow | ||
#define MAX_CD_SCOPE kCdScopeGlobal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should go in globals.h or vim.h, I think types.h only exists for unit tests and ultimately is something we want to eliminate. Its purpose was never clear.
07a4d49
to
6f2d429
Compare
{ | ||
char_u *dir_name = find_directory_in_path(new_dir, STRLEN(new_dir), | ||
FNAME_MESS, curbuf->b_ffname); | ||
if (dir_name == NULL) { | ||
return -1; | ||
} | ||
int r = os_chdir((char *)dir_name); | ||
apply_autocmd_dirchanged(dir_name, scope); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should not be applied if os_chdir
fails
|
||
before_each(clear) | ||
|
||
it('"autocmd DirChanged *" sets v:event for all :cd variants', function() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need some tests that verify DirChanged is not published if :{t,l}cd
fails (e.g. invalid directory)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it be good enough if I test that g:event
didn't change with :cd
in between or is there a better way to check that an event didn't fire?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems good. (:cd <invalid dir>
)
@@ -1522,6 +1523,35 @@ find_file_in_path_option ( | |||
return file_name; | |||
} | |||
|
|||
static void apply_autocmd_dirchanged(char_u *new_dir, CdScope scope) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe do_autocmd_dirchanged
is a better name. The precedent is do_autocmd_event()
and yank_do_autocmd
. The latter should probably be renamed to do_autocmd_textyankpost
.
static void apply_autocmd_dirchanged(char_u *new_dir, CdScope scope) | ||
{ | ||
dict_T *dict = get_vim_var_dict(VV_EVENT); | ||
char buf[8]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A check like the one in yank_do_autocmd
would be worth adding here.
if (recursive || !has_event(EVENT_DIRCHANGED)) {
// No autocommand was defined
// or we changed directory from this autocommand.
return;
}
Speaking of which, let's prevent recursive DirChanged. (We can always add it later if it is needed for some reason)
post_chdir(scope); | ||
if (vim_chdir(new_dir, scope)) { | ||
EMSG(_(e_failed)); | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this cause a memory leak? the xfree(tofree)
below will be missed. Better to keep the relevant part in an else{} block.
6f2d429
to
0868a83
Compare
@@ -2538,18 +2538,18 @@ static void yank_copy_line(yankreg_T *reg, struct block_def *bd, size_t y_idx) | |||
/// | |||
/// @param oap Operator arguments. | |||
/// @param reg The yank register used. | |||
static void yank_do_autocmd(oparg_T *oap, yankreg_T *reg) | |||
static void do_autocmd_yank(oparg_T *oap, yankreg_T *reg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be do_autocmd_textyankpost
, to be consistent with do_autocmd_dirchanged
FUNC_ATTR_NONNULL_ALL | ||
{ | ||
static bool recursive = false; | ||
static bool recursive_yank = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why change this? it's a local variable, the context is clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A static variable inside a function means that its scope is the function, but its lifetime is that of the entire program. Thus it shouldn't have such a general name to avoid name clashes in the future.
So, do_autocmd_textyankpost()
will use recursive_textyankpost
and do_autocmd_dirchanged()
will use recursive_dirchanged
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid name clashes with what? As you stated, the scope is the function, so do you expect this function to get another variable named recursive
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There you go, I just don't know C. I thought that "program-wide lifetime" equals "global namespace", too. :\
I'll change them back.
8cf69da
to
bbca094
Compare
bbca094
to
f2f01c0
Compare
@@ -6952,7 +6952,7 @@ void free_cd_dir(void) | |||
/// Deal with the side effects of changing the current directory. | |||
/// | |||
/// @param scope Scope of the function call (global, tab or window). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new_dir
parameter was added ... which isn't used?
FUNC_ATTR_NONNULL_ALL | ||
{ | ||
static bool recursive = false; | ||
|
||
if (recursive || !has_event(EVENT_TEXTYANKPOST)) { | ||
if (rerecursive || !has_event(EVENT_TEXTYANKPOST)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo rerecursive
→ recursive
and on lines 2552, 2586.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♂️
f2f01c0
to
1f7a119
Compare
FEATURES: bc4a2e1 help, man.vim: "outline" (TOC) feature neovim#5169 58422f1 'guicursor' works in the TUI (and sends info to UIs) neovim#6423 129f107 api: nvim_get_mode() neovim#6247 0b59f98 api/ui: externalize tabline neovim#6583 bc6d868 'listchars': `Whitespace` highlight group neovim#6367 6afa7d6 writefile() obeys 'fsync' option neovim#6427 c60e409 eval.c refactor (also improves some error messages) neovim#5119 9d200cd getcompletion("cmdline") neovim#6376 2ea7bfc terminal: Support extra arguments in 'shell'. neovim#4504 bf51102 DirChanged autocmd neovim#5928 neovim#6262 1743df8 'cpoptions': "_" flag to toggle `cw` behaviour neovim#6235 22337b1 CTRL-R omits trailing ^M when pasting to cmdline neovim#6137 0e44916 :edit allows unescaped spaces in filename neovim#6119 abdbfd2 eval: Add id() function and make printf("%p") useful neovim#6095 bdfa147 findfile(), :find, gf work in :terminal. neovim#6009 2f38ed1 providers: Disable if `g:loaded_*` exists. b5560a6 setpos() can set lowercase marks in other buffers neovim#5753 7c513d6 Throttle :! output, pulse "..." message. neovim#5396 d2e8c76 v:exiting neovim#5651 :terminal improvements neovim#6185 neovim#6142 - cursor keeps position after leaving insert-mode. - 4ceec30 Follows output only if cursor is at end of buffer. - e7bbd35 new option: 'scrollback' - fedb844 quasi-support for undo and 'modifiable' - b45ddf7 disables 'list' by default - disables 'relativenumber' by default :help now contains full API documentation at `:help api`. man.vim saw numerous improvements. Windows support: - Windows is no longer "experimental", it is fully supported. - Windows package includes a GUI, curl.exe and other utilities. "Vim 8" features: partials, lambdas, packages. FIXES: 12fc1de ops: fix i<c-r> with multi-byte text neovim#6524 dd391bf Windows: system() and friends neovim#6497 13352c0 Windows: os_get_hostname() neovim#6413 16babc6 tui: Less-noisy mouse seqs neovim#6411 3a9dd13 (vim bug) folding edge-cases neovim#6207 f6946c6 job-control: set CLOEXEC on pty processes. neovim#5986 d1afd43 rplugin: Call s:LoadRemotePlugins() on startup. 1215084 backtick-expansion works with `shell=fish` neovim#6224 e32ec03 tui: Improved behavior after resize. neovim#6202 86c2adc edit.c: CTRL-SPC: Insert previously-inserted text. neovim#6090 c318d8e b:changedtick now follows VimL rules neovim#6112 34e24cb terminal: Initialize colors in reverse order neovim#6160 e889917 undo: Don't set b_u_curhead in ex_undojoin() neovim#5869 d25649f undo: :earlier, g-: Set b_u_seq_cur correctly. (neovim#6016) 043d8ba 'Visual-mode put from @. register' neovim#5782 42c922b open_buffer(): Do `BufEnter` for directories. 50d0d89 inccommand: Preview :sub commands only after delimiter neovim#5932 1420e10 CheckHealth improvements neovim#5519 c8d5e92 jobstart(): Return -1 if cmd is not executable. neovim#5671 CHANGES: NVIM_TUI_ENABLE_CURSOR_SHAPE was removed. Use 'guicursor' instead. See https://github.com/neovim/neovim/wiki/Following-HEAD#20170402 81525dc 'mouse=a' is no longer the default. (This will probably change again after it is improved.) neovim#6022 0c1f783 defaults: 'showcmd', 'belloff', 'ruler' neovim#6087 eb0e94f api: {get,set}_option update local options as appropriate neovim#6405 bdcb2a3 "Reading from stdin..." message was removed. neovim#6298
FEATURES: bc4a2e1 help, man.vim: "outline" (TOC) feature neovim#5169 58422f1 'guicursor' works in the TUI (and sends info to UIs) neovim#6423 129f107 api: nvim_get_mode() neovim#6247 0b59f98 api/ui: externalize tabline neovim#6583 bc6d868 'listchars': `Whitespace` highlight group neovim#6367 6afa7d6 writefile() obeys 'fsync' option neovim#6427 c60e409 eval.c refactor (also improves some error messages) neovim#5119 9d200cd getcompletion("cmdline") neovim#6376 2ea7bfc terminal: Support extra arguments in 'shell'. neovim#4504 bf51102 DirChanged autocmd neovim#5928 neovim#6262 1743df8 'cpoptions': "_" flag to toggle `cw` behaviour neovim#6235 22337b1 CTRL-R omits trailing ^M when pasting to cmdline neovim#6137 0e44916 :edit allows unescaped spaces in filename neovim#6119 abdbfd2 eval: Add id() function and make printf("%p") useful neovim#6095 bdfa147 findfile(), :find, gf work in :terminal. neovim#6009 2f38ed1 providers: Disable if `g:loaded_*` exists. b5560a6 setpos() can set lowercase marks in other buffers neovim#5753 7c513d6 Throttle :! output, pulse "..." message. neovim#5396 d2e8c76 v:exiting neovim#5651 :terminal improvements neovim#6185 neovim#6142 - cursor keeps position after leaving insert-mode. - 4ceec30 Follows output only if cursor is at end of buffer. - e7bbd35 new option: 'scrollback' - fedb844 quasi-support for undo and 'modifiable' - b45ddf7 disables 'list' by default - disables 'relativenumber' by default :help now contains full API documentation at `:help api`. man.vim saw numerous improvements. Windows support: - Windows is no longer "experimental", it is fully supported. - Windows package includes a GUI, curl.exe and other utilities. "Vim 8" features: partials, lambdas. SECURITY FIXES: CVE-2017-5953 CVE-2017-6349 CVE-2017-6350 neovim#6485 CHANGES: NVIM_TUI_ENABLE_CURSOR_SHAPE was removed. Use 'guicursor' instead. See https://github.com/neovim/neovim/wiki/Following-HEAD#20170402 81525dc 'mouse=a' is no longer the default. (This will probably change again after it is improved.) neovim#6022 0c1f783 defaults: 'showcmd', 'belloff', 'ruler' neovim#6087 eb0e94f api: {get,set}_option update local options as appropriate neovim#6405 bdcb2a3 "Reading from stdin..." message was removed. neovim#6298 FIXES: 12fc1de ops: fix i<c-r> with multi-byte text neovim#6524 dd391bf Windows: system() and friends neovim#6497 13352c0 Windows: os_get_hostname() neovim#6413 16babc6 tui: Less-noisy mouse seqs neovim#6411 3a9dd13 (vim bug) folding edge-cases neovim#6207 f6946c6 job-control: set CLOEXEC on pty processes. neovim#5986 d1afd43 rplugin: Call s:LoadRemotePlugins() on startup. 1215084 backtick-expansion works with `shell=fish` neovim#6224 e32ec03 tui: Improved behavior after resize. neovim#6202 86c2adc edit.c: CTRL-SPC: Insert previously-inserted text. neovim#6090 c318d8e b:changedtick now follows VimL rules neovim#6112 34e24cb terminal: Initialize colors in reverse order neovim#6160 e889917 undo: Don't set b_u_curhead in ex_undojoin() neovim#5869 d25649f undo: :earlier, g-: Set b_u_seq_cur correctly. (neovim#6016) 043d8ba 'Visual-mode put from @. register' neovim#5782 42c922b open_buffer(): Do `BufEnter` for directories. 50d0d89 inccommand: Preview :sub commands only after delimiter neovim#5932 1420e10 CheckHealth improvements neovim#5519 c8d5e92 jobstart(): Return -1 if cmd is not executable. neovim#5671
FEATURES: bc4a2e1 help, man.vim: "outline" (TOC) feature neovim#5169 58422f1 'guicursor' works in the TUI (and sends info to UIs) neovim#6423 129f107 api: nvim_get_mode() neovim#6247 0b59f98 api/ui: externalize tabline neovim#6583 bc6d868 'listchars': `Whitespace` highlight group neovim#6367 6afa7d6 writefile() obeys 'fsync' option neovim#6427 c60e409 eval.c refactor (also improves some error messages) neovim#5119 9d200cd getcompletion("cmdline") neovim#6376 2ea7bfc terminal: Support extra arguments in 'shell'. neovim#4504 bf51102 DirChanged autocmd neovim#5928 neovim#6262 1743df8 'cpoptions': "_" flag to toggle `cw` behaviour neovim#6235 22337b1 CTRL-R omits trailing ^M when pasting to cmdline neovim#6137 0e44916 :edit allows unescaped spaces in filename neovim#6119 abdbfd2 eval: Add id() function and make printf("%p") useful neovim#6095 bdfa147 findfile(), :find, gf work in :terminal. neovim#6009 2f38ed1 providers: Disable if `g:loaded_*` exists. b5560a6 setpos() can set lowercase marks in other buffers neovim#5753 7c513d6 Throttle :! output, pulse "..." message. neovim#5396 d2e8c76 v:exiting neovim#5651 :terminal improvements neovim#6185 neovim#6142 - cursor keeps position after leaving insert-mode. - 4ceec30 Follows output only if cursor is at end of buffer. - e7bbd35 new option: 'scrollback' - fedb844 quasi-support for undo and 'modifiable' - b45ddf7 disables 'list' by default - disables 'relativenumber' by default :help now contains full API documentation at `:help api`. man.vim saw numerous improvements. Windows support: - Windows is no longer "experimental", it is fully supported. - Windows package includes a GUI, curl.exe and other utilities. "Vim 8" features: partials, lambdas. SECURITY FIXES: CVE-2017-5953 CVE-2017-6349 CVE-2017-6350 neovim#6485 CHANGES: NVIM_TUI_ENABLE_CURSOR_SHAPE was removed. Use 'guicursor' instead. See https://github.com/neovim/neovim/wiki/Following-HEAD#20170402 81525dc 'mouse=a' is no longer the default. (This will probably change again after it is improved.) neovim#6022 0c1f783 defaults: 'showcmd', 'belloff', 'ruler' neovim#6087 eb0e94f api: {get,set}_option update local options as appropriate neovim#6405 bdcb2a3 "Reading from stdin..." message was removed. neovim#6298 FIXES: 12fc1de ops: fix i<c-r> with multi-byte text neovim#6524 dd391bf Windows: system() and friends neovim#6497 13352c0 Windows: os_get_hostname() neovim#6413 16babc6 tui: Less-noisy mouse seqs neovim#6411 3a9dd13 (vim bug) folding edge-cases neovim#6207 f6946c6 job-control: set CLOEXEC on pty processes. neovim#5986 d1afd43 rplugin: Call s:LoadRemotePlugins() on startup. 1215084 backtick-expansion works with `shell=fish` neovim#6224 e32ec03 tui: Improved behavior after resize. neovim#6202 86c2adc edit.c: CTRL-SPC: Insert previously-inserted text. neovim#6090 c318d8e b:changedtick now follows VimL rules neovim#6112 34e24cb terminal: Initialize colors in reverse order neovim#6160 e889917 undo: Don't set b_u_curhead in ex_undojoin() neovim#5869 d25649f undo: :earlier, g-: Set b_u_seq_cur correctly. (neovim#6016) 043d8ba 'Visual-mode put from @. register' neovim#5782 42c922b open_buffer(): Do `BufEnter` for directories. 50d0d89 inccommand: Preview :sub commands only after delimiter neovim#5932 1420e10 CheckHealth improvements neovim#5519 c8d5e92 jobstart(): Return -1 if cmd is not executable. neovim#5671
v:event.cwd
and<afile>
are set to the new directoryv:event.scope
is set to either "global", "tab", "window", or "invalid" (which should never happen)This is just a suggestion so far. What do you think?