about summary refs log tree commit diff stats
path: root/source_edit.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-09-06 18:46:07 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-09-06 18:46:07 -0700
commit7e3dc2bc34e546a41252f3792161e2a09cfd2efe (patch)
tree93bbe22ddc72ae5af40ea9e548c292ba0df7aa93 /source_edit.lua
parented07f9825db5ec4899f6e15e7b2276078a9a92e7 (diff)
parentc112b8fadf86f908c71a63b88dcdb0d374c4eb6f (diff)
downloadtext.love-7e3dc2bc34e546a41252f3792161e2a09cfd2efe.tar.gz
Merge lines.love
Diffstat (limited to 'source_edit.lua')
-rw-r--r--source_edit.lua61
1 files changed, 58 insertions, 3 deletions
diff --git a/source_edit.lua b/source_edit.lua
index 6676b42..a65537d 100644
--- a/source_edit.lua
+++ b/source_edit.lua
@@ -76,6 +76,14 @@ function edit.initialize_state(top, left, right, font_height, line_height)  -- c
     cursor1 = {line=1, pos=1, posB=nil},  -- position of cursor
     screen_bottom1 = {line=1, pos=1, posB=nil},  -- position of start of screen line at bottom of screen
 
+    selection1 = {},
+    -- some extra state to compute selection between mouse press and release
+    old_cursor1 = nil,
+    old_selection1 = nil,
+    mousepress_shift = nil,
+    -- when selecting text, avoid recomputing some state on every single frame
+    recent_mouse = {},
+
     -- cursor coordinates in pixels
     cursor_x = 0,
     cursor_y = 0,
@@ -208,9 +216,22 @@ function edit.mouse_pressed(State, x,y, mouse_button)
   for line_index,line in ipairs(State.lines) do
     if line.mode == 'text' then
       if Text.in_line(State, line_index, x,y) then
+        -- delicate dance between cursor, selection and old cursor/selection
+        -- scenarios:
+        --  regular press+release: sets cursor, clears selection
+        --  shift press+release:
+        --    sets selection to old cursor if not set otherwise leaves it untouched
+        --    sets cursor
+        --  press and hold to start a selection: sets selection on press, cursor on release
+        --  press and hold, then press shift: ignore shift
+        --    i.e. mouse_released should never look at shift state
+        State.old_cursor1 = State.cursor1
+        State.old_selection1 = State.selection1
+        State.mousepress_shift = App.shift_down()
         local pos,posB = Text.to_pos_on_line(State, line_index, x, y)
   --?       print(x,y, 'setting cursor:', line_index, pos, posB)
-        State.cursor1 = {line=line_index, pos=pos, posB=posB}
+        State.selection1 = {line=line_index, pos=pos, posB=posB}
+--?         print('selection', State.selection1.line, State.selection1.pos, State.selection1.posB)
         break
       end
     elseif line.mode == 'drawing' then
@@ -236,6 +257,30 @@ function edit.mouse_released(State, x,y, mouse_button)
       record_undo_event(State, {before=Drawing.before, after=snapshot(State, State.lines.current_drawing_index)})
       Drawing.before = nil
     end
+  else
+    for line_index,line in ipairs(State.lines) do
+      if line.mode == 'text' then
+        if Text.in_line(State, line_index, x,y) then
+--?           print('reset selection')
+          local pos,posB = Text.to_pos_on_line(State, line_index, x, y)
+          State.cursor1 = {line=line_index, pos=pos, posB=posB}
+--?           print('cursor', State.cursor1.line, State.cursor1.pos, State.cursor1.posB)
+          if State.mousepress_shift then
+            if State.old_selection1.line == nil then
+              State.selection1 = State.old_cursor1
+            else
+              State.selection1 = State.old_selection1
+            end
+          end
+          State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
+          if eq(State.cursor1, State.selection1) then
+            State.selection1 = {}
+          end
+          break
+        end
+      end
+    end
+--?     print('selection:', State.selection1.line, State.selection1.pos)
   end
 end
 
@@ -258,6 +303,14 @@ function edit.textinput(State, t)
 end
 
 function edit.keychord_pressed(State, chord, key)
+  if State.selection1.line and
+      not State.lines.current_drawing and
+      -- printable character created using shift key => delete selection
+      -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
+      (not App.shift_down() or utf8.len(key) == 1) and
+      chord ~= 'C-c' and chord ~= 'C-x' and chord ~= 'backspace' and backspace ~= 'delete' and not App.is_cursor_movement(chord) then
+    Text.delete_selection(State, State.left, State.right)
+  end
   if State.search_term then
     if chord == 'escape' then
       State.search_term = nil
@@ -294,7 +347,7 @@ function edit.keychord_pressed(State, chord, key)
     }
     assert(State.search_text == nil)
   -- bifold text
-  elseif chord == 'C-b' then
+  elseif chord == 'M-b' then
     State.expanded = not State.expanded
     Text.redraw_all(State)
     if not State.expanded then
@@ -303,7 +356,7 @@ function edit.keychord_pressed(State, chord, key)
       end
       edit.eradicate_locations_after_the_fold(State)
     end
-  elseif chord == 'C-i' then
+  elseif chord == 'M-d' then
     if State.cursor1.posB == nil then
       local before = snapshot(State, State.cursor1.line)
       if State.lines[State.cursor1.line].dataB == nil then
@@ -336,6 +389,7 @@ function edit.keychord_pressed(State, chord, key)
       local src = event.before
       State.screen_top1 = deepcopy(src.screen_top)
       State.cursor1 = deepcopy(src.cursor)
+      State.selection1 = deepcopy(src.selection)
       patch(State.lines, event.after, event.before)
       patch_placeholders(State.line_cache, event.after, event.before)
       -- invalidate various cached bits of lines
@@ -351,6 +405,7 @@ function edit.keychord_pressed(State, chord, key)
       local src = event.after
       State.screen_top1 = deepcopy(src.screen_top)
       State.cursor1 = deepcopy(src.cursor)
+      State.selection1 = deepcopy(src.selection)
       patch(State.lines, event.before, event.after)
       -- invalidate various cached bits of lines
       State.lines.current_drawing = nil
arbe <arg@suckless.org> 2007-02-16 16:51:27 +0100 removed useless space' href='/acidbong/suckless/dwm/commit/event.c?h=3.8&id=c3527bea5746ac23599408a32f500381475815c4'>c3527be ^
77f8c07 ^
dc5d967 ^
c09bf8d ^
da909dd ^
c09bf8d ^

b233089 ^
c09bf8d ^
67bc08d ^


b233089 ^
c09bf8d ^
b233089 ^

c09bf8d ^

e6cbe9c ^
6e22ccf ^










c09bf8d ^
c09bf8d ^




ca65478 ^
c09bf8d ^
95766d6 ^
dc5d967 ^
c09bf8d ^
1173723 ^

77f8c07 ^
6651dd7 ^
c09bf8d ^
da909dd ^
b233089 ^
c09bf8d ^
b233089 ^
c09bf8d ^
67bc08d ^
7055315 ^

67bc08d ^
dc9f62f ^
67bc08d ^
b233089 ^
c09bf8d ^
b233089 ^

c09bf8d ^

e6cbe9c ^
6e22ccf ^




c09bf8d ^
c09bf8d ^


29355bd ^

ca65478 ^
29355bd ^

b9da4b0 ^
dc5d967 ^
b9da4b0 ^
29355bd ^
e995c1b ^



6d22782 ^
1836b67 ^



6d22782 ^

1836b67 ^



05fbbbd ^
e995c1b ^
29355bd ^

cdbc84b ^
6538265 ^

879241c ^

6538265 ^









29355bd ^
6458d72 ^
7b5638f ^
d800ec0 ^
2272df9 ^
5a03daf ^
6b25d06 ^
6d22782 ^


9fce821 ^
e256afe ^
5a03daf ^
7a095d0 ^
6b25d06 ^
6d22782 ^
b9da4b0 ^




ca65478 ^
dc5d967 ^
439e15d ^

439e15d ^
b9da4b0 ^
7d7cde0 ^
64cfebc ^

5a03daf ^
b61f913 ^







e256afe ^

7a095d0 ^
39ed54a ^
b61f913 ^
14d05e7 ^
7a095d0 ^

95e8d12 ^
22d8c6f ^









71857b8 ^
439e15d ^


ca65478 ^
439e15d ^


26e134b ^

439e15d ^


ca65478 ^
439e15d ^
dc5d967 ^
439e15d ^
b79b5fa ^
439e15d ^
6c5dc70 ^
3399650 ^
fde45eb ^
87836d7 ^
b233089 ^

fde45eb ^
439e15d ^


ca65478 ^
dc5d967 ^
439e15d ^

59b4a5e ^
c0705ee ^
439e15d ^



ca65478 ^
61a1910 ^
19dcbc5 ^
adaa28a ^
dc5d967 ^
adaa28a ^

e256afe ^
ca65478 ^
e256afe ^
2ffdc19 ^
19dcbc5 ^

adaa28a ^



ca65478 ^
adaa28a ^

96e1b25 ^
87836d7 ^
373b11d ^
96e1b25 ^
adaa28a ^


ca65478 ^
0f3acce ^







ca65478 ^
439e15d ^
dc5d967 ^
439e15d ^


35e65ea ^
439e15d ^
0053620 ^

439e15d ^


ca65478 ^
439e15d ^
dc5d967 ^

439e15d ^


3399650 ^



66da153 ^
5a03daf ^
cdbc84b ^
3399650 ^

ebd17e4 ^
3399650 ^

c09bf8d ^
d2d394e ^
f8181f6 ^

3399650 ^

439e15d ^


ca65478 ^
439e15d ^


0053620 ^

439e15d ^
adaa28a ^
bf35794 ^
adaa28a ^








0f3acce ^
adaa28a ^





6b25d06 ^
61a1910 ^
adaa28a ^


0f3acce ^
adaa28a ^

adaa28a ^

3af6434 ^

ee31e38 ^
a73a882 ^
ee31e38 ^
a73a882 ^
adaa28a ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371