[lazarus] OpenFileAtCursor

Jarto Tarpio jarto at starsoft.fi
Mon Dec 10 16:37:49 EST 2001


Mattias wrote:
> I extended the path with the project directory and the lazarus
> source directory. Also the files are checked like fpc
> case-sensitive, lowercase, uppercase. 

Here's another version. I shortened FindPasFile a bit and made a  
GetFilenameAtRowCol (mostly from synedit) which also works with 
include files and directories too.

/jarto


function TMainIDE.DoOpenFileAtCursor(Sender: 
TObject):TModalResult;
var SE: TSourceEditor;
    FName,SPath: String;

  function FindPasFile(var FName: String; SPath: String): Boolean;
  //  Searches for FName in Spath
  //  If FName is not found, we'll check extensions pp and pas too
  //  Returns true if found. FName contains the full file+path in that case
  var TempFile,TempPath,CurPath,Ext: String;
      i,p,c: Integer;
  begin
    if SPath='' then SPath:='.';
    Result:=true;
    for i:=0 to 2 do begin
      case i of
        1: Ext:='.pp';
        2: Ext:='.pas';
        else Ext:='';
      end;
      TempPath:=SPath;
      while TempPath<>'' do begin
        p:=pos(';',TempPath);
        if p=0 then p:=length(TempPath)+1;
        CurPath:=copy(TempPath,1,p-1)+'/';
        Delete(TempPath,1,p);
        for c:=0 to 2 do begin
          case c of
            0: TempFile:=FName;
            1: TempFile:=LowerCase(FName);
            2: TempFile:=UpperCase(FName);
          end;
          TempFile:=ExpandFileName(CurPath+TempFile+Ext);
          if FileExists(TempFile) then begin
            FName:=TempFile;
            exit;
          end;
        end;
      end;
    end;
    result:=false;
  end;

  function GetFilenameAtRowCol(XY: TPoint): string;
  var
    Line,IdChars: string;
    Len, Stop: integer;
  begin
    Result := '';
    if (XY.Y >= 1) and (XY.Y <= SE.EditorComponent.Lines.Count) then begin
      Line := SE.EditorComponent.Lines.Strings[XY.Y - 1];
      Len := Length(Line);
      if (XY.X >= 1) and (XY.X <= Len + 1) then begin
        IdChars := ',;:[]{}() ''';
        Stop := XY.X;
        while (Stop <= Len) and (pos(Line[Stop],IdChars)=0) do
          Inc(Stop);
        while (XY.X > 1) and (pos(Line[XY.X - 1],IdChars)=0) do
          Dec(XY.X);
        if Stop > XY.X then
          Result := Copy(Line, XY.X, Stop - XY.X);
      end;
    end;
  end;

begin
  writeln('TMainIDE.DoOpenFileAtCursor');
  Result:=mrCancel;
  SE:=SourceNoteBook.GetActiveSE;
  if SE=nil then exit;
  FName:=GetFilenameAtRowCol(SE.EditorComponent.CaretXY);
  if FName='' then exit;
  SPath:=ExtractFilePath(Project.ProjectFile)+';'
        +EnvironmentOptions.LazarusDirectory+'/lcl;'
        +EnvironmentOptions.LazarusDirectory+'/designer';
  if FindPasFile(FName,SPath) then begin
    result:=mrOk;
    EnvironmentOptions.LastOpenDialogDir:=ExtractFilePath(FName);
    if DoOpenEditorFile(FName,false)=mrOk then begin
      EnvironmentOptions.AddToRecentOpenFiles(FName);
      SaveEnvironment;
    end;
  end;
end;






More information about the Lazarus mailing list