command.c 19 KB

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