Class: Find

Inherits:
Findview
  • Object
show all
Defined in:
ext/ae-editor/ae-editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_frame, _controller) ⇒ Find

Returns a new instance of Find.



1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
# File 'ext/ae-editor/ae-editor.rb', line 1983

def initialize(_frame, _controller)
  super(_frame)
  #@l_file.configure('text'=>_title)
  #Tk.tk_call('wm', 'title', self, _title )
  @controller = _controller
  @forwards = true
  @find_action = proc{
    #_radio = @rb.get_value
    
    #if _radio == 'line'
    #  go_line
    #else
      do_find_next
    #end
  }
  @b_go.bind('1', @find_action)
  
		@b_replace.bind('1', proc{do_replace})  
  
		@b_replace_all.bind('1', proc{do_replace_all})  

  e =  TkWinfo.children(@e_what)[0]
  e.bind_append('KeyPress'){|e|
    case e.keysym
    when 'Return'
      @find_action.call
      hide
      @editor_caller.text.focus
      Tk.callback_break
    else
      widget_state
    end
  }
  e2 =  TkWinfo.children(@e_with)[0]
  e2.bind_append('KeyPress'){|e|
      widget_state
  }
  @last_index='insert'
  
  @goto_line_dialog = GoToLine.new(_frame).hide
  @goto_line_dialog.on_close=proc{@goto_line_dialog.hide}

  @goto_line_dialog.b_go.bind('1',proc{go_line})
end

Instance Attribute Details

#e_whatObject (readonly)

Returns the value of attribute e_what.



1982
1983
1984
# File 'ext/ae-editor/ae-editor.rb', line 1982

def e_what
  @e_what
end

Instance Method Details

#do_find(_istart = nil) ⇒ Object



2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
# File 'ext/ae-editor/ae-editor.rb', line 2103

def do_find(_istart=nil)
  @forwards =  @cb_back.cget('onvalue') != @cb_back.cget('variable').value.to_i
  _found = false
  @idx1 = nil
  @idx2 = nil
  if @e_what.text.length > 0
    update_combo(@e_what.text)
    if !_istart && self.editor.text.index('insert')!=nil
      _istart ='insert'
    elsif defined?(@last_index)
    		_istart = @last_index
    else
      _istart = '1.0'
    end
    if @forwards
      if @cb_reg.cget('onvalue')==@cb_reg.cget('variable').value.to_i
        _index = self.editor.text.tksearch(['regexp'],@e_what.text,_istart)
      else
        _index = self.editor.text.search(@e_what.text,_istart)
      end
    else

      if @cb_reg.cget('onvalue')==@cb_reg.cget('variable').value.to_i
        _index = self.editor.text.tksearch(['regexp','backwards'],@e_what.text,_istart)
      else
        _index = self.editor.text.tksearch(['backwards'],@e_what.text,_istart)
      end
    end
    if _index && _index.length>0
      self.editor.text.see(_index)
      _row, _col = _index.split('.')
      _index_sel_end = _row.to_i.to_s+'.'+(_col.to_i+@e_what.text.length).to_i.to_s
      if @forwards
        @last_index= _index_sel_end
      else
        @last_index= _row.to_i.to_s+'.'+(_col.to_i-1).to_i.to_s
      end
      self.editor.text.tag_add('sel', _index,_index_sel_end)
      self.editor.text.set_insert(_index)
      @idx1 =_index
      @idx2 =_index_sel_end
      _found = true
      @controller.bookmark_add(self.editor.file, _index)
    else
      _message = '"'+@e_what.value+'" not found'
      TkDialog2.new('message'=>_message, 'buttons'=>['Ok']).show()
    end

  else
    self.show()
  end
  return _found
end

#do_find_nextObject



2157
2158
2159
2160
2161
2162
# File 'ext/ae-editor/ae-editor.rb', line 2157

def do_find_next
  if @idx1 != nil
  		self.editor.text.tag_remove('sel',@idx1,@idx2)
  end
  do_find(@last_index)
end

#do_replaceObject



2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
# File 'ext/ae-editor/ae-editor.rb', line 2028

def do_replace
  if do_find_next
      _message = 'Replace "'+@e_what.value+'" with "'+@e_with.value+'" ?'
      if TkDialog2.new('message'=>_message, 'buttons'=>['Yes','No']).show() == 0
        self.editor.text.delete(@idx1,@idx2)
        self.editor.text.insert(@idx1,@e_with.value)
        self.editor.check_modify
      end
  end
end

#do_replace_allObject



2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
# File 'ext/ae-editor/ae-editor.rb', line 2039

def do_replace_all
  while do_find_next
      _message = 'Replace "'+@e_what.value+'" with "'+@e_with.value+'" ?'
      _rc = TkDialog2.new('message'=>_message, 'buttons'=>['Yes','No','Annulla']).show()
      if _rc == 0
        self.editor.text.delete(@idx1,@idx2)
        self.editor.text.insert(@idx1,@e_with.value)
        self.editor.check_modify
      elsif _rc == 2
        break
      end
  end
end

#editorObject



2064
2065
2066
2067
2068
2069
# File 'ext/ae-editor/ae-editor.rb', line 2064

def editor
  if @editor_caller == nil
    @editor_caller = @controller.raised
  end
  return @editor_caller
end

#go_lineObject



2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
# File 'ext/ae-editor/ae-editor.rb', line 2075

def go_line
  if @goto_line_dialog.e_line.value.length > 0
    _row = @goto_line_dialog.e_line.value
    _index = _row+'.1'
    self.editor.text.see(_index)
    self.editor.text.tag_add('sel', _index,_index+' lineend')
    self.editor.text.set_insert(_index)
    @controller.bookmark_add(self.editor.file, _index)
  @goto_line_dialog.hide
  end
  #self.hide()
end

#show_go_to_line_dialogObject



2071
2072
2073
# File 'ext/ae-editor/ae-editor.rb', line 2071

def show_go_to_line_dialog
  @goto_line_dialog.show
end

#update_combo(_txt) ⇒ Object



2096
2097
2098
2099
2100
2101
# File 'ext/ae-editor/ae-editor.rb', line 2096

def update_combo(_txt)
  values = @e_what.cget('values')
  if (values != nil && !values.include?(_txt))
    @e_what.insert('end', @e_what.value)
  end
end

#use(_editor) ⇒ Object



2088
2089
2090
2091
2092
2093
2094
# File 'ext/ae-editor/ae-editor.rb', line 2088

def use(_editor)
  if (_editor != @editor_caller)
    @last_index='insert'
    @editor_caller = _editor
    title(_editor.file)
  end
end

#widget_stateObject



2053
2054
2055
2056
2057
2058
2059
2060
2061
# File 'ext/ae-editor/ae-editor.rb', line 2053

def widget_state
 		if (@e_what.value.length > 0) && (@e_with.value.length > 0)
 			@b_replace.configure('state'=>'active')
 			@b_replace_all.configure('state'=>'active')
 		else
 			@b_replace.configure('state'=>'disabled')
 			@b_replace_all.configure('state'=>'disabled')
 		end
end