Recent

Author Topic: Synedit: how to detect language when you open file  (Read 1025 times)

flax57

  • New Member
  • *
  • Posts: 12
Synedit: how to detect language when you open file
« on: December 25, 2020, 07:43:54 am »
hi @ll

how to detect language when you open file in synedit and set it by
this command :

if (is html code) then SynEdit1.Highlighter:=SynHTMLSyn1;
if (is php code) then SynEdit1.Highlighter:=SynPHPSyn1;

Handoko

  • Hero Member
  • *****
  • Posts: 5515
  • My goal: build my own game engine using Lazarus
Re: Synedit: how to detect language when you open file
« Reply #1 on: December 25, 2020, 07:58:44 am »
Not the best but probably the easiest way is to use ExtractFileExt:
https://www.freepascal.org/docs-html/rtl/sysutils/extractfileext.html

For example:
Code: Pascal  [Select][+][-]
  1. case ExtractFileExt(Filename).ToLower of
  2.   '.html': SynEdit1.Highlighter := SynHTMLSyn1;
  3.   '.php':  SynEdit1.Highlighter := SynPHPSyn1;
  4. end;
« Last Edit: December 25, 2020, 08:11:05 am by Handoko »

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Synedit: how to detect language when you open file
« Reply #2 on: December 25, 2020, 08:05:47 am »
You can either set it from the file's extension or look for some characteristic of the file. For example, if a file has a extension pattern of ".?htm*" or starts (inside a certain threshold, to deal with files starting with a comment) with "<!doctype html" or "<html>" then it's an html file, and so on. That's basically what the file *nix utility and most (hilighting-)editors do.

IIRC, there was (is?) a function somewhere in SynEdit's source that did something but I don't remember exactly where or how it's called, sorry.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Bart

  • Hero Member
  • *****
  • Posts: 5680
    • Bart en Mariska's Webstek
Re: Synedit: how to detect language when you open file
« Reply #3 on: December 25, 2020, 12:56:12 pm »
From my personal editor:

Code: Pascal  [Select][+][-]
  1. function TEditor.GuessSyntaxFromString(S: String): TEditorFileType;
  2. const
  3.   Shebang = '#!';
  4. begin
  5.   //Debugln('TEditor.GuessSyntaxFromString, S = "',S,'"');
  6.   Result := eftNone;
  7.   S := TrimLeft(S);
  8.   if (Pos(Shebang,S) = 1) and ((Pos('/bin/bash',S) > 0) or (Pos('/bin/sh',S) > 0)) then Result := eftUnixShell
  9.   else if (Pos(Shebang,S) = 1) and (Pos('/perl',S) > 0) then Result := eftPerl
  10.   else if Pos('<!DOCTYPE HTML',UpperCase(S)) = 1 then Result := eftHtml
  11.   else if Pos('<?XML VERSION',UpperCase(S)) = 1 then Result := eftXml
  12.   else if Pos('#INCLUDE <',UpperCase(S)) = 1 then Result := eftC;
  13.   //Debugln('TEditor.GuessSyntaxFromString: Result = ',eftNames[Result]);
  14.   //Debugln('TEditor.GuessSyntaxFromString End');
  15. end;

I invoke this if the filename has no extension (with S being the first 50 or so lines of the file).
Simple, crude, works in most simple cases.

Bart

 

TinyPortal © 2005-2018