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:
(interactive "nGrid size: "). The letternis one of a few code letters forinteractivethat instruct Emacs to prompt the user for a value. These letters allow you to specify a prompt string, as we've done here. Thisinteractivedeclaration 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access