summaryrefslogtreecommitdiffhomepage
path: root/src/edit_libedit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/edit_libedit.c')
-rw-r--r--src/edit_libedit.c86
1 files changed, 81 insertions, 5 deletions
diff --git a/src/edit_libedit.c b/src/edit_libedit.c
index d9282fc..a76844a 100644
--- a/src/edit_libedit.c
+++ b/src/edit_libedit.c
@@ -16,6 +16,12 @@
* truth, because `fc` works on it. Before each line is read the list is
* copied into libedit's history so that the arrow keys and searches see
* the same entries.
+ *
+ * A prompt is written by libedit and counted as it is written, so the
+ * colour sequences of PS1 would be counted as visible columns and the
+ * cursor would land to the right of where it belongs. Libedit is told
+ * the ignore character (EL_PROMPT_ESC) and every control sequence is
+ * bracketed with it, which prints the sequence but does not count it.
*/
#include <errno.h>
#include <histedit.h>
@@ -24,10 +30,14 @@
#include <string.h>
#include <unistd.h>
+#include "charset.h"
#include "exec.h"
#include "interact.h"
#include "shell.h"
+/** @brief The character that brackets a run libedit must not count. */
+enum { PROMPT_MARK = '\1' };
+
/**
* @brief The libedit editor, created on first use.
*/
@@ -37,9 +47,74 @@ static EditLine *el;
*/
static History *el_hist;
/**
- * @brief The prompt for the line being read.
+ * @brief The prompt for the line being read, marked for libedit.
+ */
+static char *cur_prompt;
+
+/**
+ * @brief End of the run of control sequences that starts at s.
+ *
+ * Adjacent sequences share one mark pair: libedit attaches the run to
+ * the character after the closing mark, and that character has to be a
+ * printable one.
+ *
+ * @param s Text starting with CH_ESC.
+ * @return The first byte after the run.
+ */
+static const char *ctl_run_end(const char *s) {
+ do
+ s += esc_seq_len(s);
+ while (*s == CH_ESC);
+ return s;
+}
+
+/**
+ * @brief Bracket the parts of a prompt that take no room on the screen.
+ *
+ * libedit counts every character of the prompt as a column unless it is
+ * marked. Its literal run is attached to the character after the closing
+ * mark, whose width is the run's width, so a prompt that ends with a
+ * control sequence gets a space from here to be that character: libedit
+ * would drop the run otherwise (prompt.c: "We lose the last literal"),
+ * and counting the sequence instead would put the cursor out by its
+ * length. The extra space is visible and is counted, so the line still
+ * starts where libedit thinks it does.
+ *
+ * @param s The prompt.
+ * @return The prompt with its control sequences marked, for free().
*/
-static const char *cur_prompt = "";
+static char *prompt_marked(const char *s) {
+ size_t n = strlen(s);
+ const char *last = nullptr;
+ for (const char *p = s; *p;) {
+ if (*p != CH_ESC) {
+ p++;
+ continue;
+ }
+ n += 2;
+ last = ctl_run_end(p);
+ p = last;
+ }
+ bool trailing = last && !*last;
+ char *out = xmalloc(n + (trailing ? 2 : 1));
+ char *o = out;
+ for (const char *p = s; *p;) {
+ if (*p != CH_ESC) {
+ *o++ = *p++;
+ continue;
+ }
+ const char *e = ctl_run_end(p);
+ *o++ = PROMPT_MARK;
+ memcpy(o, p, (size_t)(e - p));
+ o += e - p;
+ *o++ = PROMPT_MARK;
+ p = e;
+ }
+ if (trailing)
+ *o++ = ' ';
+ *o = '\0';
+ return out;
+}
/**
* @brief libedit callback: the prompt for the line being read.
@@ -48,7 +123,7 @@ static const char *cur_prompt = "";
*/
static char *prompt_cb(EditLine *e) {
(void)e;
- return (char *)cur_prompt;
+ return cur_prompt ? cur_prompt : "";
}
/**
@@ -80,7 +155,7 @@ char *edit_readline(const char *prompt, size_t *len) {
return nullptr;
history(el_hist, &ev, H_SETSIZE, 100000);
el_set(el, EL_HIST, history, el_hist);
- el_set(el, EL_PROMPT, prompt_cb);
+ el_set(el, EL_PROMPT_ESC, prompt_cb, PROMPT_MARK);
el_set(el, EL_SIGNAL, 0);
el_source(el, nullptr);
}
@@ -89,7 +164,8 @@ char *edit_readline(const char *prompt, size_t *len) {
for (int i = 0; i < hist_count(); i++)
history(el_hist, &ev, H_ENTER, hist_entry(i));
- cur_prompt = prompt;
+ free(cur_prompt);
+ cur_prompt = prompt_marked(prompt);
for (;;) {
int n = 0;
const char *s = el_gets(el, &n);