2013-04-02 38 views
2

將文件(foo.txt)插入到插入位置的打開文件(bar.txt)中,最好的方法是什麼?將文件(foo.txt)插入到打開文件(bar.txt)的插入位置

如果有一個打開文件對話框來選擇要插入的任何東西,那就太好了。

這裏的文字處理等效爲「插入文件」。

這裏是foo.sublime-snippet的替代品,它可以鏈接到其他地方形成文件:

import sublime, sublime_plugin  

class InsertFileCommand(sublime_plugin.TextCommand): 
    def run(self, edit):  

     v = self.view  

     template = open('foo.txt').read()  

     print template  

     v.run_command("insert_snippet", {"contents": template}) 

回答

0

這是我對https://github.com/mneuhaus/SublimeFileTemplates的非官方修改,允許我使用快速麪板到insert-a-file-here。它適用於OSX操作系統(運行Mountain Lion)。

我目前看到的唯一缺點是無法正確翻譯格式文件中的雙斜槓\\ - 它被插入而不是單斜槓\。在我的LaTex格式文件中,雙斜線\\表示一條線結束,或者如果前面有一個~,則表示一條新線。解決方法是在實際的表單文件的每次出現時插入額外的斜槓(即,放入三個斜槓,理解在運行插件時只會插入兩個斜槓)。表單文件需要是LF結尾,我使用UTF-8編碼 - CR結尾沒有正確轉換。稍作修改,也可以有多個表單文件目錄和/或文件類型。

import sublime, sublime_plugin 
import os  

class InsertFileCommand(sublime_plugin.WindowCommand):  

    def run(self): 
     self.find_templates() 
     self.window.show_quick_panel(self.templates, self.template_selected) 

    def find_templates(self): 
     self.templates = [] 
     self.template_paths = []  

     for root, dirnames, filenames in os.walk('/path_to_forms_directory'): 
      for filename in filenames: 
       if filename.endswith(".tex"): # extension of form files 
        self.template_paths.append(os.path.join(root, filename)) 
        self.templates.append(os.path.basename(root) + ": " + os.path.splitext(filename)[0])  

    def template_selected(self, selected_index): 
     if selected_index != -1: 
      self.template_path = self.template_paths[selected_index]  

      print "\n" * 25 
      print "----------------------------------------------------------------------------------------\n" 
      print ("Inserting File: " + self.template_path + "\n") 
      print "----------------------------------------------------------------------------------------\n"  

      template = open(self.template_path).read()  

      print template  

      view = self.window.run_command("insert_snippet", {'contents': template})  

      sublime.status_message("Inserted File: %s" % self.template_path) 
1

從文本命令,您可以訪問當前視圖中。您可以使用self.view.sel()獲取光標位置。我不知道如何在python中做gui的東西,但是你可以使用快速麪板來做文件選擇(類似於FuzzyFileNav)。

+0

感謝您的提示 - 它看起來像這個插件將是「Hello,World!」的修改!在光標處插入。我會繼續努力的。 – lawlist

+0

我認爲自己處於正確的軌道上,並且用一個簡單的工作示例更新了我的問題(沒有打開文件對話框)。看起來最簡單的解決方案可能是修改https://github.com/mneuhaus/SublimeFileTemplates,以便將包含文件引用的模板插入到打開的文件選項卡中(不需要項目),而不是創建需要的新文件一個開放的項目。我安裝了FuzzyFileNav,並且仍在考慮如何將其納入。 HelloWorld - 從第一天的課程開始,可能不適合這個「在此處插入文件」的特定問題。 – lawlist

+0

聽起來不錯。很高興聽到你正在取得進展。請記住,使用「insert_snippet」命令將在所有遊標中插入內容。當然,對你來說可能並不重要,只是一個普遍的說明。 – skuroda