Commit Graph

65 Commits

Author SHA1 Message Date
Johannes Altmanninger
24d719bf13 Fix off-by-two error in max size of frameless infoboxes
Framed info boxes need one cell for the border and one for inner
space padding.  That's 4 extra columns when counting both sides.
Frameless boxes have neither border nor padding so 0 columns here.

Closes #5106
2024-03-15 21:54:59 +11:00
Chris Webb
a8d5b8bd2c Fix compiler warnings when char is unsigned
In several places, we check for a control character with something like

  char c;
  [...]
  if (c >= 0 and c <= 0x1F)
    [...]

When char is signed (e.g. amd64) this is fine, but when char is unsigned
by default (e.g. arm32 and arm64) this generates warnings about the
tautologous check that an unsigned value is non-negative.

Write as

  if ((unsigned char) c <= 0x1F)
    [...]

which is both correct and not suspicious under both conventions.
2023-12-10 11:09:55 +00:00
Chris Webb
32680e5d65 Skip output synchronization query when explicitly disabled
Some terminals misbehave when queried for output synchronization support,
such as Windows Terminal as reported in

  https://github.com/mawww/kakoune/issues/5032

The relatively long response from a terminal which does support output-sync
is also prone to getting torn over a slow link such as a serial console,
causing stray input to the editor.

In ui_options, the terminal_synchronized option controls the use of this
feature, but unfortunately the query is unconditionally sent at startup
even when this is set false.

Skip the query at startup when terminal_synchronized is explicitly false.

We query at most once per terminal in set_ui_options so the behaviour
is correct both when kakoune is started with terminal_synchronized unset
and when it is started with terminal_synchronized set false but this is
later unset.
2023-11-24 12:55:45 +00:00
Maxime Coste
2fa55be40a Default comparison operators that can be 2023-10-25 21:06:52 +11:00
Maxime Coste
96884193dd Remove redundant comparison operators
Since C++20 (a != b) get automatically rewritten as !(a == b) if
the != operator does not exist.
2023-10-25 20:40:04 +11:00
Maxime Coste
3bf16c0fb1 Merge remote-tracking branch 'krobelus/foot-custom-keypad-sequences' 2023-09-23 21:31:01 +10:00
Maxime Coste
6990270005 Still inkorrect inglish
Hopefully thats better now
2023-08-31 04:56:50 +10:00
Maxime Coste
a212fd25a0 Fix incorrect inglish 2023-08-29 03:24:27 +10:00
Maxime Coste
e4d7b884cd Cleanup SIGHUP handling and forking server to background
Ensure we ignore SIGHUP once the TerminalUI is gone as it will be
sent again on fork, fix the parent process terminating due to trying
to write to stdout after it was closed.

Fixes #4960
2023-08-27 08:47:33 +10:00
Johannes Altmanninger
84ea52e46e Support CSI u numpad keys
Normally page-down is sent as \033[6~ but some terminals
send a CSI u encoding for the page-down on the numpad. See
https://codeberg.org/dnkl/foot#keypad for example.

Treat them as the underlying key; we could add a modifier if anyone
cares about the distinction.
2023-06-24 22:18:49 +02:00
Adrià Arrufat
66b22a1902 Remove unneeded this capture in lambda
Clang 15 was complaining about this.
2023-06-09 21:47:49 +09:00
Johannes Altmanninger
cf94b310aa Fix crash after multiple terminal resizes
When Kakoune's terminal is shown on my laptop monitor and I plug
in my external monitor, the terminal's workspace will move to that
external monitor. When this happens, Kakoune may segfault.
There are multiple resize events (SIGWINCH) in quick succession;
it crashes because we handle SIGWINCH during rendering.

The problem happens during execution of "TerminalUI::Screen::output"
(frame #18). When we receive SIGWINCH while writing to stdout, write(2)
fails with EAGAIN, prompting us to handle pending events (See ae001a1f9
(Run EventManager whenever writing to a file descriptor would block,
2022-05-10)).  We update the screen size in check_resize() here:

	#4  Kakoune::TerminalUI::check_resize (force=<optimized out>) at terminal_ui.cc:683
	#5  Kakoune::TerminalUI::get_next_key () at terminal_ui.cc:719
	#6  operator() (__closure=0x555555984198) at terminal_ui.cc:484
	#7  std::__invoke_impl<void, Kakoune::TerminalUI::TerminalUI()::<lambda(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode)>&, Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode> (__f=...) at /usr/include/c++/12.2.1/bits/invoke.h:61
	#8  std::__invoke_r<void, Kakoune::TerminalUI::TerminalUI()::<lambda(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode)>&, Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode> (__fn=...) at /usr/include/c++/12.2.1/bits/invoke.h:111
	#9  std::_Function_handler<void(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode), Kakoune::TerminalUI::TerminalUI()::<lambda(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode)> >::_M_invoke(const std::_Any_data &, Kakoune::FDWatcher &, Kakoune::FdEvents &&, Kakoune::EventMode &&) (__functor=..., __args#0=..., __args#1=<optimized out>, __args#2=<optimized out>) at /usr/include/c++/12.2.1/bits/std_function.h:290
	#10 std::function<void (Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode)>::operator()(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode) const (__args#2=<optimized out>, __args#1=<optimized out>,  __args#0=...) at /usr/include/c++/12.2.1/bits/std_function.h:591
	#11 Kakoune::FDWatcher::run (mode=Kakoune::EventMode::Urgent, events=<optimized out>) at event_manager.cc:28
	#12 Kakoune::EventManager::handle_next_events (mode=mode@entry=Kakoune::EventMode::Urgent, sigmask=sigmask@entry=0x0, block=<optimized out>, block@entry=false) at event_manager.cc:143
	#13 Kakoune::write (fd=1, data=...) at file.cc:273
	#14 Kakoune::BufferedWriter<4096>::flush () at string.hh:236
	#15 Kakoune::BufferedWriter<4096>::write (data="t file.hh:145
	#16 Kakoune::TerminalUI::Screen::set_face (face=..., writer=...) at terminal_ui.cc:255
	#17 operator() (line=..., __closure=<synthetic pointer>) at terminal_ui.cc:326
	#18 Kakoune::TerminalUI::Screen::output (force=force@entry=true, synchronized=<optimized out>, writer=...) at terminal_ui.cc:402
	#19 Kakoune::TerminalUI::redraw (force=force@entry=true) at terminal_ui.cc:571
	#20 Kakoune::TerminalUI::refresh (force=<optimized out>) at terminal_ui.cc:592
	#21 Kakoune::Client::redraw_ifn () at client.cc:282
	#22 Kakoune::ClientManager::redraw_clients () at client_manager.cc:232
	#23 Kakoune::run_server (session=..., server_init=..., client_init=..., init_buffer="fish-rust/src/ast.rs", init_coord=..., flags=Kakoune::ServerFlags::None, ui_type=Kakoune::UIType::Terminal,
	    debug_flags=<optimized out>, files=ArrayView<Kakoune::StringView> = {...}) at main.cc:893
	#24 main (argc=<optimized out>, argv=<optimized out>) at main.cc:1243

Thereafter, "TerminalUI::Screen::output" resumes and crashes due to
a buffer overflow in "lines" which has been resized.
2023-04-24 18:31:05 +02:00
Maxime Coste
1322abef64 Convert \r to \n in bracketed pastes
It seems many terminals emits \r for newlines in bracketed pastes,
manually convert this.
2023-03-14 04:48:11 +11:00
Maxime Coste
6548846950 Slight refactoring of bracketed paste feature
Handle begin/end paste directly in paste csi, manage paste buffer
out of get_char, filter Key::Invalid earlier.

get_next_key returning Key::Invalid means there was some input but
it could not be represented as a Key. An empty optional means there
was no input at all.
2023-03-13 20:55:31 +11:00
Johannes Altmanninger
b2cf74bb4a Implement bracketed paste
Text pasted into Kakoune's normal mode is interpreted as command
sequence, which is probably never what the user wants. Text
pasted during insert mode will be inserted fine but may trigger
auto-indentation hooks which is likely not what users want.

Bracketed paste is pair of escape codes sent by terminals that allow
applications to distinguish between pasted text and typed text.

Let's use this feature to always insert pasted text verbatim, skipping
keymap lookup and the InsertChar hook. In future, we could add a
dedicated Paste hook.

We need to make a decision on whether to paste before or after the
selection. I chose "before" because that's what I'm used to.

TerminalUI::set_on_key has

	EventManager::instance().force_signal(0);

I'm not sure if we want the same for TerminalUI::set_on_paste?
I assume it doesn't matter because they are always called in tandem.

Closes #2465
2023-03-11 16:21:57 +01:00
Johannes Altmanninger
ad36585b7a Make TerminalUI::get_next_key() helpers static
The only depend on the TerminalUI object which is a singleton, so we
can make them all static.
2023-03-11 16:21:57 +01:00
Ameer Ghani
6acc18373d Add option to set maximum info box width
Some plugins (*cough* kak-lsp) and help texts tend to have immensely long content
in a single line. This generates info boxes that span the entire terminal width.
This is made especially worse on widescreen monitors or at small text size.

This grants user control over how wide these boxes are.

I deliberately avoid pushing this change to `kak-lsp` because it's not the only
plugin that this could help--see the `hook` help text for an example of this
problem in vanilla Kakoune. I would also suggest that since this is a rendering
concern, it be handled by the terminal rendering logic.
2023-02-15 21:17:22 -05:00
Maxime Coste
36d1713b09 Fix a couple bugs with underline highlighting
Add missing curly_underline attribute to json-rpc
Fix underline color not correct after attrbute only change
2022-12-13 12:14:29 +11:00
Adrià Arrufat
f50ee5bb5a Fix crash when trying to display the menu in a tiny window 2022-09-17 00:51:34 +09:00
Maxime Coste
658b6b0f1a Make space a named key to correctly handle shift modifier 2021-12-11 08:12:08 +11:00
Jason Felice
a70dbda54c use shifted key codes on kitty 2021-11-02 08:26:39 -04:00
Maxime Coste
0f688656a4 Fix DECRPM parsing for 2026
As discovered in #4320 the previous code was buggy and would enable
synchronized output on any response.
2021-11-02 19:43:16 +11:00
Maxime Coste
874f3e0764 Fix terminal underline color not being properly reset 2021-10-29 07:51:29 +11:00
Maxime Coste
60165bacf9 Use DECRQM/DECRPM to detect support for synchronized output
Enable it if supported by default, let the user override it with
the existing terminal_synchronized ui option.

This should finalize work discussed on #4317
2021-10-23 09:42:31 +11:00
Maxime Coste
3acf85f267 Restore diff based terminal output optimization when synchronized
terminal_synchronized ui_option now also controls this behaviour,
update out of date documentation for ui_options as well.

As discussed in #4317
2021-09-26 11:03:50 +10:00
Maxime Coste
b3a1017a26 Remove scrolling detection/optimization in terminal output
Just validate if line changed or not. This should avoid flickering
on terminals such as the linux console that eagerly redraw on line
deletions. Unfortunately this means drawing will use more data and
might add a bit of latency on slow links.

Fixes #4317
Fixes #4320
2021-09-09 09:01:42 +10:00
Maxime Coste
3fc8e29d10 Add support for curly underline and separate underline color
Add support for a third color in face definition that controls
the underline and a 'c' attribute for curly underline (that takes
precedence over 'u' if both are specified)

Allow empty colors to mean default, so that `,,red+u` means the
same as `default,default,red+u`

Fixes #4138
2021-09-07 08:21:26 +10:00
Maxime Coste
5c6c58ae22 Remove unnecessary c_str() calls 2021-09-02 21:19:21 +10:00
Maxime Coste
b841f3a214 Remove terminal_wheel_up/down_button UI option
It seems nobody uses it, and it is not plugged through anyway.

Closes #2642
2021-09-02 20:46:35 +10:00
Maxime Coste
7187784936 Move control character escaping responsibility to the terminal UI
Fix atom text at display time, allow tabs/eol/etc... in display
atoms and escape them just-in-time

Fixes #4293
2021-08-17 20:40:30 +10:00
Maxime Coste
73da47258d Reduce memory usage and allocations in terminal output code
Store data in unique_ptr instead of vectors as we have fixed sizes
Do not allocate new hashes, recompute them on-demand
2021-08-05 11:52:37 +10:00
Maxime Coste
24c23af341 Remove unnecessary fflush call 2021-08-05 10:57:35 +10:00
Maxime Coste
7b126704c7 Fallback to 80x24 terminal size when ioctl fails or returns 0x0
Fixes #4286
2021-08-04 20:51:50 +10:00
Maxime Coste
1b7616b3fc Use a BufferedWriter to output in the terminal ui instead of stdio
Clearer control of buffering and flushing
2021-08-01 20:07:20 +10:00
Maxime Coste
63b67d0f31 Remove some unnecessary string copies in TerminalUI 2021-07-20 22:30:41 +10:00
Maxime Coste
c643cd4467 Fix blitting of windows halfway through double-width codepoint
Display a whitespace in place of the uncovered half of the codepoint.

(I know this is incorrect and we should be considering grapheme clusters
instead of codepoints, but this is a far bigger refactoring and another
can of worms to handle with terminal emulators).

Fixes #4262
2021-07-14 20:51:25 +10:00
Maxime Coste
ac6420ee8d Refactor TerminalUI::set_ui_options
Take advantage of Optional monadic operations
2021-07-12 10:25:59 +10:00
Maxime Coste
428ddeb97b Add 'terminal_synchronized' ui_option to opt-in synchronized output
Synchronized output does not work well with various terminals
(including the linux console). It should also be unnecessary when
not going through a slow link.

This will eventually be removed if it is not proven to be useful
to some users.
2021-07-12 10:25:59 +10:00
Jason Felice
5d497dc46e src: Support strikethrough faces 2021-07-12 10:25:59 +10:00
Maxime Coste
cbd0dc571b Fix crash when displaying a menu on a too small terminal
The code could position the menu at a negative line leading to
invalid array accesses.
2021-07-12 10:25:59 +10:00
Maxime Coste
76e5d11c8b Small cleanups in terminal ui 2021-07-12 10:25:59 +10:00
Maxime Coste
44deaeb4eb Do not trust saved active face when force redrawing 2021-07-12 10:25:59 +10:00
Maxime Coste
7bbc057cd7 Slight code simplification 2021-07-12 10:25:58 +10:00
Maxime Coste
b3025b87d0 Fix race on resize in terminal ui 2021-07-12 10:25:58 +10:00
Maxime Coste
86025f88c4 Fix force redraw handling
Fixes #3559
2021-07-12 10:25:58 +10:00
Maxime Coste
cd613da050 Tolerate bliting window that overflow the target
Although we try to avoid those, ensuring it never happens is tricky,
and tolerating them avoids the crash.

Fixes #3498
2021-07-12 10:25:58 +10:00
Kylie McClain
89e0b4b830 terminal_ui.cc: fix build on musl libc 2021-07-12 10:25:58 +10:00
Tim Allen
de065dad2d terminal_ui: restore termios settings before suspending Kakoune.
TerminalUI::suspend() is responsible for undoing all Kakoune's modifications to
the terminal state, actually suspending the process, then re-applying all the
modifications after Kakoune wakes back up.

Previously, the "undo" and "reapply" steps for termios settings were both after
the suspend point, so on some platforms they were incorrect when the user
arrived back at the shell prompt.

Now, the termios "undo" step is back before the suspend point, and the undo and
reapply steps should be in exactly reversed order.

Fixes #3488.
2021-07-12 10:25:58 +10:00
Tim Allen
e760f2df6b Use iTerm2 "synchronised update" sequences around redraws.
In terminals that support it, this sequence causes the terminal to not redraw
*its* output until the application has finished, reducing redraw flickering.

The sequence is defined in:

    https://gitlab.com/gnachman/iterm2/-/wikis/synchronized-updates-spec

...and is apparently also supported by kitty and libVTE.

Fixes #3482.
2021-07-12 10:25:58 +10:00
Maxime Coste
72271ce529 Handle skipped characters with erase line (EL) instead of insert blank (ICH)
insert blank seems to behave differently between terminals and would
be less efficient because it still has to shift all following characters
(that we will overwrite anyway).

Fixes #3437
2021-07-12 10:25:58 +10:00