diff --git a/reader.cpp b/reader.cpp index de0b36793..c3630d73c 100644 --- a/reader.cpp +++ b/reader.cpp @@ -3792,37 +3792,37 @@ const wchar_t *reader_readline(void) } case R_UPCASE_WORD: - { - size_t pos = data->buff_pos; - move_word(MOVE_DIR_RIGHT, false, move_word_style_punctuation, false); - for (; pos < data->buff_pos; pos++) - data->command_line.at(pos) = towupper(data->command_line.at(pos)); - reader_repaint(); - break; - } - case R_DOWNCASE_WORD: - { - size_t pos = data->buff_pos; - move_word(MOVE_DIR_RIGHT, false, move_word_style_punctuation, false); - for (; pos < data->buff_pos; pos++) - data->command_line.at(pos) = towlower(data->command_line.at(pos)); - reader_repaint(); - break; - } - case R_CAPITALIZE_WORD: { - size_t pos = data->buff_pos; + // For capitalize_word, whether we've capitalized a character so far bool capitalized_first = false; + + // We apply the operation from the current location to the end of the word + size_t pos = data->buff_pos; move_word(MOVE_DIR_RIGHT, false, move_word_style_punctuation, false); - for (; pos < data->buff_pos; pos++) { - if (iswalnum(data->command_line.at(pos)) && !capitalized_first) { - data->command_line.at(pos) = towupper(data->command_line.at(pos)); - capitalized_first = true; - } else - data->command_line.at(pos) = towlower(data->command_line.at(pos)); + for (; pos < data->buff_pos; pos++) + { + wchar_t chr = data->command_line.at(pos); + + // We always change the case; this decides whether we go uppercase (true) or lowercase (false) + bool make_uppercase; + if (c == R_CAPITALIZE_WORD) + make_uppercase = ! capitalized_first && iswalnum(chr); + else + make_uppercase = (c == R_UPCASE_WORD); + + // Apply the operation and then record what we did + if (make_uppercase) + chr = towupper(chr); + else + chr = towlower(chr); + + data->command_line.at(pos) = chr; + capitalized_first = capitalized_first || make_uppercase; } + data->command_line_changed(); + reader_super_highlight_me_plenty(data->buff_pos); reader_repaint(); break; }