lchat

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

slackline_emacs.c (1866B)


      1 /*
      2  * Copyright (c) 2022-2023 Tom Schwindl <schwindl@posteo.de>
      3  *
      4  * Permission to use, copy, modify, and distribute this software for any
      5  * purpose with or without fee is hereby granted, provided that the above
      6  * copyright notice and this permission notice appear in all copies.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 #include <stdio.h>
     18 #include <ctype.h>
     19 #include <stddef.h>
     20 #include <stdlib.h>
     21 
     22 #include "slackline.h"
     23 #include "slackline_internals.h"
     24 
     25 void
     26 sl_emacs(struct slackline *sl, int key)
     27 {
     28 	char tmp;
     29 
     30 	switch (key) {
     31 	case ESC_KEY:
     32 		sl->esc = ESC;
     33 		break;
     34 	case CTRL_A:	/* start of line */
     35 		sl_move(sl, HOME);
     36 		break;
     37 	case CTRL_B:	 /* previous char */
     38 		sl_move(sl, LEFT);
     39 		break;
     40 	case CTRL_D:	/* delete char in front of the cursor or exit */
     41 		if (sl->rcur < sl->rlen) {
     42 			sl_move(sl, RIGHT);
     43 			sl_backspace(sl);
     44 		} else {
     45 			exit(EXIT_SUCCESS);
     46 		}
     47 		break;
     48 	case CTRL_E:	/* end of line */
     49 		sl_move(sl, END);
     50 		break;
     51 	case CTRL_F:	/* next char */
     52 		sl_move(sl, RIGHT);
     53 		break;
     54 	case CTRL_K:	/* delete line from cursor to end */
     55 		for (int i = sl->rlen - sl->rcur; i > 0; --i) {
     56 			sl_move(sl, RIGHT);
     57 			sl_backspace(sl);
     58 		}
     59 		break;
     60 	case CTRL_T:	/* swap last two chars */
     61 		if (sl->rcur >= 2) {
     62 			tmp = *sl_postoptr(sl, sl->rcur-1);
     63 			sl->buf[sl->rcur-1] = *sl_postoptr(sl, sl->rcur-2);
     64 			sl->buf[sl->rcur-2] = tmp;
     65 		}
     66 		break;
     67 	default:
     68 		break;
     69 	}
     70 }