FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-94521: Copy or cut current line while no selection by znsoooo · Pull Request #145134 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Next Next commit
gh-94521: Copy/cut current line without selecting it
  • Loading branch information
znsoooo committed Oct 7, 2026
commit d6b41029aba0bf18a3fc0af41d36458ef7c41859
4 changes: 4 additions & 0 deletions Doc/library/idle.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ Select All

Cut
Copy selection into the system-wide clipboard; then delete the selection.
If there is no selection, cut the current line instead.

Copy
Copy selection into the system-wide clipboard.
If there is no selection, copy the current line instead.

Paste
Insert contents of the system-wide clipboard into the current window.
Expand Down Expand Up @@ -371,9 +373,11 @@ Context menus have the standard clipboard functions also on the Edit menu.

Cut
Copy selection into the system-wide clipboard; then delete the selection.
If there is no selection, cut the current line instead.

Copy
Copy selection into the system-wide clipboard.
If there is no selection, copy the current line instead.

Paste
Insert contents of the system-wide clipboard into the current window.
Expand Down
16 changes: 10 additions & 6 deletions Lib/idlelib/editor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -609,16 +609,20 @@ def python_docs(self, event=None):

def cut(self,event):
if not self.text.tag_ranges('sel'):
self.text.tag_add('sel', 'insert linestart', 'insert+1l linestart')
self.text.mark_set('insert', 'insert linestart')
self.text.event_generate("<<Cut>>")
self.copy(event)
self.text.delete('insert', 'insert+1l')
else:
self.text.event_generate("<<Cut>>")
return "break"

def copy(self,event):
if not self.text.tag_ranges("sel"):
self.text.tag_add('sel', 'insert linestart', 'insert+1l linestart')
if not self.text.tag_ranges('sel'):
self.text.mark_set('insert', 'insert linestart')
self.text.event_generate("<<Copy>>")
line = self.text.get('insert', 'insert+1l')
self.text.clipboard_clear()
self.text.clipboard_append(line)
else:
self.text.event_generate("<<Copy>>")
return "break"

def paste(self,event):
Expand Down
46 changes: 46 additions & 0 deletions Lib/idlelib/idle_test/test_editor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,51 @@ def test_rclick(self):
pass


class CopyCutLineTest(unittest.TestCase):

@classmethod
def setUpClass(cls):
requires('gui')
cls.root = Tk()
cls.root.withdraw()
cls.window = Editor(root=cls.root)

@classmethod
def tearDownClass(cls):
cls.root.destroy()
del cls.root

def test_copy_no_selection(self):
eq = self.assertEqual
text = self.window.text

insert(text, 'hello\nworld')
text.mark_set('insert', '1.2')
self.window.copy(event=None)

# Copy current line without being selected
eq(text.clipboard_get(), 'hello\n')
eq(text.tag_ranges('sel'), ())

# Cursor move to start of the line
eq(text.index('insert'), '1.0')

def test_cut_no_selection(self):
eq = self.assertEqual
text = self.window.text

insert(text, 'hello\nworld')
text.mark_set('insert', '1.2')
self.window.cut(event=None)

# Copy current line and remove it
eq(text.clipboard_get(), 'hello\n')
eq(text.get('1.0', 'end'), 'world\n')
eq(text.tag_ranges('sel'), ())

# Cursor move to start of the line
eq(text.index('insert'), '1.0')


if __name__ == '__main__':
unittest.main(verbosity=2)

Back | FazBrowse Home | New Git URL