Skip to content

Commit 61a6b22

Browse files
committed
move title escaping code to lua
1 parent 3d5fa54 commit 61a6b22

File tree

6 files changed

+31
-646
lines changed

6 files changed

+31
-646
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ configure_file(${PROJECT_SOURCE_DIR}/menu.rc.in ${PROJECT_BINARY_DIR}/menu.rc @O
1515

1616
set(CMAKE_SHARED_LIBRARY_PREFIX "")
1717
add_library(menu SHARED
18-
src/mpv/misc/bstr.c
1918
src/mpv/misc/dispatch.c
2019
src/mpv/ta/ta.c
2120
src/mpv/ta/ta_talloc.c

src/lua/dyn_menu.lua

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local msg = require('mp.msg')
88
-- user options
99
local 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
289290
end
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
299300
end
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*$')
@@ -592,9 +593,6 @@ end
592593

593594
-- parse input.conf, return menu items
594595
local 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

src/menu.c

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,9 @@
22
// SPDX-License-Identifier: GPL-2.0-only
33

44
#include <windows.h>
5-
#include "misc/bstr.h"
5+
#include "mpv_talloc.h"
66
#include "menu.h"
77

8-
// escape & to && for menu title
9-
static wchar_t *escape_title(void *talloc_ctx, char *title) {
10-
void *tmp = talloc_new(NULL);
11-
bstr left, rest;
12-
bstr escaped = bstr0(NULL);
13-
14-
left = bstr_split(bstr0(title), "&", &rest);
15-
while (rest.len > 0) {
16-
bstr_xappend(tmp, &escaped, left);
17-
bstr_xappend(tmp, &escaped, bstr0("&&"));
18-
left = bstr_split(rest, "&", &rest);
19-
}
20-
bstr_xappend(tmp, &escaped, left);
21-
22-
wchar_t *ret = mp_from_utf8(talloc_ctx, bstrdup0(tmp, escaped));
23-
talloc_free(tmp);
24-
return ret;
25-
}
26-
278
// append menu item to HMENU
289
static int append_menu(HMENU hmenu, UINT fMask, UINT fType, UINT fState,
2910
wchar_t *title, HMENU submenu, void *data) {
@@ -135,7 +116,7 @@ static void build_menu(void *talloc_ctx, HMENU hmenu, mpv_node *node) {
135116
strcmp(cmd, "ignore") == 0;
136117
}
137118
int id = append_menu(hmenu, fMask, 0, (UINT)fState,
138-
escape_title(talloc_ctx, title), submenu,
119+
mp_from_utf8(talloc_ctx, title), submenu,
139120
talloc_strdup(talloc_ctx, cmd));
140121
if (grayed) EnableMenuItem(hmenu, id, MF_BYCOMMAND | MF_GRAYED);
141122
}

0 commit comments

Comments
 (0)