Setting Up the Mode

There are two circumstances under which a user expects to enter Crossword mode. One is when visiting a file that contains a crossword grid from an earlier session. Another is when creating a brand-new crossword grid.

Creating a brand-new crossword grid requires creating an empty buffer and filling it in using crossword-insert-grid. The act of entering a major mode shouldn't change buffers or alter a buffer's contents, so crossword-mode will only be for entering Crossword mode in a buffer already containing a crossword grid. We'll devise a separate command, crossword, for creating a grid from scratch.

Here's a start at defining crossword:

(defun crossword (size)
  "Create a new buffer with an empty crossword grid."
  (interactive "nGrid size: ")
  (let* ((grid (make-crossword size))
         (buffer (generate-new-buffer "*Crossword*")))
    (switch-to-buffer buffer)
    (crossword-insert-grid grid)
    (crossword-place-cursor 0 0)      ;start in upper-left corner
    ...))

We'll leave this function unfinished for now, but before we move on, let's note some interesting things about this function:

  1. (interactive "nGrid size: "). The letter n is one of a few code letters for interactive that instruct Emacs to prompt the user for a value. These letters allow you to specify a prompt string, as we've done here. This interactive declaration means, "Prompt the user with the string "Grid size: ", and read a number in response."

    What if this command took two arguments, a number and, say, a string? What would the ...

Get Writing GNU Emacs Extensions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.