From 6460abf7ee7a3e5092f97dcf4d85a2c013a3f251 Mon Sep 17 00:00:00 2001 From: Claude Heiland-Allen Date: Fri, 21 Feb 2020 07:24:34 +0000 Subject: [PATCH 1/4] editor keys Ctrl-Up and Ctrl-Down (previous/next blank line) --- src/interface/interface.py | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/interface/interface.py b/src/interface/interface.py index 072e7f1..418cc57 100644 --- a/src/interface/interface.py +++ b/src/interface/interface.py @@ -222,6 +222,8 @@ def __init__(self, client, title, language, logging=False): self.text.bind("<{}-Right>".format(CtrlKey), self.key_ctrl_right) self.text.bind("<{}-Left>".format(CtrlKey), self.key_ctrl_left) + self.text.bind("<{}-Up>".format(CtrlKey), self.key_ctrl_up) + self.text.bind("<{}-Down>".format(CtrlKey), self.key_ctrl_down) self.text.bind("<{}-Home>".format(CtrlKey), self.key_ctrl_home) self.text.bind("<{}-End>".format(CtrlKey), self.key_ctrl_end) self.text.bind("<{}-period>".format(CtrlKey), self.stop_sound) @@ -827,6 +829,14 @@ def key_ctrl_right(self, event): """ Called when the user pressed Ctrl+Left. Sets the local peer index to right of the next word """ return self.key_direction(self.move_marker_ctrl_right) + def key_ctrl_up(self, event): + """ Called when the user pressed Ctrl+Up. Sets the local peer index to previous blank line """ + return self.key_direction(self.move_marker_ctrl_up) + + def key_ctrl_down(self, event): + """ Called when the user pressed Ctrl+Down. Sets the local peer index to next blank line """ + return self.key_direction(self.move_marker_ctrl_down) + # Deleting multiple characters def key_ctrl_backspace(self, event): @@ -947,6 +957,18 @@ def move_marker_ctrl_right(self): self.text.marker.move(index) return + def move_marker_ctrl_up(self): + """ Moves the cursor to the previous blank line """ + index = self.get_blank_line_up_index(self.text.marker.get_index_num()) + self.text.marker.move(index) + return + + def move_marker_ctrl_down(self): + """ Moves the cursor to the next blank line """ + index = self.get_blank_line_down_index(self.text.marker.get_index_num()) + self.text.marker.move(index) + return + def get_word_left_index(self, index): """ Returns the index of the start of the current word """ text = self.text.read() @@ -976,6 +998,28 @@ def get_word_right_index(self, index): i = len(text) return i + def get_blank_line_up_index(self, index): + """ Returns the index of the start of the previous blank line """ + text = self.text.read() + for i in range(index - 1, 0, -1): + if text[i - 1] == '\n' and text[i] == '\n': + break + else: + i = 0 + return i + + def get_blank_line_down_index(self, index): + """ Returns the index of the start of the next blank line """ + text = self.text.read() + if index < len(text) and text[index] == '\n': + index += 1 + for i in range(index, len(text) - 1): + if text[i - 1] == '\n' and text[i] in '\n': + break + else: + i = len(text) + return i + # Selection handling # ================== From 423a4e26a9a13fd7fe1ef109ece00167b4eb00a3 Mon Sep 17 00:00:00 2001 From: Claude Heiland-Allen Date: Fri, 21 Feb 2020 08:03:16 +0000 Subject: [PATCH 2/4] editor keys Page Up and Page Down (previous/next screenful) --- src/interface/interface.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/interface/interface.py b/src/interface/interface.py index 418cc57..59471f1 100644 --- a/src/interface/interface.py +++ b/src/interface/interface.py @@ -283,7 +283,7 @@ def __init__(self, client, title, language, logging=False): # Directional commands - self.directions = ("Left", "Right", "Up", "Down", "Home", "End") + self.directions = ("Left", "Right", "Up", "Down", "Home", "End", "Next", "Prior") self.handle_direction = {} self.handle_direction["Left"] = self.key_left @@ -292,6 +292,8 @@ def __init__(self, client, title, language, logging=False): self.handle_direction["Up"] = self.key_up self.handle_direction["Home"] = self.key_home self.handle_direction["End"] = self.key_end + self.handle_direction["Next"] = self.key_page_down + self.handle_direction["Prior"] = self.key_page_up self.block_messages = False # flag to stop sending messages @@ -813,6 +815,16 @@ def key_end(self): with the new location """ return self.key_direction(self.move_marker_end) + def key_page_down(self): + """ Called when the Page Down key is pressed. Increases local peer index + to correspond to a screenful of text """ + return self.key_direction(self.move_marker_page_down) + + def key_page_up(self): + """ Called when the Page Up key is pressed. Decreases local peer index + to correspond to a screenful of text """ + return self.key_direction(self.move_marker_page_up) + def key_ctrl_home(self, event): """ Called when the user pressed Ctrl+Home. Sets the local peer index to 0 """ return self.key_direction(self.move_marker_ctrl_home) @@ -934,6 +946,20 @@ def move_marker_end(self): self.text.marker.move(index) return + def move_marker_page_up(self): + """ Moves the cursor up one page """ + lines_per_page = self.get_lines_per_page() + for i in range(lines_per_page): + self.move_marker_up() + return + + def move_marker_page_down(self): + """ Moves the cursor down one page """ + lines_per_page = self.get_lines_per_page() + for i in range(lines_per_page): + self.move_marker_down() + return + def move_marker_ctrl_home(self): """ Moves the cursor the beginning of the document """ self.text.marker.move(0) @@ -1203,6 +1229,10 @@ def increase_font_size(self, event=None): self.change_font_size(+1) return 'break' + def get_lines_per_page(self): + """ Returns the number of lines visible in the window """ + return int(self.text.winfo_height() / self.text.char_h) + # Mouse Clicks # ============ From f9c4103737fc4e7400555461f038a4f3827cc380 Mon Sep 17 00:00:00 2001 From: Claude Heiland-Allen Date: Fri, 21 Feb 2020 08:37:13 +0000 Subject: [PATCH 3/4] allow decreasing font size without an event (it is unused) --- src/interface/interface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interface/interface.py b/src/interface/interface.py index 59471f1..75b17a4 100644 --- a/src/interface/interface.py +++ b/src/interface/interface.py @@ -1219,7 +1219,7 @@ def change_font_size(self, amount): return - def decrease_font_size(self, event): + def decrease_font_size(self, event=None): """ Calls `self.ChangeFontSize(-1)` and then resizes the line numbers bar accordingly """ self.change_font_size(-1) return 'break' From 95ecb508f62a513df7595591e0e448979f38f82b Mon Sep 17 00:00:00 2001 From: Claude Heiland-Allen Date: Fri, 21 Feb 2020 08:42:14 +0000 Subject: [PATCH 4/4] editor Ctrl-MouseWheel adjusts font size - tested on Linux (Debian Buster, Xorg) - not tested on Windows or Mac yet (I have no such machines); I followed the platform differences listed here: --- src/interface/interface.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/interface/interface.py b/src/interface/interface.py index 75b17a4..89f4324 100644 --- a/src/interface/interface.py +++ b/src/interface/interface.py @@ -265,6 +265,11 @@ def __init__(self, client, title, language, logging=False): self.text.bind("", self.mouse_left_double_click) self.text.bind("" if SYSTEM==MAC_OS else "", self.mouse_press_right) self.text.bind("" if SYSTEM!=MAC_OS else "", lambda *e: "break") # disable middle button + if SYSTEM == WINDOWS or SYSTEM == MAC_OS: + self.text.bind("<{}-MouseWheel>".format(CtrlKey), self.mouse_wheel) + else: + self.text.bind("<{}-Button-4>".format(CtrlKey), self.mouse_wheel_up) + self.text.bind("<{}-Button-5>".format(CtrlKey), self.mouse_wheel_down) # select_background self.text.tag_configure(SEL, background=COLOURS["Background"]) # Temporary fix - set normal highlighting to background colour @@ -1301,6 +1306,24 @@ def mouse_press_right(self, event): self.popup.show(event) return "break" + def mouse_wheel(self, event): + """ Changes font size; called by Tk on Windows and Mac """ + delta = event.delta + if SYSTEM == WINDOWS: + delta = int(delta / 120) + if (delta > 0): + return self.mouse_wheel_up() + else: + return self.mouse_wheel_down() + + def mouse_wheel_up(self, event=None): + """ Increase font size; called by Tk on X11 """ + return self.increase_font_size() + + def mouse_wheel_down(self, event=None): + """ Decrease font size; called by Tk on X11 """ + return self.decrease_font_size() + # Copy, paste, undo etc # =====================