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 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
27 ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
28 ;; inspired by an earlier asm-mode by Martin Neitzel.
30 ;; This minor mode is based on text mode. It defines a private abbrev table
31 ;; that can be used to save abbrevs for assembler mnemonics. It binds just
34 ;; TAB tab to next tab stop
35 ;; : outdent preceding label, tab to tab stop
36 ;; comment char place or move comment
37 ;; asm-comment-char specifies which character this is;
38 ;; you can use a different character in different
40 ;; C-j, C-m newline and tab to tab stop
42 ;; Code is indented to the first tab stop level.
44 ;; This mode runs two hooks:
45 ;; 1) An asm-mode-set-comment-hook before the part of the initialization
46 ;; depending on asm-comment-char, and
47 ;; 2) an asm-mode-hook at the end of initialization.
52 "Mode for editing assembler code."
53 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
56 (defcustom asm-comment-char ?\
;
57 "*The comment-start character assumed by Asm mode."
61 (defvar asm-mode-syntax-table
62 (let ((st (make-syntax-table)))
63 (modify-syntax-entry ?
\n "> b" st
)
64 (modify-syntax-entry ?
/ ". 124b" st
)
65 (modify-syntax-entry ?
* ". 23" st
)
67 "Syntax table used while in Asm mode.")
69 (defvar asm-mode-abbrev-table nil
70 "Abbrev table used while in Asm mode.")
71 (define-abbrev-table 'asm-mode-abbrev-table
())
74 (let ((map (make-sparse-keymap)))
75 ;; Note that the comment character isn't set up until asm-mode is called.
76 (define-key map
":" 'asm-colon
)
77 (define-key map
"\C-c;" 'comment-region
)
78 (define-key map
"\C-j" 'newline-and-indent
)
79 (define-key map
"\C-m" 'newline-and-indent
)
80 (define-key map
[menu-bar
] (make-sparse-keymap))
81 (define-key map
[menu-bar asm-mode
] (cons "Asm" map
))
82 (define-key map
[comment-region
]
83 '(menu-item "Comment Region" comment-region
84 :help
"Comment or uncomment each line in the region"))
85 (define-key map
[newline-and-indent
]
86 '(menu-item "Insert Newline and Indent" newline-and-indent
87 :help
"Insert a newline, then indent according to major mode"))
88 (define-key map
[asm-colon
]
89 '(menu-item "Insert Colon" asm-colon
90 :help
"Insert a colon; if it follows a label, delete the label's indentation"))
92 "Keymap for Asm mode.")
94 (defconst asm-font-lock-keywords
96 '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\(\\.\\sw+\\)*\\)?"
97 (1 font-lock-function-name-face
) (3 font-lock-keyword-face nil t
))
98 ;; label started from ".".
99 ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>:"
100 1 font-lock-function-name-face
)
101 ("^\\((\\sw+)\\)?\\s +\\(\\(\\.?\\sw\\|\\s_\\)+\\(\\.\\sw+\\)*\\)"
102 2 font-lock-keyword-face
)
103 ;; directive started from ".".
104 ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>[^:]?"
105 1 font-lock-keyword-face
)
107 ("%\\sw+" . font-lock-variable-name-face
))
108 cpp-font-lock-keywords
)
109 "Additional expressions to highlight in Assembler mode.")
113 "Major mode for editing typical assembler code.
114 Features a private abbrev table and the following bindings:
116 \\[asm-colon]\toutdent a preceding label, tab to next tab stop.
117 \\[tab-to-tab-stop]\ttab to next tab stop.
118 \\[asm-newline]\tnewline, then tab to next tab stop.
119 \\[asm-comment]\tsmart placement of assembler comments.
121 The character used for making comments is set by the variable
122 `asm-comment-char' (which defaults to `?\\;').
124 Alternatively, you may set this variable in `asm-mode-set-comment-hook',
125 which is called near the beginning of mode initialization.
127 Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
132 (kill-all-local-variables)
133 (setq mode-name
"Assembler")
134 (setq major-mode
'asm-mode
)
135 (setq local-abbrev-table asm-mode-abbrev-table
)
136 (make-local-variable 'font-lock-defaults
)
137 (setq font-lock-defaults
'(asm-font-lock-keywords))
138 (set (make-local-variable 'indent-line-function
) 'asm-indent-line
)
139 ;; Stay closer to the old TAB behavior (was tab-to-tab-stop).
140 (set (make-local-variable 'tab-always-indent
) nil
)
142 (run-hooks 'asm-mode-set-comment-hook
)
143 ;; Make our own local child of asm-mode-map
144 ;; so we can define our own comment character.
145 (use-local-map (nconc (make-sparse-keymap) asm-mode-map
))
146 (local-set-key (vector asm-comment-char
) 'asm-comment
)
147 (set-syntax-table (make-syntax-table asm-mode-syntax-table
))
148 (modify-syntax-entry asm-comment-char
"< b")
150 (make-local-variable 'comment-start
)
151 (setq comment-start
(string asm-comment-char
))
152 (make-local-variable 'comment-add
)
154 (make-local-variable 'comment-start-skip
)
155 (setq comment-start-skip
"\\(?:\\s<+\\|/[/*]+\\)[ \t]*")
156 (make-local-variable 'comment-end-skip
)
157 (setq comment-end-skip
"[ \t]*\\(\\s>\\|\\*+/\\)")
158 (make-local-variable 'comment-end
)
159 (setq comment-end
"")
160 (setq fill-prefix
"\t")
161 (run-mode-hooks 'asm-mode-hook
))
163 (defun asm-indent-line ()
164 "Auto-indent the current line."
166 (let* ((savep (point))
167 (indent (condition-case nil
170 (skip-chars-forward " \t")
171 (if (>= (point) savep
) (setq savep nil
))
172 (max (asm-calculate-indentation) 0))
175 (save-excursion (indent-line-to indent
))
176 (indent-line-to indent
))))
178 (defun asm-calculate-indentation ()
180 ;; Flush labels to the left margin.
181 (and (looking-at "\\(\\sw\\|\\s_\\)+:") 0)
182 ;; Same thing for `;;;' comments.
183 (and (looking-at "\\s<\\s<\\s<") 0)
184 ;; Simple `;' comments go to the comment-column.
185 (and (looking-at "\\s<\\(\\S<\\|\\'\\)") comment-column
)
186 ;; The rest goes at the first tab stop.
187 (or (car tab-stop-list
) tab-width
)))
190 "Insert a colon; if it follows a label, delete the label's indentation."
194 (skip-syntax-backward "w_")
195 (skip-syntax-backward " ")
196 (if (setq labelp
(bolp)) (delete-horizontal-space)))
197 (call-interactively 'self-insert-command
)
199 (delete-horizontal-space)
202 ;; Obsolete since Emacs-22.1.
203 (defalias 'asm-newline
'newline-and-indent
)
205 (defun asm-comment ()
206 "Convert an empty comment to a `larger' kind, or start a new one.
207 These are the known comment classes:
209 1 -- comment to the right of the code (at the comment-column)
210 2 -- comment on its own line, indented like code
211 3 -- comment on its own line, beginning at the left-most column.
213 Suggested usage: while writing your code, trigger asm-comment
214 repeatedly until you are satisfied with the kind of comment."
216 (comment-normalize-vars)
217 (let (comempty comment
)
221 (setq comment
(comment-search-forward (line-end-position) t
)))
222 (setq comempty
(looking-at "[ \t]*$")))
226 ;; Blank line? Then start comment at code indent level.
227 ;; Just like `comment-dwim'. -stef
228 ((save-excursion (beginning-of-line) (looking-at "^[ \t]*$"))
229 (indent-according-to-mode)
230 (insert asm-comment-char asm-comment-char ?\
))
232 ;; Nonblank line w/o comment => start a comment at comment-column.
233 ;; Also: point before the comment => jump inside.
234 ((or (null comment
) (< (point) comment
))
235 (indent-for-comment))
237 ;; Flush-left or non-empty comment present => just insert character.
238 ((or (not comempty
) (save-excursion (goto-char comment
) (bolp)))
239 (insert asm-comment-char
))
241 ;; Empty code-level comment => upgrade to next comment level.
242 ((save-excursion (goto-char comment
) (skip-chars-backward " \t") (bolp))
244 (insert asm-comment-char
)
245 (indent-for-comment))
247 ;; Empty comment ends non-empty code line => new comment above.
250 (skip-chars-backward " \t")
251 (delete-region (point) (line-end-position))
252 (beginning-of-line) (insert "\n") (backward-char)
257 ;; arch-tag: 210e695f-f338-4376-8913-a4c5c72ac848
258 ;;; asm-mode.el ends here