Example #1
0
 def open_file_in_commander(self, ext, path):
     '''Open the given path in a Leonine manner.'''
     #
     # 1. Open .leo files as in open-outline command...
     path = os.path.normpath(path)
     if g.app.loadManager.isLeoFile(path):
         c = g.openWithFileName(path, old_c=self.c)
         if not c:
             return
         c.k.makeAllBindings()
         g.chdir(path)
         g.setGlobalOpenDir(path)
         return
     #
     # 2. Search open commanders for a matching @<file> node.
     for c in g.app.commanders():
         for p in c.all_unique_positions():
             if (p.isAnyAtFileNode()
                     and path == os.path.normpath(g.fullPath(c, p))):
                 if getattr(c.frame.top, 'leo_master', None):
                     c.frame.top.leo_master.select(c)
                     c.selectPosition(p)
                 c.redraw()
                 return
     #
     # 3. Open a dummy file, removing sentinels from derived files.
     c = g.openWithFileName(path, old_c=self.c)
     c.k.makeAllBindings()
     g.chdir(path)
     g.setGlobalOpenDir(path)
     c.selectPosition(c.rootPosition())
     c.redraw()
    def open_completer(c, closeFlag, fileName):

        c.bringToFront()
        c.init_error_dialogs()
        ok = False
        if fileName:
            if g.app.loadManager.isLeoFile(fileName):
                c2 = g.openWithFileName(fileName, old_c=c)
                if c2:
                    c2.k.makeAllBindings()
                    # Fix #579: Key bindings don't take for commands defined in plugins.
                    g.chdir(fileName)
                    g.setGlobalOpenDir(fileName)
                if c2 and closeFlag:
                    g.app.destroyWindow(c.frame)
            elif c.looksLikeDerivedFile(fileName):
                # Create an @file node for files containing Leo sentinels.
                ok = c.importCommands.importDerivedFiles(parent=c.p,
                                                         paths=[fileName],
                                                         command='Open')
            else:
                # otherwise, create an @edit node.
                ok = c.createNodeFromExternalFile(fileName)
        c.raise_error_dialogs(kind='write')
        g.app.runAlreadyOpenDialog(c)
        # openWithFileName sets focus if ok.
        if not ok:
            c.initialFocusHelper()
def import_txt_file(c, fn):
    """Import the .txt file into a new node."""
    u = c.undoer
    g.setGlobalOpenDir(fn)
    undoData = u.beforeInsertNode(c.p)
    last = c.lastTopLevel()
    p = last.insertAfter()
    p.h = f"@edit {fn}"
    s, e = g.readFileIntoString(fn, kind='@edit')
    p.b = s
    u.afterInsertNode(p, 'Import', undoData)
    c.setChanged()
    c.redraw(p)
def exportHeadlines(self, event=None):
    """Export all headlines to an external file."""
    c = self
    filetypes = [("Text files", "*.txt"), ("All files", "*")]
    fileName = g.app.gui.runSaveFileDialog(c,
                                           title="Export Headlines",
                                           filetypes=filetypes,
                                           defaultextension=".txt")
    c.bringToFront()
    if fileName:
        g.setGlobalOpenDir(fileName)
        g.chdir(fileName)
        c.importCommands.exportHeadlines(fileName)
def weave(self, event=None):
    """Simulate a literate-programming weave operation by writing the outline to a text file."""
    c = self
    fileName = g.app.gui.runSaveFileDialog(c,
                                           title="Weave",
                                           filetypes=[("Text files", "*.txt"),
                                                      ("All files", "*")],
                                           defaultextension=".txt")
    c.bringToFront()
    if fileName:
        g.setGlobalOpenDir(fileName)
        g.chdir(fileName)
        c.importCommands.weave(fileName)
Example #6
0
def open(self, event=None):
    '''Open a Leo window containing the contents of a .leo file.'''
    c = self
    # Close the window if this command completes successfully?
    closeFlag = (
        c.frame.startupWindow and
        # The window was open on startup
        not c.changed and not c.frame.saved and
        # The window has never been changed
        g.app.numberOfUntitledWindows == 1
        # Only one untitled window has ever been opened
    )
    table = [
        # 2010/10/09: Fix an interface blunder. Show all files by default.
        ("All files", "*"),
        g.fileFilters("LEOFILES"),
        ("Python files", "*.py"),
    ]
    fileName = ''.join(c.k.givenArgs)
    if not fileName:
        fileName = g.app.gui.runOpenFileDialog(
            c,
            title="Open",
            filetypes=table,
            defaultextension=g.defaultLeoFileExtension(c))
    c.bringToFront()
    c.init_error_dialogs()
    ok = False
    if fileName:
        if g.app.loadManager.isLeoFile(fileName):
            c2 = g.openWithFileName(fileName, old_c=c)
            if c2:
                c2.k.makeAllBindings()
                # Fix #579: Key bindings don't take for commands defined in plugins.
                g.chdir(fileName)
                g.setGlobalOpenDir(fileName)
            if c2 and closeFlag:
                g.app.destroyWindow(c.frame)
        elif c.looksLikeDerivedFile(fileName):
            # Create an @file node for files containing Leo sentinels.
            ok = c.importCommands.importDerivedFiles(parent=c.p,
                                                     paths=[fileName],
                                                     command='Open')
        else:
            # otherwise, create an @edit node.
            ok = c.createNodeFromExternalFile(fileName)
    c.raise_error_dialogs(kind='write')
    g.app.runAlreadyOpenDialog(c)
    # openWithFileName sets focus if ok.
    if not ok:
        c.initialFocusHelper()
def weave(self, event=None):
    '''Simulate a literate-programming weave operation by writing the outline to a text file.'''
    c = self
    filetypes = [("Text files", "*.txt"), ("All files", "*")]
    fileName = g.app.gui.runSaveFileDialog(c,
        initialfile="weave.txt",
        title="Weave",
        filetypes=filetypes,
        defaultextension=".txt")
    c.bringToFront()
    if fileName:
        g.setGlobalOpenDir(fileName)
        g.chdir(fileName)
        c.importCommands.weave(fileName)
def exportHeadlines(self, event=None):
    '''Export all headlines to an external file.'''
    c = self
    filetypes = [("Text files", "*.txt"), ("All files", "*")]
    fileName = g.app.gui.runSaveFileDialog(c,
        initialfile="headlines.txt",
        title="Export Headlines",
        filetypes=filetypes,
        defaultextension=".txt")
    c.bringToFront()
    if fileName:
        g.setGlobalOpenDir(fileName)
        g.chdir(fileName)
        c.importCommands.exportHeadlines(fileName)
def open(self, event=None):
    '''Open a Leo window containing the contents of a .leo file.'''
    c = self
    # Close the window if this command completes successfully?
    closeFlag = (
        c.frame.startupWindow and
            # The window was open on startup
        not c.changed and not c.frame.saved and
            # The window has never been changed
        g.app.numberOfUntitledWindows == 1
            # Only one untitled window has ever been opened
    )
    table = [
        # 2010/10/09: Fix an interface blunder. Show all files by default.
        ("All files", "*"),
        g.fileFilters("LEOFILES"),
        ("Python files", "*.py"),]
    fileName = ''.join(c.k.givenArgs)
    if not fileName:
        fileName = g.app.gui.runOpenFileDialog(c,
            title="Open",
            filetypes=table,
            defaultextension=g.defaultLeoFileExtension(c))
    c.bringToFront()
    c.init_error_dialogs()
    ok = False
    if fileName:
        if g.app.loadManager.isLeoFile(fileName):
            c2 = g.openWithFileName(fileName, old_c=c)
            if c2:
                c2.k.makeAllBindings()
                    # Fix #579: Key bindings don't take for commands defined in plugins.
                g.chdir(fileName)
                g.setGlobalOpenDir(fileName)
            if c2 and closeFlag:
                g.app.destroyWindow(c.frame)
        elif c.looksLikeDerivedFile(fileName):
            # Create an @file node for files containing Leo sentinels.
            ok = c.importCommands.importDerivedFiles(parent=c.p,
                paths=[fileName], command='Open')
        else:
            # otherwise, create an @edit node.
            ok = c.createNodeFromExternalFile(fileName)
    c.raise_error_dialogs(kind='write')
    g.app.runAlreadyOpenDialog(c)
    # openWithFileName sets focus if ok.
    if not ok:
        c.initialFocusHelper()
def flattenOutline(self, event=None):
    """
    Export the selected outline to an external file.
    The outline is represented in MORE format.
    """
    c = self
    filetypes = [("Text files", "*.txt"), ("All files", "*")]
    fileName = g.app.gui.runSaveFileDialog(c,
                                           title="Flatten Selected Outline",
                                           filetypes=filetypes,
                                           defaultextension=".txt")
    c.bringToFront()
    if fileName:
        g.setGlobalOpenDir(fileName)
        g.chdir(fileName)
        c.importCommands.flattenOutline(fileName)
def outlineToCWEB(self, event=None):
    """
    Export the selected outline to an external file.
    The outline is represented in CWEB format.
    """
    c = self
    filetypes = [("CWEB files", "*.w"), ("Text files", "*.txt"),
                 ("All files", "*")]
    fileName = g.app.gui.runSaveFileDialog(c,
                                           title="Outline To CWEB",
                                           filetypes=filetypes,
                                           defaultextension=".w")
    c.bringToFront()
    if fileName:
        g.setGlobalOpenDir(fileName)
        g.chdir(fileName)
        c.importCommands.outlineToWeb(fileName, "cweb")
def flattenOutline(self, event=None):
    '''
    Export the selected outline to an external file.
    The outline is represented in MORE format.
    '''
    c = self
    filetypes = [("Text files", "*.txt"), ("All files", "*")]
    fileName = g.app.gui.runSaveFileDialog(c,
        initialfile="flat.txt",
        title="Flatten Selected Outline",
        filetypes=filetypes,
        defaultextension=".txt")
    c.bringToFront()
    if fileName:
        g.setGlobalOpenDir(fileName)
        g.chdir(fileName)
        c.importCommands.flattenOutline(fileName)
def open_theme_file_helper(event, closeFlag, fn):
    '''Open a theme file and apply the theme.'''
    trace = False and not g.unitTesting
    c = event and event.get('c')
    if not c: return
    old_dir = g.os_path_abspath(os.curdir)
    themes_dir = g.os_path_finalize_join(g.app.loadDir, '..', 'themes')
    if fn:
        fn = c.styleSheetManager.find_theme_file(fn)
        if not fn: return
    else:
        fn = g.app.gui.runOpenFileDialog(c,
            title="Open Theme",
            filetypes=[
                 g.fileFilters("LEOFILES"),
                ("All files", "*"),
            ],
            defaultextension=g.defaultLeoFileExtension(c),
            startpath=themes_dir,
        )
    c.bringToFront()
    c.init_error_dialogs()
    # Adapted from c.open().
    if fn and g.app.loadManager.isLeoFile(fn):
        # Close the file if it is already open, provided there is another.
        aList = g.app.commanders()
        if len(aList) > 1:
            for c2 in aList:
                if trace: g.trace('COMPARE\n%s\n%s' % (fn, c2.fileName()))
                if fn == c2.fileName():
                    if trace: g.trace('===== CLOSING', fn)
                    c2.close(new_c=c)
                    break
        c2 = g.openWithFileName(fn, old_c=c)
        if c2:
            c2.k.makeAllBindings()
            g.chdir(fn)
            g.setGlobalOpenDir(fn)
            if closeFlag:
                g.app.destroyWindow(c2.frame)
                g.app.windowList.remove(c2.frame)
    os.chdir(old_dir)
    c.raise_error_dialogs(kind='write')
    g.app.runAlreadyOpenDialog(c)
    c.initialFocusHelper()
Example #14
0
def outlineToNoweb(self, event=None):
    '''
    Export the selected outline to an external file.
    The outline is represented in noweb format.
    '''
    c = self
    filetypes = [("Noweb files", "*.nw"), ("Text files", "*.txt"),
                 ("All files", "*")]
    fileName = g.app.gui.runSaveFileDialog(
        c,
        initialfile=self.outlineToNowebDefaultFileName,
        title="Outline To Noweb",
        filetypes=filetypes,
        defaultextension=".nw")
    c.bringToFront()
    if fileName:
        g.setGlobalOpenDir(fileName)
        g.chdir(fileName)
        c.importCommands.outlineToWeb(fileName, "noweb")
        c.outlineToNowebDefaultFileName = fileName
def outlineToCWEB(self, event=None):
    '''
    Export the selected outline to an external file.
    The outline is represented in CWEB format.
    '''
    c = self
    filetypes = [
        ("CWEB files", "*.w"),
        ("Text files", "*.txt"),
        ("All files", "*")]
    fileName = g.app.gui.runSaveFileDialog(c,
        initialfile="cweb.w",
        title="Outline To CWEB",
        filetypes=filetypes,
        defaultextension=".w")
    c.bringToFront()
    if fileName:
        g.setGlobalOpenDir(fileName)
        g.chdir(fileName)
        c.importCommands.outlineToWeb(fileName, "cweb")