From 2789e5da50987b908a4aa5758a17c86570d94d63 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sat, 7 Mar 2015 02:45:56 +0100 Subject: cleaned up usage of the escape control character: introduced CTL_KEY_ESC and CTL_KEY_ESC_STR * the reason for the CTL_KEY() macro is to get the control character resulting from a CTRL+Key press -- at least this is how SciTECO presents these key presses. It is also a macro and may be resolved to a constant expression, so it can be used in switch-case statements. Sometimes it is clearer to use standard C escape sequences (like '\t'). * CTL_KEY('[') for escape is hard to read, so I always used '\x1B' which is even more cryptic. --- src/cmdline.cpp | 6 +++--- src/interface-curses.cpp | 2 +- src/interface-gtk.cpp | 4 ++-- src/main.cpp | 2 +- src/parser.cpp | 10 +++++----- src/sciteco.h | 17 +++++++++++++++-- 6 files changed, 27 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/cmdline.cpp b/src/cmdline.cpp index a995d55..888a70c 100644 --- a/src/cmdline.cpp +++ b/src/cmdline.cpp @@ -102,7 +102,7 @@ copy(void) const void Cmdline::replace(void) { - QRegister *cmdline_reg = QRegisters::globals["\x1B"]; + QRegister *cmdline_reg = QRegisters::globals[CTL_KEY_ESC_STR]; /* use heap object to avoid copy constructors etc. */ Cmdline *new_cmdline = new Cmdline(); @@ -426,11 +426,11 @@ Cmdline::process_edit_cmd(gchar key) } break; - case '\x1B': /* terminate command line */ + case CTL_KEY_ESC: /* terminate command line */ interface.popup_clear(); if (States::current == &States::start && - str && str[len-1] == '\x1B') { + str && str[len-1] == CTL_KEY_ESC) { if (Goto::skip_label) { interface.msg(InterfaceCurrent::MSG_ERROR, "Label \"%s\" not found", diff --git a/src/interface-curses.cpp b/src/interface-curses.cpp index dd08e8f..e05643b 100644 --- a/src/interface-curses.cpp +++ b/src/interface-curses.cpp @@ -257,7 +257,7 @@ InterfaceCurses::format_chr(chtype *&target, gchar chr, attr_t attr) * View::set_representations() */ switch (chr) { - case '\x1B': /* escape */ + case CTL_KEY_ESC: *target++ = '$' | attr | A_REVERSE; break; case '\r': diff --git a/src/interface-gtk.cpp b/src/interface-gtk.cpp index e7f6927..8375dd8 100644 --- a/src/interface-gtk.cpp +++ b/src/interface-gtk.cpp @@ -196,7 +196,7 @@ InterfaceGtk::cmdline_insert_chr(gint &pos, gchar chr) * View::set_representations() */ switch (chr) { - case '\x1B': /* escape */ + case CTL_KEY_ESC: strcpy(buffer, "$"); break; case '\r': @@ -308,7 +308,7 @@ handle_key_press(bool is_shift, bool is_ctl, guint keyval) { switch (keyval) { case GDK_Escape: - cmdline.keypress('\x1B'); + cmdline.keypress(CTL_KEY_ESC); break; case GDK_BackSpace: cmdline.keypress('\b'); diff --git a/src/main.cpp b/src/main.cpp index ea192db..ed054d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -338,7 +338,7 @@ main(int argc, char **argv) * If munged file didn't quit, switch into interactive mode */ /* commandline replacement string register */ - QRegisters::globals.insert("\x1B"); + QRegisters::globals.insert(CTL_KEY_ESC_STR); Goto::table = &cmdline_goto_table; undo.enabled = true; diff --git a/src/parser.cpp b/src/parser.cpp index ce77aaf..1514e24 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -77,7 +77,7 @@ static bool skip_else = false; static gint nest_level = 0; gchar *strings[2] = {NULL, NULL}; -gchar escape_char = '\x1B'; +gchar escape_char = CTL_KEY_ESC; /** * Handles all expected exceptions, converting them to @@ -440,7 +440,7 @@ StateExpectString::custom(gchar chr) undo.push_var(Modifiers::at) = false; switch (escape_char) { - case '\x1B': + case CTL_KEY_ESC: case '{': undo.push_var(escape_char) = g_ascii_toupper(chr); return this; @@ -466,7 +466,7 @@ StateExpectString::custom(gchar chr) undo.push_str(strings[0]) = NULL; if (last) - undo.push_var(escape_char) = '\x1B'; + undo.push_var(escape_char) = CTL_KEY_ESC; nesting = 1; if (string_building) @@ -1024,7 +1024,7 @@ StateStart::custom(gchar chr) "interactive mode"); current_doc_undo_edit(); - QRegisters::globals.edit("\x1B"); + QRegisters::globals.edit(CTL_KEY_ESC_STR); interface.ssm(SCI_BEGINUNDOACTION); interface.ssm(SCI_CLEARALL); @@ -1041,7 +1041,7 @@ StateStart::custom(gchar chr) if (!undo.enabled) throw Error("Command-line editing only possible in " "interactive mode"); - if (QRegisters::current != QRegisters::globals["\x1B"]) + if (QRegisters::current != QRegisters::globals[CTL_KEY_ESC_STR]) throw Error("Command-line replacement only allowed when " "editing the replacement register"); diff --git a/src/sciteco.h b/src/sciteco.h index 53131dc..c4395c1 100644 --- a/src/sciteco.h +++ b/src/sciteco.h @@ -49,15 +49,28 @@ namespace Flags { extern sig_atomic_t sigint_occurred; -/* - * for sentinels: NULL might not be defined as a +/** + * For sentinels: NULL might not be defined as a * pointer type (LLVM/CLang) */ #define NIL ((void *)0) +/** true if C is a control character */ #define IS_CTL(C) ((C) < ' ') +/** ASCII character to echo control character C */ #define CTL_ECHO(C) ((C) | 0x40) +/** + * Control character of ASCII C, i.e. + * control character corresponding to CTRL+ keypress. + */ #define CTL_KEY(C) ((C) & ~0x40) +/** + * Control character of the escape key. + * Equivalent to CTL_KEY('[') + */ +#define CTL_KEY_ESC 27 +/** String containing the escape character */ +#define CTL_KEY_ESC_STR "\x1B" #define SUCCESS (-1) #define FAILURE (0) -- cgit v1.2.3