2005-03-08 04:19:03 -05:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
#
|
2006-11-06 01:56:37 -05:00
|
|
|
# TkTextIO class :: handling I/O stream on a TkText widget
|
|
|
|
# by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
|
2005-03-08 04:19:03 -05:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# NOTE: TkTextIO supports 'character' (not 'byte') access only.
|
|
|
|
# So, for example, TkTextIO#getc returns a character, TkTextIO#pos
|
|
|
|
# means the character position, TkTextIO#read(size) counts by
|
2005-03-08 04:48:46 -05:00
|
|
|
# characters, and so on.
|
2009-03-05 22:56:38 -05:00
|
|
|
# Of course, it is available to make TkTextIO class to suuport
|
|
|
|
# 'byte' access. However, it may break multi-byte characters.
|
2005-03-08 04:48:46 -05:00
|
|
|
# and then, displayed string on the text widget may be garbled.
|
2009-03-05 22:56:38 -05:00
|
|
|
# I think that it is not good on the supposed situation of using
|
|
|
|
# TkTextIO.
|
2005-03-08 04:48:46 -05:00
|
|
|
#
|
2005-03-08 04:19:03 -05:00
|
|
|
require 'tk'
|
2006-11-06 01:56:37 -05:00
|
|
|
require 'tk/text'
|
|
|
|
require 'tk/textmark'
|
|
|
|
require 'thread'
|
2005-03-08 04:19:03 -05:00
|
|
|
|
|
|
|
class TkTextIO < TkText
|
2006-11-06 01:56:37 -05:00
|
|
|
# keep safe level
|
|
|
|
@@create_queues = proc{ [Queue.new, Mutex.new, Queue.new, Mutex.new] }
|
|
|
|
|
|
|
|
OPT_DEFAULTS = {
|
|
|
|
'mode' => nil,
|
2009-03-05 22:56:38 -05:00
|
|
|
'overwrite' => false,
|
|
|
|
'text' => nil,
|
|
|
|
'show' => :pos,
|
|
|
|
'wrap' => 'char',
|
|
|
|
'sync' => true,
|
|
|
|
'prompt' => nil,
|
|
|
|
'prompt_cmd' => nil,
|
|
|
|
'hist_size' => 1000,
|
2006-11-06 01:56:37 -05:00
|
|
|
}
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
def create_self(keys)
|
2006-11-06 01:56:37 -05:00
|
|
|
opts = _get_io_params((keys.kind_of?(Hash))? keys: {})
|
2005-03-08 04:19:03 -05:00
|
|
|
|
|
|
|
super(keys)
|
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
@count_var = TkVariable.new
|
2005-03-08 04:19:03 -05:00
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
@write_buffer = ''
|
|
|
|
@read_buffer = ''
|
|
|
|
@buf_size = 0
|
|
|
|
@buf_max = 1024
|
2005-03-08 23:39:25 -05:00
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
@write_buf_queue, @write_buf_mutex,
|
2006-11-06 01:56:37 -05:00
|
|
|
@read_buf_queue, @read_buf_mutex = @@create_queues.call
|
2005-03-09 01:08:33 -05:00
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
@idle_flush = TkTimer.new(:idle, 1, proc{ @flusher.run rescue nil })
|
|
|
|
@timer_flush = TkTimer.new(250, -1, proc{ @flusher.run rescue nil })
|
|
|
|
|
|
|
|
@flusher = Thread.new{ loop { Thread.stop; flush() } }
|
|
|
|
|
|
|
|
@receiver = Thread.new{
|
|
|
|
begin
|
|
|
|
loop {
|
|
|
|
str = @write_buf_queue.deq
|
|
|
|
@write_buf_mutex.synchronize { @write_buffer << str }
|
|
|
|
@idle_flush.start
|
|
|
|
}
|
|
|
|
ensure
|
|
|
|
@flusher.kill
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
@timer_flush.start
|
|
|
|
|
|
|
|
_setup_io(opts)
|
|
|
|
end
|
|
|
|
private :create_self
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@flusher.kill rescue nil
|
|
|
|
|
|
|
|
@idle_flush.stop rescue nil
|
|
|
|
@timer_flush.stop rescue nil
|
|
|
|
|
|
|
|
@receiver.kill rescue nil
|
|
|
|
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
|
|
|
####################################
|
|
|
|
|
|
|
|
def _get_io_params(keys)
|
|
|
|
opts = {}
|
2009-03-05 22:56:38 -05:00
|
|
|
self.class.const_get(:OPT_DEFAULTS).each{|k, v|
|
2006-11-06 01:56:37 -05:00
|
|
|
if keys.has_key?(k)
|
|
|
|
opts[k] = keys.delete(k)
|
|
|
|
else
|
|
|
|
opts[k] = v
|
|
|
|
end
|
|
|
|
}
|
|
|
|
opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def _setup_io(opts)
|
|
|
|
unless defined? @txtpos
|
|
|
|
@txtpos = TkTextMark.new(self, '1.0')
|
|
|
|
else
|
|
|
|
@txtpos.set('1.0')
|
|
|
|
end
|
|
|
|
@txtpos.gravity = :left
|
2005-03-08 23:39:25 -05:00
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
@lineno = 0
|
|
|
|
@line_offset = 0
|
2006-11-06 01:56:37 -05:00
|
|
|
|
2007-01-29 14:56:28 -05:00
|
|
|
@hist_max = opts['hist_size'].to_i
|
2006-11-06 01:56:37 -05:00
|
|
|
@hist_index = 0
|
|
|
|
@history = Array.new(@hist_max)
|
|
|
|
@history[0] = ''
|
|
|
|
|
|
|
|
self['wrap'] = wrap
|
|
|
|
|
|
|
|
self.show_mode = opts['show']
|
|
|
|
|
|
|
|
self.value = opts['text'] if opts['text']
|
|
|
|
|
|
|
|
@overwrite = (opts['overwrite'])? true: false
|
|
|
|
|
|
|
|
@sync = opts['sync']
|
|
|
|
|
|
|
|
@prompt = opts['prompt']
|
|
|
|
@prompt_cmd = opts['prompt_cmd']
|
2005-03-08 04:19:03 -05:00
|
|
|
|
2005-03-08 23:39:25 -05:00
|
|
|
@open = {:r => true, :w => true} # default is 'r+'
|
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
@console_mode = false
|
|
|
|
@end_of_stream = false
|
|
|
|
@console_buffer = nil
|
|
|
|
|
|
|
|
case opts['mode']
|
|
|
|
when nil
|
|
|
|
# do nothing
|
|
|
|
|
|
|
|
when :console, 'console'
|
|
|
|
@console_mode = true
|
|
|
|
# @console_buffer = TkTextIO.new(:mode=>'r')
|
|
|
|
@console_buffer = self.class.new(:mode=>'r')
|
|
|
|
self.show_mode = :insert
|
|
|
|
|
|
|
|
when 'r', 'rb'
|
2005-03-08 23:39:25 -05:00
|
|
|
@open[:r] = true; @open[:w] = nil
|
2005-03-08 04:19:03 -05:00
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
when 'r+', 'rb+', 'r+b'
|
2005-03-08 23:39:25 -05:00
|
|
|
@open[:r] = true; @open[:w] = true
|
2005-03-08 04:19:03 -05:00
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
when 'w', 'wb'
|
2005-03-08 23:39:25 -05:00
|
|
|
@open[:r] = nil; @open[:w] = true
|
2005-03-08 04:19:03 -05:00
|
|
|
self.value=''
|
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
when 'w+', 'wb+', 'w+b'
|
2005-03-08 23:39:25 -05:00
|
|
|
@open[:r] = true; @open[:w] = true
|
2005-03-08 04:19:03 -05:00
|
|
|
self.value=''
|
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
when 'a', 'ab'
|
2005-03-08 23:39:25 -05:00
|
|
|
@open[:r] = nil; @open[:w] = true
|
2006-11-06 01:56:37 -05:00
|
|
|
@txtpos.set('end - 1 char')
|
2005-03-08 23:39:25 -05:00
|
|
|
@txtpos.gravity = :right
|
2005-03-08 04:19:03 -05:00
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
when 'a+', 'ab+', 'a+b'
|
2005-03-08 04:19:03 -05:00
|
|
|
@open[:r] = true; @open[:w] = true
|
2006-11-06 01:56:37 -05:00
|
|
|
@txtpos.set('end - 1 char')
|
2005-03-08 23:39:25 -05:00
|
|
|
@txtpos.gravity = :right
|
2006-11-06 01:56:37 -05:00
|
|
|
|
|
|
|
else
|
|
|
|
fail ArgumentError, "unknown mode `#{opts['mode']}'"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless defined? @ins_head
|
|
|
|
@ins_head = TkTextMark.new(self, 'insert')
|
|
|
|
@ins_head.gravity = :left
|
|
|
|
end
|
|
|
|
|
|
|
|
unless defined? @ins_tail
|
|
|
|
@ins_tail = TkTextMark.new(self, 'insert')
|
|
|
|
@ins_tail.gravity = :right
|
|
|
|
end
|
|
|
|
|
|
|
|
unless defined? @tmp_mark
|
|
|
|
@tmp_mark = TkTextMark.new(self, 'insert')
|
|
|
|
@tmp_mark.gravity = :left
|
|
|
|
end
|
|
|
|
|
|
|
|
if @console_mode
|
|
|
|
_set_console_line
|
|
|
|
_setup_console_bindings
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private :_get_io_params, :_setup_io
|
|
|
|
|
|
|
|
def _set_console_line
|
|
|
|
@tmp_mark.set(@ins_tail)
|
|
|
|
|
|
|
|
mark_set('insert', 'end')
|
|
|
|
|
|
|
|
prompt = ''
|
|
|
|
prompt << @prompt_cmd.call if @prompt_cmd
|
|
|
|
prompt << @prompt if @prompt
|
|
|
|
insert(@tmp_mark, prompt)
|
|
|
|
|
|
|
|
@ins_head.set(@ins_tail)
|
|
|
|
@ins_tail.set('insert')
|
|
|
|
|
|
|
|
@txtpos.set(@tmp_mark)
|
|
|
|
|
|
|
|
_see_pos
|
|
|
|
end
|
|
|
|
|
|
|
|
def _replace_console_line(str)
|
|
|
|
self.delete(@ins_head, @ins_tail)
|
|
|
|
self.insert(@ins_head, str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def _get_console_line
|
|
|
|
@tmp_mark.set(@ins_tail)
|
|
|
|
s = self.get(@ins_head, @tmp_mark)
|
|
|
|
_set_console_line
|
|
|
|
s
|
|
|
|
end
|
|
|
|
private :_set_console_line, :_replace_console_line, :_get_console_line
|
|
|
|
|
|
|
|
def _cb_up
|
|
|
|
@history[@hist_index].replace(self.get(@ins_head, @ins_tail))
|
|
|
|
@hist_index += 1
|
|
|
|
@hist_index -= 1 if @hist_index >= @hist_max || !@history[@hist_index]
|
|
|
|
_replace_console_line(@history[@hist_index]) if @history[@hist_index]
|
|
|
|
Tk.callback_break
|
|
|
|
end
|
|
|
|
def _cb_down
|
|
|
|
@history[@hist_index].replace(self.get(@ins_head, @ins_tail))
|
|
|
|
@hist_index -= 1
|
|
|
|
@hist_index = 0 if @hist_index < 0
|
|
|
|
_replace_console_line(@history[@hist_index]) if @history[@hist_index]
|
|
|
|
Tk.callback_break
|
|
|
|
end
|
|
|
|
def _cb_left
|
|
|
|
if @console_mode && compare('insert', '<=', @ins_head)
|
|
|
|
mark_set('insert', @ins_head)
|
|
|
|
Tk.callback_break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def _cb_backspace
|
|
|
|
if @console_mode && compare('insert', '<=', @ins_head)
|
|
|
|
Tk.callback_break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def _cb_ctrl_a
|
|
|
|
if @console_mode
|
|
|
|
mark_set('insert', @ins_head)
|
|
|
|
Tk.callback_break
|
|
|
|
end
|
|
|
|
end
|
* ext/tk/tcltklib.c: SEGV when tcltk-stubs is enabled.
* ext/tk/tcltklib.c: avoid error on a shared object.
* ext/tk/extconf.rb: support --with-tcltkversion
* ext/tk/README.tcltklib: add document about --with-tcltkversion
* ext/tk/lib/tk.rb, ext/tk/lib/multi-tk.rb, ext/tk/lib/remote-tk.rb:
not work on $SAFE==4
* ext/tk/lib/multi-tk.rb: Object#methods returns Symbols on Ruby1.9.
* ext/tk/lib/tk/timer.rb: add TkTimer#at_end(proc) to register the
procedure which called at end of the timer.
* ext/tk/lib/tk.rb, ext/tk/lib/tk/itemfont.rb, ext/tk/lib/font.rb:
support __IGNORE_UNKNOWN_CONFIGURE_OPTION__ about font options.
* ext/tk/lib/*: treat __IGNORE_UNKNOWN_CONFIGURE_OPTION__
* ext/tk/lib/tkextlib/iwidgets/scrolledcanvas.rb,
ext/tk/lib/tkextlib/iwidgets/scrolledlistbox.rb,
ext/tk/lib/tkextlib/iwidgets/scrolledtext.rb: bug fix.
* ext/tk/lib/tk/text.rb: typo. call a wrong method.
* ext/tk/lib/tk/itemconfig.rb: ditto.
* ext/tk/lib/tk.rb, ext/tk/lib/tk/itemconfig.rb,
ext/tk/lib/tk/canvas.rb: support alias names of option keys.
* ext/tk/lib/tk/grid.rb: lack of module-method definitions.
* ext/tk/lib/tk/pack.rb, ext/tk/lib/tk/grid.rb: increase supported
parameter patterns of configure method.
* ext/tk/lib/tk.rb: add TkWindow#grid_anchor, grid_column, grid_row.
* ext/tk/lib/tk/wm.rb: methods of Tk::Wm_for_General module cannot
pass the given block to methods of Tk::Wm module.
* ext/tk/lib/tk/wm.rb: Wm#overrideredirect overwrites arguemnt to
an invalid value.
* ext/tk/lib/tk.rb: fix memory (object) leak bug.
* ext/tk/tcltklib.c, ext/tk/tkutil/tkutil.c: fix memory leak.
* ext/tk/sample/demos-jp/aniwave.rb, ext/tk/sample/demos-en/aniwave.rb:
bug fix.
* ext/tk/lib/tkextlib/blt/component.rb,
ext/tk/lib/tkextlib/tile/tentry.rb,
ext/tk/lib/tkextlib/tile/treeview.rb: ditto.
* ext/tk/lib/tkextlib/tile/tpaned.rb: improve TPaned#add.
* ext/tk/sample/demos-jp/widget, ext/tk/sample/demos-en/widget,
ext/tk/sample/demos-jp/style.rb, ext/tk/sample/demos-en/style.rb,
ext/tk/sample/demos-jp/bind.rb, ext/tk/sample/demos-en/bind.rb:
bug fix.
* ext/tk/sample/ttk_wrapper.rb: ditto.
* ext/tk/sample/ttk_wrapper.rb: support "if __FILE__ == $0" idiom.
* ext/tk/sample/tktextio.rb: add binding for 'Ctrl-u' at console mode.
* ext/tk/lib/tkextlib/tile.rb, ext/tk/lib/tkextlib/tile/style.rb,
ext/tk/sample/ttk_wrapper.rb: improve treating and control themes.
add Tk::Tile.themes and Tk::Tile.set_theme(theme).
* ext/tk/lib/tkextlib/tile.rb: lack of autoload definitions.
* ext/tk/lib/tkextlib/tile/tnotebook.rb: cannot use kanji (not UTF-8)
characters for headings.
* ext/tk/lib/tkextlib/tkDND/shape.rb: wrong package name.
* ext/tk/tkutil/tkutil.c: improve handling callback-subst-keys.
Now, support longnam-keys (e.g. '%CTT' on tkdnd-2.0; however, still
not support tkdnd-2.0 on tkextlib), and symbols of parameters (e.g.
:widget=>'%W', :keycode=>'%k', '%x'=>:x, '%X'=>:root_x, and so on;
those are attributes of event object). It means that Ruby/Tk accepts
not only "widget.bind(ev, '%W', '%k', ...){|w, k, ...| ... }", but
also "widget.bind(ev, :widget, :keycode, ...){|w, k, ...| ... }".
It is potentially incompatible, when user passes symbols to the
arguments of the callback block (the block receives the symbols as
strings). I think that is very rare case (probably, used by Ruby/Tk
experts only). When causes such trouble, please give strings instead
of such symbol parameters (e.g. call Symbol#to_s method).
* ext/tk/lib/tk/event.rb, ext/tk/lib/tk/validation.rb,
ext/tk/lib/tkextlib/blt/treeview.rb,
ext/tk/lib/tkextlib/winico/winico.rb: ditto.
* ext/tk/tkutil/tkutil.c: strings are available on subst_tables on
TkUtil::CallbackSubst class (it is useful on Ruby 1.9).
* ext/tk/lib/tk/spinbox.rb, ext/tk/lib/tkextlib/iwidgets/hierarchy.rb,
ext/tk/lib/tkextlib/iwidgets/spinner.rb,
ext/tk/lib/tkextlib/iwidgets/entryfield.rb,
ext/tk/lib/tkextlib/iwidgets/calendar.rb,
ext/tk/lib/tkextlib/blt/dragdrop.rb,
ext/tk/lib/tkextlib/tkDND/tkdnd.rb,
ext/tk/lib/tkextlib/treectrl/tktreectrl.rb,
ext/tk/lib/tkextlib/tktable/tktable.rb: disable code piece became
unnecessary by reason of the changes of ext/tk/tkutil/tkutil.c.
* ext/tk/lib/tk.rb, ext/tk/lib/multi-tk.rb: change strategy to define
the constant WITH_ENCODING.
* ext/tk/lib/tk.rb: fix bug on Tk::Encoding.tk_encoding_names.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17083 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-06-10 16:59:10 -04:00
|
|
|
def _cb_ctrl_u
|
|
|
|
if @console_mode
|
|
|
|
mark_set('insert', @ins_head)
|
|
|
|
delete('insert', 'insert lineend')
|
|
|
|
Tk.callback_break
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
private :_cb_up, :_cb_down, :_cb_left, :_cb_backspace,
|
* ext/tk/tcltklib.c: SEGV when tcltk-stubs is enabled.
* ext/tk/tcltklib.c: avoid error on a shared object.
* ext/tk/extconf.rb: support --with-tcltkversion
* ext/tk/README.tcltklib: add document about --with-tcltkversion
* ext/tk/lib/tk.rb, ext/tk/lib/multi-tk.rb, ext/tk/lib/remote-tk.rb:
not work on $SAFE==4
* ext/tk/lib/multi-tk.rb: Object#methods returns Symbols on Ruby1.9.
* ext/tk/lib/tk/timer.rb: add TkTimer#at_end(proc) to register the
procedure which called at end of the timer.
* ext/tk/lib/tk.rb, ext/tk/lib/tk/itemfont.rb, ext/tk/lib/font.rb:
support __IGNORE_UNKNOWN_CONFIGURE_OPTION__ about font options.
* ext/tk/lib/*: treat __IGNORE_UNKNOWN_CONFIGURE_OPTION__
* ext/tk/lib/tkextlib/iwidgets/scrolledcanvas.rb,
ext/tk/lib/tkextlib/iwidgets/scrolledlistbox.rb,
ext/tk/lib/tkextlib/iwidgets/scrolledtext.rb: bug fix.
* ext/tk/lib/tk/text.rb: typo. call a wrong method.
* ext/tk/lib/tk/itemconfig.rb: ditto.
* ext/tk/lib/tk.rb, ext/tk/lib/tk/itemconfig.rb,
ext/tk/lib/tk/canvas.rb: support alias names of option keys.
* ext/tk/lib/tk/grid.rb: lack of module-method definitions.
* ext/tk/lib/tk/pack.rb, ext/tk/lib/tk/grid.rb: increase supported
parameter patterns of configure method.
* ext/tk/lib/tk.rb: add TkWindow#grid_anchor, grid_column, grid_row.
* ext/tk/lib/tk/wm.rb: methods of Tk::Wm_for_General module cannot
pass the given block to methods of Tk::Wm module.
* ext/tk/lib/tk/wm.rb: Wm#overrideredirect overwrites arguemnt to
an invalid value.
* ext/tk/lib/tk.rb: fix memory (object) leak bug.
* ext/tk/tcltklib.c, ext/tk/tkutil/tkutil.c: fix memory leak.
* ext/tk/sample/demos-jp/aniwave.rb, ext/tk/sample/demos-en/aniwave.rb:
bug fix.
* ext/tk/lib/tkextlib/blt/component.rb,
ext/tk/lib/tkextlib/tile/tentry.rb,
ext/tk/lib/tkextlib/tile/treeview.rb: ditto.
* ext/tk/lib/tkextlib/tile/tpaned.rb: improve TPaned#add.
* ext/tk/sample/demos-jp/widget, ext/tk/sample/demos-en/widget,
ext/tk/sample/demos-jp/style.rb, ext/tk/sample/demos-en/style.rb,
ext/tk/sample/demos-jp/bind.rb, ext/tk/sample/demos-en/bind.rb:
bug fix.
* ext/tk/sample/ttk_wrapper.rb: ditto.
* ext/tk/sample/ttk_wrapper.rb: support "if __FILE__ == $0" idiom.
* ext/tk/sample/tktextio.rb: add binding for 'Ctrl-u' at console mode.
* ext/tk/lib/tkextlib/tile.rb, ext/tk/lib/tkextlib/tile/style.rb,
ext/tk/sample/ttk_wrapper.rb: improve treating and control themes.
add Tk::Tile.themes and Tk::Tile.set_theme(theme).
* ext/tk/lib/tkextlib/tile.rb: lack of autoload definitions.
* ext/tk/lib/tkextlib/tile/tnotebook.rb: cannot use kanji (not UTF-8)
characters for headings.
* ext/tk/lib/tkextlib/tkDND/shape.rb: wrong package name.
* ext/tk/tkutil/tkutil.c: improve handling callback-subst-keys.
Now, support longnam-keys (e.g. '%CTT' on tkdnd-2.0; however, still
not support tkdnd-2.0 on tkextlib), and symbols of parameters (e.g.
:widget=>'%W', :keycode=>'%k', '%x'=>:x, '%X'=>:root_x, and so on;
those are attributes of event object). It means that Ruby/Tk accepts
not only "widget.bind(ev, '%W', '%k', ...){|w, k, ...| ... }", but
also "widget.bind(ev, :widget, :keycode, ...){|w, k, ...| ... }".
It is potentially incompatible, when user passes symbols to the
arguments of the callback block (the block receives the symbols as
strings). I think that is very rare case (probably, used by Ruby/Tk
experts only). When causes such trouble, please give strings instead
of such symbol parameters (e.g. call Symbol#to_s method).
* ext/tk/lib/tk/event.rb, ext/tk/lib/tk/validation.rb,
ext/tk/lib/tkextlib/blt/treeview.rb,
ext/tk/lib/tkextlib/winico/winico.rb: ditto.
* ext/tk/tkutil/tkutil.c: strings are available on subst_tables on
TkUtil::CallbackSubst class (it is useful on Ruby 1.9).
* ext/tk/lib/tk/spinbox.rb, ext/tk/lib/tkextlib/iwidgets/hierarchy.rb,
ext/tk/lib/tkextlib/iwidgets/spinner.rb,
ext/tk/lib/tkextlib/iwidgets/entryfield.rb,
ext/tk/lib/tkextlib/iwidgets/calendar.rb,
ext/tk/lib/tkextlib/blt/dragdrop.rb,
ext/tk/lib/tkextlib/tkDND/tkdnd.rb,
ext/tk/lib/tkextlib/treectrl/tktreectrl.rb,
ext/tk/lib/tkextlib/tktable/tktable.rb: disable code piece became
unnecessary by reason of the changes of ext/tk/tkutil/tkutil.c.
* ext/tk/lib/tk.rb, ext/tk/lib/multi-tk.rb: change strategy to define
the constant WITH_ENCODING.
* ext/tk/lib/tk.rb: fix bug on Tk::Encoding.tk_encoding_names.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17083 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-06-10 16:59:10 -04:00
|
|
|
:_cb_ctrl_a, :_cb_ctrl_u
|
2006-11-06 01:56:37 -05:00
|
|
|
|
|
|
|
def _setup_console_bindings
|
|
|
|
@bindtag = TkBindTag.new
|
|
|
|
|
|
|
|
tags = self.bindtags
|
|
|
|
tags[tags.index(self)+1, 0] = @bindtag
|
|
|
|
self.bindtags = tags
|
|
|
|
|
|
|
|
@bindtag.bind('Return'){
|
|
|
|
insert('end - 1 char', "\n")
|
|
|
|
if (str = _get_console_line)
|
|
|
|
@read_buf_queue.push(str)
|
|
|
|
|
|
|
|
@history[0].replace(str.chomp)
|
|
|
|
@history.pop
|
|
|
|
@history.unshift('')
|
|
|
|
@hist_index = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
Tk.update
|
|
|
|
Tk.callback_break
|
|
|
|
}
|
|
|
|
@bindtag.bind('Alt-Return'){
|
|
|
|
Tk.callback_continue
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindtag.bind('FocusIn'){
|
|
|
|
if @console_mode
|
|
|
|
mark_set('insert', @ins_tail)
|
|
|
|
Tk.callback_break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
ins_mark = TkTextMark.new(self, 'insert')
|
|
|
|
|
|
|
|
@bindtag.bind('ButtonPress'){
|
|
|
|
if @console_mode
|
|
|
|
ins_mark.set('insert')
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindtag.bind('ButtonRelease-1'){
|
|
|
|
if @console_mode && compare('insert', '<=', @ins_head)
|
|
|
|
mark_set('insert', ins_mark)
|
|
|
|
Tk.callback_break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindtag.bind('ButtonRelease-2', '%x %y'){|x, y|
|
|
|
|
if @console_mode
|
|
|
|
# paste a text at 'insert' only
|
|
|
|
x1, y1, x2, y2 = bbox(ins_mark)
|
|
|
|
unless x == x1 && y == y1
|
|
|
|
Tk.event_generate(self, 'ButtonRelease-2', :x=>x1, :y=>y1)
|
|
|
|
Tk.callback_break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindtag.bind('Up'){ _cb_up }
|
|
|
|
@bindtag.bind('Control-p'){ _cb_up }
|
|
|
|
|
|
|
|
@bindtag.bind('Down'){ _cb_down }
|
|
|
|
@bindtag.bind('Control-n'){ _cb_down }
|
|
|
|
|
|
|
|
@bindtag.bind('Left'){ _cb_left }
|
|
|
|
@bindtag.bind('Control-b'){ _cb_left }
|
|
|
|
|
|
|
|
@bindtag.bind('BackSpace'){ _cb_backspace }
|
|
|
|
@bindtag.bind('Control-h'){ _cb_backspace }
|
|
|
|
|
|
|
|
@bindtag.bind('Home'){ _cb_ctrl_a }
|
|
|
|
@bindtag.bind('Control-a'){ _cb_ctrl_a }
|
* ext/tk/tcltklib.c: SEGV when tcltk-stubs is enabled.
* ext/tk/tcltklib.c: avoid error on a shared object.
* ext/tk/extconf.rb: support --with-tcltkversion
* ext/tk/README.tcltklib: add document about --with-tcltkversion
* ext/tk/lib/tk.rb, ext/tk/lib/multi-tk.rb, ext/tk/lib/remote-tk.rb:
not work on $SAFE==4
* ext/tk/lib/multi-tk.rb: Object#methods returns Symbols on Ruby1.9.
* ext/tk/lib/tk/timer.rb: add TkTimer#at_end(proc) to register the
procedure which called at end of the timer.
* ext/tk/lib/tk.rb, ext/tk/lib/tk/itemfont.rb, ext/tk/lib/font.rb:
support __IGNORE_UNKNOWN_CONFIGURE_OPTION__ about font options.
* ext/tk/lib/*: treat __IGNORE_UNKNOWN_CONFIGURE_OPTION__
* ext/tk/lib/tkextlib/iwidgets/scrolledcanvas.rb,
ext/tk/lib/tkextlib/iwidgets/scrolledlistbox.rb,
ext/tk/lib/tkextlib/iwidgets/scrolledtext.rb: bug fix.
* ext/tk/lib/tk/text.rb: typo. call a wrong method.
* ext/tk/lib/tk/itemconfig.rb: ditto.
* ext/tk/lib/tk.rb, ext/tk/lib/tk/itemconfig.rb,
ext/tk/lib/tk/canvas.rb: support alias names of option keys.
* ext/tk/lib/tk/grid.rb: lack of module-method definitions.
* ext/tk/lib/tk/pack.rb, ext/tk/lib/tk/grid.rb: increase supported
parameter patterns of configure method.
* ext/tk/lib/tk.rb: add TkWindow#grid_anchor, grid_column, grid_row.
* ext/tk/lib/tk/wm.rb: methods of Tk::Wm_for_General module cannot
pass the given block to methods of Tk::Wm module.
* ext/tk/lib/tk/wm.rb: Wm#overrideredirect overwrites arguemnt to
an invalid value.
* ext/tk/lib/tk.rb: fix memory (object) leak bug.
* ext/tk/tcltklib.c, ext/tk/tkutil/tkutil.c: fix memory leak.
* ext/tk/sample/demos-jp/aniwave.rb, ext/tk/sample/demos-en/aniwave.rb:
bug fix.
* ext/tk/lib/tkextlib/blt/component.rb,
ext/tk/lib/tkextlib/tile/tentry.rb,
ext/tk/lib/tkextlib/tile/treeview.rb: ditto.
* ext/tk/lib/tkextlib/tile/tpaned.rb: improve TPaned#add.
* ext/tk/sample/demos-jp/widget, ext/tk/sample/demos-en/widget,
ext/tk/sample/demos-jp/style.rb, ext/tk/sample/demos-en/style.rb,
ext/tk/sample/demos-jp/bind.rb, ext/tk/sample/demos-en/bind.rb:
bug fix.
* ext/tk/sample/ttk_wrapper.rb: ditto.
* ext/tk/sample/ttk_wrapper.rb: support "if __FILE__ == $0" idiom.
* ext/tk/sample/tktextio.rb: add binding for 'Ctrl-u' at console mode.
* ext/tk/lib/tkextlib/tile.rb, ext/tk/lib/tkextlib/tile/style.rb,
ext/tk/sample/ttk_wrapper.rb: improve treating and control themes.
add Tk::Tile.themes and Tk::Tile.set_theme(theme).
* ext/tk/lib/tkextlib/tile.rb: lack of autoload definitions.
* ext/tk/lib/tkextlib/tile/tnotebook.rb: cannot use kanji (not UTF-8)
characters for headings.
* ext/tk/lib/tkextlib/tkDND/shape.rb: wrong package name.
* ext/tk/tkutil/tkutil.c: improve handling callback-subst-keys.
Now, support longnam-keys (e.g. '%CTT' on tkdnd-2.0; however, still
not support tkdnd-2.0 on tkextlib), and symbols of parameters (e.g.
:widget=>'%W', :keycode=>'%k', '%x'=>:x, '%X'=>:root_x, and so on;
those are attributes of event object). It means that Ruby/Tk accepts
not only "widget.bind(ev, '%W', '%k', ...){|w, k, ...| ... }", but
also "widget.bind(ev, :widget, :keycode, ...){|w, k, ...| ... }".
It is potentially incompatible, when user passes symbols to the
arguments of the callback block (the block receives the symbols as
strings). I think that is very rare case (probably, used by Ruby/Tk
experts only). When causes such trouble, please give strings instead
of such symbol parameters (e.g. call Symbol#to_s method).
* ext/tk/lib/tk/event.rb, ext/tk/lib/tk/validation.rb,
ext/tk/lib/tkextlib/blt/treeview.rb,
ext/tk/lib/tkextlib/winico/winico.rb: ditto.
* ext/tk/tkutil/tkutil.c: strings are available on subst_tables on
TkUtil::CallbackSubst class (it is useful on Ruby 1.9).
* ext/tk/lib/tk/spinbox.rb, ext/tk/lib/tkextlib/iwidgets/hierarchy.rb,
ext/tk/lib/tkextlib/iwidgets/spinner.rb,
ext/tk/lib/tkextlib/iwidgets/entryfield.rb,
ext/tk/lib/tkextlib/iwidgets/calendar.rb,
ext/tk/lib/tkextlib/blt/dragdrop.rb,
ext/tk/lib/tkextlib/tkDND/tkdnd.rb,
ext/tk/lib/tkextlib/treectrl/tktreectrl.rb,
ext/tk/lib/tkextlib/tktable/tktable.rb: disable code piece became
unnecessary by reason of the changes of ext/tk/tkutil/tkutil.c.
* ext/tk/lib/tk.rb, ext/tk/lib/multi-tk.rb: change strategy to define
the constant WITH_ENCODING.
* ext/tk/lib/tk.rb: fix bug on Tk::Encoding.tk_encoding_names.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17083 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-06-10 16:59:10 -04:00
|
|
|
|
|
|
|
@bindtag.bind('Control-u'){ _cb_ctrl_u }
|
2006-11-06 01:56:37 -05:00
|
|
|
end
|
|
|
|
private :_setup_console_bindings
|
|
|
|
|
|
|
|
def _block_read(size = nil, ret = '', block_mode = true)
|
|
|
|
return '' if size == 0
|
|
|
|
return nil if ! @read_buf_queue && @read_buffer.empty?
|
|
|
|
ret = '' unless ret.kind_of?(String)
|
|
|
|
ret.replace('') unless ret.empty?
|
|
|
|
|
|
|
|
if block_mode == nil # partial
|
|
|
|
if @read_buffer.empty?
|
|
|
|
ret << @read_buffer.slice!(0..-1)
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if size.kind_of?(Numeric)
|
|
|
|
loop{
|
|
|
|
@read_buf_mutex.synchronize {
|
|
|
|
buf_len = @read_buffer.length
|
|
|
|
if buf_len >= size
|
|
|
|
ret << @read_buffer.slice!(0, size)
|
|
|
|
return ret
|
|
|
|
else
|
|
|
|
ret << @read_buffer.slice!(0..-1)
|
|
|
|
size -= buf_len
|
|
|
|
return ret unless @read_buf_queue
|
|
|
|
end
|
|
|
|
}
|
|
|
|
@read_buffer << @read_buf_queue.pop
|
|
|
|
}
|
|
|
|
else # readline
|
|
|
|
rs = (size)? size: $/
|
|
|
|
rs = rs.to_s if rs.kind_of?(Regexp)
|
|
|
|
loop{
|
|
|
|
@read_buf_mutex.synchronize {
|
|
|
|
if (str = @read_buffer.slice!(/\A(.*)(#{rs})/m))
|
|
|
|
ret << str
|
|
|
|
return ret
|
|
|
|
else
|
|
|
|
ret << @read_buffer.slice!(0..-1)
|
|
|
|
return ret unless @read_buf_queue
|
|
|
|
end
|
|
|
|
}
|
|
|
|
@read_buffer << @read_buf_queue.pop
|
|
|
|
}
|
2005-03-08 04:19:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
def _block_write
|
|
|
|
###### currently, not support
|
|
|
|
end
|
|
|
|
private :_block_read, :_block_write
|
|
|
|
|
|
|
|
####################################
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
def <<(obj)
|
|
|
|
_write(obj)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def binmode
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def clone
|
|
|
|
fail NotImplementedError, 'cannot clone TkTextIO'
|
|
|
|
end
|
|
|
|
def dup
|
|
|
|
fail NotImplementedError, 'cannot duplicate TkTextIO'
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
2005-03-08 23:39:25 -05:00
|
|
|
close_read
|
|
|
|
close_write
|
2005-03-08 04:19:03 -05:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
def close_read
|
2005-03-08 23:39:25 -05:00
|
|
|
@open[:r] = false if @open[:r]
|
2005-03-08 04:19:03 -05:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
def close_write
|
2005-03-08 23:39:25 -05:00
|
|
|
@open[:w] = false if @opne[:w]
|
2005-03-08 04:19:03 -05:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
def closed?(dir=nil)
|
|
|
|
case dir
|
|
|
|
when :r, 'r'
|
|
|
|
!@open[:r]
|
|
|
|
when :w, 'w'
|
|
|
|
!@open[:w]
|
|
|
|
else
|
|
|
|
!@open[:r] && !@open[:w]
|
|
|
|
end
|
2005-03-08 04:19:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def _check_readable
|
2005-03-08 23:39:25 -05:00
|
|
|
fail IOError, "not opened for reading" if @open[:r].nil?
|
|
|
|
fail IOError, "closed stream" if !@open[:r]
|
2005-03-08 04:19:03 -05:00
|
|
|
end
|
|
|
|
def _check_writable
|
2005-03-08 23:39:25 -05:00
|
|
|
fail IOError, "not opened for writing" if @open[:w].nil?
|
|
|
|
fail IOError, "closed stream" if !@open[:w]
|
2005-03-08 04:19:03 -05:00
|
|
|
end
|
|
|
|
private :_check_readable, :_check_writable
|
|
|
|
|
|
|
|
def each_line(rs = $/)
|
|
|
|
_check_readable
|
2006-11-06 01:56:37 -05:00
|
|
|
while(s = self.gets(rs))
|
2005-03-08 04:19:03 -05:00
|
|
|
yield(s)
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
alias each each_line
|
|
|
|
|
|
|
|
def each_char
|
|
|
|
_check_readable
|
2006-11-06 01:56:37 -05:00
|
|
|
while(c = self.getc)
|
2005-03-08 04:19:03 -05:00
|
|
|
yield(c)
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
alias each_byte each_char
|
|
|
|
|
|
|
|
def eof?
|
|
|
|
compare(@txtpos, '==', 'end - 1 char')
|
|
|
|
end
|
|
|
|
alias eof eof?
|
|
|
|
|
|
|
|
def fcntl(*args)
|
2006-11-06 01:56:37 -05:00
|
|
|
fail NotImplementedError, "fcntl is not implemented on #{self.class}"
|
2005-03-08 04:19:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def fsync
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
|
|
|
def fileno
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def flush
|
2006-11-06 01:56:37 -05:00
|
|
|
Thread.pass
|
|
|
|
if @open[:w] || ! @write_buffer.empty?
|
|
|
|
@write_buf_mutex.synchronize {
|
2009-03-05 22:56:38 -05:00
|
|
|
_sync_write_buf(@write_buffer)
|
2006-11-06 01:56:37 -05:00
|
|
|
@write_buffer[0..-1] = ''
|
|
|
|
}
|
|
|
|
end
|
2005-03-08 04:19:03 -05:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def getc
|
2006-11-06 01:56:37 -05:00
|
|
|
return _block_read(1) if @console_mode
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
_check_readable
|
|
|
|
return nil if eof?
|
|
|
|
c = get(@txtpos)
|
|
|
|
@txtpos.set(@txtpos + '1 char')
|
2005-03-09 01:08:33 -05:00
|
|
|
_see_pos
|
2005-03-08 04:19:03 -05:00
|
|
|
c
|
|
|
|
end
|
|
|
|
|
|
|
|
def gets(rs = $/)
|
2006-11-06 01:56:37 -05:00
|
|
|
return _block_read(rs) if @console_mode
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
_check_readable
|
2006-11-06 01:56:37 -05:00
|
|
|
return nil if eof?
|
2005-03-08 04:19:03 -05:00
|
|
|
_readline(rs)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ioctrl(*args)
|
|
|
|
fail NotImplementedError, 'iocntl is not implemented on TkTextIO'
|
|
|
|
end
|
|
|
|
|
|
|
|
def isatty
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def tty?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def lineno
|
|
|
|
@lineno + @line_offset
|
|
|
|
end
|
|
|
|
|
|
|
|
def lineno=(num)
|
|
|
|
@line_offset = num - @lineno
|
|
|
|
num
|
|
|
|
end
|
|
|
|
|
2005-03-08 23:39:25 -05:00
|
|
|
def overwrite?
|
|
|
|
@overwrite
|
|
|
|
end
|
|
|
|
|
|
|
|
def overwrite=(ovwt)
|
|
|
|
@overwrite = (ovwt)? true: false
|
|
|
|
end
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
def pid
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def index_pos
|
|
|
|
index(@txtpos)
|
|
|
|
end
|
|
|
|
alias tell_index index_pos
|
|
|
|
|
2005-03-08 23:39:25 -05:00
|
|
|
def index_pos=(idx)
|
|
|
|
@txtpos.set(idx)
|
|
|
|
@txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end)
|
2005-03-09 01:08:33 -05:00
|
|
|
_see_pos
|
2005-03-08 23:39:25 -05:00
|
|
|
idx
|
|
|
|
end
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
def pos
|
|
|
|
s = get('1.0', @txtpos)
|
2005-03-08 23:39:25 -05:00
|
|
|
number(tk_call('string', 'length', s))
|
2005-03-08 04:19:03 -05:00
|
|
|
end
|
|
|
|
alias tell pos
|
|
|
|
|
|
|
|
def pos=(idx)
|
|
|
|
seek(idx, IO::SEEK_SET)
|
|
|
|
idx
|
|
|
|
end
|
|
|
|
|
2005-03-08 23:39:25 -05:00
|
|
|
def pos_gravity
|
|
|
|
@txtpos.gravity
|
|
|
|
end
|
|
|
|
|
|
|
|
def pos_gravity=(side)
|
|
|
|
@txtpos.gravity = side
|
|
|
|
side
|
|
|
|
end
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
def print(arg=$_, *args)
|
|
|
|
_check_writable
|
|
|
|
args.unshift(arg)
|
|
|
|
args.map!{|val| (val == nil)? 'nil': val.to_s }
|
|
|
|
str = args.join($,)
|
|
|
|
str << $\ if $\
|
|
|
|
_write(str)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
def printf(*args)
|
|
|
|
_check_writable
|
|
|
|
_write(sprintf(*args))
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def putc(c)
|
|
|
|
_check_writable
|
|
|
|
c = c.chr if c.kind_of?(Fixnum)
|
|
|
|
_write(c)
|
|
|
|
c
|
|
|
|
end
|
|
|
|
|
|
|
|
def puts(*args)
|
|
|
|
_check_writable
|
|
|
|
if args.empty?
|
|
|
|
_write("\n")
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
args.each{|arg|
|
|
|
|
if arg == nil
|
|
|
|
_write("nil\n")
|
|
|
|
elsif arg.kind_of?(Array)
|
|
|
|
puts(*arg)
|
|
|
|
elsif arg.kind_of?(String)
|
|
|
|
_write(arg.chomp)
|
|
|
|
_write("\n")
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
arg = arg.to_ary
|
|
|
|
puts(*arg)
|
|
|
|
rescue
|
|
|
|
puts(arg.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def _read(len)
|
|
|
|
epos = @txtpos + "#{len} char"
|
|
|
|
s = get(@txtpos, epos)
|
|
|
|
@txtpos.set(epos)
|
2005-03-08 23:39:25 -05:00
|
|
|
@txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end)
|
2005-03-09 01:08:33 -05:00
|
|
|
_see_pos
|
2005-03-08 04:19:03 -05:00
|
|
|
s
|
|
|
|
end
|
|
|
|
private :_read
|
|
|
|
|
|
|
|
def read(len=nil, buf=nil)
|
2006-11-06 01:56:37 -05:00
|
|
|
return _block_read(len, buf) if @console_mode
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
_check_readable
|
|
|
|
if len
|
|
|
|
return "" if len == 0
|
|
|
|
return nil if eof?
|
|
|
|
s = _read(len)
|
|
|
|
else
|
|
|
|
s = get(@txtpos, 'end - 1 char')
|
|
|
|
@txtpos.set('end - 1 char')
|
2005-03-09 01:08:33 -05:00
|
|
|
_see_pos
|
2005-03-08 04:19:03 -05:00
|
|
|
end
|
|
|
|
buf.replace(s) if buf.kind_of?(String)
|
|
|
|
s
|
|
|
|
end
|
|
|
|
|
|
|
|
def readchar
|
2006-11-06 01:56:37 -05:00
|
|
|
return _block_read(1) if @console_mode
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
_check_readable
|
|
|
|
fail EOFError if eof?
|
|
|
|
c = get(@txtpos)
|
|
|
|
@txtpos.set(@txtpos + '1 char')
|
2005-03-09 01:08:33 -05:00
|
|
|
_see_pos
|
2005-03-08 04:19:03 -05:00
|
|
|
c
|
|
|
|
end
|
|
|
|
|
|
|
|
def _readline(rs = $/)
|
|
|
|
if rs == nil
|
|
|
|
s = get(@txtpos, 'end - 1 char')
|
|
|
|
@txtpos.set('end - 1 char')
|
|
|
|
elsif rs == ''
|
2006-11-06 01:56:37 -05:00
|
|
|
@count_var.value # make it global
|
2009-03-05 22:56:38 -05:00
|
|
|
idx = tksearch_with_count([:regexp], @count_var,
|
2005-03-08 04:19:03 -05:00
|
|
|
"\n(\n)+", @txtpos, 'end - 1 char')
|
|
|
|
if idx
|
|
|
|
s = get(@txtpos, idx) << "\n"
|
|
|
|
@txtpos.set("#{idx} + #{@count_var.value} char")
|
2005-03-08 23:39:25 -05:00
|
|
|
@txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end)
|
2005-03-08 04:19:03 -05:00
|
|
|
else
|
|
|
|
s = get(@txtpos, 'end - 1 char')
|
|
|
|
@txtpos.set('end - 1 char')
|
|
|
|
end
|
|
|
|
else
|
2006-11-06 01:56:37 -05:00
|
|
|
@count_var.value # make it global
|
2005-03-08 04:19:03 -05:00
|
|
|
idx = tksearch_with_count(@count_var, rs, @txtpos, 'end - 1 char')
|
|
|
|
if idx
|
|
|
|
s = get(@txtpos, "#{idx} + #{@count_var.value} char")
|
|
|
|
@txtpos.set("#{idx} + #{@count_var.value} char")
|
2005-03-09 01:08:33 -05:00
|
|
|
@txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end)
|
2005-03-08 04:19:03 -05:00
|
|
|
else
|
|
|
|
s = get(@txtpos, 'end - 1 char')
|
|
|
|
@txtpos.set('end - 1 char')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-03-09 01:08:33 -05:00
|
|
|
_see_pos
|
2005-03-08 04:19:03 -05:00
|
|
|
@lineno += 1
|
|
|
|
$_ = s
|
|
|
|
end
|
|
|
|
private :_readline
|
|
|
|
|
|
|
|
def readline(rs = $/)
|
2006-11-06 01:56:37 -05:00
|
|
|
return _block_readline(rs) if @console_mode
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
_check_readable
|
|
|
|
fail EOFError if eof?
|
|
|
|
_readline(rs)
|
|
|
|
end
|
|
|
|
|
|
|
|
def readlines(rs = $/)
|
2006-11-06 01:56:37 -05:00
|
|
|
if @console_mode
|
|
|
|
lines = []
|
|
|
|
while (line = _block_readline(rs))
|
|
|
|
lines << line
|
|
|
|
end
|
|
|
|
return lines
|
|
|
|
end
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
_check_readable
|
|
|
|
lines = []
|
|
|
|
until(eof?)
|
|
|
|
lines << _readline(rs)
|
|
|
|
end
|
|
|
|
$_ = nil
|
|
|
|
lines
|
|
|
|
end
|
|
|
|
|
|
|
|
def readpartial(maxlen, buf=nil)
|
2006-11-06 01:56:37 -05:00
|
|
|
#return @console_buffer.readpartial(maxlen, buf) if @console_mode
|
|
|
|
return _block_read(maxlen, buf, nil) if @console_mode
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
_check_readable
|
2006-11-06 01:56:37 -05:00
|
|
|
fail EOFError if eof?
|
2005-03-08 04:19:03 -05:00
|
|
|
s = _read(maxlen)
|
|
|
|
buf.replace(s) if buf.kind_of?(String)
|
|
|
|
s
|
|
|
|
end
|
|
|
|
|
|
|
|
def reopen(*args)
|
|
|
|
fail NotImplementedError, 'reopen is not implemented on TkTextIO'
|
|
|
|
end
|
|
|
|
|
|
|
|
def rewind
|
|
|
|
@txtpos.set('1.0')
|
2005-03-09 01:08:33 -05:00
|
|
|
_see_pos
|
2005-03-08 04:19:03 -05:00
|
|
|
@lineno = 0
|
|
|
|
@line_offset = 0
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def seek(offset, whence=IO::SEEK_SET)
|
|
|
|
case whence
|
|
|
|
when IO::SEEK_SET
|
|
|
|
offset = "1.0 + #{offset} char" if offset.kind_of?(Numeric)
|
|
|
|
@txtpos.set(offset)
|
|
|
|
|
|
|
|
when IO::SEEK_CUR
|
|
|
|
offset = "#{offset} char" if offset.kind_of?(Numeric)
|
|
|
|
@txtpos.set(@txtpos + offset)
|
|
|
|
|
|
|
|
when IO::SEEK_END
|
|
|
|
offset = "#{offset} char" if offset.kind_of?(Numeric)
|
|
|
|
@txtpos.set("end - 1 char + #{offset}")
|
|
|
|
|
|
|
|
else
|
|
|
|
fail Errno::EINVAL, 'invalid whence argument'
|
|
|
|
end
|
|
|
|
|
2005-03-08 23:39:25 -05:00
|
|
|
@txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end)
|
2005-03-09 01:08:33 -05:00
|
|
|
_see_pos
|
2005-03-08 23:39:25 -05:00
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
0
|
|
|
|
end
|
|
|
|
alias sysseek seek
|
|
|
|
|
2005-03-09 01:08:33 -05:00
|
|
|
def _see_pos
|
|
|
|
see(@show) if @show
|
|
|
|
end
|
|
|
|
private :_see_pos
|
|
|
|
|
|
|
|
def show_mode
|
|
|
|
(@show == @txtpos)? :pos : @show
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_mode=(mode)
|
2009-03-05 22:56:38 -05:00
|
|
|
# define show mode when file position is changed.
|
|
|
|
# mode == :pos or "pos" or true :: see current file position.
|
|
|
|
# mode == :insert or "insert" :: see insert cursor position.
|
2005-03-09 01:08:33 -05:00
|
|
|
# mode == nil or false :: do nothing
|
|
|
|
# else see 'mode' position ('mode' should be text index or mark)
|
|
|
|
case mode
|
|
|
|
when :pos, 'pos', true
|
|
|
|
@show = @txtpos
|
|
|
|
when :insert, 'insert'
|
|
|
|
@show = :insert
|
|
|
|
when nil, false
|
|
|
|
@show = false
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
index(mode)
|
|
|
|
rescue
|
|
|
|
fail ArgumentError, 'invalid show-position'
|
|
|
|
end
|
|
|
|
@show = mode
|
|
|
|
end
|
|
|
|
|
|
|
|
_see_pos
|
|
|
|
|
|
|
|
mode
|
|
|
|
end
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
def stat
|
|
|
|
fail NotImplementedError, 'stat is not implemented on TkTextIO'
|
|
|
|
end
|
|
|
|
|
|
|
|
def sync
|
|
|
|
@sync
|
|
|
|
end
|
|
|
|
|
|
|
|
def sync=(mode)
|
|
|
|
@sync = mode
|
|
|
|
end
|
|
|
|
|
|
|
|
def sysread(len, buf=nil)
|
2006-11-06 01:56:37 -05:00
|
|
|
return _block_read(len, buf) if @console_mode
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
_check_readable
|
|
|
|
fail EOFError if eof?
|
|
|
|
s = _read(len)
|
|
|
|
buf.replace(s) if buf.kind_of?(String)
|
|
|
|
s
|
|
|
|
end
|
|
|
|
|
|
|
|
def syswrite(obj)
|
|
|
|
_write(obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_io
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def trancate(len)
|
|
|
|
delete("1.0 + #{len} char", :end)
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
|
|
|
def ungetc(c)
|
2006-11-06 01:56:37 -05:00
|
|
|
if @console_mode
|
|
|
|
@read_buf_mutex.synchronize {
|
|
|
|
@read_buffer[0,0] = c.chr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
_check_readable
|
|
|
|
c = c.chr if c.kind_of?(Fixnum)
|
|
|
|
if compare(@txtpos, '>', '1.0')
|
2005-03-08 23:39:25 -05:00
|
|
|
@txtpos.set(@txtpos - '1 char')
|
|
|
|
delete(@txtpos)
|
2005-03-08 04:19:03 -05:00
|
|
|
insert(@txtpos, tk_call('string', 'range', c, 0, 1))
|
2005-03-08 23:39:25 -05:00
|
|
|
@txtpos.set(@txtpos - '1 char') if @txtpos.gravity == 'right'
|
2005-03-09 01:08:33 -05:00
|
|
|
_see_pos
|
2005-03-08 23:39:25 -05:00
|
|
|
else
|
|
|
|
fail IOError, 'cannot ungetc at head of stream'
|
2005-03-08 04:19:03 -05:00
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
=begin
|
2005-03-08 04:19:03 -05:00
|
|
|
def _write(obj)
|
2006-11-06 01:56:37 -05:00
|
|
|
#s = _get_eval_string(obj)
|
|
|
|
s = (obj.kind_of?(String))? obj: obj.to_s
|
2005-03-08 04:19:03 -05:00
|
|
|
n = number(tk_call('string', 'length', s))
|
2005-03-08 23:39:25 -05:00
|
|
|
delete(@txtpos, @txtpos + "#{n} char") if @overwrite
|
2005-03-08 04:19:03 -05:00
|
|
|
self.insert(@txtpos, s)
|
|
|
|
@txtpos.set(@txtpos + "#{n} char")
|
2005-03-09 01:08:33 -05:00
|
|
|
@txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end)
|
|
|
|
_see_pos
|
2005-03-08 04:19:03 -05:00
|
|
|
Tk.update if @sync
|
|
|
|
n
|
|
|
|
end
|
|
|
|
private :_write
|
2006-11-06 01:56:37 -05:00
|
|
|
=end
|
|
|
|
#=begin
|
|
|
|
def _sync_write_buf(s)
|
|
|
|
if (n = number(tk_call('string', 'length', s))) > 0
|
|
|
|
delete(@txtpos, @txtpos + "#{n} char") if @overwrite
|
|
|
|
self.insert(@txtpos, s)
|
|
|
|
#Tk.update
|
|
|
|
|
|
|
|
@txtpos.set(@txtpos + "#{n} char")
|
|
|
|
@txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end)
|
|
|
|
|
|
|
|
@ins_head.set(@txtpos) if compare(@txtpos, '>', @ins_head)
|
|
|
|
|
|
|
|
_see_pos
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
private :_sync_write_buf
|
|
|
|
|
|
|
|
def _write(obj)
|
|
|
|
s = (obj.kind_of?(String))? obj: obj.to_s
|
|
|
|
n = number(tk_call('string', 'length', s))
|
|
|
|
@write_buf_queue.enq(s)
|
|
|
|
if @sync
|
|
|
|
Thread.pass
|
|
|
|
Tk.update
|
|
|
|
end
|
|
|
|
n
|
|
|
|
end
|
|
|
|
private :_write
|
|
|
|
#=end
|
2005-03-08 04:19:03 -05:00
|
|
|
|
|
|
|
def write(obj)
|
|
|
|
_check_writable
|
|
|
|
_write(obj)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
####################
|
|
|
|
# TEST
|
|
|
|
####################
|
|
|
|
if __FILE__ == $0
|
2006-11-06 01:56:37 -05:00
|
|
|
ev_loop = Thread.new{Tk.mainloop}
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
f = TkFrame.new.pack
|
2009-03-05 22:56:38 -05:00
|
|
|
#tio = TkTextIO.new(f, :show=>:nil,
|
|
|
|
#tio = TkTextIO.new(f, :show=>:pos,
|
|
|
|
tio = TkTextIO.new(f, :show=>:insert,
|
2005-03-09 01:08:33 -05:00
|
|
|
:text=>">>> This is an initial text line. <<<\n\n"){
|
2006-11-06 01:56:37 -05:00
|
|
|
# yscrollbar(TkScrollbar.new(f).pack(:side=>:right, :fill=>:y))
|
2005-03-08 04:19:03 -05:00
|
|
|
pack(:side=>:left, :fill=>:both, :expand=>true)
|
|
|
|
}
|
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
Tk.update
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
$stdin = tio
|
|
|
|
$stdout = tio
|
|
|
|
$stderr = tio
|
|
|
|
|
2005-03-08 23:39:25 -05:00
|
|
|
STDOUT.print("\n========= TkTextIO#gets for inital text ========\n\n")
|
|
|
|
|
|
|
|
while(s = gets)
|
|
|
|
STDOUT.print(s)
|
|
|
|
end
|
|
|
|
|
|
|
|
STDOUT.print("\n============ put strings to TkTextIO ===========\n\n")
|
|
|
|
|
2005-03-08 04:19:03 -05:00
|
|
|
puts "On this sample, a text widget works as if it is a I/O stream."
|
|
|
|
puts "Please see the code."
|
|
|
|
puts
|
2005-03-09 01:08:33 -05:00
|
|
|
printf("printf message: %d %X\n", 123456, 255)
|
2005-03-08 04:19:03 -05:00
|
|
|
puts
|
2005-03-09 01:08:33 -05:00
|
|
|
printf("(output by 'p' method) This TkTextIO object is ...\n")
|
2005-03-08 04:19:03 -05:00
|
|
|
p tio
|
2005-03-09 01:08:33 -05:00
|
|
|
print(" [ Current wrap mode of this object is 'char'. ]\n")
|
2005-03-08 04:19:03 -05:00
|
|
|
puts
|
2005-03-08 23:39:25 -05:00
|
|
|
warn("This is a warning message generated by 'warn' method.")
|
2005-03-08 04:19:03 -05:00
|
|
|
puts
|
2005-03-09 01:08:33 -05:00
|
|
|
puts "current show_mode is #{tio.show_mode}."
|
|
|
|
if tio.show_mode == :pos
|
|
|
|
puts "So, you can see the current file position on this text widget."
|
|
|
|
else
|
|
|
|
puts "So, you can see the position '#{tio.show_mode}' on this text widget."
|
|
|
|
end
|
|
|
|
print("Please scroll up this text widget to see the head of lines.\n")
|
2005-03-08 04:19:03 -05:00
|
|
|
print("---------------------------------------------------------\n")
|
|
|
|
|
|
|
|
STDOUT.print("\n=============== TkTextIO#readlines =============\n\n")
|
|
|
|
|
|
|
|
tio.seek(0)
|
|
|
|
lines = readlines
|
2005-03-08 23:39:25 -05:00
|
|
|
STDOUT.puts(lines.inspect)
|
2005-03-08 04:19:03 -05:00
|
|
|
|
|
|
|
STDOUT.print("\n================== TkTextIO#each ===============\n\n")
|
|
|
|
|
|
|
|
tio.rewind
|
|
|
|
tio.each{|line| STDOUT.printf("%2d: %s\n", tio.lineno, line.chomp)}
|
|
|
|
|
|
|
|
STDOUT.print("\n================================================\n\n")
|
|
|
|
|
2005-03-10 05:13:30 -05:00
|
|
|
STDOUT.print("\n========= reverse order (seek by lines) ========\n\n")
|
|
|
|
|
|
|
|
tio.seek(-1, IO::SEEK_END)
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
tio.seek(:linestart, IO::SEEK_CUR)
|
|
|
|
rescue
|
|
|
|
# maybe use old version of tk/textmark.rb
|
|
|
|
tio.seek('0 char linestart', IO::SEEK_CUR)
|
|
|
|
end
|
|
|
|
STDOUT.print(gets)
|
|
|
|
tio.seek('-1 char linestart -1 char', IO::SEEK_CUR)
|
|
|
|
end while(tio.pos > 0)
|
|
|
|
|
|
|
|
STDOUT.print("\n================================================\n\n")
|
|
|
|
|
|
|
|
tio.seek(0, IO::SEEK_END)
|
|
|
|
|
2006-11-06 01:56:37 -05:00
|
|
|
STDOUT.print("tio.sync == #{tio.sync}\n")
|
|
|
|
# tio.sync = false
|
|
|
|
# STDOUT.print("tio.sync == #{tio.sync}\n")
|
|
|
|
|
|
|
|
(0..10).each{|i|
|
|
|
|
STDOUT.print("#{i}\n")
|
|
|
|
s = ''
|
|
|
|
(0..1000).each{ s << '*' }
|
|
|
|
print(s)
|
|
|
|
}
|
|
|
|
print("\n")
|
|
|
|
print("\n=========================================================\n\n")
|
|
|
|
|
|
|
|
s = ''
|
|
|
|
timer = TkTimer.new(:idle, -1, proc{
|
|
|
|
#STDOUT.print("idle call\n")
|
|
|
|
unless s.empty?
|
|
|
|
print(s)
|
|
|
|
s = ''
|
|
|
|
end
|
|
|
|
}).start
|
|
|
|
(0..10).each{|i|
|
|
|
|
STDOUT.print("#{i}\n")
|
|
|
|
(0..1000).each{ s << '*' }
|
|
|
|
}
|
|
|
|
# timer.stop
|
|
|
|
until s.empty?
|
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
timer.stop
|
|
|
|
|
|
|
|
=begin
|
|
|
|
tio.sync = false
|
|
|
|
print("\n")
|
|
|
|
#(0..10000).each{ putc('*') }
|
|
|
|
(0..10).each{|i|
|
|
|
|
STDOUT.print("#{i}\n")
|
|
|
|
(0..1000).each{ putc('*') }
|
|
|
|
}
|
|
|
|
|
|
|
|
(0..10).each{|i|
|
|
|
|
STDOUT.print("#{i}\n")
|
|
|
|
s = ''
|
|
|
|
(0..1000).each{ s << '*' }
|
|
|
|
print(s)
|
|
|
|
}
|
|
|
|
=end
|
|
|
|
|
|
|
|
num = 0
|
|
|
|
# io = TkTextIO.new(:mode=>:console, :prompt=>'').pack
|
|
|
|
#=begin
|
2009-03-05 22:56:38 -05:00
|
|
|
io = TkTextIO.new(:mode=>:console,
|
2006-11-06 01:56:37 -05:00
|
|
|
:prompt_cmd=>proc{
|
|
|
|
s = "[#{num}]"
|
|
|
|
num += 1
|
|
|
|
s
|
2009-03-05 22:56:38 -05:00
|
|
|
},
|
2006-11-06 01:56:37 -05:00
|
|
|
:prompt=>'-> ').pack
|
|
|
|
#=end
|
|
|
|
Thread.new{loop{sleep 2; io.puts 'hoge'}}
|
|
|
|
Thread.new{loop{p io.gets}}
|
|
|
|
|
|
|
|
ev_loop.join
|
2005-03-08 04:19:03 -05:00
|
|
|
end
|