The following in the user properties file works for me in Ubuntu 6.10:
command.name.1.*=Open Selected URL
command.1.*=x-www-browser $(CurrentSelection)
command.save.before.1.*=2
command.subsystem.1.*=2
Ctrl+1 opens the selected text as a URL in the default browser.
For custom shortcuts, you can use the command.shortcut.1.*=...,
further information can be found in SciTEDoc.html.
HTH,
--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia
Note that in SciTEIO.cxx, the OpenSelected method has a Windows-specific
section to open an URL when hitting Ctrl+Shift+O when the caret is on
the URL (or the URL is selected).
Perhaps somebody would be able to make it portable, if Linux has a
generic method to open such URLs.
--
Philippe Lhoste
-- (near) Paris -- France
-- http://Phi.Lho.free.fr
-- -- -- -- -- -- -- -- -- -- -- -- -- --
NonFilenameChar is either a whitespace or one of the symbols
disallowed for filenames on Win32. Since URLs allow more, you can
use a modified NonFilenameChar to gather the possible extent of
the URL up to whitespace on both sides. Then, you can apply
pattern matching on the selected segment to grab the URL. It
depends on how 'smart' you want the URL extraction to be.
> 2. Instead of finding the file extension (fext) the code should
> probably look for "http://" or "www." to ensure that the caret is in a
> URI.
Depends on 'smartness'. A user might want to place the caret
anywhere on a URL and still get the editor to recognize that. So
my script scans left and right from caret position to get a
candidate text segment, then it analyzes that segment. The same
method can probably be used for URLs.
> 3. What is the max length of a URI? (Maybe I'll just set it to 1024
> for starters....)
The MAX_PATH is only for older Unixes and Win32 file calls, for
safety's sake. Dunno what are the usual limits to command lines, I
think about 4K is usually enough.
> ---------------------------------------------------------------
> -- range selection and extension
> ---------------------------------------------------------------
> [snip]
The Lua extension is an advanced feature, and is a little more
involved than adjusting property files. Please see the following
to get you started:
SciTELua.html in the SciTE documentation
http://lua-users.org/wiki/SciteScripts
http://lua-users.org/wiki/UsingLuaWithScite
> What is "CRLF chomping"? My desired behavior is for the range
> extension to continue past a CRLF until it reaches a space or tab or
> any other white space other than a line end character -- because a
> long URL might wrap to multiple lines (and if I pasted the URL it
> might have hard line breaks in it). I think the comment in the above
> code is telling me that this is already the behavior. Is that true?
Not sure exactly why I wrote that. It's related to chomp(expr) in
Perl. Basically, I wanted the later processing stages to see a
single line if multiple lines got selected. It doesn't do that now
because NonFilenameChar stops at \r\n already. If you change
NonFilenameChar to remove \r\n, you'd have to add the code to
handle multiple lines for later processing.
> I also can't figure out how to modify the extension finding code to
> instead look for either "www." or "http://" and abort if it doesn't
> find one of those matches.
The only sane way of matching for such patterns is a regular
expression (regex). Lua's regexes isn't that powerful but should
be adequate.
I don't know how gedit behaves, but it seems to me that it's hard
to detect exact extents of a URL, moreso with autodetection of
URLs broken into multiple lines. What do we recognize as part of
the URL? That's why I suggested the earlier simple and unambiguous
solution.
How does gedit's open URL feature behave anyway? If you can
analyze gedit's behaviour (or look at the sources), you might get
a better idea how to do the detection. That's what I did for PFE's
paragraph formatting function; I did some careful testing and then
wrote a Lua script, tried it for a while, then dropped PFE entirely.
> Finally, I thought I would assign the max int value to MAX_PATH for
> simplicity (rather than removing MAX_PATH from the code). How would I
> do that?
Perhaps you can remove it entirely. Just assume os.execute will
bork if it can't swallow the command line you gave it.
I will try to rustle up a URL opener soon without the open
filename stuff. Looking at older code I wrote, some of it can be
written in a much better way, so I might take the opportunity to
practice writing tighter code. For the Lua extension in SciTE,
there are often more than one way to code things, and the
inefficient methods tend to be very verbose.
For sure, I have tested the following and it works fine on Ubuntu
6.10 for me:
os.execute("x-www-browser http://www.cnn.com/ &")
No timeframe guarantees but I hope to do it soon.
Here, a much cleaner script:
Wiki page created as: http://lua-users.org/wiki/SciteOpenUrl
-- opens URL via selection or by checking text under cursor
-- Kein-Hong Man <kh...@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
> I would really appreciate a working example that will expand the URL
> selection to the beginning "www." or "http://" and the ending
> whitespace and call gnome-open with that selection. Thanks!
'gnome-open'? I'm not that well-versed in Linux desktops yet. I
have kept 'x-www-browser' because it seems to be more portable;
would 'gnome-open' work on KDE distros? If 'x-www-browser' isn't
the correct one to use, please feel free to amend the wiki page.
Thanks for the clarification. I've updated the wiki page with the
additional material you provided above.