command.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /* command.c, Ait, BSD 3-Clause, Kevin Bloom, 2023,
  2. Derived from: Atto January 2017
  3. Derived from: AnthonyEditor January 93
  4. */
  5. #include "header.h"
  6. #include "termbox.h"
  7. #include "util.h"
  8. void quit() { done = 1; }
  9. void up()
  10. {
  11. curbp->b_point = lncolumn(curbp, upup(curbp, curwp, curbp->b_point),curbp->b_pcol - curwp->w_left);
  12. }
  13. void down()
  14. {
  15. curbp->b_point = lncolumn(curbp, dndn(curbp, curwp, curbp->b_point),curbp->b_pcol - curwp->w_left);
  16. }
  17. void lnbegin()
  18. {
  19. char_t *p;
  20. p = ptr(curbp, curbp->b_point);
  21. while(*(p = ptr(curbp, curbp->b_point-1)) != '\n' && p > curbp->b_buf)
  22. --curbp->b_point;
  23. if(curbp->b_point != 0 && p == curbp->b_buf)
  24. --curbp->b_point;
  25. curbp->b_pcol = 0;
  26. }
  27. void version() { msg(VERSION); }
  28. void top() { curbp->b_point = 0; }
  29. void bottom()
  30. {
  31. curbp->b_point = pos(curbp, curbp->b_ebuf);
  32. if (curbp->b_epage < pos(curbp, curbp->b_ebuf))
  33. curbp->b_reframe = 1;
  34. }
  35. void block() { curbp->b_mark = curbp->b_point; }
  36. void copy() { copy_cut(FALSE, TRUE, FALSE); }
  37. void cut() { copy_cut(TRUE, TRUE, FALSE); }
  38. void resize_terminal()
  39. {
  40. LINES = tb_height();
  41. COLS = tb_width();
  42. MSGLINE = LINES-1;
  43. one_window(curwp);
  44. }
  45. void print_to_msgline(const char *msg)
  46. {
  47. printf_tb(0, MSGLINE, TB_DEFAULT, TB_DEFAULT, msg);
  48. tb_set_cursor(strlen(msg), MSGLINE);
  49. }
  50. void quit_ask()
  51. {
  52. if (modified_buffers() > 0) {
  53. const char *msg = "Modified buffers exist; really exit (y/N) ?";
  54. print_to_msgline(msg);
  55. clrtoeol(msg, MSGLINE);
  56. if (!yesno(FALSE)) {
  57. clrtoeol("", MSGLINE);
  58. return;
  59. }
  60. }
  61. quit();
  62. }
  63. void redraw()
  64. {
  65. window_t *wp;
  66. for (wp=wheadp; wp != NULL; wp = wp->w_next)
  67. wp->w_update = TRUE;
  68. update_display();
  69. }
  70. void left()
  71. {
  72. int n = prev_utf8_char_size();
  73. while (0 < curbp->b_point && n-- > 0)
  74. --curbp->b_point;
  75. }
  76. void right()
  77. {
  78. int n = utf8_size(*ptr(curbp,curbp->b_point));
  79. while ((curbp->b_point < pos(curbp, curbp->b_ebuf)) && n-- > 0)
  80. ++curbp->b_point;
  81. }
  82. /* work out number of bytes based on first byte */
  83. int utf8_size(char_t c)
  84. {
  85. if (c >= 192 && c < 224) return 2;
  86. if (c >= 224 && c < 240) return 3;
  87. if (c >= 240 && c < 248) return 4;
  88. return 1; /* if in doubt it is 1 */
  89. }
  90. int prev_utf8_char_size()
  91. {
  92. int n;
  93. for (n=2;n<5;n++)
  94. if (-1 < curbp->b_point - n && (utf8_size(*(ptr(curbp, curbp->b_point - n))) == n))
  95. return n;
  96. return 1;
  97. }
  98. void lnend()
  99. {
  100. char_t *p;
  101. int cols = 0;
  102. lnbegin(); // reset the line so we get the right number for `cols`
  103. while(*(p = ptr(curbp, curbp->b_point)) != '\n' && curbp->b_ebuf > p) {
  104. ++curbp->b_point;
  105. cols++;
  106. }
  107. /* loop until we get to the correct column */
  108. while(cols > curwp->w_cols) {
  109. cols -= curwp->w_cols;
  110. }
  111. curbp->b_pcol = cols; // set it for column-memory
  112. }
  113. void wleft()
  114. {
  115. char_t *p;
  116. if ((!isspace(*(p = ptr(curbp, curbp->b_point))) || !is_symbol(*p)) && curbp->b_buf < p)
  117. --curbp->b_point;
  118. while ((isspace(*(p = ptr(curbp, curbp->b_point))) || is_symbol(*p)) && curbp->b_buf < p)
  119. --curbp->b_point;
  120. while (!isspace(*(p = ptr(curbp, curbp->b_point))) && !is_symbol(*p) && curbp->b_buf < p)
  121. --curbp->b_point;
  122. if(isspace(*(p = ptr(curbp, curbp->b_point))) || is_symbol(*p))
  123. ++curbp->b_point;
  124. }
  125. void wleftdelete()
  126. {
  127. currentcommand = KBD_DELETE_CHAR;
  128. // undoset(4);
  129. iblock();
  130. wleft();
  131. copy_cut(TRUE, TRUE, FALSE);
  132. }
  133. void pgdown()
  134. {
  135. curbp->b_page = curbp->b_point = upup(curbp, curwp, curbp->b_epage);
  136. while (0 < curbp->b_row--)
  137. down();
  138. curbp->b_epage = pos(curbp, curbp->b_ebuf);
  139. curbp->b_pcol = 0;
  140. }
  141. void pgup()
  142. {
  143. int i = curwp->w_rows;
  144. while (0 < --i) {
  145. curbp->b_page = upup(curbp, curwp, curbp->b_page);
  146. up();
  147. }
  148. curbp->b_pcol = 0;
  149. }
  150. void wright()
  151. {
  152. char_t *p;
  153. if ((!isspace(*(p = ptr(curbp, curbp->b_point))) || !is_symbol(*p)) && p < curbp->b_ebuf)
  154. ++curbp->b_point;
  155. while ((isspace(*(p = ptr(curbp, curbp->b_point))) || is_symbol(*p)) && p < curbp->b_ebuf)
  156. ++curbp->b_point;
  157. while (!isspace(*(p = ptr(curbp, curbp->b_point))) && !is_symbol(*p) && p < curbp->b_ebuf)
  158. ++curbp->b_point;
  159. }
  160. void wrightdelete()
  161. {
  162. currentcommand = KBD_DELETE_CHAR;
  163. // undoset(4);
  164. iblock();
  165. wright();
  166. copy_cut(TRUE, TRUE, FALSE);
  167. }
  168. void insert()
  169. {
  170. assert(curbp->b_gap <= curbp->b_egap);
  171. // if(lastcommand != KBD_INSERT)
  172. // undoset(1);
  173. if (curbp->b_gap == curbp->b_egap && !growgap(curbp, CHUNK))
  174. return;
  175. curbp->b_point = movegap(curbp, curbp->b_point);
  176. // if(!undoset_flag)
  177. // undoset(1);
  178. /* overwrite if mid line, not EOL or EOF, CR will insert as normal */
  179. if ((curbp->b_flags & B_OVERWRITE) && *input != '\r' && *(ptr(curbp, curbp->b_point)) != '\n' && curbp->b_point < pos(curbp,curbp->b_ebuf) ) {
  180. *(ptr(curbp, curbp->b_point)) = *input;
  181. if (curbp->b_point < pos(curbp, curbp->b_ebuf))
  182. ++curbp->b_point;
  183. } else {
  184. *curbp->b_gap++ = *input == '\r' ? '\n' : *input;
  185. curbp->b_point = pos(curbp, curbp->b_egap);
  186. // force reframe if scrolled off bottom of screen and at EOF
  187. if (curbp->b_point == pos(curbp, curbp->b_ebuf) && curbp->b_point >= curbp->b_epage &&
  188. curwp->w_rows == curwp->w_row)
  189. curbp->b_reframe = 1;
  190. }
  191. curbp->b_flags |= B_MODIFIED;
  192. undoset_flag = TRUE;
  193. currentcommand = KBD_INSERT;
  194. }
  195. void insert_str()
  196. {
  197. int len = strlen((const char *)input);
  198. assert(curbp->b_gap <= curbp->b_egap);
  199. undoset(1, FALSE);
  200. if (curbp->b_gap == curbp->b_egap && !growgap(curbp, CHUNK))
  201. return;
  202. curbp->b_point = movegap(curbp, curbp->b_point);
  203. // if(!undoset_flag)
  204. // undoset(1);
  205. /* overwrite if mid line, not EOL or EOF, CR will insert as normal */
  206. if ((curbp->b_flags & B_OVERWRITE) && input[0] != '\r' && *(ptr(curbp, curbp->b_point)) != '\n' && curbp->b_point < pos(curbp,curbp->b_ebuf) ) {
  207. *(ptr(curbp, curbp->b_point)) = *input;
  208. if (curbp->b_point < pos(curbp, curbp->b_ebuf))
  209. ++curbp->b_point;
  210. } else {
  211. for(int i = 0; i < len; i++) {
  212. *curbp->b_gap++ = input[i] == '\r' ? '\n' : input[i];
  213. }
  214. curbp->b_point = pos(curbp, curbp->b_egap);
  215. // force reframe if scrolled off bottom of screen and at EOF
  216. if (curbp->b_point == pos(curbp, curbp->b_ebuf) && curbp->b_point >= curbp->b_epage &&
  217. curwp->w_rows == curwp->w_row)
  218. curbp->b_reframe = 1;
  219. }
  220. curbp->b_flags |= B_MODIFIED;
  221. undoset_flag = TRUE;
  222. }
  223. void insert_unicode()
  224. {
  225. int len = strlen((const char *)unicode_buf);
  226. assert(curbp->b_gap <= curbp->b_egap);
  227. undoset(INSERT, lastcommand == KBD_INSERT);
  228. if (curbp->b_gap == curbp->b_egap && !growgap(curbp, CHUNK))
  229. return;
  230. curbp->b_point = movegap(curbp, curbp->b_point);
  231. // if(!undoset_flag)
  232. // undoset(1);
  233. /* overwrite if mid line, not EOL or EOF, CR will insert as normal */
  234. for(int i = 0; i < len; i++) {
  235. *curbp->b_gap++ = unicode_buf[i];
  236. }
  237. curbp->b_point = pos(curbp, curbp->b_egap);
  238. // force reframe if scrolled off bottom of screen and at EOF
  239. if (curbp->b_point == pos(curbp, curbp->b_ebuf) && curbp->b_point >= curbp->b_epage &&
  240. curwp->w_rows == curwp->w_row)
  241. curbp->b_reframe = 1;
  242. curbp->b_flags |= B_MODIFIED;
  243. undoset_flag = TRUE;
  244. unicode_buf[0] = '\0';
  245. currentcommand = KBD_INSERT;
  246. }
  247. void backsp()
  248. {
  249. // if(lastcommand != KBD_DELETE_CHAR)
  250. undoset(3, lastcommand == KBD_DELETE_CHAR);
  251. if(*ptr(curbp, curbp->b_point - 1) == '\n')
  252. curbp->b_line--;
  253. curbp->b_point = movegap(curbp, curbp->b_point);
  254. if (curbp->b_buf < curbp->b_gap) {
  255. curbp->b_gap -= prev_utf8_char_size();
  256. curbp->b_flags |= B_MODIFIED;
  257. }
  258. curbp->b_point = pos(curbp, curbp->b_egap);
  259. currentcommand = KBD_DELETE_CHAR;
  260. }
  261. void delete()
  262. {
  263. // if(lastcommand != KBD_DELETE_CHAR)
  264. undoset(2, lastcommand == KBD_DELETE_CHAR);
  265. curbp->b_point = movegap(curbp, curbp->b_point);
  266. if (curbp->b_egap < curbp->b_ebuf) {
  267. curbp->b_egap += utf8_size(*curbp->b_egap);
  268. curbp->b_point = pos(curbp, curbp->b_egap);
  269. curbp->b_flags |= B_MODIFIED;
  270. }
  271. currentcommand = KBD_DELETE_CHAR;
  272. }
  273. void gotoline()
  274. {
  275. int line;
  276. point_t p;
  277. if (getinput("Goto line: ", temp, STRBUF_S, F_CLEAR, FALSE)) {
  278. line = atoi(temp);
  279. p = line_to_point(line);
  280. if (p != -1) {
  281. curbp->b_point = p;
  282. curbp->b_pcol = 0;
  283. if (curbp->b_epage < pos(curbp, curbp->b_ebuf)) curbp->b_reframe = 1;
  284. curwp->w_update = TRUE;
  285. msg("Line %d", line);
  286. } else {
  287. msg("Line %d, not found", line);
  288. }
  289. }
  290. }
  291. void get_current_path(char *cur_path)
  292. {
  293. int cutoff = 0;
  294. for(int i = strlen(curbp->b_fname) - 1; i > -1; i--) {
  295. if(curbp->b_fname[i] == '/') {
  296. cutoff = i;
  297. break;
  298. }
  299. }
  300. for(int i = 0; i <= cutoff; i++)
  301. cur_path[i] = curbp->b_fname[i];
  302. cur_path[cutoff+1] = '\0';
  303. }
  304. void insertfile()
  305. {
  306. char cur_path[NAME_MAX] = "\0";
  307. if(curbp->b_path) {
  308. get_current_path(cur_path);
  309. strcpy(temp, cur_path);
  310. }
  311. else
  312. strcpy(temp, editor_dir);
  313. if (getfilename("Insert file: ", temp, NAME_MAX))
  314. (void)insert_file(temp, TRUE);
  315. }
  316. void readfile()
  317. {
  318. buffer_t *bp;
  319. char cur_path[NAME_MAX];
  320. if(curbp->b_path) {
  321. get_current_path(cur_path);
  322. strcpy(temp, cur_path);
  323. }
  324. else
  325. strcpy(temp, editor_dir);
  326. int result = getfilename("Find file: ", (char*)temp, NAME_MAX);
  327. if (result) {
  328. bp = find_buffer(temp, TRUE);
  329. disassociate_b(curwp);
  330. curbp = bp;
  331. associate_b2w(curbp, curwp);
  332. /* load the file if not already loaded */
  333. if (bp != NULL && bp->b_fname[0] == '\0') {
  334. if (!load_file(temp)) {
  335. msg("New file %s", temp);
  336. }
  337. strncpy(curbp->b_fname, temp, NAME_MAX);
  338. curbp->b_fname[NAME_MAX] = '\0'; /* truncate if required */
  339. }
  340. }
  341. }
  342. void savebuffer()
  343. {
  344. const char *message = "No newline at the end of file, add one (Y/n) ?";
  345. if(curbp->b_flags & B_MODIFIED) {
  346. /* move the gap to point 0 so that the ebuf is updated. */
  347. (void) movegap(curbp, 0);
  348. if(*(curbp->b_ebuf - 1) != '\n') {
  349. print_to_msgline(message);
  350. clrtoeol(message, MSGLINE);
  351. if (yesno(TRUE)) {
  352. clrtoeol("", MSGLINE);
  353. *curbp->b_ebuf++ = '\n';
  354. }
  355. }
  356. if (curbp->b_fname[0] != '\0') {
  357. save(curbp->b_fname);
  358. return;
  359. } else {
  360. writefile();
  361. }
  362. } else {
  363. msg("(No changes need to be saved.)");
  364. }
  365. }
  366. void writefile()
  367. {
  368. const char *message = "Write file: ";
  369. strncpy(temp, curbp->b_fname, NAME_MAX);
  370. if (getinput((char *)message, temp, NAME_MAX, F_NONE, FALSE))
  371. if (save(temp) == TRUE)
  372. strncpy(curbp->b_fname, temp, NAME_MAX);
  373. clrtoeol(message, MSGLINE);
  374. }
  375. void killbuffer()
  376. {
  377. buffer_t *kill_bp = curbp;
  378. buffer_t *bp;
  379. int bcount = count_buffers();
  380. const char *message = "Discard changes (y/N) ?";
  381. /* do nothing if only buffer left is the scratch buffer */
  382. if (bcount == 1 && 0 == strcmp(get_buffer_name(curbp), "*scratch*"))
  383. return;
  384. if (curbp->b_flags & B_MODIFIED) {
  385. print_to_msgline(message);
  386. clrtoeol(message, MSGLINE);
  387. if (!yesno(FALSE))
  388. return;
  389. }
  390. if (bcount == 1) {
  391. /* create a scratch buffer */
  392. bp = find_buffer("*scratch*", TRUE);
  393. strncpy(bp->b_bname, "*scratch*", STRBUF_S);
  394. bp->b_path = FALSE;
  395. }
  396. next_buffer();
  397. assert(kill_bp != curbp);
  398. delete_buffer(kill_bp);
  399. }
  400. void iblock()
  401. {
  402. block();
  403. msg("Mark set");
  404. }
  405. void unmark()
  406. {
  407. curbp->b_pmark = curbp->b_mark;
  408. curbp->b_mark = NOMARK;
  409. msg("Mark removed");
  410. }
  411. void toggle_overwrite_mode() {
  412. if (curbp->b_flags & B_OVERWRITE)
  413. curbp->b_flags &= ~B_OVERWRITE;
  414. else
  415. curbp->b_flags |= B_OVERWRITE;
  416. }
  417. void killtoeol()
  418. {
  419. if (curbp->b_point == pos(curbp, curbp->b_ebuf))
  420. return; /* do nothing if at end of file */
  421. if (*(ptr(curbp, curbp->b_point)) == 0xa) {
  422. delete(); /* delete CR if at start of empty line */
  423. } else {
  424. // undoset(4);
  425. curbp->b_mark = curbp->b_point;
  426. lnend();
  427. if (curbp->b_mark != curbp->b_point) copy_cut(TRUE, TRUE, FALSE);
  428. }
  429. }
  430. void copy_cut(int cut, int displaymsg, int internal)
  431. {
  432. char_t *p;
  433. /* if no mark or point == marker, nothing doing */
  434. if (curbp->b_mark == NOMARK || curbp->b_point == curbp->b_mark)
  435. return;
  436. if (scrap != NULL) {
  437. free(scrap);
  438. scrap = NULL;
  439. }
  440. if(cut && !internal)
  441. undoset(4, FALSE);
  442. if (curbp->b_point < curbp->b_mark) {
  443. /* point above marker: move gap under point, region = marker - point */
  444. (void) movegap(curbp, curbp->b_point);
  445. p = ptr(curbp, curbp->b_point);
  446. nscrap = curbp->b_mark - curbp->b_point;
  447. if(cut && currentcommand == KBD_DELETE_CHAR)
  448. for(point_t pt = curbp->b_mark-1; pt > curbp->b_point; pt--) {
  449. if(*ptr(curbp, pt) == '\n')
  450. curbp->b_line--;
  451. }
  452. } else {
  453. /* if point below marker: move gap under marker, region = point - marker */
  454. (void) movegap(curbp, curbp->b_mark);
  455. p = ptr(curbp, curbp->b_mark);
  456. nscrap = curbp->b_point - curbp->b_mark;
  457. if (cut && currentcommand != KBD_DELETE_CHAR)
  458. for(point_t pt = curbp->b_mark; pt < curbp->b_point; pt++) {
  459. if(*ptr(curbp, pt) == '\n')
  460. curbp->b_line--;
  461. }
  462. }
  463. if ((scrap = (char_t*) malloc(nscrap)) == NULL && displaymsg) {
  464. msg("No more memory available.");
  465. } else {
  466. (void) memcpy(scrap, p, nscrap * sizeof (char_t));
  467. if (cut) {
  468. curbp->b_egap += nscrap; /* if cut expand gap down */
  469. curbp->b_point = pos(curbp, curbp->b_egap); /* set point to after region */
  470. curbp->b_flags |= B_MODIFIED;
  471. if(displaymsg)
  472. msg("%ld bytes cut.", nscrap);
  473. currentcommand = KBD_CUT;
  474. } else {
  475. if(displaymsg)
  476. msg("%ld bytes copied.", nscrap);
  477. }
  478. curbp->b_mark = NOMARK; /* unmark */
  479. }
  480. }
  481. void paste_internal(int internal)
  482. {
  483. int new_rows = 0;
  484. if(curbp->b_flags & B_OVERWRITE)
  485. return;
  486. if (nscrap <= 0) {
  487. msg("Scrap is empty. Nothing to paste.");
  488. } else if (nscrap < curbp->b_egap - curbp->b_gap || growgap(curbp, nscrap)) {
  489. if(!internal)
  490. undoset(5, FALSE);
  491. curbp->b_point = movegap(curbp, curbp->b_point);
  492. memcpy(curbp->b_gap, scrap, nscrap * sizeof (char_t));
  493. curbp->b_gap += nscrap;
  494. curbp->b_point = pos(curbp, curbp->b_egap);
  495. curbp->b_flags |= B_MODIFIED;
  496. for(int i = 0; scrap[i] != '\0'; i++) {
  497. if(scrap[i] == '\n')
  498. new_rows++;
  499. }
  500. if (curbp->b_point >= curbp->b_epage)
  501. curbp->b_reframe = 1;
  502. }
  503. }
  504. void paste()
  505. {
  506. paste_internal(FALSE);
  507. }
  508. void showpos()
  509. {
  510. int current, lastln;
  511. point_t end_p = pos(curbp, curbp->b_ebuf);
  512. get_line_stats(&current, &lastln, curbp);
  513. if (curbp->b_point == end_p) {
  514. msg("[EOB] Line = %d/%d Point = %d/%d", current, lastln,
  515. curbp->b_point, ((curbp->b_ebuf - curbp->b_buf) - (curbp->b_egap - curbp->b_gap)));
  516. } else {
  517. char c = unctrl(*(ptr(curbp, curbp->b_point)));
  518. msg("Char = %c 0x%x Line = %d/%d Point = %d/%d", c, *(ptr(curbp, curbp->b_point)),
  519. current, lastln,
  520. curbp->b_point, ((curbp->b_ebuf - curbp->b_buf) - (curbp->b_egap - curbp->b_gap)));
  521. }
  522. }
  523. /* Delete whitespace between non-whitespace */
  524. void deletewhitespacebetween()
  525. {
  526. char_t *p;
  527. while (isspace(*(p = ptr(curbp, curbp->b_point - 1))) && curbp->b_buf < p && *p != '\n')
  528. backsp();
  529. while (isspace(*(p = ptr(curbp, curbp->b_point))) && curbp->b_buf < p && *p != '\n')
  530. delete();
  531. }
  532. void insertnewlinebelow()
  533. {
  534. char_t newline[2];
  535. newline[0] = '\n';
  536. newline[1] = '\0';
  537. input = newline;
  538. undoset(1, lastcommand == KBD_INSERT);
  539. insert();
  540. up();
  541. currentcommand = KBD_INSERT;
  542. }
  543. void insertnewline()
  544. {
  545. point_t point;
  546. char_t *p, *space = NULL, *str;
  547. int spaces = 0, i;
  548. point = segstart(curbp, curwp, lnstart(curbp, curbp->b_point), curbp->b_point);
  549. while(isspace(*(p = ptr(curbp, point))) && *p != '\n' && curwp->w_col != 0) {
  550. if(spaces == 0) {
  551. space = p;
  552. }
  553. if(*p != '\n') {
  554. spaces++;
  555. point++;
  556. }
  557. }
  558. str = (char_t *) malloc(sizeof(char_t)*spaces+2);
  559. str[0] = '\n';
  560. for(i = 0; i < spaces; i++) {
  561. str[i+1] = *space;
  562. }
  563. str[i+1] = '\0';
  564. input = str;
  565. undoset(1, lastcommand == KBD_INSERT);
  566. insert_str();
  567. curbp->b_pcol = spaces;
  568. currentcommand = KBD_INSERT;
  569. free(str);
  570. }
  571. void inserttab()
  572. {
  573. input = (char_t *)"\t";
  574. undoset(1, FALSE);
  575. insert();
  576. }
  577. void inserttabasspace()
  578. {
  579. input = (char_t *)" ";
  580. undoset(1, FALSE);
  581. insert_str();
  582. }
  583. void suspend()
  584. {
  585. tb_shutdown();
  586. raise(SIGTSTP);
  587. }
  588. void transpose()
  589. {
  590. char_t *cur = ptr(curbp, curbp->b_point);
  591. char_t *prev = ptr(curbp, curbp->b_point-1);
  592. char_t replace[3];
  593. point_t mark = curbp->b_mark;
  594. replace[0] = *cur;
  595. replace[1] = *prev;
  596. replace[2] = '\0';
  597. curbp->b_point--;
  598. curbp->b_mark = curbp->b_point + 2;
  599. undoset(REPLACE, 2);
  600. curbp->b_mark = mark;
  601. curbp->b_point++;
  602. memcpy(ptr(curbp, curbp->b_point-1), replace, 2 * sizeof (char_t));
  603. curbp->b_flags |= B_MODIFIED;
  604. }
  605. /* Delete a word but don't display any messages. */
  606. void deleteword(int dir) {
  607. block();
  608. if(dir)
  609. wright();
  610. else
  611. wleft();
  612. copy_cut(TRUE, FALSE, TRUE);
  613. }
  614. /* Transpose words and put scrap back to how it was. */
  615. void transposeword()
  616. {
  617. char_t *current_scrap;
  618. int n_scrap = nscrap;
  619. point_t mark = curbp->b_mark, epoint, point;
  620. /* copy the current scrap */
  621. current_scrap = (char_t*) malloc(nscrap);
  622. (void) memcpy(current_scrap, scrap, nscrap * sizeof (char_t));
  623. /* Find all the key points for the undo */
  624. wright();
  625. epoint = curbp->b_point;
  626. wleft();
  627. wleft();
  628. curbp->b_mark = epoint;
  629. point = curbp->b_point;
  630. undoset(REPLACE, curbp->b_mark - point);
  631. /* Cut the word to the left*/
  632. curbp->b_mark = point;
  633. curbp->b_point = point;
  634. wright();
  635. copy_cut(TRUE, FALSE, TRUE);
  636. /* paste the left word */
  637. right();
  638. paste_internal(TRUE);
  639. /* cut the right word */
  640. curbp->b_mark = curbp->b_point;
  641. wright();
  642. copy_cut(TRUE, FALSE, TRUE);
  643. wleft();
  644. /* paste the right word */
  645. left();
  646. paste_internal(TRUE);
  647. /* Put it all back together */
  648. if (scrap != NULL) {
  649. free(scrap);
  650. scrap = NULL;
  651. }
  652. nscrap = n_scrap;
  653. scrap = (char_t*) malloc(nscrap);
  654. (void) memcpy(scrap, current_scrap, nscrap * sizeof (char_t));
  655. curbp->b_mark = mark;
  656. }
  657. void lowercaseword()
  658. {
  659. char_t *p, *word;
  660. char_t c[2];
  661. point_t sword, eword;
  662. int olast = lastcommand;
  663. while ((isspace(*(p = ptr(curbp, curbp->b_point))) || is_symbol(*p)) && p < curbp->b_ebuf)
  664. ++curbp->b_point;
  665. sword = curbp->b_point;
  666. wright();
  667. eword = curbp->b_point;
  668. word = (char_t *) malloc(sizeof(char_t)*(eword - sword));
  669. curbp->b_point = sword;
  670. lastcommand = KBD_DELETE_CHAR;
  671. for(int i = sword, k = 0; i < eword; i++, k++) {
  672. word[k] = *ptr(curbp, curbp->b_point);
  673. delete();
  674. }
  675. lastcommand = olast;
  676. for(int i = sword, k = 0; i < eword; i++, k++) {
  677. c[0] = tolower(word[k]);
  678. c[1] = '\0';
  679. input = c;
  680. undoset(INSERT, i != 0);
  681. insert();
  682. }
  683. free(word);
  684. }
  685. void capitalizeword()
  686. {
  687. char_t *p;
  688. while (isspace(*(p = ptr(curbp, curbp->b_point))) && p < curbp->b_ebuf)
  689. ++curbp->b_point;
  690. p = ptr(curbp, curbp->b_point);
  691. char_t c[1];
  692. c[0] = toupper(*p);
  693. input = c;
  694. delete();
  695. undoset(INSERT, FALSE);
  696. insert();
  697. if(isspace(*(p = ptr(curbp, curbp->b_point+1))) || is_symbol(*p))
  698. curbp->b_point++;
  699. else
  700. wright();
  701. }
  702. void uppercaseword()
  703. {
  704. char_t *p, *word;
  705. char_t c[2];
  706. point_t sword, eword;
  707. int olast = lastcommand;
  708. while ((isspace(*(p = ptr(curbp, curbp->b_point))) || is_symbol(*p)) && p < curbp->b_ebuf)
  709. ++curbp->b_point;
  710. sword = curbp->b_point;
  711. wright();
  712. eword = curbp->b_point;
  713. word = (char_t *) malloc(sizeof(char_t)*(eword - sword));
  714. curbp->b_point = sword;
  715. lastcommand = KBD_DELETE_CHAR;
  716. for(int i = sword, k = 0; i < eword; i++, k++) {
  717. word[k] = *ptr(curbp, curbp->b_point);
  718. delete();
  719. }
  720. lastcommand = olast;
  721. for(int i = sword, k = 0; i < eword; i++, k++) {
  722. c[0] = toupper(word[k]);
  723. c[1] = '\0';
  724. input = c;
  725. undoset(INSERT, i != 0);
  726. insert();
  727. }
  728. free(word);
  729. }
  730. /* type = 0, zap
  731. type = 1, jump
  732. */
  733. /* TODO: Throw error when putting non-char in.
  734. */
  735. void gotochar(int type)
  736. {
  737. char_t *p;
  738. int c;
  739. struct tb_event ev;
  740. char *prompt = type == 0 ? "Zap to Char: " : "Jump to Char: ";
  741. if(character[0] == '\0') {
  742. display_prompt_and_response(prompt, character);
  743. tb_present();
  744. if(tb_poll_event(&ev) != TB_OK) return;
  745. if(!ev.mod)
  746. c = ev.ch;
  747. else
  748. c = ev.key;
  749. /* Ignore all control keys other than C-g and ESC*/
  750. if (c < 32 && c != TB_KEY_CTRL_G && c != TB_KEY_ESC)
  751. return;
  752. if(c == TB_KEY_CTRL_G || c == TB_KEY_ESC)
  753. return;
  754. else
  755. character[0] = c;
  756. display_prompt_and_response(prompt, character);
  757. tb_present();
  758. }
  759. if(type == 0) {
  760. block();
  761. }
  762. if(*ptr(curbp, curbp->b_point) == character[0]) {
  763. if(negated)
  764. left();
  765. else
  766. right();
  767. }
  768. while (*(p = ptr(curbp, curbp->b_point)) != character[0] && p < curbp->b_ebuf && p > curbp->b_buf) {
  769. if(negated)
  770. left();
  771. else
  772. right();
  773. }
  774. if(type == 0 && !negated)
  775. right();
  776. if(type == 0)
  777. copy_cut(TRUE, FALSE, FALSE);
  778. negated = FALSE;
  779. tb_set_cursor(0, MSGLINE);
  780. clrtoeol("", MSGLINE);
  781. }
  782. void zaptochar()
  783. {
  784. gotochar(0);
  785. }
  786. void negated_zaptochar()
  787. {
  788. negated = TRUE;
  789. gotochar(0);
  790. }
  791. void jumptochar()
  792. {
  793. gotochar(1);
  794. }
  795. void negated_jumptochar()
  796. {
  797. negated = TRUE;
  798. gotochar(1);
  799. }
  800. void poptomark()
  801. {
  802. if(curbp->b_mark > -1)
  803. curbp->b_point = curbp->b_mark;
  804. else
  805. curbp->b_point = curbp->b_pmark;
  806. }
  807. void universal_argument_load()
  808. {
  809. universal_argument++;
  810. msg("C-u %d", universal_argument);
  811. }
  812. void numeric_argument_load()
  813. {
  814. numeric_argument = (numeric_argument * 10) + atoi((const char *)&input_char);
  815. msg("C-u %d", numeric_argument);
  816. }
  817. void back_to_indentation()
  818. {
  819. char_t *p;
  820. while (isspace(*(p = ptr(curbp, curbp->b_point))) && p < curbp->b_ebuf)
  821. ++curbp->b_point;
  822. }
  823. void negate()
  824. {
  825. negated = !negated;
  826. msg("C-u -");
  827. }
  828. void forward_bracket()
  829. {
  830. point_t p, eol;
  831. int col = 0;
  832. if((p = find_matching_bracket(curbp, curwp, 1, FALSE)) >= 0)
  833. curbp->b_point = curbp->b_mark == NOMARK ? p : p + 1;
  834. /* Make sure the column memory updates to the new column */
  835. eol = lnstart(curbp, curbp->b_point);
  836. for(p = curbp->b_point; p > eol; p -= utf8_size(*ptr(curbp,p)))
  837. col++;
  838. curbp->b_pcol = col;
  839. }
  840. void backward_bracket()
  841. {
  842. point_t p, eol;
  843. int col = 0;
  844. if((p = find_matching_bracket(curbp, curwp, -1, FALSE)) >= 0) {
  845. curbp->b_point = p;
  846. if(curbp->b_mark != NOMARK)
  847. curbp->b_mark++;
  848. }
  849. /* Make sure the column memory updates to the new column */
  850. eol = lnstart(curbp, curbp->b_point);
  851. for(p = curbp->b_point; p > eol; p -= utf8_size(*ptr(curbp,p)))
  852. col++;
  853. curbp->b_pcol = col;
  854. }
  855. void start_kbd_macro()
  856. {
  857. record_input = TRUE;
  858. for(int i = 0; i < record_buffer_index; i++) {
  859. memset(&record_buffer[i], 0, sizeof(record_buffer[i]));
  860. }
  861. record_buffer_index = 0;
  862. msg("Started keyboard macro...");
  863. }
  864. void end_kbd_macro()
  865. {
  866. record_input = FALSE;
  867. msg("Ended keyboard macro.");
  868. }
  869. void run_kbd_macro()
  870. {
  871. if(numeric_argument > 0)
  872. numeric_argument--;
  873. execute_kbd_macro = TRUE;
  874. }
  875. void open_file_from_shell()
  876. {
  877. get_popen_data(1);
  878. }
  879. void insert_from_shell()
  880. {
  881. get_popen_data(0);
  882. }