Page 1 of 1

[Solved] Directory tree based usermenu

Posted: Sun 05 Nov, 2017 19:03
by pepak
Hi!

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:
  1. Create a standard global usermenu (for "everywhere else").
  2. 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.
  3. 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):

Code: Select all

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;
}
That's it!

Pepak

[Solved] Directory tree based usermenu

Posted: Sun 05 Nov, 2017 19:25
by Shmuel
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.

[Solved] Directory tree based usermenu

Posted: Sun 05 Nov, 2017 19:33
by pepak
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).

I will switch the code as you suggested.

[Solved] Directory tree based usermenu

Posted: Sun 05 Nov, 2017 19:36
by pepak
Fixed version as per Shmuel's suggestion:

Code: Select all

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;
}

[Solved] Directory tree based usermenu

Posted: Sun 05 Nov, 2017 22:33
by John Doe
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.
Here is my solution for this task:

Code: Select all

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).

[Solved] Directory tree based usermenu

Posted: Mon 06 Nov, 2017 08:23
by Konstantin
John Doe wrote: Sun 05 Nov, 2017 22:33(Perhaps I should publish the complete set of my UserMenu macros).
Of course, this would be useful!

[Solved] Directory tree based usermenu

Posted: Wed 08 Nov, 2017 10:37
by John Doe
Ok, I will, after some cleanup.
For now see improved version of the macro above.

[Solved] Directory tree based usermenu

Posted: Tue 04 Apr, 2023 16:53
by Konstantin
John Doe,
Previously, you considered being able to publish a set of your UserMenu macros. If this is not a burden to you, make them public, please.

Lately, I've been particularly interested in a macro that would call a local UserMenu located in the %FarHome%/Plugins folder and act (called) on the contents of the plugin subfolders.

[Solved] Directory tree based usermenu

Posted: Tue 04 Apr, 2023 17:15
by HaRT
 ! Message from: HaRT
Konstantin, please edit your post so that it's in English, because it is in the English-language subforum.

[Solved] Directory tree based usermenu

Posted: Tue 04 Apr, 2023 17:19
by John Doe
I never got those macros working right.
What exactly does not work as it should, I do no longer remember.
LocalMenu.lua:

[Solved] Directory tree based usermenu

Posted: Tue 04 Apr, 2023 20:37
by Konstantin
John Doe wrote: Tue 04 Apr, 2023 17:19 I never got those macros working right.
However, almost all macros from LocalMenu.lua work for me.

I especially liked the one that quickly shows the full name of the active UserMenu-file.