JavaScript Timers object

These are just two short functions I wrote a while ago as improvements to the JavaScript setInterval() and setTimeout() timer functions.

Both functions take at minimum two arguments; the function to run and the interval/delay. They take additional optional arguments to specify an object to apply() the function to, and an array of arguments to pass to the function.

Timers.timeout() only runs once, but Timers.interval() will run forever unless you tell it not to. To do that, simply return false. It you return anything else, it will be passed as the first argument next time the function is run.

var Timers = {

interval: function(func, time, obj, args) {
    var r;
    var a = args ? args : [];
    var o = obj ? obj : null;
    var ifunc = function() {
        if(r === false) {
            return;
        }
        if(r !== undefined) {
            r = func.apply(o, [r].concat(a));
        } else {
            r = func.apply(o, a);
        }
        setTimeout(ifunc, time)
    ifunc();
},

timeout: function(func, time, obj, args) {
    var o = obj ? obj : null;
    if(args) {
        setTimeout(function() {
            func.apply(o, args);
        }, time);
    } else {
        setTimeout(function() {
            func.apply(o);
        }, time);
    }
}

};
Advertisements

pytextedit 0.1 fixed

This is the fixed pytextedit 0.1. I fixed opening/saving under Python 2, many thanks to Dale Everett for noticing that it didn’t work. I fixed a couple bugs I found as well:

  • Opening/saving now works with Python 2. Running it in Python 2 removes all Unicode support, however. (I’ll try to add at least some in later.)
  • Fixed bug where “Statistics” wouldn’t work if no text was selected.
  • Fixed bug where “Statistics” would report there being one less line than there actually was.
  • Line numbers now match up to the text. (Better than before, anyway. Still not perfect.)

Download.

And that’s why you test your code before you say it’s done…

pytextedit 0.1 is complete!

Right, so I just finished pytextedit 0.1. It should (hopefully) work now, with no major bugs or anything. However, I haven’t tested it much with Python 2, so I can only say it works well with Python 3.

pytextedit 0.1

Download

You can download it here, if you want. It’s currently source code only, but I’m working on creating both Linux and Windows executables.

Changelog

The biggest change for a user is probably the improved Options dialog. Additionally, it is now no longer necessary to edit the configuration file manually, as the new dialog also saves the settings.

Not all the important changes would be obvious to a user though. I cleaned up a lot of the code, as well as improved the comments.

Here are all of the changes:

  • Added .json to the filetypes list for JavaScript.
  • Removed “Options -> View Options”.
  • The background color of the toolbar is now set to ED_LN_BG as well.
  • Removed spellchecker.
  • Removed ED_CMD_INIT_DIR. Now ED_INIT_DIR is used for the command line as well.
  • opt_font_size() will now refuse to change the font size if it would be less than 1.
  • Added an option for .java files in the filetypes list.
  • Added a command in “Code -> Compile” for compiling Java files.
  • ED_INIT_DIR will now be set to “C:” for the HOME dircetory on Windows.
  • Created a new directory called “resources” for storing anything the application needs.
  • Moved “images” directory into “resources”.
  • Added a keybinding for F5 to file_reload().
  • Added “File -> Quick Open”.
  • There is now a LICENSE file.
  • There is now a README file.
  • Added “Code -> Remove Tags”.
  • Clicking on “Cancel” when saving a file when exiting will now not close the application anyway.
  • Line numbers can now be hidden.
  • Added improved “About” dialog.
  • Code menu can no longer be disabled.
  • Context menu can no longer be disabled.
  • Modified a few menu items in “File”.
  • Added “File -> Favorites” and “Options -> Edit Favorites”.
  • “Recently Opened” now saves across sessions.
  • Added key binding for “Favorites”.
  • Default font name is now “courier”.
  • Fixed how line numbers wouldn’t match up with text.
  • Changing the font name/size will change the font in the line numbers as well.
  • file_open() now closes files after it is done reading.
  • Window size and position is now saved across sessions.
  • Removed ED_WIN_SIZE.
  • Added a CSS entry in the filetypes list.
  • Removed all references to ED_TITLE, and removed that variable.
  • Renamed Textbox_LN to TextboxLN.
  • Added function internal_return_focus() for simplifying returning the focus to the text box.
  • Fixed bug where pressing Tab wouldn’t change gbl_text_modified.
  • Removed “import glob”. It wasn’t used by anything.
  • Removed gbl_browse_dlg and gbl_fullscreen. I honestly have no idea what they were for, as they weren’t used by anything.
  • Fixed bug where you couldn’t open a new file if current file was locked.
  • Removed “Search -> Move Insert To…”.
  • Removed tools_insert_pos() function, it wasn’t used anymore.
  • Simplified lots of the code. (Mostly removing unnecessary try-except blocks.)
  • Made some except blocks handle specific errors, rather then catching all.
  • Simplified the GUI code. There was an unnecessary frame.
  • Simplified edit_lock().
  • Removed “Edit -> Selection -> Move Selection”.
  • Merged file_save_as() into file_save().
  • When getting all text, the indexes “1.0” and “end-1c” are now always used. Before it was very inconsistent.
  • Same as above, but for deleting all text.
  • Fixed bug where “Revert to Last Save” would scroll to the bottom.
  • Fixed bug where “Revert to Last Save” would not mark the text as having no changes.
  • “Revert to Last Save” now clears the undo/redo stack.
  • Improved some of the code for loading a file from the command line. I had forgotten to update it as I did the other stuff. (Oops.)
  • Further improved comments.
  • Changed the default gbl_dlg_open_url to “http://”.
  • gbl_saved_data is now updated when the file is opened as well.
  • UI options now apply to scrollbars as well.
  • Rewrote check_modified().
  • Added function internal_text_modified() function for simplifying marking text as having changes.
  • Rewrote all functions to use internal_text_modified().
  • Simplified the code for opening from command line.
  • Rewrote file_exit() to use a Toplevel (again).
  • Removed “Font” toolbar button.
  • Added “Options” toolbar button.
  • Added the options dialog.
  • Removed “Options -> Font” and corrosponding function.
  • Fixed bug where “Indent All” and “Unindent All” would not work for the last line.
  • Simplified TextboxLN.

What’s next?

There are still a lot of features I want to add that will probably go into pytextedit 0.2:

  • Syntax highlighting. I already have the basics of how to do this, but figuring out all the regular expressions will take a while.
  • Multi-document interface. Again, I already know how I am basically going to do this. It might take some time to implement it, though, as there are many changes to make.
  • File browser. This will allow users to browse, organize, and otherwise manage their files without having to switch to another application.
  • Better support for opening files over HTTP. Biggest improvement will be added Unicode support.
  • Tons of other miscellaneous stuff.

Changes in pytextedit, Aug. 27

pytextedit 0.1 is almost complete, I just have to add the new Options dialog and set it up to save to the configuration files. Well, I say “just”, but it’s going to take a while…

Anyway, this is everything else I’ve done since my last post:

  • Fixed bug where “Revert to Last Save” would scroll to the bottom.
  • Fixed bug where “Revert to Last Save” would not mark the text as having no changes.
  • “Revert to Last Save” now clears the undo/redo stack.
  • Improved some of the code for loading a file from the command line. I had forgotten to update it as I did the other stuff. (Oops.)
  • Further improved comments.
  • Changed the default gbl_dlg_open_url to “http://”.
  • gbl_saved_data is now updated when the file is opened as well.
  • UI options now apply to scrollbars as well.
  • Rewrote check_modified().
  • Added function internal_text_modified() function for simplifying marking text as having changes.
  • Rewrote all functions to use internal_text_modified().
  • Simplified the code for opening from command line.
  • Rewrote file_exit() to use a Toplevel (again).

Changes in pytextedit, Aug. 24

I don’t have any main thing I’ve been working on today, so here’s a list of the changes in pytextedit since last post:

  • Window size and position is now saved across sessions.
  • Removed ED_WIN_SIZE.
  • Added a CSS entry in the filetypes list.
  • Removed all references to ED_TITLE, and removed that variable.
  • Renamed Textbox_LN to TextboxLN.
  • Added function internal_return_focus() for simplifying returning the focus to the text box.
  • Fixed bug where pressing Tab wouldn’t change gbl_text_modified.
  • Removed “import glob”. It wasn’t used by anything.
  • Removed gbl_browse_dlg and gbl_fullscreen. I honestly have no idea what they were for, as they weren’t used by anything.
  • Fixed bug where you couldn’t open a new file if current file was locked.
  • Removed “Search -> Move Insert To…”.
  • Removed tools_insert_pos() function, it wasn’t used anymore.
  • Simplified lots of the code. (Mostly removing unnecessary try-except blocks.)
  • Made some except blocks handle specific errors, rather then catching all.
  • Simplified the GUI code. There was an unnecessary frame.
  • Simplified edit_lock().
  • Removed “Edit -> Selection -> Move Selection”.
  • Merged file_save_as() into file_save().
  • When getting all text, the indexes “1.0” and “end-1c” are now always used. Before it was very inconsistent.
  • Same as above, but for deleting all text.

pytextedit: options dialog is complete!

I completed this a lot earlier than I though I would. Well, the UI is complete, anyway. The functionality still needs to be added.

Everything works exactly how I wanted it to, although the code is rather larger than what I’d prefer. It’s basically impossible to do much about that, though, as it’s mostly for the Entry and OptionMenu widgets.

If you’re interested you can try it out yourself.