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 | 56 | local completion_append |
| 57 | 57 | local last_text |
| 58 | 58 | local last_cursor_position |
| 59 | −local commands | |
| 59 | + | |
| 60 | +local cache = {} | |
| 61 | + | |
| 60 | 62 | |
| 61 | 63 | local function get_commands() |
| 62 | − if not commands then | |
| 63 | − commands = mp.get_property_native("command-list") | |
| 64 | + if cache["commands"] then | |
| 65 | + return cache["commands"] | |
| 64 | 66 | end |
| 65 | 67 | |
| 66 | − return commands | |
| 67 | −end | |
| 68 | − | |
| 69 | −local function help_command(param) | |
| 70 | − table.sort(get_commands(), function(c1, c2) | |
| 68 | + cache["commands"] = mp.get_property_native("command-list") | |
| 69 | + table.sort(cache["commands"], function(c1, c2) | |
| 71 | 70 | return c1.name < c2.name |
| 72 | 71 | end) |
| 73 | 72 | |
| 73 | + return cache["commands"] | |
| 74 | +end | |
| 75 | + | |
| 76 | +local function help_command(param) | |
| 74 | 77 | local output = "" |
| 75 | 78 | if param == "" then |
| 76 | 79 | output = "Available commands:\n" |
| @@ -148,17 +151,21 @@ local function command_list() · 71 unmodified lines | ||
| 148 | 151 | end |
| 149 | 152 | |
| 150 | 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 | 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 | 162 | end |
| 156 | 163 | |
| 157 | 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 | 166 | end |
| 160 | 167 | |
| 161 | − return properties | |
| 168 | + return cache["property-list"] | |
| 162 | 169 | end |
| 163 | 170 | |
| 164 | 171 | local function profile_list() |
| @@ -171,28 +178,49 @@ local function profile_list() · 6 unmodified lines | ||
| 171 | 178 | return profiles |
| 172 | 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 | 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 | 206 | -- (Just mp.enable_messages("fatal") still logs them to the terminal.) |
| 179 | 207 | local msg_level_backup = mp.get_property("msg-level") |
| 180 | 208 | mp.set_property("msg-level", msg_level_backup == "" and "cplayer=no" |
| 181 | 209 | or msg_level_backup .. ",cplayer=no") |
| 182 | 210 | |
| 183 | 211 | for _, option in pairs(mp.get_property_native("options")) do |
| 184 | − if mp.get_property("option-info/" .. option .. "/type", ""):find(" list$") then | |
| 185 | − opts[#opts + 1] = option | |
| 212 | + if option_info(option, "type", ""):find(" list$") then | |
| 213 | + table.insert(cache["option-list"], option) | |
| 186 | 214 | end |
| 187 | 215 | end |
| 188 | 216 | |
| 189 | 217 | mp.set_property("msg-level", msg_level_backup) |
| 190 | 218 | |
| 191 | − return opts | |
| 219 | + return cache["option-list"] | |
| 192 | 220 | end |
| 193 | 221 | |
| 194 | 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 | 225 | if type == "Key/value list" then |
| 198 | 226 | return {"add", "append", "set", "remove"} |
| @@ -222,10 +250,16 @@ local function list_option_value_list(option) · 23 unmodified lines | ||
| 222 | 250 | end |
| 223 | 251 | |
| 224 | 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 | 258 | for _, command in pairs(get_commands()) do |
| 226 | 259 | if command.name == candidate_command then |
| 227 | − return command.args[1] and | |
| 228 | − (command.args[1].name == "filename" or command.args[1].name == "url") | |
| 260 | + cache[key] = command.args[1] and | |
| 261 | + (command.args[1].name == "filename" or command.args[1].name == "url") | |
| 262 | + return cache[key] | |
| 229 | 263 | end |
| 230 | 264 | end |
| 231 | 265 | end |
| @@ -260,7 +294,7 @@ local function handle_file_completion(before_cur) · 28 unmodified lines | ||
| 260 | 294 | end |
| 261 | 295 | |
| 262 | 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 | 299 | if info.type == "Flag" then |
| 266 | 300 | return { "no", "yes" }, before_cur |