For some time I have been thinking about ways of creating a usermenu which would work for a whole directory (sub)tree. E.g. in C:\Development and all of its subdirectories I would have a different menu that everywhere else. Turns out it's surprisingly easy:
Create a standard global usermenu (for "everywhere else").
In %FARPROFILE%\Menus, create usermenus for your specific directory trees. E.g. Development.ini and MenuForDriveD.ini for the example below. Their format is the same as for the standard usermenu.
In %FARPROFILE%\Macros\scripts, create a file TreeBasedUsermenu.lua with the following content (modified according to your requirements; note that the directory names must not end with a backslash):
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
Macro { area="Shell"; key="F2"; flags=""; action=function()
if string.starts(APanel.Path, "C:\\Development") then
mf.usermenu(3, "Development.ini")
elseif string.starts(APanel.Path, "D:") then
mf.usermenu(3, "MenuForDriveD.ini")
else
mf.usermenu(0, "")
end
end;
}
Your solution is good, but there are some things to note:
First, one can create "local" usermenus FarMenu.ini in the root directories of subtrees, e.g. place it into C:\Development. Then one can switch between the global and local usermenus with ShiftF2.
Second, do not use functions of string library - they are not unicode compatible. Instead of string.len(Start) use Start:len(), the same goes for sub, etc.
I know about the local usermenus, but I disliked the need to switch between the menus. Also, it seemed rather slow with huge directories (but I may have been mistaken here).
function string.starts(String,Start)
return String:sub(1,Start:len())==Start
end
Macro { area="Shell"; key="F2"; flags=""; action=function()
if string.starts(APanel.Path, "C:\\Development") then
mf.usermenu(3, "Development.ini")
elseif string.starts(APanel.Path, "D:") then
mf.usermenu(3, "MenuForDriveD.ini")
else
mf.usermenu(0, "")
end
end;
}
Shmuel wrote: ↑Sun 05 Nov, 2017 19:25
First, one can create "local" usermenus FarMenu.ini in the root directories of subtrees, e.g. place it into C:\Development. Then one can switch between the global and local usermenus with ShiftF2.
Macro { description="UserMenu: open nearest";
area="Shell Tree"; key="F2";
id="84B0CBF2-8790-44DC-A311-32104F03FE0F";
action=function()
mf.usermenu(1)
Keys"Enter"
while Object.Title:find"%(" do Keys"ShiftF2" end --skip Main menu (User/Global)
if not Menu.Value then Keys"ShiftF2" end --return to main if no local
end;
}
(Perhaps I should publish the complete set of my UserMenu macros).
Last edited by John Doe on Wed 08 Nov, 2017 10:33, edited 1 time in total.