Bug 445590

Summary: Konsole switches back to previous profile settings on vim newline
Product: [Applications] konsole Reporter: Cimbali <me>
Component: generalAssignee: Konsole Developer <konsole-devel>
Status: RESOLVED FIXED    
Severity: normal CC: a.samirh78
Priority: NOR    
Version: 21.08.3   
Target Milestone: ---   
Platform: openSUSE   
OS: Linux   
Latest Commit: Version Fixed In: 21.12
Sentry Crash Report:
Attachments: screen capture of bug

Description Cimbali 2021-11-16 13:44:09 UTC
Created attachment 143625 [details]
screen capture of bug

SUMMARY
Konsole profile settings changes (in particular colors) are undone by inserting a new line in vim, except in new tabs.


STEPS TO REPRODUCE
1. Have a tab with some activity (opening vim once is sufficient)
2. Change profile of said tab
3. Open vim and insert a new line

OBSERVED RESULT
Profile settings are changed back to initial tab settings

EXPECTED RESULT
Profile settings are unaffected by applications that run inside it

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: OpenSUSE Tumbleweed (rolling release, current version id 20211106) Kernel 5.14.14-1-default (64-bit)
(available in About System)
KDE Plasma Version: 5.32.2
KDE Frameworks Version: 5.87.0
Qt Version: 5.15.2
vim: 8.2 (from package vim-8.2.3408-1.4.x86_64, see below for full version info)

ADDITIONAL INFORMATION
- I’ve reported the current software versions, but I’ve been having this bug for a while.
- I’m also attaching a screen capture of the bug, with only `export PS1='> '` in the .bashrc.
- Below the full vim --version info.

----

VIM - Vi IMproved 8.2 (2019 Dec 12)
Included patches: 1-3408
Compiled by 'http://www.opensuse.org/'
Huge version without GUI.  Features included (+) or not (-):
+acl               +file_in_path      +mouse_urxvt       -tag_any_white
+arabic            +find_in_path      +mouse_xterm       -tcl
+autocmd           +float             +multi_byte        +termguicolors
+autochdir         +folding           +multi_lang        +terminal
-autoservername    -footer            -mzscheme          +terminfo
-balloon_eval      +fork()            +netbeans_intg     +termresponse
+balloon_eval_term +gettext           +num64             +textobjects
-browse            -hangul_input      +packages          +textprop
++builtin_terms    +iconv             +path_extra        +timers
+byte_offset       +insert_expand     +perl              +title
+channel           +ipv6              +persistent_undo   -toolbar
+cindent           +job               +popupwin          +user_commands
-clientserver      +jumplist          +postscript        +vartabs
-clipboard         +keymap            +printer           +vertsplit
+cmdline_compl     +lambda            +profile           +virtualedit
+cmdline_hist      +langmap           -python            +visual
+cmdline_info      +libcall           +python3/dyn       +visualextra
+comments          +linebreak         +quickfix          +viminfo
+conceal           +lispindent        +reltime           +vreplace
+cryptv            +listcmds          +rightleft         +wildignore
+cscope            +localmap          +ruby/dyn          +wildmenu
+cursorbind        +lua/dyn           +scrollbind        +windows
+cursorshape       +menu              +signs             +writebackup
+dialog_con        +mksession         +smartindent       -X11
+diff              +modify_fname      -sodium            -xfontset
+digraphs          +mouse             -sound             -xim
-dnd               -mouseshape        +spell             -xpm
-ebcdic            +mouse_dec         +startuptime       -xsmp
+emacs_tags        -mouse_gpm         +statusline        -xterm_clipboard
+eval              -mouse_jsbterm     -sun_workshop      -xterm_save
+ex_extra          +mouse_netterm     +syntax            
+extra_search      +mouse_sgr         +tag_binary        
-farsi             -mouse_sysmouse    -tag_old_static    
   system vimrc file: "/etc/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -O2 -Wall -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -Werror=return-type -flto=auto -g -Wall -pipe -fno-strict-aliasing -D_REENTRANT -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 
Linking: gcc -L. -flto=auto -fstack-protector-strong -rdynamic -Wl,-export-dynamic -Wl,-E -Wl,-rpath,/usr/lib/perl5/5.34.0/x86_64-linux-thread-multi/CORE -flto=auto -L/usr/local/lib -Wl,--as-needed -o vim -lm -lselinux -ltinfo -lacl -Wl,-E -Wl,-rpath,/usr/lib/perl5/5.34.0/x86_64-linux-thread-multi/CORE -L/usr/local/lib64 -fstack-protector-strong -L/usr/lib/perl5/5.34.0/x86_64-linux-thread-multi/CORE -lperl -lm -ldl -lcrypt -lpthread
Comment 1 Ahmad Samir 2021-11-16 14:48:01 UTC
I couldn't reproduce this issue.

Maybe try vim with any extra addons?
Comment 2 Cimbali 2021-11-16 15:42:29 UTC
This happens even with all plugins removed − but not with the vimrc removed. I’ll try and identify which option(s) are problematic and report back a minimal vimrc that reproduces the issue.
Comment 3 Cimbali 2021-11-16 17:04:55 UTC
I’ve found the problematic lines:

let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_SR = "\<Esc>]50;CursorShape=2\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"

The docs for there says:

	t_SI 	start insert mode (bar cursor shape)			t_SI
	t_SR	start replace mode (underline cursor shape)	t_SR
	t_EI 	end insert or replace mode (block cursor shape)	t_EI
		|termcap-cursor-shape|

These control the cursor shapes to avoid displaying block cursors in all modes (i.e. bar in insert mode, underline in replace mode, and block in normal mode). Related discussions:
- https://forum.kde.org/viewtopic.php?t=126004
- https://bugs.kde.org/show_bug.cgi?id=103399

So in fact vim is not needed to reproduce the bug, and it’s only selecting a CursorShape that reset profile options. Here’s alternate steps to reproduce:

STEPS TO REPRODUCE
1. Change cursor shape, e.g. with: printf '\e]50;CursorShape=1\a'
2. Change profile
3. Change cursor shape again, e.g. with: printf '\e]50;CursorShape=0\a'

OBSERVED RESULT
Profile settings (e.g. colours) are changed back to initial settings from step 1

EXPECTED RESULT
Profile settings are unaffected by changing cursor shape, only cursor shape should change.


----

Note that this doesn’t seem to happen with DECSCUSR codes, so a good workaround for my vim use case is to use instead:

	let &t_SI = "\<Esc>[6 q"
	let &t_SR = "\<Esc>[4 q"
	let &t_EI = "\<Esc>[2 q"
Comment 4 Cimbali 2021-11-16 17:25:24 UTC
In fact, changing the cursor shape with printf '\e]50;CursorShape=1\a' is changing a konsole profile setting, and  “forking” the current profile. The “Edit current profile” option of the tab shows which profile is currently in use.

Whether this forking is done or not before changing to a new profile, affects how the next cursor shape change behaves. It sets the cursor shape, with as a base (colours etc.) either:
- the newly loaded profile (if there were no cursor changes before loading the new profile)
- the previously forked profile (if there were cursor changes)

This “forked” profile should point to (or at least have all its values replaced by) the newly loaded profile, if the behaviour is to be consistent.
Comment 5 Bug Janitor Service 2021-11-18 09:25:39 UTC
A possibly relevant merge request was started @ https://invent.kde.org/utilities/konsole/-/merge_requests/526
Comment 6 Ahmad Samir 2021-11-18 09:25:54 UTC
Thanks for the detailed report and investigation.
Comment 7 Kurt Hindenburg 2021-11-21 03:30:06 UTC
Git commit 97feb543975f7ae2ed2ab2b90ce9bdd348e10bed by Kurt Hindenburg, on behalf of Ahmad Samir.
Committed on 21/11/2021 at 03:24.
Pushed by hindenburg into branch 'master'.

Handle changing cursor shape property without creating a new profile

Using an escape sequence, e.g. printf '\e]50;CursorShape=1\a', to change
the cursor shape property like we handle DECSCUSR, i.e. as a transient
change without creating a new profile (see
SessionManager::sessionProfileCommandReceived()).

To test:
- printf '\e]50;CursorShape=1\a' to change the cursor shape, check that the
  current profile hasn't been switched to a new profile (that has no name
  as it's a temp profile)
FIXED-IN: 21.12

M  +13   -0    src/Vt102Emulation.cpp

https://invent.kde.org/utilities/konsole/commit/97feb543975f7ae2ed2ab2b90ce9bdd348e10bed