command.c 18 KB

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