command.c 22 KB

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