[Solved] Directory tree based usermenu

You want to talk about Far Manager, but don't know where to turn?
Post Reply
pepak
Posts: 604
Joined: Sun 13 Jul, 2008 11:18
Has thanked: 17 times
Been thanked: 54 times

[Solved] Directory tree based usermenu

Post 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
Shmuel
Posts: 6819
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 41 times
Been thanked: 528 times

[Solved] Directory tree based usermenu

Post 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.
pepak
Posts: 604
Joined: Sun 13 Jul, 2008 11:18
Has thanked: 17 times
Been thanked: 54 times

[Solved] Directory tree based usermenu

Post 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.
pepak
Posts: 604
Joined: Sun 13 Jul, 2008 11:18
Has thanked: 17 times
Been thanked: 54 times

[Solved] Directory tree based usermenu

Post 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;
}
User avatar
John Doe
Бюрократ
Posts: 13807
Joined: Wed 27 Apr, 2005 20:42
Has thanked: 73 times
Been thanked: 426 times
Contact:

[Solved] Directory tree based usermenu

Post 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).
Last edited by John Doe on Wed 08 Nov, 2017 10:33, edited 1 time in total.
User avatar
Konstantin
Posts: 234
Joined: Wed 14 Jun, 2006 19:29
Location: Санкт-Петербург
Has thanked: 109 times
Been thanked: 5 times

[Solved] Directory tree based usermenu

Post 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!
«Раньше я думал, что чудаки это те, кто делают странные вещи. Теперь я знаю, что это те, кто называют других странными.» (П. Маккартни)
User avatar
John Doe
Бюрократ
Posts: 13807
Joined: Wed 27 Apr, 2005 20:42
Has thanked: 73 times
Been thanked: 426 times
Contact:

[Solved] Directory tree based usermenu

Post by John Doe »

Ok, I will, after some cleanup.
For now see improved version of the macro above.
User avatar
Konstantin
Posts: 234
Joined: Wed 14 Jun, 2006 19:29
Location: Санкт-Петербург
Has thanked: 109 times
Been thanked: 5 times

[Solved] Directory tree based usermenu

Post 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.
Last edited by Konstantin on Tue 04 Apr, 2023 18:36, edited 1 time in total.
«Раньше я думал, что чудаки это те, кто делают странные вещи. Теперь я знаю, что это те, кто называют других странными.» (П. Маккартни)
User avatar
HaRT
Moderator
Posts: 10822
Joined: Tue 30 Aug, 2005 17:21
Has thanked: 221 times
Been thanked: 358 times

[Solved] Directory tree based usermenu

Post by HaRT »

 ! Message from: HaRT
Konstantin, please edit your post so that it's in English, because it is in the English-language subforum.
Фар есть инструмент, а не нянька. © 2009 DrKnS
User avatar
John Doe
Бюрократ
Posts: 13807
Joined: Wed 27 Apr, 2005 20:42
Has thanked: 73 times
Been thanked: 426 times
Contact:

[Solved] Directory tree based usermenu

Post 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:
https://t.me/FarManager — Telegram чат
User avatar
Konstantin
Posts: 234
Joined: Wed 14 Jun, 2006 19:29
Location: Санкт-Петербург
Has thanked: 109 times
Been thanked: 5 times

[Solved] Directory tree based usermenu

Post 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.
«Раньше я думал, что чудаки это те, кто делают странные вещи. Теперь я знаю, что это те, кто называют других странными.» (П. Маккартни)
Post Reply

Return to “General Discussions”