Skip to content
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

[konsole] Doesn't work with semantic integration (mouse click to position cursor) in KDE's Konsole #391

Closed
mcarans opened this issue Jan 25, 2024 · 14 comments
Labels
bug compatibility External Problem/Bug Problems/Bugs of other projects

Comments

@mcarans
Copy link

mcarans commented Jan 25, 2024

ble version: 29cd8f1
Bash version: 5.2.15(1)-release (x86_64-pc-linux-gnu)

Thanks for a great project!

KDE's Konsole supports semantic integration. This allows clicking the left mouse button to position the cursor in Bash. I am not able to get this to work with ble.sh unfortunately.

It is activated by adding this to .bashrc:

if [[ ! $PS1 =~ 133 ]] ; then
  PS1='\[\e]133;L\a\]\[\e]133;D;$?\]\[\e]133;A\a\]'$PS1'\[\e]133;B\a\]' ;
  PS2='\[\e]133;A\a\]'$PS2'\[\e]133;B\a\]' ;
  PS0='\[\e]133;C\a\]' ; 
fi

In the Konsole, you press Ctrl+Alt+I then search for "semantic integration" and put what appears there (the lines above) in your .bashrc to make it permanent.

I found that although those lines executed manually in bash in Konsole worked fine temporarily, they would not work in .bashrc, but I got it working by adding these lines at the end of my .bashrc instead:

if [[ ! $PS1 =~ 133 ]] ; then
    source ~/.local/share/DomTerm/bash-preexec.sh
    source ~/.local/share/DomTerm/shell-integration.bash
fi

bash-preexec.sh and shell-integration.bash are copied from another terminal called DomTerm (the first I'm aware of to offer click to position cursor) into ~/.local/share/DomTerm. Those files can be found here. cc @PerBothner: I think you may be interested in this topic.

I have click to position cursor working with Konsole and bash-it. I'd love to get it working with ble.sh as well. An increasing number of terminal emulators offer click to position cursor in bash (Konsole, Domterm, Kitty are the ones I've come across).

@mcarans mcarans changed the title Doesn't work with semantic integration (mouse click to position cursor) in KDE's console Doesn't work with semantic integration (mouse click to position cursor) in KDE's Konsole Jan 25, 2024
@akinomyoga
Copy link
Owner

KDE's Konsole supports semantic integration. This allows clicking the left mouse button to position the cursor in Bash. I am not able to get this to work with ble.sh, unfortunately.

Thank you for the report. I tried to see the problem, but so far I couldn't have managed to make it work even without ble.sh.

Testing with plain Bash (without ble.sh)

I tried it with Konsole 23.08.4 on Fedora 39 Server. The mentioned script was inserted into the prompt when I press Ctrl-Alt-]. After running the script by hitting Enter, I start to see some red and blue bars on the left edge of the terminal display. The red bar seems to indicate the prompt and output of the failed command. The blue bar seems to show the recently added terminal lines. However, clicking on the command line doesn't seem to move the cursor position. Also, Ctrl-Shift-PageUp and Ctrl-Shift-PageDown mentioned in the link you provided doesn't seem to work properly; it somehow scrolls the terminal buffer, but the destination of the scroll seems to be random and doesn't seem to move between the prompts.

I also tried DomTerm's shell-integration.bash (though I'm not sure if it is totally compatible with Konsole's implementation) with bash-preexec.sh. However, it still doesn't seem to work. The behavior is similar to the shell integration inserted by Ctrl-Alt-].

Ah, OK. It turned out that I needed to enable the mouse click feature in [Settings]-[Profiles]-[General]-[Semantic Integration].

@akinomyoga
Copy link
Owner

Ah, OK. It turned out that I needed to enable the mouse click feature in [Settings]-[Profiles]-[General]-[Semantic Integration].

With this setting in Konsole, I don't see any problem both with and without ble.sh. It seems to work in every 3x2 case of (plain Bash, ble.sh emacs mode, ble.sh vi mode) x (Konsole settings in bashrc, DomTerm shell-integration in bashrc).

I found that although those lines executed manually in bash in Konsole worked fine temporarily, they would not work in .bashrc,

It doesn't seem to reproduce in my environment. When I put those lines (suggested by Konsole's Ctrl-Alt-]) in ~/.bashrc, the mouse click properly moves the cursor position to the desired location both in the plain Bash and in Bash with ble.sh.

Since the reported behavior without ble.sh seems already strange, I suspect some of the custom settings in your ~/.bashrc interfere with the shell-integration settings.

@mcarans

  • Q1: Could you check if the problem reproduces with the minimal settings of only the shell integration code in ~/.bashrc?
  • Q2: Could you check if the problem reproduces with the minimal settings of only the shell integration plus ble.sh in ~/.bashrc?

@mcarans
Copy link
Author

mcarans commented Jan 29, 2024

@akinomyoga It's a bit odd, but if I have only the semantic integration code (no ble.sh) in my .bashrc, mouse click to position cursor doesn't work. If I add an echo then it works eg.

if [[ ! $PS1 =~ 133 ]] ; then
    echo ""
    PS1='\[\e]133;L\a\]\[\e]133;D;$?\]\[\e]133;A\a\]'$PS1'\[\e]133;B\a\]' ;
    PS2='\[\e]133;A\a\]'$PS2'\[\e]133;B\a\]' ;
    PS0='\[\e]133;C\a\]' ; fi

Do you need any echo or other lines before the semantic integration code in your .bashrc (without ble.sh)?

I'm running Kubuntu 23.10.

@akinomyoga
Copy link
Owner

Do you need any echo or other lines before the semantic integration code in your .bashrc (without ble.sh)?

OK, interesting. I can reproduce this behavior. Indeed, the mouse click doesn't work for the first command unless we specify echo "". I'll take a closer look at it.

@akinomyoga
Copy link
Owner

I think this is a bug of Konsole.


When I click somewhere in the terminal display, a Qt signal for the mouse click is emitted on TerminalDisplay::mousePressEvent [1]. Then the Qt signal is received by Vt102Emulation::sendMouseEvent [2].

The problem is that, when somewhere on the first line is clicked, Vt102Emulation::sendMouseEvent receives the y coordinate cy = 0 but rejects the event by the range check [2] at the beginning. In fact, the description of Vt102Emulation::sendMouseEvent says that cx and cy are 1-based [3], which means that the first line should be specified by cy = 1 instead of cy = 0. I also checked what happens when I click the second line on the terminal display and found that Vt102Emulation::sendMouseEvent receives cy = 1. This means that the received coordinates are shifted by 1. I think cx is also shifted by 1 because cx = 0 is received when I click the leftmost column.

Then, I noticed that the other lines calling the mouse signal add 1 to its arguments [4], but only the mouse signal for the semantic mouse click lacks the addition of 1. Therefore, I think we should add 1 to the second and third arguments on line 1231 [1].


The code was introduced in Konsole by commit ccd56f20 (MR !702) [5,6].

I quickly checked if there are any existing reports or patches [7-9] but couldn't find any if I didn't miss it. It's a bit surprising that no one has ever noticed it until now. The report [10] is possibly caused by the same issue of shifted cx, but I'm not sure.

@akinomyoga
Copy link
Owner

I've created a fix at Ref. [1]:

I'm going to test this. Probably we would need adjustments in the cursor movement calculations at the receiver side.

@akinomyoga
Copy link
Owner

I submitted MR !951 to the upstream Konsole. The next release cycle of KDE is 2024-02, so hopefully it will be fixed in the release next month. If it's too late, I think it would be delayed until the 2024-08 release, unfortunately.

@mcarans
Copy link
Author

mcarans commented Jan 30, 2024

Thank you for looking into that and even more for making the effort to submit a fix to Konsole.

In the meantime, I managed to get semantic integration, Bash-It and ble.sh working by adding the following after the stuff Bash-It automatically added to my .bashrc (and not using the Bash-it ble.sh plugin):

...
source "$BASH_IT"/bash_it.sh

if [[ ! $PS1 =~ 133 ]] ; then
  source ~/.local/share/konsole/bash-preexec.sh
  source ~/.local/share/konsole/shell-integration.bash
  source ~/.local/share/blesh/ble.sh
fi

I was not able to get semantic integration to work using the recommended approach (with ble-attach etc.). I have no idea why.

@akinomyoga akinomyoga added compatibility External Problem/Bug Problems/Bugs of other projects labels Jan 30, 2024
@akinomyoga
Copy link
Owner

I was not able to get semantic integration to work using the recommended approach (with ble-attach etc.). I have no idea why.

Could you give me an example of a case where the semantic integration does not work? I tried many patterns, but I couldn't find a case. In all the cases I tried, the semantic integration works (except on the first line where Konsole has a bug).

Basically, the recommended ordering is sourcing ble.sh at the beginning of bashrc and perform ble-attach at the last line of bashrc. The following is an example without Bash-it (where I omitted the checks for $- and ${BLE_VERSION-}). I tried this in Konsole, but the semantic integration works as expected.

# bashrc

source /path/to/ble.sh/out/ble.sh --norc --attach=none
source /path/to/rcaloras/bash-preexec/bash-preexec.sh
source /path/to/PerBothner/DomTerm/tools/shell-integration.bash
ble-attach

I also tried the following with Bash-it, but the semantic integration still works.

# bashrc

# Settings for Bash-it
BASH_IT=/path/to/Bash-it/bash-it
export BASH_IT_THEME='bobby'
export GIT_HOSTING='[email protected]'
unset MAILCHECK
export IRC_CLIENT='irssi'
export TODO="t"
export SCM_CHECK=true

source /path/to/ble.sh/out/ble.sh --norc --attach=none
source /path/to/Bash-it/bash-it/bash_it.sh
source /path/to/rcaloras/bash-preexec/bash-preexec.sh
source /path/to/PerBothner/DomTerm/tools/shell-integration.bash
ble-attach

The version of bash-preexec bundled with DomTerm seems to be old, so I also tried the bash-preexec bundled with DomTerm as follows, but the result is unchanged. The semantic integration is still working.

# bashrc

source /path/to/ble.sh/out/ble.sh --norc --attach=none
source /path/to/PerBothner/DomTerm/tools/bash-preexec.sh
source /path/to/PerBothner/DomTerm/tools/shell-integration.bash
ble-attach

I also tried the blesh plugin of Bash-it, but the semantic integration still works:

# bashrc

# Settings for Bash-it
BASH_IT=/path/to/Bash-it/bash-it
export BASH_IT_THEME='bobby'
export GIT_HOSTING='[email protected]'
unset MAILCHECK
export IRC_CLIENT='irssi'
export TODO="t"
export SCM_CHECK=true

# Note: blesh plugin is enabled by "bash-it enable plugin blesh"
source /path/to/Bash-it/bash-it/bash_it.sh
source /path/to/rcaloras/bash-preexec/bash-preexec.sh
source /path/to/PerBothner/DomTerm/tools/shell-integration.bash
ble-attach

I checked other ordering where ble.sh is not sourced at the beginning with and without Bash-it, but both cases work for the semantic integration.

# bashrc
source /path/to/rcaloras/bash-preexec/bash-preexec.sh
source /path/to/PerBothner/DomTerm/tools/shell-integration.bash
source /path/to/ble.sh/out/ble.sh --norc --attach=none
ble-attach
# bashrc

# Settings for Bash-it
BASH_IT=/path/to/Bash-it/bash-it
export BASH_IT_THEME='bobby'
export GIT_HOSTING='[email protected]'
unset MAILCHECK
export IRC_CLIENT='irssi'
export TODO="t"
export SCM_CHECK=true

source /path/to/Bash-it/bash-it/bash_it.sh
source /path/to/ble.sh/out/ble.sh --norc --attach=none
source /path/to/rcaloras/bash-preexec/bash-preexec.sh
source /path/to/PerBothner/DomTerm/tools/shell-integration.bash
ble-attach

On the other hand, ble-attach needs to be at the end of bashrc (or at least strictly the last command that changes the shell settings and outputs something to stdout/stderr).

@mcarans
Copy link
Author

mcarans commented Jan 31, 2024

I have tested more. I've removed all the stuff Kubuntu put in my .bashrc and simplified as a starting point.

export BASH_IT_THEME='brainy'
export SCM_CHECK=true
export THEME_SHOW_PYTHON=true
source ~/.bash_it/bash_it.sh
source ~/.local/share/konsole/bash-preexec.sh
source ~/.local/share/konsole/shell-integration.bash
source ~/.local/share/blesh/ble.sh

That works perfectly.

export BASH_IT_THEME='brainy'
export SCM_CHECK=true
export THEME_SHOW_PYTHON=true
#source ~/.bash_it/bash_it.sh
source ~/.local/share/konsole/bash-preexec.sh
source ~/.local/share/konsole/shell-integration.bash
source ~/.local/share/blesh/ble.sh

No bash it. Only works after I press Enter ie. not working on first line.

export BASH_IT_THEME='brainy'
export SCM_CHECK=true
export THEME_SHOW_PYTHON=true
#source ~/.bash_it/bash_it.sh
source ~/.local/share/konsole/bash-preexec.sh
source ~/.local/share/konsole/shell-integration.bash
#source ~/.local/share/blesh/ble.sh

No bash it or blesh. Only works after I press Enter ie. not working on first line.

source ~/.local/share/blesh/ble.sh --norc --attach=none
export BASH_IT_THEME='brainy'
export SCM_CHECK=true
export THEME_SHOW_PYTHON=true
source ~/.bash_it/bash_it.sh
source ~/.local/share/konsole/bash-preexec.sh
source ~/.local/share/konsole/shell-integration.bash
ble-attach

Like 1, but launching ble.sh the proper way. Works on first line, but not on subsequent lines after pressing Enter.

source ~/.local/share/blesh/ble.sh --norc --attach=none
export BASH_IT_THEME='brainy'
export SCM_CHECK=true
export THEME_SHOW_PYTHON=true
#source ~/.bash_it/bash_it.sh
source ~/.local/share/konsole/bash-preexec.sh
source ~/.local/share/konsole/shell-integration.bash
ble-attach

Like 2 (no bash it), but launching ble.sh the proper way. Only works after I press Enter ie. not working on first line.

@akinomyoga
Copy link
Owner

Thank you. OK, I can reproduce the problem in case 4. I haven't checked the behavior in the second or later prompt. I'll take a look.

For the other cases, case 1 is just fine. The behaviors in cases 2, 3, and 5 should be fixed by MR !951 that I submitted to Konsole.

@akinomyoga
Copy link
Owner

I fixed it in commit 50af4d9. Could you update ble.sh by running ble-update and check the behavior?

Note that the issue that the sematic click doesn't work on the first line is not solved at the ble.sh's side. If you want to use the sematic click on the first line, you may pick a patch at MR !951 and build Konsole by yourself.

Here's the summary of the issue: Actually, ble.sh has an integration with bash-preexec, which was originally written two years before, but that integration was broken after the two years. There were mainly two causes: one was a change in the upstream bash-preexec, which is ironically submitted by me. The other was the change of the treatment of PS1 and PROMPT_COMMAND in the internal state of ble.sh. I re-adjusted the integration and related hooks in commit 50af4d9.

@mcarans
Copy link
Author

mcarans commented Jan 31, 2024

I can confirm that option 4 works perfectly now like option 1. It works on the first line and on subsequent lines. Thanks so much for looking into this!

@akinomyoga
Copy link
Owner

Thanks for the confirmation! I'm closing the issue now. Thank you again for raising the issue.

@akinomyoga akinomyoga added the bug label Jan 31, 2024
@akinomyoga akinomyoga changed the title Doesn't work with semantic integration (mouse click to position cursor) in KDE's Konsole [konsole semantic integration] Doesn't work with semantic integration (mouse click to position cursor) in KDE's Konsole Jan 31, 2024
@akinomyoga akinomyoga changed the title [konsole semantic integration] Doesn't work with semantic integration (mouse click to position cursor) in KDE's Konsole [konsole] Doesn't work with semantic integration (mouse click to position cursor) in KDE's Konsole Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug compatibility External Problem/Bug Problems/Bugs of other projects
Projects
None yet
Development

No branches or pull requests

2 participants