Skip to content

Commit 6248660

Browse files
committed
Make sure there is a final newline when pasting non-binary data to file
1 parent f72d00d commit 6248660

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Diff for: v2/copypaste.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package main
22

33
import (
4+
"bytes"
45
"fmt"
56
"os"
67
"strings"
78
"time"
89

10+
"github.com/xyproto/binary"
911
"github.com/xyproto/clip"
1012
"github.com/xyproto/env/v2"
1113
"github.com/xyproto/files"
@@ -73,6 +75,13 @@ func WriteClipboardToFile(filename string, overwrite, primaryClipboard bool) (in
7375
return 0, "", "", err
7476
}
7577

78+
// If it's not binary data, make sure there is a final newline
79+
if !binary.Data(contents) {
80+
if !bytes.HasSuffix(contents, []byte{'\n'}) {
81+
contents = append(contents, '\n')
82+
}
83+
}
84+
7685
// Write to file
7786
f, err := os.Create(filename)
7887
if err != nil {
@@ -226,7 +235,7 @@ func (e *Editor) Paste(c *vt100.Canvas, status *StatusBar, copyLines, previousCo
226235
)
227236

228237
// Consider smart indentation for programming languages
229-
if e.ProgrammingLanguage() || e.mode == mode.Config { // not mode.Ini and mode.Fstab, since those seldom have indentations
238+
if ProgrammingLanguage(e.mode) || e.mode == mode.Config { // not mode.Ini and mode.Fstab, since those seldom have indentations
230239
// Indent the block that is about to be pasted to the smart indentation level, if the block had no indentation
231240
if getLeadingWhitespace(firstLine) == "" {
232241
leadingWhitespace := e.LeadingWhitespace()

0 commit comments

Comments
 (0)