Skip to content

Commit

Permalink
Reimplemented fhook magic string handling so it doesn't clobber $resu…
Browse files Browse the repository at this point in the history
…lt. Documented feature.
  • Loading branch information
Steven Phillips committed Aug 29, 2024
1 parent d4042fe commit 0273406
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 88 deletions.
3 changes: 1 addition & 2 deletions microemacs/3rdparty/tfs/winmsvc.mak
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ LDLSTT =
!ENDIF
OUTDIRR = .$(BUILDID)-release
OUTDIRD = .$(BUILDID)-debug
INSTDIR = ..\..\bin\$(BUILDID)
TRDPARTY = ..

CCDEFS = /DWIN32 /D_WIN32 /D_WIN32_WINNT=0x0600 /D_$(BIT_SIZE)BIT /W3 /Zi /EHs-c- /D_HAS_EXCEPTIONS=0 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_DEPRECATE /I$(TRDPARTY)\zlib
Expand All @@ -96,14 +97,12 @@ OUTDIR = $(OUTDIRD)
CCFLAGS = $(CCFLAGSD)
LDFLAGS = $(LDFLAGSD)
ARFLAGS = $(ARFLAGSD)
INSTDIR =
INSTPRG = - echo No install - debug
!ELSE
OUTDIR = $(OUTDIRR)
CCFLAGS = $(CCFLAGSR)
LDFLAGS = $(LDFLAGSR)
ARFLAGS = $(ARFLAGSR)
INSTDIR = ..\..\bin\$(BUILDID)
INSTPRG = copy
!ENDIF

Expand Down
12 changes: 9 additions & 3 deletions microemacs/doc/m3mac064.3
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,16 @@ a file extension or magic string association with the hook file. i.e.
.CE
.Me $ehf-scheme
.RE
On loading a file which matches the
The first time a file is loaded that that is bound to \fBfhook-XXX\fR via an
.Ht add-file-hook 2
definition then the \fChkXXX.emf\fR file is executed and the template is
applied to the file.
definition, the \fChkXXX.emf\fR macro file is executed and then the \fBfhook-XXX\fR command is executed
with the file's buffer current so that the template is applied to it. Only the execution of \fBfhook-XXX\fR is done on
subsequent loading of files of this type.
.PP
When the \fBfhook-XXX\fR command is executed it's
.Hl #l9 Variables 4
register variable is set to the matched magic string if the binding was via a file content hook, or an empty string.
This can be useful as the magic string often contains configuration values such as the tab and indent width.
.PP
A basic template framework to be used with the \fBbuff-init\fR commands may be
defined as follows for a language template called \fIXXX\fR:-
Expand Down
6 changes: 3 additions & 3 deletions microemacs/macros/buffinit.emf
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ define-macro-file collapse collapse-current collapse-all beginning-of-function e
!emacro

0 define-macro buffer-init
set-variable #l9 $result
set-variable #l9 #p9
set-variable $buffer-fhook &cat "fhook-" &set #l8 @1
buffer-command-setup
set-variable #l0 &ind &spr ".%s.setup" $buffer-fhook
Expand Down Expand Up @@ -171,8 +171,8 @@ define-macro-file collapse collapse-current collapse-all beginning-of-function e
buffer-bind-create "bo" "esc q" "" comment-restyle
!endif
!endif
!if &or &exi &ind &spr ".%s.fold" $buffer-fhook &sin "fold-open:" $result
!force 0 fold-setup $result
!if &or &exi &ind &spr ".%s.fold" $buffer-fhook &sin "fold-open:" #l9
!force 0 fold-setup #l9
!iif &not $status !jump 2
!elif &sin "f" #l0
buffer-bind-create "b" "f2" "" collapse-current
Expand Down
9 changes: 3 additions & 6 deletions microemacs/macros/hkjst.emf
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@
; Notes: broken out of hkdoc.emf and docmacro.emf
;
define-macro fhook-jst
set-variable #l1 $result
@# buffer-init "jst"
; Over-ride any special document modes
; Search for additional modes in a:
; -!- jst; fill-column: <digit> ; fill-mode: <mode> -!-
;
!if &not &seq #l1 ""
!if &xseq #l1 ".*fill-column:[ \t]*\\([0-9]+\\)[ \t]*;.*"
set-variable $buffer-fill-col @s1
!endif
!if &xseq #l1 ".*fill-mode:[ \t]*\\([a-zA-Z]+\\)[ \t]*;.*"
!if &not &seq #l9 ""
!iif &xseq #l9 ".*fill-column:[ \t]*\\([0-9]+\\)[ \t]*;.*" set-variable $buffer-fill-col @s1
!if &xseq #l9 ".*fill-mode:[ \t]*\\([a-zA-Z]+\\)[ \t]*;.*"
set-variable #l0 @s1
!if &iseq #l0 "left"
set-variable $buffer-fill-mode "L"
Expand Down
7 changes: 3 additions & 4 deletions microemacs/macros/hkpls.emf
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
; ^proceedure[ \t\n]+[a-zA-Z0-9_]+[ \t]*([^)]*)[ \t\n]*;

define-macro fhook-pls
set-variable #l1 $result
set-variable $buffer-mask "luh1"
@# buffer-init "pls"
buffer-init-hooks
; Search for additional modes in a -!- pl/sql -!-
;
; -!- pls; tabwidth:<digit>; -!-
;
!if &not &seq #l1 ""
!if &not &seq #l9 ""
!iif &xseq #l9 ".*tabsize[ \t]*:[ \t]*\\([0-9]+\\)[ \t]*[-;].*" set-variable $buffer-indent-width @s1
!iif &xseq #l9 ".*tabwidth[ \t]*:[ \t]*\\([0-9]+\\)[ \t]*[-;].*" set-variable $buffer-tab-width @s1
!endif
Expand Down Expand Up @@ -569,10 +568,10 @@ set-variable .fhook-pls.fold-mopen "1"
;EXTRA; hilight .hilight.pls 1 "wait" .scheme.keyword
!if &band .hilight.flags 0x08
; hilight constants, e.g. numbers
hilight .hilight.pls 1 "[[:digit:]]+" .scheme.constant
hilight .hilight.pls 1 "[[:digit:]]+" .scheme.constant
hilight .hilight.pls 1 "-[[:digit:]]+" .scheme.constant
hilight .hilight.pls 1 "[[:digit:]]+\\.[[:digit:]]+" .scheme.constant
hilight .hilight.c 1 "-[[:digit:]]+\\.[[:digit:]]+" .scheme.constant
hilight .hilight.pls 1 "-[[:digit:]]+\\.[[:digit:]]+" .scheme.constant
!endif
!endif

Expand Down
117 changes: 58 additions & 59 deletions microemacs/src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,34 +168,33 @@ assignHooks(meBuffer *bp, meUByte *hooknm)
{
meUByte fn[meBUF_SIZE_MAX], buff[meBUF_SIZE_MAX] ; /* Temporary buffer */

buff[0] = 'h' ;
buff[1] = 'k' ;
meStrcpy(buff+2,hooknm+6) ;
buff[0] = 'h';
buff[1] = 'k';
meStrcpy(buff+2,hooknm+6);
if(!fileLookup(buff,extMacroCnt,extMacroLst,meFL_CHECKDOT|meFL_USESRCHPATH,fn))
{
if(hooknm == defaultHookName)
triedDefault++ ;
triedDefault++;
else if(hooknm == binaryHookName)
triedBinary++ ;
triedBinary++;
else if(hooknm == rbinHookName)
triedRbin++ ;
triedRbin++;
else
mlwrite(MWABORT|MWWAIT,(meUByte *)"Failed to find file [%s]",buff);
}
else
{
execFile(fn,0,1) ;
bp->fhook = decode_fncname(hooknm,1) ;
execFile(fn,0,1);
bp->fhook = decode_fncname(hooknm,1);
}
}
if((bp->fhook = decode_fncname(hooknm,1)) >= 0)
resultStr[0] = '\0' ;
bp->fhook = decode_fncname(hooknm,1);
*hooknm = 'b' ;
bp->bhook = decode_fncname(hooknm,1) ;
bp->bhook = decode_fncname(hooknm,1);
*hooknm = 'd' ;
bp->dhook = decode_fncname(hooknm,1) ;
bp->dhook = decode_fncname(hooknm,1);
*hooknm = 'e' ;
bp->ehook = decode_fncname(hooknm,1) ;
bp->ehook = decode_fncname(hooknm,1);
*hooknm = 'f';
return;
}
Expand All @@ -221,7 +220,8 @@ assignHooks(meBuffer *bp, meUByte *hooknm)
void
setBufferContext(meBuffer *bp)
{
int ii ;
meLine *lp, *tlp;
int ii, ml=0;

#if MEOPT_COLOR
/* First setup the global scheme - this can be missed by buffers loaded with -c */
Expand All @@ -233,9 +233,8 @@ setBufferContext(meBuffer *bp)
!meModeTest(bp->mode,MDRBIN) && ((bp->fileFlag & meBFFLAG_DIR) == 0) &&
((ii = fileHookCount) > 0))
{
meUByte *pp, cc ;
meLine *lp, *tlp ;
int nn ;
meUByte *pp, cc;
int nn;

/* search for the first non-blank line */
for(lp=meLineGetNext(bp->baseLine) ; lp != bp->baseLine ; lp = meLineGetNext(lp))
Expand All @@ -250,84 +249,74 @@ setBufferContext(meBuffer *bp)
(meRegexComp(&meRegexStrCmp,fileHookExt[ii],(nn < 0) ? meREGEX_ICASE:0) == meREGEX_OKAY))
{
if(nn < 0)
nn = -nn ;
tlp = lp ;
nn = -nn;
tlp = lp;
do {
if(meRegexMatch(&meRegexStrCmp,meLineGetText(tlp),
meLineGetLength(tlp),0,meLineGetLength(tlp),0))
{
assignHooks(bp,fileHookFunc[ii]) ;
if(bp->fhook >= 0)
{
ii = meRegexStrCmp.group[0].end - meRegexStrCmp.group[0].start ;
if(ii >= meBUF_SIZE_MAX)
ii = meBUF_SIZE_MAX - 1 ;
meStrncpy(resultStr,meLineGetText(tlp)+
meRegexStrCmp.group[0].start,ii) ;
resultStr[ii] = '\0' ;
ii = 0 ;
break ;
}
assignHooks(bp,fileHookFunc[ii]);
ml = meRegexStrCmp.group[0].end - meRegexStrCmp.group[0].start;
ii = 0;
break;
}
} while((--nn > 0) && ((tlp=meLineGetNext(tlp)) != bp->baseLine)) ;
} while((--nn > 0) && ((tlp=meLineGetNext(tlp)) != bp->baseLine));
}
}
break ;
break;
}
}
}
}
if(bp->fhook < 0)
{
meUByte *hooknm ;
meUByte *hooknm;

/* Do file hooks */
if(meModeTest(bp->mode,MDBINARY))
hooknm = binaryHookName ;
hooknm = binaryHookName;
else if(meModeTest(bp->mode,MDRBIN))
hooknm = rbinHookName ;
hooknm = rbinHookName;
else
{
meUByte *sp, *bn ;
int ll, bnll ;
meUByte *sp, *bn;
int ll, bnll;

/* Find the length of the string to pass into check_extension.
* Check if its name has a <?> and/or a backup ~, if so reduce
* the length so '<?>' & '~'s not inc. */
sp = bp->name ;
ll = meStrlen(sp) ;
if((sp[ll-1] == '>') && (bp->fileName != NULL) &&
((bn = getFileBaseName(bp->fileName)) != NULL) &&
((bnll = meStrlen(bn)) > 0) && (ll > bnll) &&
(sp[bnll] == '<') && !meStrncmp(sp,bn,bnll))
ll = bnll ;
sp = bp->name;
ll = meStrlen(sp);
if((sp[ll-1] == '>') && (bp->fileName != NULL) && ((bn = getFileBaseName(bp->fileName)) != NULL) &&
((bnll = meStrlen(bn)) > 0) && (ll > bnll) && (sp[bnll] == '<') && !meStrncmp(sp,bn,bnll))
ll = bnll;
if(sp[ll-1] == '~')
ll-- ;
ll--;
if(ll)
{
meUByte cc = sp[ll-1] ;
meUByte cc = sp[ll-1];
if(cc == '~')
{
ll-- ;
ll--;
if((ll > 2) && (sp[ll-1] == '~') && (sp[ll-2] == '.'))
ll -= 2 ;
ll -= 2;
}
else if(isDigit(cc))
{
int ii=ll-2 ;
int ii=ll-2;
while(ii > 0)
{
cc = sp[ii--] ;
cc = sp[ii--];
if(!isDigit(cc))
{
if((cc == '~') && (sp[ii] == '.'))
ll = ii ;
break ;
ll = ii;
break;
}
}
}
}
ii = fileHookCount ;
ii = fileHookCount;
while(--ii >= 0)
if((fileHookArg[ii] == 0) &&
checkExtent(sp,ll,fileHookExt[ii],
Expand All @@ -338,16 +327,26 @@ setBufferContext(meBuffer *bp)
#endif
))
{
hooknm = fileHookFunc[ii] ;
break ;
hooknm = fileHookFunc[ii];
break;
}
if(ii < 0)
hooknm = defaultHookName ;
hooknm = defaultHookName;
}
assignHooks(bp,hooknm) ;
assignHooks(bp,hooknm);
}
if(bp->fhook >= 0)
execBufferFunc(bp,bp->fhook,meEBF_ARG_GIVEN,(bp->intFlag & BIFFILE)) ;
{
/* copy the magic string identifier or "" to fhook's #l9 */
if(ml)
{
if(ml >= meBUF_SIZE_MAX)
ml = meBUF_SIZE_MAX - 1;
meStrncpy(meRegCurr->next->reg[9],meLineGetText(tlp)+meRegexStrCmp.group[0].start,ml);
}
meRegCurr->next->reg[9][ml] = '\0';
execBufferFunc(bp,bp->fhook,meEBF_ARG_GIVEN,(bp->intFlag & BIFFILE));
}
}
#endif

Expand Down
14 changes: 7 additions & 7 deletions microemacs/src/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1517,21 +1517,21 @@ donbuf(meLine *hlp, meVariable **varList, meUByte *commandName, int f, int n)
meUByte oldcle;
int oldexec, status;

if((rp=meRegCurr->next) == NULL)
/* Always make sure there is another child so functions calling execBufferFunc can use it */
if((rp=meRegCurr->next)->next == NULL)
{
if(meRegCurr->depth >= meMACRO_DEPTH_MAX)
if(rp->depth > meMACRO_DEPTH_MAX)
{
/* macro recursion gone too deep, bail out.
* attempt to force the break out by pretending C-g was pressed */
TTbreakFlag = 1 ;
return mlwrite(MWABORT|MWWAIT,(meUByte *)"[Macro recursion gone too deep]") ;
}
if((rp = meMalloc(sizeof(meRegister))) == NULL)
if((rp->next = meMalloc(sizeof(meRegister))) == NULL)
return meABORT;
meRegCurr->next = rp;
rp->depth = meRegCurr->depth + 1;
rp->prev = meRegCurr;
rp->next = NULL;
rp->next->depth = rp->depth + 1;
rp->next->prev = rp;
rp->next->next = NULL;
}
/* save the arguments */
oldcle = clexec;
Expand Down
3 changes: 1 addition & 2 deletions microemacs/src/winmsvc.mak
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ LDLSTT =
!ENDIF
OUTDIRR = .$(BUILDID)-release
OUTDIRD = .$(BUILDID)-debug
INSTDIR = ..\bin\$(BUILDID)
TRDPARTY = ..\3rdparty

CCDEFS = /D_WIN32 /D_ARCHITEC=$(ARCHITEC) /D_TOOLKIT=$(TOOLKIT) /D_TOOLKIT_VER=$(TOOLKIT_VER) /D_PLATFORM_VER=$(PLATFORM_VER) /D_$(BIT_SIZE)BIT /D_WIN$(BIT_SIZE) /D_WIN32_WINNT=0x0600 /DWINVER=0x0600 /W3 /Zi /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_DEPRECATE /I$(TRDPARTY)\tfs /I$(TRDPARTY)\zlib /DmeVER_CN=$(meVER_CN) /DmeVER_YR=$(meVER_YR) /DmeVER_MN=$(meVER_MN) /DmeVER_DY=$(meVER_DY)
Expand All @@ -131,14 +132,12 @@ BOUTDIR = $(OUTDIRD)
CCFLAGS = $(CCFLAGSD)
LDFLAGS = $(LDFLAGSD)
ARFLAGS = $(ARFLAGSD)
INSTDIR =
INSTPRG = - echo No install - debug
!ELSE
BOUTDIR = $(OUTDIRR)
CCFLAGS = $(CCFLAGSR)
LDFLAGS = $(LDFLAGSR)
ARFLAGS = $(ARFLAGSR)
INSTDIR = ..\bin\$(BUILDID)
INSTPRG = copy
!ENDIF

Expand Down
Loading

0 comments on commit 0273406

Please sign in to comment.