Class: FilesHistrory

Inherits:
ArcadiaExt show all
Defined in:
ext/ae-file-history/ae-file-history.rb

Instance Attribute Summary collapse

Attributes inherited from ArcadiaExt

#arcadia, #name

Instance Method Summary collapse

Methods inherited from ArcadiaExt

#conf, #conf_array, #conf_default, #exec, #float_frame, #frame, #frame_def_visible?, #frame_domain, #frame_domain_default, #frame_visible?, #initialize, #maximize, #maximized?, #resize, #restore_default_conf

Constructor Details

This class inherits a constructor from ArcadiaExt

Instance Attribute Details

#htreeObject (readonly)

Returns the value of attribute htree.



77
78
79
# File 'ext/ae-file-history/ae-file-history.rb', line 77

def htree
  @htree
end

Instance Method Details

#add2history(_filename, _text_index = '1.0') ⇒ Object



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
# File 'ext/ae-file-history/ae-file-history.rb', line 484

def add2history(_filename, _text_index='1.0')
  if _filename && File.exist?(_filename)
    _filename = _filename.sub(Dir::pwd+'/',"")
    _filename = File.expand_path(_filename)
    _filename_index = _filename+";"+_text_index
   	 #Arcadia.new_error_msg(self, "add2history _filename=#{_filename}")
   	 #Arcadia.new_error_msg(self, "add2history history_file=#{history_file}")

    if File.exist?(history_file)
      f = File::open(history_file,'r')
      begin
        _lines = f.readlines.collect!{| line | line.chomp}
      ensure
        f.close unless f.nil?
      end
      f = File.new(history_file, "w")
      begin
        if f
          _l= conf('length').to_i
          f.syswrite(_filename_index+"\n")
          _lines[0.._l-2].each{|_line|
            _lfile = _line.split(';')
            if _lfile
              f.syswrite(_line+"\n") if (_lfile[0] != _filename)&&(_line.length > 0)
            end
          }
        end
      ensure
        f.close unless f.nil?
      end
    else
   			dir,fil =File.split(File.expand_path(history_file))
   			if !File.exist?(dir)
   			  Dir.mkdir(dir)
   			end
      f = File.new(history_file, "w+")
      begin
        f.syswrite(_filename+"\n") if f
      ensure
        f.close unless f.nil?
      end
    end
  end
end

#add_to_tree(_file) ⇒ Object



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
# File 'ext/ae-file-history/ae-file-history.rb', line 396

def add_to_tree(_file)
_d, _f = File.split(File.expand_path(_file))
return if _f.nil?
#Arcadia.new_error_msg(self, "add_to_tree _file=#{_file}")
#Arcadia.new_error_msg(self, "add_to_tree _d=#{_d}")
#Arcadia.new_error_msg(self, "add_to_tree _f=#{_f}")
_d = _d.downcase if Arcadia.is_windows?
_f = _f.downcase if Arcadia.is_windows?
  #_foreground = conf('color.foreground')
node = root.dir(_d)

node_stack = Array.new
node_stack << node
_parent = node.parent
while _parent.rif != 'root'
  node_stack << _parent
  _parent = _parent.parent
end
node_stack.reverse!

node_stack.each{|_node|
    if !@htree.exist?(_node.rif)
      @htree.insert('end', _node.parent.rif ,_node.rif, {
          'text' =>  _node.label,
          'helptext' => _node.rif,
          'deltax'=>-1,
          'image'=> image('KDir')
        }.update(Arcadia.style('treeitem'))
      )
    end
}
#    if [email protected]?(node.rif)
#      @htree.insert('end', node.parent.rif ,node.rif, {
#          'text' =>  node.label,
#          'helptext' => node.rif,
#          'deltax'=>-1,
#          'image'=> image('KDir')
#        }.update(Arcadia.style('treeitem'))
#      )
#    end


  _file_node_rif = _d+'@@@'+_f
  if !@htree.exist?(_file_node_rif)
    @htree.insert('end', node.rif ,_file_node_rif, {
        'text' =>  _f ,
        'helptext' => _f,
        'deltax'=>-1,
        #'fill' => _foreground,
        #'font'=>@font_b,
        'image'=> image('KFile',_f)
      }.update(Arcadia.style('treeitem'))
    )
  end
  
end

#build_treeObject



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
# File 'ext/ae-file-history/ae-file-history.rb', line 296

def build_tree
  file_dir = Hash.new
  nodes = Array.new
  if File.exist?(history_file)
    f = File::open(history_file,'r')
    f_open = $arcadia['pers']['editor.files.open'].split("|") if $arcadia['pers']['editor.files.open']
    begin
      _lines = f.readlines.collect!{| line | line.chomp+"\n" }
      _lines.sort.each{|_line|
        _file = _line.split(';')[0]
        if FileTest::exist?(_file)
				dir,fil =File.split(File.expand_path(_file))
				dir = dir.downcase if Arcadia.is_windows?
				fil = fil.downcase if Arcadia.is_windows?
				file_dir[dir] = Array.new if file_dir[dir] == nil
				file_dir[dir] << fil if !file_dir[dir].include?(fil)
        end
      }
    ensure
      f.close unless f.nil?
    end
  end

  file_dir.keys.sort.each{|_dir|
  				node = root.dir(_dir)
		file_dir[_dir].each{|file|
		  TreeNode.new(node, 'KFile'){|_node|
            _node.rif= _dir+'@@@'+file
            _node.label=file
		  }
		}
  }

  #@image_kdir = TkPhotoImage.new('dat' => BOOK_GIF)
  @image_kdir = TkPhotoImage.new('dat' => ICON_FOLDER_OPEN_GIF)
#    @image_kfile_rb = TkPhotoImage.new('dat' => RUBY_DOCUMENT_GIF)
#    @image_kfile = TkPhotoImage.new('dat' => DOCUMENT_GIF)

 build_tree_from_node(root)
end

#build_tree_from_node(_node) ⇒ Object



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'ext/ae-file-history/ae-file-history.rb', line 465

def build_tree_from_node(_node)
  #_foreground = conf('color.foreground')
  _sorted_sons = _node.sons.sort
  for inode in 0.._sorted_sons.length - 1
    _son = _sorted_sons[inode]
    @htree.insert('end', _son.parent.rif ,_son.rif, {
      'text' =>  _son.label ,
   #   'fill' => _foreground,
      'helptext' => _son.helptext,
      'deltax'=>-1,
    #  'font'=>@font,
      'image'=> image(_son.kind, _son.label)
    }.update(Arcadia.style('treeitem'))
    )
    build_tree_from_node(_son)
  end
end

#file2node(_file) ⇒ Object



209
210
211
212
213
214
215
# File 'ext/ae-file-history/ae-file-history.rb', line 209

def file2node(_file)
  if _file.include?("\s") && _file[0..0]!='{'
    return "{#{_file}}"
  else
    return _file
  end
end

#history_fileObject



180
181
182
183
184
185
# File 'ext/ae-file-history/ae-file-history.rb', line 180

def history_file
   if !defined?(@arcadia_history_file)
   		@arcadia_history_file = @arcadia.local_dir+'/'+conf('file.name')
   end
   return @arcadia_history_file
end

#image(_kind, _label = '.rb') ⇒ Object



453
454
455
456
457
458
459
460
461
462
463
# File 'ext/ae-file-history/ae-file-history.rb', line 453

def image(_kind, _label='.rb')
    if _kind == 'KDir'
      return @image_kdir
    elsif _kind == 'KFile'
      return Arcadia.file_icon(_label)
#      elsif _kind == 'KFile' && _label.include?('.rb')
#        return @image_kfile_rb
#      else
#        return @image_kfile
    end
end

#node2file(_node) ⇒ Object

def on_after_close_buffer(_event)

  if _event.file
    self.add2history(_event.file)
    add_to_tree(_event.file)
  end
end


201
202
203
204
205
206
207
# File 'ext/ae-file-history/ae-file-history.rb', line 201

def node2file(_node)
  if _node[0..0]=='{' && _node[-1..-1]=='}'
    return _node[1..-2]
  else
    return _node
  end
end

#on_after_build(_event) ⇒ Object



173
174
175
176
177
178
# File 'ext/ae-file-history/ae-file-history.rb', line 173

def on_after_build(_event)
   Arcadia.attach_listener(self, OpenBufferEvent)
   #Arcadia.attach_listener(self, BufferRaisedEvent)
   self.frame.show
   #Arcadia.attach_listener(self, CloseBufferEvent)
end

#on_after_open_buffer(_event) ⇒ Object



187
188
189
190
191
192
# File 'ext/ae-file-history/ae-file-history.rb', line 187

def on_after_open_buffer(_event)
  if _event.file && File.exist?(_event.file)
    self.add2history(_event.file)
    add_to_tree(_event.file)
  end
end

#on_before_build(_event) ⇒ Object



88
89
90
# File 'ext/ae-file-history/ae-file-history.rb', line 88

def on_before_build(_event)
  Arcadia.attach_listener(self, BufferRaisedEvent)
end

#on_buffer_raised(_event) ⇒ Object



265
266
267
268
269
270
271
# File 'ext/ae-file-history/ae-file-history.rb', line 265

def on_buffer_raised(_event)
  return if _event.file.nil?
  @last_file = _event.file
  if @sync
    select_file_without_event(_event.file)
  end
end

#on_build(_event) ⇒ Object



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
# File 'ext/ae-file-history/ae-file-history.rb', line 92

def on_build(_event)
   @h_stack = Array.new
   @cb_sync = TkCheckButton.new(self.frame.hinner_frame, Arcadia.style('checkbox')){
     text  'Sync'
     justify  'left'
     indicatoron 0
     offrelief 'raised'
     image TkPhotoImage.new('dat' => SYNCICON20_GIF)
     place('x' => 0,'y' => 0,'height' => 26)
   }

   Tk::BWidget::DynamicHelp::add(@cb_sync, 
     'text'=>'Link open editors with content in the Navigator')

   do_check = proc {
     if @cb_sync.cget('onvalue')==@cb_sync.cget('variable').value.to_i
       sync_on
     else
       sync_off
     end
   }
   @sync = false
   @cb_sync.command(do_check)
   
   do_select_item = proc{|_self|
     _selected = @htree.selected
     _dir, _file = _selected.sub("%%%",":").split('@@@')
     if _file
       _file = File.expand_path( _file  , _dir ) 
#	    else
#	      _file = Tk.getOpenFile('initialdir'=>_dir)
    end
    if _file && _file.strip.length > 0 
      Arcadia.process_event(OpenBufferTransientEvent.new(self,'file'=>_file))
     end
   }
   
   do_open_folder = proc{|_node|
     children = @htree.nodes(_node)
     if children.length == 1 
       @htree.open_tree(children[0],false)
     end
   }    

   @font =  Arcadia.conf('treeitem.font')
   @font_b = "#{Arcadia.conf('treeitem.font')} bold"

  @htree = BWidgetTreePatched.new(self.frame.hinner_frame, Arcadia.style('treepanel')){
     showlines false
     deltay 18
     opencmd proc{|node| do_open_folder.call(node)} 
     selectcommand proc{ do_select_item.call(self) } 
#      place('relwidth' => 1,'relheight' => '1', 'x' => '0','y' => '22', 'height' => -22)
   }
   @htree.extend(TkScrollableWidget).show(0,26)

   do_double_click = proc{
       #_selected = @htree.selection_get[0]
       _selected = @htree.selected
       _dir, _file = _selected.sub("%%%",":").split('@@@')
       if _dir && _file.nil? && File.ftype(node2file(_dir)) == 'directory'
        _file = Tk.getOpenFile('initialdir'=>node2file(_dir))
     	    if _file && _file.strip.length > 0 
     	       Arcadia.process_event(OpenBufferTransientEvent.new(self,'file'=>_file))
          end
#          if !_selected.nil? && @htree.open?(node2file(_selected))
#            @htree.close_tree(node2file(_selected))
#          elsif !_selected.nil?
#            @htree.open_tree(node2file(_selected),false) 
#            do_open_folder.call(_selected)
#          end
       end
   }
   
   @htree.textbind_append('Double-1',do_double_click)
   
   self.build_tree
   self.pop_up_menu_tree

end

#pop_up_menu_treeObject



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
# File 'ext/ae-file-history/ae-file-history.rb', line 337

def pop_up_menu_tree
  @pop_up_tree = TkMenu.new(
    :parent=>@htree,
    :tearoff=>0,
    :title => 'Menu tree'
  )
  @pop_up_tree.extend(TkAutoPostMenu)
  @pop_up_tree.configure(Arcadia.style('menu'))
  #----- search submenu
  sub_ref_search = TkMenu.new(
    :parent=>@pop_up_tree,
    :tearoff=>0,
    :title => 'Ref'
  )
  sub_ref_search.extend(TkAutoPostMenu)
  sub_ref_search.configure(Arcadia.style('menu'))
  sub_ref_search.insert('end',
    :command,
    :label=>'Find in files...',
    :hidemargin => false,
    :command=> proc{
      _target = @htree.selected
      _dir, _file = _target.sub("%%%",":").split('@@@')
      if _dir
        Arcadia.process_event(SearchInFilesEvent.new(self,'dir'=>_dir))
      end
    }
  )
  
  sub_ref_search.insert('end',
    :command,
    :label=>'Act in files...',
    :hidemargin => false,
    :command=> proc{
      _target = @htree.selected
      _dir, _file = _target.sub("%%%",":").split('@@@')
      if _dir
        Arcadia.process_event(AckInFilesEvent.new(self,'dir'=>_dir))
      end
    }
  )
  @pop_up_tree.insert('end',
    :cascade,
    :label=>'Search from here',
    :menu=>sub_ref_search,
    :hidemargin => false
  )
  
  
  @htree.areabind_append("Button-3",
    proc{|x,y|
      _x = TkWinfo.pointerx(@htree)
      _y = TkWinfo.pointery(@htree)
      @pop_up_tree.popup(_x,_y)
    },
  "%x %y")
end

#rootObject

def do_editor_event(_event)

 case _event.signature 
 		when EditorContract::FILE_AFTER_OPEN
 		  if _event.context.file
		      self.add2history(_event.context.file)
		      add_to_tree(_event.context.file)
 			  end
 		when EditorContract::FILE_AFTER_CLOSE
 		  if _event.context.file 
 		end
 end

end



286
287
288
289
290
291
292
293
294
# File 'ext/ae-file-history/ae-file-history.rb', line 286

def root
  if !defined?(@root)
    @root = TreeNode.new(nil, 'KRoot'){|_node|
      _node.rif= 'root'
      _node.label=''
    }
  end
  return @root
end

#select_file_without_event(_file) ⇒ Object

def selected

  if @htree.selection_get[0]
    if @htree.selection_get[0].length >0
     	_selected = ""
      if String.method_defined?(:lines)
    	   selection_lines = @htree.selection_get[0].lines
      else
    	   selection_lines = @htree.selection_get[0].split("\n")
      end
      selection_lines.each{|_block|
        _selected = _selected + _block.to_s + "\s" 
      }
      _selected = _selected.strip
    else
      _selected = @htree.selection_get[0]
    end
  end
  return _selected
end


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
# File 'ext/ae-file-history/ae-file-history.rb', line 238

def select_file_without_event(_file)
  _d, _f = File.split(File.expand_path(_file))
  _d = _d.downcase if Arcadia.is_windows?
  _f = _f.downcase if Arcadia.is_windows?
  _file_node_rif = _d+'@@@'+_f
  if @htree.exist?(_file_node_rif)
    _proc = @htree.cget('selectcommand')
    @htree.configure('selectcommand'=>nil)
    #_proc = @htree.selectcommand
    #@htree.selectcommand(proc{nil})
    begin
      parent = root.dir(_d)
      @htree.selection_clear
      @htree.selection_add(_file_node_rif)
      #@htree.close_tree('root')
      while !parent.nil? && parent.rif != 'root'
        @htree.open_tree(parent.rif, false)
        parent = parent.parent
      end
      @htree.see(_file_node_rif)
    ensure
      #@htree.selectcommand(_proc)
      @htree.configure('selectcommand'=>_proc)
    end
  end
end

#sync_offObject



84
85
86
# File 'ext/ae-file-history/ae-file-history.rb', line 84

def sync_off
  @sync = false
end

#sync_onObject



79
80
81
82
# File 'ext/ae-file-history/ae-file-history.rb', line 79

def sync_on
  @sync = true
  select_file_without_event(@last_file) if @last_file
end