Scite Compare Files

lua-users home
wiki

This script allows you to compare the selected files with an external tool such as Meld. You need to select 2 files using the menu command Tools > Add File to Comparison. After selecting the second file, an external comparison program opens automatically.

Put the following code into file compare_files.lua:

-- -*- coding: utf-8 -*-

local f1, f2
local diffTool
local cmdNum = 48       -- menu commands occupy the last 2 slots

if props["PLAT_WIN"] == "1" then
    diffTool = 'start /b C:\\"Program Files (x86)"\\Meld\\Meld.exe -n %q %q'
else
    diffTool = "meld -n %q %q &"
end

local function setMenuCommands()
    local which

    which = "."..cmdNum..".*"
    props["command.name"..which] = f1 and "Add File to Comparison (1)"
                                      or "Add File to Comparison (0)"
    props["command.subsystem"..which] = 3
    props["command"..which] = "addFileToComparison"
    props["command.mode"..which] = "savebefore:no"
    props["command.shortcut"..which] = ""

    which = "."..(cmdNum+1)..".*"
    props["command.name"..which] = f1 and "Reset File Comparison" or ""
    props["command.subsystem"..which] = 3
    props["command"..which] = "resetFileComparison"
    props["command.mode"..which] = "savebefore:no"
    props["command.shortcut"..which] = ""
end

function addFileToComparison()
    if props["FileNameExt"] ~= "" then
        if f1 then
            f2 = props["FilePath"]
            if f2 ~= f1 then
                local exec = diffTool:format(f1, f2)
                resetFileComparison()
                os.execute(exec)
            else
                f2 = nil
                print("Error: Same file selected")
            end
        else
            f1 = props["FilePath"]
            setMenuCommands()
        end
    else
        print("Error: File not saved")
    end
end

function resetFileComparison()
    f1 = nil
    f2 = nil
    setMenuCommands()
end

setMenuCommands()

After that, include compare_files.lua in your Lua startup script using the dofile command.

--Andrey Moskalev--


RecentChanges · preferences
edit · history
Last edited February 11, 2021 9:38 pm GMT (diff)