| 1 |
#!/usr/bin/tclsh |
| 2 |
# Part of MCU 8051 IDE ( http://http://www.moravia-microsystems.com/mcu8051ide ) |
| 3 |
|
| 4 |
############################################################################ |
| 5 |
# Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 by Martin Ošmera # |
| 6 |
# [email protected] # |
| 7 |
# # |
| 8 |
# Copyright (C) 2014 by Moravia Microsystems, s.r.o. # |
| 9 |
# [email protected] # |
| 10 |
# # |
| 11 |
# This program is free software; you can redistribute it and#or modify # |
| 12 |
# it under the terms of the GNU General Public License as published by # |
| 13 |
# the Free Software Foundation; either version 2 of the License, or # |
| 14 |
# (at your option) any later version. # |
| 15 |
# # |
| 16 |
# This program is distributed in the hope that it will be useful, # |
| 17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 19 |
# GNU General Public License for more details. # |
| 20 |
# # |
| 21 |
# You should have received a copy of the GNU General Public License # |
| 22 |
# along with this program; if not, write to the # |
| 23 |
# Free Software Foundation, Inc., # |
| 24 |
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # |
| 25 |
############################################################################ |
| 26 |
|
| 27 |
# >>> File inclusion guard |
| 28 |
if { ! [ info exists _FIND_IN_FILES_TCL ] } { |
| 29 |
set _FIND_IN_FILES_TCL _ |
| 30 |
# <<< File inclusion guard |
| 31 |
|
| 32 |
# -------------------------------------------------------------------------- |
| 33 |
# DESCRIPTION |
| 34 |
# Implements panel "Find in files", GUI and function |
| 35 |
# Inteted for bottom panel |
| 36 |
# -------------------------------------------------------------------------- |
| 37 |
|
| 38 |
class FindInFiles { |
| 39 |
public common find_inf_count 0 ;# Counter of class instances |
| 40 |
|
| 41 |
# Variables related to object initialization |
| 42 |
private variable parent ;# Widget: parent widget |
| 43 |
private variable find_inf_gui_initialized 0 ;# Bool: GUI initialized |
| 44 |
|
| 45 |
private variable obj_idx ;# Int: Object index |
| 46 |
private variable abort_variable 0 ;# Bool: Abort search |
| 47 |
private variable iteration 0 ;# Int: Counter of search iterations (lines read) |
| 48 |
private variable folder ;# String: Choosen folder |
| 49 |
private variable pattern ;# String: Search pattern |
| 50 |
private variable reg_expr ;# Bool: Use regular expression |
| 51 |
private variable case_sen ;# Bool: Perform case sensitive search |
| 52 |
private variable pattern_length ;# Int: Pettern length |
| 53 |
|
| 54 |
private variable pattern_entry ;# Windegt: EntryBox "Pattern" |
| 55 |
private variable main_frame ;# Widget: Main frame |
| 56 |
private variable menu {} ;# Widget: popup menu for text widge |
| 57 |
private variable text_widget ;# Widget: Text widget to show results |
| 58 |
private variable clear_button ;# Widget: Button "Clear" |
| 59 |
private variable find_stop_button {} ;# Widget: Button "Find / Stop" |
| 60 |
|
| 61 |
constructor {} { |
| 62 |
# Increment object counter |
| 63 |
incr find_inf_count |
| 64 |
set obj_idx $find_inf_count |
| 65 |
|
| 66 |
# Load configuration |
| 67 |
if {[catch { |
| 68 |
set ::FindInFiles::recursive_$obj_idx [lindex $::CONFIG(FIND_IN_FILES_CONFIG) 0] |
| 69 |
set ::FindInFiles::regular_expr_$obj_idx [lindex $::CONFIG(FIND_IN_FILES_CONFIG) 1] |
| 70 |
set ::FindInFiles::case_sensitive_$obj_idx [lindex $::CONFIG(FIND_IN_FILES_CONFIG) 2] |
| 71 |
set ::FindInFiles::folder_$obj_idx [lindex $::CONFIG(FIND_IN_FILES_CONFIG) 3] |
| 72 |
set ::FindInFiles::mask_$obj_idx [lindex $::CONFIG(FIND_IN_FILES_CONFIG) 4] |
| 73 |
set ::FindInFiles::pattern_$obj_idx [lindex $::CONFIG(FIND_IN_FILES_CONFIG) 5] |
| 74 |
}]} then { |
| 75 |
set ::FindInFiles::recursive_$obj_idx 1 |
| 76 |
set ::FindInFiles::regular_expr_$obj_idx 0 |
| 77 |
set ::FindInFiles::case_sensitive_$obj_idx 1 |
| 78 |
set ::FindInFiles::folder_$obj_idx {} |
| 79 |
set ::FindInFiles::mask_$obj_idx {*.asm,*.c,*.h} |
| 80 |
set ::FindInFiles::pattern_$obj_idx {} |
| 81 |
} |
| 82 |
|
| 83 |
# Validate loaded configuration |
| 84 |
if {![string is boolean -strict [subst -nocommands "\$::FindInFiles::recursive_$obj_idx"]]} { |
| 85 |
set ::FindInFiles::recursive_$obj_idx 1 |
| 86 |
} |
| 87 |
if {![string is boolean -strict [subst -nocommands "\$::FindInFiles::regular_expr_$obj_idx"]]} { |
| 88 |
set ::FindInFiles::regular_expr_$obj_idx 0 |
| 89 |
} |
| 90 |
if {![string is boolean -strict [subst -nocommands "\$::FindInFiles::case_sensitive_$obj_idx"]]} { |
| 91 |
set ::FindInFiles::case_sensitive_$obj_idx 1 |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
destructor { |
| 96 |
# Remove status bar help for popup menus |
| 97 |
if {$menu != {}} { |
| 98 |
menu_Sbar_remove $menu |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
## Prepare object for creating its GUI |
| 103 |
# @parm Widget _parent - GUI parent widget |
| 104 |
# @return void |
| 105 |
public method PrepareFindInFiles {_parent} { |
| 106 |
set parent $_parent |
| 107 |
set find_inf_gui_initialized 0 |
| 108 |
} |
| 109 |
|
| 110 |
## Inform this tab than it has became active |
| 111 |
# @return void |
| 112 |
public method FindInFilesTabRaised {} { |
| 113 |
$pattern_entry selection range 0 end |
| 114 |
$pattern_entry icursor end |
| 115 |
focus $pattern_entry |
| 116 |
} |
| 117 |
|
| 118 |
## Create GUI of messages tab |
| 119 |
# @return void |
| 120 |
public method CreateFindInFilesGUI {} { |
| 121 |
if {$find_inf_gui_initialized} {return} |
| 122 |
set find_inf_gui_initialized 1 |
| 123 |
|
| 124 |
if {${::DEBUG}} { |
| 125 |
puts "CreateFindInFilesGUI \[ENTER\]" |
| 126 |
} |
| 127 |
|
| 128 |
create_findinfilesgui |
| 129 |
create_tags_and_bindings |
| 130 |
create_popup_menu |
| 131 |
} |
| 132 |
|
| 133 |
## Create GUI elements |
| 134 |
# @return void |
| 135 |
private method create_findinfilesgui {} { |
| 136 |
set main_frame [frame $parent.main_frame] |
| 137 |
|
| 138 |
## Top frame |
| 139 |
set top_frame [frame $main_frame.top_frame] |
| 140 |
# Entry "Pattern" |
| 141 |
set pattern_entry [ttk::entry $top_frame.pattern_entry \ |
| 142 |
-validate all \ |
| 143 |
-width 1 \ |
| 144 |
-textvariable ::FindInFiles::pattern_$obj_idx \ |
| 145 |
-validatecommand "$this findinfiles_validate_crit_ent 0 %P" \ |
| 146 |
] |
| 147 |
bind $pattern_entry <Return> "$this findinfiles_search" |
| 148 |
bind $pattern_entry <KP_Enter> "$this findinfiles_search" |
| 149 |
setStatusTip -widget $pattern_entry \ |
| 150 |
-text [mc "Search pattern"] |
| 151 |
# Entry "Mask" |
| 152 |
set mask_entry [ttk::entry $top_frame.mask_entry \ |
| 153 |
-validate all \ |
| 154 |
-width 1 \ |
| 155 |
-textvariable ::FindInFiles::mask_$obj_idx \ |
| 156 |
-validatecommand "$this findinfiles_validate_crit_ent 1 %P" \ |
| 157 |
] |
| 158 |
bind $mask_entry <Return> "$this findinfiles_search" |
| 159 |
bind $mask_entry <KP_Enter> "$this findinfiles_search" |
| 160 |
setStatusTip -widget $mask_entry \ |
| 161 |
-text [mc "File mask (e.g. \"*.c,*.asm\")"] |
| 162 |
# Entry "Folder" |
| 163 |
set folder_entry_frm [frame $top_frame.folder_entry_frm] |
| 164 |
set folder_entry [ttk::entry $folder_entry_frm.folder_entry \ |
| 165 |
-textvariable ::FindInFiles::folder_$obj_idx \ |
| 166 |
-validate all \ |
| 167 |
-width 1 \ |
| 168 |
-validatecommand "$this findinfiles_validate_crit_ent 2 %P" \ |
| 169 |
] |
| 170 |
set ::FindInFiles::folder_$obj_idx [$this cget -projectPath] |
| 171 |
bind $folder_entry <Return> "$this findinfiles_search" |
| 172 |
bind $folder_entry <KP_Enter> "$this findinfiles_search" |
| 173 |
pack $folder_entry -side left -fill x -expand 1 -pady 2 |
| 174 |
# Button "Select directory" |
| 175 |
pack [ttk::button $folder_entry_frm.select_dir_but \ |
| 176 |
-image ::ICONS::16::fileopen \ |
| 177 |
-style Flat.TButton \ |
| 178 |
-command "$this findinfiles_select_dir" \ |
| 179 |
] -side left |
| 180 |
DynamicHelp::add $folder_entry_frm.select_dir_but \ |
| 181 |
-text [mc "Choose destination location"] |
| 182 |
setStatusTip -widget $folder_entry_frm.select_dir_but -text [mc "Select folder"] |
| 183 |
# Checkbutton "Recursive" |
| 184 |
pack [checkbutton $folder_entry_frm.recursive_chb \ |
| 185 |
-variable ::FindInFiles::recursive_$obj_idx \ |
| 186 |
-text [mc "Recursive"] \ |
| 187 |
] -side left -padx 10 |
| 188 |
setStatusTip -widget $folder_entry_frm.recursive_chb -text [mc "Search in all subfolders"] |
| 189 |
# Button "Start / Stop search" |
| 190 |
set top_bottom_frame [frame $top_frame.bottom_frame] |
| 191 |
set find_stop_button [ttk::button $top_bottom_frame.find_stop_button \ |
| 192 |
-text [mc "Find"] \ |
| 193 |
-image ::ICONS::16::find \ |
| 194 |
-compound left \ |
| 195 |
-command "$this findinfiles_search" \ |
| 196 |
-width 7 \ |
| 197 |
] |
| 198 |
setStatusTip -widget $find_stop_button -text [mc "Start / Stop search"] |
| 199 |
# Button "Clear" |
| 200 |
set clear_button [ttk::button $top_bottom_frame.clear_button \ |
| 201 |
-text [mc "Clear"] \ |
| 202 |
-image ::ICONS::16::clear_left \ |
| 203 |
-compound left \ |
| 204 |
-command "$this findinfiles_clear" \ |
| 205 |
-state disabled \ |
| 206 |
-width 7 \ |
| 207 |
] |
| 208 |
setStatusTip -widget $clear_button -text [mc "Clear results"] |
| 209 |
pack $find_stop_button -side left -padx 2 |
| 210 |
pack $clear_button -side left -padx 2 |
| 211 |
# Separator |
| 212 |
pack [ttk::separator $top_bottom_frame.sep \ |
| 213 |
-orient vertical \ |
| 214 |
] -fill y -side left -padx 10 -pady 2 |
| 215 |
# Checkbutton "Case sensitive" |
| 216 |
pack [checkbutton $top_bottom_frame.case_sen_chb \ |
| 217 |
-text [mc "Case sensitive"] \ |
| 218 |
-variable ::FindInFiles::case_sensitive_$obj_idx \ |
| 219 |
] -side left |
| 220 |
setStatusTip -widget $top_bottom_frame.case_sen_chb \ |
| 221 |
-text [mc "Perform case sensitive search"] |
| 222 |
# Checkbutton "Regular expression" |
| 223 |
pack [checkbutton $top_bottom_frame.regular_expr_chb \ |
| 224 |
-text [mc "Regular expression"] \ |
| 225 |
-variable ::FindInFiles::regular_expr_$obj_idx \ |
| 226 |
] -side left |
| 227 |
setStatusTip -widget $top_bottom_frame.regular_expr_chb \ |
| 228 |
-text [mc "Pattern is a regular expression"] |
| 229 |
# Labels ... (Pattern, Folder, Mask) |
| 230 |
grid [label $top_frame.pattern_lbl \ |
| 231 |
-text [mc "Pattern:"] \ |
| 232 |
] -row 0 -column 0 -sticky w |
| 233 |
grid [label $top_frame.folder_lbl \ |
| 234 |
-text [mc "Folder:"] \ |
| 235 |
] -row 0 -column 4 -sticky w |
| 236 |
grid [label $top_frame.template_lbl \ |
| 237 |
-text [mc "Mask:"] \ |
| 238 |
] -row 1 -column 0 -sticky w |
| 239 |
# Button "Clear pattern entrybox" |
| 240 |
grid [ttk::button $top_frame.pattern_clr_but \ |
| 241 |
-image ::ICONS::16::clear_left \ |
| 242 |
-style Flat.TButton \ |
| 243 |
-command "set ::FindInFiles::pattern_$obj_idx {}"\ |
| 244 |
] -row 0 -column 2 |
| 245 |
setStatusTip -widget $top_frame.pattern_clr_but \ |
| 246 |
-text [mc "Clear pattern entrybox"] |
| 247 |
# Button "Show help for file mask" |
| 248 |
grid [ttk::button $top_frame.mask_show_help \ |
| 249 |
-image ::ICONS::16::help \ |
| 250 |
-style Flat.TButton \ |
| 251 |
-command "$this findinfiles_hlp" \ |
| 252 |
] -row 1 -column 2 |
| 253 |
setStatusTip -widget $top_frame.mask_show_help \ |
| 254 |
-text [mc "Show help for file mask"] |
| 255 |
# Place some widgets into the grid |
| 256 |
grid $pattern_entry -row 0 -column 1 -sticky we |
| 257 |
grid $folder_entry_frm -row 0 -column 5 -sticky we |
| 258 |
grid $mask_entry -row 1 -column 1 -sticky we |
| 259 |
grid $top_bottom_frame -row 1 -column 4 -columnspan 2 -sticky we |
| 260 |
grid columnconfigure $top_frame 1 -weight 1 |
| 261 |
grid columnconfigure $top_frame 5 -weight 1 |
| 262 |
grid columnconfigure $top_frame 3 -minsize 20 |
| 263 |
|
| 264 |
## Bottom frame (text widget and its scrollbar) |
| 265 |
set bottom_frame [frame $main_frame.bottom_frame] |
| 266 |
set text_widget [text $bottom_frame.text \ |
| 267 |
-bg white \ |
| 268 |
-state disabled \ |
| 269 |
-bd 1 \ |
| 270 |
-wrap none \ |
| 271 |
-highlightthickness 0 \ |
| 272 |
-exportselection 0 \ |
| 273 |
-cursor left_ptr \ |
| 274 |
-width 0 \ |
| 275 |
-height 0 \ |
| 276 |
-yscrollcommand "$bottom_frame.scrollbar set" \ |
| 277 |
-font [font create \ |
| 278 |
-family helvetica \ |
| 279 |
-size [expr {int(-12 * $::font_size_factor)}] \ |
| 280 |
] \ |
| 281 |
-fg {#555555} \ |
| 282 |
] |
| 283 |
pack $text_widget -side left -fill both -expand 1 |
| 284 |
pack [ttk::scrollbar $bottom_frame.scrollbar \ |
| 285 |
-orient vertical -command "$text_widget yview" \ |
| 286 |
] -side right -after $text_widget -fill y |
| 287 |
|
| 288 |
# Mask panel frames |
| 289 |
pack $top_frame -fill x -anchor nw |
| 290 |
pack $bottom_frame -fill both -expand 1 |
| 291 |
pack $main_frame -fill both -expand 1 |
| 292 |
|
| 293 |
# Adjust GUI |
| 294 |
findinfiles_validate_crit_ent -1 {} |
| 295 |
} |
| 296 |
|
| 297 |
## Create text tags and event binding for the text widget |
| 298 |
# @return void |
| 299 |
private method create_tags_and_bindings {} { |
| 300 |
# Create tags |
| 301 |
set bold_font [font create -family helvetica -size [expr {int(-12 * $::font_size_factor)}] -weight bold] |
| 302 |
$text_widget tag configure tag_highlight -foreground {#000000} -font $bold_font |
| 303 |
$text_widget tag configure tag_filename -foreground {#0000DD} |
| 304 |
$text_widget tag configure tag_linenumber -foreground {#00DD00} |
| 305 |
$text_widget tag configure tag_normal -foreground {#000000} |
| 306 |
$text_widget tag configure tag_cur_line -background {#FFFF88} |
| 307 |
|
| 308 |
# Create evet bindings |
| 309 |
bind $text_widget <ButtonRelease-3> "$this findinfiles_popupmenu %X %Y %x %y; break" |
| 310 |
bind $text_widget <Button-1> "$this findinfiles_click %x %y; break" |
| 311 |
bind $text_widget <Double-Button-1> "$this findinfiles_doubleclick %x %y; break" |
| 312 |
bind $text_widget <<Selection>> "false_selection $text_widget; break" |
| 313 |
} |
| 314 |
|
| 315 |
## Create popup menu for the text widget |
| 316 |
# @return void |
| 317 |
private method create_popup_menu {} { |
| 318 |
set menu $text_widget.popup_menu |
| 319 |
menuFactory { |
| 320 |
{command "Go to" {} 0 "findinfiles_goto_cur_line" |
| 321 |
{goto} "Go to this line"} |
| 322 |
{command "Clear" {} 0 "findinfiles_clear" |
| 323 |
{editdelete} "Clear this panel"} |
| 324 |
} $menu 0 "$this " 0 {} [namespace current] |
| 325 |
$menu entryconfigure [::mc "Clear"] -state disabled |
| 326 |
} |
| 327 |
|
| 328 |
## Invoke file selection dialog to select folder where to search |
| 329 |
# @return void |
| 330 |
public method findinfiles_select_dir {} { |
| 331 |
KIFSD::FSD ::fsd \ |
| 332 |
-title [mc "Choose directory - MCU 8051 IDE"] \ |
| 333 |
-fileson 0 -master . \ |
| 334 |
-directory [subst -nocommands "\$::FindInFiles::folder_$obj_idx"] |
| 335 |
fsd setokcmd "set ::FindInFiles::folder_$obj_idx \[::fsd get\]" |
| 336 |
fsd activate |
| 337 |
} |
| 338 |
|
| 339 |
## Start searching |
| 340 |
# @return void |
| 341 |
public method findinfiles_search {} { |
| 342 |
# Gain search options |
| 343 |
set folder [file normalize [subst -nocommands "\$::FindInFiles::folder_$obj_idx"]] |
| 344 |
set mask [subst -nocommands "\$::FindInFiles::mask_$obj_idx"] |
| 345 |
set pattern [subst -nocommands "\$::FindInFiles::pattern_$obj_idx"] |
| 346 |
set reg_expr [subst -nocommands "\$::FindInFiles::regular_expr_$obj_idx"] |
| 347 |
set case_sen [subst -nocommands "\$::FindInFiles::case_sensitive_$obj_idx"] |
| 348 |
|
| 349 |
# Validate search options |
| 350 |
if {![string length $folder] || ![string length $mask] || ![string length $pattern]} { |
| 351 |
return |
| 352 |
} |
| 353 |
if {![file exists $folder]} { |
| 354 |
tk_messageBox \ |
| 355 |
-parent . \ |
| 356 |
-type ok \ |
| 357 |
-icon warning \ |
| 358 |
-title [mc "Folder not found"] \ |
| 359 |
-message [mc "The specified folder does not exist.\n'%s'" $folder] |
| 360 |
return |
| 361 |
} |
| 362 |
if {![file isdirectory $folder]} { |
| 363 |
tk_messageBox \ |
| 364 |
-parent . \ |
| 365 |
-type ok \ |
| 366 |
-icon warning \ |
| 367 |
-title [mc "Folder not found"] \ |
| 368 |
-message [mc "The string specified as a folder is not a folder.\n'%s'" $folder] |
| 369 |
return |
| 370 |
} |
| 371 |
if {$reg_expr && [catch {regexp -about $pattern}]} { |
| 372 |
tk_messageBox \ |
| 373 |
-parent . \ |
| 374 |
-type ok \ |
| 375 |
-icon warning \ |
| 376 |
-title [mc "Error"] \ |
| 377 |
-message [mc "Invalid regular expression"] |
| 378 |
return |
| 379 |
} |
| 380 |
|
| 381 |
# Adjust GUI (Find button, text widget ...) |
| 382 |
$find_stop_button configure \ |
| 383 |
-text [mc "Stop"] -image ::ICONS::16::cancel \ |
| 384 |
-command "$this findinfiles_stop" |
| 385 |
$text_widget configure -state normal |
| 386 |
$text_widget delete 0.0 end |
| 387 |
update |
| 388 |
|
| 389 |
# Determinate list of files |
| 390 |
set new_mask {} |
| 391 |
foreach glob [split $mask {,}] { |
| 392 |
lappend new_mask $glob |
| 393 |
} |
| 394 |
set mask $new_mask |
| 395 |
set files [list] |
| 396 |
catch { ;# For Microsoft Windows it has to be enclosed by catch |
| 397 |
foreach m $mask { |
| 398 |
eval "append files { } \[glob -directory {$folder} -nocomplain -types {f l} -- $m\]" |
| 399 |
} |
| 400 |
} |
| 401 |
if {[subst -nocommands "\$::FindInFiles::recursive_$obj_idx"]} { |
| 402 |
append files { } [regsub -all {[\{\}]} [recursive_search $folder $mask] {\\&}] |
| 403 |
} |
| 404 |
|
| 405 |
# Search |
| 406 |
if {!$reg_expr && !$case_sen} { |
| 407 |
set pattern [string tolower $pattern] |
| 408 |
} |
| 409 |
set pattern_length [string length $pattern] |
| 410 |
set iteration 0 |
| 411 |
foreach filename $files { |
| 412 |
if {[search_in_file $filename]} { |
| 413 |
break |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
# Adjust GUI (Find button, text widget ...) |
| 418 |
$text_widget delete end-1l end |
| 419 |
$text_widget configure -state disabled |
| 420 |
$find_stop_button configure \ |
| 421 |
-text [mc "Find"] \ |
| 422 |
-image ::ICONS::16::find \ |
| 423 |
-command "$this findinfiles_search" |
| 424 |
|
| 425 |
# Enable / Disable clear button and clear entry in the popup menu |
| 426 |
if {[$text_widget index {1.0 lineend}] == {1.0}} { |
| 427 |
set state disabled |
| 428 |
} else { |
| 429 |
set state normal |
| 430 |
} |
| 431 |
$menu entryconfigure [::mc "Clear"] -state $state |
| 432 |
$clear_button configure -state $state |
| 433 |
} |
| 434 |
|
| 435 |
## Perform recursive search for ceratin files in certain folder |
| 436 |
# @parm String folder - Directory to search |
| 437 |
# @parm String mask - File masks |
| 438 |
# @return List - Found files |
| 439 |
private method recursive_search {folder mask} { |
| 440 |
set files {} |
| 441 |
catch { ;# For Microsoft Windows it has to be enclosed by catch |
| 442 |
foreach dir [glob -directory $folder -nocomplain -types {d} -- *] { |
| 443 |
foreach m $mask { |
| 444 |
eval "append files { } \[glob -directory {$dir} -nocomplain -types {f l} -- $m\]" |
| 445 |
} |
| 446 |
append files { } [recursive_search $dir $mask] |
| 447 |
} |
| 448 |
} |
| 449 |
if {[llength $files]} { |
| 450 |
update |
| 451 |
} |
| 452 |
return $files |
| 453 |
} |
| 454 |
|
| 455 |
## Search in certain file |
| 456 |
# @parm String filename - File where to search in |
| 457 |
# @return Bool - 1 == Search aborted; 0 == Normal |
| 458 |
private method search_in_file {filename} { |
| 459 |
# Open file |
| 460 |
if {[catch { |
| 461 |
set file [open $filename r] |
| 462 |
}]} then { |
| 463 |
return 0 |
| 464 |
} |
| 465 |
|
| 466 |
# Local variables |
| 467 |
set relative_filename [string replace $filename 0 [string length $folder]] |
| 468 |
set indexes {} |
| 469 |
set text_idx {end} |
| 470 |
set line_number 0 |
| 471 |
set matched_str {} |
| 472 |
|
| 473 |
# Iterate over lines |
| 474 |
while {![eof $file]} { |
| 475 |
incr line_number |
| 476 |
incr iteration |
| 477 |
set line [gets $file] |
| 478 |
|
| 479 |
# Update GUI and evaluate abort variable |
| 480 |
if {$iteration > 100} { |
| 481 |
if {[lindex [$text_widget yview] 1] == 1} { |
| 482 |
$text_widget see end |
| 483 |
} |
| 484 |
update |
| 485 |
if {$abort_variable} { |
| 486 |
set abort_variable 0 |
| 487 |
return 1 |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
## Search string |
| 492 |
if {!$case_sen} { |
| 493 |
set line [string tolower $line] |
| 494 |
} |
| 495 |
set indexes {} |
| 496 |
set lengths {} |
| 497 |
set last_idx -1 |
| 498 |
set idx 0 |
| 499 |
set found 1 |
| 500 |
# Regular expression |
| 501 |
if {$reg_expr} { |
| 502 |
while $found { |
| 503 |
set found 0 |
| 504 |
if {$case_sen} { |
| 505 |
set found [regexp -start $idx -- $pattern $line matched_str] |
| 506 |
} else { |
| 507 |
set found [regexp -nocase -start $idx -- $pattern $line matched_str] |
| 508 |
} |
| 509 |
|
| 510 |
set idx [string first $matched_str $line $idx] |
| 511 |
if {$last_idx >= $idx} { |
| 512 |
break |
| 513 |
} |
| 514 |
|
| 515 |
lappend indexes $idx |
| 516 |
lappend lengths [string length $matched_str] |
| 517 |
incr idx |
| 518 |
set last_idx $idx |
| 519 |
} |
| 520 |
# Pure string pattern |
| 521 |
} else { |
| 522 |
while {$idx != -1} { |
| 523 |
set idx [string first $pattern $line $idx] |
| 524 |
if {$last_idx >= $idx} { |
| 525 |
break |
| 526 |
} |
| 527 |
|
| 528 |
lappend indexes $idx |
| 529 |
lappend lengths $pattern_length |
| 530 |
incr idx |
| 531 |
set last_idx $idx |
| 532 |
} |
| 533 |
} |
| 534 |
if {![llength $indexes]} { |
| 535 |
continue |
| 536 |
} |
| 537 |
|
| 538 |
## Display result |
| 539 |
# Filename |
| 540 |
$text_widget insert insert $relative_filename |
| 541 |
$text_widget tag add tag_filename [list insert linestart] insert |
| 542 |
set text_idx [$text_widget index insert] |
| 543 |
# ":" |
| 544 |
$text_widget insert insert ": " |
| 545 |
$text_widget tag add tag_normal $text_idx insert |
| 546 |
set text_idx [$text_widget index insert] |
| 547 |
# Line number |
| 548 |
$text_widget insert insert $line_number |
| 549 |
$text_widget tag add tag_linenumber $text_idx insert |
| 550 |
set text_idx [$text_widget index insert] |
| 551 |
# ":" |
| 552 |
$text_widget insert insert ": " |
| 553 |
$text_widget tag add tag_normal $text_idx insert |
| 554 |
set text_idx [$text_widget index insert] |
| 555 |
scan $text_idx {%d.%d} row col |
| 556 |
# Line content and highlight pattern found |
| 557 |
$text_widget insert insert $line |
| 558 |
foreach index $indexes length $lengths { |
| 559 |
$text_widget tag add tag_highlight \ |
| 560 |
$row.[expr {$col + $index}] \ |
| 561 |
$row.[expr {$col + $index + $length}] |
| 562 |
} |
| 563 |
$text_widget insert insert "\n" |
| 564 |
} |
| 565 |
|
| 566 |
# Close file |
| 567 |
catch { |
| 568 |
close $file |
| 569 |
} |
| 570 |
return 0 |
| 571 |
} |
| 572 |
|
| 573 |
## Abort search |
| 574 |
# @return void |
| 575 |
public method findinfiles_stop {} { |
| 576 |
set abort_variable 1 |
| 577 |
} |
| 578 |
|
| 579 |
## Clear results |
| 580 |
# @return void |
| 581 |
public method findinfiles_clear {} { |
| 582 |
# Clear text widget |
| 583 |
$text_widget configure -state normal |
| 584 |
$text_widget delete 0.0 end |
| 585 |
$text_widget configure -state disabled |
| 586 |
|
| 587 |
# Adjust controls |
| 588 |
$menu entryconfigure [::mc "Clear"] -state disabled |
| 589 |
$clear_button configure -state disabled |
| 590 |
} |
| 591 |
|
| 592 |
## Invoke popup menu for the text widget |
| 593 |
# @parm Int X - Absolute mouse pointer position (X axis) |
| 594 |
# @parm Int Y - Absolute mouse pointer position (Y axis) |
| 595 |
# @parm Int x - Relative mouse pointer position (X axis) |
| 596 |
# @parm Int y - Relative mouse pointer position (Y axis) |
| 597 |
# @return void |
| 598 |
public method findinfiles_popupmenu {X Y x y} { |
| 599 |
findinfiles_click $x $y |
| 600 |
set index [$text_widget index [list @$x,$y linestart]] |
| 601 |
if {[$text_widget compare $index == [list $index lineend]]} { |
| 602 |
set state disabled |
| 603 |
} else { |
| 604 |
set state normal |
| 605 |
} |
| 606 |
$menu entryconfigure [::mc "Go to"] -state $state |
| 607 |
tk_popup $menu $X $Y |
| 608 |
} |
| 609 |
|
| 610 |
## Select line in the text widget by mouse click |
| 611 |
# @parm Int x - Relative mouse pointer position (X axis) |
| 612 |
# @parm Int y - Relative mouse pointer position (Y axis) |
| 613 |
# @return void |
| 614 |
public method findinfiles_click {x y} { |
| 615 |
set index [$text_widget index [list @$x,$y linestart]] |
| 616 |
$text_widget tag remove tag_cur_line 0.0 end |
| 617 |
if {[$text_widget compare $index != [list $index lineend]]} { |
| 618 |
$text_widget tag add tag_cur_line $index $index+1l |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
## Handle <DoubleButton-1> event on the text widget |
| 623 |
# - Switch editor and go to specified line |
| 624 |
# @parm Int x - Relative mouse pointer position (X axis) |
| 625 |
# @parm Int y - Relative mouse pointer position (Y axis) |
| 626 |
# @return void |
| 627 |
public method findinfiles_doubleclick {x y} { |
| 628 |
editor_goto_line [$text_widget index [list @$x,$y linestart]] |
| 629 |
} |
| 630 |
|
| 631 |
## Activate hypertext link on the specified index |
| 632 |
# @parm TextIndex index - Index of linestart |
| 633 |
# @return void |
| 634 |
private method editor_goto_line {index} { |
| 635 |
# Determinate line number and relative name of file |
| 636 |
set filename [$text_widget tag nextrange tag_filename $index [list $index lineend]] |
| 637 |
set linenumber [$text_widget tag nextrange tag_linenumber $index [list $index lineend]] |
| 638 |
set filename [$text_widget get [lindex $filename 0] [lindex $filename 1]] |
| 639 |
set linenumber [$text_widget get [lindex $linenumber 0] [lindex $linenumber 1]] |
| 640 |
if {![string length $filename] || ![string length $linenumber]} { |
| 641 |
return |
| 642 |
} |
| 643 |
|
| 644 |
# Switch editor |
| 645 |
set current_filename [lindex [$this editor_procedure {} getFileName {}] 1] |
| 646 |
if {$filename != $current_filename} { |
| 647 |
if {![$this fucus_specific_editor $filename 1]} { |
| 648 |
set filename [file join $folder $filename] |
| 649 |
if {[$this openfile $filename 1 . def def 0 0 {}] != {}} { |
| 650 |
$this switch_to_last |
| 651 |
update idletasks |
| 652 |
$this editor_procedure {} parseAll {} |
| 653 |
} else { |
| 654 |
return |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
# Go to target line |
| 660 |
$this editor_procedure {} focus_in {} |
| 661 |
$this editor_procedure {} goto $linenumber |
| 662 |
} |
| 663 |
|
| 664 |
## Validator function for all entry widgets |
| 665 |
# Conditionaly disables button "Find" |
| 666 |
# @parm Int for_what - Number of entrybox from which this function was invoked |
| 667 |
# 0 - Pattern |
| 668 |
# 1 - Folder |
| 669 |
# 2 - Mask |
| 670 |
# @parm String content - String to validate |
| 671 |
# @return Bool - Always 1 |
| 672 |
public method findinfiles_validate_crit_ent {for_what content} { |
| 673 |
if {![winfo exists $find_stop_button]} { |
| 674 |
return 1 |
| 675 |
} |
| 676 |
set state normal |
| 677 |
set string {} |
| 678 |
foreach var [list pattern_$obj_idx folder_$obj_idx mask_$obj_idx] \ |
| 679 |
number {0 1 2} \ |
| 680 |
{ |
| 681 |
if {$for_what == $number} { |
| 682 |
set string $content |
| 683 |
} else { |
| 684 |
set string [subst -nocommands "\$::FindInFiles::$var"] |
| 685 |
} |
| 686 |
if {![string length $string]} { |
| 687 |
set state disabled |
| 688 |
break |
| 689 |
} |
| 690 |
} |
| 691 |
$find_stop_button configure -state $state |
| 692 |
return 1 |
| 693 |
} |
| 694 |
|
| 695 |
## Invoke helwindow for entrybox "Mask" |
| 696 |
# @return void |
| 697 |
public method findinfiles_hlp {} { |
| 698 |
# Destroy legend window |
| 699 |
if {[winfo exists .findinfiles_help_win]} { |
| 700 |
grab release .findinfiles_help_win |
| 701 |
destroy .findinfiles_help_win |
| 702 |
return |
| 703 |
} |
| 704 |
set x [expr {[winfo pointerx .] + 10}] |
| 705 |
set y [winfo pointery .] |
| 706 |
|
| 707 |
# Create legend window |
| 708 |
set win [toplevel .findinfiles_help_win -class {Help} -bg ${::COMMON_BG_COLOR}] |
| 709 |
set frame [frame $win.f -bg {#555555} -bd 0 -padx 1 -pady 1] |
| 710 |
wm overrideredirect $win 1 |
| 711 |
|
| 712 |
# Click to close |
| 713 |
bind $win <Button-1> "grab release $win; destroy $win" |
| 714 |
|
| 715 |
# Create header "-- click to close --" |
| 716 |
pack [label $frame.lbl_header \ |
| 717 |
-text [mc "-- click to close --"] \ |
| 718 |
-bg {#FFFF55} -font $::smallfont \ |
| 719 |
-fg {#000000} -anchor c \ |
| 720 |
] -side top -anchor c -fill x |
| 721 |
|
| 722 |
# Create text widget |
| 723 |
set text [text $frame.text \ |
| 724 |
-bg {#FFFFCC} \ |
| 725 |
-exportselection 0 \ |
| 726 |
-takefocus 0 \ |
| 727 |
-cursor left_ptr \ |
| 728 |
-bd 0 -relief flat \ |
| 729 |
-font ${::Editor::defaultFont} \ |
| 730 |
] |
| 731 |
|
| 732 |
pack $frame -fill both -expand 1 |
| 733 |
|
| 734 |
# Fill the text widget |
| 735 |
$text insert end [mc "Comma separated list of file masks (e.g \"*.c,*.h,*.asm\")\n"] |
| 736 |
$text insert end [mc "The mask may contain any of the following special characters:\n"] |
| 737 |
$text insert end [mc " ? Matches any single character.\n"] |
| 738 |
$text insert end [mc " * Matches any sequence of zero or more characters.\n"] |
| 739 |
$text insert end [mc " \[chars\] Matches any single character in chars.\n"] |
| 740 |
$text insert end [mc " If chars contains a sequence of the form a-b then any\n"] |
| 741 |
$text insert end [mc " character between a and b (inclusive) will match.\n"] |
| 742 |
$text insert end [mc " \\x Matches the character x."] |
| 743 |
|
| 744 |
# Show the text |
| 745 |
$text configure -state disabled |
| 746 |
pack $text -side bottom -fill both -expand 1 |
| 747 |
|
| 748 |
# Show the window |
| 749 |
wm geometry $win "=600x180+$x+$y" |
| 750 |
update |
| 751 |
catch { |
| 752 |
grab -global $win |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
## Menu action "Go to" |
| 757 |
# @return void |
| 758 |
public method findinfiles_goto_cur_line {} { |
| 759 |
set index [lindex [$text_widget tag nextrange tag_cur_line 1.0 end] 0] |
| 760 |
if {$index == {}} { |
| 761 |
return |
| 762 |
} |
| 763 |
editor_goto_line $index |
| 764 |
} |
| 765 |
|
| 766 |
## Get configuration list for this panel |
| 767 |
# - Intented for session management |
| 768 |
# @return void |
| 769 |
public method findinfiles_get_config {} { |
| 770 |
return [list \ |
| 771 |
[subst -nocommands "\$::FindInFiles::recursive_$obj_idx"] \ |
| 772 |
[subst -nocommands "\$::FindInFiles::regular_expr_$obj_idx"] \ |
| 773 |
[subst -nocommands "\$::FindInFiles::case_sensitive_$obj_idx"] \ |
| 774 |
[subst -nocommands "\$::FindInFiles::folder_$obj_idx"] \ |
| 775 |
[subst -nocommands "\$::FindInFiles::mask_$obj_idx"] \ |
| 776 |
[subst -nocommands "\$::FindInFiles::pattern_$obj_idx"] \ |
| 777 |
] |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
# >>> File inclusion guard |
| 782 |
} |
| 783 |
# <<< File inclusion guard |