From: Bernard D. <bde...@or...> - 2019-08-24 07:00:58
|
Hi Eberhard, Here is a proc called centerRegion that centers a block of text. By default, it applies to the current window and takes the current value of the fillColumn preference (usually 75). You could copy this proc in your prefs.tcl file. If you want to bind it to ctrl-cmd-C for instance, add the following instruction: binding create {zc 'C'} centerRegion ### START OF CODE ### ## # ------------------------------------------------------------------------ # # "centerRegion" -- # # Center a selected block of text. If no 'width' is specified, use the # current 'fillColumn' value. If there is no selection, the proc applies # to the line containing the insertion cursor. It converts all tabs to # spaces and removes any leading or trailing spaces in each line before # centering. # # ------------------------------------------------------------------------ ## proc centerRegion {args} { win::parseArgs w {width ""} if {$width eq ""} { global fillColumn set width $fillColumn } set pos [getPos -w $w] set end [selEnd -w $w] if {$pos == $end} { # If no selection, apply to the current line lassign [linePos all -w $w $pos] start end } else { set start [linePos start -w $w $pos] } # Convert all tabs to spaces in given range if {![catch {tabsToSpaces -w $w $start $end} res]} { # The cursor is now at the end of the region set end [getPos -w $w] set result [list] set txt [text get -w $w $start $end] regsub {\n$} $txt "" txt foreach line [split $txt "\n\r"] { set line [string trim $line " "] set slen [string length $line] if {$slen >= $width} { lappend result $line } else { set wlen [expr ($width-$slen)/2] lappend result "[string repeat " " $wlen]$line" } } text replace -w $w $start $end "[join $result \n]\n" } } ### END OF CODE ### Hope this helps, Bernard > Le 22 août 2019 à 14:57, Dr Eberhard W Lisse <el...@li...> a écrit : > > > > Bernard, > > Perhaps something like if there is a wrap mode, such as 72 or 80 > characters, take half of that, if not, maybe take half of the window > width (if it can be measured in characters) > > el > > On 22/08/2019 14:23, Bernard Desgraupes wrote: >> Oh I see, you want to center a selection. Right. >> It should not be difficult to write a proc for this. The question is >> "where is the center" ? >> >> Bernard |