Scite 開啟網址

lua-users home
wiki

開啟選擇項目或游標下的文字作為網址 (Linux)

下列函式會以預設瀏覽器開啟選擇文字作為網址。如果沒有選擇文字,則會剖析游標下的文字,並將以「http://」、「ftp://」或「www.」開頭的文字視為網址。網址必須只在一行內,並以空白或單引號或雙引號區格。

這個函式可以設為捷徑,例如,使用下列屬性檔案項目設定為 Ctrl+'

command.name.1.*=Open URL in Browser
command.1.*=open_url
command.subsystem.1.*=3
command.mode.1.*=savebefore:no
command.shortcut.1.*=Ctrl+'

這個函式已經在 Ubuntu 6.10 測試過,也應該可以在之後的 Ubuntu 版本執行。它是用 Lua 5.1 編寫的,因此需要 SciTE 1.74 或更新版本。網址萃取程式碼很原始,可能可以改良。沒有進行任何文字編碼處理;文字會視為 8 位元位元組,在函式內處理,且不會轉換任何東西。

-- opens URL via selection or by checking text under cursor
-- Kein-Hong Man <khman@users.sf.net> Public Domain 2008
-- * execute call is non-Win32! tested on Ubuntu 6.10
-- * URL delimited by ", ' or whitespace
-- * does nothing about text encoding!
function open_url()
  local string = string
  local function charat(s, p) return string.sub(s, p, p) end
  local function delim(c) return string.match(c, "[\"'%s]") end
  -- if there is a selection, use exactly, else analyze
  local txt = editor:GetSelText()
  if #txt == 0 then
    -- get details of current line, position
    local p1 = editor.CurrentPos
    local ln = editor:LineFromPosition(p1)
    txt = editor:GetLine(ln)
    if not txt then return end
    local p2 = editor:PositionFromLine(ln)
    p1 = p1 - p2 + 1; p2 = p1
    -- extend text segment to left
    while p1 > 1 do
      if delim(charat(txt, p1 - 1)) then break end
      p1 = p1 - 1
    end
    -- extend text segment to right
    while p2 <= #txt do
      if delim(charat(txt, p2)) then break end
      p2 = p2 + 1
    end
    -- exit if nothing matched
    if p1 == p2 then return end
    txt = string.sub(txt, p1, p2 - 1)
  else
    -- trim extraneous whitespace
    txt = string.gsub(txt, "^%s*(.-)%s*$", "%1")
    -- fail on embedded whitespace
    if string.match(txt, "%s") then return end
  end
  if string.match(txt, "^http://.+") or
     string.match(txt, "^ftp://.+") or
     string.match(txt, "^www%..+") then
    --print("URL='"..txt.."'") --DEBUG
    os.execute("x-www-browser "..txt.." &")
  end
end

其他注意事項:「gnome-open」也可以取代「x-www-browser」。前者比較靈活,但可能比較沒有後者那麼相容。(如果有人能澄清「gnome-open」是否能在 KDE 版本中執行,請更新這個 wiki。)

開啟選擇項目或游標下的文字作為網址 (Windows)

請注意,在 Windows 上,Ctrl+Shift+O 已經可以使用預設瀏覽器開啟游標下的網址了,感謝 Philippe Lhoste。以下介紹的項目彎彎繞繞,是因為 SciTE 很靈活。無論如何,如果使用者想要實作非標準行為,下列資訊會很有用。

在 Windows 上,下列替代程式碼(請將瀏覽器可執行檔的路徑改成電腦上的正確路徑)多少會執行,但是 os.execute() 可能會讓主控台視窗「閃爍」(簡短地開啟和關閉)

    os.execute([[D:\\Programs\\FirefoxPortable\\FirefoxPortable ]]..txt.." &")

另外,下列捷徑可以在 Windows 上執行,但是你必須先選擇網址

command.name.1.*=Open URL in Browser
command.1.*=d:/Programs/FirefoxPortable/FirefoxPortable $(CurrentSelection)
command.subsystem.1.*=1
command.mode.1.*=savebefore:no
command.shortcut.1.*=Ctrl+'

start <url>」慣例似乎無法執行,因為在上述兩種狀況下,SciTE 都會嘗試透過啟動可執行檔來執行一個程序。然而,start 似乎與命令詮譯器有關,所以似乎無法在此處使用。(這個行為沒有經過徹底測試,因此如果錯了,請更新這個頁面。)

我還沒研究過如何在 Windows 上取得預設瀏覽器路徑,所以如果你知道如何在 Windows 上使用瀏覽器開啟網址的更好方式,請更新這裡。

--KeinHongMan--

請注意:雖然上述內容對於所部署的技巧來說可能很有趣,但你必須知道 SciTE 早就允許使用 Ctrl+Shift+O 捷徑開啟網址(或路徑)。你可以自動選取網址,或是有問題的話,先手動選取完整的路徑/網址。--PhiippeLhoste?--


RecentChanges · 喜好設定
編輯 · 歷程
上次編輯時間為 2014 年 12 月 14 日上午 8:46 GMT (diff)