Derived Modes

We've now satisfied all the requirements for Quip mode save one: "It should work by and large the same way Text mode works." One way to achieve this is actually to call text-mode as part of initializing Quip mode; then perform whatever specialization is required by Quip mode. In conjunction with calling text-mode, we'd create quip-mode-map not from scratch with make-sparse-keymap, but as a copy of text-mode-map using copy-keymap:

(defvar quip-mode-map nil
  "Keymap for Quip major mode.")
(if quip-mode-map
    nil
  (setq quip-mode-map (copy-keymap text-mode-map))
  (define-key quip-mode-map "\C-x[" 'backward-quip)
  (define-key quip-mode-map "\C-x]" 'forward-quip)
  (define-key quip-mode-map "\C-xnq" 'narrow-to-quip)
  (define-key quip-mode-map "\C-cw" 'what-quip))
(defun quip-mode ()
  "Major mode for editing Quip files.
Special commands:
\\{quip-mode-map}"
  (interactive)
  (kill-all-local-variables)
  (text-mode)                                  ;first, set things up for Text mode
  (setq major-mode 'quip-mode)                 ;now, specialize for Quip mode
  (setq mode-name "Quip")
  (use-local-map quip-mode-map)
  (make-local-variable 'paragraph-separate)
  (make-local-variable 'paragraph-start)
  (make-local-variable 'page-delimiter)
  (setq paragraph-start "%%\\|[ \t\n\^L]")
  (setq paragraph-separate "%%$\\|[ \t\^L]*\$")
  (setq page-delimiter "^%%$")
  (run-hooks 'quip-mode-hook))
(provide 'quip)

For closer conformance with Text mode, we should clone text-mode-syntax-table too (using copy-syntax-table), not just text-mode-map. And there's also text-mode-abbrev-table ...

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.