View Single Post
Old 04-22-2013, 07:56 AM   #120
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,514
Default



Move edit cursor to next marker or project end (ignore time selection):
Code:
# Move edit cursor to next marker or project end (ignore time selection)

from reaper_python import *
from contextlib import contextmanager

@contextmanager
def undoable(message):
    RPR_Undo_BeginBlock2(0)
    try:
        yield
    finally:
        RPR_Undo_EndBlock2(0, message, -1)

with undoable("Move edit cursor to next marker/project end"):

    def getProjectEnd():
        RPR_PreventUIRefresh(1)
        RPR_CSurf_GoEnd()
        projectEnd = RPR_GetCursorPositionEx(0)
        RPR_SetEditCurPos2(0, cursorPos, 0, 0)
        RPR_PreventUIRefresh(-1)
        return projectEnd

    cursorPos = RPR_GetCursorPositionEx(0)

    i = 0
    while RPR_EnumProjectMarkers3(0, i, 0, 0, 0, "", 0, 0)[0] > 0:
        markerPos = RPR_EnumProjectMarkers3(0, i, 0, 0, 0, "", 0, 0)[4]

        # seek next marker after edit cursor
        if markerPos > cursorPos:

            # move cursor to next marker or project end
            projectEnd = getProjectEnd()
            if abs(cursorPos - projectEnd) < abs(cursorPos - markerPos) and projectEnd > cursorPos:
                RPR_CSurf_GoEnd()
            else:
                RPR_SetEditCurPos2(0, markerPos, 1, 0)
            break

        # if cursor is after the last marker -> move cursor to end of project...
        # ...if it is after the last marker
        elif markerPos <= cursorPos and RPR_EnumProjectMarkers3(0, i + 1, 0, 0, 0, "", 0, 0)[0] == 0:
            projectEnd = getProjectEnd()
            if projectEnd > cursorPos:
                RPR_CSurf_GoEnd()

        i += 1
Move edit cursor to previous marker or project start/end (ignore time selection):
Code:
# Move edit cursor to previous marker or project start/end (ignore time selection)

from reaper_python import *
from contextlib import contextmanager

@contextmanager
def undoable(message):
    RPR_Undo_BeginBlock2(0)
    try:
        yield
    finally:
        RPR_Undo_EndBlock2(0, message, -1)

with undoable("Move edit cursor to previous marker or project start/end"):

    def getProjectEnd():
        RPR_PreventUIRefresh(1)
        RPR_CSurf_GoEnd()
        projectEnd = RPR_GetCursorPositionEx(0)
        RPR_SetEditCurPos2(0, cursorPos, 0, 0)
        RPR_PreventUIRefresh(-1)
        return projectEnd

    cursorPos = RPR_GetCursorPositionEx(0)

    i = 0
    while RPR_EnumProjectMarkers3(0, i, 0, 0, 0, "", 0, 0)[0] > 0:
        markerPos = RPR_EnumProjectMarkers3(0, i, 0, 0, 0, "", 0, 0)[4]

        # seek next marker after edit cursor
        if markerPos >= cursorPos:
            projectEnd = getProjectEnd()

            # if cursor is between project start and 1. marker -> move to project start...
            # ...or project end if it is between 1. marker and start
            if i == 0 and markerPos >= cursorPos:
                RPR_PreventUIRefresh(1)
                RPR_CSurf_GoStart()
                projectStart = RPR_GetCursorPositionEx(0)
                RPR_SetEditCurPos2(0, cursorPos, 0, 0)
                RPR_PreventUIRefresh(-1)

                if abs(cursorPos - projectEnd) < abs(cursorPos - projectStart) and projectEnd < cursorPos:
                    RPR_CSurf_GoEnd()
                else:
                    RPR_CSurf_GoStart()
                break
            else:
                if abs(cursorPos - projectEnd) < abs(cursorPos - prevMarkerPos) and projectEnd < cursorPos:
                    RPR_CSurf_GoEnd()
                else:
                    RPR_SetEditCurPos2(0, prevMarkerPos, 1, 0)
            break

        # if cursor is after the last marker -> move cursor to end of project...
        # ...if it is after the last marker
        if markerPos < cursorPos and RPR_EnumProjectMarkers3(0, i + 1, 0, 0, 0, "", 0, 0)[0] == 0:
            projectEnd = getProjectEnd()
            if abs(cursorPos - projectEnd) < abs(cursorPos - markerPos) and projectEnd < cursorPos:
                RPR_CSurf_GoEnd()
            else:
                RPR_SetEditCurPos2(0, markerPos, 1, 0)

        i += 1
        prevMarkerPos = RPR_EnumProjectMarkers3(0, i - 1, 0, 0, 0, "", 0, 0)[4]
spk77 is offline   Reply With Quote