Permalink
Browse files

pry-editline 1.0.0

  • Loading branch information...
0 parents commit a903b4f26b32ff328a038928afbfd6b6339e2090 @tpope committed Sep 6, 2011
Showing with 206 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +2 −0 Gemfile
  3. +20 −0 MIT-LICENSE
  4. +91 −0 README.markdown
  5. +12 −0 Rakefile
  6. +60 −0 lib/pry-editline.rb
  7. +18 −0 pry-editline.gemspec
@@ -0,0 +1,3 @@
+/.bundle
+/Gemfile.lock
+/pkg/*
@@ -0,0 +1,2 @@
+source "http://rubygems.org"
+gemspec
@@ -0,0 +1,20 @@
+Copyright (c) Tim Pope
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,91 @@
+pry-editline
+------------
+
+Whenever I'm using IRB or [Pry][], my editor always feels too far away.
+Yes, there are [various gems](http://utilitybelt.rubyforge.org/) out
+there that will let me spawn an editor and evaluate the result, but
+that's not what I need. Usually I'm about 80 characters or so into a
+hairy one-liner when I think, "you know, I really wish I was in Vim
+right about now." In Bash, one can load the current command line into
+an editor with `C-x C-e`. And now, you can do so in IRB and Pry.
+
+The gem is named after [Pry][] so that it will be automatically loaded
+as a Pry plugin (and because, let's face it, that's a good train to
+hitch our wagon to). But it also works in IRB if you add `require
+'pry-editline'` to your `.irbrc`.
+
+[Pry]: http://pry.github.com/
+
+FAQ
+---
+
+> `C-x C-e` is too hard to type.
+
+You can add an alias for it in `~/.inputrc`. Observe:
+
+ $if Ruby
+ "\C-o": "\C-x\C-e"
+ $endif
+
+Actually, I already added `C-o` for you. So don't add that one. It
+already works. It stands for "open".
+
+> It's not working on OS X.
+
+By default, readline on OS X uses libedit. I don't know what that means
+exactly other than it leaves you with a horribly crippled readline that
+doesn't work with pry-editline. To link against a different readline,
+pass the `--with-readline-dir=` flag to `./configure`. If you're using
+RVM, pass it to `rvm install`. Or better yet, make it the default:
+
+ echo rvm_configure_flags=--with-readline-dir=/usr/local >> ~/.rvmrc
+
+To *install* a readline to `/usr/local`, you might consider using
+Homebrew. After installing, you need instruct it to link that readline
+into `/usr/local`:
+
+ brew install readline
+ brew link readline
+
+Actually for me, it went more like:
+
+ $ brew install readline
+ ...
+ $ brew link readline
+ Error: readline has multiple installed versions
+ $ brew link readline 6.2.1
+ Error: readline has multiple installed versions
+ $ brew link readline -v6.2.1
+ Error: readline has multiple installed versions
+ $ brew link -v6.2.1 readline
+ Error: readline has multiple installed versions
+ $ brew remove readline
+ Error: readline has multiple installed versions
+ Use `brew remove --force readline` to remove all versions.
+ $ brew remove -v6.1 readline
+ Error: readline has multiple installed versions
+ Use `brew remove --force readline` to remove all versions.
+ $ brew remove --force readline
+ Uninstalling readline...
+ $ brew install readline
+ ...
+ $ brew link readline
+
+Yeah, it's a bit of a pain. Sorry your OS hates you. :(
+
+> It's not working with `rails console`/my Bundler setup.
+
+[Here's one potential workaround][workaround]. You might have to
+explicitly `require 'pry-editline'` in your `.pryrc`, too.
+
+[workaround]: https://github.com/carlhuda/bundler/issues/183#issuecomment-1149953
+
+> How does it work?
+
+Well first, it overrides `ENV['INPUTRC']` so it can do some magic. And
+then, it does some magic!
+
+License
+-------
+
+Copyright (c) Tim Pope. MIT License.
@@ -0,0 +1,12 @@
+require "bundler/gem_tasks"
+task :default => :build
+
+include_path = "-I#{File.expand_path('../lib', __FILE__)}"
+
+task :pry do
+ exec 'pry', include_path, '-rpry-editline'
+end
+
+task :irb do
+ exec 'irb', include_path, '-rpry-editline'
+end
@@ -0,0 +1,60 @@
+module PryEditline
+
+ def self.editor
+ if defined?(Pry)
+ Pry.editor
+ else
+ ENV.values_at('VISUAL', 'EDITOR').compact.first || 'vi'
+ end
+ end
+
+ def self.hijack_inputrc
+ inputrc = [
+ ENV['INPUTRC'],
+ (File.expand_path('~/.inputrc') rescue nil),
+ '/etc/inputrc'
+ ].compact.detect { |x| File.exist?(x) }
+
+ require 'tempfile'
+ file = Tempfile.new('inputrc')
+ file.puts '"\C-x\C-l": redraw-current-line'
+ file.puts '"\C-x\C-e": " \C-a\t\C-k\C-x\C-l"'
+ file.puts '"\C-o": " \C-a\t\C-k\C-x\C-l"'
+ file.puts "$include #{inputrc}" if inputrc
+ file.close
+ ENV['INPUTRC'] = file.path
+ end
+
+ def self.completion_proc
+ lambda do |s|
+ if Readline.point == 0 && Readline.line_buffer =~ / $/
+ require 'tempfile'
+ Tempfile.open(['readline-','.rb']) do |f|
+ f.puts(Readline.line_buffer[0..-3])
+ f.close
+ system("#{editor} #{f.path}")
+ File.read(f.path).chomp
+ end
+ else
+ yield s
+ end
+ end
+ end
+
+end
+
+if defined?(Pry::InputCompleter)
+ PryEditline.hijack_inputrc
+ class <<Pry::InputCompleter
+ unless method_defined?(:build_completion_proc_without_edit)
+ alias build_completion_proc_without_edit build_completion_proc
+
+ def build_completion_proc(*args)
+ PryEditline.completion_proc(&build_completion_proc_without_edit(*args))
+ end
+ end
+ end
+elsif defined?(IRB) && defined?(Readline)
+ PryEditline.hijack_inputrc
+ Readline.completion_proc = PryEditline.completion_proc(&Readline.completion_proc)
+end
@@ -0,0 +1,18 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+
+Gem::Specification.new do |s|
+ s.name = 'pry-editline'
+ s.version = '1.0.0'
+ s.authors = ['Tim Pope']
+ s.email = ["ruby@tpop"+'e.org']
+ s.homepage = 'https://github.com/tpope/pry-editline'
+ s.summary = 'C-x C-e to invoke an editor on the current pry (or irb) line'
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
+ s.require_paths = ['lib']
+
+ s.add_development_dependency 'rake'
+end

0 comments on commit a903b4f

Please sign in to comment.