auto merge of #13432 : ruediger/rust/rustmode, r=nikomatsakis
* Use `setq-local` instead of `(set (make-local-variable 'var) value)`. Provides a version for older Emacsen. * Remove use of `cl.el`. * Use \' in file regexp instead of line end match $. * Use type for `defcustom` and add parent group.
This commit is contained in:
commit
8dc935e42c
@ -3,17 +3,29 @@
|
|||||||
;; Version: 0.2.0
|
;; Version: 0.2.0
|
||||||
;; Author: Mozilla
|
;; Author: Mozilla
|
||||||
;; Url: https://github.com/mozilla/rust
|
;; Url: https://github.com/mozilla/rust
|
||||||
|
;; Keywords: languages
|
||||||
|
|
||||||
|
;;; Commentary:
|
||||||
|
;;
|
||||||
|
|
||||||
|
;;; Code:
|
||||||
|
|
||||||
(eval-when-compile (require 'cl))
|
|
||||||
(eval-when-compile (require 'misc))
|
(eval-when-compile (require 'misc))
|
||||||
|
|
||||||
|
;; for GNU Emacs < 24.3
|
||||||
|
(eval-when-compile
|
||||||
|
(unless (fboundp 'setq-local)
|
||||||
|
(defmacro setq-local (var val)
|
||||||
|
"Set variable VAR to value VAL in current buffer."
|
||||||
|
(list 'set (list 'make-local-variable (list 'quote var)) val))))
|
||||||
|
|
||||||
;; Syntax definitions and helpers
|
;; Syntax definitions and helpers
|
||||||
(defvar rust-mode-syntax-table
|
(defvar rust-mode-syntax-table
|
||||||
(let ((table (make-syntax-table)))
|
(let ((table (make-syntax-table)))
|
||||||
|
|
||||||
;; Operators
|
;; Operators
|
||||||
(loop for i in '(?+ ?- ?* ?/ ?& ?| ?^ ?! ?< ?> ?~ ?@)
|
(dolist (i '(?+ ?- ?* ?/ ?& ?| ?^ ?! ?< ?> ?~ ?@))
|
||||||
do (modify-syntax-entry i "." table))
|
(modify-syntax-entry i "." table))
|
||||||
|
|
||||||
;; Strings
|
;; Strings
|
||||||
(modify-syntax-entry ?\" "\"" table)
|
(modify-syntax-entry ?\" "\"" table)
|
||||||
@ -30,10 +42,14 @@
|
|||||||
|
|
||||||
table))
|
table))
|
||||||
|
|
||||||
(defgroup rust-mode nil "Support for Rust code.")
|
(defgroup rust-mode nil
|
||||||
|
"Support for Rust code."
|
||||||
|
:link '(url-link "http://www.rust-lang.org/")
|
||||||
|
:group 'languages)
|
||||||
|
|
||||||
(defcustom rust-indent-offset 4
|
(defcustom rust-indent-offset 4
|
||||||
"*Indent Rust code by this number of spaces."
|
"Indent Rust code by this number of spaces."
|
||||||
|
:type 'integer
|
||||||
:group 'rust-mode)
|
:group 'rust-mode)
|
||||||
|
|
||||||
(defun rust-paren-level () (nth 0 (syntax-ppss)))
|
(defun rust-paren-level () (nth 0 (syntax-ppss)))
|
||||||
@ -226,17 +242,16 @@
|
|||||||
)
|
)
|
||||||
|
|
||||||
;; Item definitions
|
;; Item definitions
|
||||||
(loop for (item . face) in
|
(mapcar #'(lambda (x)
|
||||||
|
(list (rust-re-item-def (car x))
|
||||||
'(("enum" . font-lock-type-face)
|
1 (cdr x)))
|
||||||
("struct" . font-lock-type-face)
|
'(("enum" . font-lock-type-face)
|
||||||
("type" . font-lock-type-face)
|
("struct" . font-lock-type-face)
|
||||||
("mod" . font-lock-type-face)
|
("type" . font-lock-type-face)
|
||||||
("use" . font-lock-type-face)
|
("mod" . font-lock-type-face)
|
||||||
("fn" . font-lock-function-name-face)
|
("use" . font-lock-type-face)
|
||||||
("static" . font-lock-constant-face))
|
("fn" . font-lock-function-name-face)
|
||||||
|
("static" . font-lock-constant-face)))))
|
||||||
collect `(,(rust-re-item-def item) 1 ,face))))
|
|
||||||
|
|
||||||
(defun rust-fill-prefix-for-comment-start (line-start)
|
(defun rust-fill-prefix-for-comment-start (line-start)
|
||||||
"Determine what to use for `fill-prefix' based on what is at the beginning of a line."
|
"Determine what to use for `fill-prefix' based on what is at the beginning of a line."
|
||||||
@ -350,17 +365,17 @@
|
|||||||
|
|
||||||
;;; Imenu support
|
;;; Imenu support
|
||||||
(defvar rust-imenu-generic-expression
|
(defvar rust-imenu-generic-expression
|
||||||
(append (loop for item in
|
(append (mapcar #'(lambda (x)
|
||||||
'("enum" "struct" "type" "mod" "fn" "trait")
|
(list nil (rust-re-item-def x) 1))
|
||||||
collect `(nil ,(rust-re-item-def item) 1))
|
'("enum" "struct" "type" "mod" "fn" "trait"))
|
||||||
`(("Impl" ,(rust-re-item-def "impl") 1)))
|
`(("Impl" ,(rust-re-item-def "impl") 1)))
|
||||||
"Value for `imenu-generic-expression' in Rust mode.
|
"Value for `imenu-generic-expression' in Rust mode.
|
||||||
|
|
||||||
Create a flat index of the item definitions in a Rust file.
|
Create a flat index of the item definitions in a Rust file.
|
||||||
|
|
||||||
Imenu will show all the enums, structs, etc. at the same level.
|
Imenu will show all the enums, structs, etc. at the same level.
|
||||||
Implementations will be shown under the `Impl` subheading.
|
Implementations will be shown under the `Impl` subheading. Use
|
||||||
Use idomenu (imenu with ido-mode) for best mileage.")
|
idomenu (imenu with `ido-mode') for best mileage.")
|
||||||
|
|
||||||
;;; Defun Motions
|
;;; Defun Motions
|
||||||
|
|
||||||
@ -369,8 +384,7 @@ Use idomenu (imenu with ido-mode) for best mileage.")
|
|||||||
(concat "^\\s-*\\(?:priv\\|pub\\)?\\s-*"
|
(concat "^\\s-*\\(?:priv\\|pub\\)?\\s-*"
|
||||||
(regexp-opt
|
(regexp-opt
|
||||||
'("enum" "struct" "type" "mod" "use" "fn" "static" "impl"
|
'("enum" "struct" "type" "mod" "use" "fn" "static" "impl"
|
||||||
"extern" "impl" "static" "trait"
|
"extern" "impl" "static" "trait"))))
|
||||||
))))
|
|
||||||
|
|
||||||
(defun rust-beginning-of-defun (&optional arg)
|
(defun rust-beginning-of-defun (&optional arg)
|
||||||
"Move backward to the beginning of the current defun.
|
"Move backward to the beginning of the current defun.
|
||||||
@ -411,43 +425,36 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
|
|||||||
(define-derived-mode rust-mode rust-parent-mode "Rust"
|
(define-derived-mode rust-mode rust-parent-mode "Rust"
|
||||||
"Major mode for Rust code."
|
"Major mode for Rust code."
|
||||||
:group 'rust-mode
|
:group 'rust-mode
|
||||||
|
:syntax-table rust-mode-syntax-table
|
||||||
;; Basic syntax
|
|
||||||
(set-syntax-table rust-mode-syntax-table)
|
|
||||||
|
|
||||||
;; Indentation
|
;; Indentation
|
||||||
(set (make-local-variable 'indent-line-function)
|
(setq-local indent-line-function 'rust-mode-indent-line)
|
||||||
'rust-mode-indent-line)
|
|
||||||
|
|
||||||
;; Fonts
|
;; Fonts
|
||||||
(set (make-local-variable 'font-lock-defaults)
|
(setq-local font-lock-defaults '(rust-mode-font-lock-keywords nil nil nil nil))
|
||||||
'(rust-mode-font-lock-keywords nil nil nil nil))
|
|
||||||
|
|
||||||
;; Misc
|
;; Misc
|
||||||
(set (make-local-variable 'comment-start) "// ")
|
(setq-local comment-start "// ")
|
||||||
(set (make-local-variable 'comment-end) "")
|
(setq-local comment-end "")
|
||||||
(set (make-local-variable 'indent-tabs-mode) nil)
|
(setq-local indent-tabs-mode nil)
|
||||||
|
|
||||||
;; Allow paragraph fills for comments
|
;; Allow paragraph fills for comments
|
||||||
(set (make-local-variable 'comment-start-skip)
|
(setq-local comment-start-skip "\\(?://[/!]*\\|/\\*[*!]?\\)[[:space:]]*")
|
||||||
"\\(?://[/!]*\\|/\\*[*!]?\\)[[:space:]]*")
|
(setq-local paragraph-start
|
||||||
(set (make-local-variable 'paragraph-start)
|
|
||||||
(concat "[[:space:]]*\\(?:" comment-start-skip "\\|\\*/?[[:space:]]*\\|\\)$"))
|
(concat "[[:space:]]*\\(?:" comment-start-skip "\\|\\*/?[[:space:]]*\\|\\)$"))
|
||||||
(set (make-local-variable 'paragraph-separate) paragraph-start)
|
(setq-local paragraph-separate paragraph-start)
|
||||||
(set (make-local-variable 'normal-auto-fill-function) 'rust-do-auto-fill)
|
(setq-local normal-auto-fill-function 'rust-do-auto-fill)
|
||||||
(set (make-local-variable 'fill-paragraph-function) 'rust-fill-paragraph)
|
(setq-local fill-paragraph-function 'rust-fill-paragraph)
|
||||||
(set (make-local-variable 'fill-forward-paragraph-function) 'rust-fill-forward-paragraph)
|
(setq-local fill-forward-paragraph-function 'rust-fill-forward-paragraph)
|
||||||
(set (make-local-variable 'adaptive-fill-function) 'rust-find-fill-prefix)
|
(setq-local adaptive-fill-function 'rust-find-fill-prefix)
|
||||||
(set (make-local-variable 'comment-multi-line) t)
|
(setq-local comment-multi-line t)
|
||||||
(set (make-local-variable 'comment-line-break-function) 'rust-comment-indent-new-line)
|
(setq-local comment-line-break-function 'rust-comment-indent-new-line)
|
||||||
(set (make-local-variable 'imenu-generic-expression) rust-imenu-generic-expression)
|
(setq-local imenu-generic-expression rust-imenu-generic-expression)
|
||||||
(set (make-local-variable 'beginning-of-defun-function) 'rust-beginning-of-defun)
|
(setq-local beginning-of-defun-function 'rust-beginning-of-defun)
|
||||||
(set (make-local-variable 'end-of-defun-function) 'rust-end-of-defun)
|
(setq-local end-of-defun-function 'rust-end-of-defun))
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(add-to-list 'auto-mode-alist '("\\.rs$" . rust-mode))
|
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))
|
||||||
|
|
||||||
(defun rust-mode-reload ()
|
(defun rust-mode-reload ()
|
||||||
(interactive)
|
(interactive)
|
||||||
@ -455,8 +462,6 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
|
|||||||
(require 'rust-mode)
|
(require 'rust-mode)
|
||||||
(rust-mode))
|
(rust-mode))
|
||||||
|
|
||||||
(provide 'rust-mode)
|
|
||||||
|
|
||||||
;; Issue #6887: Rather than inheriting the 'gnu compilation error
|
;; Issue #6887: Rather than inheriting the 'gnu compilation error
|
||||||
;; regexp (which is broken on a few edge cases), add our own 'rust
|
;; regexp (which is broken on a few edge cases), add our own 'rust
|
||||||
;; compilation error regexp and use it instead.
|
;; compilation error regexp and use it instead.
|
||||||
@ -480,4 +485,6 @@ See `compilation-error-regexp-alist for help on their format.")
|
|||||||
(cons 'rustc rustc-compilation-regexps))
|
(cons 'rustc rustc-compilation-regexps))
|
||||||
(add-to-list 'compilation-error-regexp-alist 'rustc)))
|
(add-to-list 'compilation-error-regexp-alist 'rustc)))
|
||||||
|
|
||||||
|
(provide 'rust-mode)
|
||||||
|
|
||||||
;;; rust-mode.el ends here
|
;;; rust-mode.el ends here
|
||||||
|
Loading…
Reference in New Issue
Block a user