Skip to content

Commit 40fefd0

Browse files
committed
player/command: refactor loadfile and loadlist to be more flexible
By allowing more flexible flags combinations we can replace current single playlist entry, instead of replacing whole thing. And on top of that adds convinience positional flags, while refactoring everything to be use enum flags, instead of hardcoded values.
1 parent 2422db7 commit 40fefd0

5 files changed

Lines changed: 213 additions & 116 deletions

File tree

DOCS/man/input.rst

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -549,32 +549,37 @@ Playlist Manipulation
549549
a ``loadfile`` command that replaces the current file with a new one returns
550550
before the current file is stopped, and the new file even begins loading.
551551

552-
Second argument:
552+
Second argument is combination of flags controlling how the file is added
553+
to the playlist:
553554

554555
<replace> (default)
555-
Clear the playlist, and play the new file immediately.
556-
<append>
557-
Append the file to the playlist.
558-
<insert-next>
559-
Insert the file into the playlist, directly after the current entry.
560-
<insert-at>
561-
Insert the file into the playlist, at the index given in the third
562-
argument.
556+
Replace the playlist entry at the given index with the new file. If no
557+
index is given (default ``-1``), the whole playlist is cleared and the
558+
file is added as the only entry.
559+
<insert>
560+
Insert the file into the playlist at the given index, or append it if
561+
no index is given.
562+
563563
<play>
564564
If nothing is currently playing, start playback. (Always starts with the
565565
added file, even if the playlist was not empty before running this
566566
command).
567567

568-
Multiple flags can be combined, e.g.: ``append+play``.
568+
Multiple flags can be combined, e.g.: ``insert+play``.
569569

570-
By default, ``append``, ``insert-next``, and ``insert-at`` will not
571-
immediately start playback even if the playlist was previously empty. Adding
572-
the ``play`` flag to them forces playback to start.
570+
By default, playback will not immediately start even if the playlist was
571+
previously empty. Adding the ``play`` flag forces playback to start.
573572

574573
The following values are considered deprecated and were the old way
575-
(before mpv 0.42) of forcing playback to start before the ``play`` flag was
576-
added.
574+
(before mpv 0.42) of providing flags.
577575

576+
<append>
577+
Append the file to the playlist. Equivalent to ``insert`` with no index.
578+
<insert-next>
579+
Insert the file into the playlist, directly after the current entry.
580+
<insert-at>
581+
Insert the file into the playlist, at the index given in the third
582+
argument.
578583
<append-play>
579584
Append the file, and if nothing is currently playing, start playback.
580585
(Always starts with the added file, even if the playlist was not empty
@@ -588,11 +593,25 @@ Playlist Manipulation
588593
is currently playing, start playback. (Always starts with the added
589594
file, even if the playlist was not empty before running this command.)
590595

591-
The third argument is an insertion index, used only by the ``insert-at``
592-
action. When used with those actions, the new item will be inserted at the
593-
index position in the playlist, or appended to the end if index is less than
594-
0 or greater than the size of the playlist. This argument will be ignored for
595-
all other actions. This argument was added in mpv 0.38.0.
596+
The third argument is a numeric insertion index (0-based). For ``insert``,
597+
the new item is placed at the given position, shifting later entries right,
598+
or appended if the index is out of range. For ``replace``, the entry at
599+
the given position is replaced. If out of range the new item is appended.
600+
The default value ``-1`` means no specific position.
601+
602+
.. admonition:: Examples
603+
604+
# insert at the start of the playlist
605+
loadfile url insert 0
606+
607+
# append to the end of the playlist
608+
loadfile url insert
609+
610+
# replace the currently playing entry
611+
loadfile url replace ${playlist-pos}
612+
613+
# replace the next entry
614+
loadfile url replace ${playlist-pos-1}
596615

597616
The fourth argument is a list of options and values which should be set
598617
while the file is playing. It is of the form ``opt1=value1,opt2=value2,..``.
@@ -615,29 +634,35 @@ Playlist Manipulation
615634
Second argument:
616635

617636
<replace> (default)
618-
Stop playback and replace the internal playlist with the new one.
619-
<append>
620-
Append the new playlist at the end of the current internal playlist.
621-
<insert-next>
622-
Insert the new playlist into the current internal playlist, directly
623-
after the current entry.
624-
<insert-at>
625-
Insert the new playlist at the index given in the third argument.
637+
Replace entries in the internal playlist with the loaded playlist. If no
638+
index is given (default ``-1``), the whole internal playlist is cleared
639+
and replaced. If an index is given, entries starting at that position are
640+
replaced with the new playlist, any excess loaded entries are appended.
641+
<insert>
642+
Insert the new playlist into the internal playlist at the given index,
643+
or append it if no index is given.
644+
626645
<play>
627646
If nothing is currently playing, start playback. (Always starts with the
628647
added playlist, even if the internal playlist was not empty before running
629648
this command).
630649

631-
Multiple flags can be combined, e.g.: ``append+play``.
650+
Multiple flags can be combined, e.g.: ``insert+play``.
632651

633-
By default, ``append``, ``insert-next``, and ``insert-at`` will not
634-
immediately start playback even if the playlist was previously empty. Adding
635-
the ``play`` flag to them forces playback to start.
652+
By default, playback will not immediately start even if the playlist was
653+
previously empty. Adding the ``play`` flag forces playback to start.
636654

637655
The following values are considered deprecated and were the old way
638-
(before mpv 0.42) of forcing playback to start before the ``play`` flag was
639-
added.
656+
(before mpv 0.42) of providing flags.
640657

658+
<append>
659+
Append the new playlist to the end of the internal playlist. Equivalent
660+
to ``insert`` with no index.
661+
<insert-next>
662+
Insert the new playlist into the current internal playlist, directly
663+
after the current entry.
664+
<insert-at>
665+
Insert the new playlist at the index given in the third argument.
641666
<append-play>
642667
Append the new playlist, and if nothing is currently playing, start
643668
playback. (Always starts with the new playlist, even if the internal
@@ -652,11 +677,11 @@ Playlist Manipulation
652677
new playlist, even if the internal playlist was not empty before running
653678
this command.)
654679

655-
The third argument is an insertion index, used only by the ``insert-at`` action.
656-
When used with those actions, the new playlist will be inserted at the index
657-
position in the internal playlist, or appended to the end if index is less
658-
than 0 or greater than the size of the internal playlist. This argument will be
659-
ignored for all other actions. This argument was added in mpv 0.38.0.
680+
The third argument is a numeric index (0-based). For ``insert``, the loaded
681+
playlist is inserted starting at the given position, or appended if out of
682+
range. For ``replace``, entries starting at that position are replaced; any
683+
excess loaded entries are appended. The default value ``-1`` means no
684+
specific position.
660685

661686
``playlist-clear``
662687
Clear the playlist, except the currently played file.

common/playlist.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,19 @@ void playlist_set_stream_flags(struct playlist *pl, int flags)
312312
}
313313

314314
int64_t playlist_transfer_entries_to(struct playlist *pl, int dst_index,
315-
struct playlist *source_pl)
315+
struct playlist *source_pl, bool replace)
316316
{
317317
mp_assert(pl != source_pl);
318318
struct playlist_entry *first = playlist_get_first(source_pl);
319319

320320
int count = source_pl->num_entries;
321+
322+
if (replace) {
323+
int replace_count = MPMIN(count, pl->num_entries - dst_index);
324+
for (int n = dst_index + replace_count - 1; n >= dst_index; n--)
325+
playlist_remove(pl, pl->entries[n]);
326+
}
327+
321328
MP_TARRAY_INSERT_N_AT(pl, pl->entries, pl->num_entries, dst_index, count);
322329

323330
for (int n = 0; n < count; n++) {
@@ -356,12 +363,12 @@ int64_t playlist_transfer_entries(struct playlist *pl, struct playlist *source_p
356363
mp_assert(add_at >= 0);
357364
mp_assert(add_at <= pl->num_entries);
358365

359-
return playlist_transfer_entries_to(pl, add_at, source_pl);
366+
return playlist_transfer_entries_to(pl, add_at, source_pl, false);
360367
}
361368

362369
int64_t playlist_append_entries(struct playlist *pl, struct playlist *source_pl)
363370
{
364-
return playlist_transfer_entries_to(pl, pl->num_entries, source_pl);
371+
return playlist_transfer_entries_to(pl, pl->num_entries, source_pl, false);
365372
}
366373

367374
// Return number of entries between list start and e.

common/playlist.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct playlist_entry *playlist_get_first_in_same_playlist(struct playlist_entry
112112
char *current_playlist_path);
113113
void playlist_set_stream_flags(struct playlist *pl, int flags);
114114
int64_t playlist_transfer_entries_to(struct playlist *pl, int dst_index,
115-
struct playlist *source_pl);
115+
struct playlist *source_pl, bool replace);
116116
int64_t playlist_transfer_entries(struct playlist *pl, struct playlist *source_pl);
117117
int64_t playlist_append_entries(struct playlist *pl, struct playlist *source_pl);
118118

0 commit comments

Comments
 (0)