[Lazarus] WriteLn back to the GUI

Sven Barth pascaldragon at googlemail.com
Wed Oct 23 17:58:26 CEST 2013

Am 23.10.2013 17:50, schrieb ListMember:
> On 2013-10-23 18:10, Sven Barth wrote:
>> Am 23.10.2013 17:06, schrieb Sven Barth:
>>> Am 23.10.2013 08:59, schrieb ListMember:
>>>> Hello,
>>>> I am trying to find where WriteLn is declared; but, apparently, 
>>>> WriteLn is among the select few that Lazarus will ignore 'Find 
>>>> Declaration' requests --at least that's how it behaves (latest 
>>>> Lazarus stable) on my side.
>>>> My ultimate goal, after finding where it is declared, is to locally 
>>>> modify that part of the sources so that (instead of writing to the 
>>>> console) it will use a global callback to display whatever 
>>>> WriteLn'ed in a TMemo.
>>> Write(Ln), Read(Ln), WriteStr and ReadStr are compiler magic 
>>> functions which are not available in Pascal source.
>>>> Or, is there already an alternative to redirect console output to 
>>>> the running GUI?
>>> The Pascal IO mechanism relies on variables of type "file" or of 
>>> type "text". For the latter you can specify your own "textdriver" 
>>> with which you can replace the e.g. "Output" variable (which is used 
>>> by "Write(Ln)" without a file argument) which can then redirect the 
>>> output to something else. For an example of how to implement a text 
>>> driver you can take a look at the "StreamIO" ( 
>>> http://www.freepascal.org/docs-html/fcl/streamio/index.html ) unit 
>>> provided with Free Pascal (in 
>>> $fpcdir/packages/fcl-base/src/streamio.pp). There a text driver is 
>>> implemented which allows to "assign" streams to TextFile variables 
>>> and thus to use Write(Ln)/Read(Ln) on them. You'll need to implement 
>>> something similar for e.g. a memo (or whatever method you want to 
>>> use to display the output).
>> And I just found an example which already does this for TCustomEdit 
>> descendants: http://kd5col.info/swag/DELPHI/0017.PAS.html
>> I've not tested it in FPC, so maybe small adjustments might be 
>> necessary...
> Wow.
> This is far too (incredibly) elaborate.
> It's a bit like giving me plans to set up an army to invade a country 
> when all I want to do is to visit it as a tourist :)
No, this is the officially supported way to interfere with Pascal's IO 
> As I mentioned in my first post, I am prepared to alter the compiler 
> sources; and I had this in mind:
> I would like to add these to a common unit --such as globals.pas
What would adding a callback to "globals.pas" provide? The string that 
is written by Writeln is created at runtime and not at compile time.
> type
>   TMyWriteLnCallback = procedure(AText: String);
> var
>   GMyWriteLnCallback: TMyCallback = nil;
> and then, wherever the 'compiler magic' for WriteLn is, use something 
> like
> if Assigned(GMyWriteLnCallback) then GMyWriteLnCallback(TextStuff)
> else {do the usual console output}
> So, could someone tell me where I have to inject these lines.
I've already written you how you can do it. Write a text driver which 
uses your callback function.

This is much easier than trying to understand how the compiler handles 
Writeln plus you don't need to manipulate the compiler or the RTL for that.


More information about the Lazarus mailing list