Project

General

Profile

Feature #12659

Readline: expose rl_char_is_quoted_p setting

Added by georgebrock (George Brocklehurst) about 4 years ago. Updated about 4 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:76750]

Description

I maintain an interactive Git shell 1 built in Ruby, which made extensive use of the Readline module. We found we outgrew the functionality provided by the module, so we built our own Gitsh::LineEditor module using Ruby's Readline module as a starting point and adding more GNU Readline features to it. I'd like to contribute the features I add back upstream to Ruby. This is the first of them.

This patch exposes rl_char_is_quoted_p 2 as Readline.quoting_detection_proc.

rl_char_is_quoted_p is used to specify a function that determines if a character is escaped. It's called with completer word break characters that would normally indicate the boundary between two arguments, so that Readline can determine how much text to pass to the completion proc. For example, if we were implementing a Unix shell in Ruby using Readline, and a user typed ls some\ fil<tab> by default we'd get "fil" passed to our completion proc, but what we'd really want would be "some\ fil". Setting rl_char_is_quoted_p allows us to implement this behaviour.

The C function we assign to rl_char_is_quoted_p is called with a char array and an index into it, we convert those into a Ruby string and character index and pass them on to the proc. The implementation accounts for multibyte characters.

There are tests included in the patch that demonstrate the feature, and here's a simple program that uses it:

require "readline"

completion_text = nil
Readline.completion_proc = -> (text) do
  completion_text = text
  ["completed"]
end
Readline.completer_word_break_characters = " "
Readline.completer_quote_characters = "\"'"
Readline.quoting_detection_proc = -> (text, index) do
  index > 0 && text[index-1] == "\\"
end

while line = Readline.readline("> ") do
  p line
  p completion_text
  completion_text = nil
end

This is my first contribution to Ruby, so I'm sure there'll be some changes I need to make before it's accepted. I hope to be able to follow up with a few more Readline features as I build more gitsh features.

Thanks to my colleague Adam Sharp for pairing with me on the multibyte string support.


Files

readline_quoting_detection_proc.patch (9.29 KB) readline_quoting_detection_proc.patch georgebrock (George Brocklehurst), 08/06/2016 12:55 AM

Also available in: Atom PDF