@@ -43,6 +43,7 @@ static gint glspi_newfile(lua_State* L)
4343 } else {
4444 ft = filetypes_lookup_by_name (tmp );
4545 }
46+ /* Fallthrough */
4647 default :
4748 if (!lua_isstring (L , 1 )) { return FAIL_STRING_ARG (1 ); }
4849 fn = lua_tostring (L , 1 );
@@ -102,7 +103,7 @@ static gint glspi_activate(lua_State* L)
102103 }
103104 } else if (lua_idx > 0 ) {
104105 /* Positive number refers to the geany->documents_array index */
105- gint doc_idx = lua_idx - 1 ;
106+ guint doc_idx = ( guint ) ( lua_idx - 1 ) ;
106107 if ((doc_idx < geany -> documents_array -> len ) && documents [doc_idx ]-> is_valid ) {
107108 tab_idx = document_get_notebook_page (documents [doc_idx ]);
108109 }
@@ -208,11 +209,16 @@ static gint glspi_save(lua_State* L)
208209 } else {
209210 if (lua_isnumber (L ,1 )) {
210211 gint idx = (gint )lua_tonumber (L ,1 )- 1 ;
211- status = document_save_file (documents [idx ], TRUE);
212+ GeanyDocument * doc = document_index (idx );
213+ if (DOC_VALID (doc )) {
214+ status = document_save_file (doc , TRUE);
215+ }
212216 } else {
213217 if (lua_isstring (L ,1 )) {
214218 gint idx = filename_to_doc_idx (lua_tostring (L ,1 ));
215- status = document_save_file (documents [idx ], TRUE);
219+ if (idx >= 0 ) {
220+ status = document_save_file (documents [idx ], TRUE);
221+ }
216222 } else { return FAIL_STR_OR_NUM_ARG (1 ); }
217223 }
218224 }
@@ -241,7 +247,10 @@ static gint glspi_open(lua_State* L)
241247 }
242248 }
243249 if (!fn ) {
244- status = document_reload_force (documents [idx ],NULL ) ? idx : -1 ;
250+ GeanyDocument * doc = document_index (idx );
251+ if (DOC_VALID (doc )) {
252+ status = document_reload_force (doc ,NULL ) ? idx : -1 ;
253+ }
245254 } else {
246255 guint len = geany -> documents_array -> len ;
247256 GeanyDocument * doc = document_open_file (fn ,FALSE,NULL ,NULL );
@@ -268,12 +277,17 @@ static gint glspi_close(lua_State* L)
268277 status = document_close (document_get_current ());
269278 } else {
270279 if (lua_isnumber (L ,1 )) {
271- guint idx = (guint )lua_tonumber (L ,1 )- 1 ;
272- status = document_close (documents [idx ]);
280+ gint idx = (gint )lua_tonumber (L ,1 )- 1 ;
281+ GeanyDocument * doc = document_index (idx );
282+ if (DOC_VALID (doc )) {
283+ status = document_close (doc );
284+ }
273285 } else {
274286 if (lua_isstring (L ,1 )) {
275- guint idx = (guint )filename_to_doc_idx (lua_tostring (L ,1 ));
276- status = document_close (documents [idx ]);
287+ gint idx = filename_to_doc_idx (lua_tostring (L ,1 ));
288+ if (idx >= 0 ) {
289+ status = document_close (documents [idx ]);
290+ }
277291 } else { return FAIL_STR_OR_NUM_ARG (1 ); }
278292 }
279293 }
0 commit comments