edit.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. // Simple text editor
  2. // NOTE: this uses more memory than is allowed in FPGC7, so update this when new BDOS memory map.
  3. // Also, code should be refactored.
  4. /* global structure of program:
  5. - check first argument, exit if none
  6. - read file from first argument into mem
  7. - using a rectangular buffer (uninitialized 2d array, pointer to FILE_MEMORY_ADDR) with a max linewidth
  8. - while reading file, fill line until newline or MAX_LINE_WIDTH (if max, then error and exit)
  9. - add zeros until max_line_width after newline character
  10. - if current line number >= MAX_LINES: error out of mem and exit
  11. - use a >8bit code for indicating end of file, so print can stop
  12. - keep track of cursor x and y, which corresponds to line and pos in mem buffer
  13. - need for fast (asm!) memory move operations for inserting and deleting line based stuff (within the same line does not matter that much)
  14. - need for fast (asm!) print function which prints TEXT_WINDOW_HEIGHT lines with an xscroll offset (with palette indication that a line extends right)
  15. - end of file should have a special palette color
  16. - header line as first line with filename, mode, etc (like nano)
  17. - keep track of window (start) cursor (only y, line number)
  18. - when cursor y < window cursor, window cursor --, if y > window cursor + TEXT_WINDOW_HEIGHT, window cursor++ (check on start and end of file boundaries)
  19. - after each operation, print from window start
  20. - print cursor on top of character, by inverting the palette
  21. - escape for menu
  22. - save
  23. - function to
  24. - exit
  25. */
  26. /* future possible features that are not that hard to implement
  27. - use control key (which should be implemented in hid fifo first!)
  28. - then, ctrl s and other stuff can be implemented
  29. - selection mode (set start and end x,y), use a color palette to indicate selection
  30. - copy or delete (or cut, by copy and then delete) the selection into a separate buffer (check on bounds!)
  31. - paste by inserting from the buffer
  32. - text search which selects the matches one by one (different mode again)
  33. - tab inserts 2 spaces, maybe stops at even x for alignment?
  34. - insert mode that overwrites characters instead of moving them
  35. */
  36. #define word char
  37. #include "lib/math.c"
  38. #include "lib/sys.c"
  39. #include "lib/gfx.c"
  40. #include "lib/stdlib.c"
  41. #include "lib/brfs.c"
  42. #define FILE_MEMORY_ADDR 0x480000 // location of file content
  43. #define MAX_LINE_WIDTH 240 // max width of line in mem buffer, multiple of 40 (screen width)
  44. #define MAX_LINES 8192 // maximum number of lines to prevent out of memory writes (8192*256*4 = 8MiB)
  45. #define TEXT_WINDOW_HEIGHT 20 // number of lines to print on screen
  46. #define SCREEN_WIDTH 40
  47. #define SCREEN_HEIGHT 25
  48. // Palettes
  49. #define PALETTE_CURSOR 1
  50. #define PALETTE_NEWLINE 2
  51. #define PALETTE_EOF 3
  52. #define PALETTE_SELECTED 4
  53. #define PALETTE_HEADER 5
  54. // HID
  55. #define ARROW_LEFT 256
  56. #define ARROW_RIGHT 257
  57. #define ARROW_UP 258
  58. #define ARROW_DOWN 259
  59. #define BTN_INSERT 260
  60. #define BTN_HOME 261
  61. #define BTN_PAGEUP 262
  62. #define BTN_END 263
  63. #define BTN_PAGEDOWN 264
  64. char headerText[SCREEN_WIDTH]; // text on header (first line)
  65. // Buffer for file contents in memory
  66. char (*mem)[MAX_LINE_WIDTH] = (char (*)[MAX_LINE_WIDTH]) FILE_MEMORY_ADDR; // 2d array containing all lines of the input file
  67. // Buffer for file reading
  68. #define FBUF_LEN 4096
  69. #define EOF -1
  70. word lastLineNumber = 0;
  71. // Cursors
  72. word windowYscroll = 0; // vertical line offset for drawing window
  73. word windowXscroll = 0; // horizontal position offset for drawing window
  74. word userCursorX = 0; // char position within line, of user cursor
  75. word userCursorY = 0; // line of user cursor
  76. // Write mem to file
  77. void writeMemToFile(char* absolute_path)
  78. {
  79. // Open file
  80. word fd = fs_open(absolute_path);
  81. if (fd == -1)
  82. {
  83. bdos_println("File not found");
  84. exit();
  85. }
  86. word y;
  87. for (y = 0; y <= lastLineNumber; y++)
  88. {
  89. word lineEndPos = 0;
  90. while (mem[y][lineEndPos] != '\n' && mem[y][lineEndPos] != EOF)
  91. {
  92. lineEndPos++;
  93. if (lineEndPos == MAX_LINE_WIDTH)
  94. {
  95. exitRoutine();
  96. bdos_print("E: could not find end of line\n");
  97. fs_close(fd);
  98. exit();
  99. }
  100. }
  101. if (mem[y][lineEndPos] == EOF)
  102. {
  103. fs_write(fd, mem[y], lineEndPos);
  104. fs_close(fd);
  105. return;
  106. }
  107. lineEndPos++; // include the newline token
  108. fs_write(fd, mem[y], lineEndPos);
  109. }
  110. fs_close(fd);
  111. }
  112. // returns the current char at cursor within the opened file (EOF if end of file)
  113. // increments the cursor
  114. word fgetc(word fd, word filesize)
  115. {
  116. word cursor = fs_getcursor(fd);
  117. if (cursor == filesize)
  118. {
  119. return EOF;
  120. }
  121. char c;
  122. fs_read(fd, &c, 1);
  123. return c;
  124. }
  125. // reads a line from the input file
  126. word readFileLine(word fd, word filesize)
  127. {
  128. char c = fgetc(fd, filesize);
  129. char cprev = c;
  130. word currentChar = 0;
  131. // stop on EOF or newline or max line width reached
  132. while (c != EOF && c != '\n' && currentChar < (MAX_LINE_WIDTH-1))
  133. {
  134. mem[lastLineNumber][currentChar] = c;
  135. currentChar++;
  136. cprev = c;
  137. c = fgetc(fd, filesize);
  138. }
  139. // error when line was too long
  140. if (c != EOF && c != '\n' && currentChar >= (MAX_LINE_WIDTH-1))
  141. {
  142. bdos_print("E: line is too long\n");
  143. fs_close(fd);
  144. exit();
  145. }
  146. mem[lastLineNumber][currentChar] = c; // add EOF or \n
  147. // append line with zeros
  148. while (currentChar < (MAX_LINE_WIDTH-1))
  149. {
  150. currentChar++;
  151. char fillerChar = 0;
  152. if (c == EOF)
  153. {
  154. fillerChar = EOF;
  155. }
  156. mem[lastLineNumber][currentChar] = fillerChar;
  157. }
  158. return c;
  159. }
  160. void readInputFile(char* absolute_path, word filesize)
  161. {
  162. // Open file
  163. word fd = fs_open(absolute_path);
  164. if (fd == -1)
  165. {
  166. bdos_println("File not found");
  167. exit();
  168. }
  169. while (readFileLine(fd, filesize) != EOF)
  170. {
  171. if (lastLineNumber >= MAX_LINES)
  172. {
  173. fs_close(fd);
  174. bdos_print("E: File too large\n");
  175. exit();
  176. }
  177. else
  178. {
  179. lastLineNumber++;
  180. }
  181. }
  182. fs_close(fd);
  183. }
  184. // Applies window to mem and prints on screen (using window plane)
  185. void printWindow()
  186. {
  187. printHeader();
  188. char* vramWindowpattern = (char*) 0xC01420;
  189. word vramPaletteOffset = 2048;
  190. word lastLineFound = 0;
  191. word x, y;
  192. for (y = 0; y < SCREEN_HEIGHT-1; y++) // leave first line open
  193. {
  194. for (x = 0; x < SCREEN_WIDTH; x++)
  195. {
  196. word c = mem[y + windowYscroll][x + windowXscroll];
  197. if (c == '\n')
  198. {
  199. word vramOffset = GFX_WindowPosFromXY(x, y+1);
  200. *(vramWindowpattern + vramOffset) = 7;
  201. *(vramWindowpattern + vramOffset + vramPaletteOffset) = PALETTE_NEWLINE;
  202. }
  203. else if (c == EOF)
  204. {
  205. // print empty space if EOF is already drawn
  206. if (lastLineFound)
  207. {
  208. word vramOffset = GFX_WindowPosFromXY(x, y+1);
  209. *(vramWindowpattern + vramOffset) = 0;
  210. *(vramWindowpattern + vramOffset + vramPaletteOffset) = 0;
  211. }
  212. else
  213. {
  214. word vramOffset = GFX_WindowPosFromXY(x, y+1);
  215. *(vramWindowpattern + vramOffset) = 4;
  216. *(vramWindowpattern + vramOffset + vramPaletteOffset) = PALETTE_EOF;
  217. lastLineFound = 1; // notify, but continue rendering the line
  218. }
  219. }
  220. else
  221. {
  222. word vramOffset = GFX_WindowPosFromXY(x, y+1);
  223. *(vramWindowpattern + vramOffset) = c;
  224. *(vramWindowpattern + vramOffset + vramPaletteOffset) = 0;
  225. }
  226. }
  227. if (lastLineFound)
  228. {
  229. return;
  230. }
  231. }
  232. }
  233. // Remove cursor from screen (bg plane)
  234. void clearCursor()
  235. {
  236. word vramCursorOffset = GFX_BackgroundPosFromXY(userCursorX, userCursorY+1);
  237. char* vramBackgroundpalette = (char*) 0xC00C20;
  238. *(vramBackgroundpalette + vramCursorOffset) = 0;
  239. }
  240. // Print the cursor on screen (using background plane)
  241. void printCursor()
  242. {
  243. word vramCursorOffset = GFX_BackgroundPosFromXY(userCursorX, userCursorY+1);
  244. char* vramBackgroundpalette = (char*) 0xC00C20;
  245. *(vramBackgroundpalette + vramCursorOffset) = PALETTE_CURSOR;
  246. }
  247. // Print the header on screen (using window plane)
  248. void printHeader()
  249. {
  250. GFX_printWindowColored(headerText, SCREEN_WIDTH, 0, PALETTE_HEADER);
  251. }
  252. void setPalettes()
  253. {
  254. word* paletteAddress = (word*) 0xC00400;
  255. paletteAddress[PALETTE_CURSOR] = 0x7 << 24;
  256. paletteAddress[PALETTE_NEWLINE] = 12;
  257. paletteAddress[PALETTE_EOF] = 224;
  258. paletteAddress[PALETTE_HEADER] = (0x51 << 24) + 0xDA;
  259. }
  260. // Make sure the cursor stays left from the newline
  261. void fixCursorToNewline()
  262. {
  263. word xpos = userCursorX+windowXscroll;
  264. word ypos = userCursorY+windowYscroll;
  265. word newLinePos = 0;
  266. while (mem[ypos][newLinePos] != '\n' && mem[ypos][newLinePos] != EOF)
  267. {
  268. newLinePos++;
  269. if (newLinePos == MAX_LINE_WIDTH)
  270. {
  271. exitRoutine();
  272. bdos_print("E: could not find newline\n");
  273. exit();
  274. }
  275. }
  276. clearCursor();
  277. word appliedScolling = 0;
  278. while (xpos > newLinePos)
  279. {
  280. if (userCursorX > 0)
  281. {
  282. userCursorX--;
  283. }
  284. else
  285. {
  286. if (windowXscroll > 0)
  287. {
  288. windowXscroll--;
  289. appliedScolling = 1;
  290. }
  291. else
  292. {
  293. exitRoutine();
  294. bdos_print("E: scroll left limit reached\n");
  295. exit();
  296. }
  297. }
  298. xpos = userCursorX+windowXscroll;
  299. }
  300. if (appliedScolling)
  301. {
  302. printWindow();
  303. }
  304. }
  305. // Move the cursor to targetPos of the previous line, from the left
  306. // Assumes targetPos is valid and ypos is not 0
  307. // Does not print window at the end, caller should do that
  308. void gotoPosOfPrevLine(word targetPos)
  309. {
  310. clearCursor();
  311. userCursorX = 0;
  312. windowXscroll = 0;
  313. if (userCursorY > 0)
  314. {
  315. userCursorY--;
  316. }
  317. else
  318. {
  319. if (windowYscroll > 0)
  320. {
  321. windowYscroll--;
  322. }
  323. else
  324. {
  325. exitRoutine();
  326. bdos_print("E: could not move up a line\n");
  327. exit();
  328. }
  329. }
  330. word xpos = 0;
  331. word ypos = userCursorY+windowYscroll;
  332. while (xpos != targetPos)
  333. {
  334. if (userCursorX < SCREEN_WIDTH-1)
  335. {
  336. userCursorX++;
  337. }
  338. else
  339. {
  340. if (windowXscroll < MAX_LINE_WIDTH-SCREEN_WIDTH)
  341. {
  342. windowXscroll++;
  343. }
  344. }
  345. xpos = userCursorX + windowXscroll;
  346. if (xpos == MAX_LINE_WIDTH)
  347. {
  348. exitRoutine();
  349. bdos_print("E: target out of bounds\n");
  350. exit();
  351. }
  352. }
  353. printCursor();
  354. }
  355. // Move the cursor to the newline character of the previous line
  356. void gotoEndOfPrevLine()
  357. {
  358. userCursorX = 0;
  359. windowXscroll = 0;
  360. word appliedScolling = 0;
  361. if (userCursorY > 0)
  362. {
  363. userCursorY--;
  364. }
  365. else
  366. {
  367. if (windowYscroll > 0)
  368. {
  369. windowYscroll--;
  370. appliedScolling = 1;
  371. }
  372. else
  373. {
  374. // don't need to do anything when on the first line
  375. return;
  376. }
  377. }
  378. word xpos = 0;
  379. word ypos = userCursorY+windowYscroll;
  380. while (mem[ypos][xpos] != '\n' && mem[ypos][xpos] != EOF)
  381. {
  382. if (userCursorX < SCREEN_WIDTH-1)
  383. {
  384. userCursorX++;
  385. }
  386. else
  387. {
  388. if (windowXscroll < MAX_LINE_WIDTH-SCREEN_WIDTH)
  389. {
  390. windowXscroll++;
  391. appliedScolling = 1;
  392. }
  393. }
  394. xpos = userCursorX + windowXscroll;
  395. if (xpos == MAX_LINE_WIDTH)
  396. {
  397. exitRoutine();
  398. bdos_print("E: could not find newline\n");
  399. exit();
  400. }
  401. }
  402. if (appliedScolling)
  403. {
  404. printWindow();
  405. }
  406. }
  407. void moveCursorDown()
  408. {
  409. // skip if current line contains EOF
  410. word ypos = userCursorY+windowYscroll;
  411. word i;
  412. for (i = 0; i < MAX_LINE_WIDTH; i++)
  413. {
  414. if (mem[ypos][i] == '\n')
  415. {
  416. break;
  417. }
  418. if (mem[ypos][i] == EOF)
  419. {
  420. return;
  421. }
  422. }
  423. clearCursor();
  424. if (userCursorY < SCREEN_HEIGHT-2)
  425. {
  426. userCursorY++;
  427. }
  428. else
  429. {
  430. if (windowYscroll < lastLineNumber-(SCREEN_HEIGHT-2))
  431. {
  432. windowYscroll++;
  433. printWindow();
  434. }
  435. }
  436. fixCursorToNewline();
  437. printCursor();
  438. }
  439. void moveCursorUp()
  440. {
  441. clearCursor();
  442. if (userCursorY > 0)
  443. {
  444. userCursorY--;
  445. }
  446. else
  447. {
  448. if (windowYscroll > 0)
  449. {
  450. windowYscroll--;
  451. printWindow();
  452. }
  453. }
  454. fixCursorToNewline();
  455. printCursor();
  456. }
  457. void moveCursorLeft()
  458. {
  459. clearCursor();
  460. if (userCursorX > 0)
  461. {
  462. userCursorX--;
  463. }
  464. else
  465. {
  466. if (windowXscroll > 0)
  467. {
  468. windowXscroll--;
  469. printWindow();
  470. }
  471. else
  472. {
  473. // move to previous line
  474. windowXscroll = MAX_LINE_WIDTH - SCREEN_WIDTH;
  475. userCursorX = SCREEN_WIDTH - 1;
  476. gotoEndOfPrevLine();
  477. }
  478. }
  479. fixCursorToNewline();
  480. printCursor();
  481. }
  482. void moveCursorRight()
  483. {
  484. word xpos = userCursorX + windowXscroll;
  485. if (mem[userCursorY+windowYscroll][xpos] == EOF)
  486. {
  487. // do nothing at EOF
  488. return;
  489. }
  490. clearCursor();
  491. if (mem[userCursorY+windowYscroll][xpos] == '\n')
  492. {
  493. // if we are at the end of the line, move to next line
  494. word appliedScolling = windowXscroll;
  495. windowXscroll = 0;
  496. userCursorX = 0;
  497. moveCursorDown();
  498. if (appliedScolling)
  499. {
  500. printWindow();
  501. }
  502. }
  503. else
  504. {
  505. if (userCursorX < SCREEN_WIDTH-1)
  506. {
  507. userCursorX++;
  508. }
  509. else
  510. {
  511. if (windowXscroll < MAX_LINE_WIDTH-SCREEN_WIDTH)
  512. {
  513. windowXscroll++;
  514. printWindow();
  515. }
  516. }
  517. }
  518. printCursor();
  519. }
  520. void pageUp()
  521. {
  522. clearCursor();
  523. word i;
  524. for (i = 0; i < SCREEN_HEIGHT-2; i++)
  525. {
  526. if (userCursorY > 0)
  527. {
  528. userCursorY--;
  529. }
  530. else
  531. {
  532. if (windowYscroll > 0)
  533. {
  534. windowYscroll--;
  535. }
  536. }
  537. }
  538. printWindow();
  539. fixCursorToNewline();
  540. printCursor();
  541. }
  542. void pageDown()
  543. {
  544. clearCursor();
  545. word lastLineFound = 0;
  546. word i;
  547. for (i = 0; i < SCREEN_HEIGHT-2; i++)
  548. {
  549. if (lastLineFound)
  550. {
  551. break;
  552. }
  553. // skip if current line contains EOF
  554. word ypos = userCursorY+windowYscroll;
  555. word i;
  556. for (i = 0; i < MAX_LINE_WIDTH; i++)
  557. {
  558. if (mem[ypos][i] == '\n')
  559. {
  560. break;
  561. }
  562. if (mem[ypos][i] == EOF)
  563. {
  564. lastLineFound = 1;
  565. break;
  566. }
  567. }
  568. if (userCursorY < SCREEN_HEIGHT-2)
  569. {
  570. userCursorY++;
  571. }
  572. else
  573. {
  574. if (windowYscroll < lastLineNumber-(SCREEN_HEIGHT-2))
  575. {
  576. windowYscroll++;
  577. }
  578. }
  579. }
  580. printWindow();
  581. fixCursorToNewline();
  582. printCursor();
  583. }
  584. void home()
  585. {
  586. clearCursor();
  587. userCursorX = 0;
  588. windowXscroll = 0;
  589. printWindow();
  590. printCursor();
  591. }
  592. void end()
  593. {
  594. clearCursor();
  595. userCursorX = 0;
  596. windowXscroll = 0;
  597. word xpos = 0;
  598. word ypos = userCursorY+windowYscroll;
  599. while (mem[ypos][xpos] != '\n' && mem[ypos][xpos] != EOF)
  600. {
  601. if (userCursorX < SCREEN_WIDTH-1)
  602. {
  603. userCursorX++;
  604. }
  605. else
  606. {
  607. if (windowXscroll < MAX_LINE_WIDTH-SCREEN_WIDTH)
  608. {
  609. windowXscroll++;
  610. }
  611. }
  612. xpos = userCursorX + windowXscroll;
  613. if (xpos == MAX_LINE_WIDTH)
  614. {
  615. exitRoutine();
  616. bdos_print("E: could not find newline\n");
  617. exit();
  618. }
  619. }
  620. printWindow();
  621. printCursor();
  622. }
  623. void removeCurrentLine()
  624. {
  625. word ypos = userCursorY+windowYscroll;
  626. // move all lines below ypos up by one, starting at beginning
  627. word i;
  628. for (i = ypos+1; i <= lastLineNumber; i++)
  629. {
  630. memcpy(mem[i-1], mem[i], MAX_LINE_WIDTH);
  631. }
  632. lastLineNumber--;
  633. }
  634. void insertNewLine()
  635. {
  636. word ypos = userCursorY+windowYscroll;
  637. if (lastLineNumber == MAX_LINES-1)
  638. {
  639. // ignore if we reached the memory limit
  640. return;
  641. }
  642. clearCursor();
  643. // move all lines below ypos down by one, starting at the end
  644. word i;
  645. for (i = lastLineNumber; i > ypos; i--)
  646. {
  647. memcpy(mem[i+1], mem[i], MAX_LINE_WIDTH);
  648. }
  649. lastLineNumber++;
  650. // move everything right from cursor to new line
  651. // this should include the newline/EOF automatically
  652. word newLineTmpCursor = 0;
  653. word xpos = userCursorX+windowXscroll;
  654. while(xpos < MAX_LINE_WIDTH)
  655. {
  656. mem[ypos+1][newLineTmpCursor] = mem[ypos][xpos];
  657. mem[ypos][xpos] = 0;
  658. newLineTmpCursor++;
  659. xpos++;
  660. }
  661. // insert newline character at current line
  662. xpos = userCursorX+windowXscroll;
  663. mem[ypos][xpos] = '\n';
  664. // move the cursor down to the start of the new line
  665. windowXscroll = 0;
  666. userCursorX = 0;
  667. moveCursorDown();
  668. printWindow(); // always needed in case of a new line
  669. printCursor();
  670. }
  671. // Insert character c at cursor
  672. void insertCharacter(char c)
  673. {
  674. word xpos = userCursorX+windowXscroll;
  675. word ypos = userCursorY+windowYscroll;
  676. memmove(&mem[ypos][xpos+1], &mem[ypos][xpos], MAX_LINE_WIDTH-(xpos+1));
  677. mem[ypos][xpos] = c;
  678. printWindow();
  679. moveCursorRight();
  680. }
  681. // Remove character at cursor
  682. void removeCharacter()
  683. {
  684. word xpos = userCursorX+windowXscroll;
  685. word ypos = userCursorY+windowYscroll;
  686. if (xpos > 0)
  687. {
  688. memmove(&mem[ypos][xpos-1], &mem[ypos][xpos], MAX_LINE_WIDTH-(xpos-1));
  689. mem[ypos][MAX_LINE_WIDTH-1] = 0; // remove new char at right of line
  690. printWindow();
  691. moveCursorLeft();
  692. }
  693. else
  694. {
  695. if (ypos > 0)
  696. {
  697. // append current line to previous line at newline
  698. word prevLinePasteLocation = 0;
  699. word newLinePos = 0;
  700. while (mem[ypos-1][newLinePos] != '\n')
  701. {
  702. newLinePos++;
  703. if (newLinePos == MAX_LINE_WIDTH)
  704. {
  705. exitRoutine();
  706. bdos_print("E: could not find newline\n");
  707. exit();
  708. }
  709. }
  710. // copy to overwrite the newline
  711. memcpy(&mem[ypos-1][newLinePos], &mem[ypos][0], MAX_LINE_WIDTH-(newLinePos));
  712. prevLinePasteLocation = newLinePos;
  713. // check if resulting line has a newline or EOF, else add to end
  714. newLinePos = 0;
  715. while (mem[ypos-1][newLinePos] != '\n' && mem[ypos-1][newLinePos] != EOF && newLinePos < MAX_LINE_WIDTH)
  716. {
  717. newLinePos++;
  718. if (newLinePos == MAX_LINE_WIDTH)
  719. {
  720. if (ypos == lastLineNumber)
  721. {
  722. mem[ypos-1][MAX_LINE_WIDTH-1] = EOF;
  723. }
  724. else
  725. {
  726. mem[ypos-1][MAX_LINE_WIDTH-1] = '\n';
  727. }
  728. }
  729. }
  730. // remove the current line
  731. removeCurrentLine();
  732. // move cursor to the place we pasted the line
  733. gotoPosOfPrevLine(prevLinePasteLocation);
  734. // update the view
  735. printWindow();
  736. }
  737. }
  738. }
  739. void addEditHeader()
  740. {
  741. headerText[29] = 'Y';
  742. headerText[30] = ':';
  743. itoa(userCursorY+windowYscroll, &headerText[31]);
  744. headerText[35] = 'X';
  745. headerText[36] = ':';
  746. itoa(userCursorX+windowXscroll, &headerText[37]);
  747. }
  748. void exitRoutine()
  749. {
  750. GFX_clearWindowtileTable();
  751. GFX_clearWindowpaletteTable();
  752. GFX_clearBGtileTable();
  753. GFX_clearBGpaletteTable();
  754. }
  755. int main()
  756. {
  757. // Read number of arguments
  758. word argc = shell_argc();
  759. if (argc < 2)
  760. {
  761. bdos_println("Usage: edit <file>");
  762. return 1;
  763. }
  764. // Read filename
  765. char** args = shell_argv();
  766. char* filename = args[1];
  767. char absolute_path[MAX_PATH_LENGTH];
  768. // Check if absolute path
  769. if (filename[0] != '/')
  770. {
  771. char* cwd = fs_getcwd();
  772. strcpy(absolute_path, cwd);
  773. strcat(absolute_path, "/");
  774. strcat(absolute_path, filename);
  775. }
  776. else
  777. {
  778. strcpy(absolute_path, filename);
  779. }
  780. // Get file size
  781. struct brfs_dir_entry* entry = (struct brfs_dir_entry*)fs_stat(absolute_path);
  782. if ((word)entry == -1)
  783. {
  784. bdos_println("File not found");
  785. return 1;
  786. }
  787. word filesize = entry->filesize;
  788. // Get filename
  789. char decompressed_filename[17];
  790. strdecompress(decompressed_filename, entry->filename);
  791. strcpy(headerText, decompressed_filename);
  792. readInputFile(absolute_path, filesize);
  793. // init gfx
  794. GFX_clearWindowtileTable();
  795. GFX_clearWindowpaletteTable();
  796. GFX_clearBGtileTable();
  797. GFX_clearBGpaletteTable();
  798. setPalettes();
  799. addEditHeader();
  800. printWindow();
  801. printCursor();
  802. // main loop
  803. while (1)
  804. {
  805. if (hid_checkfifo())
  806. {
  807. word c = hid_fiforead();
  808. switch (c)
  809. {
  810. case 27: // escape
  811. memcpy(&headerText[24], "Save? Type y/n/c", 16);
  812. printHeader();
  813. // ask for confirmation
  814. while (c != 'y' && c != 'n' && c != 'Y' && c != 'N' && c != 'c' && c != 'C')
  815. {
  816. // wait until a character is pressed
  817. while (!hid_checkfifo());
  818. c = hid_fiforead();
  819. }
  820. if (c == 'y' || c == 'Y')
  821. {
  822. // First delete file
  823. fs_delete(absolute_path);
  824. // Then create file
  825. fs_mkfile(absolute_path);
  826. // Then write to file
  827. writeMemToFile(absolute_path);
  828. exitRoutine();
  829. return 'q'; // exit
  830. }
  831. if (c == 'n' || c == 'N')
  832. {
  833. exitRoutine();
  834. return 'q'; // exit
  835. }
  836. memcpy(&headerText[24], " ", 16); // clear header text
  837. break;
  838. case ARROW_LEFT:
  839. moveCursorLeft();
  840. break;
  841. case ARROW_RIGHT:
  842. moveCursorRight();
  843. break;
  844. case ARROW_DOWN:
  845. moveCursorDown();
  846. break;
  847. case ARROW_UP:
  848. moveCursorUp();
  849. break;
  850. case BTN_HOME:
  851. home();
  852. break;
  853. case BTN_END:
  854. end();
  855. break;
  856. case BTN_PAGEUP:
  857. pageUp();
  858. break;
  859. case BTN_PAGEDOWN:
  860. pageDown();
  861. break;
  862. case 0x8: // backspace
  863. removeCharacter();
  864. break;
  865. case '\n':
  866. insertNewLine();
  867. break;
  868. case '\t':
  869. insertCharacter(' ');
  870. insertCharacter(' ');
  871. break;
  872. default:
  873. // default to inserting the character as text
  874. insertCharacter(c);
  875. break;
  876. }
  877. addEditHeader();
  878. printHeader();
  879. }
  880. }
  881. return 'q';
  882. }
  883. void interrupt()
  884. {
  885. // Handle all interrupts
  886. word i = get_int_id();
  887. switch(i)
  888. {
  889. case INTID_TIMER1:
  890. timer1Value = 1; // Notify ending of timer1
  891. break;
  892. }
  893. }