説明を見る。00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <readline/readline.h>
00030 #include <readline/history.h>
00031
00032
00033 #include "d8.h"
00034
00035
00036 namespace v8 {
00037
00038
00039 class ReadLineEditor: public LineEditor {
00040 public:
00041 ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { }
00042 virtual i::SmartPointer<char> Prompt(const char* prompt);
00043 virtual bool Open();
00044 virtual bool Close();
00045 virtual void AddHistory(const char* str);
00046 private:
00047 static char** AttemptedCompletion(const char* text, int start, int end);
00048 static char* CompletionGenerator(const char* text, int state);
00049 static char kWordBreakCharacters[];
00050 };
00051
00052
00053 static ReadLineEditor read_line_editor;
00054 char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"',
00055 '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(',
00056 '\0'};
00057
00058
00059 bool ReadLineEditor::Open() {
00060 rl_initialize();
00061 rl_attempted_completion_function = AttemptedCompletion;
00062 rl_completer_word_break_characters = kWordBreakCharacters;
00063 rl_bind_key('\t', rl_complete);
00064 using_history();
00065 return read_history(Shell::kHistoryFileName) == 0;
00066 }
00067
00068
00069 bool ReadLineEditor::Close() {
00070 return write_history(Shell::kHistoryFileName) == 0;
00071 }
00072
00073
00074 i::SmartPointer<char> ReadLineEditor::Prompt(const char* prompt) {
00075 char* result = readline(prompt);
00076 return i::SmartPointer<char>(result);
00077 }
00078
00079
00080 void ReadLineEditor::AddHistory(const char* str) {
00081 add_history(str);
00082 }
00083
00084
00085 char** ReadLineEditor::AttemptedCompletion(const char* text,
00086 int start,
00087 int end) {
00088 char** result = rl_completion_matches(text, CompletionGenerator);
00089 rl_attempted_completion_over = true;
00090 return result;
00091 }
00092
00093
00094 char* ReadLineEditor::CompletionGenerator(const char* text, int state) {
00095 static unsigned current_index;
00096 static Persistent<Array> current_completions;
00097 if (state == 0) {
00098 i::SmartPointer<char> full_text(strndup(rl_line_buffer, rl_point));
00099 HandleScope scope;
00100 Handle<Array> completions =
00101 Shell::GetCompletions(String::New(text), String::New(*full_text));
00102 current_completions = Persistent<Array>::New(completions);
00103 current_index = 0;
00104 }
00105 if (current_index < current_completions->Length()) {
00106 HandleScope scope;
00107 Handle<Integer> index = Integer::New(current_index);
00108 Handle<Value> str_obj = current_completions->Get(index);
00109 current_index++;
00110 String::Utf8Value str(str_obj);
00111 return strdup(*str);
00112 } else {
00113 current_completions.Dispose();
00114 current_completions.Clear();
00115 return NULL;
00116 }
00117 }
00118
00119
00120 }