on master help sign in
Implement P as paste_before.
commit12961d657f9ca78f54156457ed27732e60992734 authorBlaž Hrastnik <blaz@mxxn.io> dateWed 7 Apr 2021, 17:03 (6 years ago) parent31e6bcb
2 files changed+53−29
helix-term/src/commands.rs+51−28 helix-term/src/keymap.rs+2−1
helix-term/src/commands.rs+51−28
@@ -1405,7 +1405,13 @@ pub fn yank(cx: &mut Context) {
14051405 cx.set_status(msg)
14061406}
14071407
1408−pub fn paste(cx: &mut Context) {
1408+#[derive(Copy, Clone)]
1409+enum Paste {
1410+ Before,
1411+ After,
1412+}
1413+
1414+fn _paste(doc: &mut Document, view: &View, action: Paste) -> Option<Transaction> {
14091415 // TODO: allow specifying reg
14101416 let reg = '"';
14111417 if let Some(values) = register::get(reg) {
@@ -1416,40 +1422,57 @@ pub fn paste(cx: &mut Context) {
14161422 .unwrap(),
14171423 );
14181424
1419− // TODO: if any of values ends \n it's linewise paste
1420− //
1421− // p => paste after
1422− // P => paste before
1423− // alt-p => paste every yanked selection after selected text
1424− // alt-P => paste every yanked selection before selected text
1425− // R => replace selected text with yanked text
1426− // alt-R => replace selected text with every yanked text
1427− //
1428− // append => insert at next line
1429− // insert => insert at start of line
1430− // replace => replace
1431− // default insert
1432−
1425+ // if any of values ends \n it's linewise paste
14331426 let linewise = values.iter().any(|value| value.ends_with('\n'));
14341427
14351428 let mut values = values.into_iter().map(Tendril::from).chain(repeat);
14361429
1437− let (view, doc) = cx.current();
1430+ // paste on the next line
1431+ // TODO: can simply take a range + modifier and compute the right pos without ifs
1432+ let text = doc.text();
14381433
1439− let transaction = if linewise {
1440− // paste on the next line
1441− // TODO: can simply take a range + modifier and compute the right pos without ifs
1442− let text = doc.text();
1443− Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
1444− let line_end = text.line_to_char(text.char_to_line(range.head) + 1);
1445− (line_end, line_end, Some(values.next().unwrap()))
1446− })
1447− } else {
1434+ let transaction =
14481435 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
1449− (range.head + 1, range.head + 1, Some(values.next().unwrap()))
1450− })
1451− };
1436+ let pos = match (action, linewise) {
1437+ // paste linewise before
1438+ (Paste::Before, true) => text.line_to_char(text.char_to_line(range.from())),
1439+ // paste linewise after
1440+ (Paste::After, true) => text.line_to_char(text.char_to_line(range.to()) + 1),
1441+ // paste insert
1442+ (Paste::Before, false) => range.from(),
1443+ // paste append
1444+ (Paste::After, false) => range.to() + 1,
1445+ };
1446+ (pos, pos, Some(values.next().unwrap()))
1447+ });
1448+ return Some(transaction);
1449+ }
1450+ None
1451+}
1452+
1453+// alt-p => paste every yanked selection after selected text
1454+// alt-P => paste every yanked selection before selected text
1455+// R => replace selected text with yanked text
1456+// alt-R => replace selected text with every yanked text
1457+//
1458+// append => insert at next line
1459+// insert => insert at start of line
1460+// replace => replace
1461+// default insert
1462+
1463+pub fn paste_after(cx: &mut Context) {
1464+ let (view, doc) = cx.current();
1465+
1466+ if let Some(transaction) = _paste(doc, view, Paste::After) {
1467+ doc.apply(&transaction, view.id);
1468+ doc.append_changes_to_history(view.id);
1469+ }
1470+}
1471+
1472+pub fn paste_before(cx: &mut Context) {
1473+ let (view, doc) = cx.current();
14521474
1475+ if let Some(transaction) = _paste(doc, view, Paste::Before) {
14531476 doc.apply(&transaction, view.id);
14541477 doc.append_changes_to_history(view.id);
14551478 }
helix-term/src/keymap.rs+2−1
@@ -207,8 +207,9 @@ pub fn default() -> Keymaps {
207207
208208 key!('y') => commands::yank,
209209 // yank_all
210− key!('p') => commands::paste,
210+ key!('p') => commands::paste_after,
211211 // paste_all
212+ shift!('P') => commands::paste_before,
212213
213214 key!('>') => commands::indent,
214215 key!('<') => commands::unindent,