Skip to content

Commit c5b5e8d

Browse files
author
Rob Pilling
committed
Large motion restructure - details below
s/[a-z]+motion/motion_\1/ generalised getmotion() && applymotion() code added !q - like vim's gq
1 parent 7a5705a commit c5b5e8d

File tree

13 files changed

+238
-160
lines changed

13 files changed

+238
-160
lines changed

Makefile

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include config.mk
33
OBJ = main.o buffer.o buffers.o range.o command.o vars.o \
44
util/list.o util/alloc.o util/io.o util/pipe.o util/str.o util/term.o util/search.o \
55
gui/gui.o gui/motion.o gui/marks.o gui/base.o gui/intellisense.o \
6-
gui/map.o gui/macro.o gui/visual.o gui/syntax.o \
6+
gui/map.o gui/macro.o gui/visual.o gui/syntax.o gui/extra.o \
77
global.o rc.o preserve.o yank.o
88

99

@@ -60,12 +60,13 @@ gui/base.o: gui/base.c gui/../range.h gui/../buffer.h gui/../command.h \
6060
gui/../util/list.h gui/../global.h gui/motion.h gui/../util/alloc.h \
6161
gui/intellisense.h gui/gui.h gui/macro.h gui/marks.h gui/../main.h \
6262
gui/../util/str.h gui/../yank.h gui/map.h gui/../buffers.h gui/visual.h \
63-
gui/../util/search.h
63+
gui/../util/search.h gui/extra.h
64+
gui/extra.o: gui/extra.c gui/extra.h
6465
gui/gui.o: gui/gui.c gui/../range.h gui/../util/list.h gui/../buffer.h \
6566
gui/motion.h gui/intellisense.h gui/gui.h gui/../global.h \
6667
gui/../util/alloc.h gui/../util/str.h gui/../util/term.h \
6768
gui/../util/io.h gui/macro.h gui/marks.h gui/../buffers.h gui/visual.h \
68-
gui/../yank.h gui/../util/search.h
69+
gui/../yank.h gui/../util/search.h gui/syntax.h
6970
gui/intellisense.o: gui/intellisense.c gui/intellisense.h gui/../range.h \
7071
gui/../buffer.h gui/../global.h gui/../util/str.h gui/../util/list.h \
7172
gui/../util/alloc.h gui/motion.h gui/gui.h gui/../buffers.h
@@ -75,5 +76,6 @@ gui/map.o: gui/map.c gui/../range.h gui/../buffer.h gui/gui.h gui/map.h \
7576
gui/marks.o: gui/marks.c gui/marks.h gui/gui.h
7677
gui/motion.o: gui/motion.c gui/../range.h gui/../util/list.h gui/../buffer.h \
7778
gui/motion.h gui/intellisense.h gui/gui.h gui/marks.h gui/../global.h \
78-
gui/../util/str.h gui/../buffers.h
79+
gui/../util/str.h gui/../buffers.h gui/visual.h
80+
gui/syntax.o: gui/syntax.c gui/syntax.h
7981
gui/visual.o: gui/visual.c gui/../range.h gui/gui.h gui/visual.h

TODO

+5
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,8 @@ fix :e abc\t cursor positioning
7575
ihe\eAll\t should complete to hello
7676

7777
'et' 'trim'
78+
79+
buffer lines = struct {
80+
struct list *raw;
81+
struct list *vis; /* visual display, i.e. ctrl chars -> "^x", tabs -> " " */
82+
}

command.c

+3-37
Original file line numberDiff line numberDiff line change
@@ -277,55 +277,21 @@ void cmd_bang(int argc, char **argv, int force, struct range *rng)
277277
{
278278
if(rng->start != -1){
279279
/* rw!cmd command */
280-
struct list *l, *to_pipe;
281280
char *free_me = argv_to_str(argc, argv);
282281
char *cmd = strchr(free_me, '!') + 1;
283282

284283
if(argc <= 1){
285-
gui_status(GUI_ERR, "usage: range!cmd");
284+
gui_status(GUI_ERR, "usage: [range]!cmd");
286285
return;
287286
}
288287

289288
if(--rng->start < 0) rng->start = 0;
290289
if(--rng->end < 0) rng->end = 0;
291290

292-
to_pipe = buffer_extract_range(buffers_current(), rng);
293-
294-
l = pipe_readwrite(cmd, to_pipe ? to_pipe : buffer_gethead(buffers_current()));
295-
296-
if(l){
297-
if(l->data){
298-
if(to_pipe){
299-
struct list *inshere;
300-
301-
inshere = buffer_getindex(buffers_current(), rng->start);
302-
303-
if(inshere)
304-
buffer_insertlistbefore(buffers_current(), inshere, l);
305-
else
306-
buffer_appendlist(buffers_current(), l);
307-
308-
list_free_nodata(to_pipe);
309-
to_pipe = NULL;
310-
}else{
311-
buffer_replace(buffers_current(), l);
312-
}
313-
314-
buffer_modified(buffers_current()) = 1;
315-
}else{
316-
/* FIXME? assign to_pipe to "reg */
317-
list_free(l, free);
318-
gui_status(GUI_ERR, "%s: no output", cmd);
319-
}
320-
}else{
321-
gui_status(GUI_ERR, "pipe_readwrite() error: %s", strerror(errno));
322-
}
323-
324-
buffer_modified(buffers_current()) = 1;
291+
if(!range_through_pipe(rng, cmd))
292+
buffer_modified(buffers_current()) = 1;
325293

326294
free(free_me);
327-
if(to_pipe)
328-
list_free(to_pipe, free);
329295
}else{
330296
if(argc == 1){
331297
const char *shell = getenv("SHELL");

gui/base.c

+17-101
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "../buffers.h"
2828
#include "visual.h"
2929
#include "../util/search.h"
30+
#include "extra.h"
3031

3132
#define REPEAT_FUNC(nam) static void nam(unsigned int)
3233

@@ -37,13 +38,11 @@ static void insert(int append, int indent);
3738
static void put(unsigned int ntimes, int rev);
3839
static void change(struct motion *motion, int flag);
3940
REPEAT_FUNC(join);
40-
REPEAT_FUNC(tilde);
4141
REPEAT_FUNC(replace);
4242

4343
/* extra */
4444
static void colon(const char *);
4545
static int search(int, int);
46-
REPEAT_FUNC(showgirl);
4746

4847
static int is_edit_char(int);
4948

@@ -170,108 +169,21 @@ fin:;
170169
void shift(int repeat)
171170
{
172171
struct list *l;
173-
struct motion m;
174-
struct bufferpos topos;
175-
struct screeninfo si;
176-
int to_x, to_y;
177-
int cur;
178-
179-
memset(&m, 0, sizeof m);
172+
int x1, y1, x2, y2;
180173

181-
cur = to_y = gui_y();
182-
to_x = gui_x();
183-
184-
topos.x = &to_x;
185-
topos.y = &to_y;
186-
187-
si.top = gui_top();
188-
si.height = gui_max_y();
189-
190-
191-
if(getmotion(&m, 1, 0, "><", MOTION_FORWARD_LETTER) || applymotion(&m, &topos, &si))
174+
if(motion_wrap(&x1, &y1, &x2, &y2, "><", MOTION_FORWARD_LETTER))
192175
return;
193176

194-
if(to_y < cur){
195-
int x = to_y;
196-
to_y = cur;
197-
cur = x;
198-
}
199-
200-
l = buffer_getindex(buffers_current(), cur);
201-
while(cur++ <= to_y && l){
177+
l = buffer_getindex(buffers_current(), y1);
178+
while(y1++ <= y2 && l){
202179
shiftline((char **)&l->data, repeat);
203180
l = l->next;
204181
}
205182

206-
gui_move(cur - 1, 0);
207-
m.motion = MOTION_LINE_START;
208-
gui_move_motion(&m);
183+
gui_move_sol(y1 - 1);
209184
buffer_modified(buffers_current()) = 1;
210185
}
211186

212-
void tilde(unsigned int rep)
213-
{
214-
char *data = (char *)buffer_getindex(buffers_current(), gui_y())->data;
215-
char *pos = data + gui_x();
216-
217-
if(!rep)
218-
rep = 1;
219-
220-
gui_move(gui_y(), gui_x() + rep);
221-
222-
while(rep --> 0){
223-
if(islower(*pos))
224-
*pos = toupper(*pos);
225-
else
226-
*pos = tolower(*pos);
227-
228-
/* *pos ^= (1 << 5); * flip bit 100000 = 6 */
229-
230-
if(!*++pos)
231-
break;
232-
}
233-
234-
buffer_modified(buffers_current()) = 1;
235-
}
236-
237-
void showgirl(unsigned int page)
238-
{
239-
char *word = gui_current_word();
240-
char *buf;
241-
int len;
242-
243-
if(!word){
244-
gui_status(GUI_ERR, "invalid word");
245-
return;
246-
}
247-
248-
buf = umalloc(len = strlen(word) + 16);
249-
250-
if(page)
251-
snprintf(buf, len, "man %d %s", page, word);
252-
else
253-
snprintf(buf, len, "man %s", word);
254-
255-
shellout(buf, NULL);
256-
free(buf);
257-
free(word);
258-
}
259-
260-
int go_file()
261-
{
262-
char *fname = gui_current_fname();
263-
264-
if(fname){
265-
/* TODO: query - rewrite gui_readfile() and so on, all goes through one f() */
266-
buffers_load(fname);
267-
free(fname);
268-
return 0;
269-
}else{
270-
gui_status(GUI_ERR, "no file selected");
271-
return 1;
272-
}
273-
}
274-
275187
void replace(unsigned int n)
276188
{
277189
int c;
@@ -546,7 +458,7 @@ static void motion_cmd(struct motion *motion,
546458
si.top = gui_top();
547459
si.height = gui_max_y();
548460

549-
if(!applymotion(motion, &topos, &si)){
461+
if(!motion_apply(motion, &topos, &si)){
550462
struct range from;
551463

552464
from.start = gui_y();
@@ -558,7 +470,7 @@ static void motion_cmd(struct motion *motion,
558470
from.end = t;
559471
}
560472

561-
if(islinemotion(motion)){
473+
if(motion_is_line(motion)){
562474
/* delete lines between gui_y() and y, inclusive */
563475
f_line(&from);
564476
gui_move_sol(from.start);
@@ -638,7 +550,7 @@ static void change(struct motion *motion, int ins)
638550
{
639551
int x, y, dollar = 0;
640552

641-
if(getmotion(motion, 1, 0, "dc", MOTION_WHOLE_LINE))
553+
if(motion_get(motion, 1, 0, "dc", MOTION_WHOLE_LINE))
642554
return;
643555

644556
if(ins){
@@ -653,7 +565,7 @@ static void change(struct motion *motion, int ins)
653565
pos.x = &x;
654566
pos.y = &y;
655567

656-
if(!applymotion(motion, &pos, &si))
568+
if(!motion_apply(motion, &pos, &si))
657569
dollar = 1;
658570
}
659571

@@ -991,7 +903,7 @@ void gui_run()
991903
break;
992904

993905
case 'y':
994-
if(getmotion(&motion, 1, multiple, "y", MOTION_WHOLE_LINE))
906+
if(motion_get(&motion, 1, multiple, "y", MOTION_WHOLE_LINE))
995907
break;
996908
motion_cmd(&motion, yank_line, yank_range);
997909
SET_DOT();
@@ -1196,6 +1108,10 @@ void gui_run()
11961108
if(!go_file())
11971109
buffer_changed = 1;
11981110
break;
1111+
case 'q':
1112+
if(!fmt_motion())
1113+
buffer_changed = 1;
1114+
break;
11991115
default:
12001116
gui_status(GUI_ERR, "Invalid ! suffix");
12011117
}
@@ -1207,8 +1123,8 @@ void gui_run()
12071123
INC_MULTIPLE();
12081124
}else{
12091125
gui_ungetch(c);
1210-
if(!getmotion(&motion, 0, multiple, "", 0)){
1211-
if(isbigmotion(&motion) && motion.motion != MOTION_MARK)
1126+
if(!motion_get(&motion, 0, multiple, "", 0)){
1127+
if(motion_is_big(&motion) && motion.motion != MOTION_MARK)
12121128
mark_jump();
12131129
gui_move_motion(&motion);
12141130
if(visual_get() != VISUAL_NONE)

gui/extra.c

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <stdarg.h>
2+
#include <time.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <ctype.h>
6+
#include <string.h>
7+
8+
#include "gui.h"
9+
#include "../range.h"
10+
#include "../buffer.h"
11+
#include "../buffers.h"
12+
#include "../util/list.h"
13+
#include "../util/alloc.h"
14+
#include "extra.h"
15+
#include "../command.h"
16+
#include "motion.h"
17+
#include "../util/pipe.h"
18+
19+
int go_file()
20+
{
21+
char *fname = gui_current_fname();
22+
23+
if(fname){
24+
buffers_load(fname);
25+
free(fname);
26+
return 0;
27+
}else{
28+
gui_status(GUI_ERR, "no file selected");
29+
return 1;
30+
}
31+
}
32+
33+
int fmt_motion()
34+
{
35+
struct range rng;
36+
int x[2];
37+
38+
if(motion_wrap(&x[0], &rng.start, &x[1], &rng.end, "", 0))
39+
return 1;
40+
41+
buffer_modified(buffers_current()) = 1;
42+
return range_through_pipe(&rng, "fmt -80");
43+
}
44+
45+
void tilde(unsigned int rep)
46+
{
47+
char *data = (char *)buffer_getindex(buffers_current(), gui_y())->data;
48+
char *pos = data + gui_x();
49+
50+
if(!rep)
51+
rep = 1;
52+
53+
gui_move(gui_y(), gui_x() + rep);
54+
55+
while(rep --> 0){
56+
if(islower(*pos))
57+
*pos = toupper(*pos);
58+
else
59+
*pos = tolower(*pos);
60+
61+
/* *pos ^= (1 << 5); * flip bit 100000 = 6 */
62+
63+
if(!*++pos)
64+
break;
65+
}
66+
67+
buffer_modified(buffers_current()) = 1;
68+
}
69+
70+
void showgirl(unsigned int page)
71+
{
72+
char *word = gui_current_word();
73+
char *buf;
74+
int len;
75+
76+
if(!word){
77+
gui_status(GUI_ERR, "invalid word");
78+
return;
79+
}
80+
81+
buf = umalloc(len = strlen(word) + 16);
82+
83+
if(page)
84+
snprintf(buf, len, "man %d %s", page, word);
85+
else
86+
snprintf(buf, len, "man %s", word);
87+
88+
shellout(buf, NULL);
89+
free(buf);
90+
free(word);
91+
}

0 commit comments

Comments
 (0)