home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / fed0217s.zip / source / bcommand.cpp < prev    next >
C/C++ Source or Header  |  2001-11-06  |  14KB  |  738 lines

  1. /*
  2. ** Module   :BCOMMAND.CPP
  3. ** Abstract :Editor Commands
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. **
  7. ** Log: Wed  25/03/1998     Created
  8. */
  9.  
  10. #include <stddlg.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13.  
  14. #include <boxcoll.h>
  15. #include <fio.h>
  16. #include <dialog.h>
  17. #include <keynames.h>
  18. #include <version.h>
  19.  
  20. #define INCL_REXXSAA
  21. #ifdef __EMX__
  22. extern "C" {
  23. #include <os2emx.h>
  24. }
  25. #else
  26. #include <rexxsaa.h>
  27. #endif
  28.  
  29. #define MK_CLR(clr)     (app_pal[CL_APPLICATION_START+(clr)])
  30.  
  31. extern JumpList JList[];
  32. //---- General
  33.  
  34. void EditBoxCollection::doSave()
  35. {
  36.     if (current()->is_untitled())
  37.         doSaveAs();
  38.     else
  39.     {
  40.         if(current()->save())
  41.             MessageBox("Error saving file!");
  42.     }
  43. }
  44.  
  45. void EditBoxCollection::doSaveAs()
  46. {
  47.     int rc = FileDialog(5, 5, cName, 2);
  48.  
  49.     if (!rc)
  50.     {
  51.         if(current()->save_as(cName))
  52.             MessageBox("Error saving file!");
  53.     }
  54. }
  55.  
  56. void EditBoxCollection::doLoad()
  57. {
  58.     int rc = FileDialog(2, 2, cName, 0);
  59.  
  60.     if (!rc)
  61.         doOpenFile(cName);
  62. }
  63.  
  64. void EditBoxCollection::doOpenFile(char *pName)
  65. {
  66.     int rc = opened(pName);
  67.  
  68.     if(rc < 0)
  69.     {
  70.         if (!current()->is_untitled() ||
  71.             current()->get_changed())
  72.                 select(open());
  73.  
  74.         current()->load(pName);
  75.         SendKey("kbOpen");
  76.     }
  77.     else
  78.         select(rc);
  79. }
  80.  
  81. void EditBoxCollection::doNew()
  82. {
  83.     select(open());
  84.     SendKey("kbOpen");
  85. }
  86.  
  87. void EditBoxCollection::doClose()
  88. {
  89.     SendKey("kbClose");
  90.  
  91.     if(!current()->check_save())
  92.     {
  93.         close();
  94.         select(0);
  95.     }
  96. }
  97.  
  98. void EditBoxCollection::doHelpScreen()
  99. {
  100.     Dialog dlg(2, 2, Rows - 4, Cols - 4);
  101.     ListBox *list = new ListBox(1, 1, dlg.rows - 2, dlg.cols - 2);
  102.  
  103.     dlg.Ins(list);
  104.  
  105.     char *str;
  106.     char *ptr;
  107.  
  108.     for(ptr = str = help_text; *str;)
  109.     {
  110.         char chr;
  111.  
  112.         while(*str && *str != '\n')
  113.             str++;
  114.  
  115.         chr = *str;
  116.         *str = 0;
  117.  
  118.         list->add_at_end(ptr);
  119.  
  120.         *str = chr;
  121.  
  122.         if(*str)
  123.             str++;
  124.  
  125.         ptr = str;
  126.     }
  127.  
  128.     KeyInfo k;
  129.  
  130.     do
  131.     {
  132.         dlg.draw();
  133.         vio_read_key(&k);
  134.         dlg.do_key(k);
  135.     }
  136.     while (k.skey != (kbEsc | shIsCtrl));
  137. }
  138.  
  139. void EditBoxCollection::doNextFile()
  140. {
  141.     next();
  142. }
  143.  
  144. void EditBoxCollection::doPrevFile()
  145. {
  146.     prev();
  147. }
  148.  
  149. void EditBoxCollection::doSort()
  150. {
  151.     current()->sort();
  152. }
  153.  
  154. void EditBoxCollection::doSearch()
  155. {
  156.     int rr;
  157.     int rc;
  158.  
  159.     if (current()->get_cur_row() >= Rows / 2)
  160.         rr = 3;
  161.     else
  162.         rr = Rows / 2;
  163.  
  164.     rc = SearchReplace(rr,
  165.                        3,
  166.                        Search,
  167.                        Replace,
  168.                        Flags);
  169.     if (!rc)
  170.     {
  171.         search_proc();
  172.     }
  173. }
  174.  
  175. void EditBoxCollection::doSearchAgain()
  176. {
  177.     search_proc();
  178. }
  179.  
  180. void EditBoxCollection::doFileList()
  181. {
  182.     Dialog dlg(2,2, Rows - 4 , Cols - 4);
  183.     ListBox *list = new ListBox(1, 1, dlg.rows - 2, dlg.cols - 2);
  184.  
  185.     dlg.Ins(list);
  186.  
  187.     int i;
  188.  
  189.     for(i = 0; i < Count(); i++)
  190.     {
  191.         EditBox* one = GetBox(i);
  192.  
  193.         cName[0] = one->get_changed() ? '*':' ';
  194.         strcpy(&cName[1], one->get_fmt_name(list->cols - 1));
  195.  
  196.         list->add_at_end(cName);
  197.  
  198.         if(one == current())
  199.             list->go_item(i);
  200.     }
  201.  
  202.     KeyInfo k;
  203.     do
  204.     {
  205.         dlg.draw();
  206.         vio_read_key(&k);
  207.  
  208.         if((k.skey & 0x00FF) == kbEnter ||
  209.            (k.skey & 0x00FF) == kbGrEnter)
  210.         {
  211.             select(list->first_selected());
  212.             return;
  213.         }
  214.         dlg.do_key(k);
  215.     }
  216.     while(k.skey != (kbEsc | shIsCtrl));
  217. }
  218.  
  219. void EditBoxCollection::doSaveAll()
  220. {
  221.     EditBox *cur = current();
  222.  
  223.     do
  224.     {
  225.         current()->save();
  226.         next();
  227.     }
  228.     while (current() != cur);
  229. }
  230.  
  231. void EditBoxCollection::doJumpLine()
  232. {
  233.     int rr;
  234.  
  235.     if (current()->get_cur_row() >= Rows / 2)
  236.         rr = 3;
  237.     else
  238.         rr = Rows / 2;
  239.  
  240.     int place = 0;
  241.     int rc = AskNumber(rr, 3, &place, "Goto Line");
  242.  
  243.     if (!rc)
  244.     {
  245.         current()->goto_line(*current(), place - 1);
  246.     }
  247. }
  248.  
  249. void EditBoxCollection::doJumpCol()
  250. {
  251.     int rr;
  252.  
  253.     if (current()->get_cur_row() >= Rows / 2)
  254.         rr = 3;
  255.     else
  256.         rr = Rows / 2;
  257.  
  258.     int place = 0;
  259.     int rc = AskNumber(rr, 3, &place, "Goto Column");
  260.  
  261.     if (!rc)
  262.     {
  263.         current()->goto_col(*current(), place - 1);
  264.     }
  265. }
  266.  
  267. void EditBoxCollection::doExit()
  268. {
  269.     if(check_save() == -1)
  270.         shutdown = 0;
  271.     else
  272.         shutdown = 1;
  273. }
  274.  
  275. void EditBoxCollection::doAbort()
  276. {
  277.     shutdown = 1;
  278. }
  279.  
  280. void EditBoxCollection::doCopyright()
  281. {
  282.     static char * copyright[]=
  283.     {
  284.         "FAST Editor Lite for OS/2 "VERSION,
  285.         "Copyright (C) Sergey I. Yevtushenko, 1997-2000",
  286.         0
  287.     };
  288.  
  289.     vio_cls(MK_CLR(CL_DEFAULT));
  290.     int i;
  291.  
  292.     for(i = 0; copyright[i]; i++)
  293.         vio_print(i + 1, 1,copyright[i], Cols, MK_CLR(CL_DEFAULT));
  294.  
  295.     vio_show();
  296. }
  297.  
  298. void EditBoxCollection::doCopyright2()
  299. {
  300.     doCopyright();
  301.     KeyInfo k;
  302.     vio_read_key(&k);
  303. }
  304. //--------------------------------------------------------------
  305. //---- Cursor movement
  306.  
  307. void EditBoxCollection::doLeft()
  308. {
  309.     current()->unmark();
  310.     current()->cursor_left(*current());
  311. }
  312. void EditBoxCollection::doWordLeft()
  313. {
  314.     current()->unmark();
  315.     current()->word_left(*current());
  316. }
  317. void EditBoxCollection::doRight()
  318. {
  319.     current()->unmark();
  320.     current()->cursor_right(*current());
  321. }
  322. void EditBoxCollection::doWordRight()
  323. {
  324.     current()->unmark();
  325.     current()->word_right(*current());
  326. }
  327. void EditBoxCollection::doPgUp()
  328. {
  329.     current()->unmark();
  330.     current()->page_up(*current());
  331. }
  332. void EditBoxCollection::doFileBegin()
  333. {
  334.     current()->unmark();
  335.     current()->text_begin(*current());
  336. }
  337. void EditBoxCollection::doPgDn()
  338. {
  339.     current()->unmark();
  340.     current()->page_down(*current());
  341. }
  342. void EditBoxCollection::doFileEnd()
  343. {
  344.     current()->unmark();
  345.     current()->text_end(*current());
  346. }
  347. void EditBoxCollection::doHome()
  348. {
  349.     current()->unmark();
  350.     current()->line_begin(*current());
  351. }
  352. void EditBoxCollection::doEnd()
  353. {
  354.     current()->unmark();
  355.     current()->line_end(*current());
  356. }
  357. void EditBoxCollection::doUp()
  358. {
  359.     current()->unmark();
  360.     current()->cursor_up(*current());
  361. }
  362. void EditBoxCollection::doDown()
  363. {
  364.     current()->unmark();
  365.     current()->cursor_down(*current());
  366. }
  367. void EditBoxCollection::doMatchBracket()
  368. {
  369.     current()->unmark();
  370.     current()->match_bracket(*current());
  371. }
  372.  
  373. //---- Block marking
  374. void EditBoxCollection::doLeftMark()
  375. {
  376.     current()->mark();
  377.     current()->cursor_left(*current());
  378. }
  379. void EditBoxCollection::doWordLeftMark()
  380. {
  381.     current()->mark();
  382.     current()->word_left(*current());
  383. }
  384. void EditBoxCollection::doRightMark()
  385. {
  386.     current()->mark();
  387.     current()->cursor_right(*current());
  388. }
  389. void EditBoxCollection::doWordRightMark()
  390. {
  391.     current()->mark();
  392.     current()->word_right(*current());
  393. }
  394. void EditBoxCollection::doPgUpMark()
  395. {
  396.     current()->mark();
  397.     current()->page_up(*current());
  398. }
  399. void EditBoxCollection::doFileBeginMark()
  400. {
  401.     current()->mark();
  402.     current()->text_begin(*current());
  403. }
  404. void EditBoxCollection::doPgDnMark()
  405. {
  406.     current()->mark();
  407.     current()->page_down(*current());
  408. }
  409. void EditBoxCollection::doFileEndMark()
  410. {
  411.     current()->mark();
  412.     current()->text_end(*current());
  413. }
  414. void EditBoxCollection::doHomeMark()
  415. {
  416.     current()->mark();
  417.     current()->line_begin(*current());
  418. }
  419. void EditBoxCollection::doEndMark()
  420. {
  421.     current()->mark();
  422.     current()->line_end(*current());
  423. }
  424. void EditBoxCollection::doUpMark()
  425. {
  426.     current()->mark();
  427.     current()->cursor_up(*current());
  428. }
  429. void EditBoxCollection::doDownMark()
  430. {
  431.     current()->mark();
  432.     current()->cursor_down(*current());
  433. }
  434. void EditBoxCollection::doMatchBracketMark()
  435. {
  436.     current()->mark();
  437.     current()->match_bracket(*current());
  438. }
  439.  
  440. //---- Editing commands
  441.  
  442. void EditBoxCollection::doUpper()
  443. {
  444.     current()->toupper(*current());
  445. }
  446. void EditBoxCollection::doLower()
  447. {
  448.     current()->tolower(*current());
  449. }
  450. void EditBoxCollection::doBksp()
  451. {
  452.     current()->back_space(*current());
  453. }
  454. void EditBoxCollection::doDel()
  455. {
  456.     if (current()->get_mark_state())
  457.         current()->clear(*current());
  458.     else
  459.         current()->del_char(*current());
  460.     current()->unmark();
  461. }
  462.  
  463. void EditBoxCollection::doDelLine()
  464. {
  465.     PLine line = current()->del_line(*current(), current()->abs_row());
  466.     delete line;
  467. }
  468.  
  469. void EditBoxCollection::doDupLine()
  470. {
  471.     current()->dup_line(*current(), current()->abs_row());
  472. }
  473.  
  474. void EditBoxCollection::doDelWordLeft()
  475. {
  476.     current()->del_word_left(*current());
  477. }
  478. void EditBoxCollection::doDelWordRight()
  479. {
  480.     current()->del_word_right(*current());
  481. }
  482. void EditBoxCollection::doDelToEOL()
  483. {
  484.     current()->del_to_EOL(*current());
  485. }
  486. void EditBoxCollection::doIndent()
  487. {
  488.     current()->indent();
  489. }
  490. void EditBoxCollection::doUnindent()
  491. {
  492.     current()->unindent();
  493. }
  494.  
  495. void EditBoxCollection::doCut()
  496. {
  497.     delete Clipboard;
  498.  
  499.     Clipboard = current()->cut(*current());
  500. }
  501. void EditBoxCollection::doCopy()
  502. {
  503.     delete Clipboard;
  504.  
  505.     Clipboard = current()->copy();
  506. }
  507. void EditBoxCollection::doPaste()
  508. {
  509.     if (current()->get_mark_state())
  510.     {
  511.         current()->clear(*current());
  512.         current()->unmark();
  513.     }
  514.     current()->paste(*current(), Clipboard);
  515. }
  516.  
  517. void EditBoxCollection::doUndo()
  518. {
  519.     current()->track_cancel();
  520.     current()->undo(*current());
  521. }
  522.  
  523. //---- State commands
  524. void EditBoxCollection::doIns()
  525. {
  526.     current()->set_ins_mode(1 - current()->get_ins_mode());
  527. }
  528. void EditBoxCollection::doFlipBlockMode()
  529. {
  530.     current()->set_column_block(1 - current()->get_column_block());
  531. }
  532. void EditBoxCollection::doFlipAutoindent()
  533. {
  534.     current()->set_auto_indent(1 - current()->get_auto_indent());
  535. }
  536. void EditBoxCollection::doFlipHiliting()
  537. {
  538.     current()->flip_hiliting();
  539. }
  540. void EditBoxCollection::doFlipType()
  541. {
  542.     current()->set_unix(1 - current()->get_unix());
  543. }
  544.  
  545. void EditBoxCollection::doInsDate()
  546. {
  547.     char buff[FED_MAXPATH];
  548.  
  549.     if(!curr_date_str(buff))
  550.         play_macro(buff);
  551. }
  552.  
  553. void EditBoxCollection::doInsFileName()
  554. {
  555.     play_macro(current()->get_name());
  556. }
  557.  
  558. void EditBoxCollection::doInsFileNameShort()
  559. {
  560.     char buff[FED_MAXPATH];
  561.     make_short_name(current()->get_name(), buff);
  562.     play_macro(buff);
  563. }
  564.  
  565. //---- Macro Recorder commands
  566.  
  567. void EditBoxCollection::doMacroRecStart()
  568. {
  569.     start_recording();
  570. }
  571.  
  572. void EditBoxCollection::doMacroRecEnd()
  573. {
  574.     if(!recording)
  575.         return;
  576.  
  577.     char *macro = track_recording_done();
  578.     clear_recording();
  579.     KeyInfo k;
  580.  
  581.     MessageBox("Press key to assign recorded macro\n <ESC> - cancel", &k);
  582.  
  583.     if(k.skey != (kbEsc | shIsCtrl))
  584.         keys.AddAssignment(k.KeyName, macro);
  585.     delete macro;
  586. }
  587.  
  588. //---- Rexx macro processor
  589.  
  590. void EditBoxCollection::doRexx(char* prog)
  591. {
  592.     RXSTRING INSTORE[2];
  593.     RXSTRING arg;
  594.     RXSTRING rexxretval;
  595.     SHORT    rexxrc = 0;
  596.     APIRET   rc;
  597.  
  598.     INSTORE[0].strptr    = (PCH)prog;
  599.     INSTORE[0].strlength = strlen(prog);
  600.     INSTORE[1].strptr    = 0;
  601.     INSTORE[1].strlength = 0;
  602.  
  603.     arg.strptr    = (PCH)"";
  604.     arg.strlength = 0;
  605.     rexxretval.strptr    = 0;
  606.     rexxretval.strlength = 0;
  607.  
  608.     rc=RexxStart((LONG)      1,
  609.                 (PRXSTRING)  &arg,
  610.                 (PSZ)        "FED_RX",
  611.                 (PRXSTRING)  INSTORE,
  612.                 (PSZ)        "FED",
  613. //                (LONG)       RXSUBROUTINE,
  614.                 (LONG)       RXFUNCTION,
  615.                 (PRXSYSEXIT) 0,
  616.                 (PSHORT)     &rexxrc,
  617.                 (PRXSTRING)  &rexxretval);
  618.  
  619.     DosFreeMem(rexxretval.strptr);
  620. }
  621.  
  622. void EditBoxCollection::doSetMark(int i)
  623. {
  624.     current()->bmk_place(i);
  625. }
  626.  
  627. void EditBoxCollection::doGotoMark(int i)
  628. {
  629.     current()->bmk_go(i);
  630. }
  631.  
  632. void EditBoxCollection::doJumpList(int i)
  633. {
  634.     if(i >= 0 && i <= 9)
  635.         JumpListBox(&JList[i], 0, 0, 0, 0);
  636. }
  637.  
  638. void EditBoxCollection::doSetXlat()
  639. {
  640.     int rr;
  641.  
  642.     if (current()->get_cur_row() >= Rows / 2)
  643.         rr = 3;
  644.     else
  645.         rr = Rows / 2;
  646.  
  647.     int place = 0;
  648.     int rc = AskNumber(rr, 3, &place, "Code page ");
  649.  
  650.     if (!rc && place > 0)
  651.     {
  652.         char cp[64];
  653.         char *cp_num = cvt_num(place, 3);
  654.         strcpy(cp, "IBM-");
  655.         strcat(cp, cp_num);
  656.  
  657.         current()->set_xlate(cp);
  658.     }
  659. }
  660.  
  661. void EditBoxCollection::doHilitingChoice()
  662. {
  663.     char** hl_names = new char* [Parser::Count()+1];
  664.  
  665.     int i;
  666.  
  667.     for(i = 0; i < Parser::Count(); i++)
  668.         hl_names[i] = Parser::Name(i, 1);
  669.  
  670.     hl_names[i] = 0;
  671.  
  672.     int rc = AChoice(5, 5, hl_names, "Mode");
  673.  
  674.     if(rc >= 0)
  675.     {
  676.         current()->set_hiliting(Parser::Type(rc));
  677.         current()->fill_hiliting(0, ST_INITIAL);
  678.     }
  679.  
  680.     delete hl_names;
  681. }
  682.  
  683. void EditBoxCollection::doLoadProfile()
  684. {
  685.     int sz = 0;
  686.     int i;
  687.  
  688.     for(i = 0; i < current()->Count(); i++)
  689.     {
  690.         sz += current()->line(i)->len();
  691.         sz += 2; //CRLF
  692.     }
  693.     sz++; //NULL terminator
  694.  
  695.     char *pByte = new char[sz];
  696.  
  697.     char *ptr = pByte;
  698.  
  699.     for(i = 0; i < current()->Count(); i++)
  700.     {
  701.         current()->line(i)->build_print(ptr);
  702.         ptr += current()->line(i)->len();
  703.         *ptr++ = '\r';
  704.         *ptr++ = '\n';
  705.         *ptr   = 0;
  706.     }
  707.  
  708.     int iSaveStatus = iUpperStatus;
  709.  
  710.     load_profile(pByte);
  711.  
  712.     if(iSaveStatus != iUpperStatus)
  713.     {
  714.         int iShift = (iUpperStatus) ? 1:-1;
  715.  
  716.         for(i = 0; i < Count(); i++)
  717.             GetBox(i)->row += iShift;
  718.     }
  719.  
  720.     delete pByte;
  721. }
  722.  
  723. void EditBoxCollection::doFlipWordWrap()
  724. {
  725.     current()->ww_set(current()->ww_get() ^ WW_STATE);
  726. }
  727.  
  728. void EditBoxCollection::doFlipWWMerge()
  729. {
  730.     current()->ww_set(current()->ww_get() ^ WW_MERGE);
  731. }
  732.  
  733. void EditBoxCollection::doFlipWWLong()
  734. {
  735.     current()->ww_set(current()->ww_get() ^ WW_LONG);
  736. }
  737.  
  738.