@@ -8,6 +8,7 @@ local msg = require('mp.msg')
88-- user options
99local o = {
1010 uosc_syntax = false , -- toggle uosc menu syntax support
11+ escape_title = true , -- escape & to && in menu title
1112 max_title_length = 80 , -- limit the title length, set to 0 to disable.
1213 max_playlist_items = 20 , -- limit the playlist items in submenu, set to 0 to disable.
1314}
@@ -232,8 +233,8 @@ local function build_track_items(list, type, prop, prefix)
232233 local state = {}
233234 -- there may be 2 tracks selected at the same time, for example: subtitle
234235 if track .selected then
235- table.insert ( state , ' checked' )
236- if track .id ~= pos then table.insert ( state , ' disabled' ) end
236+ state [ # state + 1 ] = ' checked'
237+ if track .id ~= pos then state [ # state + 1 ] = ' disabled' end
237238 end
238239
239240 items [# items + 1 ] = {
@@ -281,11 +282,11 @@ local function update_tracks_menu(menu)
281282 local items_s = build_track_items (track_list , ' sub' , ' sid' , true )
282283
283284 -- append video/audio/sub tracks into one submenu, separated by a separator
284- for _ , item in ipairs (items_v ) do table.insert ( submenu , item ) end
285- if # submenu > 0 and # items_a > 0 then table.insert ( submenu , { type = ' separator' }) end
286- for _ , item in ipairs (items_a ) do table.insert ( submenu , item ) end
287- if # submenu > 0 and # items_s > 0 then table.insert ( submenu , { type = ' separator' }) end
288- for _ , item in ipairs (items_s ) do table.insert ( submenu , item ) end
285+ for _ , item in ipairs (items_v ) do submenu [ # submenu + 1 ] = item end
286+ if # submenu > 0 and # items_a > 0 then submenu [ # submenu + 1 ] = { type = ' separator' } end
287+ for _ , item in ipairs (items_a ) do submenu [ # submenu + 1 ] = item end
288+ if # submenu > 0 and # items_s > 0 then submenu [ # submenu + 1 ] = { type = ' separator' } end
289+ for _ , item in ipairs (items_s ) do submenu [ # submenu + 1 ] = item end
289290end
290291
291292-- handle #@tracks/<type> menu update for given type
@@ -295,7 +296,7 @@ local function update_track_menu(menu, type, prop)
295296 if # track_list == 0 then return end
296297
297298 local items = build_track_items (track_list , type , prop , false )
298- for _ , item in ipairs (items ) do table.insert ( submenu , item ) end
299+ for _ , item in ipairs (items ) do submenu [ # submenu + 1 ] = item end
299300end
300301
301302-- handle #@chapters menu update
@@ -418,7 +419,7 @@ local function update_menu_state(menu)
418419
419420 local state = {}
420421 if type (res ) == ' string' then
421- for s in res :gmatch (' [^,%s]+' ) do table.insert ( state , s ) end
422+ for s in res :gmatch (' [^,%s]+' ) do state [ # state + 1 ] = s end
422423 end
423424 menu .item .state = state
424425 menu_items_dirty = true
@@ -456,7 +457,7 @@ local function dyn_menu_load(item, keyword)
456457 state = nil ,
457458 dirty = false ,
458459 }
459- table.insert ( dyn_menus , menu )
460+ dyn_menus [ # dyn_menus + 1 ] = menu
460461 keyword_to_menu [keyword ] = menu
461462
462463 local expr = keyword :match (' ^state=(.-)%s*$' )
592593
593594-- parse input.conf, return menu items
594595local function parse_input_conf (conf )
595- local items = {}
596- local by_id = {}
597-
598596 local function extract_title (cmd )
599597 if not cmd or cmd == ' ' then return ' ' end
600598 local title = cmd :match (' #menu:%s*(.*)%s*' )
@@ -620,38 +618,40 @@ local function parse_input_conf(conf)
620618 return list
621619 end
622620
621+ local function append_menu (menu , type , title , cmd , submenu )
622+ menu [# menu + 1 ] = {
623+ type = type ,
624+ title = (title and o .escape_title ) and title :gsub (' &' , ' &&' ) or title ,
625+ cmd = cmd ,
626+ submenu = submenu ,
627+ }
628+ end
629+
630+ local items = {}
631+ local by_id = {}
632+
623633 for line in conf :gmatch (' [^\r\n ]+' ) do
624634 if line :sub (1 , 1 ) ~= ' #' or o .uosc_syntax then
625- local key , cmd = line :match (' %s*([%S]+)%s+(.-)%s*$' )
626- local title = extract_title (cmd )
627- local list = split_title (title )
628-
629635 local submenu_id = ' '
630636 local target_menu = items
637+ local key , cmd = line :match (' %s*([%S]+)%s+(.-)%s*$' )
638+ local list = split_title (extract_title (cmd ))
631639
632640 for id , name in ipairs (list ) do
633641 if id < # list then
634642 submenu_id = submenu_id .. name
635643 if not by_id [submenu_id ] then
636644 local submenu = {}
637645 by_id [submenu_id ] = submenu
638- target_menu [# target_menu + 1 ] = {
639- title = name ,
640- type = ' submenu' ,
641- submenu = submenu ,
642- }
646+ append_menu (target_menu , ' submenu' , name , nil , submenu )
643647 end
644648 target_menu = by_id [submenu_id ]
645649 else
646650 if name == ' -' or (o .uosc_syntax and name :sub (1 , 3 ) == ' ---' ) then
647- target_menu [# target_menu + 1 ] = {
648- type = ' separator' ,
649- }
651+ append_menu (target_menu , ' separator' )
650652 else
651- target_menu [# target_menu + 1 ] = {
652- title = (key ~= ' ' and key ~= ' _' ) and (name .. " \t " .. key ) or name ,
653- cmd = cmd ,
654- }
653+ local title = (key ~= ' ' and key ~= ' _' ) and (name .. " \t " .. key ) or name
654+ append_menu (target_menu , nil , title , cmd )
655655 end
656656 end
657657 end
0 commit comments