// 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 // Last change: 24-Apr-2004 // 10-Jul-2005 Ch.Ziemski: Added check and error message when problem with browser executable // 14-Jul-2005 Ch.Ziemski: Modified above check (not so restrictive now) // and fixed the alternative command "start " // Added Statline_Message("Error") on errors // 15-Jul-2005 Ch.Ziemski: Several bigger modifications and bug(!) fixes... // 18-Jul-2005 Ch.Ziemski: check for bad registry entry enhanced // 22-May-2006 Ch.Ziemski: New "open in standard application" // 16-Oct-2008 Ch.Ziemski: Filename length limit 100 -> 254; internal: no longer use DOS option // Last change: 30-Dec-2008 Ch.Ziemski: Bugfix with no/relative path (Thanks to Pauli Lindgren) // // To do: Ch.Ziemski: Some cleaning up // // Requires: VEDIT 6.12 or later. // // From VEDIT: Right-click and select "Open file at cursor" or "Open URL at cursor". // or {File, Open (More), ...} // Holding down the Ctrl key while selecting "Open file at cursor" opens the file // in its standard application (e.g. Picture viewer) and not in VEDIT. // // 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, ...}) // #103==5 : "Open file at cursor" in its standard application // //////////////// if ((#103==1) && (Key_Shift_Status&4)) {#103=5} // if Ctrl-Key is pressed: open file in its std. application and not in VEDIT // // 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) //@105 = partial temp filename Reg_Set(105,"\ved", APPEND) //CZ (the old "/" instead of "\" doesn't work in Firefox...) Reg_Set(105,PID, APPEND) Reg_Set(105,".", APPEND) 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) // create temp file Reg_Set(105,"\ved", APPEND) //CZ (the old "/" instead of "\" doesn't work in Firefox...) Reg_Set(105,PID, APPEND) Reg_Set(105,".htm", APPEND) 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") Call("Open_URL") return } if (#103==4) { // "Open URL from T-Reg 121" in browser Call("Open_URL121") // and open it in browser return } // // If "Open file at cursor" (#103==1) // or "Open URL at cursor".(#103==2) // or "Open file at cursor in std app." (#103==5) // Save_Pos() if (BE > -1 && CP >= Min(BB,BE) && CP <= Max(BB,BE)) { // If within a highlighted block get it // but check that it's only a one liner Goto_Pos(BB) #104 = Cur_Line Goto_Pos(BE) #105 = Cur_Line if (#104 != #105) { Statline_Message("Error! Block on > 1 line!") Goto ERROR } Reg_Copy_Block(121,BB,BE) } else { // else get the current word/filename/URL #106 = Cur_Pos if (!At_BOL) { Search(`|{|W,",|<,<}`,REVERSE+ADVANCE+NOERR) } #104 = Cur_Pos Goto_Pos(#106) Search(`|{|W,",|>,>}`,NOERR) #105 = Cur_Pos Reg_Copy_Block(121,#104,#105) } if (Reg_Size(121)==0) { Statline_Message("Error! No filename/URL!") Goto ERROR } Restore_Pos() Statline_Message(" ") // empty possible previous error message // // If filename contains double-quotes, remove them. // Reg_Push(98,99) //Save and empty T-Reg 98 & 99 #105=Buf_Num Reg_Set(98,PATH_ONLY) //@98 = directory of current file Buf_Switch(Buf_Free(EXTRA)) Buf_Empty(OK) Reg_Ins(121) Replace('"','',BEGIN+ALL+NOERR) // // 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. // Replace("|254) { Statline_Message("Error! Word/filename/URL too long (>254)!") Goto ERROR } // // Many actions only make sense with "open file ..." and not with "open URL ...". // if ((#103==1)||(#103==5)) { //"Open file at cursor"... or "... in std. app." // // divide full path/filename into path (if any) and filename // Reg_Empty(103) EOF Search("|{\,/}", REVERSE+ADVANCE+NOERR+NORESTORE) if (!EM) { //If filename has a path... Reg_Copy_Block(103, 0, Cur_Pos) // save path in @103 } Reg_Copy(104,1) //@104 = filename // // Temporarily change to the directory of the current file so that "relative" // pathnames are more likely to work. E.g. "shop/maincart.htm". // // Reg_Set(98,PATH_ONLY) //@98 = directory of current file. 2008-12-30: Moved above (thanks to Pauli's tip) // 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) { Buf_Quit(OK) Buf_Switch(#105) 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) { Buf_Quit(OK) Buf_Switch(#105) Goto DONE2 } //If User [Cancel] } } // if (#103==1) { //Open the file in VEDIT Buf_Quit(OK) Buf_Switch(#105) File_Open("|@(121)",MRU) } else { //Open the file in its std. application Buf_Empty(OK) Reg_Ins(121) Reg_Copy_Block(106, Cur_Pos-4, Cur_Pos) Buf_Empty(OK) Ins_Text(".exe .bat .cmd .com .pif .scr .lnk .reg") // unsecure extensions - to be completed #106=1 //Flag for "yes" Search(@106, BEGIN+NOERR) if (! Error_Match) { // if file with unsecure extension should be opened #106=DI1(106, "`Security warning`, `You are trying to **execute** the file`, `.C|@(121)`, `Do you really want to do that?`, `[&Yes]`,`[&No]`",SET+APP+CENTER,0,0) } if (#106==1) { if (Is_WinNT) { //In WinNT/2000/XP... Reg_Set(106, "cmd /c ") // use cmd command to be able to avoid DOS option } else { //In Win95,98 Reg_Set(106, 'start "" ') // use start with double quotes added to fill the parameter "title", // (important since the filename is in double quotes when calling application below!) } Sys(`|@(106) "|@(121)"`,SUPPRESS+SIMPLE+NOWAIT) //Open file in application } Buf_Quit(OK) Buf_Switch(#105) } } else { Buf_Quit(OK) Buf_Switch(#105) Call("Open_URL121") // 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. // If not able to find the browser executable specified in the Registry: // try it with the "start/cmd" command... :SETUP-HTTP: Registry_Get_Item(106,"HKEY_CLASSES_ROOT\http\shell\open\command\") //CZ for testing only: //Reg_Set(106, '"c:\Programme\Mozilla Firefox\firefox.exe" -url "%1"') // If there is a browser executable in this registry key: // Extract filename of executable to be able to check its existence below if (Reg_Size(106)>4) { #106=Buf_Num Buf_Switch(Buf_Free(EXTRA)) Buf_Empty(OK) Reg_Ins(106, BEGIN) if (Cur_Char==34) { // if beginning with double quotes Char(1) Search('"', ADVANCE+NOERR) // search closing quotes (there should be no error!) } else { Search("|{ ,|>}") // else search first space or EOL } Del_Block(Cur_Pos, EoL_Pos) // delete all after filename BOF Reg_Copy(104,1) // @104 contains the filename of the browser executable now Buf_Quit(OK) Buf_Switch(#106) } // use fallback instead if needed ... if ( (Reg_Size(106)<5) || (! File_Exist(@104, NOERR)) ) { if (Is_WinNT) { //In WinNT/2000/XP... Reg_Set(106, "cmd /c ") // use cmd command to be able to avoid DOS option #106 = SUPPRESS+SIMPLE } else { //In Win95,98 Reg_Set(106,'start "" ') // use start with double quotes added to fill the parameter "title", // (important since the filename is in double quotes when calling browser below!) #106 = 0 } return } #106 = 0 //No Sys() options needed return // // OPEN_URL121 - Prepare and then open the file/URL named in T-Reg 121 in the standard web browser // :Open_URL121: 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) //CZ: no longer needed Reg_Set(121,"file://") //CZ Reg_Set(121,PATHNAME, APPEND) //@121 = full pathname to file Reg_Set(121,PATHNAME) //@121 = full pathname to file if (#104==-1) { Buf_Quit(OK) } //If just opened it, close it } Reg_Set(105, @121) // // Open_URL - Open the file/URL named in T-Reg 105 in the standard web browser, replacing "%1" when existing // :Open_URL: #104=Buf_Num Buf_Switch(Buf_Free(EXTRA)) Buf_Empty(OK) Reg_Ins(106) Search('"%1"', BEGIN+ADVANCE+NOERR) if (! Error_Match) { Del_Char(-3) } else { Search('%1', BEGIN+NOERR) if (! Error_Match) { Del_Char(2) Ins_Text('"') } else { EoL Ins_Text(' "') } } Reg_Ins(105) Ins_Text('"') BOF Reg_Copy(106,1) Buf_Quit(OK) Buf_Switch(#104) Sys(`|@(106)`,#106+NOWAIT) //Open file in browser return