about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-07-31 10:26:19 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-07-31 10:26:19 -0700
commit3b88c6540d3a3571167fd4d624a78ea0f1deb05d (patch)
tree8d8a4f50ee64181effa40671213f9620946c5221
parentf72fe2f172a37248026ade3a07b77ff2d1c7282c (diff)
parentb93c0cd6cc8231f80dd1974224a65cdfd205c76f (diff)
downloadtext.love-3b88c6540d3a3571167fd4d624a78ea0f1deb05d.tar.gz
Merge lines.love
-rw-r--r--source_text.lua43
-rw-r--r--text.lua36
2 files changed, 47 insertions, 32 deletions
diff --git a/source_text.lua b/source_text.lua
index 1a42310..5bc8942 100644
--- a/source_text.lua
+++ b/source_text.lua
@@ -24,8 +24,9 @@ function Text.draw(State, line_index, y, startpos, hide_cursor)
       local screen_line = Text.screen_line(line, line_cache, i)
 --?       print('text.draw:', screen_line, 'at', line_index,pos, 'after', x,y)
       local frag_len = utf8.len(screen_line)
-      -- render any link decorations
+      -- render any highlights
       for _,link_offsets in ipairs(line_cache.link_offsets) do
+        -- render link decorations
         local s,e,filename = unpack(link_offsets)
         local lo, hi = Text.clip_wikiword_with_screen_line(line, line_cache, i, s, e)
         if lo then
@@ -39,11 +40,33 @@ function Text.draw(State, line_index, y, startpos, hide_cursor)
           })
         end
       end
-      -- render fragment
       if State.selection1.line then
         local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
         Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
       end
+      if not hide_cursor and line_index == State.cursor1.line then
+        -- render search highlight or cursor
+        if State.search_term then
+          local data = State.lines[State.cursor1.line].data
+          local cursor_offset = Text.offset(data, State.cursor1.pos)
+          if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
+            local save_selection = State.selection1
+            State.selection1 = {line=line_index, pos=State.cursor1.pos+utf8.len(State.search_term)}
+            local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
+            Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
+            State.selection1 = save_selection
+          end
+        elseif Focus == 'edit' then
+          if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
+            Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
+          elseif pos + frag_len == State.cursor1.pos then
+            -- Show cursor at end of line.
+            -- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
+            -- It seems useful to see a cursor whether your eye is on the left or right margin.
+            Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
+          end
+        end
+      end
       -- render colorized text
       local x = State.left
       for frag in screen_line:gmatch('%S*%s*') do
@@ -51,22 +74,6 @@ function Text.draw(State, line_index, y, startpos, hide_cursor)
         App.screen.print(frag, x,y)
         x = x+App.width(frag)
       end
-      -- render cursor if necessary
-      if not hide_cursor and line_index == State.cursor1.line then
-        if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
-          if State.search_term then
-            local data = State.lines[State.cursor1.line].data
-            local cursor_offset = Text.offset(data, State.cursor1.pos)
-            if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
-              local lo_px = Text.draw_highlight(State, line, State.left,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term))
-              App.color(Text_color)
-              love.graphics.print(State.search_term, State.left+lo_px,y)
-            end
-          elseif Focus == 'edit' then
-            Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
-          end
-        end
-      end
       y = y + State.line_height
       if y >= App.screen.height then
         break
diff --git a/text.lua b/text.lua
index a51ca4c..84339d4 100644
--- a/text.lua
+++ b/text.lua
@@ -22,29 +22,37 @@ function Text.draw(State, line_index, y, startpos)
       local screen_line = Text.screen_line(line, line_cache, i)
 --?       print('text.draw:', screen_line, 'at', line_index,pos, 'after', x,y)
       local frag_len = utf8.len(screen_line)
-      -- render fragment
+      -- render any highlights
       if State.selection1.line then
         local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
         Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
       end
-      App.color(Text_color)
-      App.screen.print(screen_line, State.left,y)
-      -- render cursor if necessary
       if line_index == State.cursor1.line then
-        if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
-          if State.search_term then
-            local data = State.lines[State.cursor1.line].data
-            local cursor_offset = Text.offset(data, State.cursor1.pos)
-            if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
-              local lo_px = Text.draw_highlight(State, line, State.left,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term))
-              App.color(Text_color)
-              love.graphics.print(State.search_term, State.left+lo_px,y)
-            end
-          else
+        -- render search highlight or cursor
+        if State.search_term then
+          local data = State.lines[State.cursor1.line].data
+          local cursor_offset = Text.offset(data, State.cursor1.pos)
+          if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
+            local save_selection = State.selection1
+            State.selection1 = {line=line_index, pos=State.cursor1.pos+utf8.len(State.search_term)}
+            local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
+            Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
+            State.selection1 = save_selection
+          end
+        else
+          if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
+            Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
+          elseif pos + frag_len == State.cursor1.pos then
+            -- Show cursor at end of line.
+            -- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
+            -- It seems useful to see a cursor whether your eye is on the left or right margin.
             Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
           end
         end
       end
+      -- render fragment
+      App.color(Text_color)
+      App.screen.print(screen_line, State.left,y)
       y = y + State.line_height
       if y >= App.screen.height then
         break