2012-12-29 17:34:59 -05:00
|
|
|
class Pry
|
|
|
|
class Editor
|
|
|
|
extend Pry::Helpers::BaseHelpers
|
|
|
|
extend Pry::Helpers::CommandHelpers
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def edit_tempfile_with_content(initial_content, line=1)
|
|
|
|
temp_file do |f|
|
|
|
|
f.puts(initial_content)
|
|
|
|
f.flush
|
|
|
|
f.close(false)
|
2013-01-02 10:25:18 -05:00
|
|
|
invoke_editor(f.path, line, true)
|
2012-12-29 17:34:59 -05:00
|
|
|
File.read(f.path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-02 10:25:18 -05:00
|
|
|
def invoke_editor(file, line, blocking=true)
|
2012-12-29 17:34:59 -05:00
|
|
|
raise CommandError, "Please set Pry.config.editor or export $VISUAL or $EDITOR" unless Pry.config.editor
|
2013-01-02 10:25:18 -05:00
|
|
|
|
|
|
|
editor_invocation = build_editor_invocation_string(file, line, blocking)
|
2012-12-29 17:34:59 -05:00
|
|
|
return nil unless editor_invocation
|
|
|
|
|
|
|
|
if jruby?
|
2013-01-02 10:25:18 -05:00
|
|
|
open_editor_on_jruby(editor_invocation)
|
2012-12-29 17:34:59 -05:00
|
|
|
else
|
2013-01-02 10:25:18 -05:00
|
|
|
open_editor(editor_invocation)
|
2012-12-29 17:34:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-01-02 10:25:18 -05:00
|
|
|
# Generate the string that's used to start the editor. This includes
|
|
|
|
# all the flags we want as well as the file and line number we
|
|
|
|
# want to open at.
|
|
|
|
def build_editor_invocation_string(file, line, blocking)
|
2013-01-13 16:09:09 -05:00
|
|
|
|
2013-01-02 10:25:18 -05:00
|
|
|
if Pry.config.editor.respond_to?(:call)
|
|
|
|
args = [file, line, blocking][0...(Pry.config.editor.arity)]
|
|
|
|
Pry.config.editor.call(*args)
|
|
|
|
else
|
2013-01-13 17:00:22 -05:00
|
|
|
sanitized_file = if windows?
|
2013-01-13 17:24:58 -05:00
|
|
|
file.gsub(/\//, '\\')
|
|
|
|
else
|
|
|
|
Shellwords.escape(file)
|
|
|
|
end
|
2013-01-13 17:00:22 -05:00
|
|
|
|
|
|
|
"#{Pry.config.editor} #{blocking_flag_for_editor(blocking)} #{start_line_syntax_for_editor(sanitized_file, line)}"
|
2013-01-02 10:25:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Start the editor running, using the calculated invocation string
|
|
|
|
def open_editor(editor_invocation)
|
|
|
|
# Note we dont want to use Pry.config.system here as that
|
|
|
|
# may be invoked non-interactively (i.e via Open4), whereas we want to
|
|
|
|
# ensure the editor is always interactive
|
2013-03-25 02:36:08 -04:00
|
|
|
system(*Shellwords.split(editor_invocation)) or raise CommandError, "`#{editor_invocation}` gave exit status: #{$?.exitstatus}"
|
2013-01-02 10:25:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# We need JRuby specific code here cos just shelling out using
|
|
|
|
# system() appears to be pretty broken :/
|
|
|
|
def open_editor_on_jruby(editor_invocation)
|
|
|
|
begin
|
|
|
|
require 'spoon'
|
2013-03-25 02:36:08 -04:00
|
|
|
pid = Spoon.spawnp(*Shellwords.split(editor_invocation))
|
2013-01-02 10:25:18 -05:00
|
|
|
Process.waitpid(pid)
|
|
|
|
rescue FFI::NotFoundError
|
|
|
|
system(editor_invocation)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-29 17:34:59 -05:00
|
|
|
# Some editors that run outside the terminal allow you to control whether or
|
|
|
|
# not to block the process from which they were launched (in this case, Pry).
|
|
|
|
# For those editors, return the flag that produces the desired behavior.
|
2013-01-02 10:25:18 -05:00
|
|
|
def blocking_flag_for_editor(blocking)
|
2012-12-29 17:34:59 -05:00
|
|
|
case editor_name
|
|
|
|
when /^emacsclient/
|
2013-01-02 10:25:18 -05:00
|
|
|
'--no-wait' unless blocking
|
2012-12-29 17:34:59 -05:00
|
|
|
when /^[gm]vim/
|
2013-01-02 10:25:18 -05:00
|
|
|
'--nofork' if blocking
|
2012-12-29 17:34:59 -05:00
|
|
|
when /^jedit/
|
2013-01-02 10:25:18 -05:00
|
|
|
'-wait' if blocking
|
2012-12-29 17:34:59 -05:00
|
|
|
when /^mate/, /^subl/
|
2013-01-02 10:25:18 -05:00
|
|
|
'-w' if blocking
|
2012-12-29 17:34:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return the syntax for a given editor for starting the editor
|
|
|
|
# and moving to a particular line within that file
|
|
|
|
def start_line_syntax_for_editor(file_name, line_number)
|
|
|
|
# special case for 1st line
|
|
|
|
return file_name if line_number <= 1
|
|
|
|
|
|
|
|
case editor_name
|
|
|
|
when /^[gm]?vi/, /^emacs/, /^nano/, /^pico/, /^gedit/, /^kate/
|
|
|
|
"+#{line_number} #{file_name}"
|
|
|
|
when /^mate/, /^geany/
|
|
|
|
"-l #{line_number} #{file_name}"
|
|
|
|
when /^subl/
|
|
|
|
"#{file_name}:#{line_number}"
|
|
|
|
when /^uedit32/
|
|
|
|
"#{file_name}/#{line_number}"
|
|
|
|
when /^jedit/
|
|
|
|
"#{file_name} +line:#{line_number}"
|
|
|
|
else
|
|
|
|
if windows?
|
|
|
|
"#{file_name}"
|
|
|
|
else
|
|
|
|
"+#{line_number} #{file_name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get the name of the binary that Pry.config.editor points to.
|
|
|
|
#
|
|
|
|
# This is useful for deciding which flags we pass to the editor as
|
|
|
|
# we can just use the program's name and ignore any absolute paths.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# Pry.config.editor="/home/conrad/bin/textmate -w"
|
|
|
|
# editor_name
|
|
|
|
# # => textmate
|
|
|
|
#
|
|
|
|
def editor_name
|
|
|
|
File.basename(Pry.config.editor).split(" ").first
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|