% fox-listings.sty — LaTeX listings language definition for COSYScript (COSY INFINITY)
%
% Copyright (c) 2026 Eremey Valetov
%
% This work may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, version 1.3c or later.
% The latest version is at https://www.latex-project.org/lppl/
% This work has the LPPL maintenance status "maintained".
% The Current Maintainer is Eremey Valetov.
%
% Usage:
%   \usepackage{fox-listings}
%   \begin{lstlisting}[language=FOX]
%     VARIABLE X 1 ;
%     X := SIN(0.5) ;
%     WRITE 6 X ;
%   \end{lstlisting}
%
% Or inline: \lstinline[language=FOX]{VARIABLE X 1 ;}
%
% Styles: FOXcolor (screen), FOXmono (print)
%   \lstset{style=FOXcolor}

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{fox-listings}[2026/03/01 v1.1 COSYScript (FOX) language definition for listings]

\RequirePackage{listings}
\RequirePackage{xcolor}

% ── Language Definition ──────────────────────────────────────────────────────

\lstdefinelanguage{FOX}{
  % Case insensitive
  sensitive=false,
  % Comments: nested { ... }
  morecomment=[n]{\{}{\}},
  % Strings: single-quoted with '' escape
  morestring=[b]',
  % Keywords: control flow and declarations
  morekeywords=[1]{
    BEGIN, END, VARIABLE, FUNCTION, ENDFUNCTION, PROCEDURE, ENDPROCEDURE,
    IF, ELSEIF, ENDIF, WHILE, ENDWHILE, LOOP, ENDLOOP,
    FIT, ENDFIT, PURE, ENDPURE, PLOOP, ENDPLOOP,
    INCLUDE, SAVE, QUIT, DEBUG, WRITE, READ,
    OPENF, OPENFB, CLOSEF, REWF, BACKF,
    GUIIO, GUISET
  },
  % Intrinsic functions
  morekeywords=[2]{
    ABS, INT, NINT, SQRT, ISRT, ISRT3, SQR, SIN, COS, TAN,
    ASIN, ACOS, ATAN, ATAN2, VATAN2, SINH, COSH, TANH,
    LOG, EXP, MOD, ERF, WERF, NOT, TRIM, LTRIM,
    RE, ST, LO, CM, VE, DA, CD,
    CONS, REAL, IMAG, CMPLX, CONJ, NORM,
    TYPE, LENGTH, VARMEM, VARPOI, GRIU,
    VMIN, VMAX, SIG, IM, COEF,
    LRE, LST, LLO, LCM, LVE, LDA, LCD, LGR,
    MAX, MIN, SS, R, S, SF, SI, WABS, ILAST, IPOS
  },
  % Intrinsic procedures
  morekeywords=[3]{
    MEMALL, MEMFRE, MEMDPV, MEMWRT, SCRLEN,
    CPUSEC, PWTIME, PNPRO, PROOT, SLEEP, MOS,
    ARGGET, READS, READB, WRITEB, READM, WRITEM,
    DAINI, DANOT, DANOTW, DAEPS, MEPSMIN,
    DAFSET, DAFILT, DAPEW, DAREA, DAPRV, DAREV,
    DAFLO, CDFLO, DAGMD, RERAN, DARAN,
    DADIU, DADMU, DADER, DAINT, DAPLU, DASCL,
    DATRN, DASGN, DAPEE, DAPEA, DACODE, DANORO,
    DANORS, DACLIW, DACQLC, DAPEP, DANOW, DAEST,
    MTREE, CDF2, CDNF, CDNFDA, CDNFDS,
    LINV, LDET, LEV, MBLOCK, LSLINE, SUBST,
    RSTCRE, RECST, VELSET, VELGET, VEDOT, VEUNIT, VEZERO,
    IMUNIT, LTRUE, LFALSE, INTPOL, CLEAR, CONTINUE,
    SVSCR, TRCAP, WORDS, FIELDS, MI, FOURIERTRANS,
    CONFIG_SET, CONFIG_SET_ECHO, CONFIG_Q, IN_CONFIG, CONFIG
  },
  % Beam physics procedures
  morekeywords=[4]{
    OV, DEF, RP, RPP, RPE, RPR, RPM, RPS,
    UM, SM, SNM, AM, ANM, PM, PSM, WM, RM, MR,
    MT, PA, PT, SB, SP, SBE, DL,
    MQ, MH, MO, MD, MZ, M5, MM, MMS,
    EQ, EH, EO, ED, EZ, E5, EM, EMS, MS, ES,
    EC, ECL, ESP, DI, DP, MC,
    WF, WC, WI, RF, CMR, CML, CMS, CMSI, CMST, CMG,
    CEL, CEA, CEG, GLS, GL, GP, GMS, GMP, GMF, GM,
    FR, FC, FC2, FD, FD2, FP,
    PS, PG, PGE, BP, EP, PTY,
    CR, SR, SSR, ER, SCDE,
    ENVEL, ENCL, TR, TRT, TRR, TRAP, AP,
    PRAY, WRAY, RRAY, SRAY, LRAY, ARAY,
    PSPI, WSPI, RSPI, SSPI, LSPI, ASPI,
    TS, TSC, RFILT, GT, MCM, ECM, AR, RR,
    WA, WAS, WL, TSP, TSS, CSG, CQG, SET, MSC
  },
  % Graphics procedures
  morekeywords=[5]{
    GRMOVE, GRDRAW, GRDOT, GRTRI, GRPOLY, GRCURV,
    GRCHAR, GRCOLR, GRWDTH, GRPROJ, GRZOOM, GRMIMA,
    GREPS, GRSTYL, GROUTF, PP
  },
  % Constants and global variables
  morekeywords=[6]{
    AMU, AMUMEV, EZERO, CLIGHT, PI,
    MAP, RAY, SPOS, E0, M0, Z0, V0, P0, K0, T0,
    CHIM, CHIE, ETA, DEGRAD,
    BEAMX, BEAMY, BEAMT, FRNGI, FRNGM,
    SPNX, SPNY, SPNZ,
    SIGMA, ME, MA, SE, OE, PB, DER, INTEG, NMON, CE
  },
  % Assignment operator
  literate={:=}{{{\bfseries :=}}}2,
  % Number format (integer, float, scientific)
  alsoletter={.},
}

% ── Color Scheme ─────────────────────────────────────────────────────────────

\definecolor{foxcomment}{RGB}{106,153,85}
\definecolor{foxstring}{RGB}{206,145,120}
\definecolor{foxkeyword}{RGB}{86,156,214}
\definecolor{foxfunction}{RGB}{220,220,170}
\definecolor{foxprocedure}{RGB}{78,201,176}
\definecolor{foxbeam}{RGB}{184,215,163}
\definecolor{foxconstant}{RGB}{181,137,206}
\definecolor{foxnumber}{RGB}{181,206,168}

\lstdefinestyle{FOXcolor}{
  language=FOX,
  basicstyle=\ttfamily\small,
  commentstyle=\color{foxcomment}\itshape,
  stringstyle=\color{foxstring},
  keywordstyle=[1]\color{foxkeyword}\bfseries,
  keywordstyle=[2]\color{foxfunction},
  keywordstyle=[3]\color{foxprocedure},
  keywordstyle=[4]\color{foxbeam},
  keywordstyle=[5]\color{foxprocedure},
  keywordstyle=[6]\color{foxconstant},
  numberstyle=\color{foxnumber}\small,
  showstringspaces=false,
  breaklines=true,
  frame=single,
  framerule=0.5pt,
  tabsize=3,
}

% ── Monochrome Style (for print) ────────────────────────────────────────────

\lstdefinestyle{FOXmono}{
  language=FOX,
  basicstyle=\ttfamily\small,
  commentstyle=\itshape,
  keywordstyle=[1]\bfseries,
  keywordstyle=[2]\bfseries,
  keywordstyle=[3]{},
  keywordstyle=[4]{},
  keywordstyle=[5]{},
  keywordstyle=[6]{},
  showstringspaces=false,
  breaklines=true,
  frame=single,
  framerule=0.5pt,
  tabsize=3,
}

\endinput
