From f63fa42d98674d581a4c05e4032cc392915b3dbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=2E=20R=C3=B8dseth?= Date: Wed, 15 Jan 2025 10:25:31 +0100 Subject: [PATCH] Fix an issue with "clear on quit" when displaying images --- v2/cmenu.go | 10 ++++------ v2/command.go | 2 +- v2/editor.go | 2 +- v2/keyloop.go | 17 ++++++++++------- v2/main.go | 6 +++--- v2/neweditor.go | 6 +++--- 6 files changed, 22 insertions(+), 21 deletions(-) diff --git a/v2/cmenu.go b/v2/cmenu.go index 7cb5f521..ea0fe516 100644 --- a/v2/cmenu.go +++ b/v2/cmenu.go @@ -479,16 +479,14 @@ func (e *Editor) CommandMenu(c *vt100.Canvas, tty *vt100.TTY, status *StatusBar, if firstWordContainsOneOf(parentCommand(), searchProcessNames) { actions.Add("Kill parent and exit without saving", func() { e.stopParentOnQuit = true - e.clearOnQuit = true - e.quit = true // indicate that the user wishes to quit - e.clearOnQuit = true // clear the terminal after quitting + e.quit = true // indicate that the user wishes to quit + e.clearOnQuit.Store(true) // clear the terminal after quitting }) } else { actions.Add("Exit without saving", func() { e.stopParentOnQuit = false - e.clearOnQuit = false - e.quit = true // indicate that the user wishes to quit - e.clearOnQuit = true // clear the terminal after quitting + e.quit = true // indicate that the user wishes to quit + e.clearOnQuit.Store(true) // clear the terminal after quitting }) } diff --git a/v2/command.go b/v2/command.go index 085c400d..bffd1fc4 100644 --- a/v2/command.go +++ b/v2/command.go @@ -319,7 +319,7 @@ func (e *Editor) CommandToFunction(c *vt100.Canvas, tty *vt100.TTY, status *Stat savequitclear: func() { // save and quit, then clear the screen e.UserSave(c, tty, status) e.quit = true - e.clearOnQuit = true + e.clearOnQuit.Store(true) }, sortblock: func() { // sort the current block of lines, until the next blank line or EOF undo.Snapshot(e) diff --git a/v2/editor.go b/v2/editor.go index a83208c6..03b7349a 100644 --- a/v2/editor.go +++ b/v2/editor.go @@ -49,7 +49,6 @@ type Editor struct { expandTags bool // can be used for XML and HTML syntaxHighlight bool // syntax highlighting stopParentOnQuit bool // send SIGQUIT to the parent PID when quitting - clearOnQuit bool // clear the terminal when quitting the editor, or not quit bool // for indicating if the user wants to end the editor session readOnly bool // is the file read-only when initializing o? debugHideOutput bool // hide the GDB stdout pane when in debug mode? @@ -71,6 +70,7 @@ type Editor struct { highlightCurrentLine bool // highlight the current line highlightCurrentText bool // highlight the current text (not the entire line) // atomic.Bool are used for values that might be read when redrawing text asynchronously + clearOnQuit atomic.Bool // clear the terminal when quitting the editor, or not changed atomic.Bool // has the contents changed, since last save? redraw atomic.Bool // if the contents should be redrawn in the next loop redrawCursor atomic.Bool // if the cursor should be moved to the location it is supposed to be diff --git a/v2/keyloop.go b/v2/keyloop.go index 75951a5e..70cbdd3a 100644 --- a/v2/keyloop.go +++ b/v2/keyloop.go @@ -91,12 +91,15 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber e, messageAfterRedraw, displayedImage, err := NewEditor(tty, c, fnord, lineNumber, colNumber, theme, syntaxHighlight, true, monitorAndReadOnly, nanoMode, createDirectoriesIfMissing, displayQuickHelp) if err != nil { if e != nil { - return "", false, e.clearOnQuit, err + return "", false, e.clearOnQuit.Load(), err } return "", false, false, err } else if displayedImage { // A special case for if an image was displayed instead of a file being opened - return "", false, e.clearOnQuit, nil + if e != nil { + return "", false, e.clearOnQuit.Load(), nil + } + return "", false, false, nil } // Find the absolute path to this filename @@ -181,7 +184,7 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber } else { // Lock the current file, if it's not already locked if err := fileLock.Lock(absFilename); err != nil { - return fmt.Sprintf("Locked by another (possibly dead) instance of this editor.\nTry: o -f %s", filepath.Base(absFilename)), false, e.clearOnQuit, errors.New(absFilename + " is locked") + return fmt.Sprintf("Locked by another (possibly dead) instance of this editor.\nTry: o -f %s", filepath.Base(absFilename)), false, e.clearOnQuit.Load(), errors.New(absFilename + " is locked") } // Immediately save the lock file as a signal to other instances of the editor fileLock.Save() @@ -1052,7 +1055,7 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber e.blockMode = false // If o is used as a man page viewer, exit at the press of esc if e.mode == mode.ManPage { - e.clearOnQuit = false + e.clearOnQuit.Store(false) e.quit = true break } @@ -2097,7 +2100,7 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber break } else if e.mode == mode.ManPage && keyRunes[0] == 'q' { // If o is used as a man page viewer, exit at the press of "q" - e.clearOnQuit = false + e.clearOnQuit.Store(false) e.quit = true break } @@ -2256,7 +2259,7 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber e.CloseLocksAndLocationHistory(canUseLocks, absFilename, lockTimestamp, forceFlag, &closeLocksWaitGroup) // Quit everything that has to do with the terminal - if e.clearOnQuit { + if e.clearOnQuit.Load() { vt100.Clear() vt100.Close() } else { @@ -2276,5 +2279,5 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber stopBackgroundProcesses() // All done - return "", e.stopParentOnQuit, e.clearOnQuit, nil + return "", e.stopParentOnQuit, e.clearOnQuit.Load(), nil } diff --git a/v2/main.go b/v2/main.go index fb104ca0..0965ebf8 100644 --- a/v2/main.go +++ b/v2/main.go @@ -467,10 +467,10 @@ func main() { NoTitle() // Clear the current color attribute - if !clearOnQuit { - fmt.Print("\n" + vt100.Stop()) - } else { + if clearOnQuit { fmt.Print(vt100.Stop()) + } else { + fmt.Print("\n" + vt100.Stop()) } traceComplete() // if building with -tags trace diff --git a/v2/neweditor.go b/v2/neweditor.go index add140cb..d5d090b4 100644 --- a/v2/neweditor.go +++ b/v2/neweditor.go @@ -119,7 +119,7 @@ func NewEditor(tty *vt100.TTY, c *vt100.Canvas, fnord FilenameOrData, lineNumber // Minor adjustments for some file types switch e.mode { case mode.Email, mode.Git, mode.ManPage: - e.clearOnQuit = true + e.clearOnQuit.Store(true) } // Set the editor filename @@ -370,12 +370,12 @@ func NewEditor(tty *vt100.TTY, c *vt100.Canvas, fnord FilenameOrData, lineNumber // NetBSD, FreeBSD, OpenBSD or Dragonfly e.SetTheme(NewRedBlackTheme()) DisableQuickHelpScreen(nil) - e.clearOnQuit = true + e.clearOnQuit.Store(true) } else if shell := env.Str("SHELL"); shell != "/usr/local/bin/fish" && (shell == "/bin/csh" || shell == "/bin/ksh" || strings.HasPrefix(shell, "/usr/local/bin")) && !inVTEGUI && filepath.Base(os.Args[0]) != "default" { // This is likely to be FreeBSD or OpenBSD (and the executable/link name is not "default") e.SetTheme(NewRedBlackTheme()) DisableQuickHelpScreen(nil) - e.clearOnQuit = true + e.clearOnQuit.Store(true) } else if colorString := env.Str("COLORFGBG"); strings.Contains(colorString, ";") { fields := strings.Split(colorString, ";") backgroundColor := fields[len(fields)-1]