From b4eaa7eaceeeec749b74e3c5ebbf900d5754b17d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Simen=20Heggest=C3=B8yl?= Date: Thu, 18 Jul 2019 20:39:47 +0200 Subject: [PATCH] Use lexical-binding in asm-mode.el and add tests * lisp/progmodes/asm-mode.el: Use lexical-binding. (asm-comment-char): Remove redundant :group arg. (asm-mode): Use `setq-local'. (asm-calculate-indentation): Remove moot `or'. * test/lisp/progmodes/asm-mode-tests.el: New file with tests for asm-mode.el. --- lisp/progmodes/asm-mode.el | 24 ++++----- test/lisp/progmodes/asm-mode-tests.el | 72 +++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 test/lisp/progmodes/asm-mode-tests.el diff --git a/lisp/progmodes/asm-mode.el b/lisp/progmodes/asm-mode.el index d6e155997d4..017a5b5bace 100644 --- a/lisp/progmodes/asm-mode.el +++ b/lisp/progmodes/asm-mode.el @@ -1,4 +1,4 @@ -;;; asm-mode.el --- mode for editing assembler code +;;; asm-mode.el --- mode for editing assembler code -*- lexical-binding: t; -*- ;; Copyright (C) 1991, 2001-2019 Free Software Foundation, Inc. @@ -54,8 +54,7 @@ (defcustom asm-comment-char ?\; "The `comment-start' character assumed by Asm mode." - :type 'character - :group 'asm) + :type 'character) (defvar asm-mode-syntax-table (let ((st (make-syntax-table))) @@ -127,10 +126,10 @@ Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization. Special commands: \\{asm-mode-map}" (setq local-abbrev-table asm-mode-abbrev-table) - (set (make-local-variable 'font-lock-defaults) '(asm-font-lock-keywords)) - (set (make-local-variable 'indent-line-function) 'asm-indent-line) + (setq-local font-lock-defaults '(asm-font-lock-keywords)) + (setq-local indent-line-function #'asm-indent-line) ;; Stay closer to the old TAB behavior (was tab-to-tab-stop). - (set (make-local-variable 'tab-always-indent) nil) + (setq-local tab-always-indent nil) (run-hooks 'asm-mode-set-comment-hook) ;; Make our own local child of asm-mode-map @@ -140,12 +139,11 @@ Special commands: (set-syntax-table (make-syntax-table asm-mode-syntax-table)) (modify-syntax-entry asm-comment-char "< b") - (set (make-local-variable 'comment-start) (string asm-comment-char)) - (set (make-local-variable 'comment-add) 1) - (set (make-local-variable 'comment-start-skip) - "\\(?:\\s<+\\|/[/*]+\\)[ \t]*") - (set (make-local-variable 'comment-end-skip) "[ \t]*\\(\\s>\\|\\*+/\\)") - (set (make-local-variable 'comment-end) "") + (setq-local comment-start (string asm-comment-char)) + (setq-local comment-add 1) + (setq-local comment-start-skip "\\(?:\\s<+\\|/[/*]+\\)[ \t]*") + (setq-local comment-end-skip "[ \t]*\\(\\s>\\|\\*+/\\)") + (setq-local comment-end "") (setq fill-prefix "\t")) (defun asm-indent-line () @@ -172,7 +170,7 @@ Special commands: ;; Simple `;' comments go to the comment-column. (and (looking-at "\\s<\\(\\S<\\|\\'\\)") comment-column) ;; The rest goes at the first tab stop. - (or (indent-next-tab-stop 0)))) + (indent-next-tab-stop 0))) (defun asm-colon () "Insert a colon; if it follows a label, delete the label's indentation." diff --git a/test/lisp/progmodes/asm-mode-tests.el b/test/lisp/progmodes/asm-mode-tests.el new file mode 100644 index 00000000000..a10add0680d --- /dev/null +++ b/test/lisp/progmodes/asm-mode-tests.el @@ -0,0 +1,72 @@ +;;; asm-mode-tests.el --- Tests for asm-mode.el -*- lexical-binding: t; -*- + +;; Copyright (C) 2019 Free Software Foundation, Inc. + +;; Author: Simen Heggestøyl +;; Keywords: + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Commentary: + +;; + +;;; Code: + +(require 'asm-mode) +(require 'ert) + +(defmacro asm-mode-tests--with-temp-buffer (&rest body) + "Create a temporary Asm mode buffer and evaluate BODY there." + (declare (indent 0)) + `(with-temp-buffer + (let ((asm-comment-char ?\;)) + (asm-mode) + ,@body))) + +(ert-deftest asm-mode-tests-colon () + (asm-mode-tests--with-temp-buffer + (let ((indent-tabs-mode t)) + (insert "\t label") + (let ((last-command-event ?:)) + (asm-colon)) + (should (equal (buffer-string) "label:\t"))))) + +(ert-deftest asm-mode-tests-colon-inside-comment () + (asm-mode-tests--with-temp-buffer + (insert ";comment") + (let ((last-command-event ?:)) + (asm-colon)) + (should (equal (buffer-string) ";comment:")))) + +(ert-deftest asm-mode-tests-comment () + (asm-mode-tests--with-temp-buffer + (insert "label:") + (goto-char (point-min)) + ;; First invocation + (asm-comment) + (should (string-match-p "label:[ \t]+;" (buffer-string))) + (should (= (current-column) (+ comment-column 1))) + ;; Second invocation + (asm-comment) + (should (string-match-p "[ \t]+;; \nlabel:" (buffer-string))) + (should (= (current-column) (+ tab-width 3))) + ;; Third invocation + (asm-comment) + (should (string-match-p ";;; \nlabel:" (buffer-string))) + (should (= (current-column) 4)))) + +;;; asm-mode-tests.el ends here -- 2.39.2