command.c 23 KB

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