Skip to content

got-dna/sgf-mode

Repository files navigation

sgf-mode

An Emacs package for viewing, editing, and analyzing Go games in Smart Game Format (SGF). It provides interactive SVG board rendering, game tree visualization, KataGo AI integration, and Org-mode Babel support for embedding SGF games in documents.

Requirements

  • Emacs 30.1 or later
  • librsvg (for SVG rendering)
  • KataGo (optional, for AI analysis)

Installation

  • Add to load-path:
    (add-to-list 'load-path "~/git/sgf-mode")
    (require 'sgf-mode)
        
  • Or install with use-package:
    (use-package sgf-mode
      :vc (:url https://github.com/got-dna/sgf-mode.git)
      :bind
      (:map sgf-mode-display-map
            ("f" . sgf-forward-move)) ; example of keybinding setup
      :config
      ;; make sgf-mode-map bindings available in org-mode
      (set-keymap-parent org-mode-map sgf-mode-map)
      (org-babel-do-load-languages 'org-babel-load-languages
                                   '((sgf . t)))
      (setq org-babel-default-header-args:sgf
            '(;(:show-hints . "nil")
              (:results . "none")))
    
      (setq katago-model "katago.bin.gz"
            katago-analysis-config "analysis_example.cfg"
            katago-gtp-config "gtp_example.cfg"))
        

Quick Start

  1. Open an .sgf file in Emacs. sgf-mode activates automatically.
  2. Press C-c C-c to display the SVG board.
  3. Use f / b (or arrow keys) to navigate moves.
  4. Click on the board to play stones.

Usage

SGF Files

Opening an .sgf file activates sgf-mode. Press C-c C-c to create an overlay that renders the game as an interactive SVG board. A game tree buffer (*SGF TREE*) also opens to show all variations.

KeyCommandDescription
C-c C-csgf-toggle-game-displayToggle SVG board display
C-c s isgf-init-new-gameInitialize a new game
C-c s rsgf-remove-game-displayRemove the SVG board display

Board Display

Once the SVG board is displayed, place your cursor on it to activate the board keybindings (sgf-mode-display-map). These keys are only active when the cursor is on the overlay.

Navigation

KeyCommandDescription
f / <right>sgf-forward-moveNext move (prompts at forks)
b / <left>sgf-backward-movePrevious move
M-f / <down>sgf-forward-forkJump to next fork
M-b / <up>sgf-backward-forkJump to previous fork
asgf-first-moveJump to first move
esgf-last-moveJump to last move (first branch at forks)
jsgf-jump-movesJump forward/backward N moves
tsgf-traverseTraverse using a path specification
rsgf-back-to-gameReturn to main variation

View

KeyCommandDescription
csgf-show-commentDisplay comment for current move
psgf-show-pathShow path to current position
zsgf-export-imageExport board to SVG file or display in buffer
gsgf-graph-hvToggle horizontal/vertical game tree layout
oSwitch to game tree buffer
+image-increase-sizeZoom in
-image-decrease-sizeZoom out

Display Toggles (s prefix)

KeyCommandDescription
s nsgf-toggle-numbersToggle move numbers on stones
s msgf-toggle-marksToggle marks
s hsgf-toggle-hintsToggle next move hints
s ksgf-toggle-koToggle KO position mark
s asgf-toggle-katagoToggle KataGo analysis overlay
s isgf-toggle-katago-metricsToggle KataGo metrics display
s wsgf-toggle-serialize-katagoToggle saving KataGo data to SGF
s ssgf-toggle-new-moveToggle allowing new moves (exam mode)

Move Editing (m prefix)

KeyCommandDescription
m psgf-passPlay a pass move
m rsgf-make-rootSet current node as game root
m ksgf-prune-inclusiveDelete current node and children
m Ksgf-pruneDelete all children
m csgf-edit-commentEdit/add comment
m nsgf-edit-move-numberSet move number
m asgf-edit-mark-triangleAdd/delete triangle mark
m dsgf-edit-mark-squareAdd/delete square mark
m osgf-edit-mark-circleAdd/delete circle mark
m xsgf-edit-mark-crossAdd/delete cross mark
m lsgf-edit-mark-labelAdd/delete text label
m -sgf-delete-markRemove mark from position
m bsgf-edit-setup-black-stoneAdd/remove black setup stone
m wsgf-edit-setup-white-stoneAdd/remove white setup stone
m isgf-edit-game-infoEdit game info (players, result, etc.)
m hsgf-edit-annotationAdd move annotation (BM/DO/IT/TE)
m msgf-merge-branchesMerge branches with same moves
m ssgf-swap-branchesReorder branches
m vsgf-remove-variationsDelete variations before current
m esgf-serialize-gameUpdate buffer with game state
m Esgf-write-gameWrite game to a new file

Mouse

ActionDescription
Left click on boardPlay stone or navigate to existing move
Right click on stoneContext menu
M-clickShow KataGo principal variation for a candidate move
C-M-clickExpand KataGo recommended moves onto the board

Game Tree

The game tree buffer (*SGF TREE*) shows the structure of all game variations. See sgf-graph-mode-map for all commands.

KeyCommandDescription
fsgf-graph-forward-nodeMove to next node
bsgf-graph-backward-nodeMove to previous node
Fsgf-graph-forward-commentJump to next node with comment
Bsgf-graph-backward-commentJump to previous node with comment
psgf-graph-pos-to-pathConvert cursor position to path
Psgf-graph-path-to-posConvert path to cursor position
ssgf-graph-sync-gameSync game display with tree position

KataGo Integration

sgf-mode integrates with KataGo for AI analysis. Analysis runs asynchronously without blocking the UI.

Setup

(setq katago-exe "katago"                        ; path to executable
      katago-model "/path/to/model.bin.gz"       ; neural network model
      katago-analysis-config "/path/to/analysis.cfg"
      katago-gtp-config "/path/to/gtp.cfg")

Keybindings

KeyCommandDescription
ksgf-katago-analyze-next-stepAnalyze the next move
Ksgf-katago-analyzeAnalyze entire game

Analysis results are displayed as overlays on the board showing win rate, score lead, and candidate moves. Use s a to toggle the analysis display.

Org-mode Babel

sgf-mode supports SGF code blocks in Org-mode. See examples in tests/test.org.

Header Arguments

ArgumentValuesDescription
:show-numberst or nilDisplay move numbers on stones
:show-hintst or nilDisplay next move hints
:show-markst or nilDisplay marks on board
:show-kot or nilDisplay KO position
:show-katagot or nilDisplay KataGo analysis
:new-movet or nilAllow playing new moves
:suicide-movet or nilAllow suicide moves
:traverse-patht / int / listInitial position to navigate to

Workflow

  • C-c ' to edit the SGF block in a separate buffer with full sgf-mode support (recommended).
  • C-c C-c to display the game board in-place.
  • C-c C-v b to display all SGF blocks in the buffer.
  • C-c C-v t to tangle SGF blocks to files. Use C-u C-c C-v t for only the current block.

Customization

Display Options

VariableDefaultDescription
sgf-show-numberstShow move numbers on stones
sgf-show-hintstShow hint marks for next move(s)
sgf-show-markstShow marks on the board
sgf-show-kotShow KO position
sgf-show-katagotShow KataGo analysis

Game Behavior

VariableDefaultDescription
sgf-new-movenilAllow new moves (nil = self-exam mode)
sgf-suicide-movenilAllow suicide moves
sgf-traverse-pathnilDefault path to traverse on display init

SVG Rendering

VariableDefaultDescription
sgf-svg-size28Grid cell size in pixels
sgf-svg-stone-size0.48Stone radius (ratio to cell size)
sgf-svg-font-family"Arial"Font family for numbers and labels

Alternatives

About

emacs SGF mode: play go in emacs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •