| 1 |
name = "list"; |
| 2 |
|
| 3 |
group = "files"; |
| 4 |
|
| 5 |
shortDescription = "List lines of a script"; |
| 6 |
|
| 7 |
longDescription = "list <file>:<line> : Lists lines around the given location."; |
| 8 |
longDescription += "\nlist <line> : Lists lines in the current file."; |
| 9 |
|
| 10 |
argumentTypes = [ "script-filename" ]; |
| 11 |
|
| 12 |
listLineNumber = 1; |
| 13 |
listScriptId = -1; |
| 14 |
lastSessionId = -1; |
| 15 |
lastFrameIndex = -1; |
| 16 |
|
| 17 |
function execute() { |
| 18 |
state = 0; |
| 19 |
if (arguments.length > 0) { |
| 20 |
var arg = arguments[0]; |
| 21 |
var colonIndex = arg.lastIndexOf(':'); |
| 22 |
var fileName; |
| 23 |
var lineNumber; |
| 24 |
if (colonIndex == -1) { |
| 25 |
lineNumber = parseInt(arg); |
| 26 |
if (isNaN(lineNumber)) { |
| 27 |
fileName = arg; |
| 28 |
lineNumber = 1; |
| 29 |
} |
| 30 |
} else if (colonIndex == 0) { |
| 31 |
fileName = arg; |
| 32 |
lineNumber = 1; |
| 33 |
} else { |
| 34 |
fileName = arg.slice(0, colonIndex); |
| 35 |
lineNumber = parseInt(arg.slice(colonIndex+1)); |
| 36 |
} |
| 37 |
listLineNumber = Math.max(lineNumber, 1); |
| 38 |
if (fileName != undefined) { |
| 39 |
scheduleResolveScript(fileName); |
| 40 |
state = 1; |
| 41 |
} else { |
| 42 |
setCurrentLineNumber(listLineNumber); |
| 43 |
execute(); |
| 44 |
} |
| 45 |
} else { |
| 46 |
if ((getSessionId() != lastSessionId) |
| 47 |
|| (getCurrentFrameIndex() != lastFrameIndex) |
| 48 |
|| (listScriptId == -1)) { |
| 49 |
listScriptId = getCurrentScriptId(); |
| 50 |
listLineNumber = getCurrentLineNumber(); |
| 51 |
lastSessionId = getSessionId(); |
| 52 |
lastFrameIndex = getCurrentFrameIndex(); |
| 53 |
} |
| 54 |
scheduleGetScriptData(listScriptId); |
| 55 |
state = 2; |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
function handleResponse(resp) { |
| 60 |
if (state == 1) { |
| 61 |
var id = resp.result; |
| 62 |
if (id == -1) { |
| 63 |
message("That script isn't loaded."); |
| 64 |
state = 0; |
| 65 |
return; |
| 66 |
} |
| 67 |
listScriptId = id; |
| 68 |
scheduleGetScriptData(listScriptId); |
| 69 |
state = 2; |
| 70 |
} else if (state == 2) { |
| 71 |
var data = resp.result; |
| 72 |
if (data == undefined) { |
| 73 |
message("No script."); |
| 74 |
state = 0; |
| 75 |
return; |
| 76 |
} |
| 77 |
var base = data.baseLineNumber; |
| 78 |
var lines = data.contents.split('\n'); |
| 79 |
var start = Math.max(listLineNumber - 5, base); |
| 80 |
for (var i = start; i < start + 10; ++i) { |
| 81 |
var ln = lines[i - base]; |
| 82 |
var msg = String(i); |
| 83 |
if (ln != undefined) |
| 84 |
msg += "\t" + ln; |
| 85 |
message(msg); |
| 86 |
} |
| 87 |
listLineNumber += 10; |
| 88 |
state = 0; |
| 89 |
} |
| 90 |
} |