Menu

FORTRAN Syntax Highlighting Issue

2006-08-29
2012-11-13
  • Nobody/Anonymous

    I've been using N++ for about 6 months now and about the only complaint I have is that syntax highlighting in FORTRAN does not work right. FORTRAN syntax is column sensitive and if you have only spaces in your files N++ is awesome. Unfortunately several of our source files (in source control so I can't just edit willy-nilly) have leading tabs on the lines. These tabs get interpreted as a single column (as far as I can tell since they are not misinterpreted as comment lines), so that the fifth character is interpreted as a line number, which screws up the font and coloration for the rest of the line. If it instead saw the tab as the number of columns that a tab represents, it seems like the problem would just disappear.

    Thanks

     
    • Nobody/Anonymous

      I forgot to say that this file is located in /scintilla/src/ library.
      If you think it's good, we can post it to the developers.
      Morag

       
    • Greg Bullock

      Greg Bullock - 2006-08-30

      Are you sure those source files aren't using free-format style Fortran (in which column number doesn't matter)?

      If so, then it seems the solution, in N++, is to assign the width of a tab character to be 1.

      Note, however, that Fortran does not specify (as I recall) how wide a tab character is, so the compiler vendor probably gets to choose.  In that case, you should set the tab width in N++ to be whatever your compiler says it is.

      Regards.
      Greg

       
      • Nobody/Anonymous

        N++ Fortran formatting is mainly for old style Fortran 77 where cols were important. Except there is a bug in that they allow 73 cols instead of the standard 72.

        I agree is it very annoying for Fortran 90 "free-format" where columns are irrelevent. N++ incorrectly colour codes and font changes in the wrong spots. The best soltn is to wait for an enhancement that either has a Fortran 90 style or enable users to configure the columns themselves.

        AMo

         
        • Greg Bullock

          Greg Bullock - 2007-04-03

          You don't have to wait.  My colleagues and I use the following in our UserDefineLang.xml file:

              <UserLang name="FortranFreeform" ext="f90 f95 f2k">
                  <Settings>
                      <Global caseIgnored="yes" />
                      <TreatAsSymbol comment="no" commentLine="yes" />
                      <Prefix words1="no" words2="no" words3="no" words4="no" />
                  </Settings>
                  <KeywordLists>
                      <Keywords name="Delimiters">&apos;&quot;0&apos;&quot;0</Keywords>
                      <Keywords name="Folder+">do then</Keywords>
                      <Keywords name="Folder-">enddo endif end</Keywords>
                      <Keywords name="Operators">- % &amp; ( ) * , . / : ; + &lt; = &gt;</Keywords>
                      <Keywords name="Comment">1 2 0!</Keywords>
                      <Keywords name="Words1">access action advance allocatable allocate apostrophe assign assignment associate asynchronous backspace bind blank blockdata call case character class close common complex contains continue cycle data deallocate decimal delim default dimension direct dowhile double doubleprecision else elseif elsewhere encoding end endassociate endblockdata endfile endforall endfunction endinterface endmodule endprogram endselect endsubroutine endtype endwhere entry eor equivalence err errmsg exist exit external file flush fmt forall form format formatted function go goto id if implicit in include inout integer inquire intent interface intrinsic iomsg iolength iostat kind len logical module name named namelist nextrec nml none nullify number only open opened operator optional out pad parameter pass pause pending pointer pos position precision print private program protected public quote read readwrite real rec recl recursive result return rewind save select selectcase selecttype sequential sign size stat status stop stream subroutine target to type unformatted unit use value volatile wait where while write</Keywords>
                      <Keywords name="Words2">abs achar acos acosd adjustl adjustr aimag aimax0 aimin0 aint ajmax0 ajmin0 akmax0 akmin0 all allocated alog alog10 amax0 amax1 amin0 amin1 amod anint any asin asind associated atan atan2 atan2d atand bitest bitl bitlr bitrl bjtest bit_size bktest break btest cabs ccos cdabs cdcos cdexp cdlog cdsin cdsqrt ceiling cexp char clog cmplx conjg cos cosd cosh count cpu_time cshift csin csqrt dabs dacos dacosd dasin dasind datan datan2 datan2d datand date date_and_time dble dcmplx dconjg dcos dcosd dcosh dcotan ddim dexp dfloat dflotk dfloti dflotj digits dim dimag dint dlog dlog10 dmax1 dmin1 dmod dnint dot_product dprod dreal dsign dsin dsind dsinh dsqrt dtan dtand dtanh eoshift epsilon errsns exp exponent float floati floatj floatk floor fraction free huge iabs iachar iand ibclr ibits ibset ichar idate idim idint idnint ieor ifix iiabs iiand iibclr iibits iibset iidim iidint iidnnt iieor iifix iint iior iiqint iiqnnt iishft iishftc iisign ilen imax0 imax1 imin0 imin1 imod index inint inot int int1 int2 int4 int8 iqint iqnint ior ishft ishftc isign isnan izext jiand jibclr jibits jibset jidim jidint jidnnt jieor jifix jint jior jiqint jiqnnt jishft jishftc jisign jmax0 jmax1 jmin0 jmin1 jmod jnint jnot jzext kiabs kiand kibclr kibits kibset kidim kidint kidnnt kieor kifix kind kint kior kishft kishftc kisign kmax0 kmax1 kmin0 kmin1 kmod knint knot kzext lbound leadz len len_trim lenlge lge lgt lle llt log log10 logical lshift malloc matmul max max0 max1 maxexponent maxloc maxval merge min min0 min1 minexponent minloc minval mod modulo mvbits nearest nint not nworkers number_of_processors pack popcnt poppar precision present product radix random random_number random_seed range real repeat reshape rrspacing rshift scale scan secnds selected_int_kind selected_real_kind set_exponent shape sign sin sind sinh size sizeof sngl snglq spacing spread sqrt sum system_clock tan tand tanh tiny transfer transpose trim ubound unpack verify</Keywords>
                      <Keywords name="Words3">cdabs cdcos cdexp cdlog cdsin cdsqrt cotan cotand dcmplx dconjg dcotan dcotand decode dimag dll_export dll_import doublecomplex dreal dvchk encode find flen flush getarg getcharqq getcl getdat getenv gettim hfix ibchng identifier imag int1 int2 int4 intc intrup invalop iostat_msg isha ishc ishl jfix lacfar locking locnear map nargs nbreak ndperr ndpexc offset ovefl peekcharqq precfill prompt qabs qacos qacosd qasin qasind qatan qatand qatan2 qcmplx qconjg qcos qcosd qcosh qdim qexp qext qextd qfloat qimag qlog qlog10 qmax1 qmin1 qmod qreal qsign qsin qsind qsinh qsqrt qtan qtand qtanh ran rand randu rewrite segment setdat settim system timer undfl unlock union val virtual volatile zabs zcos zexp zlog zsin zsqrt</Keywords>
                      <Keywords name="Words4">eq and or ne ge gt le lt true false</Keywords>
                  </KeywordLists>
                  <Styles>
                      <WordsStyle name="DEFAULT" styleID="11" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="FOLDEROPEN" styleID="12" fgColor="0000FF" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="FOLDERCLOSE" styleID="13" fgColor="0000FF" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="KEYWORD1" styleID="5" fgColor="0000FF" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="KEYWORD2" styleID="6" fgColor="8000FF" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="KEYWORD3" styleID="7" fgColor="0080C0" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="KEYWORD4" styleID="8" fgColor="808080" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="COMMENT" styleID="1" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="COMMENT LINE" styleID="2" fgColor="008000" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="NUMBER" styleID="4" fgColor="FF0000" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="OPERATOR" styleID="10" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="DELIMINER1" styleID="14" fgColor="8000FF" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="DELIMINER2" styleID="15" fgColor="8000FF" bgColor="FFFFFF" fontName="" fontStyle="0" />
                      <WordsStyle name="DELIMINER3" styleID="16" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" />
                  </Styles>
              </UserLang>

          It's not perfect, but it's pretty good.

          Regards.
          Greg

           
          • Nobody/Anonymous

            What about changing the source code?
            Threr is a file in the source code, named "LexFortran.cxx". In this file there are definitions of when the program uses different styles and colors.
            I thought of two possibilities:
            1. assignment of isFixFormat variable to be true, but I haven't found where the definitions of this case are.
            2. changing in the "Handle the fix format generically" section the conditions of toLineStart variable.
            I don't have the right compiler to try it, and I also don't understand so much in programming, so I'm afraid to try it.
            But, what do tou think about it?
            Morag

             
    • Nobody/Anonymous

      Yes I am sure that the FORTRAN is column sensitive.

      Visual Studio has the nice solid green bar in column 6. I'm not asking for that.

      I just want to have a line that starts with a tab then has a control function like "IF" or "FOR" do be highlighted properly.

      It works wonderfully if I replace the tabs with 6 spaces or whatever. And it works millions of times better than TextPad or anything else I've tried.

      If WinMerge can interpret these files correctly, why can't Notepad++. It is hands-down the best text editor I've ever used. I don't think I'm asking for all that much.

      Thanks for the help.

       
    • Greg Bullock

      Greg Bullock - 2006-09-06

      Probably your best solution is to
      (1) set the tab width in NP++ to be 6 spaces, and
      (2) have NP++ replace tab characters with spaces.
      The highlighting should work after that; although, with existing files, you'll probably have to either manually replace the tab characters with 6 spaces or save the file then reopen it (which might trigger the replacement).

      If I'm not mistaken, the Fortran standard does not specify how many spaces a tab character counts for, so if you're using tab characters in your source files, I believe you're at the whims of your compiler interpretation of that character (some will read it as you intend: as 6 spaces, but others will read it as a single character and give you error messages).

      In other words, I believe NP++'s behavior with respect to the tab character is within the Fortran standard, if not within your compiler's extensions to the standards.  That your compiler is interpreting the tab character as you intend, and NP++ is not, should be viewed as a virtue of your compiler, and not a drawback of NP++.

      Regards.
      Greg

       
  • jwippler

    jwippler - 2010-09-14

    Dear Greg,

    thank you very much for the code. But please allow me one stupid question: What will I have to do with it?

    Thank you so much in advance.

    Best Regards

    Johannes

     
  • cchris

    cchris - 2010-09-15

    Open the User Define Language panel (from the View menu or the tool bar).
    The click "Import" and select your file.
    The language wll be available without the need for importing next time, and will be available for highlighting at once.

    Can I post this on NpWiki++?
    I'll check the way the Fortran lexer is set, and see if something can be done on Notepad++ side.

    CChris

     
  • cchris

    cchris - 2010-09-15

    The problem can be solved on Notepad++ side by either:
    * Defining a new Fortran 90 language, the lexer of which has ID SCLEX_FORTRAN
    * Adding a specific attribute in langs.xml to switch between F77 and F90 mode. The setFortranLexer() procedure would select the internal lexer ID to use (SCLEX_F77 or SCLEX_FORTRAN) depending on this settting.

    The internal Fortran lexer defines two modules, each for each ID, and that's how isFixFormat is being figured out.

    Any thoughts?
    CChris