function smart_duplicate_items()
(
   Undo_BeginBlock();
  
   count_sel_items = CountSelectedMediaItems(0);
   count_sel_items_true = count_sel_items - 1;
   first_item = GetSelectedMediaItem(0, 0);
   first_item_start = GetMediaItemInfo_Value(first_item, "D_POSITION");
 
   i = 0;
   item_end_max0 = 0;
   loop(count_sel_items,
     item = GetSelectedMediaItem(0, i);
     item_pos = GetMediaItemInfo_Value(item, "D_POSITION");
     item_len = GetMediaItemInfo_Value(item, "D_LENGTH");
     item_end = item_pos + item_len;
     item_end_max = max(item_end, item_end_max0);
     item_end_max0 = item_end_max;
     i += 1;
   );
   last_item = GetSelectedMediaItem(0, count_sel_items_true);
   last_item_end = item_end_max0;
   
   com_len = last_item_end - first_item_start;
   com_len_qn = TimeMap2_timeToQN(0, com_len)/4;
   com_len_round_qn = ceil (com_len_qn); 
  
   ApplyNudge(0, 0, 5, 16, com_len_round_qn, 0, 1);
   Undo_EndBlock("smart_duplicate_items", 0);
);
PreventUIRefresh(1);
smart_duplicate_items();
PreventUIRefresh(-1);
UpdateArrange();