about summary refs log tree commit diff stats
path: root/source_text.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-09-05 14:16:30 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-09-05 14:16:55 -0700
commit516944b57215db43e20678b7462a46c1beae99ea (patch)
treefab0042d94f4d4d308dc4018cd86c150ed23bba7 /source_text.lua
parentfdb35ce12bd8b2e30522ada07ec756a626d01a09 (diff)
downloadlines.love-516944b57215db43e20678b7462a46c1beae99ea.tar.gz
support hyperlinks in the source editor
Integrated from the pensieve fork.
Diffstat (limited to 'source_text.lua')
-rw-r--r--source_text.lua33
1 files changed, 33 insertions, 0 deletions
diff --git a/source_text.lua b/source_text.lua
index 9c1279a..733b25a 100644
--- a/source_text.lua
+++ b/source_text.lua
@@ -110,6 +110,20 @@ function Text.draw_wrapping_line(State, line_index, x,y, startpos)
         screen_line_starting_pos = pos
         x = State.left
       end
+      -- Make [[WikiWords]] (single word, all in one screen line) clickable.
+      local trimmed_word = rtrim(frag)  -- compute_fragments puts whitespace at the end
+      if starts_with(trimmed_word, '[[') and ends_with(trimmed_word, ']]') then
+        local filename = trimmed_word:gsub('^..(.*)..$', '%1')
+        if source.link_exists(State, filename) then
+          local filename_text = App.newText(love.graphics.getFont(), filename)
+          button(State, 'link', {x=x+App.width(to_text('[[')), y=y, w=App.width(filename_text), h=State.line_height, color={1,1,1},
+            icon = icon.hyperlink_decoration,
+            onpress1 = function()
+                         source.switch_to_file(filename)
+                        end,
+          })
+        end
+      end
       App.screen.draw(frag_text, x,y)
       -- render cursor if necessary
       if State.cursor1.pos and line_index == State.cursor1.line then
@@ -1610,12 +1624,23 @@ function Text.cursor_out_of_screen(State)
 --?   return Text.lt1(State.screen_bottom1, botline1)
 end
 
+function source.link_exists(State, filename)
+  if State.link_cache == nil then
+    State.link_cache = {}
+  end
+  if State.link_cache[filename] == nil then
+    State.link_cache[filename] = file_exists(filename)
+  end
+  return State.link_cache[filename]
+end
+
 function Text.redraw_all(State)
 --?   print('clearing fragments')
   State.line_cache = {}
   for i=1,#State.lines do
     State.line_cache[i] = {}
   end
+  State.link_cache = {}
 end
 
 function Text.clear_screen_line_cache(State, line_index)
@@ -1636,3 +1661,11 @@ end
 function rtrim(s)
   return s:gsub('%s+$', '')
 end
+
+function starts_with(s, sub)
+  return s:find(sub, 1, --[[no escapes]] true) == 1
+end
+
+function ends_with(s, sub)
+  return s:reverse():find(sub:reverse(), 1, --[[no escapes]] true) == 1
+end