From 4f3d3364de8f1c23819051cb9f392fc651565b95 Mon Sep 17 00:00:00 2001
From: joe <rbousset@42lyon.fr>
Date: Tue, 1 Dec 2020 18:36:35 +0100
Subject: commit

---
 src/c_init.c      | 36 +++++++++++++++++++++++++++++++++++-
 src/c_init.h      |  3 +++
 src/c_keys_next.c | 19 ++-----------------
 src/c_keys_next.h |  3 +--
 src/m_loop.c      |  1 +
 5 files changed, 42 insertions(+), 20 deletions(-)

diff --git a/src/c_init.c b/src/c_init.c
index 965af3f..b463830 100644
--- a/src/c_init.c
+++ b/src/c_init.c
@@ -23,6 +23,7 @@
 #include "c_keys_next.h"
 #include "c_input.h"
 #include "c_utils.h"
+#include "d_define.h"
 #include "b_exit.h"
 #include "s_struct.h"
 #include "s_com.h"
@@ -105,11 +106,36 @@ static short
 		return (0);
 }
 
+char	*c_set_ptr(t_bool reset, char key, t_msh *msh)
+{
+	static char	*p = NULL;
+
+	if (reset == TRUE)
+	{
+		p = msh->curr_hist_ptr + ft_strlen(msh->curr_hist_ptr);
+		return (NULL);
+	}
+	if ((key == -1 && p - msh->curr_hist_ptr != 0) || key == 1)
+	{
+		p += (key == -1) ? (2 * key) : (0);
+		while (((key == -1 && p - msh->curr_hist_ptr != 0) || key == 1) && *p != C_LF && *p != C_NUL)
+		{
+			p += key;
+		}
+		if (p - msh->curr_hist_ptr != 0 && *p != C_NUL)
+		{
+			p += 1;
+		}
+	}
+	return (p);
+}
+
 static char
 	*c_process_key(char *buf, t_caps *tcaps, t_msh *msh)
 {
 	static char	*line = NULL;
 	int			i;
+	char		*ptr;
 
 	i = -1;
 	if (line == NULL)
@@ -120,7 +146,15 @@ static char
 	else if (((*((unsigned int *)buf)) == UP_K) ||
 			((*((unsigned int *)buf)) == CTRL_P))
 	{
-		c_key_up(&line, tcaps, msh);
+		ptr = c_set_ptr(FALSE, -1, msh);
+		c_key_up_down(&line, tcaps, ptr);
+		c_redraw_line(line, tcaps, msh);
+	}
+	else if (((*((unsigned int *)buf)) == DOWN_K) ||
+			((*((unsigned int *)buf)) == CTRL_N))
+	{
+		ptr = c_set_ptr(FALSE, 1, msh);
+		c_key_up_down(&line, tcaps, ptr);
 		c_redraw_line(line, tcaps, msh);
 	}
 	else if ((*((unsigned int *)buf)) == DEL_K)
diff --git a/src/c_init.h b/src/c_init.h
index a41202d..dae8513 100644
--- a/src/c_init.h
+++ b/src/c_init.h
@@ -13,6 +13,7 @@
 #ifndef C_INIT_H
 # define C_INIT_H
 
+# include <libft.h>
 # include <termios.h>
 # include <sys/ioctl.h>
 
@@ -34,6 +35,7 @@
 # define CTRL_E 0x05
 # define CTRL_F 0x06
 # define CTRL_L 0x0c
+# define CTRL_N 0x0e
 # define CTRL_P 0x10
 
 typedef struct		s_caps
@@ -47,6 +49,7 @@ typedef struct		s_caps
 	unsigned short	plen;
 }					t_caps;
 
+char				*c_set_ptr(t_bool reset, char key, t_msh *msh);
 short				c_gnl(int fd, char **line, char psx, t_msh *msh);
 short				c_set_term_raw(char mode);
 
diff --git a/src/c_keys_next.c b/src/c_keys_next.c
index 629d9f8..ec063d3 100644
--- a/src/c_keys_next.c
+++ b/src/c_keys_next.c
@@ -17,30 +17,15 @@
 #include "c_init.h"
 #include "c_keys.h"
 #include "d_define.h"
-#include "s_struct.h"
 
-short	c_key_up(char *line[], t_caps *tcaps, t_msh *msh)
+void	c_key_up_down(char *line[], t_caps *tcaps, char *ptr)
 {
-	char			tmp[ARG_MAX];
-	static char		*ptr;
-	static t_bool	set = TRUE;
+	char	tmp[ARG_MAX];
 
-	if (set == TRUE)
-	{
-		ptr = msh->curr_hist_ptr + ft_strlen(msh->curr_hist_ptr);
-		set = FALSE;
-	}
-	ptr -= 2;
-	while (ptr - msh->curr_hist_ptr != 0 && *ptr != '\n')
-	{
-		ptr--;
-	}
-	ptr += 1;
 	c_home_key(tcaps->plen, tcaps);
 	ft_substr_s(tmp, ptr, 0, ft_strclen(ptr, '\n'));
 	ft_memdel((void*)&(*line));
 	tputs(tgetstr("ce", NULL), 1, ft_putchar);
 	*line = ft_strdup(tmp);
 	tcaps->cpos = ft_strlen(*line);
-	return (1);
 }
diff --git a/src/c_keys_next.h b/src/c_keys_next.h
index 53e3f23..fa79d85 100644
--- a/src/c_keys_next.h
+++ b/src/c_keys_next.h
@@ -14,8 +14,7 @@
 # define C_KEYS_NEXT_H
 
 #include "c_init.h"
-#include "s_struct.h"
 
-short	c_key_up(char *line[], t_caps *tcaps, t_msh *msh);
+void	c_key_up_down(char *line[], t_caps *tcaps, char *ptr);
 
 #endif
diff --git a/src/m_loop.c b/src/m_loop.c
index 6444c1b..a5e816d 100644
--- a/src/m_loop.c
+++ b/src/m_loop.c
@@ -152,6 +152,7 @@ unsigned char	m_loop(int fd, t_msh *msh)
 			if (fd == STDIN_FILENO)
 				m_handle_hist(hist, line, msh);
 			m_parse_and_run_line(line, 0, msh);
+			c_set_ptr(TRUE, 0, msh);
 		}
 		else
 			ft_memdel((void*)&line);
-- 
cgit v1.2.3