Add Number Lines command

This commit is contained in:
No Time To Play 2024-02-08 06:24:38 +00:00
parent 52923fc34f
commit afbd317453
2 changed files with 29 additions and 3 deletions

View File

@ -1,5 +1,11 @@
# ToyEd text editor project news
## [Unreleased]
### Added
* Number Lines command
## [1.3] - 2024-01-23
### Added

View File

@ -1,7 +1,7 @@
#!/usr/bin/env tclsh
#
# ToyEd: a toy text editor for educational purposes in Tcl/Tk.
# Copyright 2022, 2023 Felix Pleșoianu <https://felix.plesoianu.ro/>
# Copyright 2022, 2023, 2024 Felix Pleșoianu <https://felix.plesoianu.ro/>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@ -234,7 +234,7 @@ $m add separator
$m add command -label "Select all" -under 7 -accel "Ctrl-A" \
-command {tk_util::select_all .editor; break}
$m add command -label "Find..." -command do_find -under 0 -accel "Ctrl-F"
$m add command -label "Again" -command find_again -under 1 -accel "Ctrl-G"
$m add command -label "Next" -command find_again -under 0 -accel "Ctrl-G"
.menubar add cascade -menu .menubar.edit -label "Edit" -underline 0
set m [menu .menubar.mFormat]
@ -248,6 +248,8 @@ $m add command -label "Upper case" -command upper_case -under 0 -accel "Alt-U"
$m add separator
$m add command -label "Prefix lines..." -command prefix_lines \
-under 0 -accel "Alt-P"
$m add command -label "Number lines" -command number_lines \
-under 0 -accel "Alt-N"
.menubar add cascade -menu .menubar.mFormat -label "Format" -underline 3
set m [menu .menubar.view]
@ -256,7 +258,7 @@ $m add checkbutton -label "Word wrap" -under 0 -var _word_wrap \
$m add checkbutton -label "Line numbers" -under 0 -var _line_nums \
-command {toggle_lines .editor}
$m add separator
$m add command -label "Bigger font" -under 0 -accel "Ctrl +" \
$m add command -label "Bigger font" -under 0 -accel "Ctrl-+" \
-command {font_size incr .editor}
$m add command -label "Smaller font" -under 0 -accel "Ctrl -" \
-command {font_size decr .editor}
@ -316,6 +318,7 @@ bind .editor <Alt-l> {lower_case; break}
bind .editor <Alt-t> {title_case; break}
bind .editor <Alt-u> upper_case
bind .editor <Alt-p> prefix_lines
bind .editor <Alt-n> {number_lines; break}
bind . <Control-equal> {font_size::increase .editor}
bind . <Control-minus> {font_size::decrease .editor}
@ -674,6 +677,23 @@ proc prefix_lines {} {
.editor replace sel.first sel.last [join $result "\n"]
}
proc number_lines {} {
global status
set sel [tk_util::text_selection .editor]
if {$sel eq ""} {
set status "Nothing selected."
return
}
set lines [split $sel "\n"]
set result [list]
set n 0
foreach l $lines {
incr n
lappend result "$n. $l"
}
.editor replace sel.first sel.last [join $result "\n"]
}
proc toggle_lines widget {
if {[$widget cget -linemap]} {
$widget configure -linemap 0