Andrew M. Kuchling | e0d0090 | 2000-07-11 10:38:24 +0000 | [diff] [blame] | 1 | """Simple textbox editing widget with Emacs-like keybindings.""" |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 2 | |
Martin v. Löwis | 9d51fc8 | 2002-02-23 22:31:53 +0000 | [diff] [blame] | 3 | import curses, ascii |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 4 | |
| 5 | def rectangle(win, uly, ulx, lry, lrx): |
| 6 | "Draw a rectangle." |
| 7 | win.vline(uly+1, ulx, curses.ACS_VLINE, lry - uly - 1) |
| 8 | win.hline(uly, ulx+1, curses.ACS_HLINE, lrx - ulx - 1) |
| 9 | win.hline(lry, ulx+1, curses.ACS_HLINE, lrx - ulx - 1) |
| 10 | win.vline(uly+1, lrx, curses.ACS_VLINE, lry - uly - 1) |
| 11 | win.addch(uly, ulx, curses.ACS_ULCORNER) |
| 12 | win.addch(uly, lrx, curses.ACS_URCORNER) |
| 13 | win.addch(lry, lrx, curses.ACS_LRCORNER) |
| 14 | win.addch(lry, ulx, curses.ACS_LLCORNER) |
| 15 | |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 16 | class Textbox: |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 17 | """Editing widget using the interior of a window object. |
| 18 | Supports the following Emacs-like key bindings: |
| 19 | |
| 20 | Ctrl-A Go to left edge of window. |
| 21 | Ctrl-B Cursor left, wrapping to previous line if appropriate. |
| 22 | Ctrl-D Delete character under cursor. |
Eric S. Raymond | 5af256d | 2000-08-04 07:33:18 +0000 | [diff] [blame] | 23 | Ctrl-E Go to right edge (stripspaces off) or end of line (stripspaces on). |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 24 | Ctrl-F Cursor right, wrapping to next line when appropriate. |
| 25 | Ctrl-G Terminate, returning the window contents. |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 26 | Ctrl-H Delete character backward. |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 27 | Ctrl-J Terminate if the window is 1 line, otherwise insert newline. |
| 28 | Ctrl-K If line is blank, delete it, otherwise clear to end of line. |
Eric S. Raymond | 5af256d | 2000-08-04 07:33:18 +0000 | [diff] [blame] | 29 | Ctrl-L Refresh screen. |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 30 | Ctrl-N Cursor down; move down one line. |
| 31 | Ctrl-O Insert a blank line at cursor location. |
| 32 | Ctrl-P Cursor up; move up one line. |
| 33 | |
| 34 | Move operations do nothing if the cursor is at an edge where the movement |
| 35 | is not possible. The following synonyms are supported where possible: |
| 36 | |
| 37 | KEY_LEFT = Ctrl-B, KEY_RIGHT = Ctrl-F, KEY_UP = Ctrl-P, KEY_DOWN = Ctrl-N |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 38 | KEY_BACKSPACE = Ctrl-h |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 39 | """ |
| 40 | def __init__(self, win): |
| 41 | self.win = win |
| 42 | (self.maxy, self.maxx) = win.getmaxyx() |
| 43 | self.maxy = self.maxy - 1 |
| 44 | self.maxx = self.maxx - 1 |
| 45 | self.stripspaces = 1 |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 46 | self.lastcmd = None |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 47 | win.keypad(1) |
| 48 | |
Eric S. Raymond | 5af256d | 2000-08-04 07:33:18 +0000 | [diff] [blame] | 49 | def _end_of_line(self, y): |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 50 | "Go to the location of the first blank on the given line." |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 51 | last = self.maxx |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 52 | while 1: |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 53 | if ascii.ascii(self.win.inch(y, last)) != ascii.SP: |
| 54 | last = last + 1 |
| 55 | break |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 56 | elif last == 0: |
| 57 | break |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 58 | last = last - 1 |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 59 | return last |
| 60 | |
| 61 | def do_command(self, ch): |
| 62 | "Process a single editing command." |
| 63 | (y, x) = self.win.getyx() |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 64 | self.lastcmd = ch |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 65 | if ascii.isprint(ch): |
| 66 | if y < self.maxy or x < self.maxx: |
| 67 | # The try-catch ignores the error we trigger from some curses |
| 68 | # versions by trying to write into the lowest-rightmost spot |
Andrew M. Kuchling | fee3126 | 2001-08-13 13:47:23 +0000 | [diff] [blame] | 69 | # in the window. |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 70 | try: |
| 71 | self.win.addch(ch) |
Andrew M. Kuchling | fee3126 | 2001-08-13 13:47:23 +0000 | [diff] [blame] | 72 | except curses.error: |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 73 | pass |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 74 | elif ch == ascii.SOH: # ^a |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 75 | self.win.move(y, 0) |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 76 | elif ch in (ascii.STX,curses.KEY_LEFT, ascii.BS,curses.KEY_BACKSPACE): |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 77 | if x > 0: |
| 78 | self.win.move(y, x-1) |
| 79 | elif y == 0: |
| 80 | pass |
| 81 | elif self.stripspaces: |
Eric S. Raymond | 5af256d | 2000-08-04 07:33:18 +0000 | [diff] [blame] | 82 | self.win.move(y-1, self._end_of_line(y-1)) |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 83 | else: |
| 84 | self.win.move(y-1, self.maxx) |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 85 | if ch in (ascii.BS, curses.KEY_BACKSPACE): |
| 86 | self.win.delch() |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 87 | elif ch == ascii.EOT: # ^d |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 88 | self.win.delch() |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 89 | elif ch == ascii.ENQ: # ^e |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 90 | if self.stripspaces: |
Eric S. Raymond | 5af256d | 2000-08-04 07:33:18 +0000 | [diff] [blame] | 91 | self.win.move(y, self._end_of_line(y)) |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 92 | else: |
| 93 | self.win.move(y, self.maxx) |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 94 | elif ch in (ascii.ACK, curses.KEY_RIGHT): # ^f |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 95 | if x < self.maxx: |
| 96 | self.win.move(y, x+1) |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 97 | elif y == self.maxy: |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 98 | pass |
| 99 | else: |
| 100 | self.win.move(y+1, 0) |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 101 | elif ch == ascii.BEL: # ^g |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 102 | return 0 |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 103 | elif ch == ascii.NL: # ^j |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 104 | if self.maxy == 0: |
| 105 | return 0 |
| 106 | elif y < self.maxy: |
| 107 | self.win.move(y+1, 0) |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 108 | elif ch == ascii.VT: # ^k |
Eric S. Raymond | 5af256d | 2000-08-04 07:33:18 +0000 | [diff] [blame] | 109 | if x == 0 and self._end_of_line(y) == 0: |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 110 | self.win.deleteln() |
| 111 | else: |
| 112 | self.win.clrtoeol() |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 113 | elif ch == ascii.FF: # ^l |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 114 | self.win.refresh() |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 115 | elif ch in (ascii.SO, curses.KEY_DOWN): # ^n |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 116 | if y < self.maxy: |
| 117 | self.win.move(y+1, x) |
Eric S. Raymond | 5af256d | 2000-08-04 07:33:18 +0000 | [diff] [blame] | 118 | if x > self._end_of_line(y+1): |
| 119 | self.win.move(y+1, self._end_of_line(y+1)) |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 120 | elif ch == ascii.SI: # ^o |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 121 | self.win.insertln() |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 122 | elif ch in (ascii.DLE, curses.KEY_UP): # ^p |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 123 | if y > 0: |
| 124 | self.win.move(y-1, x) |
Eric S. Raymond | 5af256d | 2000-08-04 07:33:18 +0000 | [diff] [blame] | 125 | if x > self._end_of_line(y-1): |
| 126 | self.win.move(y-1, self._end_of_line(y-1)) |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 127 | return 1 |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 128 | |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 129 | def gather(self): |
| 130 | "Collect and return the contents of the window." |
| 131 | result = "" |
| 132 | for y in range(self.maxy+1): |
| 133 | self.win.move(y, 0) |
Eric S. Raymond | 5af256d | 2000-08-04 07:33:18 +0000 | [diff] [blame] | 134 | stop = self._end_of_line(y) |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 135 | if stop == 0 and self.stripspaces: |
| 136 | continue |
| 137 | for x in range(self.maxx+1): |
| 138 | if self.stripspaces and x == stop: |
| 139 | break |
| 140 | result = result + chr(ascii.ascii(self.win.inch(y, x))) |
| 141 | if self.maxy > 0: |
| 142 | result = result + "\n" |
| 143 | return result |
| 144 | |
| 145 | def edit(self, validate=None): |
| 146 | "Edit in the widget window and collect the results." |
| 147 | while 1: |
| 148 | ch = self.win.getch() |
| 149 | if validate: |
| 150 | ch = validate(ch) |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 151 | if not ch: |
| 152 | continue |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 153 | if not self.do_command(ch): |
| 154 | break |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 155 | self.win.refresh() |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 156 | return self.gather() |
| 157 | |
| 158 | if __name__ == '__main__': |
| 159 | def test_editbox(stdscr): |
| 160 | win = curses.newwin(4, 9, 15, 20) |
| 161 | rectangle(stdscr, 14, 19, 19, 29) |
| 162 | stdscr.refresh() |
Andrew M. Kuchling | e8d7dbf | 2000-06-27 00:53:12 +0000 | [diff] [blame] | 163 | return Textbox(win).edit() |
Andrew M. Kuchling | 2b9d0bc | 2000-06-26 23:55:42 +0000 | [diff] [blame] | 164 | |
| 165 | str = curses.wrapper(test_editbox) |
| 166 | print str |