Skip to main content
Topic: easier editing of nwctxt? (Read 20412 times) previous topic - next topic

easier editing of nwctxt?

Hi all,

I think several of us sometimes select objects in NWC and copy and paste them into an editor (like Notepad), so we can change the nwctxt and copy and paste them back.  For example, this can be really handy to change individual notes within a chord, to change a position and/or to add/change/delete accidentals, ties, noteheads, colors, etc.

Borrowing heavily on nwsw_SimpleEditor.wxphp and on nwsw_nwctxtFileExplorer.wxphp, I have created a simple editor designed to edit nwctxt (clip or file).  The editor automatically removes the header and footer, and any comments (file), and any fake/context items (clip), and presents the selected objects in a WYSIWYG editor.

Within the editor, the standard copy/cut/paste actions are available, as well as undo/redo.  There are find and find-next actions, and also replace and replace-next.  A select-all is there, but you shouldn't need it, as all of the text is automatically passed back to NWC upon exit.  I don't currently see any need for new/open/save.

I am looking into adding an "nwctxt lexer", to add some color to the nwctxt.  This can make it easier to read, and to spot syntax errors in your changes, before exiting back to NWC with them.  I will wait to see if there is any interest in this editor, before putting any more than today's work into it.

Invoking the editor is just like invoking the existing explorer.  Create one shortcut for "clip" mode and another for "file" mode.  Simply copy the existing explorer entries and tweak them, to create the new editor entries.  (Btw, the new copy command in the user tools box is a good way to see built-in command info, since you can't edit them any more.)

The "file" mode for this editor could be particularly handy I think, to edit song and staff header information.  I don't know of any other way to cut and paste file nwctxt to an editor, so making such changes to file nwctxt previously involved saving the file and later reloading it (or exporting and reimporting), which probably was never worth doing previously.


P.S.  Conventionally, you should rename the ".v#.php" script to be ".wxphp", but it should work as ".php" too - the forum allows a "php" extension but not a "wxphp" one.

Edit - [red]You can download the latest NWC and User Tool Kit betas here:[/red]

Re: easier editing of nwctxt?

Reply #1
Initial tests OK once I figured out the invocation.

If Options->'Compress Input' is not checked, I get this error when I close the dialog:
Quote from: STDERR
Note: [in C:\Program Files\Noteworthy Software\NoteWorthy Composer 2\Scripts\lib\, at line 65]
--> Uninitialized string offset: 0
This is weird since the tool seems to support uncompressed input. STDOUT looks OK as well.

My version of is "2.493"

Code at line 65 is part of NWC2ClassifyLine($ln). It seems to throw the error if $ln is empty. IMO, should test for this and return something sensible rather than erring.
Registered user since 1996

Re: easier editing of nwctxt?

Reply #2
Thanks for looking at this Rick!

Invocation is just as for nwsw nwctxt file/clip explorer, but perhaps not everyone has that by default?!  I have it under Developer Tools, and I don't think I added it myself - it's one of the new "locked-down" built-in tools I think.  It's really easy to add the editor, if you just copy and tweak the corresponding explorer entry!

I think if input is not compressed (which I obviously did not test, and should have), the text is perhaps padded out to a full block (with null chars), and that makes it appear like there's more text after reading the last nwctxt line.  PHP's feof still returns false after reading the last nwctxt line, as there are more physical chars left, but fgets then returns an empty string (after tossing the null chars).  I agree that NWC2ClassifyLine might have handled this more gracefully (and it may well have in the past, but was lost in a speed optimization), but I've added a precheck to toss empty lines before calling it.  With the zlib decompression filter turned on, reading the true last line chews through all physical chars, and the feof is immediately detected.

In addition to adding the precheck for an empty string (line 30), I've added a SetMarginWidth call to get rid of the default left-side margin (line 204).

Re: easier editing of nwctxt?

Reply #3
Colorizing is available now!  New version 3 is attached to first message in this topic.

Lines 15-40 of the script were added, and allow a user to highly customize his colors quite easily.  It is more flexible than you might think, but less flexible than you might dream.  :-)  The very basic color scheme I have in the script right now is best seen editing an entire file, rather than a small clip.

The current NWC wxWidgets DLL did not support several critical methods that would have been required to write my own lexer.  So I borrowed the best one already available, which interestingly turned out to be one for assembler code!

Someday, I'd like to implement "code folding".  This would allow say an entire staff or measure to be collapsed down to a single line when not working in that area.  It could be expanded as needed, and would be "auto-expanded" upon saving back to NWC.  I am used to this when viewing Music XML files, and it's quite handy.

Re: easier editing of nwctxt?

Reply #4
Coming along nicely. Since this seems to be "alpha quality code", I'll assue that eventually it get cleaned up and unneeded lines will be removed. You seem to spend a lot of code defending against things that will never happen. For instance, I think it is safe to assume that STDIN is coming from NWC rather that just random bytes from the cosmos. You will always get one header. You will never get an object before a header and you will never get a mismatched footer. At most you should just stop processing STDOUT when you get a footer and err if none is found.
Registered user since 1996

Re: easier editing of nwctxt?

Reply #5
I'm likely being overconfident, but I consider this script to be beyond "alpha", and even beyond "beta" - I consider it done!  I will of course fix any bugs found, and I'd love to hear opinions on a default color schema.  But it isn't that complex of a script to expect major problems!

I actually planned on leaving the defensive code in!  I think it adds no measurable realtime to the execution, and I have a faint idea that I may allow some sort of "import" in the future.  (Say for example that one wants to pull in a "quintuplet" template from a canned file. :-)  I'd want to do a bit more verification of nwctxt from a file, as opposed to from NWC.

Re: easier editing of nwctxt?

Reply #6
Hi Rick - I think you'll be more comfortable with the V4 script attached above.  I've removed all but one of the error checks, and I've pulled some code out into functions, so a couple of places look much simpler now.

I've also added a "nwctxt.colors.php" file - I'm thinking we all should override with our personal preferences there, so that any new version of the tool does not overwrite them.

Re: easier editing of nwctxt?

Reply #7
I sorta like:
so you don't get a silly error if it can't find the user color file :-)
Registered user since 1996

Re: easier editing of nwctxt?

Reply #8
Someday, I'd like to implement "code folding".  ... I am used to this when viewing Music XML files, and it's quite handy.

Me too I'm used to it and, sure, I find it quite handy.

I use UltraEdit as my default editor so I created a syntax file for nwctxt in order to have syntax highlight, code folding and "function" (i.e. stave) list.
If there is someone else interested (beside me and Richard) I can put it in the "tools" section and/or in the scripto.

Good idea to separate the "user configurable" part (nwctxt.colors.php) from the main program.
I agree with Rick about the @include_once.

I disagree with Rick about the formal checks.
In my experience it's always better to exceed in that direction than in the other, except if it's so exagerate that you'll have troubles when the format evolves.
Furthermore, the checks were already in place!

Thank you Randy.

Re: easier editing of nwctxt?

Reply #9
In Replace, you don't allow an empty replacement string. In most cases, a "#" or space will work. A bit tedious, but one can also use F3, Delete, F3, Delete, ...
Registered user since 1996

Re: easier editing of nwctxt?

Reply #10
I'd purposely used "include_once" instead of "require_once", specifically because the include will not fail on file not found, when the require will.  Or so I thought.  Technically speaking, PHP will emit a warning if the include fails, instead of the fatal error caused by a require failing.  The warning would go unnoticed by default, but sets an error handler that makes it visible.  I should have tested the no-file situation instead of assuming that I had it covered!

I went back and forth on the replace allowing an empty replacement string.  I ended up leaving it out, since it simplified the code greatly, and it could still be done (as you say) with F3/Delete pairs.  I will revisit this, and see if I can get the code in there again, without undue complexity this time.  I wanted to wait and see if anyone noticed this, and wanted such "bulk" deleting.

I am going to fix these shortly, and also add more keywords to the default set - I think I want every possible keyword added, in the extreme.  I'd then have to update the script for any future NWCTXT extensions, but when typing in new NWCTXT, it's nice to have the "confirmation" that you've typed it right, by seeing the color turn on.  One annoyance is that "4th" through "64th" register as "numbers" even if a keyword is added for them.

I can't do "folding" or syntax error highlighting until NWC's wxWidgets supports a few more methods, and I can write my own lexer.

Re: easier editing of nwctxt?

Reply #11
I've attached version 5 to the first post in this topic.

I was not able to use the "@" for the include, as I think that it does not help at all (it certainly did not prevent the nwc error handler from kicking in just the same to display the warning!), and I think it can do harm (suppressing errors within the include file itself so the user does not know why his changes aren't taking effect!).  I simply added a file_exists check before doing a require_once, to get the best of both worlds.

I've added some code to initialized the replace text to "null", and only set it to an empty string once the user clicks "ok" in the "replace" window.  In this way, it won't do a "replacing delete" until the user has "confirmed" that that's what he really wants to do.  You'll notice that a "replace next" brings up the prompt, even if find text was previously entered, so the replace text can be confirmed.

I've added keyword sets for all the field names (in addition to the prior object types) for file properties, staff properties, staff lyrics, and staff notations.  There are several cases where a field name can be used for very different object types, and without an nwc lexer, the code cannot tell the difference and highlight accordingly.  (See the comments in getKeywordList for details.)  I have not yet tried to enter all the "enumerated" field values yet.

Re: easier editing of nwctxt?

Reply #12
Last update for now is attached above.  I've added a bunch of field value keywords.  Again, without a context-sensitive lexer, there is no way to sort out keywords with multiple uses.

Re: easier editing of nwctxt?

Reply #13
HI Randy,

Excuse me for my stupidity, but I don't succeed in invoking your editor.
I tried 2 ways:
- Starting from a .nwctxt file, using "Open with...' ==> nothing happens
- From within NWC, as a User Tool : ==> Error Message in STDERR:
Note: [in C:\Program Files\NoteWorthy Software\NoteWorthy Composer 2\Scripts\prw_nwctxtFileEditor.php, at line 13]
--> require_once(lib/ failed to open stream: No such file or directory

Please help me out.
Thanks in advance,
Always look on the bright side of life!

Re: easier editing of nwctxt?

Reply #14
Hi Gust,

No apologies needed.  I've been meaning to add more instructions here anyway!

First off, I'd recommend making sure you have the very latest beta version of NWC, and then the very latest beta version of NWC User Tools.  These are both pretty trivial to download and install from the backstage forum (once you've authenticated yourself as a valid NWC 2.X user):

Secondly, make sure you only invoke the editor as a User Tool.  From the error message below, you seem to have the user tool defined correctly.  But for posterity, the recommended user tool name is "nwctxt File Editor (prw)" for your file editing user tool entry, and/or "nwctxt Clip Editor (prw)" for your clip editing user tool entry.  The user tool command for both is "php\php.exe scripts\prw_nwctxtFileEditor.wxphp" (rename my script above from ".v#.php" to ".wxphp").  For your file editing user tool entry, make the input type "file text" and check the "returns file text" option.  For your clip editing user tool entry, make the input type "clip text".  For both user tool entries, make at least one of "long task handling" and "prompts for user input" be checked, and optionally check "compress input".

Please let me know if you have any further troubles!


Re: easier editing of nwctxt?

Reply #15
Hi Randy,

Thanks for the quick response.
I also received a personal message from Rick, saying I need to download the latest version of the user tool starter kit. Wich I did, and now I can start your editor.
I now have no time for experimenting with it, but I will soon and let you know if I have any problems with it.

And last but not least : I wish you a happy, healthy 2011 full of music!
Always look on the bright side of life!