[Solved] Directory tree based usermenu

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

[Solved] Directory tree based usermenu

Post by pepak » Sun 05 Nov, 2017 19:03

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: 5488
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 20 times
Been thanked: 176 times

[Solved] Directory tree based usermenu

Post by Shmuel » Sun 05 Nov, 2017 19:25

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: 369
Joined: Sun 13 Jul, 2008 11:18
Has thanked: 5 times
Been thanked: 11 times

[Solved] Directory tree based usermenu

Post by pepak » Sun 05 Nov, 2017 19:33

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: 369
Joined: Sun 13 Jul, 2008 11:18
Has thanked: 5 times
Been thanked: 11 times

[Solved] Directory tree based usermenu

Post by pepak » Sun 05 Nov, 2017 19:36

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: 11819
Joined: Wed 27 Apr, 2005 20:42
Has thanked: 26 times
Been thanked: 129 times

[Solved] Directory tree based usermenu

Post by John Doe » Sun 05 Nov, 2017 22:33

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: 212
Joined: Wed 14 Jun, 2006 19:29
Location: Санкт-Петербург
Has thanked: 1 time

[Solved] Directory tree based usermenu

Post by Konstantin » Mon 06 Nov, 2017 08:23

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: 11819
Joined: Wed 27 Apr, 2005 20:42
Has thanked: 26 times
Been thanked: 129 times

[Solved] Directory tree based usermenu

Post by John Doe » Wed 08 Nov, 2017 10:37

Ok, I will, after some cleanup.
For now see improved version of the macro above.

Post Reply

Return to “General Discussions”