  rem -- Some of these routines were designed by Julian Moss --
  rem -- Added ability to change font size, normal or bold text,
  rem -- and set word wrap without exiting the program --
  rem -- Creates DAT file ([program name].DAT) to save settings --

  rem -- Filename (check for command line filename) --
  if @file(%1)
    %f = %1
    if @greater(@file(%f, Z), 32000)
      if @ask("File is too large for this program."@tab()@cr()@cr()"Use Wordpad instead?")
        RUN Wordpad.exe %f
        EXIT 
      else 
        %f = "Untitled"
      end 
    end 
  else 
    %f = "Untitled"
  end 

  rem -- Check for DAT file with settings (DO NOT close/clear list 2) --
  rem -- Items: 0 = fontsize, 1 = fontstyle, 2 = WRAP/NOWRAP, 3 = tabstop
  rem -- Items: 4 - 11 = last 8 files opened (if applicable).
  LIST CREATE, 2
  if @file(@path(%0)@name(%0).DAT)
    LIST LOADFILE, 2, @path(%0)@name(%0).DAT
  else 
    rem -- Create DAT file if not found --
    LIST ADD, 2, "10"
    LIST ADD, 2, "N"
    LIST ADD, 2, "NOWRAP"
    LIST ADD, 2, "8"
    LIST SAVEFILE, 2, @path(%0)@name(%0).DAT
  end 
  if @greater(@count(2), 3)
    %%fontsize = @item(2, 0)
    %%fontstyle = @item(2, 1)
    if @equal(%%fontstyle, "B")
      %%bold = ">>"
    end 
    if @equal(@item(2, 2), "WRAP")
      %%wordwrap = @item(2, 2)
      %%wrap = ">>"
    else 
      %%wordwrap = ""
    end 
    if @numeric(@item(2, 3))
      %%stops = @item(2, 3)
    else 
      %%stops = 8
    end 
  else 
    rem -- DAT file must be corrupt, replace it and use defaults --
    LIST CLEAR, 2
    LIST ADD, 2, "10"
    LIST ADD, 2, "N"
    LIST ADD, 2, "NOWRAP"
    LIST ADD, 2, "8"
    LIST SAVEFILE, 2, @path(%0)@name(%0).DAT
    %%fontsize = 10
    %%fontstyle = N
    %%wordwrap = ""
    %%stops = 8
  end 

  OPTION SCALE, 96
  OPTION DECIMALSEP, "."
  TITLE By Mac
  DIALOG CREATE,Mac's Editor - %f,20,0,632,388,RESIZABLE
  rem -- Normal fonts --
  DIALOG ADD,STYLE,Style08N,Courier New,8,,WHITE
  DIALOG ADD,STYLE,Style09N,Courier New,9,,WHITE
  DIALOG ADD,STYLE,Style10N,Courier New,10,,WHITE
  DIALOG ADD,STYLE,Style11N,Courier New,11,,WHITE
  DIALOG ADD,STYLE,Style12N,Courier New,12,,WHITE
  rem -- Bold fonts --
  DIALOG ADD,STYLE,Style08B,Courier New,8,B,WHITE
  DIALOG ADD,STYLE,Style09B,Courier New,9,B,WHITE
  DIALOG ADD,STYLE,Style10B,Courier New,10,B,WHITE
  DIALOG ADD,STYLE,Style11B,Courier New,11,B,WHITE
  DIALOG ADD,STYLE,Style12B,Courier New,12,B,WHITE

  DIALOG ADD,EDIT,E1,0,0,632,368,,MULTI,SCROLL,TABS,%%wordwrap,Style%%fontsize%%fontstyle
  DIALOG ADD,MENU,&File,&New|Ctrl+N,&Open|Ctrl+O,&Save|Ctrl+S,Save &As,-,&Recent Files|Ctrl+R,-,&Print|Ctrl+P,-,E&xit|Alt+X
  DIALOG ADD,MENU,&Edit,&Find|Ctrl+F,Find &Next|F3,&Options|Alt+O
  DIALOG ADD,STATUS,Stat,"Line 1 Col 1"
  DIALOG SHOW

  rem -- Get ID of edit element for use in @sendmsg() --
  %q = @winexists(~E1)

  rem -- set tabstops --
  %z = @sendmsg(%q,$0CB,1,@chr(@prod(%%stops, 4)))

  GOSUB GetFontSize

  rem -- Load file from command line --
  if @file(%1)
    GOSUB LoadFile
  end 

:EVLOOP
  WAIT EVENT, .1
  %e = @event()
  if @equal(@substr(%e,2), ".")
    goto Recent
  end 
  goto %e

:TIMER
  rem -- Get line number (use %c to get a start point) --
  %c = @sendmsg(%q,$0BB,-1,0)
  %l = @succ(@sendmsg(%q,$0C9,%c,0))

  rem -- Get total number of lines in document --
  %n = @sendmsg(%q,$0BA,0,0)

  rem -- Get character position in edit line (complicated) --
  %c = @mod(@sendmsg(%q,$0B0,0,0),65536)
  %c = @mod(@sendmsg(%q,214,%c,0),65536)
  if @greater(0,%c)
    %c = @sum(%c,65536)
  end 
  if @greater(%c,32768)
    %c = @diff(%c,65536)
  end 
  %z = @mod(@sendmsg(%q,214,0,0),65536)
  if @greater(0,%z)
    %z = @sum(%z,65536)
  end 
  if @greater(%z,32768)
    %z = @diff(%z,65536)
  end 
  %c = @succ(@div(@diff(%c,%z),%%charwidth))

  rem -- Check if document has been modified --
  if @zero(@sendmsg(%q,$0B8,0,0))
    %m = ""
  else 
    %m = Modified
  end 
  DIALOG SET, Stat, "Line " %l" of "%n" lines     Col "%c"     "%m"     "@len(@dlgtext(E1))" bytes"
  goto EVLOOP

:RESIZE
  PARSE "%h;%w", @dlgpos(,HW)
  DIALOG SETPOS,E1,0,0,%w,@diff(%h,20)
  goto EVLOOP

:NewMENU
  if %m
    if @ask(Current text has been been modified. Save changes?@tab())
      GOSUB SaveFile
    end 
  end 
  DIALOG CLEAR, E1
  %f = "Untitled"
  rem -- Clear "text modified" flag --
  %z = @sendmsg(%q,$0B9,0,0)
  DIALOG TITLE, "Mac's Editor - "%f
  goto EVLOOP

:OpenMENU
  if %m
    if @ask(Current text has been been modified. Save changes?@tab())
      GOSUB SaveFile
    end 
  end 
  %f = @filedlg()
  if %f
    if @greater(@file(%f, Z), 32000)
      if @ask("File is too large for this program."@tab()@cr()@cr()"Use Wordpad instead?")
        RUN Wordpad.exe %f
        EXIT 
      end 
    else 
      gosub LoadFile
    end 
  end 
  goto EVLOOP

:SaveMENU
  if @equal(%f, "Untitled")
    goto Save asMENU
  else 
    if %f 
      GOSUB SaveFile
    end 
  end 
  goto EVLOOP

:Save asMENU
  if %f
    %g = %f
  else 
    %g = "Untitled"
  end 
  %f = @filedlg(,,,SAVE)
  if %f
    if @file(%f)
      if @ask("File already exists. Overwrite it?"@tab())
        GOSUB SaveFile
      end 
    else 
      GOSUB SaveFile
    end 
  else 
    %f = %g
  end 
  goto EVLOOP

:Recent FilesMENU
  DIALOG POPUP,1.@item(2,4)|2.@item(2,5)|3.@item(2,6)|4.@item(2,7)|5.@item(2,8)|6.@item(2,9)|7.@item(2,10)|8.@item(2,11),-5,0
  goto EVLOOP

:Recent
  if @greater(@len(%e), 6)
    %f = @substr(%e, 3, @diff(@len(%e), 4))
    GOSUB LoadFile
  end 
  goto EVLOOP

:PrintMENU
  if @dlgtext(E1)
    if @not(@ask(Is printer ready?))
      goto EVLOOP
    end 
    LIST CREATE, 1
    LIST ASSIGN, 1, @dlgtext(E1)
    %z = @input("Enter left margin spaces for printing:", 0)
    %s = ""
    if @numeric(%z)
      if @greater(%z, 0)
        %x = 0
        REPEAT 
          %s = %s" "
          %x = @succ(%x)
        UNTIL @equal(%x, %z)
        %x = 0
        REPEAT 
          %z = @item(1, %x)
          LIST PUT, 1, %s%z
          %x = @succ(%x)
        UNTIL @equal(%x, @count(1))
      end 
    end 
    LIST PRINT, 1, Courier New, %%fontsize
    LIST CLOSE, 1
  end 
  goto EVLOOP

:ExitMENU
:CLOSE
  if %m
    if @ask(Current text has been been modified. Save changes?@tab())
      if @equal(%f, "Untitled")
        %f = @filedlg(,,,SAVE)
      end 
      GOSUB SaveFile
      WAIT 
    end 
  end 
  LIST SAVEFILE, 2, @path(%0)@name(%0).DAT
  WAIT .1
  EXIT 
  STOP 

:FindMENU
  %t = @input(Enter text to search for:)
  if %t
    %s = @pred(@pos(%t,@dlgtext(E1)))
    if @greater(%s,-1)
      gosub SelectText
    else 
      INFO Text not found@tab()
    end 
  end 
  goto EVLOOP

:Find NextMENU
  if %t
    %p = @pos(%t,@strdel(@dlgtext(E1),1,@succ(%s)))
    if @greater(%p,0)
      %s = @sum(%s,%p)
      gosub SelectText
    else 
      INFO Text not found@tab()
    end 
  end 
  goto EVLOOP

:OptionsMENU
  DIALOG POPUP, %%wrap Word Wrap...|%%bold Bold Text...| Font Size...| Set Tab Stops...,-5,24
  goto EVLOOP

: Word Wrap...MENU
:>> Word Wrap...MENU
  LIST SEEK, 2, 2
  if %%wrap
    %%wrap = ""
    %%wordwrap = ""
    LIST PUT, 2, NOWRAP
  else 
    %%wrap = ">>"
    %%wordwrap = WRAP
    LIST PUT, 2, WRAP
  end 
  GOSUB SetOptions
  goto EVLOOP

: Bold Text...MENU
:>> Bold Text...MENU
  LIST SEEK, 2, 1
  if %%bold
    %%bold = ""
    %%fontstyle = N
    LIST PUT, 2, N
  else 
    %%bold = ">>"
    %%fontstyle = B
    LIST PUT, 2, B
  end 
  GOSUB SetOptions
  goto EVLOOP

: Font Size...MENU
  DIALOG POPUP,08|09|10|11|12,-5,24
  goto EVLOOP

:08MENU
:09MENU
:10MENU
:11MENU
:12MENU
  %%fontsize = @substr(%e,1,2)
  LIST SEEK, 2, 0
  LIST PUT, 2, %%fontsize
  GOSUB SetOptions
  goto EVLOOP

: Set Tab Stops...MENU
  %t = @input("Enter tab stop interval (0 - 8):")
  if @numeric(%t)
    if @both(@greater(%t,0),@greater(9,%t))
      %z = @sendmsg(%q,$0CB,1,@chr(@prod(%t,4)))
      %%stops = %t
      LIST SEEK, 2, 3
      LIST PUT, 2, %%stops
    end 
  end 
  goto EVLOOP

  rem ---------------- GOSUB PROCEDURES ------------------

:SetOptions
  LIST CREATE, 1
  LIST ASSIGN, 1, @dlgtext(E1)
  DIALOG REMOVE, E1
  WAIT .1
  DIALOG ADD,EDIT,E1,0,0,632,368,,MULTI,SCROLL,TABS,%%wordwrap,Style%%fontsize%%fontstyle
  rem -- In case ID changes when E1 is recreated --
  %q = @winexists(~E1)
  DIALOG SET, E1, @text(1)
  LIST CLOSE, 1
  rem -- set tabstops --
  %z = @sendmsg(%q,$0CB,1,@chr(@prod(%%stops, 4)))
:GetFontSize
  rem -- Get character width of current font size character --
  DIALOG ADD,TEXT,DummyTxt,0,0,,,"A",Style%%fontsize%%fontstyle
  %%charwidth = @dlgpos(DummyTxt,W)
  DIALOG REMOVE, DummyTxt
  exit 

:LoadFile
  LIST CREATE, 1
  LIST LOADFILE, 1, %f
  DIALOG SET, E1, @text(1)
  LIST CLOSE, 1
  rem -- Clear "text modified" flag --
  %z = @sendmsg(%q,$0B9,0,0)
  DIALOG TITLE, "Mac's Editor - "%f
  GOSUB AddToRecent
  exit 

:SaveFile
  LIST CREATE, 1
  LIST ASSIGN, 1, @dlgtext(E1)
  LIST SAVEFILE, 1, %f
  LIST CLOSE, 1
  rem -- Clear "text modified" flag --
  %z = @sendmsg(%q,$0B9,0,0)
  DIALOG TITLE, "Mac's Editor - "%f
  GOSUB AddToRecent
  exit 

:SelectText
  rem -- Select text found --
  %z = @sendmsg(%q,$0B1,%s,@sum(%S,@len(%t)))
  rem -- Scroll selected text (caret) into view --
  %z = @sendmsg(%q,$0B7,0,0)
  exit 

:AddToRecent
  rem -- Add to recent files and trim list --
  if @not(@equal(@item(2, 4), %f))
    LIST INSERT, 2, %f
    if @greater(@count(2), 12)
      LIST SEEK, 2, 12
      LIST DELETE, 2
    end 
  end 
  exit 
