From 5014da1b7bcf4be0cb6e394da8f968c78334cf7a Mon Sep 17 00:00:00 2001 From: Joshua Minor Date: Thu, 18 Nov 2021 23:05:33 -0800 Subject: Fixed openselected so it works on macOS. Trim whitespace from ends of selected text. Quote the selected text before sending to the shell, in case there are spaces, quotes, etc. --- plugins/openselected.lua | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'plugins') diff --git a/plugins/openselected.lua b/plugins/openselected.lua index a1e21a2..af00194 100644 --- a/plugins/openselected.lua +++ b/plugins/openselected.lua @@ -2,6 +2,17 @@ local core = require "core" local command = require "core.command" local keymap = require "core.keymap" +local config = require "core.config" + + +config.plugins.openselected = {} +if PLATFORM == "Windows" then + config.plugins.openselected.filemanager = "start" +elseif PLATFORM == "Mac OS X" then + config.plugins.openselected.filemanager = "open" +else + config.plugins.openselected.filemanager = "xdg-open" +end command.add("core.docview", { @@ -13,15 +24,21 @@ command.add("core.docview", { end local text = doc:get_text(doc:get_selection()) - core.log("Opening \"%s\"...", text) - if PLATFORM == "Windows" then - system.exec("start " .. text) - else - system.exec(string.format("xdg-open %q", text)) + -- trim whitespace from the ends + text = text:match( "^%s*(.-)%s*$" ) + + -- non-Windows platforms need the text quoted (%q) + if PLATFORM ~= "Windows" then + text = string.format("%q", text) end + + core.log("Opening %s...", text) + + system.exec(config.plugins.openselected.filemanager .. " " .. text) end, }) keymap.add { ["ctrl+shift+o"] = "open-selected:open-selected" } + -- cgit v1.2.3