11# Function called by R if options(editor = nvim.edit).
22# R.nvim sets this option during nvimcom loading.
33nvim.edit <- function (name , file , title ) {
4- if (file != " " ) stop(" Feature not implemented. Use nvim to edit files." )
5- if (is.null(name ))
4+ if (file != " " ) {
5+ stop(" Feature not implemented. Use nvim to edit files." )
6+ }
7+ if (is.null(name )) {
68 stop(" Feature not implemented. Use nvim to create R objects from scratch." )
9+ }
710
811 editf <- paste0(
912 Sys.getenv(" RNVIM_TMPDIR" ),
@@ -22,7 +25,9 @@ nvim.edit <- function(name, file, title) {
2225
2326 .C(nvimcom_msg_to_nvim , paste0(" lua require('r.edit').obj('" , editf , " ')" ))
2427
25- while (file.exists(waitf )) Sys.sleep(1L )
28+ while (file.exists(waitf )) {
29+ Sys.sleep(1L )
30+ }
2631 x <- eval(parse(editf ))
2732 unlink(waitf )
2833 unlink(editf )
@@ -118,7 +123,9 @@ nvim_viewobj <- function(
118123 }
119124 }
120125 if (is.data.frame(o ) || is.matrix(o )) {
121- if (nrows < 0L ) nrows <- ceiling(10000L / ncol(o ))
126+ if (nrows < 0L ) {
127+ nrows <- ceiling(10000L / ncol(o ))
128+ }
122129 if (nrows != 0L && nrows < nrow(o )) {
123130 o <- o [1L : nrows , ]
124131 }
@@ -184,36 +191,41 @@ Rnvim.source <- function(..., print.eval = TRUE, spaced = FALSE) {
184191# ' This function is sent to R Console when the user press `\ss`.
185192# ' @param ... Further arguments passed to base::source.
186193# ' @param local See base::source.
187- Rnvim.selection <- function (... , local = parent.frame())
194+ Rnvim.selection <- function (... , local = parent.frame()) {
188195 Rnvim.source(... , local = local )
196+ }
189197
190198# ' Call base::source.
191199# ' This function is sent to R Console when the user press `\pp`.
192200# ' @param ... Further arguments passed to base::source.
193201# ' @param local See base::source.
194- Rnvim.paragraph <- function (... , local = parent.frame())
202+ Rnvim.paragraph <- function (... , local = parent.frame()) {
195203 Rnvim.source(... , local = local )
204+ }
196205
197206# ' Call base::source.
198207# ' This function is sent to R Console when the user press `\bb`.
199208# ' @param ... Further arguments passed to base::source.
200209# ' @param local See base::source.
201- Rnvim.block <- function (... , local = parent.frame())
210+ Rnvim.block <- function (... , local = parent.frame()) {
202211 Rnvim.source(... , local = local )
212+ }
203213
204214# ' Call base::source.
205215# ' This function is sent to R Console when the user press `\ff`.
206216# ' @param ... Further arguments passed to base::source.
207217# ' @param local See base::source.
208- Rnvim.function <- function (... , local = parent.frame())
218+ Rnvim.function <- function (... , local = parent.frame()) {
209219 Rnvim.source(... , local = local )
220+ }
210221
211222# ' Call base::source.
212223# ' This function is sent to R Console when the user press `\cc`.
213224# ' @param ... Further arguments passed to base::source.
214225# ' @param local See base::source.
215- Rnvim.chunk <- function (... , local = parent.frame())
226+ Rnvim.chunk <- function (... , local = parent.frame()) {
216227 Rnvim.source(... , local = local )
228+ }
217229
218230# ' Source a temporary copy of an R file and, finally, delete it.
219231# ' This function is sent to R Console when the user press `\aa`, `\ae`, or `\ao`.
@@ -284,16 +296,21 @@ nvim.GlobalEnv.fun.args <- function(funcname) {
284296# ' @param prnt Parent environment
285297nvim.min.info <- function (obj , prnt ) {
286298 isnull <- try(is.null(obj ), silent = TRUE )
287- if (class(isnull )[1L ] != " logical" ) return (invisible (NULL ))
288- if (isnull [1L ]) return (invisible (NULL ))
299+ if (class(isnull )[1L ] != " logical" ) {
300+ return (invisible (NULL ))
301+ }
302+ if (isnull [1L ]) {
303+ return (invisible (NULL ))
304+ }
289305
290306 txt <- paste0(class(obj )[1L ], " [" , prnt , " ]" )
291307 objlbl <- attr(obj , " label" )
292- if (! is.null(objlbl ))
308+ if (! is.null(objlbl )) {
293309 txt <- append(
294310 txt ,
295311 c(" " , paste0(" **" , format_text(objlbl , " " , " \x 14" ), " **" ))
296312 )
313+ }
297314 if (is.data.frame(obj )) {
298315 txt <- append(txt , paste0(" dim: " , nrow(obj ), " x " , ncol(obj )))
299316 } else if (is.list(obj )) {
@@ -315,8 +332,12 @@ nvim.min.info <- function(obj, prnt) {
315332# ' @param prnt Parent environment
316333nvim.get.summary <- function (obj , prnt ) {
317334 isnull <- try(is.null(obj ), silent = TRUE )
318- if (class(isnull )[1L ] != " logical" ) return (invisible (NULL ))
319- if (isnull [1L ]) return (invisible (NULL ))
335+ if (class(isnull )[1L ] != " logical" ) {
336+ return (invisible (NULL ))
337+ }
338+ if (isnull [1L ]) {
339+ return (invisible (NULL ))
340+ }
320341
321342 owd <- getOption(" width" )
322343 width <- as.integer(Sys.getenv(" CMPR_DOC_WIDTH" ))
@@ -401,6 +422,18 @@ nvim.plot <- function(x) {
401422 }
402423}
403424
425+ # Display PNG image in terminal using Kitty graphics protocol
426+ # Adapted from https://codeberg.org/djvanderlaan/kitty.r
427+ png2terminal <- function (filename ) {
428+ d <- base64enc :: base64encode(filename , 4096L )
429+ for (i in seq_along(d )) {
430+ cat(paste0(" \0 33_Ga=T,f=100,m=1;" , d [i ], " \0 33\\ " ))
431+ }
432+ cat(" \0 33_Ga=T,f=100,m=0\0 33\\ " )
433+ cat(" \n " )
434+ invisible (NULL )
435+ }
436+
404437# Terminal plot functionality
405438show_plot_in_terminal <- function (
406439 expr ,
@@ -418,12 +451,16 @@ show_plot_in_terminal <- function(
418451 term_cols <- dims [2L ]
419452 char_width <- 12L # pixels per character
420453 char_height <- 24L # pixels per character line
421- if (is.null(width )) width <- as.integer(term_cols * char_width )
454+ if (is.null(width )) {
455+ width <- as.integer(term_cols * char_width )
456+ }
422457 if (is.null(height )) height <- as.integer(term_rows * char_height * 0.7 )
423458 }
424459 }
425460
426- if (is.null(width )) width <- 800L
461+ if (is.null(width )) {
462+ width <- 800L
463+ }
427464 if (is.null(height )) height <- 600L
428465 }
429466
@@ -457,21 +494,9 @@ show_plot_in_terminal <- function(
457494
458495 term <- Sys.getenv(" TERM" )
459496
460- if (term == " xterm-kitty" ) {
461- result <- system2(
462- " kitty" ,
463- c(" +kitten" , " icat" , png_file ),
464- stdout = TRUE ,
465- stderr = TRUE
466- )
467- cat(" \n\n\n " )
468- cat(paste(result , collapse = " " ))
469- cat(" \n\n\n " )
497+ if (term == " xterm-kitty" || term == " wezterm" ) {
498+ png2terminal(png_file )
470499 } else {
471- cat(" DEBUG: No suitable image display tool found\n " )
472- cat(" DEBUG: Available tools:\n " )
473- cat(" - kitty:" , Sys.which(" kitty" ), " \n " )
474- # Fallback to regular plot display
475500 warning(
476501 " Terminal plot display not supported. Install imgcat, timg, chafa, or use Kitty terminal."
477502 )
@@ -528,7 +553,9 @@ nvim.names <- function(x) {
528553# ' Get the class of object.
529554# ' @param x R object.
530555nvim.getclass <- function (x ) {
531- if (missing(x ) || length(charToRaw(x )) == 0L ) return (" #E#" )
556+ if (missing(x ) || length(charToRaw(x )) == 0L ) {
557+ return (" #E#" )
558+ }
532559
533560 if (x == " #c#" ) {
534561 return (" character" )
@@ -544,7 +571,9 @@ nvim.getclass <- function(x) {
544571 options(warn = - 1L )
545572 on.exit(options(warn = saved.warn ))
546573 tr <- try(cls <- class(get(x , envir = .GlobalEnv )), silent = TRUE )
547- if (inherits(tr , " try-error" )) return (" #E#" )
574+ if (inherits(tr , " try-error" )) {
575+ return (" #E#" )
576+ }
548577
549578 return (cls )
550579}
@@ -566,12 +595,13 @@ nvim.getmethod <- function(fname, objclass) {
566595 frm <- formals(fun )
567596 luatbl <- sapply(
568597 frm ,
569- function (x )
598+ function (x ) {
570599 if (length(x ) == 0L ) {
571600 return (" " )
572601 } else {
573602 return (" = " )
574603 }
604+ }
575605 )
576606 env <- paste0(" #\x 02" , fnm )
577607 clpstr <- paste0(" ', cls = 'a', env = '" , env , " '}, {label = '" )
@@ -683,8 +713,9 @@ update_params <- function(fname) {
683713 rm(params , envir = .GlobalEnv )
684714 }
685715 } else {
686- if (! require(knitr , quietly = TRUE ))
716+ if (! require(knitr , quietly = TRUE )) {
687717 stop(" Please, install the 'knitr' package." )
718+ }
688719 flines <- readLines(fname )
689720 params <- knitr :: knit_params(flines )
690721 assign(
0 commit comments