Skip to content

Commit

Permalink
make the newline newcell internal only
Browse files Browse the repository at this point in the history
so paste on other edtors won't have \u2028
  • Loading branch information
idanpa committed Jan 16, 2025
1 parent 55749fa commit 28c2ce3
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions pypad/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def keyPressEvent(self, e):
cursor = self.textCursor()
if e.key() == Qt.Key_C and (e.modifiers() & Qt.ControlModifier):
if cursor.hasSelection():
return super().keyPressEvent(e) # see createMimeDataFromSelection
return super().keyPressEvent(e) # copy handled by createMimeDataFromSelection
else:
self.log.debug('interrupt kernel: ctrl+c')
self.kernel_manager.interrupt_kernel()
Expand Down Expand Up @@ -755,13 +755,13 @@ def createMimeDataFromSelection(self) -> QMimeData:
mrow, mrow_num, mcol, mcol_num = cursor.selectedTableCells()
if mcol_num > 1:
# doesn't make much sense to copy multiple columns
mime_data.setText(cursor.selection().toPlainText())
text = cursor.selection().toPlainText()
elif mrow_num > 1 and mcol == 0:
# copy code
text = ''
for cell_idx in range(mrow, mrow+mrow_num):
text += self.get_cell_code(cell_idx).replace('\n', '\u2028') + '\n'
mime_data.setText(text)
text = text
elif mrow_num > 1 and mcol == 1:
# copy output
text = ''
Expand All @@ -770,22 +770,28 @@ def createMimeDataFromSelection(self) -> QMimeData:
text += self.latex[cell_idx] + '\n'
else:
text += self.get_cell_out(cell_idx) + '\n'
mime_data.setText(text)
text = text
else:
# copy within single cell
cell = self.table.cellAt(cursor)
if cell.isValid():
cell_idx = cell.row()
if cell.column() == 1 and self.latex[cell_idx] != '':
mime_data.setText(self.latex[cell_idx])
text = self.latex[cell_idx]
else:
mime_data.setText(cursor.selection().toPlainText().replace('\n', '\u2028'))
text = cursor.selection().toPlainText().replace('\n', '\u2028')

mime_data.setData('pypad', text.encode())
mime_data.setText(text.replace('\u2028', '\n'))
return mime_data

def insertFromMimeData(self, source: QMimeData):
# \u2028=newline, \n=new cell
lines = source.text().split('\n')
def insertFromMimeData(self, mime_data: QMimeData):
if mime_data.hasFormat('pypad'):
# where \u2028=newline, \n=new cell
text = mime_data.data('pypad').data().decode()
else:
text = mime_data.text()
lines = text.split('\n')
if lines[-1] == '': # ignore last new line
lines.pop()
lines.reverse()
Expand Down

0 comments on commit 28c2ce3

Please sign in to comment.