Skip to content

sync terminal buffer's cwd with shell's pwd#6290

Closed
coiby wants to merge 4 commits intovim:masterfrom
coiby:sync_terminal_cwd
Closed

sync terminal buffer's cwd with shell's pwd#6290
coiby wants to merge 4 commits intovim:masterfrom
coiby:sync_terminal_cwd

Conversation

@coiby
Copy link
Copy Markdown

@coiby coiby commented Jun 18, 2020

In gnome-terminal/xfce4-terminal, it's handy to create a new terminal tab with pwd set to the current terminal's pwd. To let vim's terminal enumerator have this feature, we should set a buffer's cwd to the shell's pwd.

In current version of vim, when a new terminal instance is started, the cwd will be set to ~. On Linux, when there is the change of current working directory, the shell will notify the terminal by the OSC 7; command. Use this feature to chdir buffer's cwd to the shell's working directory.

@lgtm-com
Copy link
Copy Markdown

lgtm-com bot commented Jun 18, 2020

This pull request introduces 1 alert when merging f012d10 into 3d9207a - view on LGTM.com

new alerts:

  • 1 for Constant return type

@coiby coiby force-pushed the sync_terminal_cwd branch 2 times, most recently from d1e00da to 3e387f4 Compare December 23, 2020 07:21
@codecov
Copy link
Copy Markdown

codecov bot commented Dec 23, 2020

Codecov Report

Merging #6290 (0ed1576) into master (bb5d87c) will decrease coverage by 0.11%.
The diff coverage is 96.31%.

❗ Current head 0ed1576 differs from pull request most recent head d8b24f3. Consider uploading reports for the commit d8b24f3 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #6290      +/-   ##
==========================================
- Coverage   89.23%   89.12%   -0.12%     
==========================================
  Files         148      148              
  Lines      165558   163179    -2379     
==========================================
- Hits       147735   145431    -2304     
+ Misses      17823    17748      -75     
Flag Coverage Δ
huge-clang-none 88.15% <100.00%> (-0.23%) ⬇️
huge-gcc-none 88.70% <93.86%> (+<0.01%) ⬆️
huge-gcc-testgui 87.19% <96.31%> (+0.01%) ⬆️
huge-gcc-unittests 2.48% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/charset.c 89.65% <ø> (-0.06%) ⬇️
src/version.c 92.64% <ø> (+0.30%) ⬆️
src/terminal.c 90.98% <85.71%> (-0.18%) ⬇️
src/vim9compile.c 93.91% <92.30%> (-0.17%) ⬇️
src/channel.c 89.90% <100.00%> (-0.13%) ⬇️
src/cmdexpand.c 93.59% <100.00%> (-0.08%) ⬇️
src/eval.c 96.23% <100.00%> (+<0.01%) ⬆️
src/evalbuffer.c 96.78% <100.00%> (-0.03%) ⬇️
src/evalfunc.c 96.03% <100.00%> (+0.01%) ⬆️
src/evalvars.c 95.87% <100.00%> (-0.07%) ⬇️
... and 125 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bb5d87c...d8b24f3. Read the comment docs.

@coiby coiby force-pushed the sync_terminal_cwd branch from 991d775 to 65d91d7 Compare December 23, 2020 11:28
@brammool
Copy link
Copy Markdown
Contributor

I do not see a change to the help files - what will the user experience?

Changing directories can have side effects. Perhaps it should be possible to disable this feature?
Or it should be disabled by default and only used when the user wants it?

@coiby coiby requested a review from cecamp as a code owner December 24, 2020 06:59
@coiby
Copy link
Copy Markdown
Author

coiby commented Dec 24, 2020

I do not see a change to the help files - what will the user experience?

Changing directories can have side effects. Perhaps it should be possible to disable this feature?
Or it should be disabled by default and only used when the user wants it?

Thank you for the suggestion! Change applied.

@coiby
Copy link
Copy Markdown
Author

coiby commented Feb 3, 2021

@brammool glib provides g_filename_from_uri to convert a file URI to a path which is exactly what I need. Since vim (vim-tiny) isn't supposed to depend on glib, now I have two choices,

  • borrow the code of g_filename_from_uri (~100 lines) from glib
  • only enable this feature for glib

Which one do you prefer?

@brammool
Copy link
Copy Markdown
Contributor

brammool commented Feb 3, 2021 via email

@coiby
Copy link
Copy Markdown
Author

coiby commented Feb 3, 2021 via email

@coiby coiby force-pushed the sync_terminal_cwd branch 4 times, most recently from 0d5668e to 8f17e2e Compare February 28, 2021 14:35
@coiby
Copy link
Copy Markdown
Author

coiby commented Feb 28, 2021

@brammool It turns out we needn't to deal with all the corner cases listed in http://web.mit.edu/barnowl/src/glib/glib-2.16.3/tests/uri-test.c since we can assume the emitted OSC 7 escape sequence is correctly uri-encoded. If there's something wrong, whoever emits the OSC 7 escape sequence should take the responsibility. So the license issues have been resolved.

I've also fixed the test failure for FreeBSD. But there is one failed test on macos (huge, clang). Unfortunately, I don't have a Mac system to test on.

@brammool
Copy link
Copy Markdown
Contributor

brammool commented Mar 2, 2021

The name 'autochdirsh' is hard to read. How about making it 'autoshelldir' ?
Please use the Vim usual C code formatting. E.g. "} else {" is written in three lines.

"char buffer[3] = {src[i+1], src[i+2], 0};" I'm not sure all C90 compilers accept this.
You can use the hexhex2nr() function.

syn_cwd() should be renamed to sync_shell_dir().

The test should probably CheckNotBSD now.

@coiby
Copy link
Copy Markdown
Author

coiby commented Mar 7, 2021 via email

@brammool
Copy link
Copy Markdown
Contributor

brammool commented Mar 7, 2021

For the change in parse_osc(), we should perhaps always recognize the code, but ignore it when p_asd is not set.
Or is the code dropped anyway when we don't recognize it?

@coiby
Copy link
Copy Markdown
Author

coiby commented Mar 10, 2021

For the change in parse_osc(), we should perhaps always recognize the code, but ignore it when p_asd is not set.
Or is the code dropped anyway when we don't recognize it?

Thank you for spotting this issue. Yes, to be consistent with OSC 52, it seems we should return 1 as long as osc 7 is recognized regardless of p_asd being set or not.

But at least right now, what's returned doesn't matter because string_fragment doesn't check what's returned from on_osc according to

(*vt->parser.callbacks->osc)(vt->parser.v.osc.command, frag, vt->parser.cbdata);

static void string_fragment(VTerm *vt, const char *str, size_t len, int final)
{
  ...
  switch(vt->parser.state) {
    case OSC:
      if(vt->parser.callbacks && vt->parser.callbacks->osc)
        (*vt->parser.callbacks->osc)(vt->parser.v.osc.command, frag, vt->parser.cbdata);
      break;

@brammool
Copy link
Copy Markdown
Contributor

You need to adjust the #ifdef for hexhex2nr(). See test failures.

@coiby coiby force-pushed the sync_terminal_cwd branch from 7f3f12a to da751fc Compare March 13, 2021 12:37
coiby added 2 commits March 27, 2021 09:21
When there is pwd change, the terminal could be notified of the
change via `OSC 7;` command. Use this feature to chdir buffer's
cwd to the shell's pwd.
pwd

The user can choose whether to set cwd to the shell's pwd automatically
when FEAT_AUTOSHELLDIR is set. FEAT_AUTOSHELLDIR will be set when
FEAT_TERMINAL is set.

Don't set cwd to the shell's pwd automatically by default.
@coiby coiby force-pushed the sync_terminal_cwd branch 2 times, most recently from 8730853 to 71ee4cc Compare March 27, 2021 01:39
@coiby
Copy link
Copy Markdown
Author

coiby commented Mar 27, 2021

You need to adjust the #ifdef for hexhex2nr(). See test failures.

Thanks. I have used FEAT_AUTOSHELLDIR to enable hexhex2nr to fix this kind of test failures.

commit 4355894 ("patch 8.2.2627: no need to check for BSD after checking for not root") removed CheckNotBSD. What can I do to skip the tests for BSD (CheckNotRoot doesn't work)?

@brammool
Copy link
Copy Markdown
Contributor

brammool commented Mar 27, 2021 via email

@coiby coiby force-pushed the sync_terminal_cwd branch 3 times, most recently from e1d9089 to 548d970 Compare March 29, 2021 14:15
@coiby coiby force-pushed the sync_terminal_cwd branch from 548d970 to d8b24f3 Compare March 29, 2021 14:27
@brammool
Copy link
Copy Markdown
Contributor

Looks OK now. We don't need a feature for this, just check that the 'autoshelldir' option works.

@brammool brammool closed this in 8b9abfd Mar 29, 2021
@Shane-XB-Qian

This comment was marked as off-topic.

@Shane-XB-Qian

This comment was marked as off-topic.

@coiby
Copy link
Copy Markdown
Author

coiby commented Mar 31, 2021

8b9abfd#r48888218
// should not recover to default value later?

Do you mean there should be an "set noacd" after finishing the test?

@coiby
Copy link
Copy Markdown
Author

coiby commented Mar 31, 2021

not sure (or not understand) the user case of this change too much,
but the doc of this change mentioned to source '/etc/profile.d/vte.sh' (ubuntu20.04 is vte-2.91.sh), BTW: be careful to do this, this looks had bug or flaw, PROMPT_COMMAND="__vte_prompt_command" may overwrite your previous 'PROMOT_COMMAND', and if it was not login shell then maynot find '__vte_prompt_command', note the order or timing of/to 'source'......

Thanks! It seems I should have mentioned this issue in the doc which according to https://gitlab.gnome.org/GNOME/vte/-/issues/37 is still not resolved.

@coiby
Copy link
Copy Markdown
Author

coiby commented Mar 31, 2021

Looks OK now. We don't need a feature for this, just check that the 'autoshelldir' option works.

Thanks! I'm not sure I understand you correctly. Do you mean FEAT_AUTOSHELLDIR is not needed?

glacambre added a commit to glacambre/neovim that referenced this pull request Apr 2, 2022
…t followed

Problem:    Directory change in a terminal window shell is not followed.
Solution:   Add the 'autoshelldir' option. (closes vim/vim#6290)
vim/vim@8b9abfd
glacambre added a commit to glacambre/neovim that referenced this pull request Apr 2, 2022
…t followed

Problem:    Directory change in a terminal window shell is not followed.
Solution:   Add the 'autoshelldir' option. (closes vim/vim#6290)
vim/vim@8b9abfd
glacambre added a commit to glacambre/neovim that referenced this pull request Apr 2, 2022
…t followed

Problem:    Directory change in a terminal window shell is not followed.
Solution:   Add the 'autoshelldir' option. (closes vim/vim#6290)
vim/vim@8b9abfd
glacambre added a commit to glacambre/neovim that referenced this pull request Apr 2, 2022
…t followed

Problem:    Directory change in a terminal window shell is not followed.
Solution:   Add the 'autoshelldir' option. (closes vim/vim#6290)
vim/vim@8b9abfd
glacambre added a commit to glacambre/neovim that referenced this pull request Apr 2, 2022
…t followed

Problem:    Directory change in a terminal window shell is not followed.
Solution:   Add the 'autoshelldir' option. (closes vim/vim#6290)
vim/vim@8b9abfd
glacambre added a commit to glacambre/neovim that referenced this pull request Apr 2, 2022
…t followed

Problem:    Directory change in a terminal window shell is not followed.
Solution:   Add the 'autoshelldir' option. (closes vim/vim#6290)
vim/vim@8b9abfd

Note that Vim's libvterm has a slightly different API - in particular,
the vterm fallbacks take a VTermStringFragment instead of a char*+len,
which means the parsing of OSC sequences had to be tweaked a little bit.
zeertzjq pushed a commit to glacambre/neovim that referenced this pull request Apr 2, 2022
…t followed

Problem:    Directory change in a terminal window shell is not followed.
Solution:   Add the 'autoshelldir' option. (closes vim/vim#6290)
vim/vim@8b9abfd

Note that Vim's libvterm has a slightly different API - in particular,
the vterm fallbacks take a VTermStringFragment instead of a char*+len,
which means the parsing of OSC sequences had to be tweaked a little bit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants