File: uedindent.py

package info (click to toggle)
far2l 2.7.0~beta%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,304 kB
  • sloc: cpp: 263,566; ansic: 53,886; python: 7,048; sh: 1,516; perl: 410; javascript: 279; xml: 145; makefile: 31
file content (101 lines) | stat: -rw-r--r-- 3,659 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import logging
import time
import ctypes as ct
from far2l.plugin import PluginBase
import far2lc


log = logging.getLogger(__name__)


class Plugin(PluginBase):
    label = "Python ED Indent"
    openFrom = ["PLUGINSMENU", "EDITOR"]

    def OpenPlugin(self, OpenFrom):
        return 0

    def ProcessEditorInput(self, Rec):
        rec = self.ffi.cast("INPUT_RECORD *", Rec)

        if (
            rec.EventType != self.ffic.KEY_EVENT
            or not rec.Event.KeyEvent.bKeyDown
            or (rec.Event.KeyEvent.wVirtualKeyCode != self.ffic.VK_TAB)
            or (rec.Event.KeyEvent.dwControlKeyState & (
                self.ffic.RIGHT_ALT_PRESSED
                | self.ffic.LEFT_ALT_PRESSED
                | self.ffic.RIGHT_CTRL_PRESSED
                | self.ffic.LEFT_CTRL_PRESSED
            ))
        ):
            return 0
        indent = (rec.Event.KeyEvent.dwControlKeyState & (self.ffic.SHIFT_PRESSED)) != 0
        self.Perform(indent)
        return 1

    def Perform(self, indent):
        ei = self.ffi.new("struct EditorInfo *")
        self.info.EditorControl(self.ffic.ECTL_GETINFO, ei)

        if ei.BlockType != self.ffic.BTYPE_STREAM:
            # working only if not vertical block selection
            return 0;

        egs = self.ffi.new("struct EditorGetString *")
        egs.StringNumber = ei.BlockStartLine
        self.info.EditorControl(self.ffic.ECTL_GETSTRING, egs)
        if egs.SelEnd != -1:
            return 0

        eur = self.ffi.new("struct EditorUndoRedo *")
        eur.Command = self.ffic.EUR_BEGIN
        self.info.EditorControl(self.ffic.ECTL_UNDOREDO, eur)

        IndentStr = '\t'
        fIndentStr = self.s2f(IndentStr)

        for lno in range(ei.BlockStartLine, ei.TotalLines):
            esp = self.ffi.new("struct EditorSetPosition *")
            esp.CurLine = lno
            esp.CurPos = esp.Overtype = 0
            esp.CurTabPos = esp.TopScreenLine = esp.LeftPos = -1
            self.info.EditorControl(self.ffic.ECTL_SETPOSITION, esp)

            egs = self.ffi.new("struct EditorGetString *")
            egs.StringNumber = -1
            self.info.EditorControl(self.ffic.ECTL_GETSTRING, egs)

            if egs.SelStart == -1 or egs.SelStart == egs.SelEnd:
                # Stop when reaching the end of the text selection
                break

            if egs.SelEnd != -1:
                # if selection in line not up to end we force selection block up to line end
                # because if selection not up to end the tab replace selected text in last line
                es = self.ffi.new("struct EditorSelect *")
                es.BlockType = ei.BlockType
                es.BlockStartLine = ei.BlockStartLine
                es.BlockStartPos = 0
                es.BlockWidth = 0
                es.BlockHeight = line - ei.BlockStartLine + 1
                self.info.EditorControl(self.ffic.ECTL_SELECT, es)

            if indent:
                self.info.EditorControl(self.ffic.ECTL_INSERTTEXT, fIndentStr)
            else:
                if egs.StringText[0] == '\t':
                    n = 1
                else:
                    n = 0
                    s = self.f2s(egs.StringText)
                    while n < ei.TabSize and s[n] == ' ':
                        n += 1
                for i in range(n):
                    self.info.EditorControl(self.ffic.ECTL_DELETECHAR, self.ffi.NULL)
            
        eur = self.ffi.new("struct EditorUndoRedo *")
        eur.Command = self.ffic.EUR_END
        self.info.EditorControl(self.ffic.ECTL_UNDOREDO, eur)

        self.info.EditorControl(self.ffic.ECTL_REDRAW, self.ffi.NULL)