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.c293
1 files changed, 0 insertions, 293 deletions
diff --git a/src/edit_libedit.c b/src/edit_libedit.c
deleted file mode 100644
index b6cdcd3..0000000
--- a/src/edit_libedit.c
+++ /dev/null
@@ -1,293 +0,0 @@
-/**
- * @file edit_libedit.c
- * @brief The line editor implemented with libedit (BSD editline).
- *
- * This is the compile-time alternative to edit_builtin.c:
- *
- * make EDIT=libedit LDLIBS=-ledit
- *
- * It provides the same two functions, edit_usable() and edit_readline(),
- * so nothing else in the shell knows which editor is linked. libedit
- * brings its own key bindings (including a fuller vi mode) and reads
- * `~/.editrc`. It also brings a dependency on the library and on
- * terminfo or termcap, which is why it is not the default.
- *
- * The shell's own history list (history.c) stays the single source of
- * 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>
-#include <stdio.h>
-#include <stdlib.h>
-#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' };
-
-const char edit_name[] = "libedit";
-
-/**
- * @brief The libedit editor, created on first use.
- */
-static EditLine *el;
-/**
- * @brief libedit's copy of the history.
- */
-static History *el_hist;
-/**
- * @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 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 zash's completion, bound to Tab.
- *
- * libedit hands the whole line and cursor to an editing function, so the
- * work is the same as the built-in editor's: one candidate is inserted
- * whole (with the quoting it needs) and the word finished with a space
- * unless it is a directory; several are completed as far as they agree,
- * and listed when that adds nothing. The listing comes from complete.c,
- * so both editors print the same block.
- *
- * @param e The editor.
- * @param ch The key, unused.
- * @return CC_REFRESH so libedit redraws the line.
- */
-static unsigned char complete_cb(EditLine *e, int ch) {
- (void)ch;
- const LineInfo *li = el_line(e);
- if (!li)
- return CC_ERROR;
- size_t cursor = (size_t)(li->cursor - li->buffer);
- completion c;
- complete_line(li->buffer, cursor, &c);
- if (!c.count) {
- complete_free(&c);
- return CC_REDISPLAY;
- }
- size_t common = strlen(c.list[0]);
- for (int i = 1; i < c.count; i++) {
- size_t k = 0;
- while (k < common && c.list[i][k] == c.list[0][k])
- k++;
- common = k;
- }
- /* The typed word without its backslashes, to know whether the
- * candidates would make it longer. */
- size_t typed = 0;
- for (size_t i = c.start; i < cursor; i++) {
- if (li->buffer[i] == '\\' && !c.quoted && i + 1 < cursor)
- i++;
- typed++;
- }
- if (c.count == 1 || common > typed) {
- el_deletestr(e, (int)(cursor - c.start));
- char *text = complete_escaped(c.list[0], common, c.quoted);
- el_insertstr(e, text);
- free(text);
- char last = c.list[0][common - 1];
- bool final = last != '/' && !(c.mvs && (last == '.' || last == '('));
- if (c.count == 1 && final) {
- if (c.quote) {
- char q[2] = {c.quote, '\0'};
- el_insertstr(e, q);
- }
- el_insertstr(e, " ");
- }
- } else {
- char *listing = complete_listing(&c, term_cols(), term_rows());
- fputs(listing, stderr);
- free(listing);
- }
- complete_free(&c);
- /* The whole line, not just the part that changed: what a completion
- * replaces is in the middle of it, and a redraw of the tail alone
- * leaves the reader's terminal showing a different line from the one
- * the shell has. */
- return CC_REDISPLAY;
-}
-
-/**
- * @brief Choose the editing mode and (re)install the completion key.
- *
- * libedit replaces the key map when the editor is selected, which drops
- * bindings made before, so the two go together and only when the mode
- * changes.
- *
- * @param vi true for vi bindings, false for emacs.
- */
-static void set_mode(bool vi) {
- static bool done;
- static bool is_vi;
- if (done && is_vi == vi)
- return;
- el_set(el, EL_EDITOR, vi ? "vi" : "emacs");
- el_set(el, EL_ADDFN, "zash-complete",
- "Complete the word before the cursor", complete_cb);
- el_set(el, EL_BIND, "^I", "zash-complete", nullptr);
- done = true;
- is_vi = vi;
-}
-
-/**
- * @brief libedit callback: the prompt for the line being read.
- * @param e Unused.
- * @return The prompt set by edit_readline().
- */
-static char *prompt_cb(EditLine *e) {
- (void)e;
- return cur_prompt ? cur_prompt : "";
-}
-
-/**
- * @brief Can lines be edited on this terminal?
- * @return true if stdin and stderr are terminals and TERM is not "dumb".
- */
-bool edit_usable(void) {
- const char *term = var_get("TERM");
- if (!term || !*term || !strcmp(term, "dumb"))
- return false;
- return isatty(0) && isatty(2);
-}
-
-/**
- * @brief Read one line with editing.
- *
- * @param prompt Text to show before the line.
- * @param len Receives the length of the line.
- * @return The line without its newline, valid until the next call;
- * nullptr at end of input.
- */
-char *edit_readline(const char *prompt, size_t *len) {
- static char *line;
- HistEvent ev;
- if (!el) {
- el = el_init("zash", stdin, stderr, stderr);
- el_hist = history_init();
- if (!el || !el_hist)
- return nullptr;
- history(el_hist, &ev, H_SETSIZE, 100000);
- el_set(el, EL_HIST, history, el_hist);
- el_set(el, EL_PROMPT_ESC, prompt_cb, PROMPT_MARK);
- el_set(el, EL_SIGNAL, 0);
- /* Before el_source(), so that ~/.editrc may bind Tab to
- * something else. */
- set_mode(opt.vi);
- el_source(el, nullptr);
- }
- set_mode(opt.vi);
- history(el_hist, &ev, H_CLEAR);
- for (int i = 0; i < hist_count(); i++)
- history(el_hist, &ev, H_ENTER, hist_entry(i));
-
- free(cur_prompt);
- cur_prompt = prompt_marked(prompt);
- for (;;) {
- int n = 0;
- const char *s = el_gets(el, &n);
- if (s && n > 0) {
- size_t k = (size_t)n;
- while (k && (s[k - 1] == '\n' || s[k - 1] == '\r'))
- k--;
- free(line);
- line = xmalloc(k + 1);
- memcpy(line, s, k);
- line[k] = '\0';
- *len = k;
- return line;
- }
- if (n < 0 && errno == EINTR) {
- trap_poll(); /* ^C abandons the line and does not return */
- continue;
- }
- return nullptr;
- }
-}
-
-int edit_bind(const char *spec) {
- sh_warn("bind: %s: this editor reads its bindings from ~/.editrc", spec);
- return -1;
-}
-
-void edit_bind_list(void) {
- sh_warn("bind: -l: this editor reads its bindings from ~/.editrc");
-}