Fix inconsistent whitespace (remove tabs) and add license

This commit is contained in:
Sean Yeh 2014-12-16 14:08:02 -06:00
parent 9c7b65e6b1
commit e5279f3209
1 changed files with 131 additions and 107 deletions

View File

@ -1,3 +1,27 @@
' vibreoffice - Vi Mode for LibreOffice/OpenOffice
'
' The MIT License (MIT)
'
' Copyright (c) 2014 Sean Yeh
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
' THE SOFTWARE.
Option Explicit Option Explicit
' -------- ' --------
@ -17,7 +41,7 @@ global MULTIPLIER as integer
' Singletons ' Singletons
' ----------- ' -----------
Function getCursor Function getCursor
getCursor = VIEW_CURSOR getCursor = VIEW_CURSOR
End Function End Function
Function getTextCursor Function getTextCursor
@ -32,23 +56,23 @@ End Function
' Helper Functions ' Helper Functions
' ----------------- ' -----------------
Sub restoreStatus 'restore original statusbar Sub restoreStatus 'restore original statusbar
dim oLayout dim oLayout
oLayout = thisComponent.getCurrentController.getFrame.LayoutManager oLayout = thisComponent.getCurrentController.getFrame.LayoutManager
oLayout.destroyElement("private:resource/statusbar/statusbar") oLayout.destroyElement("private:resource/statusbar/statusbar")
oLayout.createElement("private:resource/statusbar/statusbar") oLayout.createElement("private:resource/statusbar/statusbar")
End Sub End Sub
Sub setRawStatus(rawText) Sub setRawStatus(rawText)
thisComponent.Currentcontroller.StatusIndicator.Start(rawText, 0) thisComponent.Currentcontroller.StatusIndicator.Start(rawText, 0)
End Sub End Sub
Sub setStatus(statusText) Sub setStatus(statusText)
setRawStatus(MODE & " | " & statusText) setRawStatus(MODE & " | " & statusText)
End Sub End Sub
Sub setMode(modeName) Sub setMode(modeName)
MODE = modeName MODE = modeName
setRawStatus(modeName) setRawStatus(modeName)
End Sub End Sub
Function gotoMode(sMode) Function gotoMode(sMode)
@ -65,7 +89,7 @@ Function gotoMode(sMode)
' Deselect TextCursor ' Deselect TextCursor
oTextCursor.gotoRange(oTextCursor.getStart(), False) oTextCursor.gotoRange(oTextCursor.getStart(), False)
' Show TextCursor selection ' Show TextCursor selection
thisComponent.getCurrentController.Select(oTextCursor) thisComponent.getCurrentController.Select(oTextCursor)
End Select End Select
End Function End Function
@ -74,15 +98,15 @@ End Function
' Multiplier functions ' Multiplier functions
' -------------------- ' --------------------
Sub _setMultiplier(n as integer) Sub _setMultiplier(n as integer)
MULTIPLIER = n MULTIPLIER = n
End Sub End Sub
Sub resetMultiplier() Sub resetMultiplier()
_setMultiplier(0) _setMultiplier(0)
End Sub End Sub
Sub addToMultiplier(n as integer) Sub addToMultiplier(n as integer)
dim sMultiplierStr as String dim sMultiplierStr as String
dim iMultiplierInt as integer dim iMultiplierInt as integer
' Max multiplier: 10000 (stop accepting additions after 1000) ' Max multiplier: 10000 (stop accepting additions after 1000)
@ -99,11 +123,11 @@ End Function
' Same as getRawMultiplier, but defaults to 1 if it is unset (0) ' Same as getRawMultiplier, but defaults to 1 if it is unset (0)
Function getMultiplier() Function getMultiplier()
If MULTIPLIER = 0 Then If MULTIPLIER = 0 Then
getMultiplier = 1 getMultiplier = 1
Else Else
getMultiplier = MULTIPLIER getMultiplier = MULTIPLIER
End If End If
End Function End Function
@ -111,14 +135,14 @@ End Function
' Key Handling ' Key Handling
' ------------- ' -------------
Sub sStartXKeyHandler Sub sStartXKeyHandler
sStopXKeyHandler() sStopXKeyHandler()
oXKeyHandler = CreateUnoListener("KeyHandler_", "com.sun.star.awt.XKeyHandler") oXKeyHandler = CreateUnoListener("KeyHandler_", "com.sun.star.awt.XKeyHandler")
thisComponent.CurrentController.AddKeyHandler(oXKeyHandler) thisComponent.CurrentController.AddKeyHandler(oXKeyHandler)
End Sub End Sub
Sub sStopXKeyHandler Sub sStopXKeyHandler
thisComponent.CurrentController.removeKeyHandler(oXKeyHandler) thisComponent.CurrentController.removeKeyHandler(oXKeyHandler)
End Sub End Sub
Sub XKeyHandler_Disposing(oEvent) Sub XKeyHandler_Disposing(oEvent)
@ -135,21 +159,21 @@ function KeyHandler_KeyPressed(oEvent) as boolean
Exit Function Exit Function
End If End If
dim bConsumeInput, bIsMultiplier, bIsModified, oTextCursor dim bConsumeInput, bIsMultiplier, bIsModified, oTextCursor
bConsumeInput = True ' Block all inputs by default bConsumeInput = True ' Block all inputs by default
bIsMultiplier = False ' reset multiplier by default bIsMultiplier = False ' reset multiplier by default
bIsModified = oEvent.Modifiers > 1 ' If Ctrl or Alt is held down. (Shift=1) bIsModified = oEvent.Modifiers > 1 ' If Ctrl or Alt is held down. (Shift=1)
' -------------------------- ' --------------------------
' Process global shortcuts, exit if matched (like ESC) ' Process global shortcuts, exit if matched (like ESC)
If ProcessGlobalKey(oEvent) Then If ProcessGlobalKey(oEvent) Then
' Pass ' Pass
ElseIf MODE = "INSERT" Then ElseIf MODE = "INSERT" Then
bConsumeInput = False ' Allow all inputs bConsumeInput = False ' Allow all inputs
' If Change Mode ' If Change Mode
ElseIf ProcessModeKey(oEvent) Then ElseIf ProcessModeKey(oEvent) Then
' Pass ' Pass
ElseIf ProcessNumberKey(oEvent) Then ElseIf ProcessNumberKey(oEvent) Then
@ -164,9 +188,9 @@ function KeyHandler_KeyPressed(oEvent) as boolean
' -------------------------- ' --------------------------
' Reset multiplier ' Reset multiplier
If not bIsMultiplier Then resetMultiplier() If not bIsMultiplier Then resetMultiplier()
setStatus(getMultiplier()) setStatus(getMultiplier())
' Show terminal-like cursor ' Show terminal-like cursor
oTextCursor = getTextCursor() oTextCursor = getTextCursor()
@ -174,11 +198,11 @@ function KeyHandler_KeyPressed(oEvent) as boolean
oTextCursor.gotoRange(oTextCursor.getStart(), False) oTextCursor.gotoRange(oTextCursor.getStart(), False)
oTextCursor.goRight(1, False) oTextCursor.goRight(1, False)
oTextCursor.goLeft(1, True) oTextCursor.goLeft(1, True)
thisComponent.getCurrentController.Select(oTextCursor) thisComponent.getCurrentController.Select(oTextCursor)
ElseIf MODE = "INSERT" Then ElseIf MODE = "INSERT" Then
oTextCursor.gotoRange(oTextCursor.getStart(), False) oTextCursor.gotoRange(oTextCursor.getStart(), False)
thisComponent.getCurrentController.Select(oTextCursor) thisComponent.getCurrentController.Select(oTextCursor)
End If End If
KeyHandler_KeyPressed = bConsumeInput KeyHandler_KeyPressed = bConsumeInput
@ -193,54 +217,54 @@ End Function
' Processing Keys ' Processing Keys
' ---------------- ' ----------------
Function ProcessGlobalKey(oEvent) Function ProcessGlobalKey(oEvent)
dim bMatched dim bMatched
bMatched = True bMatched = True
Select Case oEvent.KeyCode Select Case oEvent.KeyCode
' PRESSED ESCAPE ' PRESSED ESCAPE
Case 1281: Case 1281:
' Move cursor back if was in INSERT (but stay on same line) ' Move cursor back if was in INSERT (but stay on same line)
If MODE <> "NORMAL" And Not getCursor().isAtStartOfLine() Then If MODE <> "NORMAL" And Not getCursor().isAtStartOfLine() Then
getCursor().goLeft(1, False) getCursor().goLeft(1, False)
End If End If
gotoMode("NORMAL") gotoMode("NORMAL")
Case Else: Case Else:
bMatched = False bMatched = False
End Select End Select
ProcessGlobalKey = bMatched ProcessGlobalKey = bMatched
End Function End Function
Function ProcessNumberKey(oEvent) Function ProcessNumberKey(oEvent)
dim c dim c
c = CStr(oEvent.KeyChar) c = CStr(oEvent.KeyChar)
If c >= "0" and c <= "9" Then If c >= "0" and c <= "9" Then
addToMultiplier(CInt(c)) addToMultiplier(CInt(c))
ProcessNumberKey = True ProcessNumberKey = True
Else Else
ProcessNumberKey = False ProcessNumberKey = False
End If End If
End Function End Function
Function ProcessModeKey(oEvent) Function ProcessModeKey(oEvent)
dim bMatched dim bMatched
bMatched = True bMatched = True
Select Case oEvent.KeyChar Select Case oEvent.KeyChar
' Insert modes ' Insert modes
Case "i", "a", "I", "A": Case "i", "a", "I", "A":
If oEvent.KeyChar = "a" Then getCursor().goRight(1, False) If oEvent.KeyChar = "a" Then getCursor().goRight(1, False)
If oEvent.KeyChar = "I" Then ProcessMovementKey("^") If oEvent.KeyChar = "I" Then ProcessMovementKey("^")
If oEvent.KeyChar = "A" Then ProcessMovementKey("$") If oEvent.KeyChar = "A" Then ProcessMovementKey("$")
gotoMode("INSERT") gotoMode("INSERT")
Case "v": Case "v":
gotoMode("VISUAL") gotoMode("VISUAL")
Case Else: Case Else:
bMatched = False bMatched = False
End Select End Select
ProcessModeKey = bMatched ProcessModeKey = bMatched
End Function End Function
@ -270,17 +294,17 @@ Function ProcessDeleteKey(oEvent)
bMatched = True bMatched = True
Select Case oEvent.KeyChar Select Case oEvent.KeyChar
' Case "d": ' Case "d":
' setSpecial("d") ' setSpecial("d")
Case "x": Case "x":
thisComponent.getCurrentController.Select(oTextCursor) thisComponent.getCurrentController.Select(oTextCursor)
oTextCursor.setString("") oTextCursor.setString("")
Case Else: Case Else:
bMatched = False bMatched = False
End Select End Select
ProcessDeleteKey = bMatched ProcessDeleteKey = bMatched
End Function End Function
@ -289,7 +313,7 @@ End Function
' ----------------------- ' -----------------------
' Default: bExpand = False, keyModifiers = 0 ' Default: bExpand = False, keyModifiers = 0
Function ProcessMovementKey(keyChar, Optional bExpand, Optional keyModifiers) Function ProcessMovementKey(keyChar, Optional bExpand, Optional keyModifiers)
dim oTextCursor, bSetCursor, bMatched dim oTextCursor, bSetCursor, bMatched
oTextCursor = getTextCursor() oTextCursor = getTextCursor()
bMatched = True bMatched = True
If IsMissing(bExpand) Then bExpand = False If IsMissing(bExpand) Then bExpand = False
@ -317,28 +341,28 @@ Function ProcessMovementKey(keyChar, Optional bExpand, Optional keyModifiers)
' Set global cursor to oTextCursor's new position if moved ' Set global cursor to oTextCursor's new position if moved
bSetCursor = True bSetCursor = True
Select Case keyChar Select Case keyChar
Case "l": Case "l":
oTextCursor.goRight(1, bExpand) oTextCursor.goRight(1, bExpand)
Case "h": Case "h":
oTextCursor.goLeft(1, bExpand) oTextCursor.goLeft(1, bExpand)
' oTextCursor.goUp and oTextCursor.goDown SHOULD work, but doesn't (I dont know why). ' oTextCursor.goUp and oTextCursor.goDown SHOULD work, but doesn't (I dont know why).
' So this is a weird hack ' So this is a weird hack
Case "k": Case "k":
'oTextCursor.goUp(1, False) 'oTextCursor.goUp(1, False)
getCursor().goUp(1, bExpand) getCursor().goUp(1, bExpand)
bSetCursor = False bSetCursor = False
Case "j": Case "j":
'oTextCursor.goDown(1, False) 'oTextCursor.goDown(1, False)
getCursor().goDown(1, bExpand) getCursor().goDown(1, bExpand)
bSetCursor = False bSetCursor = False
' ---------- ' ----------
Case "^": Case "^":
getCursor().gotoStartOfLine(bExpand) getCursor().gotoStartOfLine(bExpand)
bSetCursor = False bSetCursor = False
Case "$": Case "$":
dim oldPos, newPos dim oldPos, newPos
oldPos = getCursor().getPosition() oldPos = getCursor().getPosition()
getCursor().gotoEndOfLine(bExpand) getCursor().gotoEndOfLine(bExpand)
@ -354,25 +378,25 @@ Function ProcessMovementKey(keyChar, Optional bExpand, Optional keyModifiers)
' maybe eventually cursorGoto... should return True/False for bsetCursor ' maybe eventually cursorGoto... should return True/False for bsetCursor
bSetCursor = False bSetCursor = False
Case "w", "W": Case "w", "W":
oTextCursor.gotoNextWord(bExpand) oTextCursor.gotoNextWord(bExpand)
Case "b", "B": Case "b", "B":
oTextCursor.gotoPreviousWord(bExpand) oTextCursor.gotoPreviousWord(bExpand)
Case "e": Case "e":
oTextCursor.gotoEndOfWord(bExpand) oTextCursor.gotoEndOfWord(bExpand)
Case ")": Case ")":
oTextCursor.gotoNextSentence(bExpand) oTextCursor.gotoNextSentence(bExpand)
Case "(": Case "(":
oTextCursor.gotoPreviousSentence(bExpand) oTextCursor.gotoPreviousSentence(bExpand)
Case "}": Case "}":
oTextCursor.gotoNextParagraph(bExpand) oTextCursor.gotoNextParagraph(bExpand)
Case "{": Case "{":
oTextCursor.gotoPreviousParagraph(bExpand) oTextCursor.gotoPreviousParagraph(bExpand)
Case Else: Case Else:
bSetCursor = False bSetCursor = False
bMatched = False bMatched = False
End Select End Select
' If oTextCursor was moved, set global cursor to its position ' If oTextCursor was moved, set global cursor to its position
If bSetCursor Then If bSetCursor Then
@ -392,19 +416,19 @@ Sub initVibreoffice
dim oTextCursor dim oTextCursor
' Initializing ' Initializing
VIBREOFFICE_STARTED = True VIBREOFFICE_STARTED = True
VIEW_CURSOR = thisComponent.getCurrentController.getViewCursor VIEW_CURSOR = thisComponent.getCurrentController.getViewCursor
resetMultiplier() resetMultiplier()
gotoMode("NORMAL") gotoMode("NORMAL")
' Show terminal cursor ' Show terminal cursor
oTextCursor = getTextCursor() oTextCursor = getTextCursor()
oTextCursor.goRight(1, False) oTextCursor.goRight(1, False)
oTextCursor.goLeft(1, True) oTextCursor.goLeft(1, True)
thisComponent.getCurrentController.Select(oTextCursor) thisComponent.getCurrentController.Select(oTextCursor)
sStartXKeyHandler() sStartXKeyHandler()
End Sub End Sub