search.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. -- helpers for the search bar (C-f)
  2. function Text.draw_search_bar(State)
  3. local h = State.line_height+2
  4. local y = App.screen.height-h
  5. love.graphics.setColor(0.9,0.9,0.9)
  6. love.graphics.rectangle('fill', 0, y-10, App.screen.width-1, h+8)
  7. love.graphics.setColor(0.6,0.6,0.6)
  8. love.graphics.line(0, y-10, App.screen.width-1, y-10)
  9. love.graphics.setColor(1,1,1)
  10. love.graphics.rectangle('fill', 20, y-6, App.screen.width-40, h+2, 2,2)
  11. love.graphics.setColor(0.6,0.6,0.6)
  12. love.graphics.rectangle('line', 20, y-6, App.screen.width-40, h+2, 2,2)
  13. App.color(Text_color)
  14. App.screen.print(State.search_term, 25,y-5)
  15. Text.draw_cursor(State, 25+State.font:getWidth(State.search_term),y-5)
  16. end
  17. function Text.search_next(State)
  18. -- search current line from cursor
  19. local curr_pos = State.cursor1.pos
  20. local curr_line = State.lines[State.cursor1.line].data
  21. local curr_offset = Text.offset(curr_line, curr_pos)
  22. local offset = find(curr_line, State.search_term, curr_offset, --[[literal]] true)
  23. if offset then
  24. State.cursor1.pos = utf8.len(curr_line, 1, offset)
  25. end
  26. if offset == nil then
  27. -- search lines below cursor
  28. for i=State.cursor1.line+1,#State.lines do
  29. local curr_line = State.lines[i].data
  30. offset = find(curr_line, State.search_term, --[[from start]] nil, --[[literal]] true)
  31. if offset then
  32. State.cursor1 = {line=i, pos=utf8.len(curr_line, 1, offset)}
  33. break
  34. end
  35. end
  36. end
  37. if offset == nil then
  38. -- wrap around
  39. for i=1,State.cursor1.line-1 do
  40. local curr_line = State.lines[i].data
  41. offset = find(curr_line, State.search_term, --[[from start]] nil, --[[literal]] true)
  42. if offset then
  43. State.cursor1 = {line=i, pos=utf8.len(curr_line, 1, offset)}
  44. break
  45. end
  46. end
  47. end
  48. if offset == nil then
  49. -- search current line until cursor
  50. local curr_line = State.lines[State.cursor1.line].data
  51. offset = find(curr_line, State.search_term, --[[from start]] nil, --[[literal]] true)
  52. local pos = utf8.len(curr_line, 1, offset)
  53. if pos and pos < State.cursor1.pos then
  54. State.cursor1.pos = pos
  55. end
  56. end
  57. if offset == nil then
  58. State.cursor1.line = State.search_backup.cursor.line
  59. State.cursor1.pos = State.search_backup.cursor.pos
  60. State.screen_top1.line = State.search_backup.screen_top.line
  61. State.screen_top1.pos = State.search_backup.screen_top.pos
  62. end
  63. if Text.lt1(State.cursor1, State.screen_top1) or Text.lt1(State.screen_bottom1, State.cursor1) then
  64. State.screen_top1.line = State.cursor1.line
  65. local pos = Text.pos_at_start_of_screen_line(State, State.cursor1)
  66. State.screen_top1.pos = pos
  67. end
  68. end
  69. function Text.search_previous(State)
  70. -- search current line before cursor
  71. local curr_pos = State.cursor1.pos
  72. local curr_line = State.lines[State.cursor1.line].data
  73. local curr_offset = Text.offset(curr_line, curr_pos)
  74. local offset = rfind(curr_line, State.search_term, curr_offset-1, --[[literal]] true)
  75. if offset then
  76. State.cursor1.pos = utf8.len(curr_line, 1, offset)
  77. end
  78. if offset == nil then
  79. -- search lines above cursor
  80. for i=State.cursor1.line-1,1,-1 do
  81. local curr_line = State.lines[i].data
  82. offset = rfind(curr_line, State.search_term, --[[from end]] nil, --[[literal]] true)
  83. if offset then
  84. State.cursor1 = {line=i, pos=utf8.len(curr_line, 1, offset)}
  85. break
  86. end
  87. end
  88. end
  89. if offset == nil then
  90. -- wrap around
  91. for i=#State.lines,State.cursor1.line+1,-1 do
  92. local curr_line = State.lines[i].data
  93. offset = rfind(curr_line, State.search_term, --[[from end]] nil, --[[literal]] true)
  94. if offset then
  95. State.cursor1 = {line=i, pos=utf8.len(curr_line, 1, offset)}
  96. break
  97. end
  98. end
  99. end
  100. if offset == nil then
  101. -- search current line after cursor
  102. local curr_line = State.lines[State.cursor1.line].data
  103. offset = rfind(curr_line, State.search_term, --[[from end]] nil, --[[literal]] true)
  104. local pos = utf8.len(curr_line, 1, offset)
  105. if pos and pos > State.cursor1.pos then
  106. State.cursor1.pos = pos
  107. end
  108. end
  109. if offset == nil then
  110. State.cursor1.line = State.search_backup.cursor.line
  111. State.cursor1.pos = State.search_backup.cursor.pos
  112. State.screen_top1.line = State.search_backup.screen_top.line
  113. State.screen_top1.pos = State.search_backup.screen_top.pos
  114. end
  115. if Text.lt1(State.cursor1, State.screen_top1) or Text.lt1(State.screen_bottom1, State.cursor1) then
  116. State.screen_top1.line = State.cursor1.line
  117. local pos = Text.pos_at_start_of_screen_line(State, State.cursor1)
  118. State.screen_top1.pos = pos
  119. end
  120. end
  121. function find(s, pat, i, plain)
  122. if s == nil then return end
  123. return s:find(pat, i, plain)
  124. end
  125. -- TODO: avoid the expensive reverse() operations
  126. -- Particularly if we only care about literal matches, we don't need all of string.find
  127. function rfind(s, pat, i, plain)
  128. if s == nil then return end
  129. if #pat == 0 then return #s end
  130. local rs = s:reverse()
  131. local rpat = pat:reverse()
  132. if i == nil then i = #s end
  133. local ri = #s - i + 1
  134. local rendpos = rs:find(rpat, ri, plain)
  135. if rendpos == nil then return nil end
  136. local endpos = #s - rendpos + 1
  137. assert (endpos >= #pat, ('rfind: endpos %d should be >= #pat %d at this point'):format(endpos, #pat))
  138. return endpos-#pat+1
  139. end
  140. function test_rfind()
  141. check_eq(rfind('abc', ''), 3, 'empty pattern')
  142. check_eq(rfind('abc', 'c'), 3, 'final char')
  143. check_eq(rfind('acbc', 'c', 3), 2, 'previous char')
  144. check_nil(rfind('abc', 'd'), 'missing char')
  145. check_nil(rfind('abc', 'c', 2), 'no more char')
  146. end