Keybinds to move the edit position up/down by LPB

I have little experience with lua, and the documentation requires knowledge I don’t have atm… I got as far as adding keybindings. I had trouble getting the current edit position and adding/subtracting LPB from that, then setting the new edit position… basically everything that’s actually needed lol :sweat_smile:

Anyhow I’d like a tool that adds two keybindings that move the edit position up and down by lines per beat (to replace the default, rather inflexible “jump 16 lines up/down” keybindings). Any assistance with either learning resources or the actual code/tool would be appreciated.

I just wrote a similar thing yesterday as part of a tool: it jumps between beats in a song instead of moving by LPB though, but the latter is even simpler. All you have to do is add the LPB value to the selected_line_index and clamp/wrap around the edges of the pattern. My tool isn’t ready for release yet but here is a function you could bind to keys:

function move_by_beats(offset)
  local song = renoise.song()
  local length = song.selected_pattern.number_of_lines
  local lpb = song.transport.lpb

  -- get the current line index 
  local line = song.selected_line_index
  
  -- offset it by the current LPB
  line = line + lpb * offset
  
  -- wrap around the end of the pattern when moving down
  if(line > length) then
    line = line - length

  -- wrap around the beginning when going backward
  elseif(line < 1) then
    line = length + line
  end

  -- finally, set the selected line to what we just calculated
  song.selected_line_index = line
end


-- usage

-- call with 1 to move down
move_by_beats(1)

-- or with -1 to move up
move_by_beats(-1)

Note: this code could fail if you had a shorter pattern than your LPB or if you called the function with an offset larger than 1. But it shows how it can be done and what values you need from the API.

2 Likes

Thank you very much, I will take a look at this!

1 Like

Works perfectly, tysm :smiley:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.