Skip to content

Commit 3441de5

Browse files
committed
gitk: Make web links clickable
This makes gitk look for http or https URLs in the commit description and make the URLs clickable. Clicking on them will invoke an external web browser with the URL. The web browser command is by default "xdg-open" on Linux, "open" on MacOS, and "cmd /c start" on Windows. The command can be changed in the preferences window, and it can include parameters as well as the command name. If it is set to the empty string then URLs will no longer be made clickable. Signed-off-by: Paul Mackerras <[email protected]>
1 parent dec5981 commit 3441de5

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

gitk

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7016,6 +7016,7 @@ proc commit_descriptor {p} {
70167016

70177017
# append some text to the ctext widget, and make any SHA1 ID
70187018
# that we know about be a clickable link.
7019+
# Also look for URLs of the form "http[s]://..." and make them web links.
70197020
proc appendwithlinks {text tags} {
70207021
global ctext linknum curview
70217022

@@ -7032,6 +7033,18 @@ proc appendwithlinks {text tags} {
70327033
setlink $linkid link$linknum
70337034
incr linknum
70347035
}
7036+
set wlinks [regexp -indices -all -inline -line \
7037+
{https?://[^[:space:]]+} $text]
7038+
foreach l $wlinks {
7039+
set s2 [lindex $l 0]
7040+
set e2 [lindex $l 1]
7041+
set url [string range $text $s2 $e2]
7042+
incr e2
7043+
$ctext tag delete link$linknum
7044+
$ctext tag add link$linknum "$start + $s2 c" "$start + $e2 c"
7045+
setwlink $url link$linknum
7046+
incr linknum
7047+
}
70357048
}
70367049

70377050
proc setlink {id lk} {
@@ -7064,6 +7077,18 @@ proc setlink {id lk} {
70647077
}
70657078
}
70667079

7080+
proc setwlink {url lk} {
7081+
global ctext
7082+
global linkfgcolor
7083+
global web_browser
7084+
7085+
if {$web_browser eq {}} return
7086+
$ctext tag conf $lk -foreground $linkfgcolor -underline 1
7087+
$ctext tag bind $lk <1> [list browseweb $url]
7088+
$ctext tag bind $lk <Enter> {linkcursor %W 1}
7089+
$ctext tag bind $lk <Leave> {linkcursor %W -1}
7090+
}
7091+
70677092
proc appendshortlink {id {pre {}} {post {}}} {
70687093
global ctext linknum
70697094

@@ -7098,6 +7123,16 @@ proc linkcursor {w inc} {
70987123
}
70997124
}
71007125

7126+
proc browseweb {url} {
7127+
global web_browser
7128+
7129+
if {$web_browser eq {}} return
7130+
# Use eval here in case $web_browser is a command plus some arguments
7131+
if {[catch {eval exec $web_browser [list $url] &} err]} {
7132+
error_popup "[mc "Error starting web browser:"] $err"
7133+
}
7134+
}
7135+
71017136
proc viewnextline {dir} {
71027137
global canv linespc
71037138

@@ -11488,7 +11523,7 @@ proc create_prefs_page {w} {
1148811523
proc prefspage_general {notebook} {
1148911524
global NS maxwidth maxgraphpct showneartags showlocalchanges
1149011525
global tabstop limitdiffs autoselect autosellen extdifftool perfile_attrs
11491-
global hideremotes want_ttk have_ttk maxrefs
11526+
global hideremotes want_ttk have_ttk maxrefs web_browser
1149211527

1149311528
set page [create_prefs_page $notebook.general]
1149411529

@@ -11539,6 +11574,13 @@ proc prefspage_general {notebook} {
1153911574
pack configure $page.extdifff.l -padx 10
1154011575
grid x $page.extdifff $page.extdifft -sticky ew
1154111576

11577+
${NS}::entry $page.webbrowser -textvariable web_browser
11578+
${NS}::frame $page.webbrowserf
11579+
${NS}::label $page.webbrowserf.l -text [mc "Web browser" ]
11580+
pack $page.webbrowserf.l -side left
11581+
pack configure $page.webbrowserf.l -padx 10
11582+
grid x $page.webbrowserf $page.webbrowser -sticky ew
11583+
1154211584
${NS}::label $page.lgen -text [mc "General options"]
1154311585
grid $page.lgen - -sticky w -pady 10
1154411586
${NS}::checkbutton $page.want_ttk -variable want_ttk \
@@ -12310,13 +12352,19 @@ if {[tk windowingsystem] eq "win32"} {
1231012352
set bgcolor SystemWindow
1231112353
set fgcolor SystemWindowText
1231212354
set selectbgcolor SystemHighlight
12355+
set web_browser "cmd /c start"
1231312356
} else {
1231412357
set uicolor grey85
1231512358
set uifgcolor black
1231612359
set uifgdisabledcolor "#999"
1231712360
set bgcolor white
1231812361
set fgcolor black
1231912362
set selectbgcolor gray85
12363+
if {[tk windowingsystem] eq "aqua"} {
12364+
set web_browser "open"
12365+
} else {
12366+
set web_browser "xdg-open"
12367+
}
1232012368
}
1232112369
set diffcolors {red "#00a000" blue}
1232212370
set diffcontext 3
@@ -12390,6 +12438,7 @@ set config_variables {
1239012438
filesepbgcolor filesepfgcolor linehoverbgcolor linehoverfgcolor
1239112439
linehoveroutlinecolor mainheadcirclecolor workingfilescirclecolor
1239212440
indexcirclecolor circlecolors linkfgcolor circleoutlinecolor
12441+
web_browser
1239312442
}
1239412443
foreach var $config_variables {
1239512444
config_init_trace $var

0 commit comments

Comments
 (0)