Skip to content

Commit 0b45022

Browse files
committed
The config file must accept any character encoding
In Japan, so many programmers used EUC-JP to write text files that contain Japanese. Many .inputrc files which contain EUC-JP are still being copied and used. This commit supports the whole encoding of what user set including UTF-8. ref. #280
1 parent e4ac748 commit 0b45022

File tree

6 files changed

+39
-9
lines changed

6 files changed

+39
-9
lines changed

lib/reline/config.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ def reset_default_key_bindings
157157
end
158158

159159
def read_lines(lines, file = nil)
160+
if lines.first.encoding != Reline.encoding_system_needs
161+
lines = lines.map { |l| l.encode(Reline.encoding_system_needs) }
162+
end
160163
conditions = [@skip_section, @if_stack]
161164
@skip_section = nil
162165
@if_stack = []
@@ -292,7 +295,7 @@ def bind_variable(name, value)
292295

293296
def retrieve_string(str)
294297
str = $1 if str =~ /\A"(.*)"\z/
295-
parse_keyseq(str).map { |c| c.chr(Reline::IOGate.encoding) }.join
298+
parse_keyseq(str).map { |c| c.chr(Reline.encoding_system_needs) }.join
296299
end
297300

298301
def bind_key(key, func_name)

lib/reline/general_io.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
require 'timeout'
22

33
class Reline::GeneralIO
4-
def self.reset
4+
def self.reset(encoding: nil)
55
@@pasting = false
6+
@@encoding = encoding
67
end
78

89
def self.encoding
9-
RUBY_PLATFORM =~ /mswin|mingw/ ? Encoding::UTF_8 : Encoding::default_external
10+
if @@encoding
11+
@@encoding
12+
elsif RUBY_PLATFORM =~ /mswin|mingw/
13+
Encoding::UTF_8
14+
else
15+
Encoding::default_external
16+
end
1017
end
1118

1219
def self.win?

test/reline/helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class <<self
77
def test_mode
88
remove_const('IOGate') if const_defined?('IOGate')
99
const_set('IOGate', Reline::GeneralIO)
10-
Reline::GeneralIO.reset
10+
encoding = (RELINE_TEST_ENCODING rescue nil)
11+
Reline::GeneralIO.reset(encoding: encoding)
1112
send(:core).config.instance_variable_set(:@test_mode, true)
1213
send(:core).config.reset
1314
end

test/reline/test_config.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,25 @@ def test_inputrc
286286
ENV['INPUTRC'] = inputrc_backup
287287
end
288288

289-
def test_inputrc_with_utf
289+
def test_inputrc_with_utf8
290+
# This file is encoded by UTF-8 so this heredoc string is also UTF-8.
290291
@config.read_lines(<<~'LINES'.lines)
291292
set editing-mode vi
292293
set vi-cmd-mode-string 🍸
293294
set vi-ins-mode-string 🍶
294295
LINES
295-
assert_equal @config.vi_cmd_mode_string, "🍸"
296-
assert_equal @config.vi_ins_mode_string, "🍶"
296+
assert_equal '🍸', @config.vi_cmd_mode_string
297+
assert_equal '🍶', @config.vi_ins_mode_string
298+
end
299+
300+
def test_inputrc_with_eucjp
301+
@config.read_lines(<<~"LINES".encode(Encoding::EUC_JP).lines)
302+
set editing-mode vi
303+
set vi-cmd-mode-string ォャッ
304+
set vi-ins-mode-string 能
305+
LINES
306+
assert_equal 'ォャッ'.encode(Reline.encoding_system_needs), @config.vi_cmd_mode_string
307+
assert_equal '能'.encode(Reline.encoding_system_needs), @config.vi_ins_mode_string
297308
end
298309

299310
def test_xdg_config_home

test/reline/test_history.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@ def assert_external_string_equal(expected, actual)
292292
end
293293

294294
def get_default_internal_encoding
295-
if RUBY_PLATFORM =~ /mswin|mingw/
295+
if encoding = (RELINE_TEST_ENCODING rescue nil)
296+
encoding
297+
elsif RUBY_PLATFORM =~ /mswin|mingw/
296298
Encoding.default_internal || Encoding::UTF_8
297299
else
298300
Encoding.default_internal || Encoding.find("locale")

test/reline/test_reline.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ def test_may_req_ambiguous_char_width
314314
end
315315

316316
def get_reline_encoding
317-
RUBY_PLATFORM =~ /mswin|mingw/ ? Encoding::UTF_8 : Encoding::default_external
317+
if encoding = (RELINE_TEST_ENCODING rescue nil)
318+
encoding
319+
elsif RUBY_PLATFORM =~ /mswin|mingw/
320+
Encoding::UTF_8
321+
else
322+
Encoding::default_external
323+
end
318324
end
319325
end

0 commit comments

Comments
 (0)