ratpoison-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RP] How do I paste without a rat?


From: rubikitch
Subject: Re: [RP] How do I paste without a rat?
Date: Tue, 04 Apr 2006 18:09:19 +0900 (JST)

From: Shawn Betts <address@hidden>
Subject: Re: [RP] How do I paste without a rat?
Date: Tue, 04 Apr 2006 04:04:11 -0400

> stuff would send a sequence of characters to the window?
> paste would send the sequence of characters in the selection buffer?
Exactly!

> Just because I love torture i wrote them up as aliases with perl:
> 
> alias stuff exec perl -e 'for ($i=0; $i<length($ARGV[0]); $i++) { system 
> ("$ENV{RATPOISON} -c \"meta " . substr($ARGV[0],$i,1) . "\""); }' 
> alias paste stuff `$RATPOISON -c getsel`

This version cannot handle spaces and capital letters.

:putsel Shawn Betts
:paste
->shawn

> Nasty eh? They're kinda slow, too. I can see it worthwile having C
> versions. Feel free to write up a patch :).

I wrote Ruby version.
The RP command can process multiple commands at a time.
But this version cannot handle capitals and Japanese chars too... 

rp-transact.rb stocks RP commands and execute them at a time.
I use it for complex RP scripts.

### rp-transact.rb
#!/usr/bin/env ruby
require 'tmpdir'
require 'fileutils'

class << IO
  # Redirects $stdout to STDOUT and executes the block.
  def redirect(stdout)
    begin
      stdout_sv = STDOUT.dup
      STDOUT.reopen(stdout)
      yield
    ensure
      STDOUT.flush
      STDOUT.reopen(stdout_sv)
    end
  end
end

@@__system_to_string_count__ = 0
def system_to_string(*args)
  begin
    tmpf = File.join(Dir.tmpdir, "#{$$}-#{@@__system_to_string_count__}")
    @@__system_to_string_count__ += 1
    ret = nil
    open(tmpf,"w") do |f|
      IO.redirect(f) {
        system *args
      }
    end
    File.read(tmpf)
  ensure
    FileUtils.rm_f tmpf
  end
end


class RpTransact

  RATPOISON = "/usr/local/bin/ratpoison"

  def initialize
    @cmds = []
  end

  def <<(cmd)
    @cmds << cmd
    self
  end

  def clear
    @cmds.clear
  end

  def commit
    a = system_to_string(RATPOISON, address@hidden|c| ["-c", c]}.flatten)
    clear
    a
  end

end


### paste.rb
#!/usr/bin/env ruby
require 'rp-transact'
rp = RpTransact.new
`wm getsel`.split(//).each do |b|
  case b
  when " "
    rp << "meta space"
  else
    rp << "meta \"#{b}\""
  end
end
rp.commit

--
rubikitch
http://www.rubyist.net/~rubikitch/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]