File: lua-replace.lua

package info (click to toggle)
geany-plugins 2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,832 kB
  • sloc: ansic: 107,883; sh: 5,567; makefile: 1,531; sed: 16
file content (40 lines) | stat: -rw-r--r-- 887 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
--[[
  Replace text using Lua pattern syntax
--]]

function esc(s)
  s=s and s:gsub('"', '\\"') or ""
  assert(loadstring('rv="'..s..'"'))()
  return rv
end


if geany.count() > 0 then

  local dlg = dialog.new("Lua replace", {"Replace All", "Cancel"})

  dlg:heading(
    "          Replaces text using Lua pattern matching syntax.          ")
  dlg:hr()
  dlg:text("find", nil, "Old needle: ")
  dlg:text("repl", nil, "New needle: ")

  if (#geany.selection()>0) then
    dlg:group("scope", "sel", "Haystack:")
    dlg:radio("scope", "doc", "Document")
    dlg:radio("scope", "sel", "Selection")
  end

  local btn, results = dlg:run()

  if (btn==1) then
    if results.find then
      local func = (results.scope=="sel") and geany.selection or geany.text
      func(string.gsub(func(), esc(results.find), esc(results.repl) ))
    end
  end

else
  geany.message("No document!")
end