2 from rox
import g
, TRUE
, FALSE
3 from EditWindow
import Minibuffer
7 class Process(Minibuffer
):
8 "A minibuffer used to process using python expressions."
10 def setup(self
, window
):
12 self
.buffer = window
.buffer
13 self
.window
.set_mini_label('Process:')
15 window
.mini_entry
.set_text(last
)
17 info
= 'Enter a python expression and press Return to process all selected ' \
18 'lines (or the current line if nothing is selected). Press Escape to ' \
19 'close the minibuffer.\n\n' \
20 'line is the current text on the line\n' \
21 'x is the numerical value of the line, or None if it\'s not a number\n' \
22 'n is the number of the line within the selection (1, 2, ...)\n' \
24 'line.upper() converts to upper case\n' \
25 '"%d) %s" % (n, line) numbers the lines\n' \
26 'x + 1 increases the value of each line by 1'
29 command
= self
.window
.mini_entry
.get_text()
34 start
, end
= self
.window
.get_selection_range()
35 if start
.compare(end
) == 0:
36 start
.set_line_offset(0)
38 end
.forward_to_line_end()
40 if start
.compare(end
) > 0:
41 start
, end
= end
, start
43 line_start
= start
.copy()
47 while line_start
.compare(end
) <= 0:
48 line_end
= line_start
.copy()
49 line_end
.forward_to_line_end()
50 if line_end
.compare(end
) >= 0:
52 line
= buffer.get_text(line_start
, line_end
, FALSE
)
56 locals['x'] = long(line
)
58 locals['x'] = float(line
)
63 new
= str(eval(command
, locals))
65 rox
.report_exception()
67 replacements
.append(new
)
68 if not line_start
.forward_line():
70 buffer.delete(start
, end
)
71 buffer.insert_at_cursor('\n'.join(replacements
))
73 self
.window
.set_minibuffer(None)