// -*- ASCII:EDT -*- // smartLink.edt // Anthony Roy ////////////////////////////////////// BeginGroup; LetReg(0,""); LetReg(1,""); //Email search string LetReg(3,`<@{ }+$~[" @"]$\@+{+$~[" @."]$\.}+^{>|$[" @."]$}@{ }>`); //URL search string LetReg(4,'@{http|ftp{{s}|{}}:\/\/}+$Alpha+Numeric$\.@{+$Alpha+Numeric+["-_"]$\.}+$Alpha+Numeric$'); // |Protocol || Domain | LetReg(2,'{%!4}|{{\/}|{@{\.\.\/}}}{{\/}|{}@$Alpha+Numeric+["-_"]$@{\/+$Alpha+Numeric+["-_"]$}}{\.+$Alpha+Numeric$}|{{\/}|{}}{\#@$Alpha+Numeric$}|{}'); // | relative url || \folder || filename || Target | LetReg(4,'<@{ }%!2{{\?@$Alpha+Numeric+["+=-_&;:,.$"]$}|{}}@{ }>'); // | query | LetReg(2,""); // Edit the next line to set the default link type to either email or web: // LetReg(5,"1"); //URL mode: '0' for email default, or '1' for web default. LetReg(6,"0"); //Selection mode: '0' selects tagged text, '1' selects href contents. SetOK(1); //The search for valid e-mail address or URL IfSel(1,'=',> !'GetSel(0,0);> FindInString("%!0%\","%!3",8,9,1);> IfOK(!"> LetReg(1,`%!0`);LetReg(5,`0`);",> !"SetOK(1);FindInString(""%!0%\"",""%!4"",8,9,1);> IfOK(!""LetReg(1,`%!0`);LetReg(4,`%!5`);LetReg(5,`1`);"",""LetReg(1,``);"");"> )',> !'LetReg(0,"");LetReg(1,"")'); //Remove whitespace IfStr("%!1","","=",'',!'> ExtractByIndex(1,''%!1'',%!8,%!9-2);> FormatString("%!1",1,"trimboth");'); //Prefix http:// or mailto: if appropriate. IfStr("%!5","0","=","LetReg(2,`mailto:`);",!'SetOK(1);> FindInString("%!1","<{{http|ftp{{s}|{}}}:\/\/}|{{\/}|{\.\.\/}}",8,9,7);> IfOK("",!"LetReg(2,`http://`);LetReg(6,`1`);SetOK(1);> FindInString(''%!1%\'',''<+$Alpha+Numeric$>'',8,9,7);> IfOK(''LetReg(1,``);IfStr(`%!4`,`0`,`=`,`LetReg(2,""mailto:"")`,``);'','''');> ");> '); BeginGroup; InsText(''); Mark(2); InsText('%!0'); Mark(3); InsText(''); Goto(0); IfStr('%!1','','=','> CMD("Next Bullet");> ','> IfStr(`%!6`,`0`,`=`,`Goto(2);Sel(1);Goto(3);RefreshSel;`,> `Goto(0);Sel(1);Goto(1);RefreshSel;`);> '); EndGroup; EndGroup; End; EXPECTED BEHAVIOUR: This macro attempts to recognize whether a selection is an e-mail address or a web address (http, https, ftp or ftps). It then creates a link depending on the format of the selection. If an e-mail address is found, it is entered in the 'href' attribute of the link *and* between the tags, and the text between the tags is highlighted ready for editing. The same is true of a web address starting with 'http://', 'ftp://' etc. If a web format is found *without* a starting protocol, the 'http://' is added to the 'href' attribute, and the href text is highlighted ready for editing. If a valid format is not found, a bullet is put in the 'href' attribute and highlighted. If nothing is selected, then a bullet is placed both in the href, and between the tags, and the first is highlighted. INSTALLATION: Move this file into the ..\WinEdt\Macros\HTML\ directory, and then insert a new macro into the desired menu with the line [Exe("%B\Macros\HTML\smartLink.edt")] as the Macro command. Editing line 22 will set the default link type to either email (0) or web (1) - if a selection is not recognized as web or email format then the default is appended to the link. Alternatively comment out line 22 and use either [LetReg(5,"0");Exe("%B\Macros\HTML\smartLink.edt")] or [LetReg(5,"1");Exe("%B\Macros\HTML\smartLink.edt")] as the macro command. The latter will allow separate menu items (say "email link" and "web link") to use the same macro with different defaults.