// OPENCURS.VDM - Open the filename or the URL at the cursor, // or Preview current file in the Browser. // // Written by: Ted Green and Christian Ziemski // 10-Feb-2004 // Last change: 19-Apr-2004 C.Z.: added double quotes in Sys() calls to handle long filenames with spaces // changed some URL handling, esp. like "file://localhost/" // To do here: what about other special URLs? file://some_server/... // Last change: 24-Apr-2004 C.Z.: Fixed bug introduced 19-Apr // // Requires: VEDIT 6.12 or later. // // From VEDIT: Right-click and select "Open file here" or "Open URL here". // or {File, Open (More), ...} // // Input: #103==1 : "Open file at cursor" in VEDIT // #103==2 : "Open URL at cursor" in browser // #103==3 : {FILE, Open more, Preview current file in browser} // #103==4 : "Open URL from T-Reg 121" in browser (used from {Help, VEDIT Website, ...}) // //////////////// // // If {FILE, Open more, Preview current file in browser} // if (#103==3) { if (Config(F_F_TYPE)>3 || File_Size>1000000) { //Disallow binary/huge files Alert() Dialog_Input_1(105,"`Error - Preview file in browser`,`File is of wrong type or\ntoo large to open in browser.`",APP+CENTER,0,0) return } #104=2 // choose the behavior: // Version 1: always use temporary files // Version 2: if filename: File_Save() else create temporary file // Version 3: Always force a File_Save() resp. File_Save_As() if (#104 == 1) { //-- Version 1: always use temporary files Reg_Set(105,"|(VEDIT_TEMP)/ved|(PID).") //@105 = partial temp filename Reg_Set(104,PATHNAME) //@104 = current filename, if any if (Reg_Size(104) > 0) { //If buffer has filename Reg_Set(105,EXT_ONLY,APPEND) //preserve original extension } else { //Else, likely HTML-Code to be Reg_Set(105,"htm",APPEND) // viewed in a browser } Block_Save_As("|@(105)",0,File_Size,OK) //-------------------------------------------------------------------------- } else { if (#104 == 2) { //-- Version 2: if filename: File_Save() else create temporary file Reg_Set(105,PATHNAME) //@104 = current filename, if any if (Reg_Size(105) > 0) { //If buffer has filename File_Save(NOMSG) // save it } else { //if buffer has no filename Reg_Set(105,"|(VEDIT_TEMP)/ved|(PID).htm") // create temp file Block_Save_As("|@(105)",0,File_Size,OK) } //-------------------------------------------------------------------------- } else { if (#104 == 3) { //-- Version 3: Always force a File_Save() resp. File_Save_As() File_Save(NOMSG) Reg_Set(105,PATHNAME) if (Reg_Size(105) == 0) { //If buffer still has no filename return // do nothing (User has cancelled) } //-------------------------------------------------------------------------- }}} Call("SETUP-HTTP") Sys(`|@(106) "|@(105)"`,#106+NOWAIT) //Open file in browser return } if (#103==4) { // "Open URL from T-Reg 121" in browser Call("Open_URL") // and open it in browser return } // // If "Open file at cursor" or "Open URL at cursor". // Save_Pos() if (BE > -1 && Abs(BE-BB)<100 && CP >= Min(BB,BE) && CP <= Max(BB,BE)) { //If cursor within small block... Goto_Pos(BB) #104 = Cur_Line Goto_Pos(BE) #105 = Cur_Line if (#104 != #105) { Goto ERROR } //Error if block on more than 1 line Reg_Copy_Block(121,BB,BE) } else { #106 = Cur_Pos if (!At_BOL) { Search(`|{|W,",|<,<}`,REVERSE+ADVANCE+NOERR) } #104 = Cur_Pos Goto_Pos(#106) Search(`|{|W,",|>,>}`,NOERR) #105 = Cur_Pos if (Abs(#105-#104)>100) { Goto ERROR } //Error if current word too large Reg_Copy_Block(121,#104,#105) } if (Reg_Size(121)==0) { Goto ERROR } Restore_Pos() // // If filename contains double-quotes, remove them. // #106=Buf_Num Buf_Switch(Buf_Free(EXTRA)) Buf_Empty(OK) Reg_Ins(121) Replace('"','',BEGIN+ALL+NOERR) BOF Reg_Copy(121,1) //@121 = full pathname // CZ ++>> // since File_Exist(r, NOERR) in Open_URL breaks if there is an URL like "file://localhost/C:/...." // we have to workaround that for now: // (And it's a good idea here, independant of File_Exist(), IMHO) // Replace("|> Many actions only make sense with "open file ..." and not with "open URL ...". // So I put them within the following if() too. if (#103==1) { //"Open file at cursor"... // // divide full path/filename into path (if any) and filename // Reg_Empty(103) EOF Search("|{\,/}", REVERSE+ADVANCE+NOERR+NORESTORE) // CZ: search "/" too if (!EM) { //If filename has a path... Reg_Copy_Block(103, 0, Cur_Pos) // save path in @103 } Reg_Copy(104,1) //@104 = filename // Buf_Quit(OK) Buf_Switch(#106) // // Temporarily change to the directory of the current file so that "relative" // pathnames are more likely to work. E.g. "shop/maincart.htm". // Reg_Push(98,99) //Save and empty T-Reg 98 & 99 Reg_Set(98,PATH_ONLY) //@98 = directory of current file // if (Reg_Size(98) > 1) { //If current buffer has a file open... Reg_Set(99,CUR_DIR) // Save the current directory Chdir(@98) // Change to the path of the current file } if (File_Exist(@121,NOERR) < 1) { //If file does not exist... Message("File does not exist!", STATLINE) //Message on status line if (File_Exist("|@(103)\nul",NOERR) < 1) { //If path of file does not exist too if (Reg_Size(98) <= 1) { // If current buffer has no file open Reg_Set(98, CUR_DIR) // use current dir as path } Reg_Set(121, @98) // Add the path Reg_Set(121, "\", APPEND) Reg_Set(121, @104, APPEND) // and the filename for GF() } #106 = Get_Filename(121,@121,FORCE+NOERR) //Confirm it if (#106 == 0) { Goto DONE2 } //If User [Cancel] if (File_Exist(@121,NOERR) < 1) { //If file still does not exist... #106 = Dialog_Input_1(105,"`Warning - Preview file in browser`,`The file you have chosen does not exist.\n\nCreate a new one with that name?`,`[Yes]`,`[Cancel]`",APP+CENTER,0,0) if (#106 != 1) { Goto DONE2 } //If User [Cancel] } } File_Open("|@(121)",MRU) //Open the file } else { Buf_Quit(OK) Buf_Switch(#106) Call("Open_URL") // Shell to browser with URL in @121 } // :DONE2: if (Reg_Size(98) > 1) { // If current buffer has a file open... if (Reg_Size(99) > 0) { // If there is a path to restore... Chdir(@99) // Restore the original current directory } } Reg_Pop(98,99) // Restore T-Reg 98 & 99 return // :ERROR: Restore_Pos() Alert() return // // SETUP-HTTP - Set @106 with best shell command to run the current // web-browser. Set #106 with any needed Sys() options. // :SETUP-HTTP: Registry_Get_Item(106,"HKEY_CLASSES_ROOT\http\shell\open\command\") if (Reg_Size(106)<5) { Reg_Set(106,"start ") #106 = 0 //No options for Win95/98/ME if (Is_WinNT) { //In WinNT/2000/XP... #106 = DOS+SUPPRESS+SIMPLE //"start" only works from cmd box } return } // // Strip any command options specified in the registry. Needed for Opera. // // Not only old versions of Opera have options (New ones does not?!) // New Netscapes (e.g. 7.1) have '-url "%1"' // IExplorer has '-nohome' // ==> since "%1" has to be replaced with the real URL we can // do exactly that, or easier: remove the "%1" here at all: // And the URL is appended in the code above. // #106=Buf_Num Buf_Switch(Buf_Free(EXTRA)) Buf_Empty(OK) Reg_Ins(106) Replace("/|M|>","",BEGIN+NOERR) Replace('|["]%1|["]','',BEGIN+ALL+NOERR) BOF Reg_Copy(106,1) Buf_Quit(OK) Buf_Switch(#106) #106 = 0 //No Sys() options needed return // // OPEN_URL - Open the file/URL named in T-Reg 121 in the standard web browser // :Open_URL: Call("SETUP-HTTP") if (File_Exist(@121,NOERR) > 0) { //If file exists... #104=File_Check(@121) //Remember the status if already open File_Open("|@(121)") //Open it (temporarily) Reg_Set(121,"file://") Reg_Set(121,PATHNAME, APPEND) //@121 = full pathname to file if (#104==-1) { Buf_Quit(OK) } //If just opened it, close it } Sys(`|@(106) "|@(121)"`,#106+NOWAIT) //Open URL in browser return