Re: [code] [Textadept] menu suggestion

From: Gabriel Dubatti <gdubatti.att.gmail.com>
Date: Thu, 12 Jan 2017 15:22:26 -0300

El 06/01/2017 a las 10:49, Mitchell escribió:
> Hi Gabriel,
>
> On Thu, 5 Jan 2017, Gabriel Dubatti wrote:
>
>> Hi Mitchell,
>>
>> I tried to replace the menu module with my own version in
>> ~/modules/textadept/menu.lua
>> and found that this module is tightly coupled with the keys module,
>> so this module should also be replaced if you want to roll your own
>> menu.
>>
>> I'm referring to code like this:
>>
>> menu.lua:
>> {_L['Select to _Matching Brace'], function()
>> textadept.editing.match_brace('select') end},
>>
>> keys.lua:
>> local m_sel = m_edit[_L['_Select']]
>> keys[GUI and 'cM' or 'mM'] = m_sel[_L['Select to _Matching
>> Brace']][2]
>>
>> Using their own functions, like in:
>> {_L['_Highlight Word'], textadept.editing.highlight_word},
>> keys[not OSX and 'caH' or 'mH'] = textadept.editing.highlight_word
>>
>> Is much cleaner, decoupled and easy to call this commands from
>> scripts (after all, they have a menu and an accelerator, so they are
>> really useful commands).
>>
>> There are near 70 functions in this situation....
>>
>> Just my two cents.
>
> Yes, if you want to totally replace the menu and have your keybindings
> accurately reflected in the menu accelerators, you'll have to update
> some of your keybindings to reference the same functions that the
> menus do (anonymous functions in particular). Note that the menu
> accelerators are for informative purposes only -- even if there is no
> accelerator shown, a keybinding will still work.
>
> Cheers,
> Mitchell

Hi Mitchell,

I finally replaced menu.lua and keys.lua locally adding what I call
"actions" (commands that can be run from menues and/or buttons).

["action"]={"menu-text", exec(), ["icon"], [status()]} --status()= nil
or 0=enabled, 1=checked, 2=unchecked, 3=radio-checked,
4=radio-unchecked, +8=disabled

I plan to use "icon" and status() with my toolbar (these fields are not
used by default).

~~~~~~~~~MENU~~~~~~~~

The menu definition is very similar to the current menu.lua but divided
into two halves:

actions.list = {
   ["new"]= {_L['_New'], buffer.new},
   ["open"]= {_L['_Open'], io.open_file},
...
   ["about"]= {_L['_About'], function()
       ui.dialogs.msgbox({
         title = 'Textadept', text = _RELEASE, informative_text =
_COPYRIGHT,
         icon_file = _HOME..'/core/images/ta_64x64.png'
       })
     end},
}

actions.menubar = {
   {
     title = _L['_File'],
{"new","open","recent","reload","save","saveas","saveall",SEPARATOR,.."quit"}
   },
   {
     title = _L['_Edit'],
     {"undo","redo",SEPARATOR,.."join_lines","filterthrough"},
     {
       title = _L['_Select'],
       {"sel_matchbrace","sel_betweenxmltag",..."sel_paragraph"}
     },
   },
....
   {
     title = _L['_Help'],
     {"show_manual","show_luadoc",SEPARATOR,"about"}
   }
}

actions.context_menu = { {"undo","redo",SEPARATOR,
"cut","copy","paste","delete_char",SEPARATOR, "selectall"} }

actions.tab_context_menu = { {"close",SEPARATOR,
"save","saveas",SEPARATOR, "reload"} }

Only the action name is used to define menus and accelerators (you don't
need to bind to the same code twice).

Each action has a unique ID equal to the position in the actions table
(that ID is used for creating gtk menus).

The click event code do not need to differentiate between menus and
context menus because both use the same actions ID.

-- Performs the appropriate action when clicking a menu item.
events.connect(events.MENU_CLICKED, function(menu_id)
   local act = actions.action_fromid[menu_id]
   local action = actions.list[act][2]
   assert(type(action) == 'function', _L['Unknown command:']..'
'..tostring(action))
   action()
end)

~~~~~~~~~KEY~~~~~~~~

The key module handles the accelerators.

The default accelerators are loaded in a table that can be edited by
other modules before defining the keys[] bindings in the INITIALIZED event.

local default_accelerators= {
-- WIN/LINUX OSX CURSES
   "new", "cn", "mn", "cmn",
   "undo", "cz", "mz", {"cz","mz"}, --^Z suspends in some
terminals
   "redo", {"cy","cZ"}, "my", {"cy","cZ"},
}

--load the accelerators for this OS
local function load_accel_lists()
   actions.accelerators = {}
   local col= not CURSES and (OSX and 2 or 1) or 3
   for i=1, #default_accelerators, 4 do
     local act=default_accelerators[i]
     local key=default_accelerators[i+col]
     actions.accelerators[act]=key
   end
end

~~~~~~~~~SOME USE EXAMPLES ~~~~~~~~
actions.free_accelerator({"f3","cf3","af3","sf3","csf3","cg","cf"})
--make sure no action already uses this keys
keys.f3 = function() goto_nearest_occurrence(false) end
keys.cg = goto_line_col

--replace some actions code
actions.list["new"][2]= Proj.new_file
actions.list["open"][2]= Proj.open_file

--add new actions
actions.add("trim_trailingspaces", 'Trim trailing spaces',
Proj.trim_trailing_spaces)

--add PROJECT menu (before Help)
table.insert( actions.menubar, #actions.menubar,
   {title='_Project',
{"new_project","open_project","recent_project","close_project",SEPARATOR,
"search_project","goto_tag","save_position","next_position","prev_position",SEPARATOR,
     "addthisfiles_proj","addallfiles_proj","adddirfiles_proj"}
   })

--add action at the end of the EDIT menu
actions.menubar[2][#actions.menubar[2]+1]= {SEPARATOR,"trim_trailingspaces"}

--replace tab context menu with a new one
actions.tab_context_menu = {
{"close","close_others","dont_close","onlykeepproj",SEPARATOR,"save","saveas",SEPARATOR,
"reload"} }

--add some accelerators
actions.free_accelerator({"f4","sf4","cpgup","cpgdn"}) --make sure no
action already uses this keys
actions.accelerators["toggle_editproj"]="f4"
actions.accelerators["toggle_viewproj"]="sf4"
actions.accelerators["prev_buffer"]="cpgup"
actions.accelerators["next_buffer"]="cpgdn"

~~~~~~~~~~~~~

The 2 files are available at github in case someone want to see/use them.

https://github.com/gabdub/ta-tweaks/tree/master/modules/textadept

Best wishes,

Gabriel

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Thu 12 Jan 2017 - 13:22:26 EST

This archive was generated by hypermail 2.2.0 : Fri 13 Jan 2017 - 06:41:33 EST