lchat

A line oriented chat front end for ii.
git clone git://git.suckless.org/lchat
Log | Files | Refs | README

commit 6d306ad60a3ed0d9e721a43fb121804974a41b6c
parent 6dd484ff2d2a417037e56a205fc5ae22adaa6335
Author: Tom Schwindl <schwindl@posteo.de>
Date:   Wed, 11 Jan 2023 09:05:39 +0100

replace "magic"-numbers with symbolic constants

Diffstat:
Mslackline.c | 10+++++-----
Mslackline_emacs.c | 16++++++++--------
Mslackline_internals.h | 17+++++++++++++++++
3 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/slackline.c b/slackline.c @@ -158,20 +158,20 @@ static void sl_default(struct slackline *sl, int key) { switch (key) { - case 27: /* Escape */ + case ESC_KEY: sl->esc = ESC; break; - case 21: + case CTRL_U: sl_reset(sl); break; - case 23: /* ctrl+w -- erase previous word */ + case CTRL_W: while (sl->rcur != 0 && isspace((unsigned char) *(sl->ptr-1))) sl_backspace(sl); while (sl->rcur != 0 && !isspace((unsigned char) *(sl->ptr-1))) sl_backspace(sl); break; - case 127: /* backspace */ - case 8: /* backspace */ + case BACKSPACE: + case VT_BACKSPACE: sl_backspace(sl); break; default: diff --git a/slackline_emacs.c b/slackline_emacs.c @@ -11,16 +11,16 @@ sl_emacs(struct slackline *sl, int key) char tmp; switch (key) { - case 27: /* Escape */ + case ESC_KEY: sl->esc = ESC; break; - case 1: /* ctrl+a -- start of line */ + case CTRL_A: sl_move(sl, HOME); break; - case 2: /* ctrl+b -- previous char */ + case CTRL_B: sl_move(sl, LEFT); break; - case 4: /* ctrl+d -- delete char in front of the cursor or exit */ + case CTRL_D: if (sl->rcur < sl->rlen) { sl_move(sl, RIGHT); sl_backspace(sl); @@ -28,19 +28,19 @@ sl_emacs(struct slackline *sl, int key) exit(EXIT_SUCCESS); } break; - case 5: /* ctrl+e -- end of line */ + case CTRL_E: sl_move(sl, END); break; - case 6: /* ctrl+f -- next char */ + case CTRL_F: sl_move(sl, RIGHT); break; - case 11: /* ctrl+k -- delete line from cursor to end */ + case CTRL_K: for (int i = sl->rlen - sl->rcur; i > 0; --i) { sl_move(sl, RIGHT); sl_backspace(sl); } break; - case 20: /* ctrl+t -- swap last two chars */ + case CTRL_T: if (sl->rcur >= 2) { tmp = *sl_postoptr(sl, sl->rcur-1); sl->buf[sl->rcur-1] = *sl_postoptr(sl, sl->rcur-2); diff --git a/slackline_internals.h b/slackline_internals.h @@ -5,6 +5,23 @@ enum direction {LEFT, RIGHT, HOME, END}; +enum { + CTRL_A = 1, + CTRL_B = 2, + CTRL_D = 4, + CTRL_E = 5, + CTRL_F = 6, + CTRL_K = 11, + CTRL_U = 21, + CTRL_T = 20, + CTRL_W = 23, + + BACKSPACE = 127, + VT_BACKSPACE = 8, + + ESC_KEY = 27 +}; + size_t sl_postobyte(struct slackline *sl, size_t pos); char *sl_postoptr(struct slackline *sl, size_t pos); void sl_backspace(struct slackline *sl);