Program PasteSpecial;

{**********************************************
 PasteSpecial beta 0.2a

 A script that replaces Win special chars in the clipboard.
 It can be useful for copy&paste to a new message from the web or from a Word doc.

 Ciao!

 I didn't do much to write this script! :)
 It's mainly based on the SelectiveQuotedDestructor script by Alain Guerin 
 and on the Umlaut replace script on 40tude Wiki.
 I (Enrico C , Sir and Master of www.lillathedog.net :) 
 combined those two scripts and used the result for my purposes.
  
 This script replaces the special Windows CP-1252 characters, sometimes problematical,
 with their equivalent "normal" ISO characters
 (see  http://www.cs.tut.fi/~jkorpela/www/windows-chars.html#subst ).

 You can use it when you copy any text to a new message,
 especially when you copy from a web page or from a Word document
 that may have such chars. 
        
 “Don’t write like this”
 will become            
 "Don't write like this"
      
 • They only accept florins (ƒ)…
  will became  
 * They only accept florins (f)...
    
          
 Instructions:
  
 Just run the script before pasting.
 
 STEP 1: Copy a text 
 
 STEP 2: Select Dialog main window and run the script 
 (I do that by pressing CTRL-E, the shortcut I assigned to the script),
 
 STEP 3: Paste the text in the new message composition window.
       
 Steps 1 and 3 are the usual steps when you copy and paste.
 Use step 2 (the script) if you want to "purify" a text containing special chars.
 
 (Note that you can use the script to "purify" and copy a text 
 to any document, not only Dialog new messages).  
                                                                                                              
 
 
 Installation:
 
 To use this script, create a new blank script in Custom scripts,
 then replace with this script 
 (delete the || chars at the beginning of lines, of course), 
 save and compile it.
 
It's easier if you define a shortcut or a button to run the script. 
1 Pick an Icon for this script, 
2 Write a Hint ("PasteSpecial") and close the scripting window,
3 Rightclick on the toolbar in Dialog main window,
4 select Configure buttons/shortcuts,
5 browse to Script - PasteSpecial,
6a define a Shortcut, for instance CTRL-E if you don't use it otherwise, 
6b drag the script Icon on the toolbar to create a button.

To run the script by a shortcut, select Dialog main window first 
(Alt-Tab frome the Composition window).

Here are the replaced chars

// ‚  baseline single quote    Alt+130 ==> ' apostrophe
 
// ƒ  florin                   Alt+131 ==> f 

// „  baseline double quote    Alt+132 ==> " quotation mark

// …  ellipsis                 Alt+133 ==> ... 3 dots

// †  dagger                   Alt+134 ==> superscript 1: Ή (assuming use as footnote reference) 

// ‡  double dagger            Alt+135 ==> superscript 2: ² (assuming use as footnote reference) 
       
// ˆ  circumflex accent        Alt+136 ==> ^ circumflex
       
// ‰  permile                  Alt+137 ==> o/oo
           
// Š  S Hacek                  Alt+138 ==> Sh
           
// ‹  left single guillemet    Alt+139 ==> <
           
// Œ   OE ligature             Alt+140 ==> OE 
           
// ‘  left single quote        Alt+145 ==> ' apostrophe used as single quote 
       
// ’  right single quote       Alt+146 ==> ' apostrophe used as single quote       
           
// “  left double quote        Alt+147 ==> " quotation mark 
           
// ”  right double quote       Alt+148 ==> " quotation mark 
           
// •  bullet                   Alt+149 ==> * asterisk 
           
// –  endash                   Alt+150 ==> - hyphen 
           
// —  emdash                   Alt+151 ==> -- 2 hyphens 
           
// ˜  tilde accent             Alt+152 ==> ~ tilde 
           
// ™  trademark ligature       Alt+153 ==> (TM) 
           
// š  s Hacek                  Alt+154 ==> sh 
           
// ›  right single guillemet   Alt+155 ==> > 
           
// œ   oe ligature              Alt+156 ==> oe 
           
// Ÿ  Y Dieresis               Alt+159 ==> Y  


NB: some symbols can different meanings according to language/context.
You can therefore set the script to behave differently for some newsgroups, e.g. according to language
If you want the script to behave in a special way for some English groups, for example, 
write a list of those groups in a text file. See the CONST section in the script.                                                                                 
If you want to use more languages modify the "SelectLanguage" Function section accordingly. 
          
***********************************************}

uses
   Forms,
   StdCtrls;
   
// CONST                                

//   EnglishGroups = 'C:\Documenti\Short English groups.txt';
//   ItalianGroups = 'C:\Documenti\Short English groups.txt';
//   GermanGroups = 'C:\Documenti\Short German groups.txt';
//   FrenchGroups = 'C:\Documenti\Short German groups.txt';
   
TYPE
   TLanguage = (LGeneral, LEnglish, LItalian, LGerman, LFrench);     
  // TDayTime = (DTMorning, DTDay, DTAfternoon, DTEvening, DTNight);


VAR                          
   DummyForm : TForm;
   DummyMemo : TMemo;                                 
   DummyEdit : TEdit;
   HeaderName : String;
   WithDigit : boolean;
   s:string;   
   
   
   function StringReplace(S, OldPattern, NewPattern: string): string;
 var
   SearchStr, Patt, NewStr: string;
   Offset: Integer;
 begin
   SearchStr := S;
   Patt := OldPattern;
   NewStr := S;
   Result := '';
   while SearchStr <> '' do
   begin
     Offset := AnsiPos(Patt, SearchStr);
     if Offset = 0 then
     begin
       Result := Result + NewStr;
       Break;
     end;
     Result := Result + Copy(NewStr, 1, Offset - 1) + NewPattern;
     NewStr := Copy(NewStr, Offset + Length(OldPattern), 2147483647);
     SearchStr := Copy(SearchStr, Offset + Length(Patt), 2147483647);
   end;
end;


FUNCTION FindLanguage(VAR List : TStringList; LanguageFileName: string; NewsHeader : string): boolean;

begin
   result := false;
   list.LoadFromFile(LanguageFileName);
   try
writeln(inttostr(list.indexof(NewsHeader)));
   Result := list.indexof(NewsHeader) > -1;
(*  where each line is a newsgroup in the language tested, e.g. English:
news.software.readers
carlokok.public.pascalscript
*)
      
   except
      writeln('(error)')
   end;
end;

   
function SelectLanguage(NewsHeader : String):TLanguage;

var 
   list: TStringList;


begin                                      
writeln(NewsHeader);
   Result := LGeneral;
   list := TStringList.Create();

   try

//   IF FindLanguage(List, EnglishGroups, NewsHeader) THEN
//      Result := LEnglish
(*
   ELSE 
      IF FindLanguage(List, GermanGroups, NewsHeader) THEN
      	Result := LGerman*);

   finally
   list.free;
   end;    
(* or in code option:
   IF (POS('news.software.readers', NewsHeader) > 0) 
   OR (POS('carlokok.public.pascalscript', NewsHeader) > 0) THEN
      Result := LEnglish;
but it is case sensitive in this way ;-)                                   
*)

end;                                    
   
BEGIN                
   DummyForm := TForm.Create(Nil);
   try
   DummyMemo := TMemo.Create(DummyForm);
   DummyMemo.Parent := DummyForm;
   DummyMemo.WordWrap := false;
   DummyMemo.PasteFromClipBoard;
   IF DummyMemo.Lines.Count > 0 THEN
   BEGIN
      DummyEdit := TEdit.Create(DummyForm);
      try
      DummyEdit.Parent := DummyForm;
      ADo('NewgroupPane');
      Ado('Copy');
      DummyEdit.PasteFromClipBoard;
      IF Pos('(', DummyEdit.Text) > 0 THEN
         HeaderName  := copy(DummyEdit.Text, 1, Pos('(', DummyEdit.Text)-1 )
      ELSE
         HeaderName := DummyEdit.Text;
      HeaderName := trim(headername);
      WHILE Pos(#9, HeaderName)> 0 DO
        delete(HeaderName,Pos(#9, HeaderName),1);
      IF Length(HeaderName) >0 THEN
      	WithDigit := (HeaderName[1] >='0') AND (HeaderName[1] <='9')
      ELSE
         WithDigit := false;
      WHILE WithDigit AND (Length(HeaderName) >0) DO                 
      BEGIN
         Delete(HeaderName, 1, 1); 
         IF Length(HeaderName) >0 THEN
      	    WithDigit := (HeaderName[1] >='0') AND (HeaderName[1] <='9')
         ELSE
            WithDigit := false;
      END;  
      WHILE Pos(#9, HeaderName)> 0 DO
        delete(HeaderName,Pos(#9, HeaderName),1);
      
      
         s:=DummyMemo.text;
         
         CASE SelectLanguage(HeaderName) OF
            
            LGeneral :

            begin
        
            s:=StringReplace(s,'“','"'); // virgolette inglesi sx con normali
            s:=stringreplace(s,'”','"'); // virgolette inglesi dx con normali                                 
            
// ‚  baseline single quote    Alt+130 ==> apostrophe
         s:=stringreplace(s,'‚',#39);                             
 
// ƒ  florin                   Alt+131 ==> f 
           s:=stringreplace(s,'ƒ','f');                             

// „  baseline double quote    Alt+132 ==> " quotation mark
           s:=stringreplace(s,'„','"');                             

// …  ellipsis                 Alt+133 ==> ... 3 dots
           s:=stringreplace(s,'…','...');                             

// †  dagger                   Alt+134 ==> superscript 1: Ή (assuming use as footnote reference) 
           s:=stringreplace(s,'†','Ή');                             

// ‡  double dagger            Alt+135 ==> superscript 2: ² (assuming use as footnote reference) 
           s:=stringreplace(s,'‡','²');                             

// ˆ  circumflex accent        Alt+136 ==> ^ circumflex
           s:=stringreplace(s,'ˆ','^');                             

// ‰  permile                  Alt+137 ==> o/oo
           s:=stringreplace(s,'‰','o/oo');     
           
// Š  S Hacek                  Alt+138 ==> Sh
           s:=stringreplace(s,'Š','Sh');     
           
// ‹  left single guillemet    Alt+139 ==> <
           s:=stringreplace(s,'‹','<');     
           
// Œ   OE ligature              Alt+140 ==> OE 
           s:=stringreplace(s,'Œ','OE');     
           
// ‘  left single quote        Alt+145 ==> ' apostrophe used as single quote 
           s:=stringreplace(s,'‘',#39);     

// ’  right single quote       Alt+146 ==> ' apostrophe used as single quote       
           s:=stringreplace(s,'’',#39);     
           
// “  left double quote        Alt+147 ==> " quotation mark 
           s:=stringreplace(s,'“','"');     
           
// ”  right double quote       Alt+148 ==> " quotation mark 
           s:=stringreplace(s,'”','"');     
           
// •  bullet                   Alt+149 ==> * asterisk 
           s:=stringreplace(s,'•','*');     
           
// –  endash                   Alt+150 ==> - hyphen 
           s:=stringreplace(s,'–','-');     
           
// —  emdash                   Alt+151 ==> -- 2 hyphens 
           s:=stringreplace(s,'—','--');     
           
// ˜  tilde accent             Alt+152 ==> ~ tilde 
           s:=stringreplace(s,'˜','~');     
           
// ™  trademark ligature       Alt+153 ==> (TM) 
           s:=stringreplace(s,'™','(TM)');     
           
// š  s Hacek                  Alt+154 ==> sh 
           s:=stringreplace(s,'š','sh');     
           
// ›  right single guillemet   Alt+155 ==> > 
           s:=stringreplace(s,'›','>');     
           
// œ   oe ligature              Alt+156 ==> oe 
           s:=stringreplace(s,'œ','oe');     
           
// Ÿ  Y Dieresis               Alt+159 ==> Y  
           s:=stringreplace(s,'Ÿ','Y');     

            
            end;
         
         
         
            LEnglish :
                   
            begin
        
// insert chars replace commands here
        
            end;
         
                   
         // LItalian :
//       insert chars replace commands here
            
         // LGerman :
//       insert chars replace commands here  
                 
         // LFrench :
//       insert chars replace commands here         
          
         END;
   
      DummyMemo.text:=s;
      DummyMemo.SelectAll;
      DummyMemo.CopyToClipBoard;
      beep;
      finally           
         writeln('1');
         dummyedit.free;
      end; 
   END;
   finally
   beep;
   writeln('2');
   DummyForm.Free
   end          
   
END.