1 ;;; asm-mode.el --- mode for editing assembler code
3 ;; Copyright (C) 1991, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
8 ;; Keywords: tools, languages
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
30 ;; inspired by an earlier asm-mode by Martin Neitzel.
32 ;; This minor mode is based on text mode. It defines a private abbrev table
33 ;; that can be used to save abbrevs for assembler mnemonics. It binds just
36 ;; TAB tab to next tab stop
37 ;; : outdent preceding label, tab to tab stop
38 ;; comment char place or move comment
39 ;; asm-comment-char specifies which character this is;
40 ;; you can use a different character in different
42 ;; C-j, C-m newline and tab to tab stop
44 ;; Code is indented to the first tab stop level.
46 ;; This mode runs two hooks:
47 ;; 1) An asm-mode-set-comment-hook before the part of the initialization
48 ;; depending on asm-comment-char, and
49 ;; 2) an asm-mode-hook at the end of initialization.
54 "Mode for editing assembler code."
55 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
58 (defcustom asm-comment-char ?\
;
59 "*The comment-start character assumed by Asm mode."
63 (defvar asm-mode-syntax-table
64 (let ((st (make-syntax-table)))
65 (modify-syntax-entry ?
\n "> b" st
)
66 (modify-syntax-entry ?
/ ". 124b" st
)
67 (modify-syntax-entry ?
* ". 23" st
)
69 "Syntax table used while in Asm mode.")
71 (defvar asm-mode-abbrev-table nil
72 "Abbrev table used while in Asm mode.")
73 (define-abbrev-table 'asm-mode-abbrev-table
())
76 (let ((map (make-sparse-keymap)))
77 ;; Note that the comment character isn't set up until asm-mode is called.
78 (define-key map
":" 'asm-colon
)
79 (define-key map
"\C-c;" 'comment-region
)
80 (define-key map
"\C-j" 'newline-and-indent
)
81 (define-key map
"\C-m" 'newline-and-indent
)
82 (define-key map
[menu-bar
] (make-sparse-keymap))
83 (define-key map
[menu-bar asm-mode
] (cons "Asm" map
))
84 (define-key map
[asm-colon
]
85 '("Insert Colon" . asm-colon
))
86 (define-key map
[comment-region
]
87 '("Comment Region" . comment-region
))
88 (define-key map
[newline-and-indent
]
89 '("Insert Newline and Indent" . newline-and-indent
))
91 "Keymap for Asm mode.")
93 (defconst asm-font-lock-keywords
95 '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\(\\.\\sw+\\)*\\)?"
96 (1 font-lock-function-name-face
) (3 font-lock-keyword-face nil t
))
97 ;; label started from ".".
98 ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>:"
99 1 font-lock-function-name-face
)
100 ("^\\((\\sw+)\\)?\\s +\\(\\(\\.?\\sw\\|\\s_\\)+\\(\\.\\sw+\\)*\\)"
101 2 font-lock-keyword-face
)
102 ;; directive started from ".".
103 ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>[^:]?"
104 1 font-lock-keyword-face
)
106 ("%\\sw+" . font-lock-variable-name-face
))
107 cpp-font-lock-keywords
)
108 "Additional expressions to highlight in Assembler mode.")
112 "Major mode for editing typical assembler code.
113 Features a private abbrev table and the following bindings:
115 \\[asm-colon]\toutdent a preceding label, tab to next tab stop.
116 \\[tab-to-tab-stop]\ttab to next tab stop.
117 \\[asm-newline]\tnewline, then tab to next tab stop.
118 \\[asm-comment]\tsmart placement of assembler comments.
120 The character used for making comments is set by the variable
121 `asm-comment-char' (which defaults to `?\\;').
123 Alternatively, you may set this variable in `asm-mode-set-comment-hook',
124 which is called near the beginning of mode initialization.
126 Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
131 (kill-all-local-variables)
132 (setq mode-name
"Assembler")
133 (setq major-mode
'asm-mode
)
134 (setq local-abbrev-table asm-mode-abbrev-table
)
135 (make-local-variable 'font-lock-defaults
)
136 (setq font-lock-defaults
'(asm-font-lock-keywords))
137 (set (make-local-variable 'indent-line-function
) 'asm-indent-line
)
138 ;; Stay closer to the old TAB behavior (was tab-to-tab-stop).
139 (set (make-local-variable 'tab-always-indent
) nil
)
141 (run-hooks 'asm-mode-set-comment-hook
)
142 ;; Make our own local child of asm-mode-map
143 ;; so we can define our own comment character.
144 (use-local-map (nconc (make-sparse-keymap) asm-mode-map
))
145 (local-set-key (vector asm-comment-char
) 'asm-comment
)
146 (set-syntax-table (make-syntax-table asm-mode-syntax-table
))
147 (modify-syntax-entry asm-comment-char
"< b")
149 (make-local-variable 'comment-start
)
150 (setq comment-start
(string asm-comment-char
))
151 (make-local-variable 'comment-add
)
153 (make-local-variable 'comment-start-skip
)
154 (setq comment-start-skip
"\\(?:\\s<+\\|/[/*]+\\)[ \t]*")
155 (make-local-variable 'comment-end-skip
)
156 (setq comment-end-skip
"[ \t]*\\(\\s>\\|\\*+/\\)")
157 (make-local-variable 'comment-end
)
158 (setq comment-end
"")
159 (setq fill-prefix
"\t")
160 (run-mode-hooks 'asm-mode-hook
))
162 (defun asm-indent-line ()
163 "Auto-indent the current line."
165 (let* ((savep (point))
166 (indent (condition-case nil
169 (skip-chars-forward " \t")
170 (if (>= (point) savep
) (setq savep nil
))
171 (max (asm-calculate-indentation) 0))
174 (save-excursion (indent-line-to indent
))
175 (indent-line-to indent
))))
177 (defun asm-calculate-indentation ()
179 ;; Flush labels to the left margin.
180 (and (looking-at "\\(\\sw\\|\\s_\\)+:") 0)
181 ;; Same thing for `;;;' comments.
182 (and (looking-at "\\s<\\s<\\s<") 0)
183 ;; Simple `;' comments go to the comment-column.
184 (and (looking-at "\\s<\\(\\S<\\|\\'\\)") comment-column
)
185 ;; The rest goes at the first tab stop.
186 (or (car tab-stop-list
) tab-width
)))
189 "Insert a colon; if it follows a label, delete the label's indentation."
193 (skip-syntax-backward "w_")
194 (skip-syntax-backward " ")
195 (if (setq labelp
(bolp)) (delete-horizontal-space)))
196 (call-interactively 'self-insert-command
)
198 (delete-horizontal-space)
201 ;; Obsolete since Emacs-22.1.
202 (defalias 'asm-newline
'newline-and-indent
)
204 (defun asm-comment ()
205 "Convert an empty comment to a `larger' kind, or start a new one.
206 These are the known comment classes:
208 1 -- comment to the right of the code (at the comment-column)
209 2 -- comment on its own line, indented like code
210 3 -- comment on its own line, beginning at the left-most column.
212 Suggested usage: while writing your code, trigger asm-comment
213 repeatedly until you are satisfied with the kind of comment."
215 (comment-normalize-vars)
216 (let (comempty comment
)
220 (setq comment
(comment-search-forward (line-end-position) t
)))
221 (setq comempty
(looking-at "[ \t]*$")))
225 ;; Blank line? Then start comment at code indent level.
226 ;; Just like `comment-dwim'. -stef
227 ((save-excursion (beginning-of-line) (looking-at "^[ \t]*$"))
228 (indent-according-to-mode)
229 (insert asm-comment-char asm-comment-char ?\
))
231 ;; Nonblank line w/o comment => start a comment at comment-column.
232 ;; Also: point before the comment => jump inside.
233 ((or (null comment
) (< (point) comment
))
234 (indent-for-comment))
236 ;; Flush-left or non-empty comment present => just insert character.
237 ((or (not comempty
) (save-excursion (goto-char comment
) (bolp)))
238 (insert asm-comment-char
))
240 ;; Empty code-level comment => upgrade to next comment level.
241 ((save-excursion (goto-char comment
) (skip-chars-backward " \t") (bolp))
243 (insert asm-comment-char
)
244 (indent-for-comment))
246 ;; Empty comment ends non-empty code line => new comment above.
249 (skip-chars-backward " \t")
250 (delete-region (point) (line-end-position))
251 (beginning-of-line) (insert "\n") (backward-char)
256 ;;; arch-tag: 210e695f-f338-4376-8913-a4c5c72ac848
257 ;;; asm-mode.el ends here