add read-at-point function - gramscii - A simple editor for ASCII box-and-arrow… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
LICENSE | |
--- | |
commit 62267b8424170f9b136892248a77dbed3fdcbbba | |
parent 3ba178677800de55393f8ec80752ffe72660931a | |
Author: KatolaZ <[email protected]> | |
Date: Fri, 9 Aug 2019 07:08:35 +0100 | |
add read-at-point function | |
Diffstat: | |
M TODO | 7 ++++--- | |
M draw.c | 2 ++ | |
M files.c | 47 +++++++++++++++++++++++++++++… | |
M gramscii.c | 5 +++++ | |
M gramscii.h | 8 ++++++++ | |
M screen.c | 2 ++ | |
6 files changed, 68 insertions(+), 3 deletions(-) | |
--- | |
diff --git a/TODO b/TODO | |
@@ -1,8 +1,7 @@ | |
+ optimize redraws (redraw only the modified rectangle) | |
-- fir bug in reading commands from files | |
+- fix bug in visual crop | |
+- fix bug in reading commands from files | |
- add screen geometry option (-g 25x80?) | |
-- read file at point | |
- - read output of command (!) | |
- maybe move "text" mode to "t" | |
- implement ellipse | |
- (?) filled box (B) | |
@@ -17,6 +16,8 @@ | |
- allow scrolling (both vertical and horizontal) | |
- catch SIGWINCH and react appropriately (after scrolling is | |
enabled) | |
+* read file at point | |
+ * read output of command (!) | |
* fix bug with 'g' commands in arrow mode | |
* undo (by storing lines changed across insert/remove operations) | |
* re-organise undo-ring management | |
diff --git a/draw.c b/draw.c | |
@@ -1,3 +1,5 @@ | |
+#define _POSIX_C_SOURCE 2 | |
+ | |
#include <stdlib.h> | |
#include <string.h> | |
diff --git a/files.c b/files.c | |
@@ -1,3 +1,5 @@ | |
+#define _POSIX_C_SOURCE 2 | |
+ | |
#include <stdio.h> | |
#include <string.h> | |
#include "gramscii.h" | |
@@ -37,6 +39,7 @@ void write_file(FILE *fc){ | |
fclose(fout); | |
modified = 0; | |
get_key(fc, "File saved."); | |
+ redraw(); | |
} | |
void check_modified(FILE *fc){ | |
@@ -83,3 +86,47 @@ void new_file(FILE *fc){ | |
modified=0; | |
} | |
+void read_file_at(FILE *fc, int xl, int yl){ | |
+ | |
+ char nfname[512], tmp[512], *fptr, *tptr; | |
+ FILE *fin; | |
+ int i, j; | |
+ char ftype; | |
+ | |
+ get_string(fc, "Read file: ", nfname, 511); | |
+ fptr = nfname; | |
+ while(*fptr && _isblank(*fptr)) | |
+ fptr ++; | |
+ if (*fptr == '!'){ | |
+ fin = popen(++fptr, "r"); | |
+ ftype = FPIPE; | |
+ } | |
+ else { | |
+ fin = fopen(fptr, "r"); | |
+ ftype = FFILE; | |
+ } | |
+ if (fin != NULL){ | |
+ copy_lines_to_ring(0, HEIGHT-1, PRV_STATE); | |
+ i = yl; | |
+ while((fgets(tmp, WIDTH+1, fin)) != NULL && i<HEIGHT){ | |
+ j = xl; | |
+ tptr = tmp; | |
+ if (strlen(tmp)) | |
+ tmp[strlen(tmp) - 1] = '\0'; | |
+ ensure_line_length(& (screen.l[i]), xl + strlen(tmp) +… | |
+ while (*tptr && j < WIDTH){ | |
+ set_xy(j, i, *tptr); | |
+ j++; | |
+ tptr ++; | |
+ } | |
+ i++; | |
+ } | |
+ if (ftype == FFILE) | |
+ fclose(fin); | |
+ else | |
+ pclose(fin); | |
+ modified = 1; | |
+ redraw(); | |
+ copy_lines_to_ring(yl, i-1, NEW_STATE); | |
+ } | |
+} | |
diff --git a/gramscii.c b/gramscii.c | |
@@ -20,6 +20,8 @@ | |
* | |
*/ | |
+#define _POSIX_C_SOURCE 2 | |
+ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <signal.h> | |
@@ -124,6 +126,9 @@ void commands(FILE *fc){ | |
case 'U': | |
redo_change(); | |
break; | |
+ case 'r': | |
+ read_file_at(fc, x, y); | |
+ break; | |
case 'q': | |
check_modified(fc);/** FALLTHROUGH **/ | |
case 'Q': | |
diff --git a/gramscii.h b/gramscii.h | |
@@ -1,6 +1,8 @@ | |
#ifndef __GRAMSCII_H__ | |
#define __GRAMSCII_H__ | |
+#define _POSIX_C_SOURCE 2 | |
+ | |
#include <stdio.h> | |
#include <termios.h> | |
#include <unistd.h> | |
@@ -58,6 +60,10 @@ | |
#define NEW_STATE 0x02 | |
/**/ | |
+/* file types */ | |
+#define FFILE 0x01 | |
+#define FPIPE 0x02 | |
+ | |
/** types **/ | |
typedef struct{ | |
@@ -157,6 +163,7 @@ void go_to(int where); | |
void crop_to_nonblank(); | |
void crop_to_rect(); | |
void erase_blank_lines(int y1, int y2); | |
+int _isblank(int c); | |
/**/ | |
/** drawing-related functions **/ | |
@@ -176,6 +183,7 @@ void write_file(FILE *fc); | |
void check_modified(FILE *fc); | |
void load_file(FILE *fc); | |
void new_file(FILE *fc); | |
+void read_file_at(FILE *fc, int xl, int yl); | |
/**/ | |
/** line-related functions **/ | |
diff --git a/screen.c b/screen.c | |
@@ -1,3 +1,5 @@ | |
+#define _POSIX_C_SOURCE 2 | |
+ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> |