Skip to content

Instantly share code, notes, and snippets.

@Metaxal
Last active February 1, 2021 13:38
Show Gist options
  • Save Metaxal/77f5f33d558b0739ee6eff27e1d4f62e to your computer and use it in GitHub Desktop.
Save Metaxal/77f5f33d558b0739ee6eff27e1d4f62e to your computer and use it in GitHub Desktop.
A quickscript to move the current line up or down
#lang racket/base
;; License: [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) or
;; [MIT license](http://opensource.org/licenses/MIT) at your option.
(require quickscript
racket/class)
(define-script move-line-up
#:label "move-line-up"
#:shortcut up
#:shortcut-prefix (ctl shift)
(λ (selection #:editor ed)
(define pos (send ed get-start-position))
(define par (send ed position-paragraph pos))
(unless (= 0 par)
(define abovbeg (send ed paragraph-start-position (- par 1) #f))
(define abovend (send ed paragraph-end-position (- par 1) #f))
(define currbeg (send ed paragraph-start-position par #f))
(define currend (send ed paragraph-end-position par #f))
(define abovtxt (send ed get-text abovbeg abovend))
(send ed begin-edit-sequence)
(send ed set-position currend)
(send ed insert abovtxt)
(send ed delete abovbeg abovend)
(send ed set-position (+ abovbeg (- pos currbeg)))
(send ed end-edit-sequence))
#f))
(define-script move-line-down
#:label "move-line-down"
#:shortcut down
#:shortcut-prefix (ctl shift)
(λ (selection #:editor ed)
(define pos (send ed get-start-position))
(define par (send ed position-paragraph pos))
(unless (= (send ed last-paragraph) par)
(define currbeg (send ed paragraph-start-position par #f))
(define currend (send ed paragraph-end-position par #f))
(define belobeg (send ed paragraph-start-position (+ par 1) #f))
(define beloend (send ed paragraph-end-position (+ par 1) #f))
(define currtxt (send ed get-text currbeg currend))
(send ed begin-edit-sequence)
(send ed set-position beloend)
(send ed insert currtxt)
(send ed delete currbeg currend)
(send ed set-position (+ (- beloend belobeg) pos))
(send ed end-edit-sequence))
#f))
(module url2script-info racket/base
(provide filename url)
(define filename "move-line.rkt")
(define url "https://gist.github.com/Metaxal/77f5f33d558b0739ee6eff27e1d4f62e"))
@Metaxal
Copy link
Author

Metaxal commented Sep 17, 2020

Demo:
movelineupordown
(courtesy of Steven Walters)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment