Emacs Extensions for
GE Smallworld Magik Development

Screen-casts, tutorials, and productivity tools for Magik programmers who use Emacs. From tab-mode and code folding to the Magik Debugger and object inspector — explore features built by a developer, for developers.

Learn More
HydePark Consulting Screen-casts on YouTube RSS via FeedBurner

Nine screen-casts published between November 2010 and January 2011 — covering everything from ECB mode to the Tree Item GUI control. Read the story behind the project →

Get in Touch

Taming Magik Stack Traces Inside Emacs

Magik programmers who live inside the Smallworld ecosystem spend a large slice of their day chasing exceptions. A routine load on a sw_gis!rope_world or a sw_database!write_transaction can fan out through dozens of method calls, and the resulting backtrace is often longer than the method that triggered it. Reading the raw output in a terminal works, but only up to a point. Once a trace crosses twenty frames, the eye starts sliding off the interesting lines and the cursor drifts.

Emacs has long been the editor of choice for many Magik developers in Australia, partly because of its deep customisation and partly because the local Smallworld community grew up alongside GNU tools in the late nineties. Engineers at organisations such as Sydney Water, Melbourne Water, APA Group and AusNet Services have been refining their .emacs files for decades, sharing tricks at user group meetings over flat whites in the CBD. The result is a wealth of small Lisp snippets that turn Emacs into a surprisingly capable Magik front-end.

This piece pulls together those habits into a single workflow. It assumes you already run Magik inside an Emacs shell or a compile buffer, and that you would like stack traces to land somewhere you can actually read them. You will see how to capture output cleanly, colour frames by depth, hide the frames you never want to see again, and bounce from a frame to the source line in a couple of key presses.

The end goal is not a finished package but a set of patterns. You can copy what you need, drop the rest, and keep the recipe working when the Magik compiler or the Smallworld core throws a fresh surprise at you on a Monday morning in Brisbane or a Friday arvo in Perth.

Anatomy of a Magik stack trace

A Magik backtrace is a list of frames printed top-down, with the most recent call at the top. Each line typically shows the method slot name, the receiver class, the source file and a line number, alongside the arguments in shorthand. Long traces also include "Magik" frames from the engine itself, "RWO" frames from the object world, and "sw" frames from the Smallworld product modules. A trace from a real production fault at, say, a gas distribution GIS might show all three.

The order matters. The frame you want is usually the first one that sits outside Smallworld's own modules, because anything inside sw_core or sw_gis is often a wrapper that the application is meant to call into. Once you have identified your application's frame, the file and line that follow are the address you want to open. Anything above that frame is history, and anything below is plumbing.

One small detail trips up newcomers. Arguments are printed using Magik's _print representation, which is concise for built-in types and very long for collections, ropes and symbol tables. A single frame can wrap across two or three terminal lines, which is one reason the raw output feels impenetrable. Emacs will happily reflow it, but only if you let it.

The skill, then, is to teach Emacs which lines deserve attention and which should fall into the background. Faces, narrowing and a bit of regular expression muscle will do most of the work.

Capture the trace in a compilation buffer

Magik itself writes to *standard output*, which means whatever process filter Emacs attaches will receive the text. The simplest route is to run Magik through M-x compile with a command such as magik -f my_image -e proc() or through a dedicated REPL started with M-x magik-mode. Both end up with a buffer that Emacs treats like a compilation log, so next-error and previous-error are available immediately.

The trick is to keep the trace in its own buffer rather than letting it scroll past inside a shell. A short wrapper function that calls compilation-start with a sentinel and switches to the buffer in another window is usually enough. Developers in Adelaide who maintain distribution networks for SA Water have posted variants that tag the buffer name with a timestamp, so the morning's faults do not overwrite last night's run.

If you prefer the REPL approach, set compilation-read-command to nil and let the Magik process push its backtrace into the same buffer. Add compilation-auto-jump-to-first-error if you want the cursor to land on the first frame the moment a trace arrives. That single option has saved more time across Australian utility shops than any face customisation, because it removes the manual scroll to the relevant line.

One last habit worth keeping. Do not let the trace buffer grow without bound. A weekly reset, or a hook that kills the buffer when it reaches a few thousand lines, prevents Emacs from slowing down on a laptop that is already running a remote X session into a Smallworld server in Carlton or North Sydney.

Faces that make frames findable

Once a trace is captured, colour does the heavy lifting. Magik frames have a predictable shape, so a handful of font-lock rules can shade method names, file paths and line numbers in distinct colours. Many Magik programmers in Australia start with the magik-mode shipped on the Magik Emacs site, then add their own keywords for internal modules.

A practical recipe is to colour frames by depth. The first six frames get one face, the next six another, and anything beyond a configurable threshold gets a muted grey. Reading a long trace becomes a matter of skimming the bright bands. This works especially well in dimly lit control rooms where the GIS operators are watching telemetry from a substation in regional Victoria or a pumping station outside Darwin.

Faces also help separate the noise. Engine frames, which begin with _ or live under magik_kernel, can be dimmed. RWO frames can take a soft blue. Application frames stay at full brightness. The eye learns the palette quickly, and within a day the visual shorthand feels natural.

The configuration lives in a magik-stack-face minor mode. It activates only when the buffer name matches a pattern such as \\*trace\\* and it adds a single key binding for cycling the depth threshold. Once you have tuned it, you can read a sixty-frame trace the way you read a street directory in reverse.

Filtering the noise with occur and narrowing

Faces guide the eye, but they do not remove the lines. For a really long trace, you will want to hide frames that you have already investigated. Emacs offers two well-trodden tools here: occur and narrow-to-region. The first keeps the full buffer and shows a clickable summary. The second physically hides the lines you do not want to see.

M-x occur against a pattern such as sw_database! or rope! gives a one-line-per-match view, and you can jump to any match with C-c C-c. For a single fault, this is often the fastest path. You end up with a buffer of only the frames that touch the database, and the rest of the trace is one keystroke away if you need it back.

Narrowing is the better choice when you want a clean, printable view. Hide every engine frame with a quick keyboard macro, then narrow to the rest. The narrowed buffer behaves like a normal buffer, so you can save it, send it to a colleague, or attach it to a ticket without dragging along three pages of _self calls. Melbourne developers who support Jemena's gas network have a habit of mailing narrowed traces to vendors, which keeps the conversation focused on the actual fault.

For repeatable filters, wrap the narrowing in a small interactive command. Give it a name such as magik-trace-filter-application and bind it to a key. After the second or third fault, you will find yourself filtering reflexively, the way Australian drivers reflexively check the right lane before merging onto the Monash.

Jumping from a frame to the source

A stack trace is only useful if it points back to code you can open. Emacs already does the heavy lifting when file paths in the trace are absolute and the line number is in file:line:column form. The compile buffer understands this layout, and M-x next-error opens the file and drops the cursor on the line.

Magik traces usually include both the slot definition file and the source file, and the slot file is often the more useful of the two. A small defun that parses a frame, extracts the source path, and calls find-file directly saves a few seconds each jump. Over the course of a week, that adds up to the time it takes to walk from Flinders Street to the GPO and back, which is no small thing if you are debugging across time zones with a colleague in Perth.

The jump should also work with magik-goto-method, which is part of the standard Magik Emacs distribution. Bind it to a mouse click on a frame, or to RET when the cursor sits on a frame, and the editor will open the receiver's source method in the same window. Combined with the previous sections, this turns a printed trace into a navigable map of the failure.

Saving the recipes that work

The customisations above only earn their keep if they survive the next upgrade. Keep them in a dedicated magik-trace.el file that you load from your init file, and split the file into clearly named defcustom blocks. When a new Magik release changes the way traces are formatted, you will be glad to have a single file to edit rather than a tangle of hooks.

It also helps to keep a short text file of filter recipes alongside the Lisp. A line such as sw_database!write_transaction for the database team, rope! for the geometry team, and magik_kernel for engine-only investigations gives you a one-line summary you can paste into a chat window. Australian developers tend to share these recipes in stand-ups because the filter list is short enough to memorise, and nobody wants to ask the same question twice at the morning stand-up in Pyrmont.

The end state is a small, well-loved toolkit. The trace lands in a buffer, the buffer is filtered, the cursor lands on the frame, the file opens, and the developer is already typing the next fix.

If you work with Magik in Emacs and want a starting point for the Lisp snippets above, the Magik Emacs site hosts the magik-trace.el recipe along with a short screen-cast that walks through each section. Drop the file into your ~/.emacs.d, evaluate M-x magik-trace-mode, and the next backtrace you catch will already feel a touch easier to read.

Core Features

Tab Mode & ECB

Quick tab switching and Emacs Code Browsing mode for navigating Magik codebases efficiently.

Magik Smeller

Code analysis tool that helps identify potential issues in Magik source files.

Code Folding

Hide/Show mode for collapsing and expanding Magik code blocks to focus on what matters.

Visual Bookmarks

Quick visual bookmarks for jumping between key locations in your Smallworld session buffers.

Object Inspector

Inspect Magik objects and display them in an Emacs Deep Print buffer for detailed examination.

Magik Debugger

Set breakpoints and monitor slots and variables directly from within Emacs.

Development Tools

Direct links between Emacs and the Smallworld Development Tools application, including Click Monitor.

Screen-casts & Tutorials

Dark code editor window with syntax-highlighted Magik source code in muted blues and greys, conveying a focused development environment

Screen-cast 1: Tab Mode, ECB & More

Covers tab-mode, ECB, Magik Smeller, code folding, visual bookmarks, pragma toggling, moving code, external editor, and MS Explorer.

November 7, 2010
Split-pane Emacs interface with multiple buffers open, warm amber and navy tones against a dark background

Screen-cast 5: Object Inspection & Deep Print

Inspect a Magik object, prompt for an expression evaluated within a Smallworld session, and display results in a Deep Print buffer.

January 16, 2011
Debugging interface with breakpoint markers and variable watch panels in subdued teal and charcoal tones

Screen-cast 7: Magik Debugger

Useful tools for application developers: Object Inspector and Magik Debugger with breakpoints and slot/variable monitoring.

January 2011
Tree control GUI element with expandable branches rendered in clean greys and muted blues on a light background

Screen-cast 9: Tree Item GUI Control

Tree Item is a GUI control providing extensive facilities for displaying lists with rows, columns, trees, and in-place editing.

January 20, 2011