~ubuntu-branches/debian/squeeze/geany-plugins/squeeze

« back to all changes in this revision

Viewing changes to geanylua/examples/edit/lua-replace.lua

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-07-10 22:56:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090710225641-xc1126t7pq0jmpos
Tags: upstream-0.17.1
ImportĀ upstreamĀ versionĀ 0.17.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--[[
 
2
  Replace text using Lua pattern syntax
 
3
--]]
 
4
 
 
5
function esc(s)
 
6
  s=s and s:gsub('"', '\\"') or ""
 
7
  assert(loadstring('rv="'..s..'"'))()
 
8
  return rv
 
9
end
 
10
 
 
11
 
 
12
if geany.count() > 0 then
 
13
 
 
14
  local dlg = dialog.new("Lua replace", {"Replace All", "Cancel"})
 
15
 
 
16
  dlg:heading(
 
17
    "          Replaces text using Lua pattern matching syntax.          ")
 
18
  dlg:hr()
 
19
  dlg:text("find", nil, "Old needle: ")
 
20
  dlg:text("repl", nil, "New needle: ")
 
21
 
 
22
  if (#geany.selection()>0) then
 
23
    dlg:group("scope", "sel", "Haystack:")
 
24
    dlg:radio("scope", "doc", "Document")
 
25
    dlg:radio("scope", "sel", "Selection")
 
26
  end
 
27
 
 
28
  local btn, results = dlg:run()
 
29
 
 
30
  if (btn==1) then
 
31
    if results.find then
 
32
      local func = (results.scope=="sel") and geany.selection or geany.text
 
33
      func(string.gsub(func(), esc(results.find), esc(results.repl) ))
 
34
    end
 
35
  end
 
36
 
 
37
else
 
38
  geany.message("No document!")
 
39
end
 
40