1992-07-17 07:10:46 +00:00
|
|
|
;;; asm-mode.el --- mode for editing assembler code
|
|
|
|
|
2006-02-10 09:00:31 +00:00
|
|
|
;; Copyright (C) 1991, 2001, 2002, 2003, 2004, 2005, 2006
|
2005-08-01 08:43:45 +00:00
|
|
|
;; Free Software Foundation, Inc.
|
1992-07-24 21:00:38 +00:00
|
|
|
|
1992-07-17 07:10:46 +00:00
|
|
|
;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
|
1995-03-22 21:31:08 +00:00
|
|
|
;; Maintainer: FSF
|
1992-07-17 07:10:46 +00:00
|
|
|
;; Keywords: tools, languages
|
|
|
|
|
1991-12-21 08:23:15 +00:00
|
|
|
;; 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
|
1992-07-24 21:00:38 +00:00
|
|
|
;; the Free Software Foundation; either version 2, or (at your option)
|
1991-12-21 08:23:15 +00:00
|
|
|
;; 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
|
1996-01-14 07:34:30 +00:00
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
2005-07-04 17:55:18 +00:00
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
1991-12-21 08:23:15 +00:00
|
|
|
|
1992-07-17 07:10:46 +00:00
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
|
1991-12-21 08:23:15 +00:00
|
|
|
;; inspired by an earlier asm-mode by Martin Neitzel.
|
|
|
|
|
|
|
|
;; This minor mode is based on text mode. It defines a private abbrev table
|
|
|
|
;; that can be used to save abbrevs for assembler mnemonics. It binds just
|
|
|
|
;; five keys:
|
|
|
|
;;
|
|
|
|
;; TAB tab to next tab stop
|
|
|
|
;; : outdent preceding label, tab to tab stop
|
1995-03-23 20:57:04 +00:00
|
|
|
;; comment char place or move comment
|
|
|
|
;; asm-comment-char specifies which character this is;
|
|
|
|
;; you can use a different character in different
|
|
|
|
;; Asm mode buffers.
|
1991-12-21 08:23:15 +00:00
|
|
|
;; C-j, C-m newline and tab to tab stop
|
|
|
|
;;
|
|
|
|
;; Code is indented to the first tab stop level.
|
1992-07-17 07:10:46 +00:00
|
|
|
|
1991-12-21 08:23:15 +00:00
|
|
|
;; This mode runs two hooks:
|
1995-03-22 21:31:08 +00:00
|
|
|
;; 1) An asm-mode-set-comment-hook before the part of the initialization
|
1991-12-21 08:23:15 +00:00
|
|
|
;; depending on asm-comment-char, and
|
|
|
|
;; 2) an asm-mode-hook at the end of initialization.
|
|
|
|
|
1992-07-17 07:10:46 +00:00
|
|
|
;;; Code:
|
|
|
|
|
1998-01-26 11:29:46 +00:00
|
|
|
(defgroup asm nil
|
|
|
|
"Mode for editing assembler code."
|
2005-11-17 07:40:11 +00:00
|
|
|
:link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
|
1998-01-26 11:29:46 +00:00
|
|
|
:group 'languages)
|
|
|
|
|
1998-09-23 23:38:11 +00:00
|
|
|
(defcustom asm-comment-char ?\;
|
1998-01-26 11:29:46 +00:00
|
|
|
"*The comment-start character assumed by Asm mode."
|
|
|
|
:type 'character
|
|
|
|
:group 'asm)
|
1991-12-21 08:23:15 +00:00
|
|
|
|
2003-04-01 23:09:13 +00:00
|
|
|
(defvar asm-mode-syntax-table
|
|
|
|
(let ((st (make-syntax-table)))
|
|
|
|
(modify-syntax-entry ?\n ">" st)
|
|
|
|
(modify-syntax-entry ?/ ". 14b" st)
|
|
|
|
(modify-syntax-entry ?* ". 23b" st)
|
|
|
|
st)
|
1993-05-05 22:47:54 +00:00
|
|
|
"Syntax table used while in Asm mode.")
|
1991-12-21 08:23:15 +00:00
|
|
|
|
|
|
|
(defvar asm-mode-abbrev-table nil
|
1993-05-05 22:47:54 +00:00
|
|
|
"Abbrev table used while in Asm mode.")
|
1991-12-21 08:23:15 +00:00
|
|
|
(define-abbrev-table 'asm-mode-abbrev-table ())
|
|
|
|
|
2003-04-01 23:09:13 +00:00
|
|
|
(defvar asm-mode-map
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
;; Note that the comment character isn't set up until asm-mode is called.
|
|
|
|
(define-key map ":" 'asm-colon)
|
|
|
|
(define-key map "\C-c;" 'comment-region)
|
2003-04-02 16:48:11 +00:00
|
|
|
(define-key map "\C-j" 'newline-and-indent)
|
|
|
|
(define-key map "\C-m" 'newline-and-indent)
|
2003-04-01 23:09:13 +00:00
|
|
|
map)
|
1993-05-05 22:47:54 +00:00
|
|
|
"Keymap for Asm mode.")
|
1991-12-21 08:23:15 +00:00
|
|
|
|
1994-10-07 10:07:02 +00:00
|
|
|
(defconst asm-font-lock-keywords
|
2006-01-11 14:29:44 +00:00
|
|
|
(append
|
|
|
|
'(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\(\\.\\sw+\\)*\\)?"
|
|
|
|
(1 font-lock-function-name-face) (3 font-lock-keyword-face nil t))
|
|
|
|
;; label started from ".".
|
|
|
|
("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>:"
|
|
|
|
1 font-lock-function-name-face)
|
|
|
|
("^\\((\\sw+)\\)?\\s +\\(\\(\\.?\\sw\\|\\s_\\)+\\(\\.\\sw+\\)*\\)"
|
|
|
|
2 font-lock-keyword-face)
|
|
|
|
;; directive started from ".".
|
|
|
|
("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>[^:]?"
|
|
|
|
1 font-lock-keyword-face)
|
|
|
|
;; %register
|
|
|
|
("%\\sw+" . font-lock-variable-name-face))
|
|
|
|
cpp-font-lock-keywords)
|
|
|
|
"Additional expressions to highlight in Assembler mode.")
|
1994-10-07 10:07:02 +00:00
|
|
|
|
1991-12-21 08:23:15 +00:00
|
|
|
;;;###autoload
|
|
|
|
(defun asm-mode ()
|
|
|
|
"Major mode for editing typical assembler code.
|
1993-05-05 22:47:54 +00:00
|
|
|
Features a private abbrev table and the following bindings:
|
1991-12-21 08:23:15 +00:00
|
|
|
|
|
|
|
\\[asm-colon]\toutdent a preceding label, tab to next tab stop.
|
|
|
|
\\[tab-to-tab-stop]\ttab to next tab stop.
|
|
|
|
\\[asm-newline]\tnewline, then tab to next tab stop.
|
|
|
|
\\[asm-comment]\tsmart placement of assembler comments.
|
|
|
|
|
|
|
|
The character used for making comments is set by the variable
|
1998-09-23 23:38:11 +00:00
|
|
|
`asm-comment-char' (which defaults to `?\\;').
|
1991-12-21 08:23:15 +00:00
|
|
|
|
1995-03-22 21:31:08 +00:00
|
|
|
Alternatively, you may set this variable in `asm-mode-set-comment-hook',
|
|
|
|
which is called near the beginning of mode initialization.
|
1991-12-21 08:23:15 +00:00
|
|
|
|
1993-05-05 22:47:54 +00:00
|
|
|
Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
|
1991-12-21 08:23:15 +00:00
|
|
|
|
1994-03-22 20:48:30 +00:00
|
|
|
Special commands:
|
2003-04-01 23:09:13 +00:00
|
|
|
\\{asm-mode-map}"
|
1991-12-21 08:23:15 +00:00
|
|
|
(interactive)
|
|
|
|
(kill-all-local-variables)
|
|
|
|
(setq mode-name "Assembler")
|
|
|
|
(setq major-mode 'asm-mode)
|
|
|
|
(setq local-abbrev-table asm-mode-abbrev-table)
|
1994-10-12 08:06:09 +00:00
|
|
|
(make-local-variable 'font-lock-defaults)
|
|
|
|
(setq font-lock-defaults '(asm-font-lock-keywords))
|
2003-04-02 16:48:11 +00:00
|
|
|
(set (make-local-variable '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)
|
1995-03-23 20:57:04 +00:00
|
|
|
|
1991-12-21 08:23:15 +00:00
|
|
|
(run-hooks 'asm-mode-set-comment-hook)
|
1995-03-23 20:57:04 +00:00
|
|
|
;; Make our own local child of asm-mode-map
|
|
|
|
;; so we can define our own comment character.
|
|
|
|
(use-local-map (nconc (make-sparse-keymap) asm-mode-map))
|
|
|
|
(local-set-key (vector asm-comment-char) 'asm-comment)
|
2003-04-01 23:09:13 +00:00
|
|
|
(set-syntax-table (make-syntax-table asm-mode-syntax-table))
|
|
|
|
(modify-syntax-entry asm-comment-char "<")
|
|
|
|
|
|
|
|
(make-local-variable 'comment-start)
|
2003-04-02 16:48:11 +00:00
|
|
|
(setq comment-start (string asm-comment-char))
|
|
|
|
(make-local-variable 'comment-add)
|
|
|
|
(setq comment-add 1)
|
2003-04-01 23:09:13 +00:00
|
|
|
(make-local-variable 'comment-start-skip)
|
|
|
|
(setq comment-start-skip "\\(?:\\s<+\\|/\\*+\\)[ \t]*")
|
|
|
|
(make-local-variable 'comment-end-skip)
|
2003-04-02 16:48:11 +00:00
|
|
|
(setq comment-end-skip "[ \t]*\\(\\s>\\|\\*+/\\)")
|
1991-12-21 08:23:15 +00:00
|
|
|
(make-local-variable 'comment-end)
|
|
|
|
(setq comment-end "")
|
|
|
|
(setq fill-prefix "\t")
|
2003-04-02 16:48:11 +00:00
|
|
|
(run-mode-hooks 'asm-mode-hook))
|
|
|
|
|
|
|
|
(defun asm-indent-line ()
|
|
|
|
"Auto-indent the current line."
|
|
|
|
(interactive)
|
|
|
|
(let* ((savep (point))
|
|
|
|
(indent (condition-case nil
|
|
|
|
(save-excursion
|
|
|
|
(forward-line 0)
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
(if (>= (point) savep) (setq savep nil))
|
|
|
|
(max (asm-calculate-indentation) 0))
|
|
|
|
(error 0))))
|
|
|
|
(if savep
|
|
|
|
(save-excursion (indent-line-to indent))
|
|
|
|
(indent-line-to indent))))
|
|
|
|
|
|
|
|
(defun asm-calculate-indentation ()
|
|
|
|
(or
|
|
|
|
;; Flush labels to the left margin.
|
|
|
|
(and (looking-at "\\(\\sw\\|\\s_\\)+:") 0)
|
|
|
|
;; Same thing for `;;;' comments.
|
|
|
|
(and (looking-at "\\s<\\s<\\s<") 0)
|
|
|
|
;; Simple `;' comments go to the comment-column.
|
|
|
|
(and (looking-at "\\s<\\(\\S<\\|\\'\\)") comment-column)
|
|
|
|
;; The rest goes at the first tab stop.
|
|
|
|
(or (car tab-stop-list) tab-width)))
|
|
|
|
|
1991-12-21 08:23:15 +00:00
|
|
|
(defun asm-colon ()
|
|
|
|
"Insert a colon; if it follows a label, delete the label's indentation."
|
|
|
|
(interactive)
|
2003-04-02 16:48:11 +00:00
|
|
|
(let ((labelp nil))
|
|
|
|
(save-excursion
|
|
|
|
(skip-syntax-backward "w_")
|
|
|
|
(skip-syntax-backward " ")
|
|
|
|
(if (setq labelp (bolp)) (delete-horizontal-space)))
|
|
|
|
(call-interactively 'self-insert-command)
|
|
|
|
(when labelp
|
|
|
|
(delete-horizontal-space)
|
|
|
|
(tab-to-tab-stop))))
|
|
|
|
|
2005-02-09 15:50:47 +00:00
|
|
|
;; Obsolete since Emacs-22.1.
|
2003-04-02 16:48:11 +00:00
|
|
|
(defalias 'asm-newline 'newline-and-indent)
|
1991-12-21 08:23:15 +00:00
|
|
|
|
|
|
|
(defun asm-comment ()
|
|
|
|
"Convert an empty comment to a `larger' kind, or start a new one.
|
|
|
|
These are the known comment classes:
|
|
|
|
|
|
|
|
1 -- comment to the right of the code (at the comment-column)
|
|
|
|
2 -- comment on its own line, indented like code
|
|
|
|
3 -- comment on its own line, beginning at the left-most column.
|
|
|
|
|
|
|
|
Suggested usage: while writing your code, trigger asm-comment
|
|
|
|
repeatedly until you are satisfied with the kind of comment."
|
|
|
|
(interactive)
|
2003-04-02 16:48:11 +00:00
|
|
|
(comment-normalize-vars)
|
|
|
|
(let (comempty comment)
|
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
2005-07-16 18:41:15 +00:00
|
|
|
(with-no-warnings
|
|
|
|
(setq comment (comment-search-forward (line-end-position) t)))
|
2003-04-02 16:48:11 +00:00
|
|
|
(setq comempty (looking-at "[ \t]*$")))
|
|
|
|
|
1991-12-21 08:23:15 +00:00
|
|
|
(cond
|
|
|
|
|
|
|
|
;; Blank line? Then start comment at code indent level.
|
2003-04-02 16:48:11 +00:00
|
|
|
;; Just like `comment-dwim'. -stef
|
|
|
|
((save-excursion (beginning-of-line) (looking-at "^[ \t]*$"))
|
|
|
|
(indent-according-to-mode)
|
|
|
|
(insert asm-comment-char asm-comment-char ?\ ))
|
|
|
|
|
|
|
|
;; Nonblank line w/o comment => start a comment at comment-column.
|
|
|
|
;; Also: point before the comment => jump inside.
|
|
|
|
((or (null comment) (< (point) comment))
|
1991-12-21 08:23:15 +00:00
|
|
|
(indent-for-comment))
|
|
|
|
|
2003-04-02 16:48:11 +00:00
|
|
|
;; Flush-left or non-empty comment present => just insert character.
|
|
|
|
((or (not comempty) (save-excursion (goto-char comment) (bolp)))
|
1991-12-21 08:23:15 +00:00
|
|
|
(insert asm-comment-char))
|
|
|
|
|
2003-04-02 16:48:11 +00:00
|
|
|
;; Empty code-level comment => upgrade to next comment level.
|
|
|
|
((save-excursion (goto-char comment) (skip-chars-backward " \t") (bolp))
|
|
|
|
(goto-char comment)
|
|
|
|
(insert asm-comment-char)
|
|
|
|
(indent-for-comment))
|
1991-12-21 08:23:15 +00:00
|
|
|
|
2003-04-02 16:48:11 +00:00
|
|
|
;; Empty comment ends non-empty code line => new comment above.
|
1991-12-21 08:23:15 +00:00
|
|
|
(t
|
2003-04-02 16:48:11 +00:00
|
|
|
(goto-char comment)
|
|
|
|
(skip-chars-backward " \t")
|
|
|
|
(delete-region (point) (line-end-position))
|
|
|
|
(beginning-of-line) (insert "\n") (backward-char)
|
|
|
|
(asm-comment)))))
|
1991-12-21 08:23:15 +00:00
|
|
|
|
1997-06-22 18:57:55 +00:00
|
|
|
(provide 'asm-mode)
|
|
|
|
|
2003-09-01 15:45:59 +00:00
|
|
|
;;; arch-tag: 210e695f-f338-4376-8913-a4c5c72ac848
|
1991-12-21 08:23:15 +00:00
|
|
|
;;; asm-mode.el ends here
|