From 3380170af8a54ebafcf74450d524e9ea2f962584 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Mon, 14 Jan 2019 22:44:06 -0500 Subject: [PATCH] Add retab --- cmd/micro/action/actions.go | 24 +----------------------- cmd/micro/action/command.go | 1 + cmd/micro/buffer/buffer.go | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/cmd/micro/action/actions.go b/cmd/micro/action/actions.go index 76ec9ecc..a4e6e250 100644 --- a/cmd/micro/action/actions.go +++ b/cmd/micro/action/actions.go @@ -309,29 +309,7 @@ func (h *BufHandler) ParagraphNext() bool { // Retab changes all tabs to spaces or all spaces to tabs depending // on the user's settings func (h *BufHandler) Retab() bool { - // b := h.Buf - // toSpaces := b.Settings["tabstospaces"].(bool) - // tabsize := util.IntOpt(b.Settings["tabsize"]) - // dirty := false - // - // for i := 0; i < b.LinesNum(); i++ { - // l := b.LineBytes(i) - // - // ws := util.GetLeadingWhitespace(l) - // if len(ws) != 0 { - // if toSpaces { - // ws = bytes.Replace(ws, []byte("\t"), []byte(util.Spaces(tabsize)), -1) - // } else { - // ws = bytes.Replace(ws, []byte(util.Spaces(tabsize)), []byte("\t"), -1) - // } - // } - // - // l = bytes.TrimLeft(l, " \t") - // b.lines[i].data = append(ws, l...) - // dirty = true - // } - // - // b.IsModified = dirty + h.Buf.Retab() return true } diff --git a/cmd/micro/action/command.go b/cmd/micro/action/command.go index 7d40baec..e72c4687 100644 --- a/cmd/micro/action/command.go +++ b/cmd/micro/action/command.go @@ -150,6 +150,7 @@ func (h *BufHandler) PluginCmd(args []string) { // RetabCmd changes all spaces to tabs or all tabs to spaces // depending on the user's settings func (h *BufHandler) RetabCmd(args []string) { + h.Buf.Retab() } // RawCmd opens a new raw view which displays the escape sequences micro diff --git a/cmd/micro/buffer/buffer.go b/cmd/micro/buffer/buffer.go index cc5658ad..b585e688 100644 --- a/cmd/micro/buffer/buffer.go +++ b/cmd/micro/buffer/buffer.go @@ -601,3 +601,29 @@ func (b *Buffer) FindMatchingBrace(braceType [2]rune, start Loc) Loc { } return start } + +// Retab changes all tabs to spaces or vice versa +func (b *Buffer) Retab() { + toSpaces := b.Settings["tabstospaces"].(bool) + tabsize := IntOpt(b.Settings["tabsize"]) + dirty := false + + for i := 0; i < b.LinesNum(); i++ { + l := b.LineBytes(i) + + ws := GetLeadingWhitespace(l) + if len(ws) != 0 { + if toSpaces { + ws = bytes.Replace(ws, []byte{'\t'}, bytes.Repeat([]byte{' '}, tabsize), -1) + } else { + ws = bytes.Replace(ws, bytes.Repeat([]byte{' '}, tabsize), []byte{'\t'}, -1) + } + } + + l = bytes.TrimLeft(l, " \t") + b.lines[i].data = append(ws, l...) + dirty = true + } + + b.isModified = dirty +}