chiark / gitweb /
dot/emacs, el/dot-emacs.el: Support for Rust code.
[profile] / el / dot-emacs.el
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.