Created
April 24, 2011 20:22
-
-
Save cldwalker/939846 to your computer and use it in GitHub Desktop.
ripl multi-line history plugin - two ways to easily access last edited code block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add to ~/.riplrc | |
module Ripl::MultiLineHistory | |
attr_reader :last_buffer | |
# hacks multi-line's loop_once | |
def loop_once | |
catch(:multiline) do | |
Ripl::Shell::API.instance_method(:loop_once).bind(self).call | |
@last_buffer = @buffer && @input ? (@buffer << @input).dup : @input | |
history << @last_buffer.join('; ') if @buffer && @input | |
@buffer = nil | |
end | |
end | |
require 'tempfile' | |
module Commands | |
def last_edit(editor=ENV['EDITOR']) | |
t = Tempfile.new('seed') | |
File.open(t.path, 'w') {|f| f.write(Array(Ripl.shell.last_buffer).join("\n")) } | |
system(editor, t.path) | |
Ripl.shell.loop_eval File.read(t.path) | |
end | |
end | |
end | |
Ripl::Shell.include Ripl::MultiLineHistory | |
Ripl::Commands.include Ripl::MultiLineHistory::Commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ripl | |
>> module Cow | |
> def moo | |
> end | |
> end | |
=> nil | |
# Press UP to edit code block as a semi-colon delimited one-liner | |
>> module Cow; def moo; end; end | |
# or use last_edit | |
>> last_edit | |
# Opens the code block in your $EDITOR. | |
# Code is automatically evaled upon exiting file | |
module Cow | |
def moo | |
end | |
end | |
=> nil |
I see you've made ripper an optional engine. Looks good :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hm, I've implemented it with janlelis/ripl-multi_line@279d07d but I'm not sure whether to activate it by default, because it wrongly puts ; into string literals...
I am also playing around with advanced multi-line analyzing (:statement, :string, ...), but then I would need to save the kind of every buffer entry in the
@buffer
to achieve correct functionality.What's your opinion about this?
Update: I think I'll go the second way ;)