command.c 20 KB

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