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
' --------
@ -17,7 +41,7 @@ global MULTIPLIER as integer
' Singletons
' -----------
Function getCursor
getCursor = VIEW_CURSOR
getCursor = VIEW_CURSOR
End Function
Function getTextCursor
@ -32,23 +56,23 @@ End Function
' Helper Functions
' -----------------
Sub restoreStatus 'restore original statusbar
dim oLayout
oLayout = thisComponent.getCurrentController.getFrame.LayoutManager
oLayout.destroyElement("private:resource/statusbar/statusbar")
oLayout.createElement("private:resource/statusbar/statusbar")
dim oLayout
oLayout = thisComponent.getCurrentController.getFrame.LayoutManager
oLayout.destroyElement("private:resource/statusbar/statusbar")
oLayout.createElement("private:resource/statusbar/statusbar")
End Sub
Sub setRawStatus(rawText)
thisComponent.Currentcontroller.StatusIndicator.Start(rawText, 0)
thisComponent.Currentcontroller.StatusIndicator.Start(rawText, 0)
End Sub
Sub setStatus(statusText)
setRawStatus(MODE & " | " & statusText)
setRawStatus(MODE & " | " & statusText)
End Sub
Sub setMode(modeName)
MODE = modeName
setRawStatus(modeName)
MODE = modeName
setRawStatus(modeName)
End Sub
Function gotoMode(sMode)
@ -65,7 +89,7 @@ Function gotoMode(sMode)
' Deselect TextCursor
oTextCursor.gotoRange(oTextCursor.getStart(), False)
' Show TextCursor selection
thisComponent.getCurrentController.Select(oTextCursor)
thisComponent.getCurrentController.Select(oTextCursor)
End Select
End Function
@ -74,15 +98,15 @@ End Function
' Multiplier functions
' --------------------
Sub _setMultiplier(n as integer)
MULTIPLIER = n
MULTIPLIER = n
End Sub
Sub resetMultiplier()
_setMultiplier(0)
_setMultiplier(0)
End Sub
Sub addToMultiplier(n as integer)
dim sMultiplierStr as String
dim sMultiplierStr as String
dim iMultiplierInt as integer
' 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)
Function getMultiplier()
If MULTIPLIER = 0 Then
getMultiplier = 1
Else
getMultiplier = MULTIPLIER
End If
If MULTIPLIER = 0 Then
getMultiplier = 1
Else
getMultiplier = MULTIPLIER
End If
End Function
@ -111,14 +135,14 @@ End Function
' Key Handling
' -------------
Sub sStartXKeyHandler
sStopXKeyHandler()
sStopXKeyHandler()
oXKeyHandler = CreateUnoListener("KeyHandler_", "com.sun.star.awt.XKeyHandler")
thisComponent.CurrentController.AddKeyHandler(oXKeyHandler)
oXKeyHandler = CreateUnoListener("KeyHandler_", "com.sun.star.awt.XKeyHandler")
thisComponent.CurrentController.AddKeyHandler(oXKeyHandler)
End Sub
Sub sStopXKeyHandler
thisComponent.CurrentController.removeKeyHandler(oXKeyHandler)
thisComponent.CurrentController.removeKeyHandler(oXKeyHandler)
End Sub
Sub XKeyHandler_Disposing(oEvent)
@ -135,21 +159,21 @@ function KeyHandler_KeyPressed(oEvent) as boolean
Exit Function
End If
dim bConsumeInput, bIsMultiplier, bIsModified, oTextCursor
bConsumeInput = True ' Block all inputs by default
bIsMultiplier = False ' reset multiplier by default
dim bConsumeInput, bIsMultiplier, bIsModified, oTextCursor
bConsumeInput = True ' Block all inputs by default
bIsMultiplier = False ' reset multiplier by default
bIsModified = oEvent.Modifiers > 1 ' If Ctrl or Alt is held down. (Shift=1)
' --------------------------
' Process global shortcuts, exit if matched (like ESC)
If ProcessGlobalKey(oEvent) Then
' Pass
' Process global shortcuts, exit if matched (like ESC)
If ProcessGlobalKey(oEvent) Then
' Pass
ElseIf MODE = "INSERT" Then
bConsumeInput = False ' Allow all inputs
ElseIf MODE = "INSERT" Then
bConsumeInput = False ' Allow all inputs
' If Change Mode
ElseIf ProcessModeKey(oEvent) Then
' If Change Mode
ElseIf ProcessModeKey(oEvent) Then
' Pass
ElseIf ProcessNumberKey(oEvent) Then
@ -164,9 +188,9 @@ function KeyHandler_KeyPressed(oEvent) as boolean
' --------------------------
' Reset multiplier
If not bIsMultiplier Then resetMultiplier()
setStatus(getMultiplier())
' Reset multiplier
If not bIsMultiplier Then resetMultiplier()
setStatus(getMultiplier())
' Show terminal-like cursor
oTextCursor = getTextCursor()
@ -174,11 +198,11 @@ function KeyHandler_KeyPressed(oEvent) as boolean
oTextCursor.gotoRange(oTextCursor.getStart(), False)
oTextCursor.goRight(1, False)
oTextCursor.goLeft(1, True)
thisComponent.getCurrentController.Select(oTextCursor)
thisComponent.getCurrentController.Select(oTextCursor)
ElseIf MODE = "INSERT" Then
oTextCursor.gotoRange(oTextCursor.getStart(), False)
thisComponent.getCurrentController.Select(oTextCursor)
thisComponent.getCurrentController.Select(oTextCursor)
End If
KeyHandler_KeyPressed = bConsumeInput
@ -193,54 +217,54 @@ End Function
' Processing Keys
' ----------------
Function ProcessGlobalKey(oEvent)
dim bMatched
bMatched = True
Select Case oEvent.KeyCode
' PRESSED ESCAPE
Case 1281:
dim bMatched
bMatched = True
Select Case oEvent.KeyCode
' PRESSED ESCAPE
Case 1281:
' Move cursor back if was in INSERT (but stay on same line)
If MODE <> "NORMAL" And Not getCursor().isAtStartOfLine() Then
getCursor().goLeft(1, False)
End If
gotoMode("NORMAL")
Case Else:
bMatched = False
End Select
ProcessGlobalKey = bMatched
gotoMode("NORMAL")
Case Else:
bMatched = False
End Select
ProcessGlobalKey = bMatched
End Function
Function ProcessNumberKey(oEvent)
dim c
c = CStr(oEvent.KeyChar)
dim c
c = CStr(oEvent.KeyChar)
If c >= "0" and c <= "9" Then
addToMultiplier(CInt(c))
ProcessNumberKey = True
Else
ProcessNumberKey = False
End If
If c >= "0" and c <= "9" Then
addToMultiplier(CInt(c))
ProcessNumberKey = True
Else
ProcessNumberKey = False
End If
End Function
Function ProcessModeKey(oEvent)
dim bMatched
bMatched = True
Select Case oEvent.KeyChar
dim bMatched
bMatched = True
Select Case oEvent.KeyChar
' Insert modes
Case "i", "a", "I", "A":
Case "i", "a", "I", "A":
If oEvent.KeyChar = "a" Then getCursor().goRight(1, False)
If oEvent.KeyChar = "I" Then ProcessMovementKey("^")
If oEvent.KeyChar = "A" Then ProcessMovementKey("$")
gotoMode("INSERT")
gotoMode("INSERT")
Case "v":
gotoMode("VISUAL")
Case Else:
bMatched = False
End Select
ProcessModeKey = bMatched
Case Else:
bMatched = False
End Select
ProcessModeKey = bMatched
End Function
@ -270,17 +294,17 @@ Function ProcessDeleteKey(oEvent)
bMatched = True
Select Case oEvent.KeyChar
' Case "d":
' setSpecial("d")
' setSpecial("d")
Case "x":
thisComponent.getCurrentController.Select(oTextCursor)
oTextCursor.setString("")
Case "x":
thisComponent.getCurrentController.Select(oTextCursor)
oTextCursor.setString("")
Case Else:
bMatched = False
End Select
ProcessDeleteKey = bMatched
ProcessDeleteKey = bMatched
End Function
@ -289,7 +313,7 @@ End Function
' -----------------------
' Default: bExpand = False, keyModifiers = 0
Function ProcessMovementKey(keyChar, Optional bExpand, Optional keyModifiers)
dim oTextCursor, bSetCursor, bMatched
dim oTextCursor, bSetCursor, bMatched
oTextCursor = getTextCursor()
bMatched = True
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
bSetCursor = True
Select Case keyChar
Case "l":
oTextCursor.goRight(1, bExpand)
Case "h":
oTextCursor.goLeft(1, bExpand)
Select Case keyChar
Case "l":
oTextCursor.goRight(1, bExpand)
Case "h":
oTextCursor.goLeft(1, bExpand)
' oTextCursor.goUp and oTextCursor.goDown SHOULD work, but doesn't (I dont know why).
' So this is a weird hack
Case "k":
'oTextCursor.goUp(1, False)
getCursor().goUp(1, bExpand)
' oTextCursor.goUp and oTextCursor.goDown SHOULD work, but doesn't (I dont know why).
' So this is a weird hack
Case "k":
'oTextCursor.goUp(1, False)
getCursor().goUp(1, bExpand)
bSetCursor = False
Case "j":
'oTextCursor.goDown(1, False)
getCursor().goDown(1, bExpand)
Case "j":
'oTextCursor.goDown(1, False)
getCursor().goDown(1, bExpand)
bSetCursor = False
' ----------
' ----------
Case "^":
Case "^":
getCursor().gotoStartOfLine(bExpand)
bSetCursor = False
Case "$":
Case "$":
dim oldPos, newPos
oldPos = getCursor().getPosition()
getCursor().gotoEndOfLine(bExpand)
@ -354,25 +378,25 @@ Function ProcessMovementKey(keyChar, Optional bExpand, Optional keyModifiers)
' maybe eventually cursorGoto... should return True/False for bsetCursor
bSetCursor = False
Case "w", "W":
oTextCursor.gotoNextWord(bExpand)
Case "b", "B":
oTextCursor.gotoPreviousWord(bExpand)
Case "e":
oTextCursor.gotoEndOfWord(bExpand)
Case "w", "W":
oTextCursor.gotoNextWord(bExpand)
Case "b", "B":
oTextCursor.gotoPreviousWord(bExpand)
Case "e":
oTextCursor.gotoEndOfWord(bExpand)
Case ")":
oTextCursor.gotoNextSentence(bExpand)
Case "(":
oTextCursor.gotoPreviousSentence(bExpand)
Case "}":
oTextCursor.gotoNextParagraph(bExpand)
Case "{":
oTextCursor.gotoPreviousParagraph(bExpand)
Case Else:
Case ")":
oTextCursor.gotoNextSentence(bExpand)
Case "(":
oTextCursor.gotoPreviousSentence(bExpand)
Case "}":
oTextCursor.gotoNextParagraph(bExpand)
Case "{":
oTextCursor.gotoPreviousParagraph(bExpand)
Case Else:
bSetCursor = False
bMatched = False
End Select
End Select
' If oTextCursor was moved, set global cursor to its position
If bSetCursor Then
@ -392,19 +416,19 @@ Sub initVibreoffice
dim oTextCursor
' Initializing
VIBREOFFICE_STARTED = True
VIEW_CURSOR = thisComponent.getCurrentController.getViewCursor
VIEW_CURSOR = thisComponent.getCurrentController.getViewCursor
resetMultiplier()
gotoMode("NORMAL")
resetMultiplier()
gotoMode("NORMAL")
' Show terminal cursor
oTextCursor = getTextCursor()
oTextCursor.goRight(1, False)
oTextCursor.goLeft(1, True)
thisComponent.getCurrentController.Select(oTextCursor)
thisComponent.getCurrentController.Select(oTextCursor)
sStartXKeyHandler()
sStartXKeyHandler()
End Sub