lib/misc.ml \
lib/x11.ml \
lib/cfg.ml \
+ lib/colormap.ml \
+ $(LEXERS) \
lib/rope.ml \
lib/buf.ml \
- lib/colormap.ml \
lib/draw.ml \
lib/scrollmap.ml \
- $(LEXERS) \
lib/view.ml
TESTSRCS = \
edit.cmo edit.cmi : lib/x11.cmi lib/view.cmi lib/draw.cmi lib/buf.cmi edit.ml
edit.cmx edit.o edit.cmi : lib/x11.cmi lib/x11.cmx lib/view.cmi lib/view.cmx lib/draw.cmi lib/draw.cmx lib/buf.cmi lib/buf.cmx edit.ml
-lib/buf.cmo : lib/rope.cmi lib/misc.cmi lib/buf.cmi lib/buf.ml
-lib/buf.cmx lib/buf.o : lib/rope.cmi lib/rope.cmx lib/misc.cmi lib/misc.cmx lib/buf.cmi lib/buf.ml
-lib/buf.cmi :
+lib/buf.cmo : lib/rope.cmi lib/misc.cmi lib/colormap.cmi lib/buf.cmi lib/buf.ml
+lib/buf.cmx lib/buf.o : lib/rope.cmi lib/rope.cmx lib/misc.cmi lib/misc.cmx lib/colormap.cmi lib/colormap.cmx lib/buf.cmi lib/buf.ml
+lib/buf.cmi : lib/colormap.cmi
lib/cfg.cmo lib/cfg.cmi : lib/x11.cmi lib/cfg.ml
lib/cfg.cmx lib/cfg.o lib/cfg.cmi : lib/x11.cmi lib/x11.cmx lib/cfg.ml
lib/colormap.cmo : lib/cfg.cmi lib/colormap.cmi lib/colormap.ml
type buf = {
+ lexfn : Colormap.ctx -> Lexing.lexbuf -> unit;
path : string;
rope : Rope.t
}
| NextChar | PrevChar
| NextLine | PrevLine
+type filetype = {
+ syntax : Colormap.ctx -> Lexing.lexbuf -> unit;
+ names : string list;
+ exts : string list;
+}
+
+let filetypes = [
+ {
+ syntax = Lex_cpp.scan;
+ names = [];
+ exts = [".c"; ".h"; ".cpp"; ".hpp"; ".cc"; ".c++"; ".cxx"]
+ };
+ {
+ syntax = Lex_cpp.scan;
+ names = ["Rakefile"; "rakefile"; "gpkgfile"];
+ exts = [".rb"]
+ };
+ {
+ syntax = Lex_cpp.scan;
+ names = [];
+ exts = [".ml"; ".mll"; "mli"]
+ }
+]
+
+let pick_syntax path =
+ let name = Filename.basename path in
+ let ext = Filename.extension path in
+ let match_ftype ftype =
+ (List.exists ((=) name) ftype.names) ||
+ (List.exists ((=) ext) ftype.exts)
+ in match (List.find_opt match_ftype filetypes) with
+ | Some ft -> ft.syntax
+ | None -> Lex_cpp.scan
+
let empty =
- { path = ""; rope = Rope.empty }
+ { lexfn = Lex_cpp.scan;
+ path = "";
+ rope = Rope.empty }
let load path =
- { path = path; rope = Rope.from_string (Misc.load_file path) }
+ { lexfn = pick_syntax path;
+ path = path;
+ rope = Rope.from_string (Misc.load_file path) }
let path buf =
buf.path
let iter fn buf i =
iteri (fun i c -> (fn c)) buf i
-let make_lexfn buf =
+let make_lexer buf =
let pos = ref 0 in
- (fun bytebuf n ->
- let count = ref 0 in
- iteri (fun i c ->
- Bytes.set bytebuf !count (Char.chr c);
- incr count;
- (!count >= n)
- ) buf !pos;
- pos := !pos + !count;
- !count)
+ Colormap.({
+ scanfn = buf.lexfn;
+ lexbuf = Lexing.from_function (fun bytebuf n ->
+ let count = ref 0 in
+ iteri (fun i c ->
+ Bytes.set bytebuf !count (Char.chr c);
+ incr count;
+ (!count >= n)) buf !pos;
+ pos := !pos + !count;
+ !count)
+ })
module Cursor = struct
type csr = {
val is_bol : t -> int -> bool
val is_eol : t -> int -> bool
-val make_lexfn : t -> (bytes -> int -> int)
+val make_lexer : t -> Colormap.lexer
exception Eof
type style = Normal | Comment | Constant | Keyword | Type | PreProcessor
-(*
- String | Character | Number | Boolean
- | Variable | Function | Keyword | Operator | PreProcessor | Type
- | Statement | Special
-*)
module Span = struct
type t = { start : int; stop : int; style : int }
mutable pos : int;
}
-type lexer = ctx -> Lexing.lexbuf -> unit
+type lexer = {
+ scanfn : ctx -> Lexing.lexbuf -> unit;
+ lexbuf : Lexing.lexbuf
+}
let get_color = function
-| Normal -> Cfg.Color.Syntax.normal
-| Comment -> Cfg.Color.Syntax.comment
-| Constant -> Cfg.Color.Syntax.constant
-| Keyword -> Cfg.Color.Syntax.keyword
-| Type -> Cfg.Color.Syntax.typedef
-| PreProcessor -> Cfg.Color.Syntax.preproc
+ | Normal -> Cfg.Color.Syntax.normal
+ | Comment -> Cfg.Color.Syntax.comment
+ | Constant -> Cfg.Color.Syntax.constant
+ | Keyword -> Cfg.Color.Syntax.keyword
+ | Type -> Cfg.Color.Syntax.typedef
+ | PreProcessor -> Cfg.Color.Syntax.preproc
let make_span lbuf clr =
Span.({ start = (lexeme_start lbuf);
in
ctx.map <- SpanSet.add span ctx.map
-let make scanfn fetchfn =
- let lexbuf = Lexing.from_function fetchfn in
- let ctx = { lbuf = lexbuf; map = SpanSet.empty; pos = 0; } in
- (try while true do scanfn ctx lexbuf done
+let make lexer =
+ let ctx = { lbuf = lexer.lexbuf; map = SpanSet.empty; pos = 0; } in
+ (try while true do lexer.scanfn ctx lexer.lexbuf done
with Eof -> ());
ctx.map
type ctx
-type lexer = ctx -> Lexing.lexbuf -> unit
+type lexer = {
+ scanfn : ctx -> Lexing.lexbuf -> unit;
+ lexbuf : Lexing.lexbuf
+}
val empty : t
-val make : lexer -> (bytes -> int -> int) -> t
+val make : lexer -> t
val find : int -> t -> int
val set_color : ctx -> style -> unit
val range_start : ctx -> unit
val range_stop : ctx -> style -> unit
-
-(*
-val from_channel : lexer -> in_channel -> t
-val from_string : lexer -> string -> t
-val from_function : lexer -> (bytes -> int -> int) -> t
-*)
(* config settings. eventually move to Cfg module *)
let font = X11.font_load Cfg.font
-let font_height = let open X11 in font.height
+let font_height = X11.(font.height)
let tabglyph = 0x30
let tabwidth = 4
-let glyph_width g =
- let open X11 in g.xoff
+let glyph_width g = X11.(g.xoff)
module Cursor = struct
type t = {
let from_buffer buf width height =
{ num = 0; buf = buf;
map = Scrollmap.make buf width 0;
- clr = Colormap.make Lex_cpp.scan (Buf.make_lexfn buf) }
+ clr = Colormap.make (Buf.make_lexer buf) }
let empty width height =
from_buffer (Buf.empty) width height