annotate editor.luan @ 28:bdb8754f1211

find work
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 11 Apr 2025 21:41:26 -0600
parents 32232df78246
children 01b8a25b38aa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
3
a79a840691b1 handle args
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
3 local ipairs = Luan.ipairs or error()
28
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
4 local range = Luan.range or error()
15
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
5 local stringify = Luan.stringify or error()
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
6 local String = require "luan:String.luan"
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
7 local sub_string = String.sub or error()
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
8 local replace = String.replace or error()
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
9 local starts_with = String.starts_with or error()
17
273aebbcf90b add goto
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
10 local to_number = String.to_number or error()
28
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
11 local find = String.find or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local Io = require "luan:Io.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local print = Io.print or error()
3
a79a840691b1 handle args
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
14 local new_file = Io.schemes.file or error()
7
ce4233ff5ce8 add revert
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
15 local Math = require "luan:Math.luan"
ce4233ff5ce8 add revert
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
16 local min = Math.min or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local Swing = require "luan:swing/Swing.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local new_frame = require("luan:swing/Frame.luan").new or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local new_label = require("luan:swing/Label.luan").new or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 local new_text_area = require("luan:swing/Text_area.luan").new or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 local new_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error()
18
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
22 local new_text_area_line_numbers = require("luan:swing/Text_area_line_numbers.luan").new or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local new_menu_bar = require("luan:swing/Menu_bar.luan").new or error()
21
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
24 local Menu = require "luan:swing/Menu.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
25 local new_menu = Menu.new or error()
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
26 local separator = Menu.separator or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 local new_menu_item = require("luan:swing/Menu_item.luan").new or error()
2
f38f6e903369 word wrap in menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
28 local new_check_box_menu_item = require("luan:swing/Check_box_menu_item.luan").new or error()
12
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
29 local int_to_color = require("luan:swing/Color.luan").int_to_color or error()
18
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
30 local Border = require "luan:swing/Border.luan"
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
31 local create_empty_border = Border.create_empty_border or error()
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
32 local create_line_border = Border.create_line_border or error()
26
Franklin Schmidt <fschmidt@gmail.com>
parents: 25
diff changeset
33 local no_border = Border.no_border or error()
18
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
34 local Layout = require "luan:swing/Layout.luan"
25
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
35 local new_mig_layout = Layout.new_mig_layout or error()
16
90abee9e07d5 add show_column
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
36 local Option_pane = require "luan:swing/Option_pane.luan"
90abee9e07d5 add show_column
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
37 local show_message_dialog = Option_pane.show_message_dialog or error()
17
273aebbcf90b add goto
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
38 local show_input_dialog = Option_pane.show_input_dialog or error()
18
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
39 local new_dialog = require("luan:swing/Dialog.luan").new or error()
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
40 local new_panel = require("luan:swing/Component.luan").new_panel or error()
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
41 local new_button = require("luan:swing/Button.luan").new or error()
25
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
42 local new_text_field = require("luan:swing/Text_field.luan").new or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 local Logging = require "luan:logging/Logging.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 local logger = Logging.logger "editor"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 local new_window
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48
20
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
49 local function action_listener(fn)
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
50 return function(_)
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
51 fn()
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
52 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
53 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
54
1
9aa38deb5883 add open
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
55 local function make_menu_bar(window)
21
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
56 local document = window.text_area.document
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
57 local revert = new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
58 text = "Revert"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
59 enabled = window.has_file
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
60 action_listener = action_listener(window.revert)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
61 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
62 local undo = new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
63 text = "Undo"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
64 accelerator = "meta Z"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
65 action_listener = action_listener(document.undo)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
66 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
67 local redo = new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
68 text = "Redo"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
69 accelerator = "meta shift Z"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
70 action_listener = action_listener(document.redo)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
71 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
72 local function update_undo_redo()
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
73 undo.set_enabled(document.can_undo())
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
74 redo.set_enabled(document.can_redo())
4
3d6414265959 add save
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
75 end
21
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
76 window.update_undo_redo = update_undo_redo -- dont gc
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
77 update_undo_redo()
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
78 document.add_undo_listener(update_undo_redo)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
80 return new_menu_bar{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
81 menus = {
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
82 new_menu{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
83 text = "File"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
84 menu_items = {
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
85 new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
86 text = "New File"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
87 accelerator = "meta N"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
88 action_listener = function(_)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
89 new_window()
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
90 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
91 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
92 new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
93 text = "Open..."
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
94 accelerator = "meta O"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
95 action_listener = action_listener(window.open)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
96 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
97 new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
98 text = "Save"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
99 accelerator = "meta S"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
100 action_listener = function(_)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
101 if window.save() then
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
102 revert.set_enabled(true)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
103 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
104 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
105 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
106 revert
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
107 }
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
108 }
21
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
109 new_menu{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
110 text = "Edit"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
111 menu_items = {
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
112 undo
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
113 redo
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
114 separator
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
115 new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
116 text = "Cut"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
117 accelerator = "meta X"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
118 action_listener = action_listener(window.text_area.cut)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
119 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
120 new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
121 text = "Copy"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
122 accelerator = "meta C"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
123 action_listener = action_listener(window.text_area.copy)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
124 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
125 new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
126 text = "Paste"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
127 accelerator = "meta V"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
128 action_listener = action_listener(window.text_area.paste)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
129 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
130 separator
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
131 new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
132 text = "Indent"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
133 accelerator = "meta CLOSE_BRACKET"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
134 action_listener = action_listener(window.indent)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
135 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
136 new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
137 text = "Unindent"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
138 accelerator = "meta OPEN_BRACKET"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
139 action_listener = action_listener(window.unindent)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
140 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
141 separator
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
142 new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
143 text = "Select All"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
144 accelerator = "meta A"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
145 action_listener = action_listener(window.text_area.select_all)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
146 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
147 separator
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
148 new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
149 text = "Find and Replace"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
150 accelerator = "meta F"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
151 action_listener = action_listener(window.show_find_dialog)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
152 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
153 }
20
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
154 }
21
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
155 new_menu{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
156 text = "View"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
157 menu_items = {
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
158 new_check_box_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
159 text = "Word Wrap"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
160 state = window.text_area.line_wrap
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
161 action_listener = function(event)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
162 window.text_area.line_wrap = event.source.state
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
163 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
164 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
165 new_check_box_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
166 text = "Show Whitespace"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
167 action_listener = function(event)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
168 window.text_area.show_whitespace(event.source.state)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
169 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
170 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
171 new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
172 text = "Show Cursor Column"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
173 action_listener = function(_)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
174 show_message_dialog( window.frame, "Cursor Column: "..window.cursor_column() )
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
175 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
176 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
177 new_menu_item{
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
178 text = "Goto Line"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
179 accelerator = "meta G"
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
180 action_listener = function(_)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
181 local input = show_input_dialog( window.frame, "Goto line" )
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
182 local line = input and to_number(input)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
183 if line ~= nil then
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
184 window.goto(line)
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
185 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
186 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
187 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
188 }
20
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
189 }
21
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
190 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
191 }
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
192 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193
28
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
194 local function get_matches(text,s)
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
195 local n = #s
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
196 if n == 0 then
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
197 return nil
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
198 end
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
199 local matches = {}
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
200 local i = 0
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
201 while(true) do
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
202 local j = find(text,s,i)
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
203 if j == nil then
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
204 break
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
205 end
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
206 matches[#matches+1] = { start=j, end_=j+n }
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
207 i = j + n
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
208 end
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
209 return matches
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
210 end
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
211
18
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
212 local function make_find_dialog(window)
28
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
213 local text_area = window.text_area
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
214 local find_field, output
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
215 local function find_match(event)
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
216 --logger.info("action "..event.action)
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
217 local s = find_field.text
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
218 local matches = get_matches( text_area.text, s )
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
219 if matches == nil then
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
220 output.text = ""
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
221 return
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
222 end
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
223 local n_matches = #matches
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
224 if n_matches == 0 then
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
225 output.text = "0 matches"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
226 return
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
227 end
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
228 local action = event.action
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
229 if action == "next" then
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
230 local _, pos = text_area.get_selection()
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
231 for i, match in ipairs(matches) do
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
232 if match.start >= pos then
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
233 text_area.set_selection( match.start, match.end_ )
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
234 output.text = i.." of "..n_matches.." matches"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
235 return
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
236 end
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
237 end
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
238 local match = matches[1]
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
239 text_area.set_selection( match.start, match.end_ )
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
240 output.text = "1 of "..n_matches.." matches; wrapped past end"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
241 elseif action == "previous" then
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
242 local pos = text_area.get_selection()
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
243 for i in range(n_matches,1,-1) do
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
244 local match = matches[i]
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
245 if match.end_ <= pos then
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
246 text_area.set_selection( match.start, match.end_ )
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
247 output.text = i.." of "..n_matches.." matches"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
248 return
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
249 end
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
250 end
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
251 local match = matches[n_matches]
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
252 text_area.set_selection( match.start, match.end_ )
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
253 output.text = n_matches.." of "..n_matches.." matches; wrapped past end"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
254 else
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
255 error(action)
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
256 end
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
257 end
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
258 find_field = new_text_field{
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
259 constraints = "wrap,growx"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
260 columns = 20
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
261 action = "next"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
262 action_listener = find_match
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
263 }
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
264 output = new_label{
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
265 constraints = "span,grow"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
266 text = "testing"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
267 }
23
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
268 local dialog = new_dialog{
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
269 owner_frame = window.frame
25
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
270 content_pane = new_panel{
28
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
271 layout = new_mig_layout("","[][grow]")
25
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
272 children = {
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
273 new_label{
28
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
274 constraints = "right"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
275 text = "Find:"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
276 }
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
277 find_field
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
278 new_label{
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
279 constraints = "right"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
280 text = "Replace:"
25
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
281 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
282 new_text_field{
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
283 constraints = "wrap,growx"
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
284 columns = 20
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
285 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
286 new_panel{
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
287 constraints = "span,wrap"
28
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
288 layout = new_mig_layout("insets 0")
25
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
289 children = {
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
290 new_button{
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
291 text = "Find Next"
28
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
292 action = "next"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
293 action_listener = find_match
25
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
294 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
295 new_button{
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
296 text = "Find Previous"
28
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
297 action = "previous"
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
298 action_listener = find_match
25
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
299 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
300 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
301 }
28
bdb8754f1211 find work
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
302 output
25
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
303 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
304 }
22
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
305 }
18
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
306 dialog.pack()
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
307 local was_shown = false
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
308 function window.show_find_dialog()
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
309 if not was_shown then
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
310 was_shown = true
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
311 dialog.move_into_owner()
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
312 end
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
313 dialog.visible = true
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
314 end
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
315 end
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
316
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
317 local n_windows = 0
5
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
318 local documents = {}
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
319
1
9aa38deb5883 add open
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
320 function new_window(file)
5
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
321 local window = {}
7
ce4233ff5ce8 add revert
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
322 window.has_file = file~=nil and file.is_file()
24
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
323 local text_area = new_text_area{
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
324 rows = 10
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
325 columns = 20
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
326 wrap_style_word = true
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
327 line_wrap = true
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
328 tab_size = 4
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
329 font = { family="Monospaced", size=13 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
330 border = create_empty_border(0,4,0,4)
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
331 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
332 window.text_area = text_area
5
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
333 local title = file and file.canonical().to_string() or "new"
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
334 if file ~= nil then
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
335 local document = documents[title]
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
336 if document == nil then
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
337 documents[title] = text_area.document
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
338 else
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
339 text_area.document = document
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
340 end
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
341 if file.is_file() then
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
342 text_area.text = file.read_text()
20
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
343 text_area.document.clear_unedited()
5
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
344 end
1
9aa38deb5883 add open
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
345 end
24
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
346 text_area.set_selection(0)
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
347 local frame = new_frame{
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
348 content_pane = new_scroll_pane{
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
349 view = text_area
26
Franklin Schmidt <fschmidt@gmail.com>
parents: 25
diff changeset
350 border = no_border
24
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
351 row_header_view = new_text_area_line_numbers{
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
352 text_area = text_area
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
353 foreground_color = int_to_color(0x888888)
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
354 border = create_empty_border(0,8,0,8)
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
355 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
356 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
357 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
358 window.frame = frame
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
359 frame.add_close_listener(function()
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
360 n_windows = n_windows - 1
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
361 if n_windows == 0 then
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
362 Luan.exit()
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
363 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
364 end)
10
a83c8842ca3b add edited
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
365 local function set_title()
a83c8842ca3b add edited
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
366 local s = title
a83c8842ca3b add edited
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
367 if not text_area.document.is_unedited() then
a83c8842ca3b add edited
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
368 s = s.." *"
a83c8842ca3b add edited
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
369 end
a83c8842ca3b add edited
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
370 frame.title = s
a83c8842ca3b add edited
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
371 end
a83c8842ca3b add edited
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
372 set_title()
24
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
373 window.set_title = set_title -- dont gc
10
a83c8842ca3b add edited
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
374 text_area.document.add_undo_listener(set_title)
5
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
375 function window.open()
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
376 local file_chooser = frame.file_chooser_load()
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
377 if file ~= nil then
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
378 file_chooser.directory = file.parent()
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
379 end
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
380 file_chooser.visible = true
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
381 local new_file = file_chooser.file
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
382 if new_file ~= nil then
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
383 new_window(new_file)
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
384 end
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
385 end
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
386 function window.save()
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
387 if file == nil then
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
388 local file_chooser = frame.file_chooser_save()
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
389 file_chooser.visible = true
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
390 file = file_chooser.file
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
391 if file == nil then
7
ce4233ff5ce8 add revert
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
392 return false
5
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
393 end
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
394 title = file.canonical().to_string()
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
395 frame.title = title
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
396 documents[title] = text_area.document
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
397 end
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
398 file.write_text(text_area.text)
10
a83c8842ca3b add edited
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
399 text_area.document.set_unedited()
7
ce4233ff5ce8 add revert
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
400 return true
ce4233ff5ce8 add revert
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
401 end
ce4233ff5ce8 add revert
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
402 function window.revert()
15
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
403 local selection = text_area.get_selection()
7
ce4233ff5ce8 add revert
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
404 local text = file.read_text()
ce4233ff5ce8 add revert
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
405 text_area.text = text
15
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
406 text_area.set_selection(min(selection,#text+1))
10
a83c8842ca3b add edited
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
407 text_area.document.set_unedited()
5
57b4c281a03e shared documents
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
408 end
15
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
409 local function selection_lines()
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
410 local start_seletion, end_selection = text_area.get_selection()
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
411 local end_ = end_selection == start_seletion and end_selection or end_selection - 1
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
412 local start_line = text_area.get_line_from_position(start_seletion)
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
413 local end_line = text_area.get_line_from_position(end_)
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
414 local start_pos = text_area.get_line_start_position(start_line)
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
415 local end_pos = text_area.get_line_end_position(end_line)
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
416 local text = text_area.text
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
417 text = sub_string(text,start_pos,end_pos-2)
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
418 return {
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
419 text = text
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
420 start_pos = start_pos
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
421 length = #text
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
422 lines = end_line - start_line + 1
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
423 start_seletion = start_seletion
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
424 end_selection = end_selection
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
425 }
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
426 end
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
427 function window.indent()
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
428 local r = selection_lines()
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
429 local text = r.text
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
430 local start_pos = r.start_pos
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
431 text = "\t"..replace(text,"\n","\n\t")
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
432 text_area.replace(start_pos,r.length,text)
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
433 --logger.info(stringify{text_area.get_selection()})
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
434 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines )
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
435 end
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
436 function window.unindent()
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
437 local r = selection_lines()
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
438 local text = r.text
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
439 text = "\n"..text
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
440 local start_seletion = r.start_seletion
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
441 if starts_with(text,"\n\t") then
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
442 start_seletion = start_seletion - 1
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
443 end
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
444 local len1 = #text
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
445 text = replace(text,"\n\t","\n")
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
446 local len2 = #text
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
447 local end_selection = r.end_selection - (len1 - len2)
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
448 text = sub_string(text,2)
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
449 text_area.replace(r.start_pos,r.length,text)
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
450 text_area.set_selection(start_seletion,end_selection)
93e46dadb694 add indent
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
451 end
16
90abee9e07d5 add show_column
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
452 function window.cursor_column()
90abee9e07d5 add show_column
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
453 local cursor_pos = text_area.get_selection()
90abee9e07d5 add show_column
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
454 local line = text_area.get_line_from_position(cursor_pos)
90abee9e07d5 add show_column
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
455 local start_line_pos = text_area.get_line_start_position(line)
90abee9e07d5 add show_column
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
456 return cursor_pos - start_line_pos + 1
90abee9e07d5 add show_column
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
457 end
17
273aebbcf90b add goto
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
458 function window.goto(line)
273aebbcf90b add goto
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
459 local pos = text_area.get_line_start_position(line)
273aebbcf90b add goto
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
460 text_area.set_selection(pos)
273aebbcf90b add goto
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
461 end
18
fd6d0162e924 start find
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
462 make_find_dialog(window)
1
9aa38deb5883 add open
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
463 local menu_bar = make_menu_bar(window)
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
464 frame.set_menu_bar(menu_bar)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
465 frame.pack()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
466 frame.visible = true
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
467 text_area.request_focus_in_window()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
468 n_windows = n_windows + 1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
469 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
470
27
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
471 --[[
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
472 for _, laf in ipairs(Swing.get_installed_look_and_feels()) do
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
473 logger.info(laf)
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
474 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
475 ]]
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
476 --logger.info(Swing.get_look_and_feel())
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
477 Swing.ui_manager_put("control",int_to_color(0xedeff2))
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
478 Swing.set_look_and_feel("javax.swing.plaf.nimbus.NimbusLookAndFeel")
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
479
1
9aa38deb5883 add open
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
480 Swing.run(function()
3
a79a840691b1 handle args
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
481 local args = Luan.arg
a79a840691b1 handle args
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
482 if #args == 0 then
a79a840691b1 handle args
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
483 new_window()
a79a840691b1 handle args
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
484 else
a79a840691b1 handle args
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
485 for _, arg in ipairs(args) do
a79a840691b1 handle args
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
486 local file = new_file(arg)
a79a840691b1 handle args
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
487 new_window(file)
a79a840691b1 handle args
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
488 end
a79a840691b1 handle args
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
489 end
1
9aa38deb5883 add open
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
490 end)