Skip to content

Instantly share code, notes, and snippets.

@jclosure
Created September 16, 2020 08:55
Show Gist options
  • Save jclosure/8e73b3afa11c25c5dba3522ada530fd0 to your computer and use it in GitHub Desktop.
Save jclosure/8e73b3afa11c25c5dba3522ada530fd0 to your computer and use it in GitHub Desktop.
Create a derived major-mode in emacs for personal customization

I wrote in ~/.emacs.d/lisp/ros-cmake-mode.el:

(require 'cmake-mode)

(defun ros-cmake-indent ()
   ;; my code here...
  )

;;;###autoload
(define-derived-mode ros-cmake-mode cmake-mode "CMake[ROS]"
  "Major mode for editing ROS CMakeLists.txt files."

  ;; Setup indentation function.
  (set (make-local-variable 'indent-line-function) 'ros-cmake-indent))

(provide 'ros-cmake-mode)

Then in ~/.emacs.d/lisp/init.el (using use-package):

(use-package cmake-mode
  ;; major-mode for editing CMake sources
  :load-path "lisp/"
  :config
  (add-to-list 'auto-mode-alist '("\\.cmake\\'" . cmake-mode))
  (autoload 'cmake-mode "lisp/cmake-mode.el" t))

(use-package ros-cmake-mode
  ;; major-mode for editing CMakeLists.txt files with ROS-style indentation
  :load-path "lisp/"
  :config
  (add-to-list 'auto-mode-alist '("CMakeLists\\.txt\\'" . ros-cmake-mode))
  (autoload 'ros-cmake-mode "lisp/ros-cmake-mode.el" t))

When I open a CMakeLists.txt file, the major mode CMake[ROS] is used by default. In case I want to switch to CMake major mode (as provided by cmake-mode.el), I can type M-x cmake-mode RET.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment