From 1fcaac9d2ec39ebc9f32657c867589162dffd0b8 Mon Sep 17 00:00:00 2001 From: lethosor Date: Sun, 25 May 2014 21:52:16 -0400 Subject: [PATCH 1/4] OS X Console: Implement back/forward one word escape sequences --- library/Console-darwin.cpp | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/library/Console-darwin.cpp b/library/Console-darwin.cpp index 86cd657a1..081d4833e 100644 --- a/library/Console-darwin.cpp +++ b/library/Console-darwin.cpp @@ -478,14 +478,46 @@ namespace DFHack break; case 27: // escape sequence lock->unlock(); - if(!read_char(seq[0]) || !read_char(seq[1])) + if (!read_char(seq[0])) { lock->lock(); return -2; } lock->lock(); - if(seq[0] == '[') + if (seq[0] == 'b') { + // Back one word + if (raw_cursor == 0) + break; + raw_cursor--; + while (raw_cursor > 0 && !isalnum(raw_buffer[raw_cursor])) + raw_cursor--; + while (raw_cursor > 0 && isalnum(raw_buffer[raw_cursor])) + raw_cursor--; + if (!isalnum(raw_buffer[raw_cursor])) + raw_cursor++; + prompt_refresh(); + } + else if (seq[0] == 'f') + { + // Forward one word + int len = raw_buffer.size(); + if (raw_cursor == len) + break; + raw_cursor++; + while (raw_cursor <= len && !isalnum(raw_buffer[raw_cursor])) + raw_cursor++; + while (raw_cursor <= len && isalnum(raw_buffer[raw_cursor])) + raw_cursor++; + prompt_refresh(); + } + else if(seq[0] == '[') + { + if (!read_char(seq[1])) + { + lock->lock(); + return -2; + } if (seq[1] == 'D') { left_arrow: From 58b9c02ce5dc518c0904cc405fe8128d7d1592f6 Mon Sep 17 00:00:00 2001 From: lethosor Date: Sun, 25 May 2014 22:10:40 -0400 Subject: [PATCH 2/4] Migrate esc-b/f sequences to Linux --- library/Console-linux.cpp | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/library/Console-linux.cpp b/library/Console-linux.cpp index f32fa1c2a..a49b5dbdd 100644 --- a/library/Console-linux.cpp +++ b/library/Console-linux.cpp @@ -480,14 +480,46 @@ namespace DFHack break; case 27: // escape sequence lock->unlock(); - if(!read_char(seq[0]) || !read_char(seq[1])) + if (!read_char(seq[0])) { lock->lock(); return -2; } lock->lock(); - if(seq[0] == '[') + if (seq[0] == 'b') { + // Back one word + if (raw_cursor == 0) + break; + raw_cursor--; + while (raw_cursor > 0 && !isalnum(raw_buffer[raw_cursor])) + raw_cursor--; + while (raw_cursor > 0 && isalnum(raw_buffer[raw_cursor])) + raw_cursor--; + if (!isalnum(raw_buffer[raw_cursor])) + raw_cursor++; + prompt_refresh(); + } + else if (seq[0] == 'f') + { + // Forward one word + int len = raw_buffer.size(); + if (raw_cursor == len) + break; + raw_cursor++; + while (raw_cursor <= len && !isalnum(raw_buffer[raw_cursor])) + raw_cursor++; + while (raw_cursor <= len && isalnum(raw_buffer[raw_cursor])) + raw_cursor++; + prompt_refresh(); + } + else if(seq[0] == '[') + { + if (!read_char(seq[1])) + { + lock->lock(); + return -2; + } if (seq[1] == 'D') { left_arrow: From d320fe71d11274d33c1d8ff3f59e8122478fdb4c Mon Sep 17 00:00:00 2001 From: lethosor Date: Sun, 25 May 2014 23:39:34 -0400 Subject: [PATCH 3/4] Implement extended arrow key sequences --- library/Console-darwin.cpp | 69 ++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/library/Console-darwin.cpp b/library/Console-darwin.cpp index 081d4833e..f36973d5c 100644 --- a/library/Console-darwin.cpp +++ b/library/Console-darwin.cpp @@ -305,6 +305,33 @@ namespace DFHack } /// beep. maybe? //void beep (void); + void back_word() + { + if (raw_cursor == 0) + return; + raw_cursor--; + while (raw_cursor > 0 && !isalnum(raw_buffer[raw_cursor])) + raw_cursor--; + while (raw_cursor > 0 && isalnum(raw_buffer[raw_cursor])) + raw_cursor--; + if (!isalnum(raw_buffer[raw_cursor]) && raw_cursor != 0) + raw_cursor++; + prompt_refresh(); + } + void forward_word() + { + int len = raw_buffer.size(); + if (raw_cursor == len) + return; + raw_cursor++; + while (raw_cursor <= len && !isalnum(raw_buffer[raw_cursor])) + raw_cursor++; + while (raw_cursor <= len && isalnum(raw_buffer[raw_cursor])) + raw_cursor++; + if (raw_cursor > len) + raw_cursor = len; + prompt_refresh(); + } /// A simple line edit (raw mode) int lineedit(const std::string& prompt, std::string& output, recursive_mutex * lock, CommandHistory & ch) { @@ -486,30 +513,11 @@ namespace DFHack lock->lock(); if (seq[0] == 'b') { - // Back one word - if (raw_cursor == 0) - break; - raw_cursor--; - while (raw_cursor > 0 && !isalnum(raw_buffer[raw_cursor])) - raw_cursor--; - while (raw_cursor > 0 && isalnum(raw_buffer[raw_cursor])) - raw_cursor--; - if (!isalnum(raw_buffer[raw_cursor])) - raw_cursor++; - prompt_refresh(); + back_word(); } else if (seq[0] == 'f') { - // Forward one word - int len = raw_buffer.size(); - if (raw_cursor == len) - break; - raw_cursor++; - while (raw_cursor <= len && !isalnum(raw_buffer[raw_cursor])) - raw_cursor++; - while (raw_cursor <= len && isalnum(raw_buffer[raw_cursor])) - raw_cursor++; - prompt_refresh(); + forward_word(); } else if(seq[0] == '[') { @@ -577,6 +585,7 @@ namespace DFHack else if (seq[1] > '0' && seq[1] < '7') { // extended escape + unsigned char seq3[3]; lock->unlock(); if(!read_char(seq2)) { @@ -593,6 +602,24 @@ namespace DFHack prompt_refresh(); } } + if (!read_char(seq3[0]) || !read_char(seq3[1])) + { + lock->lock(); + return -2; + } + if (seq2 == ';') + { + // Format: esc [ n ; n DIRECTION + // Ignore first character (second "n") + if (seq3[1] == 'C') + { + forward_word(); + } + else if (seq3[1] == 'D') + { + back_word(); + } + } } } break; From bdca1ee7095ddc2dd5c6e4f8d84812cf69b774a6 Mon Sep 17 00:00:00 2001 From: lethosor Date: Mon, 26 May 2014 10:03:28 -0400 Subject: [PATCH 4/4] Linux: Extended back/forward word sequences --- library/Console-linux.cpp | 69 +++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/library/Console-linux.cpp b/library/Console-linux.cpp index a49b5dbdd..d4005af3c 100644 --- a/library/Console-linux.cpp +++ b/library/Console-linux.cpp @@ -307,6 +307,33 @@ namespace DFHack } /// beep. maybe? //void beep (void); + void back_word() + { + if (raw_cursor == 0) + return; + raw_cursor--; + while (raw_cursor > 0 && !isalnum(raw_buffer[raw_cursor])) + raw_cursor--; + while (raw_cursor > 0 && isalnum(raw_buffer[raw_cursor])) + raw_cursor--; + if (!isalnum(raw_buffer[raw_cursor]) && raw_cursor != 0) + raw_cursor++; + prompt_refresh(); + } + void forward_word() + { + int len = raw_buffer.size(); + if (raw_cursor == len) + return; + raw_cursor++; + while (raw_cursor <= len && !isalnum(raw_buffer[raw_cursor])) + raw_cursor++; + while (raw_cursor <= len && isalnum(raw_buffer[raw_cursor])) + raw_cursor++; + if (raw_cursor > len) + raw_cursor = len; + prompt_refresh(); + } /// A simple line edit (raw mode) int lineedit(const std::string& prompt, std::string& output, recursive_mutex * lock, CommandHistory & ch) { @@ -488,30 +515,11 @@ namespace DFHack lock->lock(); if (seq[0] == 'b') { - // Back one word - if (raw_cursor == 0) - break; - raw_cursor--; - while (raw_cursor > 0 && !isalnum(raw_buffer[raw_cursor])) - raw_cursor--; - while (raw_cursor > 0 && isalnum(raw_buffer[raw_cursor])) - raw_cursor--; - if (!isalnum(raw_buffer[raw_cursor])) - raw_cursor++; - prompt_refresh(); + back_word(); } else if (seq[0] == 'f') { - // Forward one word - int len = raw_buffer.size(); - if (raw_cursor == len) - break; - raw_cursor++; - while (raw_cursor <= len && !isalnum(raw_buffer[raw_cursor])) - raw_cursor++; - while (raw_cursor <= len && isalnum(raw_buffer[raw_cursor])) - raw_cursor++; - prompt_refresh(); + forward_word(); } else if(seq[0] == '[') { @@ -579,6 +587,7 @@ namespace DFHack else if (seq[1] > '0' && seq[1] < '7') { // extended escape + unsigned char seq3[3]; lock->unlock(); if(!read_char(seq2)) { @@ -595,6 +604,24 @@ namespace DFHack prompt_refresh(); } } + if (!read_char(seq3[0]) || !read_char(seq3[1])) + { + lock->lock(); + return -2; + } + if (seq2 == ';') + { + // Format: esc [ n ; n DIRECTION + // Ignore first character (second "n") + if (seq3[1] == 'C') + { + forward_word(); + } + else if (seq3[1] == 'D') + { + back_word(); + } + } } } break;