Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add smart newline handling #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 12 additions & 30 deletions src/microrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,37 +568,19 @@ void microrl_insert_char (microrl_t * pThis, int ch)
pThis->escape = 0;
} else {
#endif
switch (ch) {
//-----------------------------------------------------
#ifdef _ENDL_CR
case KEY_CR:
new_line_handler(pThis);
break;
case KEY_LF:
break;
#elif defined(_ENDL_CRLF)
case KEY_CR:
pThis->tmpch = KEY_CR;
break;
case KEY_LF:
if (pThis->tmpch == KEY_CR)
new_line_handler(pThis);
break;
#elif defined(_ENDL_LFCR)
case KEY_LF:
pThis->tmpch = KEY_LF;
break;
case KEY_CR:
if (pThis->tmpch == KEY_LF)
new_line_handler(pThis);
break;
#else
case KEY_CR:
break;
case KEY_LF:
if (ch == KEY_CR || ch == KEY_LF) {
// Only trigger a newline if ch doen't follow its companion's
// triggering a newline.
if (pThis->last_endl == (ch == KEY_CR ? KEY_LF : KEY_CR)) {
pThis->last_endl = 0; // ignore char, but clear newline state
} else {
pThis->last_endl = ch;
new_line_handler(pThis);
break;
#endif
}
return;
}
pThis->last_endl = 0;
switch (ch) {
//-----------------------------------------------------
#ifdef _USE_COMPLETE
case KEY_HT:
Expand Down
4 changes: 1 addition & 3 deletions src/microrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ typedef struct {
char escape_seq;
char escape;
#endif
#if (defined(_ENDL_CRLF) || defined(_ENDL_LFCR))
char tmpch;
#endif
char last_endl; // either 0 or the CR or LF that just triggered a newline
#ifdef _USE_HISTORY
ring_history_t ring_hist; // history object
#endif
Expand Down