LILYPONDPREFIX -> LILYPOND_DATADIR
[lilypad-macos.git] / URLHandlerClass.py
blob83a61c68cdfd55e19156e4421f2f9f03a7abeb6c
2 from PyObjCTools import NibClassBuilder, AppHelper
3 from Foundation import *
4 from AppKit import *
5 import re
6 import string
7 import urllib
9 NibClassBuilder.extractClasses("MainMenu")
11 class URLHandlerClass(NibClassBuilder.AutoBaseClass):
12 def performDefaultImplementation(self):
13 urlString = self.directParameter()
14 self.openURL(urlString)
15 return None
17 def openURL (self, urlString):
18 urlString = urllib.unquote (urlString)
19 m = re.match ("^textedit://([^:]*):?([0-9]*):?([0-9]*):?([0-9]*)$", urlString)
20 if m == None:
21 NSLog ("URL doesn't match")
22 return None
24 line = 1
25 char = 1
26 column = 1
27 try:
28 line = string.atoi (m.group (2))
29 char = string.atoi (m.group (3))
30 column = string.atoi (m.group (4))
31 except ValueError:
32 pass
34 path = m.group (1)
35 self.jumpFile (path, line, char)
37 def charCount (self, str, line, char):
38 line -= 1
39 char -= 1
41 lines = string.split (str, '\n')
42 lineChars = sum (map (lambda x: len (x) + 1, lines[:line]))
43 if line < len (lines):
44 lineChars += min (char, len (lines[line]))
45 return lineChars
47 def jumpFile (self, path, line, char):
48 NSLog ("Jumping to %s %d %d\n" % (path, line, char))
50 controller = NSDocumentController.sharedDocumentController ()
51 doc = controller.openDocumentWithContentsOfFile_display_ (path, True)
52 textView = doc.textView
53 range = NSRange()
54 str = doc.textView.string()
55 range.location = self.charCount (str, line, char)
56 range.location = min (range.location, len (str))
57 range.length = 1
59 textView.setSelectedRange_ (range)
60 textView.scrollRangeToVisible_ (range)
61 return None
63 if __name__ == "__main__":
64 AppHelper.runEventLoop()