Skip to content

Instantly share code, notes, and snippets.

@cldwalker
Created April 24, 2011 20:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cldwalker/939846 to your computer and use it in GitHub Desktop.
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
# 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
$ 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
@janlelis
Copy link

nice! I'll include the single-line history as an option in the next multi_line version.

btw, why didn't you call super in line 8?

@cldwalker
Copy link
Author

Because supering would have called MultiLine#loop_once which is not what I want. I only want the last_buffer code to get called at the end of a code block, not on every #loop_once. This as an option would be great. Fyi, I wrote this as a response to http://news.ycombinator.com/item?id=2479431

@janlelis
Copy link

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 ;)

@cldwalker
Copy link
Author

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