marajin
asked on
Automatic counter-tags in HTML Editor
Ok, I've knocked together an MDI Raw HTML editor with a 'preview' tab to help the user see their page taking shape without cluttering up the taskbar with browser windows. TabControl at the bottom to make navigation easy, and a SynEdit + HTML highlighter to make it easy reading. It's all working nice and spiffy, but the one last feature I want in there, I'm having trouble with.
I'd like it to be able to automatically add a counter tag when you type a normal tag in. e.g.
You type <table> and it adds </table> to the line.
Thanks for your time,
Marajin/Tom
I'd like it to be able to automatically add a counter tag when you type a normal tag in. e.g.
You type <table> and it adds </table> to the line.
Thanks for your time,
Marajin/Tom
ASKER
It seems to half work, That is, it grabs the next line of text and puts the counter tag in....after I do some messing about.
See it's a SynEdit component (http://synedit.sourceforge.net) not a normal memo...
changing the code to this:
procedure TMDIChild.SynEdit1Change(S ender: TObject);
var
s:string;
t1,t2:integer;
Y:integer;
begin
Y:=SynEdit1.CaretY;
s:=SynEdit1.lines[Y];
t2:=SynEdit1.Caretx;
if (t2>0) and (s[t2]='>') then
begin
for t1:=t2 downto 0 do
if (s[t1]='<') then
begin
if s[t1+1]='/' then break;
label1.caption:=copy(s,t1, t2-t1+1);
Y:=SynEdit1.CaretY;
if pos('</'+copy(s,t1+1,t2-t1 ),s)=0 then SynEdit1.lines[Y-1]:=(s+'< /'+copy(s, t1+1,t2-t1 ));
break;
end;
end;
end;
Gets me nearly there, only it grabs the NEXT line of text, and makes the counter tag.... If I use a Y-1 for the initial value too, which _should_ fix this, nothing happens at all.
This is also Delphi 6
See it's a SynEdit component (http://synedit.sourceforge.net) not a normal memo...
changing the code to this:
procedure TMDIChild.SynEdit1Change(S
var
s:string;
t1,t2:integer;
Y:integer;
begin
Y:=SynEdit1.CaretY;
s:=SynEdit1.lines[Y];
t2:=SynEdit1.Caretx;
if (t2>0) and (s[t2]='>') then
begin
for t1:=t2 downto 0 do
if (s[t1]='<') then
begin
if s[t1+1]='/' then break;
label1.caption:=copy(s,t1,
Y:=SynEdit1.CaretY;
if pos('</'+copy(s,t1+1,t2-t1
break;
end;
end;
end;
Gets me nearly there, only it grabs the NEXT line of text, and makes the counter tag.... If I use a Y-1 for the initial value too, which _should_ fix this, nothing happens at all.
This is also Delphi 6
i dont realy anderstand what u meen with NEXT line of text
meybe i dont andersand HTML :))
put me a example what u wont
meybe i dont andersand HTML :))
put me a example what u wont
ASKER
Say you have this in the SynEdit:
<html>
<body>
</body>
</html>
You type in <tag> between the <html> and <body> so you get:
<html>
<tag>
<body>
</body>
</html>
The code sees that tag and the result is:
<html>
<body></body>
<body>
</body>
</html>
so it is using the next line down (<body>) instead of <Tag> which the user typed in
<html>
<body>
</body>
</html>
You type in <tag> between the <html> and <body> so you get:
<html>
<tag>
<body>
</body>
</html>
The code sees that tag and the result is:
<html>
<body></body>
<body>
</body>
</html>
so it is using the next line down (<body>) instead of <Tag> which the user typed in
s:=SynEdit1.lines[Y-1];
ASKER
"s:=SynEdit1.lines[Y-1]; "
Doesn't work. Nothing at all happens
Doesn't work. Nothing at all happens
I don't have that component, but in a TMemo, I would use the OnKeyDown Event, so I could monitor all of the key input,, , , , here's my sugestion
private
{ Private declarations }
Str1: String;
Trap: Boolean;
procedure TForm1.Memo1KeyPress(Sende r: TObject; var Key: Char);
var
SelPos: Integer;
begin
inherited;
if Key = ' ' then Trap := False;
if Key = '>' then
begin
Str1 := Str1+'>';
Trap := False;
Label2.Caption := Str1;
SelPos := memo1.SelStart;
Memo1.SelText := Str1;
memo1.SelStart := SelPos;
end;
if Trap then
Str1 := Str1+Key;
if Key = '<' then
begin
Trap := True;
Str1 := '<\';
end;
end;
private
{ Private declarations }
Str1: String;
Trap: Boolean;
procedure TForm1.Memo1KeyPress(Sende
var
SelPos: Integer;
begin
inherited;
if Key = ' ' then Trap := False;
if Key = '>' then
begin
Str1 := Str1+'>';
Trap := False;
Label2.Caption := Str1;
SelPos := memo1.SelStart;
Memo1.SelText := Str1;
memo1.SelStart := SelPos;
end;
if Trap then
Str1 := Str1+Key;
if Key = '<' then
begin
Trap := True;
Str1 := '<\';
end;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for that, yeah it seems to work just fine now.
becouse i did the all coding in the memo coponent
100 % working solution
procedure TForm1.Memo1Change(Sender:
var s:string;
t1,t2:integer;
begin
s:=memo1.lines[memo1.caret
t2:=Memo1.CaretPos.X;
if (t2>0) and (s[t2]='>') then
begin
for t1:=t2 downto 0 do
if (s[t1]='<') then
begin
if s[t1+1]='\' then break;
label1.caption:=copy(s,t1,
if pos('<\'+copy(s,t1+1,t2-t1
break;
end;
end;
end;
writen in delphi 7
in earlier versions of delphi meybe is memo1.caretpos.line and memo1.caretpos.col instead of memo1.caretpos.x and .....y
easy arent :))??