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