Make scite open URLs in default web browser (Linux)

190 views
Skip to first unread message

G.A.M.

unread,
Mar 27, 2008, 7:07:32 PM3/27/08
to scite-interest
Hi,
I would like to make scite open hyperlinks in my default web browser
under Ubuntu. How can I do this? I had a command for doing this in
gedit, but I don't want to use gedit any more. Thanks.
gam

KHMan

unread,
Mar 28, 2008, 2:44:24 AM3/28/08
to scite-i...@googlegroups.com

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

Philippe Lhoste

unread,
Mar 28, 2008, 8:43:34 AM3/28/08
to scite-i...@googlegroups.com

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
-- -- -- -- -- -- -- -- -- -- -- -- -- --

G.A.M.

unread,
Mar 28, 2008, 11:46:38 AM3/28/08
to scite-interest


On Mar 28, 8:43 am, Philippe Lhoste <Phi...@GMX.net> wrote:
> On 28/03/2008 00:07, G.A.M. wrote:
>
> > I would like to make scite open hyperlinks in my default web browser
> > under Ubuntu. How can I do this? I had a command for doing this in
> > gedit, but I don't want to use gedit any more. Thanks.
>
> 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.
>
Gnome, as used in Ubuntu and others, has the following generic method
to open URLs and other files:
gnome-open

It is used like this:
gnome-open <uri>

This general command will handle any file path or url, file paths
being opened in their default applications of course.

As Kein-Hong Man suggested above, x-www-browser is another, more
specific, option that will only open a browser. I used gnome-open with
good success in gedit, but I don't want to use gedit any longer.

G.A.M.

unread,
Mar 28, 2008, 12:07:01 PM3/28/08
to scite-interest
Thank you. That will be a great start. I'm reading the docs, but I am
a long way from understanding enough to actually do much.

I would like to take your code and modify it so that the cursor only
has to be in the URI (without anything being selected). This simply
adds convenience and it is the way the Cream editor works, which is a
feature one comes to really appreciate.

To keep this project simple, I would be happy if the code simply
matched either "http://" or "www." and selected from the beginning to
the first non-linebreak whitespace character.

At this link (http://lua-users.org/files/wiki_insecure/editors/SciTE/
SciTEOpenFilename.lua) I see some code that might help do the
automatic selection of a URI. It is copied below. My questions are:
1. How should I define a NonUriChar to replace NonFilenameChar? I
can't think of many chars that aren't founds in URIs.
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.
3. What is the max length of a URI? (Maybe I'll just set it to 1024
for starters....)

---------------------------------------------------------------
-- range selection and extension
---------------------------------------------------------------
local selBeg, selEnd = editor.Anchor, editor.CurrentPos
if selBeg > selEnd then
-- explicit selection, swap index
selBeg, selEnd = selEnd, selBeg
elseif selBeg == selEnd then
-- no selection; extend caret left & right into a range
if selBeg > 0 then
while not string.find(CharAt(selBeg - 1), NonFilenameChar) do
selBeg = selBeg - 1
end
end
while not string.find(CharAt(selEnd), NonFilenameChar) do
selEnd = selEnd + 1
end
-- beware, unlike RangeExtendAndGrab, no CRLF chomping is
performed, needed?
end
if selEnd - selBeg <= 0 then return end
-- grab delimiter pair for later testing
local delimL, delimR = CharAt(selBeg - 1), CharAt(selEnd)
-- target file name and extension
local fname = editor:textrange(selBeg, selEnd)
if string.len(fname) >= MAX_PATH then
fname = string.sub(fname, 1, MAXPATH - 1)
end
local _, _, fext = string.find(fname, "%.([_%w]*)$")
fext = fext or ""
local thisLine = editor:LineFromPosition(selBeg)
-- prefix, start of line to left of possible filename
local preBeg, preEnd = editor:PositionFromLine(thisLine), selBeg - 1
if preEnd < preBeg then preEnd = preBeg end
local preTxt = editor:textrange(preBeg, preEnd)
-- current file info
local thisExt = props["FileExt"] or ""
local thisPath = props["FileDir"] or ""
thisPath = string.gsub(thisPath, "%\\", "/")
---------------------------------------------------------------

KHMan

unread,
Mar 29, 2008, 4:34:46 AM3/29/08
to scite-i...@googlegroups.com
G.A.M. wrote:
>
> On Mar 28, 2:44 am, KHMan <keinh...@gmail.com> wrote:
>> G.A.M. wrote:
>>> Hi,
>>> I would like to make scite open hyperlinks in my default web browser
>>> under Ubuntu. How can I do this? I had a command for doing this in
>>> gedit, but I don't want to use gedit any more. Thanks.
>>> gam
>> 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.
>>
> [snip]

> At this link (http://lua-users.org/files/wiki_insecure/editors/SciTE/
> SciTEOpenFilename.lua) I see some code that might help do the
> automatic selection of a URI. It is copied below. My questions are:
> 1. How should I define a NonUriChar to replace NonFilenameChar? I
> can't think of many chars that aren't founds in URIs.

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]

G.A.M.

unread,
Mar 31, 2008, 2:06:26 PM3/31/08
to scite-interest
I'm not able to get this working yet. If anyone wants to help by
putting together the auto-selection code with the gnome-open code, I
would be most appreciative.

G.A.M.

unread,
Mar 31, 2008, 2:36:26 PM3/31/08
to scite-interest

On Mar 29, 4:34 am, KHMan <keinh...@gmail.com> wrote:
> G.A.M. wrote:
>
> >> 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
>

> >   ---------------------------------------------------------------
> >   -- range selection and extension
> >   ---------------------------------------------------------------

Let me ask some more specific questions:

I have the "Open Selected URL" command working. (And I assigned it a
shortcut.) I put it in global options because I want it everywhere all
the time for all users.

Where would I put the range selection and extension and extension
code? I would think it should go in a separate file and be called like
a subroutine. Is that true? I have no idea how that works in SciTE/
lua.

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?

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.

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?

Thanks!

KHMan

unread,
Mar 31, 2008, 10:45:46 PM3/31/08
to scite-i...@googlegroups.com
G.A.M. wrote:
>
> On Mar 29, 4:34 am, KHMan <keinh...@gmail.com> wrote:
>> G.A.M. wrote:
>>
>>>> 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
>
>>> ---------------------------------------------------------------
>>> -- range selection and extension
>>> ---------------------------------------------------------------
>
> Let me ask some more specific questions:
>
> I have the "Open Selected URL" command working. (And I assigned it a
> shortcut.) I put it in global options because I want it everywhere all
> the time for all users.
>
> Where would I put the range selection and extension and extension
> code? I would think it should go in a separate file and be called like
> a subroutine. Is that true? I have no idea how that works in SciTE/
> lua.

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.

G.A.M.

unread,
Apr 1, 2008, 11:14:42 AM4/1/08
to scite-interest
Dear Kein-Hong
Thank you for your replies. I would be perfectly happy with the most
simple and unambiguous solution. I think you were suggesting that the
delimiter (stop selection expansion char) be whitespace. This is
acceptable. And simply matching "www." or "http://" at the start of
the string without a sophisticated regex is also acceptable for
version 1.

Can I ask you to put together a simple working example? It seems like
the pieces are already there but I'm not able to combine them myself.
I have to spend some more time learning lua and the scite api before I
will have the skill to do this. In the mean time, the lack of this
feature is preventing me for settling in with scite as my main editor.

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!

Regards,
Gam

KHMan

unread,
Apr 1, 2008, 9:53:30 PM4/1/08
to scite-i...@googlegroups.com
G.A.M. wrote:
> On Mar 31, 10:45 pm, KHMan <keinh...@gmail.com> wrote:
>> G.A.M. wrote:
>>> On Mar 29, 4:34 am, KHMan <keinh...@gmail.com> wrote:
>>>> G.A.M. wrote:
>>>>>> The following in the user properties file works for me in Ubuntu 6.10:
>>>>>> [snip]

>>> Let me ask some more specific questions:
>>> [snip]
>> [snip]
> [snip]
> Can I ask you to put together a simple working example? [snip]

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.

KHMan

unread,
Apr 2, 2008, 12:15:59 AM4/2/08
to scite-i...@googlegroups.com
G.A.M. wrote:
> On Mar 31, 10:45 pm, KHMan <keinh...@gmail.com> wrote:
>> G.A.M. wrote:
>>> On Mar 29, 4:34 am, KHMan <keinh...@gmail.com> wrote:
>>>> G.A.M. wrote:
>>>>>> [snip]
>>>>> [snip]
>>> [snip]
>> [snip]
> [snip]

> Can I ask you to put together a simple working example? It seems like
> the pieces are already there but I'm not able to combine them myself.
> I have to spend some more time learning lua and the scite api before I
> will have the skill to do this. In the mean time, the lack of this
> feature is preventing me for settling in with scite as my main editor.

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.

KHMan

unread,
Apr 2, 2008, 11:08:59 AM4/2/08
to scite-interest
On Apr 2, 12:15 pm, KHMan > wrote:
> [snipped all]
> Here, a much cleaner script:
>
> Wiki page created as:http://lua-users.org/wiki/SciteOpenUrl
> [snip]
> 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

Minor bug. Should anchor regexes to beginning of string,
else matching might be too promiscuous:

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

If a smarter recognizer is needed, then more code will
be needed to trim the string into a proper URL. The
above should work for the vast majority of cases.

G.A.M.

unread,
Apr 2, 2008, 1:33:37 PM4/2/08
to scite-interest
On Apr 2, 12:15 am, KHMan <keinh...@gmail.com> wrote:
> G.A.M. wrote:
> > On Mar 31, 10:45 pm, KHMan <keinh...@gmail.com> wrote:
> >> G.A.M. wrote:
> >>> On Mar 29, 4:34 am, KHMan <keinh...@gmail.com> wrote:
> >>>> G.A.M. wrote:
> >>>>>> [snip]
> >>>>> [snip]
> >>> [snip]
> >> [snip]
> > [snip]
> > Can I ask you to put together a simple working example? It seems like
> > the pieces are already there but I'm not able to combine them myself.
> > I have to spend some more time learning lua and the scite api before I
> > will have the skill to do this. In the mean time, the lack of this
> > feature is preventing me for settling in with scite as my main editor.
>
> 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
>
> > 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.
>
Thank you so much! This works for me in Ubuntu 8.04, so now it has
been tested with two versions.

Here are the steps I did to install this.
1. I reviewed this page: http://lua-users.org/wiki/UsingLuaWithScite
2. I saved the file as "/usr/share/scite/OpenUrlAtCursor.lua"
3. I opened the global properties option file in SciTE and I added
this at the bottom:

ext.lua.startup.script=/usr/share/scite/OpenUrlAtCursor.lua

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

In regard to 'x-www-browser' vs. 'gnome-open', I think you made the
right decision. 'gnome-open' is meant to be flexible in the sense that
it will open a text file with the text editor and it will open a URL
with the default browswer (which should be exactly the same as using x-
www-browser). However, I do suspect gnome-open won't work in KDE.

Thanks again! This works great.

G.A.M.

unread,
Apr 2, 2008, 1:36:00 PM4/2/08
to scite-interest
I agree that this should be adequate. It is certainly adequate for all
my needs. (It is also the same method Cream uses.) Thanks!

KHMan

unread,
Apr 2, 2008, 2:29:08 PM4/2/08
to scite-i...@googlegroups.com
G.A.M. wrote:
> On Apr 2, 12:15 am, KHMan <keinh...@gmail.com> wrote:
>> [snipped all]

> Thank you so much! This works for me in Ubuntu 8.04, so now it has
> been tested with two versions.
>
> Here are the steps I did to install this.
> 1. I reviewed this page: http://lua-users.org/wiki/UsingLuaWithScite
> 2. I saved the file as "/usr/share/scite/OpenUrlAtCursor.lua"
> 3. I opened the global properties option file in SciTE and I added
> this at the bottom:
>
> ext.lua.startup.script=/usr/share/scite/OpenUrlAtCursor.lua
>
> command.name.1.*=Open URL in Browser
> command.1.*=open_url
> command.subsystem.1.*=3
> command.mode.1.*=savebefore:no
> command.shortcut.1.*=Ctrl+'
>
> In regard to 'x-www-browser' vs. 'gnome-open', I think you made the
> right decision. 'gnome-open' is meant to be flexible in the sense that
> it will open a text file with the text editor and it will open a URL
> with the default browswer (which should be exactly the same as using x-
> www-browser). However, I do suspect gnome-open won't work in KDE.

Thanks for the clarification. I've updated the wiki page with the
additional material you provided above.

Reply all
Reply to author
Forward
0 new messages