patch 8.0.0499: taglist() does not prioritize tags for a buffer

Problem:    taglist() does not prioritize tags for a buffer.
Solution:   Add an optional buffer argument. (Duncan McDougall, closes #1194)
diff --git a/src/Makefile b/src/Makefile
index 3d13b85..1309a63 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -2213,6 +2213,7 @@
 	test_tabpage \
 	test_tagcase \
 	test_tagjump \
+	test_taglist \
 	test_tcl \
 	test_textobjects \
 	test_timers \
diff --git a/src/evalfunc.c b/src/evalfunc.c
index 5dc7f82..8869810 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -824,7 +824,7 @@
     {"tabpagenr",	0, 1, f_tabpagenr},
     {"tabpagewinnr",	1, 2, f_tabpagewinnr},
     {"tagfiles",	0, 0, f_tagfiles},
-    {"taglist",		1, 1, f_taglist},
+    {"taglist",		1, 2, f_taglist},
 #ifdef FEAT_FLOAT
     {"tan",		1, 1, f_tan},
     {"tanh",		1, 1, f_tanh},
@@ -3589,7 +3589,8 @@
     fold_count = foldedCount(curwin, lnum, &foldinfo);
     if (fold_count > 0)
     {
-	text = get_foldtext(curwin, lnum, lnum + fold_count - 1, &foldinfo, buf);
+	text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
+							       &foldinfo, buf);
 	if (text == buf)
 	    text = vim_strsave(text);
 	rettv->vval.v_string = text;
@@ -12267,6 +12268,7 @@
     static void
 f_taglist(typval_T *argvars, typval_T *rettv)
 {
+    char_u  *fname = NULL;
     char_u  *tag_pattern;
 
     tag_pattern = get_tv_string(&argvars[0]);
@@ -12275,8 +12277,10 @@
     if (*tag_pattern == NUL)
 	return;
 
+    if (argvars[1].v_type != VAR_UNKNOWN)
+	fname = get_tv_string(&argvars[1]);
     if (rettv_list_alloc(rettv) == OK)
-	(void)get_tags(rettv->vval.v_list, tag_pattern);
+	(void)get_tags(rettv->vval.v_list, tag_pattern, fname);
 }
 
 /*
diff --git a/src/proto/tag.pro b/src/proto/tag.pro
index 55f579d..497a76e 100644
--- a/src/proto/tag.pro
+++ b/src/proto/tag.pro
@@ -8,5 +8,5 @@
 void tagname_free(tagname_T *tnp);
 void simplify_filename(char_u *filename);
 int expand_tags(int tagnames, char_u *pat, int *num_file, char_u ***file);
-int get_tags(list_T *list, char_u *pat);
+int get_tags(list_T *list, char_u *pat, char_u *buf_fname);
 /* vim: set ft=c : */
diff --git a/src/tag.c b/src/tag.c
index 68d09c5..7710135 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -3876,11 +3876,11 @@
 }
 
 /*
- * Add the tags matching the specified pattern to the list "list"
- * as a dictionary
+ * Add the tags matching the specified pattern "pat" to the list "list"
+ * as a dictionary. Use "buf_fname" for priority, unless NULL.
  */
     int
-get_tags(list_T *list, char_u *pat)
+get_tags(list_T *list, char_u *pat, char_u *buf_fname)
 {
     int		num_matches, i, ret;
     char_u	**matches, *p;
@@ -3890,7 +3890,7 @@
     long	is_static;
 
     ret = find_tags(pat, &num_matches, &matches,
-				    TAG_REGEXP | TAG_NOIC, (int)MAXCOL, NULL);
+				TAG_REGEXP | TAG_NOIC, (int)MAXCOL, buf_fname);
     if (ret == OK && num_matches > 0)
     {
 	for (i = 0; i < num_matches; ++i)
diff --git a/src/testdir/test_alot.vim b/src/testdir/test_alot.vim
index 314a5b7..7b31575 100644
--- a/src/testdir/test_alot.vim
+++ b/src/testdir/test_alot.vim
@@ -47,6 +47,7 @@
 source test_tabpage.vim
 source test_tagcase.vim
 source test_tagjump.vim
+source test_taglist.vim
 source test_timers.vim
 source test_true_false.vim
 source test_unlet.vim
diff --git a/src/testdir/test_taglist.vim b/src/testdir/test_taglist.vim
new file mode 100644
index 0000000..b89b25e
--- /dev/null
+++ b/src/testdir/test_taglist.vim
@@ -0,0 +1,21 @@
+" test 'taglist' function
+
+func Test_taglist()
+  call writefile([
+	\ "FFoo\tXfoo\t1",
+	\ "FBar\tXfoo\t2",
+	\ "BFoo\tXbar\t1",
+	\ "BBar\tXbar\t2"
+	\ ], 'Xtags')
+  set tags=Xtags
+  split Xtext
+
+  call assert_equal(['FFoo', 'BFoo'], map(taglist("Foo"), {i, v -> v.name}))
+  call assert_equal(['FFoo', 'BFoo'], map(taglist("Foo", "Xtext"), {i, v -> v.name}))
+  call assert_equal(['FFoo', 'BFoo'], map(taglist("Foo", "Xfoo"), {i, v -> v.name}))
+  call assert_equal(['BFoo', 'FFoo'], map(taglist("Foo", "Xbar"), {i, v -> v.name}))
+
+  call delete('Xtags')
+  bwipe
+endfunc
+
diff --git a/src/version.c b/src/version.c
index ba8ff84..3b651cc 100644
--- a/src/version.c
+++ b/src/version.c
@@ -765,6 +765,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    499,
+/**/
     498,
 /**/
     497,