VIM: Why is this function hanging VIM? - debugging

I have added the following fine function to my status bar to show which function is currently being edited in C-derived languages:
set statusline+=%{WhatFunctionAreWeIn()}
fun WhatFunctionAreWeIn()
let strList = ["while", "foreach", "ifelse", "if else", "for", "if", "else", "try", "catch", "case"]
let foundcontrol = 1
let pos=getpos(".") " This saves the cursor position
let view=winsaveview() " This saves the window view
while (foundcontrol)
let foundcontrol = 0
" Go to the character before the last open {
normal [{
call search('\S','bW')
" If the character is a ) then go to the character
" preceding the () section
let tempchar = getline(".")[col(".") - 1]
if (match(tempchar, ")") >=0 )
normal %
call search('\S','bW')
endif
let tempstring = getline(".")
for item in strList
if( match(tempstring,item) >= 0 )
let foundcontrol = 1
break
endif
endfor
if(foundcontrol == 0)
call cursor(pos)
call winrestview(view)
return tempstring
endif
endwhile
call cursor(pos)
call winrestview(view)
return tempstring
endfun
However, after a few minutes VIM hangs. Disabling the function prevents the hang, so I feel confident that this function is to blame. Is there anything in there that might hang VIM? Is there a better way to accomplish the task of showing the currently-edited function in the status bar?
Thanks.

The issue is that your strategy for determining whether to keep moving to surrounding braces is too aggressive:
Suppose your cursor is on the f in a preprocessor directive #endif between two functions.
Since you're between two functions, there is no unmatched { for [{ to jump to, so the cursor doesn't move.
Your match() against strList hits the if in #endif, causing the loop to continue.
The loop never exits.
I suspect a ctags-based approach, like #Gowtham suggests, will work better, even if it requires some customization.

Related

In Go - anything like a NOP (assembler) command?

When debugging i often need to put a conditional breakpoint, ex :
if messageType == 1 {
nop; // <-- breakpoint
}
There is no 'nop' command and am fairly used to just put a dummy line in which is doing, ex :
if messageType == 1 {
x = 1; // <-- breakpoint
}
And then put the breakpoint at the line with x = 1.
Issue is in Go the compiler often starts battling me because im not using x etc - and am just wondering if theres a quick and smarter way of doing it with a oneline statement ? what do you guys do ?
Just don't store it in a real variable:
if messageType == 1 {
_ = 1
}

How to delete ruby surrounding block (do/end) in vim

How to delete the surround block delimited by do/end in ruby with vim
For example
(10..20).map do |i| <CURSOR HERE>
(1..10).map do |j|
p j
end
end
I want to do something like dsb (delete surround block) and get
(1..10).map do |j|
p j
end
Maybe you can make nnormap.
Every end/do pair is on the same indent, so firstly you should find pair indent - in this case, next line for the same indent (Cause your cursor is in do line.)
So you can make vimscript function with finding next indent line and delete it.
This is an example of the function. You can customize you want - i.e.) set indent for resting lines.
function! DeleteWithSameIndent(inc)
" Get the cursor current position
let currentPos = getpos('.')
let currentLine = currentPos[1]
let firstLine = currentPos[1]
let matchIndent = 0
d
" Look for a line with the same indent level whithout going out of the buffer
while !matchIndent && currentLine != line('$') + 1 && currentLine != -1
let currentLine += a:inc
let matchIndent = indent(currentLine) == indent('.')
endwhile
" If a line is found go to this line
if (matchIndent)
let currentPos[1] = currentLine
call setpos('.', currentPos)
d
endif
endfunction
nnoremap di :call DeleteWithSameIndent(1)<CR>

Why does the debugger have to jump "back and forth" before it sets my tuple value?

I've actually fixed this problem already (while documenting it for this post), but I still want to know is why it is happening, so that I can understand what I did and hopefully avoid wasting time with it in the future.
In a Swift project, I have a function that parses out a string that I know will be presented in a specific format and uses it to fill in some instance variables.
There is a helper function that is passed the string, a starting index, and a divider character and spits out a tuple made up of the next string and the index from which to continue. Just in case a poorly formatted string gets passed in, I define a return type of (String, Int)? and return nil if the divider character isn't found.
The helper function looks, in relevant part, like this:
func nextChunk(stringArray: Array<Character>, startIndex: Int, divider: Character) -> (String, Int)?
{
[...]
var returnValue: (String, Int)? = (returnString, i)
return returnValue
}
So far, so good. I set a breakpoint, and just before the function returns, I see that all is as it should be:
(lldb) po returnValue
(0 = "21三體綜合症", 1 = 7)
{
0 = "21三體綜合症"
1 = 7
}
That's what I expected to see: the correct string value, and the correct index.
However, when I go back to the init() function that called the helper in the first place, and put a breakpoint immediately after the call:
var returnedValue = self.nextChunk(stringArray, startIndex: stringArrayIndex, divider: " ")
I get a completely different value for returnedValue than I had for returnValue:
(lldb) po returnedValue
(0 = "I", 1 = 48)
{
0 = "I"
1 = 48
}
Now here's the really weird part. After I get the return value, I want to test to see if it's nil, and if it's not, I want to use the values I fetched to set a couple of instance variables:
if(returnedValue == nil)
{
return
}
else
{
self.traditionalCharacter = returnedValue!.0
stringArrayIndex = returnedValue!.1
}
If I comment out both of the lines in the "else" brackets:
else
{
// self.traditionalCharacter = returnedValue!.0
// stringArrayIndex = returnedValue!.1
}
then my original breakpoint gives the expected value for the returned tuple:
(lldb) po returnedValue
(0 = "21三體綜合症", 1 = 7)
{
0 = "21三體綜合症"
1 = 7
}
Again: the breakpoint is set before this if/else statement, so I'm taking the value before any of this code has had the chance to execute.
After banging my head against this for a few hours, I realize that...there isn't actually a problem. If I press the "step over" button in the debugger, the execution pointer jumps back from the if() line to the call to nextChunk. Pressing it again sends it forward to "if" again, and sets the values properly.
This extra double-jump only happens if the assignment code is active, consistently and reproducibly. As I know, since I reproduced it for hours trying to figure out what was wrong before even trying stepping forward and noticing that it "fixed itself."
So my question is: why? Is this a bug in the debugger, or am I using breakpoints wrong? It happens just the same whether I put the breakpoint between the function call and the if() or on the if() line. Can someone explain why the debugger is jumping back and forth and when the value I need is actually getting set?

how to remove non character letter from name field using visual foxpro

I have not used Visual FoxPro for a while. Today, my ex-colleague asks me how to remove non character from name field, i.e. only a-z and A-Z are allowed. I remember I used a function called strstran to do it. I needed to define a variable contains a-z and A-Z. But I do not remember now. Does someone knows how to handle this problem. Thanks in advance.
Use the CHRTRAN() function.
FUNCTION GetAlphaCharacters
LPARAMETERS tcExpressionSearched
LOCAL lcAllowedCharacters
m.lcAllowedCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
RETURN CHRTRAN(m.tcExpressionSearched, CHRTRAN(m.tcExpressionSearched, m.lcAllowedCharacters, ""), "")
ENDFUNC
Another option is to use ISALPHA(). This only looks at the left most position in the string but it's not case sensitive.
***This should work, but I haven't tested it.
myresults = ""
myvar = "MyText12"
FOR(i = 1 TO LEN(myvar))
IF ISALPHA( SUBSTR(myvar, i, 1) )
myresults = myresults + SUBSTR(myvar, i, 1)
ENDIF
ENDFOR
RETURN myresults
I know I'm a bit late to the party, but here is a function I wrote to clean out all non-printable ASCII characters from a character string.
CLEAR
* Contains ASCII characters 1 (SOH) and 2 (STX)
cTest = "Garbage Data "
? cTest
cTest = RemoveNonPrintableCharacters(cTest)
? cTest
FUNCTION RemoveNonPrintableCharacters
LPARAMETERS tcExpressionSearched
cCleanExpression = tcExpressionSearched
* Cleans out the first 32 ASCII characters, which are not printable
FOR decCount = 0 TO 31
cCleanExpression = CHRTRAN(m.cCleanExpression, CHR(decCount), "")
ENDFOR
* Also cleans out the non-printable DEL character (ASCII 127)
cCleanExpression = CHRTRAN(m.cCleanExpression, CHR(127), "")
* Return the clean string
RETURN cCleanExpression
ENDFUNC

Why does this LotusScript halt during a loop?

I am trying to write a LotusScript to control a motor. The script I have for reading the bits seem to work fine, but I wish to add a stop button. I have all the commands for making the device stop, but the trouble I'm having is that whenever LotusScript is running through a loop, I cannot click on any of the other buttons.
Does anyone know a way around this???
The scripts I am using are below.
Thank-you kindly.
Andy Barlow
Sub readpositionsub
Dim send_string As String
Dim readString As String
Dim tempString As String
readString = ""
REM Sets the "movement" cell to 6 (the movement int)
[b1].contents = "6"
Do While [b1].contents <> "7"
readString = ""
statusBitString = ""
REM READ STATUS ===!!!===
REM Open the handle to the motor
handle = init_RS232(19200)
REM #1$ reads the status from the controller.
send_string = "#1$"+Chr$(13)
REM Ask the controller to store the results in bits
resultStatus=write_RS232 (handle,send_string)
REM Read Status by looping through all of the bits
For n=0 To 8
tempString = "*1234567"
sendReadCommand = read_RS232(handle,tempString)
If Mid(tempString,1,1) = Chr$(13) Then
Exit For
Else
statusBitString = statusBitString + Mid(tempString,1,1)
End If
Next
[b1].contents = Mid(statusBitString,7, 1)
close_RS232(handle)
REM End Read Status
REM READ POSITION ===!!!===
REM Open the handle to the motor
handle = init_RS232(19200)
send_string = "#1C"+Chr$(13)
t=write_RS232 (handle,send_string)
REM Reading Position
For n=0 To 20
tempString = "*1234567"
r = read_RS232(handle,tempString)
If Mid(tempString,1,1) = Chr$(13) Then
Exit For
Else
readString = readString + Mid(tempString,1,1)
End If
Next
REM End Read Position
[a1].contents=Mid(readString, 4)
close_RS232(handle)
Loop
End Sub
And the stop button that should work should be...
Object btnStop
Sub Click(Source As Buttoncontrol)
REM initialise
handle = init_RS232(19200)
REM Create the string for starting the motor
send_string = "#1S"+Chr$(13)
REM Send the string for starting the motor
resultStartMotor=write_RS232 (handle,send_string)
REM Close the spin handle
close_RS232(handle)
End Sub
You're not running your Lotusscript in a threaded environment, so how do you expect the code on a button to stop the code already running?
If you want to be able to cancel a loop you need to play with the timer object. Basically you start a time that executes one iteration of your loop. At the beginning you look for a changed field value or an ini variable to cancel the timer if set. Now you can use your button to set that variable.
The loop will run much slower than without a timer (after all it has a wait interval you set)

Resources