Readline + completion of "all files in current directory"

M

Marc Heiler

Hi,

I am having a hard time understanding readline. If i have a list of
possible matches for given files (and subdirectories) in a directory,
how can I make readline work with that, so that i can type something
like:

some_method r<TAB>

where some_method would be just a method, and "r" a file in current
directory (ie. rx.rb ry.rb rz.rb files)

Readline.completion_proc = proc do |word|
Dir['*'].grep(/\A#{Regexp.quote(word)}/)
end
Readline.completion_case_fold = true
# hmm
 
C

Charles Oliver Nutter

Marc said:
Hi,

I am having a hard time understanding readline. If i have a list of
possible matches for given files (and subdirectories) in a directory,
how can I make readline work with that, so that i can type something
like:

some_method r<TAB>

where some_method would be just a method, and "r" a file in current
directory (ie. rx.rb ry.rb rz.rb files)

Readline.completion_proc = proc do |word|
Dir['*'].grep(/\A#{Regexp.quote(word)}/)
end
Readline.completion_case_fold = true

This actually already works on JRuby by mistake; something to do with
the readline library we use. It's a bug, but it's rather amusing:

~/NetBeansProjects/jruby $ jirb
irb(main):001:0> puts build<tab>
build build-config.xml build.xml build_lib
irb(main):001:0> puts build

- Charlie
 
M

Marc Heiler

~/NetBeansProjects/jruby $ jirb
irb(main):001:0> puts build<tab>
build build-config.xml build.xml build_lib
irb(main):001:0> puts build

Hmm but how does it work?
Guess i have to look at the source :/
 
C

Charles Oliver Nutter

Marc said:
Hmm but how does it work?
Guess i have to look at the source :/

Considering that it's a bug...I'm not really sure :)

I believe it has something to do with our readline library (Jline)
probably having some default mode where tab completion includes the
current directory.

- Charlie
 
A

ara.t.howard

I am having a hard time understanding readline. If i have a list of
possible matches for given files (and subdirectories) in a directory,
how can I make readline work with that, so that i can type something
like:

some_method r<TAB>

where some_method would be just a method, and "r" a file in current
directory (ie. rx.rb ry.rb rz.rb files)

Readline.completion_proc = proc do |word|
Dir['*'].grep(/\A#{Regexp.quote(word)}/)
end
Readline.completion_case_fold = true
# hmm

cfp:~ > cat a.rb
require 'readline'

class File
def self.tab arg, prompt = 'directory > '
candidates = Array === arg ? candidates : Dir[arg]
Readline.completion_proc = lambda do |abbrev|
list = candidates.select{|candidate| candidate.index(abbrev)
== 0}
list = list.empty? ? candidates : list
end
Readline.readline prompt
end
end

p File.tab('*').strip


cfp:~ > ruby a.rb
directory > s<tab>
sane_transactions.rb sfctmp.rb site
spadoc_watch src sudo
sciruby sif_nighttime_drop source
sqlite.sh ss.png svn
directory > si<tab>
sif_nighttime_drop site
directory > site
"site"


a @ http://codeforpeople.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top