about summary refs log tree commit diff stats
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
parentfdb35ce12bd8b2e30522ada07ec756a626d01a09 (diff)
downloadtext.love-516944b57215db43e20678b7462a46c1beae99ea.tar.gz
support hyperlinks in the source editor
Integrated from the pensieve fork.
-rw-r--r--icons.lua6
-rw-r--r--source_edit.lua1
-rw-r--r--source_text.lua33
3 files changed, 40 insertions, 0 deletions
diff --git a/icons.lua b/icons.lua
index 175fb13..e80f771 100644
--- a/icons.lua
+++ b/icons.lua
@@ -8,6 +8,12 @@ function icon.insert_drawing(button_params)
   love.graphics.line(10,y, 10,y+12)
 end
 
+function icon.hyperlink_decoration(button_params)
+  local x,y = button_params.x, button_params.y
+  App.color(Hyperlink_decoration_color)
+  love.graphics.line(x,y+Editor_state.line_height, x+button_params.w,y+Editor_state.line_height)
+end
+
 function icon.freehand(x, y)
   love.graphics.line(x+4,y+7,x+5,y+5)
   love.graphics.line(x+5,y+5,x+7,y+4)
diff --git a/source_edit.lua b/source_edit.lua
index 65f00a2..6676b42 100644
--- a/source_edit.lua
+++ b/source_edit.lua
@@ -1,6 +1,7 @@
 -- some constants people might like to tweak
 Text_color = {r=0, g=0, b=0}
 Cursor_color = {r=1, g=0, b=0}
+Hyperlink_decoration_color = {r=0.4, g=0.4, b=1}
 Stroke_color = {r=0, g=0, b=0}
 Current_stroke_color = {r=0.7, g=0.7, b=0.7}  -- in process of being drawn
 Current_name_background_color = {r=1, g=0, b=0, a=0.1}  -- name currently being edited
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