Skip to content

Commit

Permalink
Adding an option that enables the use of the RGB color format
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekopalypse committed Nov 1, 2023
1 parent 628fa17 commit e6321ae
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ endif


## Release History
* 1.4.0
* Adding an option that enables the use of the RGB color format.
* 1.3.1
* Support for assigning IndicatorIDs through Notepad++ (nppm_allocateindicator).
* 1.3.0
Expand Down
28 changes: 28 additions & 0 deletions config/config_reader.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mut:
pub struct Config {
pub mut:
all map[string]Lexer
use_rgb_format bool
}

pub fn read(config_file string) {
Expand Down Expand Up @@ -67,6 +68,15 @@ pub fn read(config_file string) {
p.regex_error_color = regex_error_color[1].trim(' ').int()
}
}
else if line_.starts_with('use_rgb_format') {
use_rgb_format := line_.split('=')
if use_rgb_format.len == 2 {
val := use_rgb_format[1].trim(' ').int()
if val == 1 {
p.lexers_to_enhance.use_rgb_format = true
}
}
}
else {
if line_.starts_with('excluded_styles') {
excludes := line_.split('=')
Expand Down Expand Up @@ -102,4 +112,22 @@ pub fn read(config_file string) {
if lexer.name != '' {
p.lexers_to_enhance.all[lexer.name] = lexer
}
if p.lexers_to_enhance.use_rgb_format {
// convert back to bgr format
for _, mut lexer__ in p.lexers_to_enhance.all {
for i, mut regex in lexer__.regexes {
bgr_color := rgb_to_bgr(regex.color)
regex.color = bgr_color
lexer__.regexes[i] = regex
}
}
}
}

pub fn rgb_to_bgr(rgb int) int {
red := (rgb >> 16) & 0xFF
green := (rgb >> 8) & 0xFF
blue := rgb & 0xFF
bgr := (blue << 16) | (green << 8) | red
return bgr
}
8 changes: 6 additions & 2 deletions notification_handler.v
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ fn (mut p Plugin) on_update(sci_hwnd voidptr) {
}
match true {
// keep config file check first since buffer_is_of_interest is true anyway
p.buffer_is_config_file { p.editor.style_config(p.active_scintilla_hwnd, p.indicator_id) }
p.buffer_is_config_file { p.editor.style_config(p.active_scintilla_hwnd, p.indicator_id, p.lexers_to_enhance.use_rgb_format) }
buffer_is_of_interest { p.style(hwnd, view) }
else{}
}
}

pub fn (mut p Plugin) on_modified(position isize) {
if p.buffer_is_config_file && (! p.npp.is_single_view() ) {
p.editor.highlight_match(p.active_scintilla_hwnd, position, p.indicator_id)
p.editor.highlight_match(p.active_scintilla_hwnd, position, p.indicator_id, p.lexers_to_enhance.use_rgb_format)
}
}

Expand Down Expand Up @@ -106,6 +106,9 @@ regex_error_style_id=30
; The color used by the style.
; For an explanation of how this color can be defined, see the following description of the regexes and their colors.
regex_error_color=0x756ce0
; Using the RGB format instead of the default BGR format.
; The expected values are 0 (BGR) or 1 (RGB)
use_rgb_format=0
; Each configured lexer must have a section with its name,
; (NOTE: use the menu function "Enhance current language" as it takes care of the correct naming)
Expand All @@ -120,6 +123,7 @@ regex_error_color=0x756ce0
; * blue goes in the biggest byte (0xFF0000)
; * this BGR order might conflict with your expectation of RGB order.
; * see Microsoft COLORREF documentation https://docs.microsoft.com/en-us/windows/win32/gdi/colorref
; If the RGB format is to be used, set the global variable use_rgb_format=1
; The optional whitelist is expected in the form of [1,3,16 ... ] which correspond to the style IDs of the current lexer.
; A whitelist is only useful if an excluded_styles line has been configured
Expand Down
29 changes: 24 additions & 5 deletions scintilla/scintilla.v
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub fn (e Editor) clear_regex_test(hwnd voidptr, indicator_id int) {
length := e.call(hwnd, sci_getlength, 0, 0)
e.call(hwnd, sci_setindicatorcurrent, usize(indicator_id), isize(0))
e.call(hwnd, sci_indicatorclearrange, 0, length)
e.call(hwnd, sci_annotationclearall, 0, 0)
}

fn (e Editor) set_search_target(hwnd voidptr, regex string, start_pos usize, end_pos usize) isize {
Expand Down Expand Up @@ -156,7 +157,7 @@ pub fn (e Editor) init_style(hwnd voidptr) {
e.call(hwnd, sci_stylesetitalic, usize(e.eol_error_style), 1)
}

pub fn (e Editor) style_config(hwnd voidptr, indicator_id int) {
pub fn (e Editor) style_config(hwnd voidptr, indicator_id int, use_rgb_format bool) {
mut first_visible_line := e.call(hwnd, sci_getfirstvisibleline, 0, 0)
first_visible_line = e.call(hwnd, sci_doclinefromvisible, usize(first_visible_line), 0)
lines_on_screen := e.call(hwnd, sci_linesonscreen, usize(0), 0)
Expand All @@ -172,8 +173,10 @@ pub fn (e Editor) style_config(hwnd voidptr, indicator_id int) {
// get the color text
range_pointer := charptr(e.call(hwnd, sci_getrangepointer, usize(found_pos), isize(length)))
color_text := unsafe { range_pointer.vstring_with_len(int(length)) }
color := color_text.replace('#', '0x').int()

mut color := color_text.replace('#', '0x').int()
if use_rgb_format {
color = config.rgb_to_bgr(color)
}
e.style_it(hwnd, usize(indicator_id), color, usize(found_pos), end-found_pos)
found_pos = e.set_search_target(hwnd, e.config_regex, usize(end), usize(end_pos))
}
Expand Down Expand Up @@ -207,7 +210,7 @@ pub fn (e Editor) goto_known_lexer(hwnd voidptr, search string) {
}
}

pub fn (e Editor) highlight_match(hwnd voidptr, position isize, indicator_id int) {
pub fn (e Editor) highlight_match(hwnd voidptr, position isize, indicator_id int, use_rgb_format bool) {
line := e.call(hwnd, sci_linefromposition, usize(position), 0)
start_pos := e.call(hwnd, sci_positionfromline, usize(line), 0)

Expand All @@ -226,7 +229,12 @@ pub fn (e Editor) highlight_match(hwnd voidptr, position isize, indicator_id int
split_pos := text.index('=') or { return }
if split_pos > 0 {
color_text := text[0..split_pos].trim(' ')
color := color_text.replace('#', '0x').int()
mut color := color_text.replace('#', '0x').int()
if color == 0 {
for c in color_text {
if c != `0` { return }
}
}
regex := text[split_pos..].trim_left('=').trim_space()
if regex.len == 0 { return }

Expand All @@ -238,9 +246,20 @@ pub fn (e Editor) highlight_match(hwnd voidptr, position isize, indicator_id int
e.add_error_annotation(hwnd, other_hwnd, line)
return
}
if use_rgb_format {
color = config.rgb_to_bgr(color)
}
for found_pos > -1 {
end := e.call(other_hwnd, sci_gettargetend, usize(0), isize(0))
e.style_it(other_hwnd, usize(indicator_id), color, usize(found_pos), end-found_pos)
current_style := int(e.call(other_hwnd, sci_getstyleat, usize(found_pos), 0))
if current_style > 0 {
other_line := e.call(other_hwnd, sci_linefromposition, usize(found_pos), 0)
e.call(other_hwnd, sci_annotationsetstyle, usize(other_line), e.eol_error_style)
buffer := 'This match is not considered if id ${current_style} is included in the excluded_styles list'
e.call(other_hwnd, sci_annotationsettext, usize(other_line), isize(buffer.str))
e.call(other_hwnd, sci_annotationsetvisible, annotation_boxed, 0)
}
found_pos = e.set_search_target(other_hwnd, regex, usize(end), usize(end_pos))
}
}
Expand Down
4 changes: 2 additions & 2 deletions version.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define VER_VERSION 1,3,1,0
#define VER_VERSION_STR "1.3.1.0\0"
#define VER_VERSION 1,4,0,0
#define VER_VERSION_STR "1.4.0.0\0"

#define VER_COMPANYNAME_STR "Ekopalypse\0"
#define VER_FILEDESCRIPTION_STR "Enhance any lexer plugin for Notepad++\0"
Expand Down

0 comments on commit e6321ae

Please sign in to comment.