From: Carlos Henrique Lima Melara <charlesmelara@outlook.com>
Date: Wed, 9 Nov 2022 16:19:45 -0300
Subject: Removes libreadline dependency. Uses libedit compatibility layer
 instead

Author: Carlos Henrique Lima Melara <charlesmelara@outlook.com>
Bug: https://github.com/alecthomas/devtodo/issues/4
Applied-Upstream: 0.1.21, https://github.com/alecthomas/devtodo/commit/afac3bf6670d25684a6a9aac6e05480d3aa67643
Last-Update: 2022-11-09
---
 src/TodoDB.cc  |  2 +-
 src/main.cc    |  2 +-
 src/support.cc | 37 +++++++++----------------------------
 src/support.h  |  2 +-
 src/todorl.h   | 26 --------------------------
 5 files changed, 12 insertions(+), 57 deletions(-)
 delete mode 100644 src/todorl.h

diff --git a/src/TodoDB.cc b/src/TodoDB.cc
index 000ae7b..94e9b67 100644
--- a/src/TodoDB.cc
+++ b/src/TodoDB.cc
@@ -502,7 +502,7 @@ string priority;
 		if (isatty(0)) {
 			if (options.verbose)
 				cout << info << "Enter a priority from those listed above." << normal << endl;
-			priority = readText("priority> ", current, true);
+			priority = readText("priority [" + current + "] > ", "");
 		} else
 			priority = current;
 		priority = trim(priority);
diff --git a/src/main.cc b/src/main.cc
index f3cbafd..da3e61a 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -1,7 +1,7 @@
+#include <readline/readline.h>
 #include "TodoDB.h"
 #include "support.h"
 #include "config.h"
-#include "todorl.h"
 
 void joinArgs(TodoDB &todo, vector<string> const &args, int argc, char const **argv) {
 char const *av[args.size() + argc];
diff --git a/src/support.cc b/src/support.cc
index 8922d0c..2e1db56 100644
--- a/src/support.cc
+++ b/src/support.cc
@@ -4,16 +4,16 @@
 #include <cstdio>
 #include <sstream>
 #include <stdexcept>
+#include <readline/readline.h>
+#include <readline/history.h>
 #include "support.h"
 #include "TodoDB.h"
 #include "Strings.h"
 #include "CommandArgs.h"
 #include "todoterm.h"
 #include "config.h"
-#include "todorl.h"
 
 using namespace str;
-static bool nukeonhit = false;
 
 Options options;
 
@@ -693,7 +693,6 @@ string out = ctime(&date);
 /* Readline support */
 string const *rl_buffer;
 
-static char *getMatches(char *text, int state) {
 static char const *list[] = {
 	"veryhigh",
 	"high",
@@ -702,6 +701,8 @@ static char const *list[] = {
 	"verylow",
 	0
 };
+
+static char *getMatches(const char *text, int state) {
 static int i, len;
 char const *t;
 
@@ -718,42 +719,22 @@ char const *t;
 	return 0;
 }
 
-static char **completion(char *text, int start, int end) {
+static char **completion(const char *text, int start, int end) {
 	if (start == 0)
-		return completion_matches(text, (char*(*)(...))getMatches);
+		return rl_completion_matches(text, (char*(*)(const char*, int))getMatches);
 	return 0;
 }
 
 static int init_rl() {
 	rl_insert_text(const_cast<char*>(rl_buffer->c_str()));
-	rl_attempted_completion_function = (char **(*)(...))completion;
+	rl_attempted_completion_function = (char **(*)(const char*, int, int))completion;
 	return 0;
 }
 
-static int nuke_on_hit() {
-int c = rl_getc(stdin);
-
-	if (nukeonhit && c != 13) {
-		rl_delete_text(0, rl_end);
-		rl_end = rl_mark = rl_point = 0;
-		rl_redisplay();
-		rl_clear_message();
-		nukeonhit = false;
-	}
-	if (c == 14) {
-		rl_insert_text(const_cast<char*>("\n"));
-		return 0;
-	}
-	return c;
-}
-
-string readText(string const &prompt, string existing, bool nuke) {
+string readText(string const &prompt, string existing) {
 string out;
 
-	rl_startup_hook = (int(*)(...))init_rl;
-	rl_getc_function = (int(*)())nuke_on_hit;
-
-	nukeonhit = nuke;
+	rl_startup_hook = (int(*)(const char*, int))init_rl;
 
 	rl_buffer = &existing;
 char const *tmp = readline(const_cast<char*>(prompt.c_str()));
diff --git a/src/support.h b/src/support.h
index adadbda..d1b94c3 100644
--- a/src/support.h
+++ b/src/support.h
@@ -57,7 +57,7 @@ string symbolisePriority(Todo::Priority sym);
 Todo::Priority desymbolisePriority(string sym);
 
 // text input
-string readText(string const &prompt, string existing = "", bool nuke = false);
+string readText(string const &prompt, string existing = "");
 void addHistory(string text);
 
 // Parse command line arguments
diff --git a/src/todorl.h b/src/todorl.h
deleted file mode 100644
index 1b2b5ae..0000000
--- a/src/todorl.h
+++ /dev/null
@@ -1,26 +0,0 @@
-#ifndef TODORL_H__
-#define TODORL_H__
-
-/*	
-	Explicitly declare readline functions because on several distributions
-	the readline headers do NOT compile under C++ at all due to a lack of
-	function parameters.
-*/
-typedef int intfunc ();
-
-extern "C" int rl_initialize();
-extern "C" char *rl_readline_name;
-extern "C" char **completion_matches(char*, char*(*)(...));
-extern "C" void rl_insert_text(char*);
-extern "C" char **(*rl_attempted_completion_function)(...);
-extern "C" int (*rl_startup_hook)(...);
-extern "C" char *readline(char*);
-extern "C" void add_history(char*);
-extern "C" int rl_getc(FILE *);
-extern "C" int rl_point, rl_end, rl_mark;
-extern "C" int rl_delete_text(int, int);
-extern "C" void rl_redisplay();
-extern "C" void rl_clear_message();
-extern "C" intfunc *rl_getc_function;
-
-#endif
