Commit graph

183 commits

Author SHA1 Message Date
lvyaoting
d1d38d1ed7
chore: fix some typos (#3239)
Signed-off-by: lvyaoting <lvyaoting@outlook.com>
2024-04-08 12:04:38 +02:00
Jöran Karl
a3ca054371 buffer: Uncomment InitRuntimeFiles(false) in the buffer_test.go
...since we fixed the race between the syntax highlighting and the buffer
editing.
2024-04-05 14:24:59 +02:00
Jöran Karl
6e71e37568 buffer: Rename LineBytes parameter to "lineN" to fit to the rest 2024-04-05 14:24:39 +02:00
Jöran Karl
dd7134a762 buffer: Remove superfluous rehighlight from LineArray
...which isn't used so far and probably handled better in a different way.
2024-04-05 14:24:39 +02:00
Jöran Karl
2830c4878e buffer: Lock the LineArray in case of modifications and export this lock 2024-04-05 14:24:06 +02:00
Jöran Karl
53d56d032c buffer: Remove unneeded recursion of insert()
This is necessary as a preparation to introduce a lock for the whole LineArray.
The modification can then be done without trying to lock the same lock twice.

Co-authored-by: Dmytro Maluka <dmitrymaluka@gmail.com>
2024-04-05 14:19:37 +02:00
Dmytro Maluka
69dc54b407 Temporarily don't initialize runtime files in buffer test
Adding InitRuntimeFiles() to buffer_test.go has changed the behavior
of this test: now it tests not just buffer editing per se, but also
how well buffer editing works together with syntax highlighting (since
InitRuntimeFiles() loads syntax files, and many of the test buffers
match the json header pattern in the json.yaml syntax file, so they are
"highlighted" as json). This revealed long existing races between
buffer editing and syntax highlighting.

Until we fix those races, temporarily disable InitRuntimeFiles() in this
test.
2024-04-03 04:37:44 +02:00
Dmytro Maluka
c5d32f625b Ignore user-defined runtime files in buffer test and rtfiles test
When initializing runtime files (syntax files etc) in tests, initialize
built-in runtime files only, to ensure that the tests are not affected
by whatever is in ~/.config/micro/ on the test machine.

micro_test.go already ensures that, by using its own temporary directory
as an (empty) config directory. So we only need to fix buffer_test.go
and rtfiles_test.go. In those tests, don't repeat the same dance with
a temporary directory, instead just ignore the config directory.
2024-04-03 03:44:15 +02:00
Dmytro Maluka
baca0e5cb2 Add param to InitRuntimeFiles() to init built-in files only 2024-04-03 03:41:06 +02:00
Dmytro Maluka
d67ce731ed Don't initialize plugins in buffer test and rtfiles test
Adding InitPlugins() to tests has caused noisy error logs when running
the buffer_test.go test (although the test result is still PASS):

2024/03/23 15:14:30 Plugin does not exist: autoclose at autoclose : &{autoclose autoclose <nil> [runtime/plugins/autoclose/autoclose.lua] false true}
2024/03/23 15:14:30 Plugin does not exist: comment at comment : &{comment comment <nil> [runtime/plugins/comment/comment.lua] false true}
2024/03/23 15:14:30 Plugin does not exist: diff at diff : &{diff diff <nil> [runtime/plugins/diff/diff.lua] false true}
2024/03/23 15:14:30 Plugin does not exist: ftoptions at ftoptions : &{ftoptions ftoptions <nil> [runtime/plugins/ftoptions/ftoptions.lua] false true}
...

These errors are caused simply by the fact that plugins are initialized
but not loaded. Adding config.LoadAllPlugins() to buffer_test.go "fixes"
this problem.

However, at the moment it doesn't seem a good idea to load plugins in
buffer_test.go, since buffer_test.go doesn't properly initialize Lua. It
only does ulua.L = lua.NewState() but doesn't do the other stuff that
init() in cmd/micro/initlua.go does. As a result, plugins will not be
able to do anything correctly.

So in order to initialize Lua correctly we need to be inside cmd/micro/,
so we cannot do it in buffer_test.go or any other tests except
micro_test.go.
2024-04-03 03:04:42 +02:00
Dmytro Maluka
20bf7096b8
Make set filetype off work as expected (#3216)
Disable syntax highlighting after setting filetype to `off`.
2024-03-25 19:38:33 +01:00
Dmytro Maluka
053949eac6 UpdateRules: de-densify code arouns signatureMatch
Purely cosmetic change: make the code a bit more readable by reducing
its visual "density".
2024-03-24 04:47:04 +01:00
Dmytro Maluka
9ee82a6cb3 UpdateRules: rename syntaxFileBuffer to syntaxFileInfo
To make it more clear. Why Buffer?
2024-03-24 04:47:04 +01:00
Dmytro Maluka
5492d30953 UpdateRules: add comment about the reason for signature match 2024-03-24 04:47:04 +01:00
Dmytro Maluka
6c3b5ad17c UpdateRules: refactor "header.FileType == ft" case 2024-03-24 04:47:04 +01:00
Dmytro Maluka
39e410aa46 UpdateRules: reintroduce using header regex for filetype detection
Replacing header patterns with signature patterns was a mistake, since
both are quite different from each other, and both have their uses. In
fact, this caused a serious regression: for such files as shell scripts
without *.sh extension but with #!/bin/sh inside, filetype detection
does not work at all anymore.

Since both header and signature patterns are useful, reintroduce support
for header patterns while keeping support for signature patterns as well
and make both work nicely together.

Also, unlike in the old implementation (before signatures were
introduced), ensure that filename matches take precedence over header
matches, i.e. if there is at least one filename match found, all header
matches are ignored. This makes the behavior more deterministic and
prevents previously observed issues like #2894 and #3054: wrongly
detected filetypes caused by some overly general header patterns.

Precisely, the new behavior is:

1. if there is at least one filename match, use filename matches only
2. if there are no filename matches, use header matches
3. in both cases, try to use signatures to find the best match among
multiple filename or header matches
2024-03-24 04:47:04 +01:00
Dmytro Maluka
2b8d925925 UpdateRules: rename syntaxFiles to fnameMatches
As a preparation for reintroducing header matches.
2024-03-24 04:47:04 +01:00
Dmytro Maluka
0c923aa156 UpdateRules: don't call highlight.ParseFile() needlessly
No need to parse a syntax YAML file if we are not going to use it,
it's a waste of CPU cycles.
2024-03-24 04:47:04 +01:00
Dmytro Maluka
13483602d5 UpdateRules: fix foundDef logic
The original meaning of foundDef was: "we already found the final syntax
definition in a user's custom syntax file". After introducing signatures
its meaning became: "we found some potential syntax definition in a
user's custom syntax file, but we don't know yet if it's the final one".
This makes the code confusing and actually buggy.

At least one bug is that if we found some potential filename matches in
the user's custom syntax files, we don't search for more matches in the
built-in syntax files. Which is wrong: we should keep searching for as
many potential matches as possible, in both user's and built-in syntax
files, to select the best one among them.

Fix that by restoring the original meaning of foundDef and updating the
logic accordingly.
2024-03-24 04:47:04 +01:00
occupyhabit
8b4e9d2c5e
chore: remove repetitive words (#3205)
Signed-off-by: occupyhabit <wangmengjiao@outlook.com>
2024-03-23 17:02:41 +01:00
Jöran Karl
a57d29ada9
command: Fix reload command to correctly initialize and reload all runtime files (#3062)
* rtfiles: Initialize all-/realFiles and Plugins in InitRuntimeFiles

* command: Reload plugins at ReloadCmd too

* command: Don't reload plugins in case of ReloadConfig()

* rtfiles: Split InitRuntimeFiles() into one func for assets and one for plugins

* rtfiles: Remove the unnecessary init function

With this modification the InitRuntimeFiles() and InitPlugins() (if needed)
must be called first, otherwise uninitialized runtime file variables are most
likely.
2024-03-22 20:47:30 +01:00
Jöran Karl
c64add289b
command: Fix replace to be able to insert '$' (#2954)
* command: Fix replace to be able to insert '$'

* help: commands: Precise the documentation of `replace`

* help: commands: Further improvement suggested within the review

Co-authored-by: Beni Cherniavsky-Paskin <cben@redhat.com>

* Fix replace with '$' in a more kosher way

On top of JoeKar's fix.

---------

Co-authored-by: Beni Cherniavsky-Paskin <cben@redhat.com>
Co-authored-by: Dmytro Maluka <dmitrymaluka@gmail.com>
2024-03-17 21:37:16 +01:00
Jöran Karl
4a53419c62
option: Don't apply rmtrailingws in case of timed autosave (#2850) 2024-03-15 18:46:51 +01:00
Dmytro Maluka
5bfda7b5f6
Merge branch 'master' into fix/file-detection 2024-03-14 04:32:09 +01:00
Dmitry Maluka
13d1407f60 hltrailingws: simpler and better undo/redo handling 2024-03-14 03:18:11 +01:00
Dmitry Maluka
f108c90643 hltrailingws: improve updateTrailingWs logic
Handle the case when the cursor itself hasn't really moved to
another line, but its line number has changed due to insert
or remove of some lines above.
In this case, if the cursor is still at its new trailingws,
we should not reset NewTrailingWsY to -1 but update it to the
new line number.

A scenario exemplifying this issue:
Bind some key, e.g. Alt-r, to such a lua function:

function insertNewlineAbove(bp)
    bp.Buf:Insert(buffer.Loc(0, bp.Cursor.Y), "\n")
end

Then in a file containing these lines:

aaa
bbb
ccc

insert a space at the end of bbb line, and then press Alt-r.
bbb and ccc are moved one line down, but also the trailing space
after bbb becomes highlighted, which isn't what we expect.
This commit fixes that.
2024-03-14 03:18:11 +01:00
Dmitry Maluka
104caf08dd Highlighting trailing whitespaces
Added option `hltrailingws` for highlighting trailing whitespaces
at the end of lines. Note that it behaves in a "smart" way.
It doesn't highlight newly added (transient) trailing whitespaces
that naturally occur while typing text. It would be annoying to
see transient highlighting every time we enter a space at the end
of a line while typing.
So a newly added trailing whitespace starts being highlighting
only after the cursor moves to another line. Thus the highlighting
serves its purpose: it draws our attention to annoying sloppy
forgotten trailing whitespaces.
2024-03-14 03:10:31 +01:00
toiletbril
69eaa9191a
options: add matchbracestyle (#2876)
* Update docs to include `matchbracestyle`

* Add `matchbracestyle` to infocomplete.go

* Add validator and default settings for `matchbracestyle`

* Highlight or underline braces based on `matchbracestyle`

* Add `match-brace` to default colorschemes

* Correct `FindMatchingBrace()` counting

Make brace under the cursor have priority over brace to the left in
ambiguous cases when matching braces

Co-authored-by: Dmitry Maluka <dmitrymaluka@gmail.com>

* Fix conflicts

---------

Co-authored-by: Jöran Karl <3951388+JoeKar@users.noreply.github.com>
Co-authored-by: Dmitry Maluka <dmitrymaluka@gmail.com>
2024-03-13 20:21:27 +01:00
taconi
fe4ade78df
feat: adds GetArg and GetWord methods to Buffer (#3112) 2024-03-12 21:23:08 +01:00
Dmytro Maluka
9fdea82542
Fix various issues with SpawnMultiCursor{Up,Down} (#3145)
* SpawnMultiCursorUp/Down: change order of adding cursors

SpawnMultiCursor{Up,Down} currently works in a tricky way: instead of
creating a new cursor above or below, it moves the current "primary"
cursor above or below, and then creates a new cursor below or above the
new position of the current cursor (i.e. at its previous position),
creating an illusion for the user that the current (top-most or
bottom-most) cursor is a newly spawned cursor.

This trick causes at least the following issues:

- When the line above or below, where we spawn a new cursor, is shorter
  than the current cursor position in the current line, the new cursor
  is placed at the end of this short line (which is expected), but also
  the current cursor unexpectedly changes its x position and moves
  below/above the new cursor.

- When removing a cursor in RemoveMultiCursor (default Alt-p key), it
  non-intuitively removes the cursor which, from the user point of view,
  is not the last but the last-but-one cursor.

Fix these issues by replacing the trick with a straightforward logic:
just create the new cursor above or below the last one.

Note that this fix has a user-visible side effect: the last cursor is
no longer the "primary" one (since it is now the last in the list, not
the first), so e.g. when the user clears multicursors via Esc key, the
remaining cursor is the first one, not the last one. I assume it's ok.

* SpawnMultiCursorUp/Down: move common code to a helper fn

* SpawnMultiCursorUp/Down: honor visual width and LastVisualX

Make spawning multicursors up/down behave more similarly to cursor
movements up/down. This change fixes 2 issues at once:

- SpawnMultiCursorUp/Down doesn't take into account the visual width of
  the text before the cursor, which may be different from its character
  width (e.g. if it contains tabs). So e.g. if the number of tabs before
  the cursor in the current line is not the same as in the new line, the
  new cursor is placed at an unexpected location.

- SpawnMultiCursorUp/Down doesn't take into account the cursor's
  remembered x position (LastVisualX) when e.g. spawning a new cursor
  in the below line which is short than the current cursor position, and
  then spawning yet another cursor in the next below line which is
  longer than this short line.

* SpawnMultiCursorUp/Down: honor softwrap

When softwrap is enabled and the current line is wrapped, make
SpawnMultiCursor{Up,Down} spawn cursor in the next visual line within
this wrapped line, similarly to how we handle cursor movements up/down
within wrapped lines.

* SpawnMultiCursorUp/Down: deselect when spawning cursors

To avoid weird user experience (spawned cursors messing with selections
of existing cursors).
2024-03-04 13:23:50 -08:00
niten94
422305af99
Set bits in mode used when opening files (#3095)
Set write permission bits of group and other users in mode used when
opening files.
2024-01-17 00:06:14 -08:00
Jöran Karl
3c16df87ee options: Add capability to define the line count parsed for the signature check 2023-10-26 20:59:42 +02:00
Jöran Karl
2d0d0416e7 buffer: Prefer user defined over built-in file types 2023-10-26 20:59:42 +02:00
Jöran Karl
433879046e Improve file detection with signature check capabilities
This allows more complex detection upon regex rules for a certain amount of
lines.
2023-10-26 20:20:02 +02:00
Jeffrey Smith
c55fb3329f
Fixed newline format detection for files not ending with a newline (#2875)
* Fixed newline format detection for files not ending with a newline

Files with Windows-style line endings were being converted to
Unix-style if the file did not end with a newline

* Updated file format detection fix for consistency
2023-10-16 11:03:03 +02:00
Jöran Karl
db5fcf11a9
save: Restore the screen before overwriteFile() is left (#2967)
...otherwise there is no screen anymore to draw a possible error message.
2023-10-16 10:44:35 +02:00
Dmitry Maluka
db6d4f5461
save: Restore the screen if failed to start sudo (#2971)
Similarly to the crash fixed by #2967, which happens if sudo failed,
a crash also happens when sudo even fails to start. The reason for
the crash is also similar: nil dereference of screen.Screen caused by
the fact that we do not restore temporarily disabled screen.

To reproduce this crash, set the `sucmd` option to some non-existing
command, e.g. `aaa`, and try to save a file with root privileges.
2023-10-16 10:44:05 +02:00
Dmitry Maluka
9fabffc880
Fix issues with handling invalid regex in syntax files (#2913)
* Fix panic due to invalid regex in a syntax file

When a user's custom syntax file has a malformed filename regex or
header regex, MakeHeaderYaml() returns error but we do not properly
handle it, which results in a panic due to a dereference of the `header`
pointer which is nil:

Micro encountered an error: runtime.errorString runtime error: invalid memory address or nil pointer dereference
runtime/panic.go:221 (0x44c367)
runtime/panic.go:220 (0x44c337)
github.com/zyedidia/micro/v2/internal/buffer/buffer.go:709 (0x82bc0f)
github.com/zyedidia/micro/v2/internal/buffer/buffer.go:392 (0x828292)
github.com/zyedidia/micro/v2/internal/buffer/buffer.go:261 (0x8278c8)
github.com/zyedidia/micro/v2/cmd/micro/micro.go:203 (0x8b9e7b)
github.com/zyedidia/micro/v2/cmd/micro/micro.go:331 (0x8ba9e5)
runtime/proc.go:255 (0x4386a7)
runtime/asm_amd64.s:1581 (0x467941)

* Do not ignore invalid filename regex error in a syntax file

When the filename regex in a syntax file is malformed but the subsequent
header regex is correct, the filename regex error gets silently ignored,
since the `err` value is overwritten by the subsequent successful header
regex result.
2023-09-09 19:17:23 -07:00
Jöran Karl
a78c2c3509
actions: Fix the iteration over a slice under modification in QuitAll() (#2898) 2023-08-31 12:53:33 +01:00
Jöran Karl
c46467b5b9
plugins: Add capability to dis-/enable them per buffer (#2836) 2023-06-05 17:38:33 -07:00
rfjakob
1b4f6ecb12
save: fsync data safely to disk (#2681)
On modern Linux systems, it can take 30 seconds for
the data to actually hit the disk (check
/proc/sys/vm/dirty_expire_centisecs).

If the computer crashes in those 30 seconds, the user
may end up with an empty file as seen here:
https://github.com/neovim/neovim/issues/9888

This is why editors like vim and nano call
the fsync syscall after they wrote the file.
This syscall is available as file.Sync() in Go.

Running strace against micro shows that fsync is
called as expected:

	$ strace -f -p $(pgrep micro) -e fsync
	strace: Process 3284344 attached with 9 threads
	[pid 3284351] fsync(8)                  = 0

Also, we now catch errors returned from w.Flush().
2023-05-25 22:21:19 -07:00
Ilya Grigoriev
651a30105b
Goto next/previous diff commands + minor cleanups (#2759)
* Comment fix & gofmt fix

* Goto next/previous diff commands

These commands will work in `git` repositories or whenever `set diff on` is
working. They are bound to `Alt-[` and `Alt-]` by default. I would prefer
`Alt-Up` and `Alt-Down`, but that's already taken.

There are no tests at the moment; I'm looking into writing some since that will
be needed for the rest of the plan to make
https://github.com/zyedidia/micro/discussions/2753 a reality. I'm not sure how
difficult that will be.

* Realign JSON in keybindings.md
2023-04-20 15:23:35 -07:00
Zachary Yedidia
3ef0267f0a Reload settings when filetype is changed
Fixes #2712
2023-01-26 18:21:35 -08:00
Zachary Yedidia
2e9dabd434 Fix save with sudo for large file sizes
Fixes #2200
2022-11-02 16:00:12 -07:00
Zachary Yedidia
d2dca2b6c3 Fix 2021-11-22 18:46:36 -08:00
Zachary Yedidia
0e63224dea Use abspath for local glob settings
Ref #2276
2021-11-22 18:45:19 -08:00
Dmitry Maluka
ffbb257434
Support for highlighting all search matches (hlsearch) (#1762)
* Support for highlighting all search matches (hlsearch)

hlsearch is implemented efficiently using the buffer's line array,
somewhat similarly to the syntax highlighting.
Unlike the syntax highlighter which highlights the entire file,
hlsearch searches for matches for the displayed lines only.
Matches are searched when the given line is displayed first time
or after it was modified. Otherwise the previously found matches
are used.

* Add UnhighlightSearch action

and add it to the list of actions triggered by Esc key by default.

* Add comment explaining the purpose of search map

* Add hlsearch colors to colorschemes

Mostly just copied from the corresponding original (mostly vim) colorschemes.

* Highlight matches during/after replace as well

As a side effect it also changes the last search value, i.e. affects FindNext
and FindPrevious, but it's probably fine. In vim it works the same way.

* Improve hlsearch option description
2021-09-28 13:39:03 -07:00
Zachary Yedidia
c44ccb8cc7 Merge 2021-08-21 17:58:35 -04:00
Zachary Yedidia
0914f158c2 Improve comments 2021-08-21 17:58:30 -04:00
Zachary Yedidia
dceddcfd83 Fix save with sudo auto-detection and sudo/doas message 2021-08-20 13:55:59 -04:00