Commit Graph

5792 Commits

Author SHA1 Message Date
Maxime Coste
c45a1d435a small code cleanup 2023-11-11 18:46:15 +11:00
Maxime Coste
fbbced5ed0 Support building ArrayView from contigous iterators 2023-11-10 16:35:46 +11:00
Maxime Coste
feeacd8de9 Refactor spawn_shell to return the relevant FDs
This removes the need for the setup_child callback which is quite
tricky as it cannot touch any memory due to vfork, and removes the
Pipe abstraction in favor of a more general UniqueFd one.
2023-11-05 22:47:17 +11:00
Maxime Coste
25a00bf2a9 Remove ignored packed attribute and static_assert on Node size
This static_assert is not necessary for the code to work and is
not valid on every platform.
2023-11-05 12:38:39 +11:00
Maxime Coste
0880399fbe Replace snprintf with format_to 2023-11-05 12:30:54 +11:00
Johannes Altmanninger
b0ddbfc2df Do not poll command sockets while shell command is running
Accepter is a wrapper around a socket watcher. It always uses
EventMode::Urgent, so it will be included in pselect(2) (via
EventManager::handle_next_events()) even while we are waiting for a
(blocking) shell command.  However we will not execute the command
received on this socket until after the shell command is done.

This is implemented with an early return:

	void handle_available_input(EventMode mode)
	{
	    while (not m_reader.ready() and fd_readable(sock))
	        m_reader.read_available(sock);

	    if (mode != EventMode::Normal or not m_reader.ready())
	        return;

so we read available data but don't close the socket.
When using this reproducer

	{
		sleep 1 && echo 'nop' | kak -p session
	} &
	kak -n -s session -e '%sh{sleep 7}'

the first "m_reader.read_available(sock);" will read "nop".  Then
"m_reader.ready()" is true but the socket is still readable. This
means that pselect(2) will return it every time, without blocking.

This means that the shell manager runs a hot loop between pselect(2)
and waitpid(2).

Fix this problem demoting command socket watchers from
EventMode::Urgent. This means that we won't pselect(2) it when handling
only urgent events. Control-C still works, I'm not sure why.

Alternative fix: we could read the commands but then disable the
socket. I tried this but it seems too complex.

Closes #5014
2023-11-04 17:48:25 +01:00
Maxime Coste
6a39ac224b Merge remote-tracking branch 'krobelus/fix-quoted-vals-expand' 2023-11-03 21:51:36 +11:00
Maxime Coste
f7499ccf45 Add support for 0-padding in format and replace uses of sprintf 2023-11-03 20:27:41 +11:00
Johannes Altmanninger
8a3613e5a0 Fix "%val{selections_desc}" being joined by nul instead of space
This should fix a bug in lint.kak though I didn't check.
2023-11-03 06:45:02 +01:00
Maxime Coste
7577fa1b66 Use explicit target types for gather calls to bypass clang regression
Since clang-16 there has been a regression in the P0522R0 support.
(Bug report at https://github.com/llvm/llvm-project/issue/63281)

Closes #4892
2023-11-03 13:08:44 +11:00
Maxime Coste
c889c0329c Replace std::lexicographical_compare_three_way with custom code
On latest MacOS this function is still not implemented
2023-11-03 13:08:26 +11:00
Maxime Coste
ba0cd553ba Display a message on the tty directly on fatal error
Remove the xmessage/MessageBox based implementation.
2023-11-02 18:16:43 +11:00
Maxime Coste
a2c41593aa Fix partial regex text being pushed in history 2023-11-02 13:00:12 +11:00
Maxime Coste
8cc4de5bb3 Always ensure we do not scroll past the last line
An assert fails from time to time after reloading fifo buffers due
to being scrolled past the last line of the buffer. A repro case was
not found but this should fix the underlying issue.
2023-11-01 17:24:54 +11: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
d1c8622dc7 Clear buffer values on fifo buffer recreation
The cached WordDB/Highlighters/FifoReader are not relevant and are
better fully rebuilt than updated. This speeds up rebuilding the
WordDB of big fifo buffers such as a `git log`.
2023-10-25 12:53:55 +11:00
Maxime Coste
be33dee211 Speed up WordSplitter
Only do utf8 decoding once per codepoint instead of twice, limit
the byte length instead of the codepoint length.
2023-10-25 12:52:14 +11:00
Maxime Coste
b33b673f10 Remove unnecessary operator (since C++20) 2023-10-25 12:51:59 +11:00
Maxime Coste
18902b15af Refactor regex_prompt logic and fix function being called on abort
Fixes #4993
2023-10-04 21:03:10 +11:00
Loric Brevet
a2fd401cfa
Add an InlineInformation face distinct from Information 2023-09-28 15:06:29 +02:00
Maxime Coste
23afed056b Add a daemonize-session command and refactor local client handling
Make it possible to move the current session to a daemon one after
the fact, which is useful to ensure the session state survives client
disconnecting, for example when working from ssh.
2023-09-26 17:50:56 +10:00
Maxime Coste
3bf16c0fb1 Merge remote-tracking branch 'krobelus/foot-custom-keypad-sequences' 2023-09-23 21:31:01 +10:00
Chris Webb
9a0c7cea47 Fix quoting of arguments to kak -c SESSION
Filename arguments to kak -c SESSION are passed to the remote sessions
as commands like

  edit 'FILENAME';

but single-quotes in FILENAME are incorrectly escaped as \' instead of
being doubled-up. Fix this so kak -c SESSION "foo'bar" becomes

  edit 'foo''bar';

instead of

  edit 'foo\'bar';

Reported by @FlyingWombat in https://github.com/mawww/kakoune/issues/4980
2023-09-22 13:40:58 +01:00
Maxime Coste
871631bb00 Trigger auto completion refresh when necessary on completion select
This removes the timing dependent behaviour where `Tab` would only
display the completion menu if pressed before the prompt idle timeout

This means `exec :dc<tab>` now expands 'dc' to 'define-command'
instead of just showing the completion menu a few millis early.
2023-09-19 17:15:11 +10:00
Maxime Coste
541c385aa4 Revert "Do not make cursor visible on force redraw"
This unfortunately breaks the testing framework, more work
necessary before we can do that.

This reverts commit 9b1f4f5f20.
2023-09-08 05:54:32 +10:00
Maxime Coste
dd5b624003 Merge remote-tracking branch 'divarvel/show-trailing-whitespace' 2023-09-08 05:50:11 +10:00
Maxime Coste
9787756619 Use last display setup instead of recomputing for window_range
Fixes #4964
2023-09-08 05:24:56 +10:00
Maxime Coste
9b1f4f5f20 Do not make cursor visible on force redraw 2023-09-08 05:24:56 +10:00
Maxime Coste
20a2bca52e Do not make cursor visible after mouse scrolling and view commands
ensure cursor is visible after user input except if the command
implementation opted-out. Hooks and timers should not enforce
visible cursor.

PageUp/PageDown and `<c-f>` / `<c-b>` commands still move the cursor
as this seemed a desired behaviour.
2023-09-02 12:55:57 +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
Maxime Coste
7e86230c61 Merge remote-tracking branch 'arachsys/create-default-region' 2023-08-27 08:11:07 +10:00
Maxime Coste
fe93a9df37 Remove Window::force_redraw()
This was mostly redundant with Client::force_redraw.
2023-08-27 08:03:42 +10:00
Maxime Coste
6f9f32b4bb Small code cleanup in winow.cc/hh 2023-08-27 07:01:50 +10:00
Maxime Coste
9c0c6b8fd5 Revert "Only make cursor visible after buffer or selection change"
This is currently broken on various corner cases and breaks the
"master branch should be good for day to day work" implicit rule,
ongoing work to stabilize this feature will take place on the
no-cursor-move-on-scroll branch until its deemed ready.

This reverts commit 1e38045d70.

Closes #4963
2023-08-23 14:13:22 +10:00
Maxime Coste
1e38045d70 Only make cursor visible after buffer or selection change
Kakoune now does not touch cursors when scrolling. It checks
if either the buffer or selections has been modified since
last redraw.

Fixes #4124
Fixes #2844
2023-08-16 21:02:42 +10:00
Chris Webb
491d4d47ae Fix segfault when adding an invalid default-region highlighter
RegionsHighlighter::create_region() validates the highlighter type argument
but RegionsHighlighter::create_default_region() assumes it is correct,
segfaulting from dereferencing a null pointer if the given type isn't in
the highlighter registry HashMap.

@PJungkamp reported this in https://github.com/mawww/kakoune/issues/4959
with a simple recipe to reproduce:

    :add-highlighter shared/test regions
    :add-highlighter shared/test/ default-region invalid highlighter
    :add-highlighter window/test ref test

Validate the type argument in RegionsHighlighter::create_default_region()
in the same way as RegionsHighlighter::create_region().
2023-08-15 16:22:39 +01:00
Maxime Coste
6942a4c0c9 Change + command not to duplicate identical selections more than once
The current exponential behaviour does not seem that useful, it seems
more predictible that pressing `+` twice would end up with 3 copies
of the original selections instead of 4.

Fixes #4533
2023-08-14 22:50:22 +10:00
Maxime Coste
0a06d9acbd Minor formatting tweaks 2023-08-13 03:57:46 +10:00
Maxime Coste
e090131b87 Add a ProfileScope helper class to replace most profiling uses 2023-08-13 03:57:46 +10:00
Maxime Coste
e605ad8582 Kakoune 2023.08.05 2023-08-05 11:02:11 +10:00
Maxime Coste
d7822ea588 Removed unused captures 2023-08-05 10:39:54 +10:00
Maxime Coste
d1e189f1bf Try to fix clang build issues 2023-08-05 10:34:16 +10:00
Maxime Coste
f58d686066 Kakoune 2023.07.29 2023-07-29 15:53:23 +10:00
Johannes Altmanninger
12310418b0 Allow map/unmap during mapping execution
Commits e49c0fb04 (unmap: fail if the mapping is currently executing,
2023-05-14) 42be0057a (map: fail if key is currently executing,
2023-06-24) fixed potential use-after-free issues. By doing so,
it broke configurations that in practice have not triggered any
crashes [1] [2].

For example with,

	set -remove global autocomplete insert
	hook global InsertCompletionShow .* %{
	    map window insert <esc> <c-o>
	}
	hook global InsertCompletionHide .* %{
	    unmap window insert <esc> <c-o>
	}

The execution of the <esc> mapping triggers InsertCompletionHide fails
at unmapping. This seems legit and I don't see an obvious alternative
way to write it (InsertIdle would not be correct though it would work
in practice).

Fix the regression by allowing map and unmap again while keeping the
mappings alive until they have finished executing.

Applying map/unmap immediately seems like the most obvious semantics.
Alternatively, we could apply them in between key presses.

[1]: <https://github.com/kak-lsp/kak-lsp/issues/689>
[2]: <https://github.com/alexherbo2/auto-pairs.kak/issues/60>
2023-07-20 09:18:23 +02:00
Maxime Coste
e3122ab2c1 Refactor prompt history handling
Share incremental regex logic, pass the synthetized nature of keys
through to input handlers.
2023-07-05 22:00:32 +10:00
Maxime Coste
ec79864559 Merge remote-tracking branch 'krobelus/allow-history-in-mappings' 2023-07-05 20:39:39 +10:00
Maxime Coste
96ff68aeae Merge remote-tracking branch 'krobelus/fix-remap-uaf' 2023-07-04 19:33:10 +10:00
Maxime Coste
53fed4b8b9 Only auto-insert completion when at the end of the line
Auto inserting in the middle is annoying more often than not.
2023-07-04 17:15:33 +10:00
Johannes Altmanninger
42be0057a6 map: fail if key is currently executing
If during execution of a mapping, that same mapping is replaced,
there is undefined behavior because we destroy a mapping that we are
still iterating over.

I have been using this mapping inside my kakrc to re-source the kakrc.

	map global user s %{:source "%val{config}/kakrc"<ret>} -docstring 'source "%val{config}/kakrc"'

Now <space>s happens to not trigger undefined behavior because the
mapping stays the same.

However it triggers an assertion added by Commit e49c0fb04 (unmap:
fail if the mapping is currently executing, 2023-05-14), specifically
the destructor of ScopedSetBool that guards mapping execution.

Fix these by banning map of a key that is executing, just like we
did for unmap.

Alternative solution: we could allow mapping (and even unmapping)
keys at any time and keep them alive by moving them into a trash can,
like we do for clients and others.
2023-07-03 19:03:11 +02:00
Maxime Coste
661d1a0905 Merge common docstring in key mapping assistant
Fixes #4942
2023-07-03 20:48:59 +10:00
Maxime Coste
4b605c582c Merge remote-tracking branch 'omasanori/fixup-undo-doc' 2023-07-03 20:14:21 +10:00
Sergey Fedorov
dca5043812 Unbreak build on ppc
Fixes: https://github.com/mawww/kakoune/issues/4937
2023-06-27 12:29:45 +08: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
Maxime Coste
e06e409dc1 Another small structured binding conversion 2023-06-21 17:36:33 +10:00
Masanori Ogino
58058d1213 Fixup documentation on history navigation commands
Co-authored-by: Johannes Altmanninger <aclopte@gmail.com>
2023-06-21 10:25:59 +09:00
Maxime Coste
e365c42b4b Small structured binding conversion 2023-06-20 19:32:06 +10:00
Maxime Coste
d43268fbeb Fix invalid access of display line end
When a line only contains non-range atoms we can end-up accessing
past the end atom.

Add a test that shows the issue when run with valgrind, it is
unfortunately quite hard to trigger a crash because the invalidly
accessed byte usually leads to the correct code path being taken
(when != DisplayAtom::Range) so we have only 1 in 255 chance of
triggerring a crash.

Fixes #4927
2023-06-20 13:09:03 +10:00
Maxime Coste
e58592f00a Fix highlighters being applied to empty display buffers
In some cases such as with folding we can end-up with regions
not having any atoms to highlight which can trigger a crash as
we assume display buffers not to be empty

Fixes #4926
2023-06-19 12:55:55 +10:00
Johannes Altmanninger
163eb6dbc6 Disable history only for prompts that are never shown in the UI
My terminal allows to map <c-[> and <esc> independently.  I like
to use <c-[> as escape key so I have this mapping:

	map global prompt <c-[> <esc>

Unfortunately, this is not equivalent to <esc>.  Since mappings are
run with history disabled, <c-[> will not add the command to the
prompt history.

So disabling command history inside mappings is wrong in case the
command prompt was created before mapping execution. The behavior
should be: "a prompt that is both created and closed inside a
noninteractive context does not add to prompt history", where
"noninteractive" means inside a mapping, hook, command, execute-keys
or evaluate-commands.

Implement this behavior, it should better meet user expectations.
Scripts can always use "set-register" to add to history.

Here are my test cases:

1. Basic regression test (needs above mapping):

	:nop should be added to history<c-[>

---

2. Create the prompt in a noninteractive context:

	:exec %{:}

now we're back in the interactive context, so we can type:

	nop should be added to history<ret>

---

3. To check if it works for nested prompts, first set up this mapping.

	map global prompt <c-j> '<a-semicolon>:nop should NOT be added to history<ret>'
	map global prompt <c-h> '<a-semicolon>:nop should be added to history first'

Then type

	:nop should be added to history second<c-j><c-h><ret><ret>

the inner command run by <c-j> should not be added to history because
it only existed in a noninteractive context.

---

See also the discussion https://github.com/mawww/kakoune/pull/4692

We could automate the tests if we had a test setup that allowed
feeding interactive key input into Kakoune instead of using
"execute-commands". Some projects use tmux, or maybe we can mock
the terminal.
2023-06-17 11:21:41 +02:00
Johannes Altmanninger
6563b82092 Use auto to avoid repeating type of dynamic cast
I think the clang-tidy lint is called modernize-use-auto
2023-06-17 11:21:16 +02:00
Johannes Altmanninger
00490cd084 Rename "disable_history" stack state to "noninteractive"
The commit after next will fix a bug where we wrongly disable prompt
history in some scenarios. The root cause is that life span of
"disable_history" does not model when we actually want to disable
history.

Let's rename the state variable to "noninteractive". It's set whenever
we are executing a hook, mapping or command.

Note that it's also active inside ":prompt"'s callback, which doesn't
play well with the new name :(
2023-06-17 11:21:16 +02:00
Maxime Coste
5901d2e06b Revert "Switch undo storage from a tree to a plain list"
Moving across history moved to <c-j>/<c-k> to keep <a-u>/<a-U>
for selection undo/redo

This reverts commit e0d33f51b3.
2023-06-17 17:31:57 +10:00
Maxime Coste
b2a853cfc2 Add a few missing entries to the changelog 2023-06-17 17:10:05 +10:00
Maxime Coste
5b1ad0bd0c Add a -previous switch to show-matching highlighter
This switch makes show-matching fallback to the character preceeding
the cursor if the character under the cursor is not a matching
character, which should make show-matching more useful in insert mode.
2023-06-14 22:53:39 +10:00
Maxime Coste
19cbb703a7 Merge remote-tracking branch 'arrufat/fix-build-gcc13' 2023-06-12 21:53:39 +10:00
Maxime Coste
467021c3c7 Merge remote-tracking branch 'arrufat/remove-unneeded-this-capture' 2023-06-12 21:51:13 +10:00
Maxime Coste
ca4593e5cd Fix one missing face pre-parsing 2023-06-12 17:12:24 +10:00
Maxime Coste
6984340936 Store region pointers instead of names in the RegionsHighlighter cache
Cache get fully invalidated whenever the regions change, so there
should be no risk of referencing a removed region, and this removes
one hash map lookup for every region in the displayed buffer range.
2023-06-12 16:45:19 +10:00
Maxime Coste
af66a95ef8 Trim display lines before the colorize pass
Colorizing long lines can be costly, remove all the invisible atoms
earlier. Also optimize ForwardHighlighterApplier further by trimming
empty lines.
2023-06-12 16:26:22 +10:00
Maxime Coste
5a867ebdd1 Pre-parse face specs in Highlighters
Re-parsing face specs can be expensive as highlighters can
be called many times during a redraw with nested regions.
2023-06-10 09:46:46 +10:00
Adrià Arrufat
66b22a1902 Remove unneeded this capture in lambda
Clang 15 was complaining about this.
2023-06-09 21:47:49 +09:00
Adrià Arrufat
77ade51a81 Fix build using GCC 13.1
The <functional> header was missing and the "hash.hh" was not used.
2023-06-09 21:41:51 +09:00
Maxime Coste
e0728d3434 Speed up regions highlighting on files with big lines
Range atoms should always appear in order, so we can iterate a single
time through the display lines and display atoms while applying
hightlighters to regions
2023-06-09 16:38:14 +10:00
Maxime Coste
95a4d70379 Kill current shell on <c-g> during shell execution
Closes #4907
2023-05-29 20:25:56 +10:00
Maxime Coste
cf7c638025 Refactor KeymapManager to enfore setting is_executing on key iteration
Add an iterator based remove to HashMap as that was missing. Make
KeymapManager responsible for throwing on unmap an executing mapping.
2023-05-29 20:11:06 +10:00
Johannes Altmanninger
e49c0fb040 unmap: fail if the mapping is currently executing
When unmapping a key sequence that is currently executing, we continue
executing freed memory which can have weird effects.  Let's instead
throw an error if that happens. In future we can support unmap in
this scenario.

Closes #4896
2023-05-25 00:04:23 +02:00
Maxime Coste
cfa658b899 Add <c-g> to cancel current operation
The current implementation only does this during regex operations,
but should be extensible to other operations that might take a long
time by regularly calling EventManager::handle_urgent_events().
2023-05-21 16:20:51 +10:00
Maxime Coste
e140df8f08 Add an idle callback to be called regularly while regex matching
This paves the way towards being able to cancel long regex matching
operations
2023-05-21 16:20:51 +10:00
Maxime Coste
19b4149d47 Fix warnings with gcc-13 2023-05-21 12:40:27 +10:00
Chris Webb
2f1e536ac7 Fix debug keys output for shift/ctrl modified mouse events
Although Kakoune responds to modified mouse events, they show up in the
debug buffer corrupted. to_string() tests for equality on the mouse event
modifiers rather than testing just the relevant bits, so the modified
mouse events incorrectly fall through to the normal key handling.

Fix this and restructure to allow mouse events to be modifier-prefixed.

Signed-off-by: Chris Webb <chris@arachsys.com>
2023-05-11 20:21:31 +01:00
Maxime Coste
3d989af2de Merge remote-tracking branch 'krobelus/fix-crash-connecting-monitor' 2023-05-10 20:03:22 +10:00
Maxime Coste
459007ae21 Merge remote-tracking branch 'sidkshatriya/remove-ref-ref' 2023-05-10 20:02:51 +10:00
Maxime Coste
d39930cd10 Merge remote-tracking branch 'krobelus/fix-_-at_multibyte_chars' 2023-05-10 19:57:46 +10:00
Chris Webb
fc6f32f9ee Stop _ from tearing multibyte UTF-8 sequences
Fixes #4887

[ja: add test]
2023-05-09 23:07:07 +02:00
Johannes Altmanninger
2308a17833 Update changelog for selection undo 2023-05-09 11:32:37 +02:00
Johannes Altmanninger
7d2bae63b9 Map undo selection change to <a-u>/<a-U>
Change the initial <c-h>/<c-k> bindings to the recently freed-up
<a-u></a-U>.

Pros:
- easier to remember
- the redo binding is logical.
- works on legacy terminals, unlike <c-h>

Cons:
- It's less convenient to toggle between selection undo and redo
  keys. I think this is okay since this scenario does not happen that
  often in practice.
2023-05-09 11:32:37 +02:00
Maxime Coste
04780b235b Add support for recursive expansions with %exp{...}
%exp{...} just expands its content the same way double quoted strings
do, but using a named expansion type makes it possible to use the
more quoting mechanism to avoid quoting hell.
2023-05-04 12:49:50 +10: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
a4918f934c Merge remote-tracking branch 'occivink/undo-history-as-plain-list' 2023-04-24 19:20:35 +10:00
Sidharth Kshatriya
7efcb94a5e Fix compile error: Compiler refuses to deduce alias template arguments on Darwin (clang 14.0.3)
input_handler.cc:1476:16: error: alias template 'ConstArrayView' requires template arguments; argument deduction only allowed for class templates
        insert(ConstArrayView{content});
               ^
input_handler.cc:1522:16: error: alias template 'ConstArrayView' requires template arguments; argument deduction only allowed for class templates
        insert(ConstArrayView{str});
               ^
2023-04-22 22:20:29 +05:30
Olivier Perret
e0d33f51b3 Switch undo storage from a tree to a plain list
Whenever a new history node is committed after some undo steps, instead
of creating a new branch in the undo graph, we first append the inverse
modifications starting from the end of the undo list up to the current
position before adding the new node.

For example let's assume that the undo history is A-B-C, that a single undo
has been done (bringing us to state B) and that a new change D is committed.
Instead of creating a new branch starting at B, we add the inverse of C
(noted ^C) at the end, and D afterwards. This results in the undo history
A-B-C-^C-D. Since C-^C collapses to a null change, this is equivalent to
A-B-D but without having lost the C branch of the history.

If a new change is committed while no undo has been done, the new history
node is simply appended to the list, as was the case previously.

This results in a simplification of the user interaction, as two bindings
are now sufficient to walk the entire undo history, as opposed to needing
extra bindings to switch branches whenever they occur.
The <a-u> and <a-U> bindings are now free.

It also simplifies the implementation, as the graph traversal and
branching code are not needed anymore. The parent and child of a node are
now respectively the previous and the next elements in the list, so there
is no need to store their ID as part of the node.
Only the committing of an undo group is slightly more complex, as inverse
history nodes need to be added depending on the current position in the
undo list.

The following article was the initial motivation for this change:
https://github.com/zaboople/klonk/blob/master/TheGURQ.md
2023-04-17 10:25:51 +02:00
Sidharth Kshatriya
f41f3781e1 Remove && from the template parameter given to declval
It seems redundant as declval already returns a rvalue reference
2023-03-15 12:34:12 +05:30
Maxime Coste
019fbc5439 Cleanup and speed up test runner
Add a -end-of-line switch to echo command to make it possible
to use `echo -end-of-line -to-file <file>` to collect env-vars
2023-03-14 09:01:13 +11: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
cb3512f01e Grow dual thread stack after pushing a thread on the next queue
The previous code was assuming it was fine to push_next without
growing, which used to be the case with the previous implementation
because we always have poped the current thread that we try to push.

However now that we use a ring-buffer, m_next_begin == m_next_end can
either mean full, or empty. We solve this by assuming it means empty
and never allowing the buffer to become full, which means we need
to grow after pushing to next if we get full.

Fixes #4859
2023-03-13 22:45:19 +11:00
Maxime Coste
81787792d0 Fix crash when pasting at buffer end
Fixes #4844
2023-03-13 21:49:45 +11:00
Maxime Coste
c8f682f3ad Merge remote-tracking branch 'krobelus/perror-on-chmod-failure' 2023-03-13 21:34:13 +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
Maxime Coste
f05ab99d4d Merge remote-tracking branch 'krobelus/bracketed-paste' 2023-03-13 20:37:45 +11:00
Johannes Altmanninger
1990a764e3 Make linewise bracketed paste match P behavior
This is experimental. Testing will reveal if this is the desired
behavior.
2023-03-11 16:21:57 +01: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
Maxime Coste
38077ca826 Merge remote-tracking branch 'potatoalienof13/master' 2023-03-09 21:09:43 +11:00
Maxime Coste
706e5bd215 Merge remote-tracking branch 'potatoalienof13/i-am-bad-at-git' 2023-03-09 21:08:03 +11:00
Maxime Coste
1479bf6f08 Merge remote-tracking branch 'inahga/aghani-info-width' 2023-03-01 19:50:53 +11:00
Maxime Coste
0a4c4a5de5 Merge remote-tracking branch 'mujo-hash/master' 2023-03-01 19:50:22 +11:00
Maxime Coste
3b0d91c9c7 Merge remote-tracking branch 'krobelus/complete-using-param-spec' 2023-03-01 19:45:32 +11:00
ioh
894e44fdbf Fix new gcc errors for missing types.
Errors when building with gcc 13:
ranked_match.hh:10:21: error: ‘uint64_t’ does not name a type
   10 | using UsedLetters = uint64_t;
      |                     ^~~~~~~~
2023-02-28 19:36:44 -08:00
Maxime Coste
fa060c2a17 Fix fatal exception when checking if buffer needs to be reloaded
If, for example, the buffer path now is a directory, MappedFile will
throw on construction. Using a try block to explicitely allow errors
fixes the issue.
2023-02-21 16:59:16 +11:00
Maxime Coste
be49f36205 Only decode current codepoint once per step
Instead of potentially decoding for each thread, always decode as
its only slightly slower than finding next codepoint (which will
be necessary anyway) and pass the codepoint to each thread.
2023-02-19 12:15:33 +11:00
Maxime Coste
2b74cd4b59 Remove instructions from ExecConfig
We can just compute whenever we reset last_step, which does not happen
often and we know `forward` at compile time anyway
2023-02-19 11:46:17 +11:00
Maxime Coste
f115af7a57 Optimize Regex CharacterClass matching
Take advantage of ranges sorting to early out, make the logic
inline.
2023-02-19 11:16:14 +11:00
Johannes Altmanninger
213ea922b1 Complete arguments to "echo -to-file"
Including this here because grandparent parent commit broke completions
for "edit -fifo".
2023-02-17 20:50:58 +01:00
Johannes Altmanninger
9e0502a1ca Do not complete redundant switches 2023-02-17 20:50:58 +01:00
Johannes Altmanninger
64d4d29d43 Use parameter parser to skip switch args in completion
The command line "hook -group xyz " should get scope completions but
it actually gets hook completions because "xyz" is wrongly interpreted
as positional argument.

Fix this by using the parameters parser to compute positional
arguments.

Fixes #4840
2023-02-17 20:50:58 +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
afaa47e93f Fix trimming of line front halfway through a double-width glyph
Insert a space to replace the half glyph and ensure the rest of the
line is correctly aligned.

Fixes #4843
2023-02-15 13:04:53 +11:00
Maxime Coste
0630b4f4f6 Fix scroll_window not ensuring cursor lies on a codepoint start
Fixes #4839
2023-02-14 22:00:12 +11:00
Maxime Coste
458e3ef20a Immediately execute ModuleLoaded hooks for already loaded modules
This is trickier than expected because ModuleLoaded hooks can (as
any other hooks) use arbitrary regular expressions for their filter.

Fixes #4841
2023-02-14 21:31:29 +11:00
Maxime Coste
85ceef29bd Fix broken corner cases in DualThreadStack::grow_ifn
We only grow when the ring buffer is full, which allows for a nice
simplification of the code.

Tell grow_ifn if we pushed in current or next so that we can
distinguish between filled by next or filled by current when
m_current == m_next_begin
2023-02-14 17:13:31 +11:00
Maxime Coste
d708b77186 Refactor DualThreadStack as a RingBuffer
Instead of two stacks growing from the two ends of a buffer, use
a ring buffer growing from the same mid spot.

This avoids the costly memory copy every step when we set next
threads as the current ones.
2023-02-14 07:04:54 +11:00
Maxime Coste
762064dc68 Remove scheduled optimization from ThreadedRegexVM
This does not seem to actually speed up execution as threads will
be dropped on next step anyway
2023-02-13 21:15:55 +11:00
Maxime Coste
3150e9b3cd Avoid extra indirection for storing FifoWatcher
By improving the Value interface we can avoid storing a unique_ptr
to a FifoWatcher and directly store the FifoWatcher.
2023-02-10 12:56:32 +11:00
Maxime Coste
eb0e983133 Fix DisplayLine::trim_front quadratic behaviour
Erasing fully trimmed display atoms one by one means we have to
shift all the remaining ones every time. This is wasteful and we
can just erase all the fully trimmed atom in one go.

Fixes #4797
2023-02-03 11:31:13 +11:00
Maxime Coste
d5ae08498c Merge remote-tracking branch 'krobelus/selection-undo-fix-standstill-after-buffer-change' 2023-01-28 08:29:41 +11:00
Maxime Coste
37b160e935 Merge remote-tracking branch 'occivink/fix-split-stray-sel' 2023-01-27 10:58:38 +11:00
Maxime Coste
5097884608 Fix crash in TabulationHighlighter when wrapping just after a tab 2023-01-23 17:39:40 +11:00
Maxime Coste
f5d5274c5f Fix incorrect use of subject end/begin in regex execution
This could lead to reading past subject string end in certain
conditions

Fixes #4794
2023-01-23 17:38:02 +11:00
Maxime Coste
a02bd19533 Revert "Remove compare include that seems to break clang"
Looks clang breaks differently when this is not included

This reverts commit 7030b3c47c.
2023-01-21 11:27:05 +11:00
Maxime Coste
702358b559 Replace std::strong_ordering with auto return type to not require <compare> 2023-01-21 11:19:39 +11:00
Maxime Coste
7030b3c47c Remove compare include that seems to break clang 2023-01-21 11:06:22 +11:00
Olivier Perret
b039a313a4 fix 'split' operation when the pattern occurs at the beginning
Previously it would result in a stray single-character selection at the
beginning of the input text.

For example:
[abcabc] -> split on 'a' -> [a][bc]a[bc]
or
[foobarfoobar] -> split on 'foo' -> [f]oo[bar]foo[bar]

Note that this behavior was not occuring if the input text was at the
beginning of the buffer
2023-01-20 18:05:04 +01:00
Maxime Coste
d8883d47c0 Merge remote-tracking branch 'krobelus/fix-mouse-click-during-insert' 2023-01-18 07:48:37 +11:00
Maxime Coste
029b28a85c Merge remote-tracking branch 'potatoalienof13/fix-buffer-advance' 2023-01-18 07:47:23 +11:00
Maxime Coste
f720ded96a Merge remote-tracking branch 'krobelus/missing-error-when-open-fails' 2023-01-18 07:40:35 +11:00
Johannes Altmanninger
4f7d7a5e33 Fix regression when file on command line cannot be opened
Commit 933e4a599 (Load buffer in command line order, 2022-12-06)
introduced a regression: the command

	$ kak /boot/grub/grub.cfg
	Fatal error: no such buffer '/boot/grub/grub.cfg'

quits with no indication of the underlying error.

Prior to 933e4a599, it would open the *scratch* buffer instead,
and show an error in the status line, pointing to the debug buffer,
which would contain:

	error while opening file '/boot/grub/grub.cfg':
	    /boot/grub/grub.cfg: Permission denied

Let's fix this scenario by matching the old behavior.
2023-01-08 17:26:15 +01:00
Johannes Altmanninger
35f23d6fad Remove bogus assertions preventing mouse clicks in insert mode
Recent changes for selection-undo added an assertion that triggers
when a mouse-drag overlaps with an insert mode, because both events
record selection history.  However this is actually fine.  The one
that finishes last concludes the selection edition, while the other
one will be a nop.

The test could be simpler (i.e. not require sleeps) but I figured it
doesn't hurt add this since we don't have any comparable tests.
2023-01-08 10:47:36 +01:00
Johannes Altmanninger
516759bb2f Make selection undo skip over entries that are nop after buffer change
After buffer modification - in particular after deletion - adjacent
selection history entries may correspond to the same effective
selection when applied to the current buffer. This means that we
sometimes need to press <c-h> multiple times to make one visible
change. This is not what the user expects, so let's keep walking the
selection history until we hit an actual change.

Alternatively, we could minimize the selection history after buffer
changes but I think that would make the it worse after content
undo+redo.
2022-12-27 18:24:55 +01:00
Johannes Altmanninger
8427379a5d Tweak selection-undo interaction with WinDisplay hooks
Each selection undo operation is surrounded by pair of
begin_edition()/end_edition() calls.
The original reason for adding these was that in one of my preliminary
versions, a WinDisplay hook could break an undo chain, even if the
hook did not affect selections at all. This has since been fixed.

By surrounding the undo with begin_edition()/end_edition(), try to
ensure that any selection modification that happens in a WinDisplay
hook would not break the undo chain. Essentially this means that,
after using <c-h> to undo a buffer change, this was meant to
make sure that <c-k> could redo that buffer change.

However, it turns out this actually doesn't work.  The attached test
case triggers an assertion.  As described in the first paragraph,
the only real-world motivation for this is gone, so let's simplify
the behavior.
The assertion fix means that we can test the next commit better.
2022-12-27 18:24:55 +01:00
Johannes Altmanninger
016e1be77f Extract variable and add comment in selection change recording
No functional change.
2022-12-27 18:24:55 +01:00
Johannes Altmanninger
02d0584e0f Extract variable in selection undo
No functional change.
2022-12-27 18:24:55 +01:00
Johannes Altmanninger
a50cb5f6e7 Share logic for undo/redo selection changes
I will suggest a few changes to this code.
No functional change.
2022-12-27 18:24:55 +01:00
potatoalienof13
eb447f1c43 Remove a check for inclusivity in select_to_reverse.
It turns out that neither <a-f> or <a-t> make sense when run at the
beginning of the buffer. When I first created the check, I thought
that <a-f> made sense if the character under the cursor was the
character being searched for.  I was wrong, <a-f> should always go
at least one character backwards.
2022-12-26 09:17:06 -05:00
potatoalienof13
1a4737cd20 <a-t> should not succeed when run on the first character of a file. 2022-12-25 17:57:28 -05:00
potatoalienof13
293d46837d Fix Buffer::advance out of bounds access.
This commit fixes a bug in Buffer::advance where it would first access
m_lines[-1], and then check whether or not that access would have
segfaulted.  This commit moves the check to before the segfault would
occur.
2022-12-22 17:34:42 -05:00
Maxime Coste
c31e53e8a4 Merge remote-tracking branch 'krobelus/fix-cd-after-backspace' 2022-12-22 09:51:58 +11:00
potatoalienof13
041c88c930 This commit attempts to fix a crash with -f. Specifically when kakoune
is run as

> ./kak -f '2oK.k<c-n><ret><c-n>' /dev/null

The crash occurs because when <c-n> is pressed for the second time,
it attempts to use m_completions from the first press of <c-n>.
This only happens when kakoune is run with -f, because when this is done
interactively, there is a client, which means that m_completions gets
reset.  This removes the check that causes that difference.

I am not *completely* sure that this is the best way to solve the
problem, since I am not completely sure why that check was put there
in the first place.
2022-12-21 17:46:49 -05:00