BufferStats Plugin v0.1

By Patrick Wright (jedit_devel@pdoubleya.com), July 2004

Contents



Introduction

The BufferStats plugin tracks open Buffers in JEdit as they are opened, saved, and made active.



What Information is Tracked?

Currently BufferStats tracks the following statistics about each Buffer:

The plugin can then provide the array of Buffers sorted by any of these stats, or any of these stats by Buffer, on request.

Back to top



Macros Included with the Plugin

The information itself is not that useful, however, the plugin ships with a handful of macros that use the plugin to determine if buffers should be automatically closed. For example, Close_Least_Activated.bsh closes the least-often activated buffers, until there are a max of 6 open (6 by default, can be changed at top of macro script).

The macros that ship with the plugin are as follows:

Macro Name Purpose Variables?
Close_Inactive_Buffers.bsh Closes Buffers that have not been changed or activated in a period of time. The "period of time" is hard-coded in this script. Dirty buffers are not closed. MAX_INACTIVE_SECONDS
Close_Least_Activated.bsh If the number of open buffers passes a fixed limit, least-activated buffers are closed; dirty buffers are not closed. MAX_BUFFER_COUNT
Close_Oldest_Activated.bsh If the number of open buffers passes a fixed limit, the buffers activated furthest in the past are closed; dirty buffers are not closed. MAX_BUFFER_COUNT
Log_Buffer_Stats.bsh Writes status information for all open buffers to JEdit Activity Log. If buffer was not being tracked previously, it will be listed, but stats will all be initialized to base values (usually 0)--typically this would only happen if the plugin was reloaded at runtime. None

BufferStats has no GUI and no actions--it loads on startup and remains resident. If you reload the buffer while JEdit is running, it should pick up and track any open buffers, however, stats will be reset at that point. Also, it will pick up buffers already open when JEdit is started, but note the stats are reset as of the time JEdit starts--times are not kept across sessions.

Back to top



Installation/Setup

If you want to use the macros included, copy them to your {JEdit install}/macros directory, or to the {JEdit properties}/macros directory. Once there, you may want to assign shortcut keys to them--I have control-W assigned to Close_Inactive_Buffers.bsh, for example.

As noted above, some of the macros have variables that control which buffers are closed. Currently, you have to edit the macro scripts as there are no option panes provided, and the variables are not stored as JEdit properties. The variables in question are listed above, and appear at the top of the script, below the first comment/license block.

Back to top



Auto-close Features

The macros don't support an auto-close feature (close Buffers without direct request). That is, you have to call/execute the macro you want to use, manually, to close any buffers. It is possible that you could do this in the background, automatically, with the ChronTab plugin, by calling your favorite macro on a scheduled basis (e.g. every 30 secs).

Alternately, the latest version of BufferLocal plugin has an auto-close feature with some options about what should be closed. I am using this approach, with macros, for flexibility.

Back to top



Philosophy: Why is this a separate Plugin? And why use Macros?

I started working on this plugin when a user wrote to the JEdit mailing list, asking how to easily close unused buffers once he had a lot of buffers open. My first attempt was just to write a script that checked the buffer's last modification time and dirty flag, but the only timestamps available where from the File for the Buffer itself. Specifically, I couldn't tell how often the Buffer itself had been used (made active), which was a drawback. If you opened a file just to look at the code (and not edit it), you wouldn't want it to auto-close just because the modification date was old.

So that script was limited. I suggested to the list that we add these stats (for activation) to the Buffer class, but realized that this would take time to do, as the 4.2 release is imminent, and there was at least one complaint to not add any more to the core JEdit code.

Another option was to add this to one of the several Buffer-management plugins already out there. However, there are several of them, and I didn't want to add the feature to each one, or to force people to use a specific Buffer manager just to get this feature.

I decided all I needed was some small statistical information about Buffer usage, and that this should be independent of Buffer management otherwise. By putting the auto-close logic into macros, I could more easily change the rules for closing them without recompiling and releasing a new plugin. Besides, others might have special for their own work patterns--for example, to never auto-close certain files, or always auto-close other files. Rather than making one super-powerful plugin with many options, it seemed most useful to have the plugin provide the data necessary to make the selection, and have the selection made in a macro.

The drawback of using macros right now is that it is more of a hassle to manage options for closing (max buffers open, etc.). Also, the macros don't directly support auto-closing, though you might be able to do this with the ChronTab plugin.

Back to top