zyedidia / micro

A modern and intuitive terminal-based text editor
https://micro-editor.github.io
MIT License
24.96k stars 1.17k forks source link

`replace` and `replaceall` use escape characters like `\t` and `\n` #3126

Open dustdfg opened 8 months ago

dustdfg commented 8 months ago

Description of the problem or steps to reproduce

I try to use replace and replaceall for replacing \t and \n but they don't recognize them as escape characters and just print nothing matches <...>. To clarify how I use them replaceall '\n' '...'

  1. It is impossible to insert tab into infowindow because it handles tabs. (I found the workaround: I need to select somewhere tab character, copy it and then insert via ctrl+v). Just writing \t doesn't work
  2. It is impossible to insert new line into infowindow because it handles them as confirm action. The same workaround with pasting via ctrl+v leads to another issue https://github.com/zyedidia/micro/issues/3113. If I insert new line via pasting it the command will end with error because doesn't expect to see end of line...

Specifications

Commit hash: 59dda01c OS: Debian12 Terminal: foot

JoeKar commented 8 months ago

The reason is rather simple, because the whole search mechanism iterates over the lines which don't include the line feeds yet (see: https://github.com/zyedidia/micro/blob/master/internal/buffer/search.go#L151). These will be added in the moment the file is stored (see: https://github.com/zyedidia/micro/blob/master/internal/buffer/line_array.go#L174).

This currently doesn't allow the flexibility someone would expect. A larger rework, not only of the search and replace mechanism but the line handling instead would be needed. The tab (\t) is a different story :thinking:

dustdfg commented 8 months ago

The tab (\t) is a different story 🤔

I can't reproduce it now but I am absolutely sure that when I wrote replaceall '\t' '...' it didn't work. But I found another thing that if I write replaceall '...' '\t' it inserts not tab character but just a sequence \t

JoeKar commented 8 months ago

replaceall '\t' '...'

...was working for me, but...

replaceall '...' '\t'

...doesn't. But we've to be careful, since the search string is interpret as regex and thus the . should be escaped (e.g. \.\.\.) and the latter replace is interpret as literal (simple byte stream) instead of an regex expression. To be honest, the whole replace(all) functionality is a weird and not comprehensible thing (see #2963 too).