• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

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
0
marajin
Asked:
marajin
  • 4
  • 3
  • 2
1 Solution
 
ILECommented:
If i andwrstand u corrrectly you wont to auto add counter tag when u finished tyoed a tag

becouse i did the all coding in the memo coponent
100 % working solution


procedure TForm1.Memo1Change(Sender: TObject);
var s:string;
   t1,t2:integer;
begin
s:=memo1.lines[memo1.caretpos.y];
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,t2-t1+1);
        if pos('<\'+copy(s,t1+1,t2-t1),s)=0 then memo1.lines[memo1.CaretPos.Y]:=s+'<\'+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 :))??
0
 
marajinAuthor Commented:
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(Sender: 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
0
 
ILECommented:
i dont realy anderstand what u meen with NEXT line of text

meybe i dont andersand HTML :))

put me a example what u wont



0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
marajinAuthor Commented:
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
0
 
Slick812Commented:
s:=SynEdit1.lines[Y-1];
0
 
marajinAuthor Commented:
"s:=SynEdit1.lines[Y-1]; "

Doesn't work. Nothing at all happens
0
 
Slick812Commented:
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(Sender: 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;
0
 
Slick812Commented:
whoops, I forgot to check and see if they left the tag to type somewhere else - -


  private
    { Private declarations }
    Str1: String;
    Trap: Boolean;
    LastPos: Integer;


procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
var
SelPos: Integer;
begin
inherited;
if Key = ' ' then Trap := False;
if Key = '>' then
  begin
  if Length(Str1) < 3 then Exit;
  Str1 := Str1+'>';
  Trap := False;
  Label2.Caption := Str1;
  SelPos := memo1.SelStart;
  Memo1.SelText := Str1;
  memo1.SelStart := SelPos;
  end;

if Trap then
  begin
  SelPos := memo1.SelStart;
  if SelPos - LastPos <> 1 then
    begin
    Trap := False;
    Str1 := '';
    end;
  LastPos := SelPos;
  Str1 := Str1+Key;
  end;

if Key = '<' then
  begin
  Trap := True;
  Str1 := '<\';
  LastPos := memo1.SelStart;
  end;

end;
0
 
marajinAuthor Commented:
Thanks for that, yeah it seems to work just fine now.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now