orbitalquark / textadept

Textadept is a fast, minimalist, and remarkably extensible cross-platform text editor for programmers.
https://orbitalquark.github.io/textadept
MIT License
640 stars 38 forks source link

How to color or style text #411

Closed GreedyTactician closed 1 year ago

GreedyTactician commented 1 year ago

Ideally, I just want something like color_text(start_pos, end_pos, color)

I notice there are styles. So there is view.style_fore, view.style_size, etc. These, as I understand, are tables that act as a list to manually define styles.

Then, the documentation suggests that I call buffer:start_styling and buffer:set_styling to color text. So presumably, I can:

view.style_fore[1] = 0x000099
buffer:start_styling(1, 0) --at the beginning of the buffer
buffer:set_styling(5, 1) --colors the 5 first characters of the buffer red

But it seems anytime I call buffer:set_styling, it crashes textadept.

Did I properly understand the documentation? Is this a bug? Does it also crashes your textadept?

orbitalquark commented 1 year ago

Manually styling text should only be done if there is no lexer present, as the lexer will simply overwrite you changes when text changes. If the text you want to highlight has a lexer, you can use indicators instead to mark up text (https://orbitalquark.github.io/textadept/api.html#mark-text-with-indicators), in your case you might want to use view.INDIC_TEXT_FORE.

Otherwise, the code you posted should work. It doesn't crash for me on my latest Textadept 12.0 nightly build. I don't know if it works on 11.4, though. Much has changed in the mean-time.

ClaudioGi commented 1 year ago

I have tried the code above on Linux Mint 21 Cinnamon with the 12.0 version of Textadept and a buffer without syntax highlighting. It did not throw an error, but the expected change of text color did not happen. So there seems to be something wrong with the expectation that it should work on the current buffer if I run the code using the Ctrl+e sequence for running a Lua command on a current buffer.

rgieseke commented 1 year ago

Not sure what the context is of what you are trying to achieve, but maybe Textredux can help?

https://github.com/rgieseke/textredux/

orbitalquark commented 1 year ago

I have tried the code above on Linux Mint 21 Cinnamon with the 12.0 version of Textadept and a buffer without syntax highlighting. It did not throw an error, but the expected change of text color did not happen. So there seems to be something wrong with the expectation that it should work on the current buffer if I run the code using the Ctrl+e sequence for running a Lua command on a current buffer.

You need to style the entire buffer at a time. Otherwise, Textadept will ask the lexer to complete styling, and that will likely override any manual styling you have done. This is why using indicators is a better method. If you try the code in the original post on a buffer with exactly 5 characters in it (e.g. "fooba"), it will highlight red.

oOosys commented 1 year ago

To achieve the effect of red coloring on a buffer with exactly 5 characters it is necessary to put the provided code into the user init.lua and trigger it on some key shortcut. It worked exactly as suggested (Textadept 12 GTK, Linux Mint 21.2 Xfce) coloring on keyboard shortcut the buffer red (but only in case of a buffer with exactly five characters of content). By the way (@orbitalquark ) may you provide some example code for usage of indicators? I have hard time to guess how the code need to look like which styles specified text section with custom style using indicators ... the linked api.html need in my eyes a code example illustrating usage case for the available API functions.

oOosys commented 1 year ago

@GreedyTactician : do it work for you as it has worked for me (see my comment above)? If it does, please mark the issue as closed as it is now clear what works, what does not and why it doesn't.

orbitalquark commented 1 year ago

By the way (@orbitalquark ) may you provide some example code for usage of indicators? I have hard time to guess how the code need to look like which styles specified text section with custom style using indicators ... the linked api.html need in my eyes a code example illustrating usage case for the available API functions.

Here's an example of how to highlight in blue all hyperlinks in the buffer:

-- Define hyperlink indicator.
local INDIC_LINK = _SCINTILLA.next_indic_number()
events.connect(events.VIEW_NEW, function()
    view.indic_hover_style[INDIC_LINK] = view.INDIC_TEXTFORE
    view.indic_hover_fore[INDIC_LINK] = 0xFF0000 -- blue
end)

-- Search the buffer and mark hyperlinks.
function mark_hyperlinks()
    local text = buffer:get_text()
    buffer.indicator_current = INDIC_LINK
    buffer:indicator_clear_range(1, buffer.length)
    for s, e in text:gmatch('()https?://%S+()') do
        buffer:indicator_fill_range(s, e - s)
    end
end
GreedyTactician commented 1 year ago

Feel free to close the issue. On Sep. 5, 2023 02:20, oOosys @.***> wrote: @GreedyTactician : do it work for you as it has worked for me (see my comment above)? If it does, please mark the issue as closed as it is now clear what works, what does not and why it doesn't.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>