Skip to content

Commit 3a70976

Browse files
committed
rust-mode cleanup.
* Use `setq-local' instead of (set (make-local-variable ...) 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.
1 parent 8801d89 commit 3a70976

File tree

1 file changed

+58
-51
lines changed

1 file changed

+58
-51
lines changed

src/etc/emacs/rust-mode.el

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,29 @@
33
;; Version: 0.2.0
44
;; Author: Mozilla
55
;; Url: https://github.com/mozilla/rust
6+
;; Keywords: languages
7+
8+
;;; Commentary:
9+
;;
10+
11+
;;; Code:
612

7-
(eval-when-compile (require 'cl))
813
(eval-when-compile (require 'misc))
914

15+
;; for GNU Emacs < 24.3
16+
(eval-when-compile
17+
(unless (fboundp 'setq-local)
18+
(defmacro setq-local (var val)
19+
"Set variable VAR to value VAL in current buffer."
20+
(list 'set (list 'make-local-variable (list 'quote var)) val))))
21+
1022
;; Syntax definitions and helpers
1123
(defvar rust-mode-syntax-table
1224
(let ((table (make-syntax-table)))
1325

1426
;; Operators
15-
(loop for i in '(?+ ?- ?* ?/ ?& ?| ?^ ?! ?< ?> ?~ ?@)
16-
do (modify-syntax-entry i "." table))
27+
(dolist (i '(?+ ?- ?* ?/ ?& ?| ?^ ?! ?< ?> ?~ ?@))
28+
(modify-syntax-entry i "." table))
1729

1830
;; Strings
1931
(modify-syntax-entry ?\" "\"" table)
@@ -30,10 +42,14 @@
3042

3143
table))
3244

33-
(defgroup rust-mode nil "Support for Rust code.")
45+
(defgroup rust-mode nil
46+
"Support for Rust code."
47+
:link '(url-link "http://www.rust-lang.org/")
48+
:group 'languages)
3449

3550
(defcustom rust-indent-offset 4
36-
"*Indent Rust code by this number of spaces."
51+
"Indent Rust code by this number of spaces."
52+
:type 'integer
3753
:group 'rust-mode)
3854

3955
(defun rust-paren-level () (nth 0 (syntax-ppss)))
@@ -226,17 +242,16 @@
226242
)
227243

228244
;; Item definitions
229-
(loop for (item . face) in
230-
231-
'(("enum" . font-lock-type-face)
232-
("struct" . font-lock-type-face)
233-
("type" . font-lock-type-face)
234-
("mod" . font-lock-type-face)
235-
("use" . font-lock-type-face)
236-
("fn" . font-lock-function-name-face)
237-
("static" . font-lock-constant-face))
238-
239-
collect `(,(rust-re-item-def item) 1 ,face))))
245+
(mapcar #'(lambda (x)
246+
(list (rust-re-item-def (car x))
247+
1 (cdr x)))
248+
'(("enum" . font-lock-type-face)
249+
("struct" . font-lock-type-face)
250+
("type" . font-lock-type-face)
251+
("mod" . font-lock-type-face)
252+
("use" . font-lock-type-face)
253+
("fn" . font-lock-function-name-face)
254+
("static" . font-lock-constant-face)))))
240255

241256
(defun rust-fill-prefix-for-comment-start (line-start)
242257
"Determine what to use for `fill-prefix' based on what is at the beginning of a line."
@@ -350,17 +365,17 @@
350365

351366
;;; Imenu support
352367
(defvar rust-imenu-generic-expression
353-
(append (loop for item in
354-
'("enum" "struct" "type" "mod" "fn" "trait")
355-
collect `(nil ,(rust-re-item-def item) 1))
368+
(append (mapcar #'(lambda (x)
369+
(list nil (rust-re-item-def x) 1))
370+
'("enum" "struct" "type" "mod" "fn" "trait"))
356371
`(("Impl" ,(rust-re-item-def "impl") 1)))
357372
"Value for `imenu-generic-expression' in Rust mode.
358373
359374
Create a flat index of the item definitions in a Rust file.
360375
361376
Imenu will show all the enums, structs, etc. at the same level.
362-
Implementations will be shown under the `Impl` subheading.
363-
Use idomenu (imenu with ido-mode) for best mileage.")
377+
Implementations will be shown under the `Impl` subheading. Use
378+
idomenu (imenu with `ido-mode') for best mileage.")
364379

365380
;;; Defun Motions
366381

@@ -369,8 +384,7 @@ Use idomenu (imenu with ido-mode) for best mileage.")
369384
(concat "^\\s-*\\(?:priv\\|pub\\)?\\s-*"
370385
(regexp-opt
371386
'("enum" "struct" "type" "mod" "use" "fn" "static" "impl"
372-
"extern" "impl" "static" "trait"
373-
))))
387+
"extern" "impl" "static" "trait"))))
374388

375389
(defun rust-beginning-of-defun (&optional arg)
376390
"Move backward to the beginning of the current defun.
@@ -411,52 +425,43 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
411425
(define-derived-mode rust-mode rust-parent-mode "Rust"
412426
"Major mode for Rust code."
413427
:group 'rust-mode
414-
415-
;; Basic syntax
416-
(set-syntax-table rust-mode-syntax-table)
428+
:syntax-table rust-mode-syntax-table
417429

418430
;; Indentation
419-
(set (make-local-variable 'indent-line-function)
420-
'rust-mode-indent-line)
431+
(setq-local indent-line-function 'rust-mode-indent-line)
421432

422433
;; Fonts
423-
(set (make-local-variable 'font-lock-defaults)
424-
'(rust-mode-font-lock-keywords nil nil nil nil))
434+
(setq-local font-lock-defaults '(rust-mode-font-lock-keywords nil nil nil nil))
425435

426436
;; Misc
427-
(set (make-local-variable 'comment-start) "// ")
428-
(set (make-local-variable 'comment-end) "")
429-
(set (make-local-variable 'indent-tabs-mode) nil)
437+
(setq-local comment-start "// ")
438+
(setq-local comment-end "")
439+
(setq-local indent-tabs-mode nil)
430440

431441
;; Allow paragraph fills for comments
432-
(set (make-local-variable 'comment-start-skip)
433-
"\\(?://[/!]*\\|/\\*[*!]?\\)[[:space:]]*")
434-
(set (make-local-variable 'paragraph-start)
442+
(setq-local comment-start-skip "\\(?://[/!]*\\|/\\*[*!]?\\)[[:space:]]*")
443+
(setq-local paragraph-start
435444
(concat "[[:space:]]*\\(?:" comment-start-skip "\\|\\*/?[[:space:]]*\\|\\)$"))
436-
(set (make-local-variable 'paragraph-separate) paragraph-start)
437-
(set (make-local-variable 'normal-auto-fill-function) 'rust-do-auto-fill)
438-
(set (make-local-variable 'fill-paragraph-function) 'rust-fill-paragraph)
439-
(set (make-local-variable 'fill-forward-paragraph-function) 'rust-fill-forward-paragraph)
440-
(set (make-local-variable 'adaptive-fill-function) 'rust-find-fill-prefix)
441-
(set (make-local-variable 'comment-multi-line) t)
442-
(set (make-local-variable 'comment-line-break-function) 'rust-comment-indent-new-line)
443-
(set (make-local-variable 'imenu-generic-expression) rust-imenu-generic-expression)
444-
(set (make-local-variable 'beginning-of-defun-function) 'rust-beginning-of-defun)
445-
(set (make-local-variable 'end-of-defun-function) 'rust-end-of-defun)
446-
)
447-
445+
(setq-local paragraph-separate paragraph-start)
446+
(setq-local normal-auto-fill-function 'rust-do-auto-fill)
447+
(setq-local fill-paragraph-function 'rust-fill-paragraph)
448+
(setq-local fill-forward-paragraph-function 'rust-fill-forward-paragraph)
449+
(setq-local adaptive-fill-function 'rust-find-fill-prefix)
450+
(setq-local comment-multi-line t)
451+
(setq-local comment-line-break-function 'rust-comment-indent-new-line)
452+
(setq-local imenu-generic-expression rust-imenu-generic-expression)
453+
(setq-local beginning-of-defun-function 'rust-beginning-of-defun)
454+
(setq-local end-of-defun-function 'rust-end-of-defun))
448455

449456
;;;###autoload
450-
(add-to-list 'auto-mode-alist '("\\.rs$" . rust-mode))
457+
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))
451458

452459
(defun rust-mode-reload ()
453460
(interactive)
454461
(unload-feature 'rust-mode)
455462
(require 'rust-mode)
456463
(rust-mode))
457464

458-
(provide 'rust-mode)
459-
460465
;; Issue #6887: Rather than inheriting the 'gnu compilation error
461466
;; regexp (which is broken on a few edge cases), add our own 'rust
462467
;; compilation error regexp and use it instead.
@@ -480,4 +485,6 @@ See `compilation-error-regexp-alist for help on their format.")
480485
(cons 'rustc rustc-compilation-regexps))
481486
(add-to-list 'compilation-error-regexp-alist 'rustc)))
482487

488+
(provide 'rust-mode)
489+
483490
;;; rust-mode.el ends here

0 commit comments

Comments
 (0)