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

Practical Tips for Writing Magik Macros in Emacs

Magik development becomes considerably faster when repetitive editor work is turned into a reliable macro. In an Emacs environment, that may mean a keyboard macro for reshaping methods, an Emacs Lisp command that inserts Magik templates, or a small tool that inspects the current method and generates related code. The best approach depends on whether the repetition is textual, structural, or tied to the Magik runtime.

For Australian teams working on Smallworld applications, useful macros can remove friction from everyday tasks such as navigating long method files, preparing database queries, creating collection-analysis code, or adding consistent debugging statements. A well-designed command should save keystrokes without hiding what it changes, particularly when several developers share code across Sydney, Melbourne, Brisbane, Perth, or remote regional offices.

Decide What the Macro Should Automate

Start by identifying the repeated action precisely. A keyboard macro is suitable when the task follows a stable sequence, such as moving to the next method, inserting a standard declaration, or changing indentation across a selected region. It records editor commands rather than understanding Magik syntax, so it is quick to create but vulnerable to changes in whitespace and buffer contents.

An Emacs Lisp macro or interactive command is more appropriate when the operation needs conditions. For example, a command could check whether point is inside a _proc definition, extract the current method name, and insert a matching test or documentation block. This extra structure makes the command easier to reuse than a recorded sequence of cursor movements.

Keep the boundary small. A command called magik-insert-debug-trace is easier to trust than one that claims to “prepare a complete method”. Small commands can be composed, tested, and replaced independently. They also make a shared configuration less intimidating for a team that is gradually adopting Emacs.

Before recording keystrokes, write down the expected input and output. If the input is a method such as _method parcel.update_status(), decide whether the result should add a trace before the first statement, after the declaration, or around a selected expression. Clear boundaries prevent a macro from silently modifying the wrong method.

Use Magik Syntax As Your Reference Point

Magik code has conventions that a generic text macro cannot infer. A generated method should respect _proc and _endproc boundaries, symbol notation, method naming, collection operations, and the project’s indentation style. When inserting a template, include the exact punctuation and spacing already used by the codebase rather than relying on Emacs’ default language settings.

For instance, a simple template can be generated from an interactive Lisp function:

(defun magik-insert-method-template (name)
  "Insert a basic Magik method named NAME."
  (interactive "sMethod name: ")
  (insert (format "_method %s()\n  # Implementation\n_endmethod" name)))

The exact terminator and declaration form should match the Magik mode and Smallworld version used by the project. The important principle is to centralise the template instead of copying it into several keyboard macros. When a coding standard changes, one definition can be updated.

Avoid generating large blocks of opaque code. A macro that inserts a short method skeleton is helpful; one that creates a hundred lines of nested conditionals can make review difficult. Generated code remains production code, so developers should be able to read it without first learning the implementation of the editor command.

A macro that creates collection or database-analysis snippets should also make types and assumptions visible. Insert a clear variable name, a placeholder query, and a comment explaining the expected collection contents. This is safer than producing compact code that appears general but only works for one Smallworld dataset.

Make Cursor Placement Predictable

Cursor placement is one of the most important details in an editor macro. After inserting a method skeleton, point should normally land where the developer will type first. In Emacs Lisp, use save-excursion when the command needs to inspect or modify text without leaving point in an unexpected location, and use forward-line, search-forward, or markers when the destination is deliberate.

Recorded keyboard macros should finish in a useful state as well. If a macro adds a debugging call, leave point after the inserted expression or at the next statement. This allows the next command to continue naturally. A macro that completes its text correctly but leaves the cursor at the beginning of the file will quickly become unpopular.

Prefer searches for stable Magik tokens over fixed numbers of cursor movements. Searching for _endproc, a method marker, or a distinctive comment survives modest formatting changes. Fixed movement counts can fail when a method gains an extra comment, a wrapped expression, or a blank line.

When a macro acts on a selected region, preserve the region’s meaning. Check whether the region is active, narrow changes to the selected text, and avoid replacing text outside it. A helpful command can signal an error with user-error rather than proceeding when the expected method boundary is missing.

Add Safety Checks Before Editing

The fastest macro is not useful if it damages a source file. Before changing text, validate the context. A Magik-focused command might confirm that the current buffer uses the expected major mode, that point lies inside a method, and that the target text has not already been inserted.

For repeated instrumentation, an idempotent command is especially valuable. If the debug statement already exists, the command should do nothing or report that it is present. This avoids duplicate logging when a macro is run twice, which is easy to do during a busy maintenance session.

Use undo-friendly operations. A single command should generally produce a single logical undo step, allowing a developer to reverse the complete transformation without repeatedly pressing C-/. Emacs functions such as atomic-change-group can group edits, while a recorded keyboard macro naturally reflects the sequence of operations it performs.

Be careful with automatic evaluation. A macro should insert or transform source unless there is a compelling reason to execute Magik code immediately. Runtime execution can alter databases, open files, or create side effects that are difficult to diagnose from an editor command. This distinction matters when working with operational utility networks or council data.

Australian organisations should also consider privacy obligations when adding diagnostic output. If a macro inserts object inspection or request logging, it should avoid exposing personal information in shared logs and development buffers. The Privacy Act 1988 and an organisation’s internal data-handling rules may apply even when the code is being tested locally.

Combine Keyboard Macros With Emacs Lisp

Keyboard macros remain excellent for discovering a workflow. Record the sequence while performing a task, replay it on a few similar methods, and observe where it breaks. If the operation proves useful, convert the idea into a named Emacs Lisp command with clear inputs and safety checks.

Emacs provides convenient tools for this progression. kmacro-start-macro-or-insert-counter starts recording, while kmacro-end-or-call-macro stops or replays the current macro. A recorded macro can be named and saved in the init file, but it should be reviewed rather than copied blindly. Replace fragile cursor motion with searches and add a descriptive docstring.

For Magik teams, useful commands often include “insert method header”, “jump to matching method end”, “wrap region in a collection iteration”, and “insert an object inspection form”. A command can also call existing Magik Emacs features for symbol navigation, code folding, or debugging instead of reimplementing them.

Keep local configuration portable. Put shared commands in a project file or a small package, and keep machine-specific paths out of the implementation. Australian teams working across AEST, ACST, and AWST benefit from predictable configuration that behaves the same way for a developer in Perth and a colleague in Melbourne. Version-control the Lisp source and document the required Emacs and Magik mode versions.

A generated test or example should remain understandable outside the editor. For instance, if a macro inserts symbol-handling fixtures, its output should explain the expected values and edge cases. A concise wild symbols guide can serve as an example of why terminology and symbol behaviour need to be made explicit when documenting generated examples, even when the production domain is entirely different.

Test Macros Against Real Project Code

Test an editor macro on a scratch buffer first, then on a copy of representative project files. Include short and long methods, comments, nested conditionals, strings containing Magik keywords, and code with unusual indentation. A search-based command may behave correctly on a simple method while matching the wrong token in a complex one.

Check the generated text with the same tools used for ordinary Magik code. Run syntax checks, load the relevant class or method in a safe development environment, and inspect warnings. If the command creates database-analysis code, test it against empty collections, unexpected values, and large result sets. A macro that saves typing but creates inefficient iteration can make a production issue harder to find.

Use examples from the local business context. Smallworld installations in Australia may support electricity, water, transport, mining, or local government systems, each with different object models and data volumes. A macro designed around a tiny demonstration collection may need safeguards before it is used against a metropolitan utility dataset in Sydney or Melbourne.

Treat the macro as maintained software. Give it a name, a docstring, a small test fixture, and a note describing known limitations. If the team uses a package manager or a shared Emacs configuration, add the command through the same review process as other developer tooling. This keeps a clever personal shortcut from becoming an undocumented dependency.

Keep Generated Code Easy to Review

Readable output is the final measure of a useful Magik macro. Follow the project’s naming conventions, preserve indentation, and avoid unnecessary comments that merely describe obvious syntax. If the macro inserts a placeholder, make the placeholder unmistakable and easy to search for before code is committed.

Provide feedback after the command runs. A short message such as “Inserted method template for parcel.refresh” confirms what happened without interrupting the workflow. If no suitable method is found, explain the reason and leave the buffer unchanged. Silent failure is particularly costly in macros that operate across many files.

Document key bindings near the command definition and choose bindings that do not conflict with standard Emacs navigation. Teams may use different keyboards and terminal setups, so a named command available through M-x is a useful fallback. A screen-cast or short internal note can demonstrate the intended workflow without forcing every developer to inspect Lisp code.

Review macro changes with the same care given to Magik source changes. Ask whether the command is deterministic, whether it can be undone cleanly, and whether its output remains valid after a Magik mode upgrade. In a consulting environment, this discipline also makes it easier to transfer tooling between clients without carrying over assumptions about one site’s coding style.

Build a small library of dependable commands rather than a collection of clever tricks. Start with the repetitions that occur every day, measure whether they genuinely reduce editing time, and remove commands that create more confusion than value. A focused set of macros can make method navigation, debugging, object inspection, and collection analysis feel like a coherent part of Emacs rather than a series of unrelated shortcuts.

Add your most reliable Magik commands to the team’s version-controlled Emacs configuration, document their expected context, and test them against a safe copy of the project. That small investment turns repeated editor work into maintainable tooling while keeping generated code visible, reversible, and ready for review.

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