Build a function to swap two adjacent phrases. How?

I have been trying to build a simple function to be able to permute the content of two adjoining phrases.

I have built this function to see if the miracle occurs. But it does not work:

 local function phm_swap_phrase_down()
   local idx=song.selected_phrase_index
   local ssi=song.selected_instrument
   if (idx<#ssi.phrases) then
     local current_phrase=ssi:phrase(idx)
     local next_phrase=ssi:phrase(idx+1)
     next_phrase,current_phrase=current_phrase,next_phrase  --NOT WORK!
   end
 end

This kind of permutation only works with simple information (variables), such as swapping a string or numbers (A=B and B=A).

– Copy contents from another phrase.
renoise.song().instruments.phrases:copy_from(other renoise.InstrumentPhrase object)

This serves to copy the content of a phrase into another phrase.
If there was a simple way to store everything in a temporary clipboard, maybe it could be solved.

Any ideas?

I wrote a tool that swaps around lines in a pattern.

It uses a temp pattern added to the end of the song as a work space, then deletes that pattern when done.

I am quite sure there are bugs in there; the tool works but occasionally does something wrong : ( (I suspect an off-by-one error I cannot find.)

But the code for creating and deleting the temporary pattern could be useful to you.

I think the only way to make it “correct” is to create a table and put all the properties there, in addition to the whole pattern of the phrase. But that is too much “loose” data.

Another way to do this is to use the last phrase (with the last index<126 and empty) of clipboard, but it is a trick not highly recommended.
It would be to add the last phrase, copy the current phrase to be exchanged there, copy the next phrase in the current phrase and then the last phrase in the next phrase and finally delete the last phrase. This would only work with phrases with index <126. It is not an ideal solution.

Ideally to put all the data from a phrase in a “container”, and that the container can be = to another phrase.