retext icon indicating copy to clipboard operation
retext copied to clipboard

Add a keyboard shortcut for duplicating a line

Open user20190106 opened this issue 6 years ago • 0 comments

First, thanks for a great programme! I've started to use ReText more and more instead of normal word processing software, as it is then so easy to convert the text to html if needed -- without the bloated CSS created by fully-fledged word processors.

Would it be possible to add a shortcut for duplicating a line? Some text editors use Ctrl+D, and it is very handy (much quicker than select-copy-paste) when you need to repeat almost the same text. For instance, would be handly when creating tables, which often have repetitive contents. Even more so for complicated tables written directly in html.

However, as Ctrl+D seems to be already defined for delete, one could probably use Alt+L (L for line). Or can one override Qt standards? I wonder how often people use Ctrl+D instead of Del key.

By the way, I noticed that the Qt standard shortcuts page (link above) lists Ctrl+U as a shortcut (also) for deleting the entire line [which I have been missing], but I just get (ReText 7.0.3) <u></u> inserted (as Ctrl+U is also shortcut for underline). Is there a special trick how I can make `Ctrl+U´ to delete the line instead, or is it not implemented?

Best regards, user20190106

== EDIT (2019-01-15) ==

Shamelessly borrowing from Issue #337 I managed to figure it out myself!!!

Here a couple of convenience functions for your perusal:

Duplicate a line (= a block) or an arbitrary text selection.

This I put to Alt-L (and also added to the context menu)

# Duplicate a line (=block) OR selected text
# If no text is currently selected, 
# the block in which the cursor is currently located gets duplicated
# If text is selected, the entire text will be duplicated.
# This allows to duplicate arbitrary amounts of text
def duplicate(self):
	cursor = self.textCursor()
	if cursor.hasSelection():
		text = cursor.selectedText()
		currEnd = cursor.selectionEnd()
		len = currEnd - cursor.selectionStart()
		cursor.setPosition(currEnd, QTextCursor.MoveAnchor)
		cursor.insertText(text)
		# The following is for convenience:
		# After insert, select (=highlight) the duplicated block or text
		# Then, pressing arrow left sets cursor to the start, arrow right to the end
		# (and pressing Del removes an accidental duplication)
		cursor.setPosition(cursor.position() - len, QTextCursor.KeepAnchor)
	else:
		# Select the current block
		cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
		cursor.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor)
		text = cursor.selectedText()
		cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
		# Paste the block
		cursor.insertText(text)
		# Move to the pasted block
		cursor.movePosition(QTextCursor.PreviousBlock, QTextCursor.MoveAnchor)
		# Here, one cannot select the duplicated block, 
		# because one would move to "duplicate selection mode" if repeated
	# Update cursor
	self.setTextCursor(cursor)

Delete a block of text

This I put to Alt-D (and also added to the context menu).

def deleteBlock(self):
	cursor = self.textCursor()
	# Select the current block
	cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
	cursor.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor)
	text = cursor.selectedText()
	cursor.removeSelectedText()
	# Update cursor
	self.setTextCursor(cursor)

I'm happy now, I got two functionalities that speed up my work. However, it is up to the project team to judge whether they merit incorporation in the official ReText.

user20190106 avatar Jan 13 '19 07:01 user20190106