From 36db1ea7bd3f0e8b199d1de66c4b86d0381a4c68 Mon Sep 17 00:00:00 2001 Message-Id: <36db1ea7bd3f0e8b199d1de66c4b86d0381a4c68.1717720259.git.mdw@distorted.org.uk> From: Mark Wooding Date: Tue, 2 Jun 2015 20:17:25 +0100 Subject: [PATCH] dot/emacs, el/dot-emacs.el: Support for Rust code. Organization: Straylight/Edgeware From: Mark Wooding --- dot/emacs | 9 +++++- el/dot-emacs.el | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/dot/emacs b/dot/emacs index 2128756..1550b13 100644 --- a/dot/emacs +++ b/dot/emacs @@ -93,6 +93,12 @@ (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) @@ -608,7 +614,7 @@ 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 @@ -686,6 +692,7 @@ (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) diff --git a/el/dot-emacs.el b/el/dot-emacs.el index 7eddea5..f8af576 100644 --- a/el/dot-emacs.el +++ b/el/dot-emacs.el @@ -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. -- [mdw]