File: Utils.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (890 lines) | stat: -rw-r--r-- 20,754 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
#  Utils.tcl ---
#  
#      This file is part of The Coccinella application. We collect some handy 
#      small utility procedures here.
#      
#  Copyright (c) 1999-2005  Mats Bengtsson
#  
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  
# $Id: Utils.tcl,v 1.79 2008-09-14 13:02:27 sdevrieze Exp $

package require uri
package provide Utils 1.0

namespace eval ::Utils:: {

}

# SecondCoccinella --
# 
#       This gets called if a second instance of this app is launched,
#       before it kills itself.

proc SecondCoccinella {args} {
    
    set w [::JUI::GetMainWindow]
    wm deiconify $w
    raise $w
    eval {::hooks::run relaunchHook} $args
}

#--- Utilities for general usage -----------------------------------------------

# ::Utils::FontEqual --
# 
#       Compares two fonts to see if identical.

proc ::Utils::FontEqual {font1 font2} {
    
    set ans 1
    array set font1Arr [font actual $font1]
    foreach {key value} [font actual $font2] {
	if {![string equal $font1Arr($key) $value]} {
	    set ans 0
	    break
	}
    }
    return $ans
}

proc ::Utils::FontBold {fontName} {    
    array set fontA [font actual $fontName]
    return [list $fontA(-family) $fontA(-size) bold]
}

proc ::Utils::FontItalic {fontName} {    
    array set fontA [font actual $fontName]
    return [list $fontA(-family) $fontA(-size) italic]
}

# ::Utils::GetMaxMsgcatWidth --
# 
#       Returns the max string length for the current catalog of the
#       given source strings.

proc ::Utils::GetMaxMsgcatWidth {args} {
    
    set width 0
    foreach str $args {
	set len [string length [mc $str]]
	if {$len > $width} {
	    set width $len
	}
    }
    return $width
}

proc ::Utils::GetMaxMsgcatString {args} {
    
    set width 0
    foreach str $args {
	set mcstr [mc $str]
	set len [string length $mcstr]
	if {$len > $width} {
	    set width $len
	    set maxstr $mcstr
	}
    }
    return $maxstr
}

# ::Utils::IsIPNumber --
#
#       Tests if the arguments is a ip number, that is, 200.54.2.0
#       Not foolproof! Should be able to tell the difference between
#       a ip name and a ip number.

proc ::Utils::IsIPNumber {thing} {
    
    set sub_ {([0-2][0-9][0-9]|[0-9][0-9]|[0-9])}
    set d_ {\.}
    return [regexp "^${sub_}${d_}${sub_}${d_}${sub_}${d_}${sub_}$" $thing]
}

# ::Utils::IsWellformedUrl --
#
#       Returns boolean depending on if argument is a well formed URL.
#       Not foolproof!

proc ::Utils::IsWellformedUrl {url} {
    
    return [regexp {[^:]+://[^:/]+(:[0-9]+)?[^ ]*} $url]
}

# ::Utils::GetFilePathFromUrl --
#
#       Returns the file path part from a well formed url, or empty if
#       didn't recognize it as a valid url.

proc ::Utils::GetFilePathFromUrl {url} {
    
    if {[regexp {[^:]+://[^:/]+(:[0-9]+)?/(.*)} $url match port path]} {
	return $path
    } else {
	return
    }
}

proc ::Utils::GetDomainNameFromUrl {url} {
    
    if {[regexp {[^:]+://([^:/]+)(:[0-9]+)?/.*} $url match domain port]} {
	return $domain
    } else {
	return
    }
}

# ::Utils::SmartClockFormat --
#
#       Pretty formatted time & date.
#
# Arguments:
#       secs        Number of seconds since system defined time.
#                   This must be local time.
#       args:   -detail
#               -weekdays
#               -showsecs
#       
# Results:
#       nice time string that still can be used by 'clock scan'

proc ::Utils::SmartClockFormat {secs args} {
    
    array set opts {
	-weekdays 0
	-detail   0
	-showsecs 1
	-today    0
    }
    array set opts $args
    if {$secs eq ""} {
	return
    }
    
    # 'days': 0=today, -1=yesterday etc.
    set secs00 [clock scan "today 00:00"]
    set days [expr {($secs - $secs00)/(60*60*24)}]
    
    switch -regexp -- $days {
	^1$ {
	    set date "tomorrow"
	}
	^0$ {
	    if {$opts(-today)} {
		set date "today"
	    } else {
		set date ""
	    }
	}
	^-1$ {
	    set date "yesterday"
	}
	^-[2-5]$ {
	    if {$opts(-weekdays)} {
		set date [string tolower  \
		  [clock format [clock scan "$days days ago"] -format "%A"]]
	    } else {
		set date [clock format $secs -format "%y-%m-%d"]
	    }
	}
	default {
	    set date [clock format $secs -format "%y-%m-%d"]
	}
    }
    if {$opts(-showsecs)} {
	set fmt "%H:%M:%S"
    } else {
	set fmt "%H:%M"
    }

    if {$opts(-detail) && ($days == 0)} {
	set now [clock seconds]
	set minutes [expr {($now - $secs)/60}]
	if {$minutes == 0} {
	    set time [clock format $secs -format $fmt]
	} elseif {$minutes == 1} {
	    set time "one minute ago"
	} elseif {$minutes == 2} {
	    set time "two minutes ago"
	} elseif {$minutes == 3} {
	    set time "three minutes ago"
	} elseif {$minutes < 60} {
	    set time "$minutes minutes ago"
	} else {
	    set time [clock format $secs -format $fmt]
	}
    } else {	
	set time [clock format $secs -format $fmt]
    }
    return [string trim "$date $time"]
}

proc ::Utils::IsToday {secs} {
    return [expr {($secs - [clock scan "today 00:00"])/(60*60*24) >= 0 ? 1 : 0}]
}

proc ::Utils::FormatBytes {bytes} {
    
    if {$bytes < 1000} {
	set str "$bytes bytes"
    } elseif {$bytes < 1000000} {
	set str "[format %.1f [expr {$bytes/1000.0}] ]Kb"
    } else {
	set str "[format %.1f [expr {$bytes/1000000.0}] ]Mb"
    }

    return $str
}

# Utils::UnixGetWebBrowser, UnixGetAllWebBrowsers ... --
# 
#       These functions get the execution paths to the applications,
#       not the application names. Always from the auto_execok command.

proc ::Utils::UnixGetWebBrowser {} {
    global  prefs env
    
    set browser {}
    set e [auto_execok $prefs(webBrowser)]
    if {[llength $e]} {
	set browser [lindex $e 0]
    }
    if {$browser eq {}} {
	if {[info exists env(BROWSER)]} {
	    if {[llength [set e [auto_execok $env(BROWSER)]]]} {
		set browser [lindex $e 0]
	    }
	}
    }
    if {$browser eq {}} {
	set browser [KDEGetBrowser]
    }
    if {$browser eq {}} {
	set browser [lindex [UnixGetAllWebBrowsers] 0]
    }
    set prefs(webBrowser) $browser
    return $browser
}

proc ::Utils::UnixGetAllWebBrowsers {} {
    set browsers [KDEGetBrowser]
    foreach name {
	firefox galeon konqueror mozilla-firefox google-chrome
	mozilla-firebird mozilla netscape iexplorer opera
    } {
	if {[llength [set e [auto_execok $name]]]} {
	    lappend browsers [lindex $e 0]
	}
    }
    return [lsort -unique $browsers]
}

proc ::Utils::UnixGetEmailClient {} {
    global  prefs
    
    set mail ""
    if {$prefs(mailClient) eq "gmail"} {
	return "gmail"
    }
    set e [auto_execok $prefs(mailClient)]
    if {[llength $e]} {
	set mail [lindex $e 0]
    }
    if {$mail eq {}} {
	set mail [KDEGetEmailClient]
    }
    if {$mail eq {}} {
	set mail [lindex [UnixGetAllEmailClients] 0]
    }
    return $mail
}

proc ::Utils::UnixGetAllEmailClients {} {
    set mailers [KDEGetEmailClient]
    foreach name {thunderbird kmail} {
	if {[llength [set e [auto_execok $name]]]} {
	    lappend mailers [lindex $e 0]
	}
    }
    # Special.
    lappend mailers gmail
    return [lsort -unique $mailers]
}

proc ::Utils::KDEGetBrowser {} {
    set name [KDEGetConfigValue ~/.kde/share/config/kdeglobals BrowserApplication]
    if {[llength [set e [auto_execok $name]]]} {
	return $e
    } else {
	return {}
    }
}

proc ::Utils::KDEGetEmailClient {} {
    set name [KDEGetConfigValue ~/.kde/share/config/emaildefaults EmailClient]
    if {[llength [set e [auto_execok $name]]]} {
	return $e
    } else {
	return
    }
}

proc ::Utils::KDEGetConfigValue {f key} {
    
    set ans ""
    if {[file exists $f] && [file readable $f]} {
	set fd [open $f r]
	set lines [split [read $fd] "\n"]
	foreach line $lines {
	    if {[string match "$key=*" $line]} {
		set RE [format {%s=(.+)$} $key]
		regexp $RE $line - ans
	    }
	}
	close $fd
    }
    return $ans
}

proc ::Utils::UnixOpenUrl {url} {
    global  prefs

    if {$prefs(webBrowser) eq ""} {
	set browser [UnixGetWebBrowser]
    } else {
	set browser $prefs(webBrowser)
    }
    if {$browser ne ""} {
	if {[catch {eval exec $browser -remote \"openURL($url, new-tab)\"}]} {
	    if {[catch {exec $browser -remote $url}]} {
		if {[catch {exec $browser $url &}]} {
		    set browser ""
		}
	    }
	}
    } 
    if {$browser eq ""} {
	::UI::MessageBox -icon error -type ok -title [mc "Error"] -message \
	  "Couldn't localize a web browser on this system.\
	  Define a shell variable \"BROWSER\" to point to a web browser."
    }
}

proc ::Utils::OpenURLInBrowser {url} {
    global  tcl_platform prefs
    
    ::Debug 2 "::Utils::OpenURLInBrowser url=$url"
    
    switch -glob -- $tcl_platform(platform),$tcl_platform(os) {
	unix,Darwin {
	    exec open $url
	}
	unix,* {
	    UnixOpenUrl $url
	}
	windows,* {	    
	    ::Windows::OpenUrl $url
	}
    }
}


proc ::Utils::ValidMinutes {str} {
    if {[regexp {^[0-9]*$} $str]} {
	return 1
    } else {
	bell
	return 0
    }
}

# Utils::GetDirIfExist --
#
#       Returns $dir only if actually exists, else returns $this(path).

proc ::Utils::GetDirIfExist {dir} {
    global  this
    
    if {[file isdirectory $dir]} {
	return $dir
    } else {
	return $this(path)
    }
}

proc ::Utils::GetFontListFromName {fontSpec} {
    
    if {[llength $fontSpec] == 1} {
	array set fontArr [font actual $fontSpec]
	return [list $fontArr(-family) $fontArr(-size) $fontArr(-weight)] 
    } elseif {[llength $fontSpec] == 3} {
	return $fontSpec
    } else {
	return -code error "unknown font specification $fontSpec"
    }
}
    
# Utils::GetHttpFromFile --
# 
#       Translates an absolute file path to an uri encoded http address
#       for our built in http server.

proc ::Utils::GetHttpFromFile {filePath} {
    global  prefs

    set relPath [::tinyhttpd::mountfile $filePath]
    set ip [::Network::GetThisPublicIP]
    return "http://${ip}:$prefs(httpdPort)/$relPath"
}

proc ::Utils::IsAnimatedGif {fileName} {
    
    # If we can read -index 1 then this is likely an animated gif. CRUDE!
    set code [catch {
	image create photo -file $fileName -format {gif89 -index 1}
    } name]
    catch {image delete $name}
    return [expr {$code == 0 ? 1 : 0}]
}

proc ::Utils::CreateGif {fileName {imageName ""}} {
    
    if {[IsAnimatedGif $fileName]} {
	return [::anigif::anigif $fileName $imageName]
    } else {
	if {$imageName eq ""} {
	    return [image create photo -file $fileName -format gif]
	} else {
	    return [image create photo $imageName -file $fileName -format gif]
	}
    }
}

proc ::Utils::ImageFromData {data {mime ""}} {
    global  this
    
    set type ""
    lassign [split $mime /] x type
    if {$type eq "gif"} {
	return [image create photo -data $data -format gif]
    } else {
	if {![catch {image create photo -data $data} name]} {
	    return $name
	} 
	set tmpfile [::tfileutils::tempfile $this(tmpPath) vcard]
	if {$type ne ""} {
	    append tmpfile .$type
	}
	#puts "tmpfile=$tmpfile"
	set fd [open $tmpfile {CREAT WRONLY}]
	fconfigure $fd -translation binary
	puts -nonewline $fd [::base64::decode $data]
	close $fd
	if {![catch {image create photo -file $tmpfile} name]} {
	    return $name
	} else {
	    return
	}
    }
}

# Utils::LanguageMenubutton --
# 
#       Make a language menubutton selector.

proc ::Utils::LanguageMenubutton {w varName args} {
    global  this prefs

    # Add entries here for new message catalogs.
    array set code2Name {
	cs    {Čeština}
	da    {Dansk}
	de    {Deutsch}
	en    {English}
	es    {Castellano}
	fr    {Français}
	it    {Italiano}
	ko    {한국어}
	nl    {Nederlands}
	pl    {Polski}
	pt    {Português}
	ru    {русский}
	sv    {Svenska}
	zh_cn {简体中文}
    }
    set langs [list]
    foreach f [glob -nocomplain -tails -directory $this(msgcatPath) -- *.msg] {
	set code [file rootname $f]
	if {[info exists code2Name($code)]} {
	    set str $code2Name($code)
	} else {
	    set str $code
	}
	lappend langs $str
	set langCode2Str($code) $str
    }
    if {$prefs(messageLocale) eq ""} {
	set $varName [lindex [split [::msgcat::mclocale] _] 0]
    } else {
	set $varName $prefs(messageLocale)
    }
    set menuDef [list]
    foreach {code str} [array get langCode2Str] {
	lappend menuDef [list $str -value $code]
    }
    set menuDef [lsort -index 0 $menuDef]
    ui::optionmenu $w -menulist $menuDef -direction flush -variable $varName

    return $w
}

#--- Animation -----------------------------------------------------------------

namespace eval ::Utils:: {
    variable anim
    set anim(uid) 0
}

proc ::Utils::AnimateStart {delay vlist command} {
    variable anim
    
    set uid [incr anim(uid)]
    set anim($uid,delay) $delay
    set anim($uid,vlist) $vlist
    set anim($uid,command) $command
    set anim($uid,pos) 0
    set anim($uid,pending) [after $delay [namespace current]::AnimateHandle $uid]
 
    return $uid
}

proc ::Utils::AnimateHandle {uid} {
    variable anim

    if {[info exists anim($uid,pending)]} {
	set pos $anim($uid,pos)
	set cmd $anim($uid,command)
	set val [lindex $anim($uid,vlist) $pos]
	regsub -all {\\|&} $val {\\\0} val
	regsub -all {%v} $cmd $val cmd
	if {[catch {uplevel #0 $cmd}]} {
	    AnimateStop $uid
	    return
	}
	if {[incr pos] >= [llength $anim($uid,vlist)]} {
	    set pos 0
	}
	set anim($uid,pos) $pos
	set anim($uid,pending) \
	  [after $anim($uid,delay) [namespace current]::AnimateHandle $uid]
    }
}

proc ::Utils::AnimateStop {uid} {
    variable anim

    if {[info exists anim($uid,pending)]} {
	after cancel $anim($uid,pending)
    }
    array unset anim $uid,*
}

#--- Utilities for the Text widget ---------------------------------------------

namespace eval ::Text:: {

    # Unique counter to produce specified link tags.
    variable idurl 1000
        
    # @@@ Use uri package here!
    variable urlRE {^(http://|https://|www\.|ftp://|ftp\.)([^ \t\r\n]+)}
    set httpRE $::uri::http::url
    set ftpRE $::uri::ftp::url
    variable urlColor
    array set urlColor {fg blue activefg red}

    # Plugin model for parsing URI's
    variable reguri
    lappend reguri $urlRE ::Text::UrlButton
}

proc ::Text::RegisterURI {re cmd} {
    variable reguri
    
    lappend reguri $re $cmd
}

proc ::Text::GetREUriList {} {
    variable reguri
    return $reguri
}

# Text::ParseMsg --
# 
#       Parses message text (body).
#       
# Arguments:
#       type        chat|groupchat|normal
#       jid         JID to send to
#       w           text widget
#       str         the text
#       tagList     current text tag list
#       
# Results:
#       none

proc ::Text::ParseMsg {type jid w str tagList} {
    
    # Split string into words and whitespaces.
    set wsp {[ \t\r\n]+}
    set len [string length $str]
    if {$len == 0} {
	return
    }
    
    # Have hook for complete text.
    if {[hooks::run textPreParseHook $type $jid $w $str $tagList] eq "stop"} {	    
	return
    }
    
    set start 0
    while {[regexp -start $start -indices -- $wsp $str match]} {
	lassign $match matchStart matchEnd
	
	# The "space" part.
	set space [string range $str $matchStart $matchEnd]
	incr matchStart -1
	incr matchEnd
	
	# The word preceeding the space.
	set word [string range $str $start $matchStart]
	set start $matchEnd
	
	# Process the actual word:
	# Run first the hook to see if anyone else wants to parse it.
	if {[hooks::run textParseWordHook $type $jid $w $word $tagList] ne "stop"} {	    
	    ParseWord $w $word $tagList
	}
	
	# Insert the whitespace after word.
	$w insert insert $space $tagList
    }
    
    # And the final word.
    set word [string range $str $start end]
    if {[hooks::run textParseWordHook $type $jid $w $word $tagList] ne "stop"} {	    
	ParseWord $w $word $tagList
    }
}

# Text::Parse, ... --
# 
#       It takes a text widget, the text, and a default tag, and parses
#       smileys and urls.

proc ::Text::Parse {w str tagList} {

    ParseMsg {} {} $w $str $tagList
}

proc ::Text::ParseWord {w word tagList} {
    
    if {[::Emoticons::Exists $word]} {
	::Emoticons::Make $w $word
    } elseif {![ParseURI $w $word]} {
	$w insert insert $word $tagList
    }
}

proc ::Text::ParseURI {w word} {
    variable reguri
    variable idurl
    variable urlColor

    foreach {re cmd} $reguri {
	if {[regexp $re $word]} {
	    set urltag url$idurl
	    set urlfg       [option get $w urlForeground       Text]
	    set urlactivefg [option get $w urlActiveForeground Text]
	    if {$urlfg eq ""} {
		set urlfg $urlColor(fg)
	    }
	    if {$urlactivefg eq ""} {
		set activefg $urlColor(activefg)
	    }
	    $w tag configure $urltag -foreground $urlfg -underline 1
	    $w tag bind $urltag <Button-1>   \
	      [list $cmd [string map {% %%} $word]]
	    $w tag bind $urltag <Any-Enter>  \
	      [list ::Text::UrlEnter $w $urltag $activefg]
	    $w tag bind $urltag <Any-Leave>  \
	      [list ::Text::UrlLeave $w $urltag $urlfg]
	    set tags [$w tag names "insert - 1 chars"]
	    $w insert insert $word [concat $tags $urltag]
	    incr idurl
	    return 1
	}
    }
    return 0
}

proc ::Text::UrlButton {url} {

    # If no xxx:// assume http://
    if {![regexp {^[a-z]+://} $url]} {
	set url "http://$url"
    }
    if {[::Utils::IsWellformedUrl $url]} {
	::Utils::OpenURLInBrowser $url
    }
}

proc ::Text::UrlEnter {w tag fgcol} {
    $w configure -cursor hand2
    $w tag configure $tag -foreground $fgcol -underline 1
}

proc ::Text::UrlLeave {w tag fgcol} {
    $w configure -cursor arrow
    $w tag configure $tag -foreground $fgcol -underline 1
}

# Text::InsertURL --
#
#       Insert a link where the text string and url may differ.

proc ::Text::InsertURL {w str url tag} {    
    variable idurl
    variable urlColor
    
    set urltag url${idurl}
    set urlfg       [option get $w urlForeground       Text]
    set urlactivefg [option get $w urlActiveForeground Text]
    if {$urlfg eq ""} {
	set urlfg $urlColor(fg)
    }
    if {$urlactivefg eq ""} {
	set activefg $urlColor(activefg)
    }
    $w tag configure $urltag -foreground $urlfg -underline 1
    $w tag bind $urltag <Button-1>  \
      [list ::Text::UrlButton [string map {% %%} $url]]
    $w tag bind $urltag <Any-Enter>  \
      [list ::Text::UrlEnter $w $urltag $activefg]
    $w tag bind $urltag <Any-Leave>  \
      [list ::Text::UrlLeave $w $urltag $urlfg]
    $w insert insert $str [list $tag $urltag]
    incr idurl
}

proc ::Text::TransformSelToPureText {w } {
    variable puretext
    
    unset -nocomplain puretext($w)
    set puretext($w) ""
    foreach {key value index} [$w dump sel.first sel.last] {
	TransformToPureTextCallback $w $key $value $index
    }
    return $puretext($w)
}

proc ::Text::TransformToPureText {w args} {    
    variable puretext
    
    unset -nocomplain puretext($w)
    set puretext($w) ""
    foreach {key value index} [$w dump 1.0 end] {
	TransformToPureTextCallback $w $key $value $index
    }
    return $puretext($w)
}

proc ::Text::TransformToPureTextCallback {w key value index} {    
    variable puretext

    switch -- $key {
	tagon {
	    
	}
	tagoff {
	    
	}
	text {
	    append puretext($w) $value
	}
	image {
	    
	    # Treat smileys specially.
	    set name [$w image cget $index -name]	    
	    if {[string length $name] > 0} {
		append puretext($w) $name
	    } else {
		set imname [$w image cget $index -image]	    
		append puretext($w) {[Image:}
		set tail [file tail [$imname cget -file]]
		if {[string length $tail]} {
		    append puretext($w) " [file tail $tail]"
		}
		append puretext($w) {]}
	    }
	}
    }
}

namespace eval ::Text {
    
    variable tagOptions
    set tagOptions {
	-background
	-borderwidth
	-font
	-foreground
	-justify
	-lmargin1
	-lmargin2
	-offset
	-overstrike
	-relief
	-rmargin
	-spacing1
	-spacing2
	-spacing3
	-tabs
	-underline
	-wrap
    }
}

proc ::Text::ConfigureTags {w} {
    variable tagOptions

    set tags [$w tag names]

    foreach tag $tags {
	set optsL [list]
	foreach opt $tagOptions {
	    set optName [string totitle [string trimleft $opt "-"]]
	    set value [option get $w $tag$optName {}]
	    if {$value ne ""} {
		lappend optsL $opt $value
	    }
	}
	if {[llength $optsL]} {
	    eval {$w tag configure $tag} $optsL
	}
    }
}

#-------------------------------------------------------------------------------