Re: How to open selection/current file in web browser?

From: Robert <>
Date: Fri, 2 Apr 2010 02:05:30 -0700 (PDT)

On 1 Apr., 03:40, phayz <> wrote:
> Having read about someone's recent changes to menu.lua to have
> Textadept's manual etc loaded in the user's preferred web browser, it
> got me thinking. :)
> Since I often edit HTML files, which themselves often contain URLs,
> I'd like to have an option in Textadept to load either the current
> selected text, or the currently active file, loaded in the user's
> default web browser. At a guess I need to write a custom menu item and/
> or keypress, either of which activate a module, the "guts" of which I
> could borrow from the open_webpage menu option in menu.lua.
> Does this sound like a reasonable approach?

add the following to a key_commands.lua file in your .textadept

keys.hypertext = {
 cr = { function ()
          local url
          local sel = buffer:get_sel_text()
          if #sel == 0 then
            url = buffer.filename
            url = sel
          local cmd
          if WIN32 then
            cmd = string.format('start "" "%s"', url)
            local p = io.popen(cmd)
            if not p then error(l.MENU_BROWSER_ERROR..url) end -- else
p:close() end
            cmd = string.format(MAC and 'open "file://%s"' or 'xdg-
open "%s" &', url)
            if os.execute(cmd) ~= 0 then
error(l.MENU_BROWSER_ERROR..url) end
        end },

Ctrl-r should open the webpage in your default browser. I copied the
code from menu.lua because there seems to be a bug (see my other post)
You might like to hardcode the path to your browser or add another
short cut to a different browser. I was about to postpone the opening
of a selection but then it were just a few more lines :-)

Received on Fri 02 Apr 2010 - 05:05:30 EDT

This archive was generated by hypermail 2.2.0 : Thu 08 Mar 2012 - 11:41:49 EST