Macro for substring file search

Here you can discuss any topic concerning Far macro commands.
Post Reply
KlepetoX
Posts: 129
Joined: Sun 08 Jan, 2012 08:23
Location: Czech Republic
Has thanked: 2 times
Been thanked: 1 time

Macro for substring file search

Post by KlepetoX »

I was inspired by this topic: http://forum.farmanager.com/viewtopic.php?f=60&t=8435 and tried to write a macro with mask for searching files with mask _ANYWHERE_ in the filename instead of from the beginning. Simply, set the default mask "**.*" instead of "*.*". And if inputbox has not default value, then select all between the first and second asterisk.

The macro seems to work well, but can anybody suggest some improvements?

Code: Select all

local function GetSearchString()
  mf.clip(5,2)
  Keys('CtrlIns')     -- content of inputbox to clipboard
  local s=mf.clip(0)  -- load string from clipboard
  if s==0 then s="" end
  return s
end;

Macro {
  key="CtrlShiftF7";
  area="Shell";
  description="Substring search **.*";
  action=function()
    Keys('AltF7')
    local s=GetSearchString()
    if s=='*.*' then    -- if default value, replace it with **.* and set cursor to the second position
      Keys('Multiply Multiply . Multiply Home Right') return
    end
    if s:find('*') then                   -- is * in inptutbox?
      local b=mf.index(s,'*')+1           -- find the first *
      s=mf.substr(s,b)                    -- position of * in inptubox
      if not s:find('*') then return end  -- is the second *?
      local e=mf.index(s,'*')+b-1         -- find the second *
      Keys'Home'                          -- select all between **
      for i=1,b do Keys('Right') end
      for i=1,e do Keys('ShiftRight') end
    end
  end;
}

User avatar
John Doe
Бюрократ
Posts: 13801
Joined: Wed 27 Apr, 2005 20:42
Has thanked: 72 times
Been thanked: 425 times
Contact:

Re: Macro for substring file search

Post by John Doe »

Creative approach!

Suggestions:
  1. Code: Select all

    if s=='*.*' then    -- if default value, replace it with **.* and set cursor to the second position
          Keys('Multiply Multiply . Multiply Home Right') return
    end
    Do it simpler: if s=="*.*" then Keys "Home *"; return end
  2. lua string library functions are more powerfull than mf.index/mf.substr/etc
    You can do searching of asterisks positions with one simple regex, such as local a,b = s:cfind"(%*).-(%*)"
  3. GetSearchString is unneeded, just use Dlg.Getvalue()
  4. From your short description it is hard to figure out the purpose of macro. Please improve it in your first post.
KlepetoX
Posts: 129
Joined: Sun 08 Jan, 2012 08:23
Location: Czech Republic
Has thanked: 2 times
Been thanked: 1 time

Re: Macro for substring file search

Post by KlepetoX »

ok, thanks for your advice, now it is even more simple and works fine

Code: Select all

local function GetSearchString()
  mf.clip(5,2)
  Keys'CtrlIns'     -- content of inputbox to clipboard
  local s=mf.clip(0)  -- load string from clipboard
  if s==0 then s="" end
  return s
end;

Macro {
  key="CtrlShiftF7";
  area="Shell";
  description="Substring search **.*";
  action=function()
    Keys'AltF7' local s=GetSearchString()
    if s=='*.*' then Keys('Home *') return end  -- if default value, replace it with **.*
    local a,b=s:cfind"(%*).-(%*)"       -- find the first and second asterisk
    if a==nil or b==nil then return end -- if less than two asterisks, exit
    Keys'Home'                          -- now select all between asterisks
    for i=1,a do Keys'Right' end
    for i=1,b-a-1 do Keys'ShiftRight' end
  end;
}
User avatar
John Doe
Бюрократ
Posts: 13801
Joined: Wed 27 Apr, 2005 20:42
Has thanked: 72 times
Been thanked: 425 times
Contact:

Re: Macro for substring file search

Post by John Doe »

(Just added 2 more suggestions)

And here is another, about new revision of your macro:

Code: Select all

if a==nil or b==nil then return end
You can write that as following:

Code: Select all

if not (a and b) then return end
or

Code: Select all

  if a and b then
    Keys'Home'
    for i=1,a do Keys'Right' end
    for i=1,b-a-1 do Keys'ShiftRight' end
  end
KlepetoX
Posts: 129
Joined: Sun 08 Jan, 2012 08:23
Location: Czech Republic
Has thanked: 2 times
Been thanked: 1 time

Re: Macro for substring file search

Post by KlepetoX »

I wrote this macro for I mostly want to search for mask anywhere inside the filename. That is why I need two asterisks in mask by default. And if the mask is used from the previous searching I want to have the substring between asterisks selected for quick rewriting. Simply try the macro, I think that is very useful, much more than the event in this topic http://forum.farmanager.com/viewtopic.php?f=60&t=8435
  1. Macro {
  2.  
  3.   key="AltF7";
  4.  
  5.   area="Shell";
  6.  
  7.   description="Substring search **.*";
  8.  
  9.   action=function()
  10.  
  11.     Keys'AltF7' local s=Dlg.GetValue()
  12.  
  13.     if s=='*.*' then Keys('Home *') return end  -- if default mask, replace it with **.*
  14.  
  15.     local a,b=s:cfind"(%*).-(%*)"     -- find the first and second asterisk
  16.  
  17.     if a and b then                   -- if two asterisks exist in mask
  18.  
  19.       Keys'Home'                      -- now select all between asterisks
  20.  
  21.       for i=1,a do Keys'Right' end
  22.  
  23.       for i=1,b-a-1 do Keys'ShiftRight' end
  24.  
  25.     end
  26.  
  27.   end;
  28.  
  29. }
User avatar
John Doe
Бюрократ
Posts: 13801
Joined: Wed 27 Apr, 2005 20:42
Has thanked: 72 times
Been thanked: 425 times
Contact:

Re: Macro for substring file search

Post by John Doe »

KlepetoX wrote:I think that is very useful, much more than the event in this topic http://forum.farmanager.com/viewtopic.php?f=60&t=8435
Well, it is trivial to adapt that event handler to your exact needs.
But I can agree that writing macro is simpler than writing event handler )
Post Reply

Return to “Macro Commands Discussions”