diff options
-rwxr-xr-x | core/locale.conf | 1 | ||||
-rw-r--r-- | modules/textadept/editing.lua | 69 | ||||
-rw-r--r-- | modules/textadept/menu.lua | 3 |
3 files changed, 61 insertions, 12 deletions
diff --git a/core/locale.conf b/core/locale.conf index c83a8023..1c58b164 100755 --- a/core/locale.conf +++ b/core/locale.conf @@ -85,6 +85,7 @@ Match _Brace = Match _Brace Select t_o Brace = Select t_o Brace Complete _Word = Complete _Word De_lete Word = De_lete Word +_Highlight Word = _Highlight Word Tran_spose Characters = Tran_spose Characters _Join Lines = _Join Lines Convert _Indentation = Convert _Indentation diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua index e3b19e80..d611e982 100644 --- a/modules/textadept/editing.lua +++ b/modules/textadept/editing.lua @@ -21,12 +21,25 @@ module('_m.textadept.editing', package.seeall) -- previous line. -- * `SAVE_STRIPS_WS`: Flag indicating whether or not to strip trailing -- whitespace on file save. +-- * `MARK_HIGHLIGHT`: The unique integer mark used to identify a line +-- containing a highlighted word. +-- * `MARK_HIGHLIGHT_BACK`: The [Scintilla color][scintilla_color] used for a +-- line containing a highlighted word. +-- * `INDIC_HIGHLIGHT`: The unique integer indicator for highlighted words. +-- * `INDIC_HIGHLIGHT_BACK`: The [Scintilla color][scintilla_color] used for a +-- highlighted word. +-- * `INDIC_HIGHLIGHT_ALPHA`: The transparency used for a highlighted word. -- settings AUTOPAIR = true HIGHLIGHT_BRACES = true AUTOINDENT = true SAVE_STRIPS_WS = true +MARK_HIGHLIGHT = 2 +MARK_HIGHLIGHT_BACK = 0xEEEEEE +INDIC_HIGHLIGHT = 8 -- INDIC_CONTAINER +INDIC_HIGHLIGHT_BACK = 0xC08040 +INDIC_HIGHLIGHT_ALPHA = 40 -- end settings --- @@ -490,18 +503,50 @@ function convert_indentation() buffer:end_undo_action() end --- Returns the number to the left of the caret. --- This is used for the enclose function. --- @see enclose -get_preceding_number = function() +-- Clears highlighted words. +local function clear_highlighted_words() local buffer = buffer - local caret = buffer.current_pos - local char = buffer.char_at[caret - 1] - local txt = '' - while tonumber(string.char(char)) do - txt = txt..string.char(char) - caret = caret - 1 - char = buffer.char_at[caret - 1] + buffer:marker_delete_all(MARK_HIGHLIGHT) + buffer.indicator_current = INDIC_HIGHLIGHT + buffer:indicator_clear_range(0, buffer.length) +end + +--- +-- Highlights all occurances of the word under the caret. +function highlight_word() + clear_highlighted_words() + local buffer = buffer + local s, e = buffer.anchor, buffer.current_pos + if s == e then + s, e = buffer:word_start_position(s), buffer:word_end_position(s) end - return tonumber(txt) or 1, #txt + local word = buffer:text_range(s, e) + if word == '' then return end + buffer.search_flags = _SCINTILLA.constants.SCFIND_WHOLEWORD + + _SCINTILLA.constants.SCFIND_MATCHCASE + buffer.target_start = 0 + buffer.target_end = buffer.length + while buffer:search_in_target(word) > 0 do + local len = buffer.target_end - buffer.target_start + buffer:marker_add(buffer:line_from_position(buffer.target_start), + MARK_HIGHLIGHT) + buffer:indicator_fill_range(buffer.target_start, len) + buffer.target_start = buffer.target_end + buffer.target_end = buffer.length + end + buffer:set_sel(s, e) +end + +events.connect('keypress', + function(c) if c == 0xff1b then clear_highlighted_words() end end) -- Esc + +-- Sets view properties for highlighted words. +local function set_highlight_properties() + local buffer = buffer + buffer:marker_set_back(MARK_HIGHLIGHT, MARK_HIGHLIGHT_BACK) + buffer.indic_fore[INDIC_HIGHLIGHT] = INDIC_HIGHLIGHT_BACK + buffer.indic_style[INDIC_HIGHLIGHT] = _SCINTILLA.constants.INDIC_ROUNDBOX + buffer.indic_alpha[INDIC_HIGHLIGHT] = INDIC_HIGHLIGHT_ALPHA end +if buffer then set_highlight_properties() end +events.connect('view_new', set_highlight_properties) diff --git a/modules/textadept/menu.lua b/modules/textadept/menu.lua index 658b72b4..daf3f6df 100644 --- a/modules/textadept/menu.lua +++ b/modules/textadept/menu.lua @@ -38,6 +38,7 @@ local ID = { SELECT_TO_BRACE = 209, COMPLETE_WORD = 210, DELETE_WORD = 211, + HIGHLIGHT_WORD = 213, TRANSPOSE_CHARACTERS = 212, JOIN_LINES = 245, CONVERT_INDENTATION = 216, @@ -162,6 +163,7 @@ local menubar = { { L('Select t_o Brace'), ID.SELECT_TO_BRACE }, { L('Complete _Word'), ID.COMPLETE_WORD }, { L('De_lete Word'), ID.DELETE_WORD }, + { L('_Highlight Word'), ID.HIGHLIGHT_WORD }, { L('Tran_spose Characters'), ID.TRANSPOSE_CHARACTERS }, { L('_Join Lines'), ID.JOIN_LINES }, { L('Convert _Indentation'), ID.CONVERT_INDENTATION }, @@ -384,6 +386,7 @@ local actions = { [ID.SELECT_TO_BRACE] = { m_editing.match_brace, 'select' }, [ID.COMPLETE_WORD] = { m_editing.autocomplete_word, '%w_' }, [ID.DELETE_WORD] = { m_editing.current_word, 'delete' }, + [ID.HIGHLIGHT_WORD] = { m_editing.highlight_word }, [ID.TRANSPOSE_CHARACTERS] = { m_editing.transpose_chars }, [ID.JOIN_LINES] = { m_editing.join_lines }, [ID.CONVERT_INDENTATION] = { m_editing.convert_indentation }, |