src/Tools/jEdit/src/jedit/isabelle_hyperlinks.scala
author wenzelm
Tue, 15 Dec 2009 20:20:07 +0100
changeset 34788 3779c54a2d21
parent 34784 02959dcea756
child 34791 b97d5b38dea4
permissions -rw-r--r--
direct apply for Document_Model and Document_View;

/*
 * Hyperlink setup for Isabelle proof documents
 *
 * @author Fabian Immler, TU Munich
 */

package isabelle.jedit


import java.io.File

import gatchan.jedit.hyperlinks.Hyperlink
import gatchan.jedit.hyperlinks.HyperlinkSource
import gatchan.jedit.hyperlinks.AbstractHyperlink

import org.gjt.sp.jedit.View
import org.gjt.sp.jedit.jEdit
import org.gjt.sp.jedit.Buffer
import org.gjt.sp.jedit.TextUtilities

import isabelle.proofdocument.Command


private class Internal_Hyperlink(start: Int, end: Int, line: Int, ref_offset: Int)
  extends AbstractHyperlink(start, end, line, "")
{
  override def click(view: View) {
    view.getTextArea.moveCaretPosition(ref_offset)
  }
}

class External_Hyperlink(start: Int, end: Int, line: Int, ref_file: String, ref_line: Int)
  extends AbstractHyperlink(start, end, line, "")
{
  override def click(view: View) = {
    Isabelle.system.source_file(ref_file) match {
      case None => System.err.println("Could not find source file " + ref_file)  // FIXME ??
      case Some(file) =>
        jEdit.openFiles(view, file.getParent, Array(file.getName, "+line:" + ref_line))
    }
  }
}

class Isabelle_Hyperlinks extends HyperlinkSource
{
	def getHyperlink(buffer: Buffer, original_offset: Int): Hyperlink =
	{
    Document_Model(buffer) match {
      case Some(model) =>
        val document = model.current_document()
        val offset = model.from_current(document, original_offset)
        document.command_at(offset) match {
          case Some(command) =>
            command.ref_at(document, offset - command.start(document)) match {
              case Some(ref) =>
                val command_start = command.start(document)
                val begin = model.to_current(document, command_start + ref.start)
                val line = buffer.getLineOfOffset(begin)
                val end = model.to_current(document, command_start + ref.stop)
                ref.info match {
                  case Command.RefInfo(Some(ref_file), Some(ref_line), _, _) =>
                    new External_Hyperlink(begin, end, line, ref_file, ref_line)
                  case Command.RefInfo(_, _, Some(id), Some(offset)) =>
                    Isabelle.session.command(id) match {
                      case Some(ref_cmd) =>
                        new Internal_Hyperlink(begin, end, line,
                          model.to_current(document, ref_cmd.start(document) + offset - 1))
                      case None => null
                    }
                  case _ => null
                }
              case None => null
            }
          case None => null
        }
      case None => null
    }
  }
}