O'Reilly logo

Writing GNU Emacs Extensions by Bob Glickstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required