Mac OS X TextMate 运行 OCaml代码提示出错

Mac OS X TextMate 运行 OCaml代码提示出错 Uncaught exception: Invalid_argument("Fl_package_base.query")  
解决办法,修改 ~/Library/Application\ Support/TextMate/Managed/Bundles/OCaml.tmbundle/Support/OCamlMate/run_script.rb 文件

@ocamlc = ARGV[0]
#修改为:
@ocamlc = File.basename(ARGV[0])

搞定。

顺便美化一下输出,对比测试一下效果:

Printf.printf "Hello world\nHello world";;

美化前:

210814_nPFk_170216.png

美化后:

210837_EgnR_170216.png

输出看上去清晰很多。

最终修改文件:

#!/usr/bin/env ruby18

require "#{ENV["TM_SUPPORT_PATH"]}/lib/escape"

require 'open3'
require 'cgi'
require 'fcntl'

def esc(str)
  CGI.escapeHTML(str).gsub(/\n/, '<br/>')
end

class UserScript
  def initialize
    @ocamlc = File.basename(ARGV[0])
    @srcfile = ARGV[1]
    @dstfile = ARGV[2]

    if ENV.has_key? 'TM_FILEPATH' then
      @path = ENV['TM_FILEPATH']
      @display_name = File.basename(@path)
    else
      @path = '-'
      @display_name = 'untitled'
    end
    
    @findlibpackages = (ENV['TM_OCAML_FINDLIB_PACKAGES'] || '').strip()
    @findlib = "ocamlfind"
  end


  # looks for the location of the threads library
  def find_threads()
    possibilities = [
        '/opt/local/godi/lib/ocaml/std-lib/threads',
        '/usr/local/godi/lib/ocaml/std-lib/threads',
        '/godi/lib/ocaml/std-lib/threads',
        '/opt/local/lib/ocaml/threads',
        '/usr/local/lib/ocaml/threads',
        '/usr/lib/ocaml/threads'
      ]
      
    possibilities.each() do |p|
      if File.exists?(p)
        return p
      end
    end
    
    return ""
  end


  def compile
    threadsincludedir = find_threads()
    
    
    # compile it
    if threadsincludedir != ""
      command = "#{e_sh @findlib} #{e_sh @ocamlc} -o #{e_sh @dstfile} -I #{e_sh(threadsincludedir)} -package #{e_sh @findlibpackages} -linkpkg str.cma unix.cma threads.cma #{e_sh @srcfile} 2>&1"
    else
      command = "#{e_sh @findlib} #{e_sh @ocamlc} -o #{e_sh @dstfile} -package #{e_sh @findlibpackages} -linkpkg str.cma unix.cma #{e_sh @srcfile} 2>&1"
    end
    puts "<div style=\"color: #999;border:1px solid #CCC;padding:3px;\">#{command}</div>"
    puts "<pre style=\"background:#FFF; margin:10px 0; padding: 10px; text-shadow:0 0 2px rgba(0,0,0,0.2);\">"
    output = `#{command}`
    
    onlywarnings = true
    if output != ""
      output.each_line() do |line|
        if line =~ /^File "(?:.*?)", (line ([0-9]+), characters [0-9]+-[0-9]+):/
          location, line = $1, $2
          print "<span>File \"#{@path}\", <a style=\"color: blue;\" href=\"txmt://open?url=file://#{e_url(@path)}&line=#{line}\">#{location}</a>:</span><br/>"
        else
          print esc(line)
          if line !~ /^Warning/
            onlywarnings = false
          end
        end
      end
      
      if !onlywarnings
        exit 1
      end
    end
  end


  def run
    # run it if the compile suceeded
    stdin, stdout, stderr = Open3.popen3(@dstfile)
    Thread.new { stdin.write @content; stdin.close } unless ENV.has_key? 'TM_FILEPATH'

    [stdout, stderr]
  end

  attr_reader :display_name, :path
  
end

error = ""
STDOUT.sync = true

script = UserScript.new
map = {
  'SCRIPT_NAME'       => script.display_name,
  'BUNDLE_SUPPORT'    => "file://#{ENV['TM_BUNDLE_SUPPORT'].gsub(/ /, '%20')}",
}
puts DATA.read.gsub(/\$\{([^}]+)\}/) { |m| map[$1] }

script.compile
stdout, stderr = script.run
descriptors = [ stdout, stderr ]

descriptors.each { |fd| fd.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) }
until descriptors.empty?
  select(descriptors).shift.each do |io|
    str = io.read
    if str.to_s.empty? then
      descriptors.delete io
      io.close
    elsif io == stdout then
      print esc(str)
    elsif io == stderr then
      print "<span style='color: red'>#{esc str}</span>"
    elsif io == stack_dump then
      error << str
    end
  end
end

puts '</pre></div></div>'
puts error
puts '<div id="exception_report" class="framed" style="color: #999;">Program exited.</div>'
puts '</body></html>'

__END__
<html>
  <head>
    <title>OCamlMate</title>
    <link rel="stylesheet" href="${BUNDLE_SUPPORT}/pastel.css" type="text/css">
  </head>
<body>
  <div id="script_output" class="framed" style="font-size:10px;">
  <strong>OCamlMate</strong>
  <strong>${SCRIPT_NAME}</strong>
  <div id="actual_output" style="word-wrap: break-word;margin-top:3px;">



TextMate的PATH变量里,需要有 ~/.opam/system/bin/ 路径

转载于:https://my.oschina.net/jsk/blog/477148

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值