Find string in buffer

Find string in buffer

Postby Constin » Fri Feb 22, 2013 12:55 pm


Pattern: ^bfind((\[(\d+)\])?) (.*)$


Code: Select all
-- usage:
--   bfind rat
--   bfind[2] rat
--   bdind ^lua
--   bfind[10] quest

-- config
local bfindMark = "bfind"  -- should be same as alias
local hist = 7500
local minL = 0

-- getting lines
if getLineCount() > hist then
   minL = getLineCount() - hist
local data = getLines(minL, getLineCount())

-- prepare for search
local pattern = string.genNocasePattern(matches[5])
local delta = nil
if matches[4] then
   delta = tonumber(matches[4])

-- looking for pattern in history
for i,line in ipairs(data) do
   local match = string.findPattern(line, pattern)
   if match then
      if not string.starts(line, bfindMark) then
         local hline = string.gsub(line, pattern, "<purple>" .. match .. "<lime_green>")   
         -- printing lines before match
         if delta then
             for ii=delta,1,-1 do
               if (i-ii) >= 1 then
                  cecho(string.format("<black>%s <dark_green>%4d: <lime_green>%s\n", bfindMark, (i-ii), data[i-ii]))
         -- printing match line
         cecho(string.format("<black>%s <medium_sea_green>%4d: <lime_green>%s\n", bfindMark, i, hline))
         -- printing lines after match
         if delta then
            for ii=1,delta do
               if (i+ii) <= hist then
                  cecho(string.format("<black>%s <dark_green>%4d: <lime_green>%s\n", bfindMark, (i+ii), data[i+ii]))
            -- printing separator
            cecho(string.format("<black>%s<dark_slate_grey>-----\n", bfindMark))

-- reseting color and adding final blank line
Posts: 14
Joined: Fri Jan 11, 2013 3:37 pm
Status: Offline

Return to Mudlet - unlimited possibilities

Who is online

Users browsing this forum: No registered users and 1 guest