def save(self, filename=None): """ Save the file. No checking is done. """ # get filename if filename is None: filename = self._filename if not filename: raise ValueError("No filename specified, and no filename known.") # Test whether it was changed without us knowing. If so, dont save now. if self.testWhetherFileWasChanged(): return # Get text and remember where we are text = self.toPlainText() cursor = self.textCursor() linenr = cursor.blockNumber() + 1 index = cursor.positionInBlock() scroll = self.verticalScrollBar().value() # Convert line endings (optionally remove trailing whitespace if pyzo.config.settings.removeTrailingWhitespaceWhenSaving: lines = [line.rstrip() for line in text.split('\n')] if lines[-1]: lines.append('') # Ensure the file ends in an empty line text = self.lineEndings.join(lines) self.setPlainText(text) # Go back to where we were cursor = self.textCursor() cursor.movePosition(cursor.Start) # move to begin of the document cursor.movePosition(cursor.NextBlock, n=linenr - 1) # n blocks down index = min(index, cursor.block().length() - 1) cursor.movePosition(cursor.Right, n=index) # n chars right self.setTextCursor(cursor) self.verticalScrollBar().setValue(scroll) else: text = text.replace('\n', self.lineEndings) # Make bytes bb = text.encode(self.encoding) # Store f = open(filename, 'wb') try: f.write(bb) finally: f.close() # Update stats self._filename = normalizePath(filename) self._name = os.path.split(self._filename)[1] self.document().setModified(False) self._modifyTime = os.path.getmtime(self._filename) # update title (in case of a rename) self.setTitleInMainWindow()
def save(self, filename=None): """ Save the file. No checking is done. """ # get filename if filename is None: filename = self._filename if not filename: raise ValueError("No filename specified, and no filename known.") # Test whether it was changed without us knowing. If so, dont save now. if self.testWhetherFileWasChanged(): return # Get text and remember where we are text = self.toPlainText() cursor = self.textCursor() linenr = cursor.blockNumber() + 1 index = cursor.positionInBlock() scroll = self.verticalScrollBar().value() # Convert line endings (optionally remove trailing whitespace if pyzo.config.settings.removeTrailingWhitespaceWhenSaving: lines = [line.rstrip() for line in text.split('\n')] if lines[-1]: lines.append('') # Ensure the file ends in an empty line text = self.lineEndings.join(lines) self.setPlainText(text) # Go back to where we were cursor = self.textCursor() cursor.movePosition(cursor.Start) # move to begin of the document cursor.movePosition(cursor.NextBlock,n=linenr-1) # n blocks down index = min(index, cursor.block().length()-1) cursor.movePosition(cursor.Right,n=index) # n chars right self.setTextCursor(cursor) self.verticalScrollBar().setValue(scroll) else: text = text.replace('\n', self.lineEndings) # Make bytes bb = text.encode(self.encoding) # Store f = open(filename, 'wb') try: f.write(bb) finally: f.close() # Update stats self._filename = normalizePath( filename ) self._name = os.path.split(self._filename)[1] self.document().setModified(False) self._modifyTime = os.path.getmtime(self._filename) # update title (in case of a rename) self.setTitleInMainWindow()
def loadFile(self, filename, updateTabs=True): """ Load the specified file. On success returns the item of the file, also if it was already open.""" # Note that by giving the name of a tempfile, we can select that # temp file. # normalize path if filename[0] != '<': filename = normalizePath(filename) if not filename: return None # if the file is already open... for item in self._tabs.items(): if item.id == filename: # id gets _filename or _name for temp files break else: item = None if item: self._tabs.setCurrentItem(item) print("File already open: '{}'".format(filename)) return item # create editor try: editor = createEditor(self, filename) except Exception as err: # Notify in logger print("Error loading file: ", err) # Make sure the user knows m = QtGui.QMessageBox(self) m.setWindowTitle("Error loading file") m.setText(str(err)) m.setIcon(m.Warning) m.exec_() return None # create list item item = FileItem(editor) self._tabs.addItem(item, updateTabs) if updateTabs: self._tabs.setCurrentItem(item) # store the path self._lastpath = os.path.dirname(item.filename) return item
def loadFile(self, filename, updateTabs=True): """ Load the specified file. On success returns the item of the file, also if it was already open.""" # Note that by giving the name of a tempfile, we can select that # temp file. # normalize path if filename[0] != '<': filename = normalizePath(filename) if not filename: return None # if the file is already open... for item in self._tabs.items(): if item.id == filename: # id gets _filename or _name for temp files break else: item = None if item: self._tabs.setCurrentItem(item) print("File already open: '{}'".format(filename)) return item # create editor try: editor = createEditor(self, filename) except Exception as err: # Notify in logger print("Error loading file: ", err) # Make sure the user knows m = QtGui.QMessageBox(self) m.setWindowTitle("Error loading file") m.setText(str(err)) m.setIcon(m.Warning) m.exec_() return None # create list item item = FileItem(editor) self._tabs.addItem(item, updateTabs) if updateTabs: self._tabs.setCurrentItem(item) # store the path self._lastpath = os.path.dirname(item.filename) return item
def save(self, filename=None): """ Save the file. No checking is done. """ # get filename if filename is None: filename = self._filename if not filename: raise ValueError("No filename specified, and no filename known.") # Test whether it was changed without us knowing. If so, dont save now. if self.testWhetherFileWasChanged(): return # Remove whitespace in a single undo-able action if (self.removeTrailingWS or pyzo.config.settings.removeTrailingWhitespaceWhenSaving): # Original cursor to put state back at the end oricursor = self.textCursor() # Screen cursor to select document screenCursor = self.textCursor() screenCursor.movePosition(screenCursor.Start) screenCursor.movePosition(screenCursor.End, screenCursor.KeepAnchor) # Cursor for doing the editor editCursor = self.textCursor() # Go! editCursor.beginEditBlock() try: editCursor.setPosition(screenCursor.selectionStart()) editCursor.movePosition(editCursor.StartOfBlock) while (editCursor.position() < screenCursor.selectionEnd() or editCursor.position() <= screenCursor.selectionStart()): editCursor.movePosition(editCursor.StartOfBlock) editCursor.movePosition(editCursor.EndOfBlock, editCursor.KeepAnchor) text1 = editCursor.selectedText() text2 = text1.rstrip() if len(text1) != len(text2): editCursor.insertText(text2) if not editCursor.block().next().isValid(): break editCursor.movePosition(editCursor.NextBlock) finally: self.setTextCursor(oricursor) editCursor.endEditBlock() # Get text and convert line endings text = self.toPlainText() text = text.replace("\n", self.lineEndings) # Make bytes bb = text.encode(self.encoding) # Store f = open(filename, "wb") try: f.write(bb) finally: f.close() # Update stats self._filename = normalizePath(filename) self._name = os.path.split(self._filename)[1] self.document().setModified(False) self._modifyTime = os.path.getmtime(self._filename) # update title (in case of a rename) self.setTitleInMainWindow()