OEM to ANSI string conversion inside macros

You have encountered a problem using Far macros? Here you can get help.
Post Reply
KlepetoX
Posts: 129
Joined: Sun 08 Jan, 2012 08:23
Location: Czech Republic
Has thanked: 2 times
Been thanked: 1 time

OEM to ANSI string conversion inside macros

Post by KlepetoX »

Hi, I have macro that runs another FAR instance with the same folders in panels (with ConEmu). But there is a problem with national characters when I create temporary startup file. Startup file is read as ANSI. I can write this file as OEM or UTF8 only. How do I convert OEM string to ANSI?

Here is the whole macro:

Code: Select all

Macro {
  key="CtrlAltO";
  area="Common";
  description="Clone Far";
  action=function()
    local FarName=win.GetEnv("FARHOME").."\\far.exe"
    local ConemuName=win.GetEnv("FARHOME").."\\conemu.exe"
    local StartFile=win.GetEnv("TEMP").."\\startfile.txt"
    local FarLeft,FarRight
--    if APanel.Left then FarLeft=APanel.Path..'\\.' FarRight=PPanel.Path..'\\.' end
--    if PPanel.Left then FarLeft=PPanel.Path..'\\.' FarRight=APanel.Path..'\\.' end
    if APanel.Left then FarLeft=APanel.Path FarRight=PPanel.Path end
    if PPanel.Left then FarLeft=PPanel.Path FarRight=APanel.Path end

    if     APanel.Left and APanel.Prefix=='arc' then FarLeft ='arc:'..APanel.HostFile end
    if not APanel.Left and APanel.Prefix=='arc' then FarRight='arc:'..APanel.HostFile end
    if     PPanel.Left and PPanel.Prefix=='arc' then FarLeft ='arc:'..PPanel.HostFile end
    if not PPanel.Left and PPanel.Prefix=='arc' then FarRight='arc:'..PPanel.HostFile end

    if FarLeft =='' then FarLeft ='c:' end
    if FarRight=='' then FarRight='c:' end

    local Text='"'..FarName..'" "'..FarLeft..'" "'..FarRight..'"'

-- OEM code page 852
    win.ShellExecute(nil,nil,'cmd.exe','/c echo '..Text..' > '..StartFile,nil,nil)

-- UTF8 65001
--    local File=io.open(StartFile,"w+b")
--    if File then
--       File:write(Text) File:close()
--    end

    win.ShellExecute(nil,nil,ConemuName,'/cmd @'..StartFile,nil,nil)  -- StartFile is read as ANSI
  end;
}
Thanks for help...
Shmuel
Posts: 6815
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 41 times
Been thanked: 526 times

OEM to ANSI string conversion inside macros

Post by Shmuel »

Code: Select all

function OEMtoANSI (str)
  return win.WideCharToMultiByte(win.MultiByteToWideChar(str, win.GetOEMCP()), win.GetACP())
end
KlepetoX
Posts: 129
Joined: Sun 08 Jan, 2012 08:23
Location: Czech Republic
Has thanked: 2 times
Been thanked: 1 time

OEM to ANSI string conversion inside macros

Post by KlepetoX »

Shmuel, it should work fine, but shouldn't. When I enter the expression

Code: Select all

win.WideCharToMultiByte(win.MultiByteToWideChar('ščř',852),1250)
it evaluates as

Code: Select all

+��+�
and should be

Code: Select all

çźý
Shmuel
Posts: 6815
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 41 times
Been thanked: 526 times

OEM to ANSI string conversion inside macros

Post by Shmuel »

The function should work just OK, it is tested.

When you write win.WideCharToMultiByte(win.MultiByteToWideChar('ščř',852),1250) it will work correctly only if your script itself is written in codepage 852 (which is probably a bad idea).
Shmuel
Posts: 6815
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 41 times
Been thanked: 526 times

OEM to ANSI string conversion inside macros

Post by Shmuel »

I'll try to make it more clear:
- Function OEMtoANSI expects an OEM-encoded string as its input. Then it outputs its ANSI-equivalent.
- When you specify 'ščř' in your UTF-8-encoded macrofile, the function receives a UTF-8-encoded string which is not a correct input.
KlepetoX
Posts: 129
Joined: Sun 08 Jan, 2012 08:23
Location: Czech Republic
Has thanked: 2 times
Been thanked: 1 time

OEM to ANSI string conversion inside macros

Post by KlepetoX »

OK, now I understand. Anyway, the problem is solved. I have changed ConEmu command line, now it reads command directly as parameter and not from the file. Macro is now very simple:

Code: Select all

Macro {
  key	="CtrlAltO";
  area="Common";
  description="Clone Far";
  action=function()
    local FarName=win.GetEnv("FARHOME").."\\far.exe"
    local ConemuName=win.GetEnv("FARHOME").."\\conemu.exe"
    local FarLeft,FarRight

    if APanel.Left then FarLeft=APanel.Path FarRight=PPanel.Path end
    if PPanel.Left then FarLeft=PPanel.Path FarRight=APanel.Path end

    if     APanel.Left and APanel.Prefix=='arc' then FarLeft ='arc:'..APanel.HostFile end
    if not APanel.Left and APanel.Prefix=='arc' then FarRight='arc:'..APanel.HostFile end
    if     PPanel.Left and PPanel.Prefix=='arc' then FarLeft ='arc:'..PPanel.HostFile end
    if not PPanel.Left and PPanel.Prefix=='arc' then FarRight='arc:'..PPanel.HostFile end

    if FarLeft =='' then FarLeft ='c:' end
    if FarRight=='' then FarRight='c:' end

    local Text='""'..FarName..'" "'..FarLeft..'" "'..FarRight..'""'
    win.ShellExecute(nil,nil,ConemuName,'-run '..Text,nil,nil)
  end;
}
Post Reply

Return to “Support and Troubleshooting”