aboutsummaryrefslogtreecommitdiffhomepage
path: root/core/ui.lua
diff options
context:
space:
mode:
authorGravatar mitchell <70453897+667e-11@users.noreply.github.com>2020-07-19 13:01:49 -0400
committerGravatar mitchell <70453897+667e-11@users.noreply.github.com>2020-07-19 13:01:49 -0400
commit0c10d1b7a38e84b11c9e54c29bfda080ce05b89c (patch)
tree5e740fb88a1ed3ba32c5b3484898558fbc292cce /core/ui.lua
parent322e8af00ae62a42be1c6ca2a522c2ce6f5f418e (diff)
Make `ui.highlight_words` a multi-option setting.
Highlighting the current word could be useful instead of just the selected word.
Diffstat (limited to 'core/ui.lua')
-rw-r--r--core/ui.lua56
1 files changed, 37 insertions, 19 deletions
diff --git a/core/ui.lua b/core/ui.lua
index ce7aeb84..7aa15070 100644
--- a/core/ui.lua
+++ b/core/ui.lua
@@ -33,16 +33,25 @@ local ui = ui
-- with a group of [`ui.print()`]() and [`ui._print()`]() function calls.
-- The default value is `false`, and focuses buffers when messages are printed
-- to them.
--- @field highlight_words (bool)
--- Whether or not to automatically highlight all instances of the selected
--- word.
--- The default value is `true` except in the terminal version.
+-- @field highlight_words (number)
+-- The word highlight mode.
+--
+-- * `ui.HIGHLIGHT_CURRENT`
+-- Automatically highlight all instances of the current word.
+-- * `ui.HIGHLIGHT_SELECTED`
+-- Automatically highlight all instances of the selected word.
+-- * `ui.HIGHLIGHT_NONE`
+-- Do not automatically highlight words.
+--
+-- The default value is `ui.HIGHLIGHT_SELECTED` except in the terminal
+-- version, where it is `ui.HIGHLIGHT_NONE`.
-- @field INDIC_HIGHLIGHT (number)
-- The word highlight indicator number.
module('ui')]]
ui.silent_print = false
-ui.highlight_words = not CURSES
+ui.HIGHLIGHT_NONE, ui.HIGHLIGHT_CURRENT, ui.HIGHLIGHT_SELECTED = 1, 2, 3
+ui.highlight_words = not CURSES and ui.HIGHLIGHT_SELECTED or ui.HIGHLIGHT_NONE
ui.INDIC_HIGHLIGHT = _SCINTILLA.next_indic_number()
-- Helper function for printing messages to buffers.
@@ -300,21 +309,30 @@ events_connect(events.KEYPRESS, function(code)
if keys.KEYSYMS[code] == 'esc' then clear_highlighted_words() end
end, 1)
--- Highlight all instances of the selected word.
+-- Highlight all instances of the current or selected word.
events_connect(events.UPDATE_UI, function(updated)
- if updated and updated & buffer.UPDATE_SELECTION > 0 and
- ui.highlight_words and not buffer.selection_empty and
- buffer:is_range_word(buffer.selection_start, buffer.selection_end) and
- buffer:get_sel_text():find('^%S+$') then
- clear_highlighted_words()
- local word = buffer:text_range(buffer.selection_start, buffer.selection_end)
- buffer.search_flags = buffer.FIND_MATCHCASE | buffer.FIND_WHOLEWORD
- buffer:target_whole_document()
- while buffer:search_in_target(word) ~= -1 do
- buffer:indicator_fill_range(
- buffer.target_start, buffer.target_end - buffer.target_start)
- buffer:set_target_range(buffer.target_end, buffer.length + 1)
- end
+ if not updated or updated & buffer.UPDATE_SELECTION == 0 then return end
+ local word
+ if ui.highlight_words == ui.HIGHLIGHT_CURRENT then
+ local s = buffer:word_start_position(buffer.current_pos, true)
+ local e = buffer:word_end_position(buffer.current_pos, true)
+ if s == e then clear_highlighted_words() return end
+ word = buffer:text_range(s, e)
+ elseif ui.highlight_words == ui.HIGHLIGHT_SELECTED then
+ local s, e = buffer.selection_start, buffer.selection_end
+ if not buffer:is_range_word(s, e) then return end
+ word = buffer:get_sel_text()
+ if not word:find('^%S+$') then return end
+ else
+ return
+ end
+ clear_highlighted_words()
+ buffer.search_flags = buffer.FIND_MATCHCASE | buffer.FIND_WHOLEWORD
+ buffer:target_whole_document()
+ while buffer:search_in_target(word) ~= -1 do
+ buffer:indicator_fill_range(
+ buffer.target_start, buffer.target_end - buffer.target_start)
+ buffer:set_target_range(buffer.target_end, buffer.length + 1)
end
end)