Skip to content
This repository has been archived by the owner on Dec 10, 2023. It is now read-only.

Commit

Permalink
auto-complete file names and redo undo
Browse files Browse the repository at this point in the history
  • Loading branch information
MorganPeterson committed Jan 3, 2022
1 parent 28cab7a commit 66691df
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ There is regex code in the editor that is derived from Marc André Tanner's Vis
C-P previous line
C-R search-backwards
C-S search-forwards
C-U Undo
C-U undo
C-V Page Down
C-W Kill Region (Cut)
C-X CTRL-X command prefix
Expand Down
13 changes: 11 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ S=strutils
B=buffer
W=window
K=keys
P=complete
C=commands
D=display
U=utf
Expand All @@ -33,6 +34,7 @@ SU=$(SRCD)/$(S).c
BF=$(SRCD)/$(B).c
WN=$(SRCD)/$(W).c
KY=$(SRCD)/$(K).c
CP=$(SRCD)/$(P).c
CM=$(SRCD)/$(C).c
DY=$(SRCD)/$(D).c
U8=$(SRCD)/$(U).c
Expand All @@ -48,6 +50,7 @@ SUO=$(OBJD)/$(S).o
BFO=$(OBJD)/$(B).o
WNO=$(OBJD)/$(W).o
KYO=$(OBJD)/$(K).o
CPO=$(OBJD)/$(P).o
CMO=$(OBJD)/$(C).o
DYO=$(OBJD)/$(D).o
U8O=$(OBJD)/$(U).o
Expand All @@ -58,16 +61,17 @@ SYO=$(OBJD)/$(X).o
TMO=$(OBJD)/$(T).o
RPO=$(OBJD)/$(E).o

OBJS=$(SUO) $(IUO) $(BFO) $(WNO) $(KYO) $(CMO) $(DYO) $(U8O) $(RXO) $(CHO) $(RPO) $(UNO) $(SYO) $(TMO)
OBJS=$(SUO) $(IUO) $(BFO) $(WNO) $(KYO) $(CPO) $(CMO) $(DYO) $(U8O) $(RXO) $(CHO) $(RPO) $(UNO) $(SYO) $(TMO)

.PHONY:all $(I) $(S) $(B) $(W) $(K) $(C) $(D) $(U) $(R) $(H) $(E) $(N) $(X) $(T) $(NAME)
.PHONY:all $(I) $(S) $(B) $(W) $(K) $(P) $(C) $(D) $(U) $(R) $(H) $(E) $(N) $(X) $(T) $(NAME)

all: $(OBJS) $(NAME)
$(I):$(IUO)
$(S):$(SUO)
$(B):$(BFO)
$(W):$(WNO)
$(K):$(KYO)
$(P):$(CPO)
$(C):$(CMO)
$(D):$(DYO)
$(U):$(U8O)
Expand Down Expand Up @@ -104,6 +108,11 @@ $(KYO):$(KY)
@mkdir -p $(@D)
@$(CC) $(FLAGS) -c $(KY) -o $@

$(CPO):$(CP)
@echo "building $(P)"
@mkdir -p $(@D)
@$(CC) $(FLAGS) -c $(CP) -o $@

$(CMO):$(CM)
@echo "building $(C)"
@mkdir -p $(@D)
Expand Down
4 changes: 2 additions & 2 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ insertfile(void)
{
char_t temp[FNAME_MAX];
temp[0] = '\0';
if (get_input("insert-file: ", temp, FNAME_MAX, 1))
if (getfilename("insert-file: ", temp, FNAME_MAX))
(void)insert_file(temp, 1);
}

Expand All @@ -273,7 +273,7 @@ findfile(void)

fname[0] = '\0';

if (get_input("find-file: ", fname, FNAME_MAX, 1)) {
if (getfilename("find-file: ", fname, FNAME_MAX)) {
buffer_t *b = find_buffer_fname(fname);

disassociate_buffer(curwin);
Expand Down
87 changes: 87 additions & 0 deletions src/complete.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* complete.c, editor, Morgan Peterson, Public Domain, 2021
* complete.c, Atto Emacs, Hugh Barney, Public Domain, 2016
*/

#include "header.h"

/* basic filename completion, based on code in uemacs/PK */
int32_t
getfilename(char *prompt, char_t *buf, int32_t nbuf)
{
static char temp_file[] = TEMPFILE;
int32_t cpos = 0; /* current character position in string */
int32_t k = 0, c, fd, didtry, iswild = 0;

char sys_command[255];
FILE *fp = NULL;
buf[0] ='\0';

for (;;) {
didtry = (k == 0x09); /* Was last command tab-completion? */
display_prompt_and_response(prompt, (char*)buf);
k = getch(); /* get a character from the user */

switch(k) {
case 0x07: /* ctrl-g, abort */
case 0x0a: /* cr, lf */
case 0x0d:
if (fp != NULL) fclose(fp);
return (k != 0x07 && cpos > 0);

case 0x7f: /* del, erase */
case 0x08: /* backspace */
if (cpos == 0) continue;
buf[--cpos] = '\0';
break;

case 0x15: /* C-u kill */
cpos = 0;
buf[0] = '\0';
break;

case 0x09: /* TAB, complete file name */
/* scan backwards for a wild card and set */
iswild=0;
while (cpos > 0) {
cpos--;
if (buf[cpos] == '*' || buf[cpos] == '?')
iswild = 1;
}

/* first time retrieval */
if (! didtry) {
if (fp != NULL) fclose(fp);
strcpy(temp_file, TEMPFILE);
if (-1 == (fd = mkstemp(temp_file)))
fatal("%s: Failed to create temp file\n");
strcpy(sys_command, "echo ");
strcat(sys_command, (char*)buf);
if (!iswild) strcat(sys_command, "*");
strcat(sys_command, " >");
strcat(sys_command, temp_file);
strcat(sys_command, " 2>&1");
(void) ! system(sys_command); /* stop compiler unused result warning */
fp = fdopen(fd, "r");
unlink(temp_file);
}

/* copy next filename into buf */
while ((c = getc(fp)) != EOF && c != '\n' && c != ' ')
if (cpos < nbuf - 1 && c != '*')
buf[cpos++] = c;

buf[cpos] = '\0';
if (c != ' ') rewind(fp);
didtry = 1;
break;

default:
if (cpos < nbuf - 1) {
buf[cpos++] = k;
buf[cpos] = '\0';
}
break;
}
}
}
3 changes: 3 additions & 0 deletions src/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <locale.h>
#include <signal.h>
Expand All @@ -31,6 +32,7 @@
#define MIN_GAP_SIZE 16
#define FNAME_MAX 256
#define BNAME_MAX 16
#define TEMPFILE "/tmp/mpeXXXXXX"

enum {
HL_HIGHLIGHT_NUMBERS = 1 << 0,
Expand Down Expand Up @@ -283,4 +285,5 @@ int32_t parse_text(buffer_t *b, int32_t p);
void replace_string(buffer_t *b, char_t *s, char_t *r, int32_t slen, int32_t rlen);
void init_colors(void);
void query_replace(void);
int32_t getfilename(char *prompt, char_t *buf, int32_t nbuf);
#endif
4 changes: 3 additions & 1 deletion src/strutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
#define addu __builtin_add_overflow
#else
static inline bool addu(size_t a, size_t b, size_t *c) {
static inline bool
addu(size_t a, size_t b, size_t *c)
{
if (SIZE_MAX - a < b)
return false;
*c = a + b;
Expand Down

0 comments on commit 66691df

Please sign in to comment.