Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://anggtwu.net/LUA/WithFakePrint1.lua.html
--   http://anggtwu.net/LUA/WithFakePrint1.lua
--          (find-angg "LUA/WithFakePrint1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/WithFakePrint1.lua"))
-- Supersedes:
--   (find-emlua "Repl1.lua" "WithFakePrint")
--   (find-angg "LUA/Repl1.lua" "WithFakePrint")
-- Used by (?):
--   (find-angg "LUA/Repl1.lua")

-- «.WithFakePrint»		(to "WithFakePrint")
-- «.WithFakePrint-tests»	(to "WithFakePrint-tests")

-- __        ___ _   _     _____     _        ____       _       _   
-- \ \      / (_) |_| |__ |  ___|_ _| | _____|  _ \ _ __(_)_ __ | |_ 
--  \ \ /\ / /| | __| '_ \| |_ / _` | |/ / _ \ |_) | '__| | '_ \| __|
--   \ V  V / | | |_| | | |  _| (_| |   <  __/  __/| |  | | | | | |_ 
--    \_/\_/  |_|\__|_| |_|_|  \__,_|_|\_\___|_|   |_|  |_|_| |_|\__|
--                                                                   
-- «WithFakePrint»  (to ".WithFakePrint")
-- This class is not used by EdrxRepl, only by EdrxEmacsRepl.
--
-- WithFakePrint.run(f) runs f() with a fake print() function that
-- saves the results to the global variable withprint_. "f" must be a
-- function that does not yield errors; if it aborts the original
-- print will not be restored, and we're fscked.
--
-- WithFakePrint.run(f) also substitutes temporarily write() by a fake
-- write() function. I always use "write" instead of io.write" in my
-- code, so this trick works.
--
withprint_ = {}

WithFakePrint = Class {
  type      = "WithFakePrint",
  fakeprint = function (...)
      local A = pack(...)
      local out = mapconcat(tostring, A, "\t", A.n).."\n"
      table.insert(withprint_, out)
    end,
  fakewrite = function (str)
      table.insert(withprint_, str)
    end,
  out = function () return table.concat(withprint_, "") end,
  --
  run1 = function (f)
      local old_print = print
      local old_write = write
      print = WithFakePrint.fakeprint
      write = WithFakePrint.fakewrite
      local results = pack(f())
      print = old_print
      write = old_write
      return myunpack(results)
    end,
  run = function (f)
      withprint_ = {}
      WithFakePrint.run1(f)
      return WithFakePrint.out()
    end,
  xpcall = function (f, errhandler)
      errhandler = errhandler or WithFakePrint.errhandler
      return WithFakePrint.run(function () xpcall(f, errhandler) end)
    end,
  errhandler = function (err)
      print(err)
      print(debug.traceback())
    end,
  --
  __index = {
  },
}



-- «WithFakePrint-tests»  (to ".WithFakePrint-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "WithFakePrint1.lua"
ff = function ()
    print(22, {}, nil)
    PP(22, {}, nil)
    return 33, 44
  end
ff()
= ff()
= "<<" .. WithFakePrint.run(                       ff       ) .. ">>"
= "<<" .. WithFakePrint.run(function ()            ff()  end) .. ">>"
= "<<" .. WithFakePrint.run(function () PP("ret:", ff()) end) .. ">>"
= "<<" .. WithFakePrint.xpcall(function ()
            PP("ret:", ff())
          end) ..
  ">>"
= "<<" .. WithFakePrint.xpcall(function ()
            PP("ret:", ff())
            error("foo")
          end) ..
  ">>"

--]]




-- Local Variables:
-- coding:  utf-8-unix
-- End: