summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authortomoya ishida <tomoyapenguin@gmail.com>2024-03-17 00:19:59 +0900
committergit <svn-admin@ruby-lang.org>2024-03-16 15:20:03 +0000
commitbda5b099375d91274a5314aad2608f8e5b37e891 (patch)
tree5bcb4eec33c813bf311a72c5c8fc9b20c4c335d1 /lib
parentaae9f5628c5af6a5eb8027e9f21b44fa80162d35 (diff)
[ruby/irb] Fix irb_history saved to current directory
(https://github.com/ruby/irb/pull/901) * Always save irb_history in HOME or XDG_CONFIG_HOME Also split irbrc search logic from irb_history search logic as a refactor * Remove IRB.conf[:RC_NAME_GENERATOR] because it's not configurable This conf is used to specify which irbrc to load. Need to configure before irbrc is loaded, so it's actually not configurable. This conf is also used for history file search, but it is configurable by conf[:HISTORY_FILE]. * remove rc_file_test because it is tested with rc_files, remove useless test setup * Make internal irbrc searching method private https://github.com/ruby/irb/commit/11d03a6ff7
Diffstat (limited to 'lib')
-rw-r--r--lib/irb/command/irb_info.rb2
-rw-r--r--lib/irb/history.rb9
-rw-r--r--lib/irb/init.rb104
-rw-r--r--lib/irb/lc/error.rb5
-rw-r--r--lib/irb/lc/ja/error.rb5
5 files changed, 67 insertions, 58 deletions
diff --git a/lib/irb/command/irb_info.rb b/lib/irb/command/irb_info.rb
index 31e6d77d25..cc93fdcbd5 100644
--- a/lib/irb/command/irb_info.rb
+++ b/lib/irb/command/irb_info.rb
@@ -13,7 +13,7 @@ module IRB
str += "IRB version: #{IRB.version}\n"
str += "InputMethod: #{IRB.CurrentContext.io.inspect}\n"
str += "Completion: #{IRB.CurrentContext.io.respond_to?(:completion_info) ? IRB.CurrentContext.io.completion_info : 'off'}\n"
- rc_files = IRB.rc_files.select { |rc| File.exist?(rc) }
+ rc_files = IRB.irbrc_files
str += ".irbrc paths: #{rc_files.join(", ")}\n" if rc_files.any?
str += "RUBY_PLATFORM: #{RUBY_PLATFORM}\n"
str += "LANG env: #{ENV["LANG"]}\n" if ENV["LANG"] && !ENV["LANG"].empty?
diff --git a/lib/irb/history.rb b/lib/irb/history.rb
index 2489f7403f..685354b2d8 100644
--- a/lib/irb/history.rb
+++ b/lib/irb/history.rb
@@ -16,8 +16,8 @@ module IRB
if history_file = IRB.conf[:HISTORY_FILE]
history_file = File.expand_path(history_file)
end
- history_file = IRB.rc_files("_history").first unless history_file
- if File.exist?(history_file)
+ history_file = IRB.rc_file("_history") unless history_file
+ if history_file && File.exist?(history_file)
File.open(history_file, "r:#{IRB.conf[:LC_MESSAGES].encoding}") do |f|
f.each { |l|
l = l.chomp
@@ -41,7 +41,10 @@ module IRB
if history_file = IRB.conf[:HISTORY_FILE]
history_file = File.expand_path(history_file)
end
- history_file = IRB.rc_files("_history").first unless history_file
+ history_file = IRB.rc_file("_history") unless history_file
+
+ # When HOME and XDG_CONFIG_HOME are not available, history_file might be nil
+ return unless history_file
# Change the permission of a file that already exists[BUG #7694]
begin
diff --git a/lib/irb/init.rb b/lib/irb/init.rb
index 5813ff7ae0..355047519c 100644
--- a/lib/irb/init.rb
+++ b/lib/irb/init.rb
@@ -395,10 +395,8 @@ module IRB # :nodoc:
# Run the config file
def IRB.run_config
if @CONF[:RC]
- rc_files.each do |rc|
- # Because rc_file always returns `HOME/.irbrc` even if no rc file is present, we can't warn users about missing rc files.
- # Otherwise, it'd be very noisy.
- load rc if File.exist?(rc)
+ irbrc_files.each do |rc|
+ load rc
rescue StandardError, ScriptError => e
warn "Error loading RC file '#{rc}':\n#{e.full_message(highlight: false)}"
end
@@ -406,53 +404,27 @@ module IRB # :nodoc:
end
IRBRC_EXT = "rc"
- def IRB.rc_file(ext = IRBRC_EXT)
- warn "rc_file is deprecated, please use rc_files instead."
- rc_files(ext).first
- end
-
- def IRB.rc_files(ext = IRBRC_EXT)
- if !@CONF[:RC_NAME_GENERATOR]
- @CONF[:RC_NAME_GENERATOR] ||= []
- existing_rc_file_generators = []
- rc_file_generators do |rcgen|
- @CONF[:RC_NAME_GENERATOR] << rcgen
- existing_rc_file_generators << rcgen if File.exist?(rcgen.call(ext))
- end
+ def IRB.rc_file(ext)
+ prepare_irbrc_name_generators
- if existing_rc_file_generators.any?
- @CONF[:RC_NAME_GENERATOR] = existing_rc_file_generators
- end
+ # When irbrc exist in default location
+ if (rcgen = @existing_rc_name_generators.first)
+ return rcgen.call(ext)
end
- @CONF[:RC_NAME_GENERATOR].map do |rc|
- rc_file = rc.call(ext)
- fail IllegalRCNameGenerator unless rc_file.is_a?(String)
- rc_file
+ # When irbrc does not exist in default location
+ rc_file_generators do |rcgen|
+ return rcgen.call(ext)
end
+
+ # When HOME and XDG_CONFIG_HOME are not available
+ nil
end
- # enumerate possible rc-file base name generators
- def IRB.rc_file_generators
- if irbrc = ENV["IRBRC"]
- yield proc{|rc| rc == "rc" ? irbrc : irbrc+rc}
- end
- if xdg_config_home = ENV["XDG_CONFIG_HOME"]
- irb_home = File.join(xdg_config_home, "irb")
- if File.directory?(irb_home)
- yield proc{|rc| irb_home + "/irb#{rc}"}
- end
- end
- if home = ENV["HOME"]
- yield proc{|rc| home+"/.irb#{rc}"}
- yield proc{|rc| home+"/.config/irb/irb#{rc}"}
- end
- current_dir = Dir.pwd
- yield proc{|rc| current_dir+"/.irb#{rc}"}
- yield proc{|rc| current_dir+"/irb#{rc.sub(/\A_?/, '.')}"}
- yield proc{|rc| current_dir+"/_irb#{rc}"}
- yield proc{|rc| current_dir+"/$irb#{rc}"}
+ def IRB.irbrc_files
+ prepare_irbrc_name_generators
+ @irbrc_files
end
# loading modules
@@ -468,6 +440,50 @@ module IRB # :nodoc:
class << IRB
private
+
+ def prepare_irbrc_name_generators
+ return if @existing_rc_name_generators
+
+ @existing_rc_name_generators = []
+ @irbrc_files = []
+ rc_file_generators do |rcgen|
+ irbrc = rcgen.call(IRBRC_EXT)
+ if File.exist?(irbrc)
+ @irbrc_files << irbrc
+ @existing_rc_name_generators << rcgen
+ end
+ end
+ generate_current_dir_irbrc_files.each do |irbrc|
+ @irbrc_files << irbrc if File.exist?(irbrc)
+ end
+ @irbrc_files.uniq!
+ end
+
+ # enumerate possible rc-file base name generators
+ def rc_file_generators
+ if irbrc = ENV["IRBRC"]
+ yield proc{|rc| rc == "rc" ? irbrc : irbrc+rc}
+ end
+ if xdg_config_home = ENV["XDG_CONFIG_HOME"]
+ irb_home = File.join(xdg_config_home, "irb")
+ if File.directory?(irb_home)
+ yield proc{|rc| irb_home + "/irb#{rc}"}
+ end
+ end
+ if home = ENV["HOME"]
+ yield proc{|rc| home+"/.irb#{rc}"}
+ if xdg_config_home.nil? || xdg_config_home.empty?
+ yield proc{|rc| home+"/.config/irb/irb#{rc}"}
+ end
+ end
+ end
+
+ # possible irbrc files in current directory
+ def generate_current_dir_irbrc_files
+ current_dir = Dir.pwd
+ %w[.irbrc irbrc _irbrc $irbrc].map { |file| "#{current_dir}/#{file}" }
+ end
+
def set_encoding(extern, intern = nil, override: true)
verbose, $VERBOSE = $VERBOSE, nil
Encoding.default_external = extern unless extern.nil? || extern.empty?
diff --git a/lib/irb/lc/error.rb b/lib/irb/lc/error.rb
index 9041ec4d6c..ee0f047822 100644
--- a/lib/irb/lc/error.rb
+++ b/lib/irb/lc/error.rb
@@ -47,11 +47,6 @@ module IRB
super("Undefined prompt mode(#{val}).")
end
end
- class IllegalRCGenerator < StandardError
- def initialize
- super("Define illegal RC_NAME_GENERATOR.")
- end
- end
# :startdoc:
end
diff --git a/lib/irb/lc/ja/error.rb b/lib/irb/lc/ja/error.rb
index f7f2b13c45..9e2e5b8870 100644
--- a/lib/irb/lc/ja/error.rb
+++ b/lib/irb/lc/ja/error.rb
@@ -47,11 +47,6 @@ module IRB
super("プロンプトモード(#{val})は定義されていません.")
end
end
- class IllegalRCGenerator < StandardError
- def initialize
- super("RC_NAME_GENERATORが正しく定義されていません.")
- end
- end
# :startdoc:
end