acefael

.. on software

Follow me on GitHub

Faster Editing Magik in Emacs

19 Feb 2015

When editing Smallworld™ Magik source in Emacs, the tooling experience is usually very comprehensive. There are the common facilities provided by Emacs, such as tags tables, abbrevs, the keyboard navigation that is unmatched in todays development environments, to name a few. There is of course also the Smallworld™ class browser for those that need more interactivity.

Compared to other languages, Magik has a less regular syntax, and that makes navigation in the source less efficient at times. In particular, there is no way to move over blocks of code such as procs or methods. Gladly Emacs helps us with that. A block of code typically ends with a dollar sign on a line of it’s own. So we can use that to move forward or backwards between them. The snippets below hook to Alt+Up and Alt+Down (M-<up> and M-<down> in Emacs speek).

(defun acefael-magik-forward-$ ()
  "move forward until at ^$"
  (interactive)
  (let ((keep-going t))
    (while keep-going
      (forward-char)
      (if (or (eobp)
              (and (bolp)
                   (= 36 (following-char))))
          (setq keep-going nil)))))

(defun acefael-magik-backward-$ ()
  "move backward until at ^$"
  (interactive)
  (let ((keep-going t))
    (while keep-going
      (backward-char)
      (if (or (bobp)
              (and (bolp)
                   (= 36 (following-char))))
          (setq keep-going nil)))))

(defun acefael-magik-mode-hook ()
  (local-set-key (kbd "M-<down>") #'acefael-magik-forward-$)
  (local-set-key (kbd "M-<up>") #'acefael-magik-backward-$))

(add-hook 'magik-mode-hook #'acefael-magik-mode-hook)