summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichele Bini <rev@alienbuntu.local>2019-04-22 01:19:23 (GMT)
committerMichele Bini <rev@alienbuntu.local>2019-04-22 01:19:23 (GMT)
commita9988a219f53c8b7a1029dc0f2e98c59f84f3964 (patch)
tree35371f2c975296e655a1b246a16541095242378a
parent8d1afb7766c948c58d45333888a5f99477038167 (diff)
Modified: nanobox.c
-rw-r--r--nanobox.c127
1 files changed, 97 insertions, 30 deletions
diff --git a/nanobox.c b/nanobox.c
index 3254329..cd83a20 100644
--- a/nanobox.c
+++ b/nanobox.c
@@ -170,6 +170,24 @@
//config: Unless you want more (or less) frequent "undo points" while typing,
//config: you should probably leave this unchanged.
//config:
+#define ENABLE_FEATURE_NANO_INDENT_SENSITIVE 1
+//config:config FEATURE_NANO_INDENT_SENSITIVE
+//config: bool "Make common operations indent-sensitive"
+//config: default y
+//config: depends on NANO
+//config: help
+//config: This option makes the HOME and RETURN key behave differently
+//config: depending on the current indentation level.
+//config:
+// #define ENABLE_FEATURE_NANO_FOLDING 1
+//config:config FEATURE_NANO_FOLDING
+//config: bool "Support code folding"
+//config: default n
+//config: depends on NANO
+//config: help
+//config: Support code folding.
+//config: NOT IMPLEMENTED YET
+//config:
#define ENABLE_FEATURE_NANO_HIGHLIGHT 1
//config:config FEATURE_NANO_HIGHLIGHT
//config: bool "Support syntax highlighting"
@@ -177,6 +195,7 @@
//config: depends on NANO
//config: help
//config: Support syntax highlighting.
+//config: NOT IMPLEMENTED YET
//config:
#define ENABLE_FEATURE_NANO_HIGHLIGHT_LANGUAGE_C 1
//config:config FEATURE_NANO_HIGHLIGHT_LANGUAGE_C
@@ -185,6 +204,7 @@
//config: depends on NANO
//config: help
//config: Support syntax highlighting for the C language
+//config: NOT IMPLEMENTED YET
//config:
#define _GNU_SOURCE
@@ -316,16 +336,6 @@ enum {
//#define ESC_CURSOR_UP "\033[A"
//#define ESC_CURSOR_DOWN "\n"
-enum {
- YANKONLY = FALSE,
- YANKDEL = TRUE,
- FORWARD = 1, // code depends on "1" for array index
- BACK = -1, // code depends on "-1" for array index
- LIMITED = 0, // how much of text[] in char_search
- FULL = 1, // how much of text[] in char_search
-};
-
-
/* nano.c expects chars to be unsigned. */
/* busybox build system provides that, but it's better */
/* to audit and fix the source */
@@ -453,6 +463,9 @@ struct globals {
char undo_text[1]; // text that was deleted (if deletion)
} *undo_stack_tail;
#endif /* ENABLE_FEATURE_NANO_UNDO */
+#if ENABLE_FEATURE_NANO_FOLDING
+ char *current_fold;
+#endif
};
#define G (*ptr_to_globals)
#define text (G.text )
@@ -519,6 +532,9 @@ struct globals {
#define undo_queue_spos (G.undo_queue_spos )
# endif
#endif
+#if ENABLE_FEATURE_NANO_FOLDING
+#define current_fold (G.current_fold)
+#endif
#define INIT_G() do { \
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
@@ -564,9 +580,9 @@ static char *text_hole_delete(char *, char *, int); // at "p", delete a 'size' b
// and be careful to not use pointers into potentially freed text[]!
static uintptr_t text_hole_make(char *, int); // at "p", make a 'size' byte hole
#if !ENABLE_FEATURE_NANO_UNDO
-#define yank_delete(a,b,c,d,e) yank_delete(a,b,c,d)
+#define yank_delete(a,b,c,d) yank_delete(a,b,c)
#endif
-static char *yank_delete(char *, char *, int, int, int); // yank text[] into register then delete
+static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
static void show_help(void); // display some help info
static void rawmode(void); // set "raw" mode on tty
static void cookmode(void); // return to "cooked" mode on tty
@@ -615,7 +631,6 @@ static uintptr_t string_insert(char *, const char *, int); // insert the string
#endif
#if ENABLE_FEATURE_NANO_YANKMARK
static char *text_yank(char *, char *); // save copy of "p" into a register
-static void check_context(char); // remember context for '' command
static int yank_append = 0;
static int previous_yank_position = -1;
#endif
@@ -1152,6 +1167,27 @@ static void dot_right(void)
#endif
}
+static int dot_beginning_of_code_line(void)
+{
+ char *prev_dot;
+ undo_queue_commit();
+ prev_dot = dot;
+ dot_begin(); // return pointer to first char cur line
+ while (dot < end) {
+ switch (*dot) {
+ case ' ': case '\t':
+ break;
+ default:
+ if (dot >= prev_dot) goto break_out;
+ return 1;
+ }
+ dot++;
+ }
+ break_out:
+ dot = prev_dot;
+ return 0;
+}
+
static void dot_begin(void)
{
undo_queue_commit();
@@ -1743,7 +1779,7 @@ static char *text_hole_delete(char *p, char *q, int undo) // delete "p" through
// copy text into register, then delete text.
// if dist <= 0, do not include, or go past, a NewLine
//
-static char *yank_delete(char *start, char *stop, int dist, int yf, int undo)
+static char *yank_delete(char *start, char *stop, int dist, int undo)
{
char *p;
@@ -1771,9 +1807,7 @@ static char *yank_delete(char *start, char *stop, int dist, int yf, int undo)
#if ENABLE_FEATURE_NANO_YANKMARK
text_yank(start, stop);
#endif
- if (yf == YANKDEL) {
- p = text_hole_delete(start, stop, undo);
- } // delete lines
+ p = text_hole_delete(start, stop, undo);
return p;
}
@@ -1838,7 +1872,7 @@ static uintptr_t string_insert(char *p, const char *s, int undo) // insert the s
if (*s == '\n')
cnt++;
}
- status_line("Put %d lines (%d chars)", cnt, i);
+ if (cnt > 4) status_line("Put %d lines (%d chars)", cnt, i);
}
#endif
return bias;
@@ -2598,12 +2632,38 @@ static int save_file(void)
static int get_filename(void)
{
char *p = get_input_line("Save to file: ", current_filename);
- if (p == NULL || p[0] == 0) return 0;
+ if (p[0] == 0) return 0;
free(current_filename);
current_filename = xstrdup(p);
return 1;
}
+#if ENABLE_FEATURE_NANO_INDENT_SENSITIVE
+static void newline_cmd(void)
+{
+ char *save_dot = dot;
+ char *a, *b, *s;
+ if (dot_beginning_of_code_line()) {
+ b = dot;
+ dot_begin();
+ a = dot;
+ dot = save_dot;
+ s = xstrndup(a, (b - a));
+ s[b - a];
+ dot = char_insert(dot, '\n', ALLOW_UNDO_QUEUED);
+ dot += string_insert(dot, s, ALLOW_UNDO) + b - a;
+ free(s);
+ } else {
+ dot = char_insert(dot, '\n', ALLOW_UNDO_QUEUED);
+ }
+}
+#endif
+#if ENABLE_FEATURE_NANO_FOLDING
+static void fold_cmd(void)
+{
+}
+#endif
+
//---------------------------------------------------------------------
//----- the Ascii Chart -----------------------------------------------
//
@@ -2643,9 +2703,9 @@ static void do_cmd(int c)
/* if this is a cursor key, skip these checks */
switch (c) {
-#if ENABLE_FEATURE_NANO_INSERT
- // Unsupported yet, should insert file at point
- case KEYCODE_INSERT:
+#if 0 // ENABLE_FEATURE_NANO_INSERT
+ // Unsupported yet, should insert file at point
+ case KEYCODE_INSERT:
#endif
default:
if (1 <= c || Isprint(c)) {
@@ -2734,6 +2794,11 @@ static void do_cmd(int c)
// try stay in same col
dot = move_to_col(dot, ccol + offset);
break;
+#if ENABLE_FEATURE_NANO_INDENT_SENSITIVE
+ case '\r':
+ newline_cmd();
+ break;
+#endif
case 12: // ctrl-L force redraw whole screen
case 18: // ctrl-R force redraw
place_cursor(0, 0);
@@ -2751,7 +2816,7 @@ static void do_cmd(int c)
#endif
save_dot = begin_line(dot);
dot = end_line(dot);
- dot = yank_delete(save_dot, dot, 1, YANKDEL, ALLOW_UNDO);
+ dot = yank_delete(save_dot, dot, 1, ALLOW_UNDO);
#if ENABLE_FEATURE_NANO_YANKMARK
yank_append = 1;
previous_yank_position = dot - text;
@@ -2842,7 +2907,16 @@ static void do_cmd(int c)
dot = move_to_col(dot, ccol + offset); // try stay in same col
break;
case KEYCODE_HOME: // Cursor Key Home
+#if ENABLE_FEATURE_NANO_INDENT_SENSITIVE
+ save_dot = dot;
+ dot_beginning_of_code_line();
+ if (save_dot != dot) break;
+#endif
dot_begin();
+#if ENABLE_FEATURE_NANO_FOLDING
+ if (save_dot != dot) break;
+ fold_cmd();
+#endif
break;
// The Fn keys could point to do_macro which could translate them
#if 0
@@ -2863,13 +2937,6 @@ static void do_cmd(int c)
}
// if text[] just became empty, add back an empty line
-#if 0
- // I tried disabling this by two newlines are added instead!
- if (end == text) {
- char_insert(text, '\n', NO_UNDO); // start empty buf with dummy line
- dot = text;
- }
-#endif
// it is OK for dot to exactly equal to end, otherwise check dot validity
if (dot != end) {
dot = bound_dot(dot); // make sure "dot" is valid