Chris Stern's BBEdit Python language module.
This commit is contained in:
parent
22fcae976a
commit
dbc692e286
|
@ -0,0 +1,456 @@
|
|||
#include <AEDataModel.h>
|
||||
|
||||
#define DEBUG 0
|
||||
|
||||
#define kComponentSignatureString "BBPy.LM"
|
||||
#include <Debugging.h>
|
||||
|
||||
|
||||
#include <BBLMInterface.h>
|
||||
#include <BBXTInterface.h>
|
||||
//#include <BBLMTextIterator.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <Sound.h>
|
||||
|
||||
#if DEBUG
|
||||
void debugf_(const char* func,const char* fileName,long line, const char*fmt,...)
|
||||
{
|
||||
va_list arg;
|
||||
char msg[256];
|
||||
va_start(arg, fmt);
|
||||
vsnprintf(msg,256 ,fmt, arg);
|
||||
DebugAssert(COMPONENT_SIGNATURE, DEBUG_NO_OPTIONS, kComponentSignatureString ": " , msg, nil, fileName, line, 0 );
|
||||
|
||||
//debug_string(msg);
|
||||
}
|
||||
#define debugf(FMT,...) debugf_( __FUNCTION__,__FILE__, __LINE__,FMT,__VA_ARGS__);
|
||||
#else
|
||||
#define debugf(FMT,...)
|
||||
#endif
|
||||
|
||||
typedef const char *Str;
|
||||
|
||||
|
||||
enum{
|
||||
kPyBBLMStringSubst = kBBLMFirstUserRunKind
|
||||
};
|
||||
|
||||
#define iswordchar(x) (isalnum(x)||x=='_')
|
||||
|
||||
|
||||
struct runloc{
|
||||
bool past_gap;
|
||||
long pos;
|
||||
long last_start;
|
||||
unsigned char*p;
|
||||
};
|
||||
|
||||
char start(struct runloc& r,BBLMParamBlock &pb)
|
||||
{
|
||||
r.past_gap = false;
|
||||
r.last_start = pb.fCalcRunParams.fStartOffset;
|
||||
r.pos = pb.fCalcRunParams.fStartOffset;
|
||||
r.p = ((unsigned char*)pb.fText) + pb.fCalcRunParams.fStartOffset;
|
||||
// Adjust for the gap if we¹re not already past it.
|
||||
if ((!r.past_gap) && (r.pos >= pb.fTextGapLocation)){
|
||||
r.p += pb.fTextGapLength;
|
||||
r.past_gap = true;
|
||||
}
|
||||
return *r.p;
|
||||
|
||||
}
|
||||
|
||||
char nextchar(struct runloc&r,BBLMParamBlock &pb)
|
||||
{
|
||||
if ( r.pos< pb.fTextLength){
|
||||
++r.pos;
|
||||
++r.p;
|
||||
if ((!r.past_gap) && (r.pos >= pb.fTextGapLocation)){
|
||||
r.p += pb.fTextGapLength;
|
||||
r.past_gap = true;
|
||||
}
|
||||
return *r.p;
|
||||
}
|
||||
else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool addRun(BBLMRunCode kind, int start,int len , const BBLMCallbackBlock& bblm_callbacks)
|
||||
{
|
||||
if (len > 0){ // Tie off the code run we were in, unless the length is zero.
|
||||
debugf("Run %d %d:%d", kind, start, start+len-1 );
|
||||
return bblmAddRun( &bblm_callbacks, 'Pyth',
|
||||
kind, start, len, false);
|
||||
|
||||
}
|
||||
else{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool addRunBefore (BBLMRunCode kind,struct runloc& r, const BBLMCallbackBlock& bblm_callbacks)
|
||||
{
|
||||
bool more_runs = addRun(kind, r.last_start, r.pos - r.last_start, bblm_callbacks);
|
||||
r.last_start = r.pos;
|
||||
return more_runs;
|
||||
}
|
||||
|
||||
bool addRunTo (BBLMRunCode kind, struct runloc& r, const BBLMCallbackBlock& bblm_callbacks)
|
||||
{
|
||||
bool more_runs = addRun(kind, r.last_start, r.pos - r.last_start+1, bblm_callbacks);
|
||||
r.last_start = r.pos+1;
|
||||
return more_runs;
|
||||
}
|
||||
|
||||
|
||||
bool colorstr( char delim,
|
||||
BBLMParamBlock &pb,
|
||||
struct runloc &r,
|
||||
const BBLMCallbackBlock &bblm_callbacks)
|
||||
{
|
||||
bool tripple = false , pers = false, lookup = false, more_runs = true;
|
||||
char c = nextchar(r,pb);
|
||||
|
||||
if (c == delim){
|
||||
c = nextchar(r,pb);
|
||||
if (c == delim){
|
||||
tripple = true;
|
||||
c = nextchar(r,pb);
|
||||
}
|
||||
else{
|
||||
//double
|
||||
return addRunBefore(kBBLMRunIsSingleString,r,bblm_callbacks);
|
||||
}
|
||||
}
|
||||
while (c && more_runs){
|
||||
if (pers ){
|
||||
if (isalpha(c)){
|
||||
more_runs = addRunTo(kPyBBLMStringSubst,r,bblm_callbacks);
|
||||
}
|
||||
else if (c == '('){
|
||||
lookup = true;
|
||||
}
|
||||
}
|
||||
pers = false;
|
||||
if (c == delim){
|
||||
if (tripple){
|
||||
if ((c = nextchar(r,pb))== delim && (c = nextchar(r,pb)) == delim){
|
||||
break; // end of tripple-quote.
|
||||
}
|
||||
}
|
||||
else{
|
||||
break; // end of single-quote.
|
||||
}
|
||||
|
||||
}
|
||||
else if (c== '\\'){
|
||||
nextchar(r,pb);
|
||||
}
|
||||
else if (c=='\r'||c=='\n'){
|
||||
if (!tripple){
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (c=='%'){
|
||||
more_runs = addRunBefore(kBBLMRunIsSingleString,r,bblm_callbacks);
|
||||
pers = true;
|
||||
}
|
||||
else if (c==')' && lookup){
|
||||
more_runs = addRunTo(kPyBBLMStringSubst,r,bblm_callbacks);
|
||||
lookup = false;
|
||||
}
|
||||
c = nextchar(r,pb);
|
||||
}
|
||||
return more_runs && addRunTo(lookup?kPyBBLMStringSubst:kBBLMRunIsSingleString,r,bblm_callbacks);
|
||||
}
|
||||
|
||||
bool colorcomment(BBLMParamBlock &pb,
|
||||
struct runloc &r,
|
||||
const BBLMCallbackBlock &bblm_callbacks)
|
||||
{
|
||||
while (char c = nextchar(r,pb)){
|
||||
if (c=='\r'|| c=='\n'){
|
||||
break;
|
||||
}
|
||||
}
|
||||
return addRunTo(kBBLMRunIsLineComment,r,bblm_callbacks);
|
||||
}
|
||||
|
||||
void CalculateRuns(BBLMParamBlock &pb,
|
||||
const BBLMCallbackBlock &bblm_callbacks)
|
||||
|
||||
{
|
||||
const struct rundesc *state = NULL;
|
||||
bool more_runs=true;
|
||||
|
||||
struct runloc r;
|
||||
|
||||
char c = start(r,pb);
|
||||
|
||||
while (c && more_runs){
|
||||
loop:
|
||||
// Process a char
|
||||
if (state==NULL){
|
||||
//If we're in the basic 'code' state, check for each interesting char (rundelims[i].start).
|
||||
switch (c){
|
||||
case '\'':
|
||||
case '"':
|
||||
more_runs = addRunBefore(kBBLMRunIsCode,r,bblm_callbacks);
|
||||
if (more_runs){
|
||||
more_runs = colorstr(c,pb,r,bblm_callbacks);
|
||||
}
|
||||
break;
|
||||
case '#' :
|
||||
more_runs = addRunBefore(kBBLMRunIsCode,r,bblm_callbacks);
|
||||
if (more_runs){
|
||||
more_runs = colorcomment(pb,r,bblm_callbacks);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
c = nextchar(r,pb);
|
||||
}
|
||||
if (more_runs){
|
||||
addRunBefore(kBBLMRunIsCode,r,bblm_callbacks);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
static void AdjustRange(BBLMParamBlock ¶ms,
|
||||
const BBLMCallbackBlock &callbacks)
|
||||
{
|
||||
DescType language;
|
||||
BBLMRunCode kind;
|
||||
SInt32 charPos;
|
||||
SInt32 length;
|
||||
UInt32 index = params.fAdjustRangeParams.fStartIndex;
|
||||
|
||||
while( index > 0 &&
|
||||
bblmGetRun(&callbacks, index, language, kind, charPos, length) &&
|
||||
(kind==kPyBBLMStringSubst||kind==kBBLMRunIsSingleString)){
|
||||
index--;
|
||||
};
|
||||
params.fAdjustRangeParams.fStartIndex = index;
|
||||
}
|
||||
|
||||
|
||||
// The next couple funcs process the text of a file assumming it's in 1 piece in memory,
|
||||
// so they may not be called from CalculateRuns.
|
||||
|
||||
bool matchword(BBLMParamBlock &pb, const char *pat ,unsigned long *pos)
|
||||
{
|
||||
const char *asciText = (const char *) (pb.fTextIsUnicode?NULL:pb.fText);
|
||||
|
||||
int i;
|
||||
for (i=0; pat[i]; i++){
|
||||
if (*pos+i>=pb.fTextLength){
|
||||
return false;
|
||||
}
|
||||
if (asciText[*pos+i] != pat[i]){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if ((*pos+i<pb.fTextLength)&&iswordchar(asciText[*pos+i])){
|
||||
return false;
|
||||
}
|
||||
*pos+=i;
|
||||
return true;
|
||||
}
|
||||
|
||||
int matchindent(BBLMParamBlock &pb, UInt32 *pos)
|
||||
{
|
||||
const char *asciText = (const char *) (pb.fTextIsUnicode?NULL:pb.fText);
|
||||
int indent=0;
|
||||
|
||||
while(*pos<pb.fTextLength){
|
||||
switch (/*(char)(pb.fTextIsUnicode?uniText[pos]:*/asciText[*pos]/*)*/){
|
||||
case ' ':
|
||||
++*pos;
|
||||
indent++;
|
||||
break;
|
||||
case '\t':
|
||||
++*pos;
|
||||
indent+=8;
|
||||
break;
|
||||
case '#':
|
||||
return -1;
|
||||
break;
|
||||
default:
|
||||
return indent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void eat_line(BBLMParamBlock &pb, unsigned long* pos)
|
||||
{
|
||||
const char *asciText = (const char *) (pb.fTextIsUnicode?NULL:pb.fText);
|
||||
while (asciText[*pos]!='\r' && asciText[*pos]!='\n' && *pos<pb.fTextLength) {++*pos;}
|
||||
while ((asciText[*pos]=='\r' || asciText[*pos]=='\n') && *pos<pb.fTextLength) {++*pos;}
|
||||
|
||||
}
|
||||
|
||||
void addItem(BBLMParamBlock &pb, UInt32 pos, int nest, BBLMFunctionKinds kind,
|
||||
const BBLMCallbackBlock *bblm_callbacks)
|
||||
{
|
||||
UInt32 funcstartpos = pos;
|
||||
UInt32 funcnamelen=0;
|
||||
UInt32 offset=0;
|
||||
const char *asciText = (const char *) pb.fText;
|
||||
UInt32 index;
|
||||
OSErr err;
|
||||
|
||||
while (isspace(asciText[pos]) && pos<pb.fTextLength) {++pos;}
|
||||
UInt32 fnamestart = pos;
|
||||
while ((isalnum(asciText[pos])||asciText[pos]=='_') && pos<pb.fTextLength) {pos++; funcnamelen++;}
|
||||
|
||||
err = bblmAddTokenToBuffer( bblm_callbacks,
|
||||
pb.fFcnParams.fTokenBuffer,
|
||||
(void*)&asciText[fnamestart],
|
||||
funcnamelen,
|
||||
pb.fTextIsUnicode,
|
||||
&offset);
|
||||
BBLMProcInfo procInfo;
|
||||
procInfo.fFunctionStart = fnamestart; // char offset in file of first character of function
|
||||
procInfo.fFunctionEnd = pos; // char offset of last character of function
|
||||
|
||||
procInfo.fSelStart = fnamestart; // first character to select when choosing function
|
||||
procInfo.fSelEnd = pos; // last character to select when choosing function
|
||||
|
||||
procInfo.fFirstChar = fnamestart; // first character to make visible when choosing function
|
||||
|
||||
procInfo.fKind = kind;
|
||||
|
||||
procInfo.fIndentLevel = nest; // indentation level of token
|
||||
procInfo.fFlags = 0; // token flags (see BBLMFunctionFlags)
|
||||
procInfo.fNameStart = offset; // char offset in token buffer of token name
|
||||
procInfo.fNameLength = funcnamelen; // length of token name
|
||||
|
||||
err = bblmAddFunctionToList(bblm_callbacks,
|
||||
pb.fFcnParams.fFcnList,
|
||||
procInfo,
|
||||
&index);
|
||||
}
|
||||
|
||||
|
||||
|
||||
enum{
|
||||
maxnest=5
|
||||
};
|
||||
|
||||
void ScanForFunctions(BBLMParamBlock &pb,
|
||||
const BBLMCallbackBlock &bblm_callbacks)
|
||||
{
|
||||
|
||||
const char *asciText = (const char *) (pb.fTextIsUnicode?NULL:pb.fText);
|
||||
UniCharPtr uniText = (UniCharPtr) (pb.fTextIsUnicode?pb.fText:NULL);
|
||||
|
||||
int indents[maxnest]= {0};
|
||||
int nest = 0;
|
||||
|
||||
UInt32 pos=0; // current character offset
|
||||
|
||||
|
||||
while (pos<pb.fTextLength){
|
||||
|
||||
int indent = matchindent(pb, &pos);
|
||||
|
||||
if (indent >= 0){
|
||||
for (int i=0; i <= nest; i++){
|
||||
if (indent<=indents[i]){
|
||||
nest = i;
|
||||
indents[nest]=indent;
|
||||
goto x;
|
||||
}
|
||||
}
|
||||
indents[++nest]=indent;
|
||||
x:
|
||||
|
||||
if (matchword(pb,"def",&pos)){
|
||||
addItem( pb, pos, nest, kBBLMFunctionMark, &bblm_callbacks);
|
||||
}
|
||||
else if (matchword(pb, "class", &pos)){
|
||||
addItem( pb, pos, nest, kBBLMTypedef, &bblm_callbacks);
|
||||
}
|
||||
}
|
||||
eat_line(pb,&pos);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
OSErr main( BBLMParamBlock ¶ms,
|
||||
const BBLMCallbackBlock &bblm_callbacks,
|
||||
const BBXTCallbackBlock &bbxt_callbacks)
|
||||
{
|
||||
OSErr result;
|
||||
|
||||
if ((params.fSignature != kBBLMParamBlockSignature) ||
|
||||
(params.fLength < sizeof(BBLMParamBlock)))
|
||||
{
|
||||
return paramErr;
|
||||
}
|
||||
|
||||
switch (params.fMessage)
|
||||
{
|
||||
case kBBLMInitMessage:
|
||||
case kBBLMDisposeMessage:
|
||||
{
|
||||
result = noErr; // nothing to do
|
||||
break;
|
||||
}
|
||||
|
||||
case kBBLMCalculateRunsMessage:
|
||||
CalculateRuns(params, bblm_callbacks);
|
||||
result = noErr;
|
||||
break;
|
||||
|
||||
case kBBLMScanForFunctionsMessage:
|
||||
ScanForFunctions(params, bblm_callbacks);
|
||||
result = noErr;
|
||||
break;
|
||||
|
||||
case kBBLMAdjustRangeMessage:
|
||||
AdjustRange(params, bblm_callbacks);
|
||||
result = noErr;
|
||||
break;
|
||||
|
||||
case kBBLMMapRunKindToColorCodeMessage:
|
||||
switch (params.fMapRunParams.fRunKind){
|
||||
case kPyBBLMStringSubst:
|
||||
params.fMapRunParams.fColorCode = kBBLMSGMLAttributeNameColor;
|
||||
params.fMapRunParams.fMapped = true;
|
||||
break;
|
||||
default:
|
||||
params.fMapRunParams.fMapped = false;
|
||||
}
|
||||
result = noErr;
|
||||
break;
|
||||
|
||||
case kBBLMEscapeStringMessage:
|
||||
case kBBLMAdjustEndMessage:
|
||||
case kBBLMMapColorCodeToColorMessage:
|
||||
case kBBLMSetCategoriesMessage:
|
||||
case kBBLMMatchKeywordMessage:
|
||||
{
|
||||
result = userCanceledErr;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
result = paramErr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,35 @@
|
|||
#include "BBLMTypes.r"
|
||||
#include "MacTypes.r"
|
||||
|
||||
#define kKeyWords 1057
|
||||
|
||||
resource 'BBLF' (128, "Python Language Mappings", purgeable)
|
||||
{
|
||||
kCurrentBBLFVersion,
|
||||
|
||||
{
|
||||
kLanguagePython,
|
||||
(kBBLMScansFunctions|kBBLMColorsSyntax|kBBLMIsCaseSensitive),
|
||||
kKeyWords,
|
||||
"Python",
|
||||
{
|
||||
kNeitherSourceNorInclude, ".py",
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#define VERSION 0x1, 0x0, final, 0x0
|
||||
|
||||
resource 'vers' (1) {
|
||||
VERSION,
|
||||
verUS,
|
||||
"1.1",
|
||||
"1.1,"
|
||||
};
|
||||
|
||||
resource 'vers' (2) {
|
||||
VERSION,
|
||||
verUS,
|
||||
$$Date,
|
||||
$$Date
|
||||
};
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1 @@
|
|||
(This file must be converted with BinHex 4.0)
:!!"58e*$4'peC`#3#!(GM(8!N!3"!!!!!CX!!!#E!!!!3J#3mjGKEQ3*$@&cFf9
bG!eLFQ9KD`eME'&cF`eMEfjdD@jeC3eNC@B0C'9X$@9XD@B0C@acC3ePH'0PF(3
0CAKPB`eQD@jKE'aj$@C[FJeQFQpY$@GXEf*KE!eTCJeTEA"[FR30D@i0DA-0E'&
YBQ4K$@j[G!e[FJe`BA0c$A"bD@jd$A*KDA0P$A*PG(9bEJedFRN0GfKTE'80!!!
"!!!!!CX!!!#E!!!!3J1S!h`#qJ!!!"`!-J!!9%9B9!!!!!S%)3#3#Jp3HA4SEfi
J5f9jGfpbC(0&9`:
|
|
@ -0,0 +1,16 @@
|
|||
This is the Python Language Module for BBEdit.
|
||||
|
||||
This software is a plugin to Bare Bones Software's BBEdit 6.0.2 (or more), designed to make editing & browsing Python Language files easer.
|
||||
|
||||
It parses any file ending in .py (or extentions of your choice.) providing BBEdit with the information BBEdit needs to provide services for python files similar to those it provides for 'C'. Namely: syntax coloring and populating BBEdit's 'Ÿ' popup menu with file's functions and classes.
|
||||
|
||||
This Plug-in needs to be placed in your :BBEdit 6.0:BBEdit Support:Language Modules: folder.
|
||||
|
||||
If you wish, I have no objections to redistributing it in whole or in part, modify it, or beating small fury animals to death with rolled up printouts of the source code.
|
||||
|
||||
Christopher Stern
|
||||
cistern@earthlink.net
|
||||
|
||||
Note by Jack:
|
||||
Also check out the BBPy contributed software, which allows you to execute
|
||||
Python code from within BBEdit.
|
Loading…
Reference in New Issue