commands, properties, options, options info, never changes during runtime, so we can cache them to make the value instantly available.
Unified · Split
| @@ -56,21 +56,24 @@ local completion_pos · 55 unmodified lines | |||
| 56 | local completion_append | 56 | local completion_append |
| 57 | local last_text | 57 | local last_text |
| 58 | local last_cursor_position | 58 | local last_cursor_position |
| 59 | local commands | 59 | |
| 60 | local cache = {} | ||
| 61 | |||
| 60 | 62 | ||
| 61 | local function get_commands() | 63 | local function get_commands() |
| 62 | if not commands then | 64 | if cache["commands"] then |
| 63 | commands = mp.get_property_native("command-list") | 65 | return cache["commands"] |
| 64 | end | 66 | end |
| 65 | 67 | ||
| 66 | return commands | 68 | cache["commands"] = mp.get_property_native("command-list") |
| 67 | end | 69 | table.sort(cache["commands"], function(c1, c2) |
| 68 | |||
| 69 | local function help_command(param) | ||
| 70 | table.sort(get_commands(), function(c1, c2) | ||
| 71 | return c1.name < c2.name | 70 | return c1.name < c2.name |
| 72 | end) | 71 | end) |
| 73 | 72 | ||
| 73 | return cache["commands"] | ||
| 74 | end | ||
| 75 | |||
| 76 | local function help_command(param) | ||
| 74 | local output = "" | 77 | local output = "" |
| 75 | if param == "" then | 78 | if param == "" then |
| 76 | output = "Available commands:\n" | 79 | output = "Available commands:\n" |
| @@ -148,17 +151,21 @@ local function command_list() · 71 unmodified lines | |||
| 148 | end | 151 | end |
| 149 | 152 | ||
| 150 | local function property_list() | 153 | local function property_list() |
| 151 | local properties = mp.get_property_native("property-list") | 154 | if cache["property-list"] then |
| 155 | return cache["property-list"] | ||
| 156 | end | ||
| 157 | |||
| 158 | cache["property-list"] = mp.get_property_native("property-list") | ||
| 152 | 159 | ||
| 153 | for _, sub_property in pairs({"video", "audio", "sub", "sub2"}) do | 160 | for _, sub_property in pairs({"video", "audio", "sub", "sub2"}) do |
| 154 | properties[#properties + 1] = "current-tracks/" .. sub_property | 161 | table.insert(cache["property-list"], "current-tracks/" .. sub_property) |
| 155 | end | 162 | end |
| 156 | 163 | ||
| 157 | for _, sub_property in pairs({"text", "text-primary"}) do | 164 | for _, sub_property in pairs({"text", "text-primary"}) do |
| 158 | properties[#properties + 1] = "clipboard/" .. sub_property | 165 | table.insert(cache["property-list"], "clipboard/" .. sub_property) |
| 159 | end | 166 | end |
| 160 | 167 | ||
| 161 | return properties | 168 | return cache["property-list"] |
| 162 | end | 169 | end |
| 163 | 170 | ||
| 164 | local function profile_list() | 171 | local function profile_list() |
| @@ -171,28 +178,49 @@ local function profile_list() · 6 unmodified lines | |||
| 171 | return profiles | 178 | return profiles |
| 172 | end | 179 | end |
| 173 | 180 | ||
| 181 | local function option_info(option, prop, default) | ||
| 182 | local key = "option-info/" .. option | ||
| 183 | cache[key] = cache[key] or mp.get_property_native(key, false) | ||
| 184 | |||
| 185 | if not cache[key] then | ||
| 186 | return default | ||
| 187 | end | ||
| 188 | |||
| 189 | if prop then | ||
| 190 | if cache[key][prop] ~= nil then | ||
| 191 | return cache[key][prop] | ||
| 192 | end | ||
| 193 | return default | ||
| 194 | end | ||
| 195 | |||
| 196 | return cache[key] | ||
| 197 | end | ||
| 198 | |||
| 174 | local function list_option_list() | 199 | local function list_option_list() |
| 175 | local opts = {} | 200 | if cache["option-list"] then |
| 201 | return cache["option-list"] | ||
| 202 | end | ||
| 203 | cache["option-list"] = {} | ||
| 176 | 204 | ||
| 177 | -- Don"t log errors for renamed and removed properties. | 205 | -- Don't log errors for renamed and removed properties. |
| 178 | -- (Just mp.enable_messages("fatal") still logs them to the terminal.) | 206 | -- (Just mp.enable_messages("fatal") still logs them to the terminal.) |
| 179 | local msg_level_backup = mp.get_property("msg-level") | 207 | local msg_level_backup = mp.get_property("msg-level") |
| 180 | mp.set_property("msg-level", msg_level_backup == "" and "cplayer=no" | 208 | mp.set_property("msg-level", msg_level_backup == "" and "cplayer=no" |
| 181 | or msg_level_backup .. ",cplayer=no") | 209 | or msg_level_backup .. ",cplayer=no") |
| 182 | 210 | ||
| 183 | for _, option in pairs(mp.get_property_native("options")) do | 211 | for _, option in pairs(mp.get_property_native("options")) do |
| 184 | if mp.get_property("option-info/" .. option .. "/type", ""):find(" list$") then | 212 | if option_info(option, "type", ""):find(" list$") then |
| 185 | opts[#opts + 1] = option | 213 | table.insert(cache["option-list"], option) |
| 186 | end | 214 | end |
| 187 | end | 215 | end |
| 188 | 216 | ||
| 189 | mp.set_property("msg-level", msg_level_backup) | 217 | mp.set_property("msg-level", msg_level_backup) |
| 190 | 218 | ||
| 191 | return opts | 219 | return cache["option-list"] |
| 192 | end | 220 | end |
| 193 | 221 | ||
| 194 | local function list_option_action_list(option) | 222 | local function list_option_action_list(option) |
| 195 | local type = mp.get_property("option-info/" .. option .. "/type") | 223 | local type = option_info(option, "type") |
| 196 | 224 | ||
| 197 | if type == "Key/value list" then | 225 | if type == "Key/value list" then |
| 198 | return {"add", "append", "set", "remove"} | 226 | return {"add", "append", "set", "remove"} |
| @@ -222,10 +250,16 @@ local function list_option_value_list(option) · 23 unmodified lines | |||
| 222 | end | 250 | end |
| 223 | 251 | ||
| 224 | local function has_file_argument(candidate_command) | 252 | local function has_file_argument(candidate_command) |
| 253 | local key = "command-file/" .. candidate_command | ||
| 254 | if cache[key] ~= nil then | ||
| 255 | return cache[key] | ||
| 256 | end | ||
| 257 | |||
| 225 | for _, command in pairs(get_commands()) do | 258 | for _, command in pairs(get_commands()) do |
| 226 | if command.name == candidate_command then | 259 | if command.name == candidate_command then |
| 227 | return command.args[1] and | 260 | cache[key] = command.args[1] and |
| 228 | (command.args[1].name == "filename" or command.args[1].name == "url") | 261 | (command.args[1].name == "filename" or command.args[1].name == "url") |
| 262 | return cache[key] | ||
| 229 | end | 263 | end |
| 230 | end | 264 | end |
| 231 | end | 265 | end |
| @@ -260,7 +294,7 @@ local function handle_file_completion(before_cur) · 28 unmodified lines | |||
| 260 | end | 294 | end |
| 261 | 295 | ||
| 262 | local function handle_choice_completion(option, before_cur) | 296 | local function handle_choice_completion(option, before_cur) |
| 263 | local info = mp.get_property_native("option-info/" .. option, {}) | 297 | local info = option_info(option, nil, {}) |
| 264 | 298 | ||
| 265 | if info.type == "Flag" then | 299 | if info.type == "Flag" then |
| 266 | return { "no", "yes" }, before_cur | 300 | return { "no", "yes" }, before_cur |