chiark / gitweb /
dot/emacs, el/dot-emacs.el: Support for Rust code.
authorMark Wooding <mwooding@good.com>
Tue, 2 Jun 2015 19:17:25 +0000 (20:17 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 3 Jun 2015 09:31:56 +0000 (10:31 +0100)
dot/emacs
el/dot-emacs.el

index 2128756beb64e8c69775d537f59f0e6516e270b9..1550b13377f845311c154cf11e45d752a62e3df8 100644 (file)
--- a/dot/emacs
+++ b/dot/emacs
 (autoload 'mdw-multiple-cursors-keymap "mdw-multiple-cursors.el"
   "A keymap for Magnar Sveen's awesome multiple-cursors." nil 'keymap)
 
+;; Rust mode.
+
+(setq load-path (nconc load-path (list "~/lib/emacs/rust-mode/")))
+(autoload 'rust-mode "rust-mode" nil t)
+(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))
+
 ;; Temporary directory handling.
 
 (defun mdw-check-dir-exists (dir)
        tcl-mode-hook go-mode-hook js-mode-hook javascript-mode-hook
        conf-mode-hook m4-mode-hook autoconf-mode-hook autotest-mode-hook
        a68-mode-hook a68-mode-hooks asm-mode-hook fsharp-mode-hook
-       scala-mode-hook TeX-mode-hook LaTeX-mode-hook
+       scala-mode-hook rust-mode-hook TeX-mode-hook LaTeX-mode-hook
        TeXinfo-mode-hook tex-mode-hook latex-mode-hook
        texinfo-mode-hook emacs-lisp-mode-hook scheme-mode-hook
        lisp-mode-hook lisp-interaction-mode-hook makefile-mode-hook
   (add-hook 'linux-c-mode-hook #'(lambda () (setq c-basic-offset 8)))
   (add-hook 'asm-mode-hook 'mdw-fontify-asm t)
   (add-hook 'go-mode-hook 'mdw-fontify-go t)
+  (add-hook 'rust-mode-hook 'mdw-fontify-rust t)
 
   (add-hook 'icon-mode-hook 'mdw-fontify-icon t)
 
index 7eddea546b9d39ba4519eca4cb7e6b3d82ca025e..f8af576e737c066d7eeea0954565888b7cdaa0b3 100644 (file)
@@ -1978,6 +1978,84 @@ (defun mdw-fontify-go ()
 
   (mdw-post-config-mode-hack))
 
+;;;--------------------------------------------------------------------------
+;;; Rust programming configuration.
+
+(setq-default rust-indent-offset 2)
+
+(defun mdw-self-insert-and-indent (count)
+  (interactive "p")
+  (self-insert-command count)
+  (indent-according-to-mode))
+
+(defun mdw-fontify-rust ()
+
+  ;; Hack syntax categories.
+  (modify-syntax-entry ?= ".")
+
+  ;; Fontify keywords and things.
+  (make-local-variable 'font-lock-keywords)
+  (let ((rust-keywords
+        (mdw-regexps "abstract" "alignof" "as"
+                     "become" "box" "break"
+                     "const" "continue" "create"
+                     "do"
+                     "else" "enum" "extern"
+                     "false" "final" "fn" "for"
+                     "if" "impl" "in"
+                     "let" "loop"
+                     "macro" "match" "mod" "move" "mut"
+                     "offsetof" "override"
+                     "priv" "pub" "pure"
+                     "ref" "return"
+                     "self" "sizeof" "static" "struct" "super"
+                     "true" "trait" "type" "typeof"
+                     "unsafe" "unsized" "use"
+                     "virtual"
+                     "where" "while"
+                     "yield"))
+       (rust-builtins
+        (mdw-regexps "array" "pointer" "slice" "tuple"
+                     "bool" "true" "false"
+                     "f32" "f64"
+                     "i8" "i16" "i32" "i64" "isize"
+                     "u8" "u16" "u32" "u64" "usize"
+                     "char" "str")))
+    (setq font-lock-keywords
+         (list
+
+          ;; Handle the keywords defined above.
+          (list (concat "\\<\\(" rust-keywords "\\)\\>")
+                '(0 font-lock-keyword-face))
+          (list (concat "\\<\\(" rust-builtins "\\)\\>")
+                '(0 font-lock-variable-name-face))
+
+          ;; Handle numbers too.
+          (list (concat "\\<\\("
+                              "[0-9][0-9_]*"
+                              "\\(" "\\(\\.[0-9_]+\\)?[eE][-+]?[0-9_]+"
+                              "\\|" "\\.[0-9_]+"
+                              "\\)"
+                              "\\(f32\\|f64\\)?"
+                        "\\|" "\\(" "[0-9][0-9_]*"
+                              "\\|" "0x[0-9a-fA-F_]+"
+                              "\\|" "0o[0-7_]+"
+                              "\\|" "0b[01_]+"
+                              "\\)"
+                              "\\([ui]\\(8\\|16\\|32\\|64\\|s\\|size\\)\\)?"
+                        "\\)\\>")
+                '(0 mdw-number-face))
+
+          ;; And anything else is punctuation.
+          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
+                '(0 mdw-punct-face)))))
+
+  ;; Hack key bindings.
+  (local-set-key [?}] 'mdw-self-insert-and-indent)
+
+  ;; Final hacking.
+  (mdw-post-config-mode-hack))
+
 ;;;--------------------------------------------------------------------------
 ;;; Awk programming configuration.