command.c 21 KB

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