search.lua 1006 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. local search = {}
  2. local default_opt = {}
  3. local function pattern_lower(str)
  4. if str:sub(1, 1) == "%" then
  5. return str
  6. end
  7. return str:lower()
  8. end
  9. local function init_args(doc, line, col, text, opt)
  10. opt = opt or default_opt
  11. line, col = doc:sanitize_position(line, col)
  12. if opt.no_case then
  13. if opt.pattern then
  14. text = text:gsub("%%?.", pattern_lower)
  15. else
  16. text = text:lower()
  17. end
  18. end
  19. return doc, line, col, text, opt
  20. end
  21. function search.find(doc, line, col, text, opt)
  22. doc, line, col, text, opt = init_args(doc, line, col, text, opt)
  23. for line = line, #doc.lines do
  24. local line_text = doc.lines[line]
  25. if opt.no_case then
  26. line_text = line_text:lower()
  27. end
  28. local s, e = line_text:find(text, col, not opt.pattern)
  29. if s then
  30. return line, s, line, e + 1
  31. end
  32. col = 1
  33. end
  34. if opt.wrap then
  35. opt = { no_case = opt.no_case, pattern = opt.pattern }
  36. return search.find(doc, 1, 1, text, opt)
  37. end
  38. end
  39. return search