Skip to content

Commit

Permalink
Updated forked linenoise to latest upstream.
Browse files Browse the repository at this point in the history
This removes the need to force it to build as C++, and adds proper UTF-8
support for Windows.

Since this is a fork of linenoise, there's no hope for getting
lua-linenoise to sync with it upstream.  I made the bare minimum changes
to keep it working, but didn't add bindings for new functionality (e.g.
multi-line editing).
  • Loading branch information
cuavas committed Mar 6, 2023
1 parent e9ecdc9 commit 8384223
Show file tree
Hide file tree
Showing 14 changed files with 2,511 additions and 1,274 deletions.
26 changes: 18 additions & 8 deletions 3rdparty/linenoise/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
all: linenoise_example linenoise_utf8_example linenoise_cpp_example
CFLAGS += -Wall -W -Os -g
CC ?= gcc

linenoise_example: linenoise.h linenoise.c example.c
$(CC) -Wall -W -Os -g -o $@ linenoise.c example.c
all: linenoise_example linenoise_utf8_example

linenoise_utf8_example: linenoise.c utf8.c example.c
$(CC) -DNO_COMPLETION -DUSE_UTF8 -Wall -W -Os -g -o $@ linenoise.c utf8.c example.c
linenoise_example: linenoise.h linenoise-ship.c linenoise-win32.c example.c
$(CC) $(CFLAGS) -o $@ linenoise-ship.c example.c

linenoise_cpp_example: linenoise.h linenoise.c
g++ -Wall -W -Os -g -o $@ linenoise.c example.c
linenoise_utf8_example: linenoise.h linenoise-ship.c linenoise-win32.c
$(CC) $(CFLAGS) -DUSE_UTF8 -o $@ linenoise-ship.c example.c

clean:
rm -f linenoise_example linenoise_utf8_example linenoise_cpp_example *.o
rm -f linenoise_example linenoise_utf8_example linenoise-ship.c *.o

ship: linenoise-ship.c

# linenoise-ship.c simplifies delivery of linenoise support
# simple copy linenoise-ship.c to linenoise.c in your application, and also linenoise.h
# - If you want win32 support, also copy linenoise-win32.c
# - If you never want to support utf-8, you can omit utf8.h and utf8.c

linenoise-ship.c: utf8.h utf8.c stringbuf.h stringbuf.c linenoise.c
cat $^ >$@
69 changes: 69 additions & 0 deletions 3rdparty/linenoise/README.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,74 @@
# Linenoise

## What's different in this fork?

- Win32 console
- full utf8 support (what about utf8 on windows)
- insert control characters
- now with multiline

## How do I include linenoise line editing support in my application?

From the Makefile:

linenoise-ship.c simplifies delivery of linenoise support

simple copy linenoise-ship.c to linenoise.c in your application, and also linenoise.h

* If you want win32 support, also copy linenoise-win32.c
* If you never want to support utf-8, you can omit utf8.h and utf8.c

To enable utf-8 support, define USE_UTF8

## Where do I get it?

Get it here: [https://github.com/msteveb/linenoise](https://github.com/msteveb/linenoise)

## Key bindings

This version supports the following key bindings:

ctrl-j, Enter Return the current line as the result
ctrl-a, Home Go to the start of the line
ctrl-e, End Go to the end of the line
ctrl-u Delete to beginning of line
ctrl-k Delete to end of line
ctrl-y Insert previously deleted chars at cursor
ctrl-l Clear screen
ctrl-c Quit
ctrl-z Exit to background (Unix only)
ctrl-h, Backspace Delete char to left of cursor
ctrl-d With empty line - return
ctrl-d, Del Delete char to right of cursor
meta-b Move word left
meta-f Move word right
ctrl-w Delete word to left
ctrl-t Transpose char and cursor and char to left of cursor, then move right
ctrl-v Insert next char as control character
ctrl-b, Left Move one char left
ctrl-f, Right Move one char right
ctrl-p, Up Move to previous history line
ctrl-n, Down Move to next history line
Page-Up Move to start of history
Page-Down Move to end of history
Tab Tab complete
ctrl-r Begin reverse incremental search

In reverse incremental search:

Normal char Add char to incremental search word
ctrl-h, Backspace Remove last char from incremental search word
ctrl-p, Up Move to previous match
ctrl-n, Down Move to next match
ctrl-g, ctrl-c Return to normal mode with empty line
Any other key Return to normal mode with the current line and process the key

--------------------------------------------------------

## Original README below

Can a line editing library be 20k lines of code?

A minimal, zero-config, BSD licensed, readline replacement.

News: linenoise now includes minimal completion support, thanks to Pieter Noordhuis (@pnoordhuis).
Expand Down
32 changes: 26 additions & 6 deletions 3rdparty/linenoise/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,70 @@

#ifndef NO_COMPLETION
void completion(const char *buf, linenoiseCompletions *lc, void *userdata) {
(void)userdata;
if (buf[0] == 'h') {
linenoiseAddCompletion(lc,"hello");
linenoiseAddCompletion(lc,"hello there");
}
}

char *hints(const char *buf, int *color, int *bold, void *userdata) {
(void)userdata;
if (!strcasecmp(buf,"hello")) {
*color = 35;
*bold = 0;
return " World";
}
return NULL;
}
#endif

int main(int argc, char *argv[]) {
const char *prompt = "hello> ";
char *line;
#ifdef HAVE_MULTILINE
/* Note: multiline support has not yet been integrated */
char *prgname = argv[0];
const char *initial;

/* Parse options, with --multiline we enable multi line editing. */
while(argc > 1) {
while(argc > 1 && argv[1][0] == '-') {
argc--;
argv++;
if (!strcmp(*argv,"--multiline")) {
linenoiseSetMultiLine(1);
printf("Multi-line mode enabled.\n");
} else if (!strcmp(*argv,"--fancyprompt")) {
prompt = "\x1b[1;31m\xf0\xa0\x8a\x9d-\xc2\xb5hello>\x1b[0m ";
} else if (!strcmp(*argv,"--prompt") && argc > 1) {
argc--;
argv++;
prompt = *argv;
} else {
fprintf(stderr, "Usage: %s [--multiline]\n", prgname);
fprintf(stderr, "Usage: %s [--multiline] [--fancyprompt] [--prompt text]\n", prgname);
exit(1);
}
}
#endif

#ifndef NO_COMPLETION
/* Set the completion callback. This will be called every time the
* user uses the <tab> key. */
linenoiseSetCompletionCallback(completion, NULL);
linenoiseSetHintsCallback(hints, NULL);
#endif

/* Load history from file. The history file is just a plain text file
* where entries are separated by newlines. */
linenoiseHistoryLoad("history.txt"); /* Load the history at startup */

initial = (argc > 1) ? argv[1] : "";

/* Now this is the main loop of the typical linenoise-based application.
* The call to linenoise() will block as long as the user types something
* and presses enter.
*
* The typed string is returned as a malloc() allocated string by
* linenoise, so the user needs to free() it. */
while((line = linenoise("hello> ")) != NULL) {
while((line = linenoiseWithInitial(prompt, initial)) != NULL) {
initial = "";
/* Do something with the string. */
if (line[0] != '\0' && line[0] != '/') {
printf("echo: '%s'\n", line);
Expand Down
Loading

0 comments on commit 8384223

Please sign in to comment.