Leverage -itersel and -draft to discard selections based on criteria

A simple and useful pattern to pass tests against the current selections.

Example of scenario for your script:

– Do some stuff
– Iterate each selection
  – Raise an error to discard the selection
  – Do passing stuff
– Do other passing stuff

And implementation to discard a selection hitting the top or the bottom of the buffer, so move-line does not apply on edges (selections hitting the top / bottom discard themselves):

provide-module move-line %{
  define-command move-line-below -docstring 'Move selected lines below' %{
    execute-keys '<a-x><a-_><a-:>'
    evaluate-commands -itersel %{
      execute-keys -draft 'w'
      execute-keys -draft 'Zj<a-x>dzP'
    }
  }
  define-command move-line-above -docstring 'Move selected lines above' %{
    execute-keys '<a-x><a-_><a-:>'
    evaluate-commands -itersel %{
      execute-keys -draft '<a-;>b'
      execute-keys -draft '<a-;>Zk<a-x>dzp'
    }
  }
}

require-module move-line
1 Like