patch 9.0.1686: undotree() only works for the current buffer

Problem:    undotree() only works for the current buffer
Solution:   Add an optional "buffer number" parameter to undotree().  If
            omitted, use the current buffer for backwards compatibility.

closes: #4001
closes: #12292

Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: zeertzjq <zeertzjq@outlook.com>
Co-authored-by: Devin J. Pohly <djpohly@gmail.com>
diff --git a/src/undo.c b/src/undo.c
index 9e122f9..85c4670 100644
--- a/src/undo.c
+++ b/src/undo.c
@@ -3629,7 +3629,7 @@
  * Recursive.
  */
     static void
-u_eval_tree(u_header_T *first_uhp, list_T *list)
+u_eval_tree(buf_T *buf, u_header_T *first_uhp, list_T *list)
 {
     u_header_T  *uhp = first_uhp;
     dict_T	*dict;
@@ -3641,9 +3641,9 @@
 	    return;
 	dict_add_number(dict, "seq", uhp->uh_seq);
 	dict_add_number(dict, "time", (long)uhp->uh_time);
-	if (uhp == curbuf->b_u_newhead)
+	if (uhp == buf->b_u_newhead)
 	    dict_add_number(dict, "newhead", 1);
-	if (uhp == curbuf->b_u_curhead)
+	if (uhp == buf->b_u_curhead)
 	    dict_add_number(dict, "curhead", 1);
 	if (uhp->uh_save_nr > 0)
 	    dict_add_number(dict, "save", uhp->uh_save_nr);
@@ -3655,7 +3655,7 @@
 	    if (alt_list != NULL)
 	    {
 		// Recursive call to add alternate undo tree.
-		u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
+		u_eval_tree(buf, uhp->uh_alt_next.ptr, alt_list);
 		dict_add_list(dict, "alt", alt_list);
 	    }
 	}
@@ -3721,28 +3721,40 @@
  #endif
 
 /*
- * "undotree()" function
+ * "undotree(expr)" function
  */
     void
 f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
 {
+    typval_T	*tv = &argvars[0];
+    buf_T	*buf;
+    dict_T	*dict;
+    list_T	*list;
+
+    if (in_vim9script() && check_for_opt_buffer_arg(argvars, 0) == FAIL)
+	return;
+
+    if (tv->v_type == VAR_UNKNOWN)
+	buf = curbuf;
+    else
+	buf = tv_get_buf_from_arg(tv);
+
     if (rettv_dict_alloc(rettv) == FAIL)
 	return;
 
-    dict_T *dict = rettv->vval.v_dict;
-    list_T *list;
+    dict = rettv->vval.v_dict;
 
-    dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
-    dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
-    dict_add_number(dict, "save_last", curbuf->b_u_save_nr_last);
-    dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
-    dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
-    dict_add_number(dict, "save_cur", curbuf->b_u_save_nr_cur);
+    dict_add_number(dict, "synced", (long)buf->b_u_synced);
+    dict_add_number(dict, "seq_last", buf->b_u_seq_last);
+    dict_add_number(dict, "save_last", buf->b_u_save_nr_last);
+    dict_add_number(dict, "seq_cur", buf->b_u_seq_cur);
+    dict_add_number(dict, "time_cur", (long)buf->b_u_time_cur);
+    dict_add_number(dict, "save_cur", buf->b_u_save_nr_cur);
 
     list = list_alloc();
     if (list != NULL)
     {
-	u_eval_tree(curbuf->b_u_oldhead, list);
+	u_eval_tree(buf, buf->b_u_oldhead, list);
 	dict_add_list(dict, "entries", list);
     }
 }