Anonymous avatar Anonymous committed af60548

orgmode.py: Added OrgmodeLinkCompletions which navigates thru the local file system.

Comments (0)

Files changed (1)

         view.sel().clear()
         for region in backup:
             view.sel().add(region)
+
+
+class OrgmodeLinkCompletions(sublime_plugin.EventListener):
+
+    def on_query_completions(self, view, prefix, locations):
+        import os
+        from glob import glob
+        # print 'view =', view
+        # print 'preifx =', prefix
+        # print 'locations =', locations
+        location = locations[0]
+        if not 'orgmode.link' in view.scope_name(location):
+            return []
+        region = view.extract_scope(location)
+        content = view.substr(region)
+        inner_region = region
+        if content.startswith('[[') and content.endswith(']]'):
+            content = content[2:-2]
+            inner_region = sublime.Region(region.begin() + 2, region.end() - 2)
+        if not inner_region.contains(location):
+            return []
+        content = view.substr(sublime.Region(inner_region.begin(), location))
+        content = os.path.expandvars(content)
+        content = os.path.expanduser(content)
+        # print 'region =', region
+        # print 'content =', content
+        path, base = os.path.split(content)
+        # print 'split =', path, base
+        if not len(path):
+            path = os.path.dirname(view.file_name())
+        if not os.path.exists(path):
+            path = os.path.join(os.path.dirname(view.file_name()), path)
+        # print 'path =', path, base
+        pattern = os.path.join(path, '%s*' % base)
+        # print 'pattern =', pattern
+        files = glob(pattern)
+        basename = os.path.basename
+        isdir = os.path.isdir
+        for pos, item in enumerate(files[:]):
+            expr = basename(item)
+            snippet = basename(item)
+            if isdir(item):
+                expr += '/'
+                snippet += '/'
+            files[pos] = (expr, snippet)
+        # print 'files =', files
+        if not files:
+            return [(base + '/', base)]
+        return files
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.