Commit | Line | Data |
---|---|---|
f961a17c ER |
1 | ;;; asm-mode.el --- mode for editing assembler code |
2 | ||
769c4c63 | 3 | ;; Copyright (C) 1991, 2003 Free Software Foundation, Inc. |
1bd60093 | 4 | |
f961a17c | 5 | ;; Author: Eric S. Raymond <esr@snark.thyrsus.com> |
148d2a8d | 6 | ;; Maintainer: FSF |
f961a17c ER |
7 | ;; Keywords: tools, languages |
8 | ||
0231f2dc JB |
9 | ;; This file is part of GNU Emacs. |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
1bd60093 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
0231f2dc JB |
14 | ;; any later version. |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
0231f2dc | 25 | |
f961a17c ER |
26 | ;;; Commentary: |
27 | ||
28 | ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>, | |
0231f2dc | 29 | ;; inspired by an earlier asm-mode by Martin Neitzel. |
0231f2dc JB |
30 | |
31 | ;; This minor mode is based on text mode. It defines a private abbrev table | |
32 | ;; that can be used to save abbrevs for assembler mnemonics. It binds just | |
33 | ;; five keys: | |
34 | ;; | |
35 | ;; TAB tab to next tab stop | |
36 | ;; : outdent preceding label, tab to tab stop | |
f45a220a RS |
37 | ;; comment char place or move comment |
38 | ;; asm-comment-char specifies which character this is; | |
39 | ;; you can use a different character in different | |
40 | ;; Asm mode buffers. | |
0231f2dc JB |
41 | ;; C-j, C-m newline and tab to tab stop |
42 | ;; | |
43 | ;; Code is indented to the first tab stop level. | |
f961a17c | 44 | |
0231f2dc | 45 | ;; This mode runs two hooks: |
148d2a8d | 46 | ;; 1) An asm-mode-set-comment-hook before the part of the initialization |
0231f2dc JB |
47 | ;; depending on asm-comment-char, and |
48 | ;; 2) an asm-mode-hook at the end of initialization. | |
49 | ||
f961a17c ER |
50 | ;;; Code: |
51 | ||
28d16ed3 AS |
52 | (defgroup asm nil |
53 | "Mode for editing assembler code." | |
54 | :group 'languages) | |
55 | ||
1ce965fe | 56 | (defcustom asm-comment-char ?\; |
28d16ed3 AS |
57 | "*The comment-start character assumed by Asm mode." |
58 | :type 'character | |
59 | :group 'asm) | |
0231f2dc | 60 | |
c7d565f4 SM |
61 | (defvar asm-mode-syntax-table |
62 | (let ((st (make-syntax-table))) | |
63 | (modify-syntax-entry ?\n ">" st) | |
64 | (modify-syntax-entry ?/ ". 14b" st) | |
65 | (modify-syntax-entry ?* ". 23b" st) | |
66 | st) | |
1d928d69 | 67 | "Syntax table used while in Asm mode.") |
0231f2dc JB |
68 | |
69 | (defvar asm-mode-abbrev-table nil | |
1d928d69 | 70 | "Abbrev table used while in Asm mode.") |
0231f2dc JB |
71 | (define-abbrev-table 'asm-mode-abbrev-table ()) |
72 | ||
c7d565f4 SM |
73 | (defvar asm-mode-map |
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) | |
dfc42f38 SM |
78 | (define-key map "\C-j" 'newline-and-indent) |
79 | (define-key map "\C-m" 'newline-and-indent) | |
c7d565f4 | 80 | map) |
1d928d69 | 81 | "Keymap for Asm mode.") |
0231f2dc | 82 | |
a4d8ed20 | 83 | (defconst asm-font-lock-keywords |
769c4c63 | 84 | '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\(\\.\\sw+\\)*\\)?" |
a4d8ed20 | 85 | (1 font-lock-function-name-face) (3 font-lock-keyword-face nil t)) |
c1b55932 MY |
86 | ;; label started from ".". |
87 | ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>:" | |
88 | 1 font-lock-function-name-face) | |
89 | ("^\\((\\sw+)\\)?\\s +\\(\\(\\.?\\sw\\|\\s_\\)+\\(\\.\\sw+\\)*\\)" | |
90 | 2 font-lock-keyword-face) | |
91 | ;; directive started from ".". | |
92 | ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>[^:]?" | |
f4733d22 NR |
93 | 1 font-lock-keyword-face) |
94 | ;; %register | |
95 | ("%\\sw+" . font-lock-variable-name-face)) | |
a4d8ed20 RS |
96 | "Additional expressions to highlight in Assembler mode.") |
97 | ||
0231f2dc JB |
98 | ;;;###autoload |
99 | (defun asm-mode () | |
100 | "Major mode for editing typical assembler code. | |
1d928d69 | 101 | Features a private abbrev table and the following bindings: |
0231f2dc JB |
102 | |
103 | \\[asm-colon]\toutdent a preceding label, tab to next tab stop. | |
104 | \\[tab-to-tab-stop]\ttab to next tab stop. | |
105 | \\[asm-newline]\tnewline, then tab to next tab stop. | |
106 | \\[asm-comment]\tsmart placement of assembler comments. | |
107 | ||
108 | The character used for making comments is set by the variable | |
1ce965fe | 109 | `asm-comment-char' (which defaults to `?\\;'). |
0231f2dc | 110 | |
148d2a8d RS |
111 | Alternatively, you may set this variable in `asm-mode-set-comment-hook', |
112 | which is called near the beginning of mode initialization. | |
0231f2dc | 113 | |
1d928d69 | 114 | Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization. |
0231f2dc | 115 | |
18e3ab41 | 116 | Special commands: |
c7d565f4 | 117 | \\{asm-mode-map}" |
0231f2dc JB |
118 | (interactive) |
119 | (kill-all-local-variables) | |
0231f2dc JB |
120 | (setq mode-name "Assembler") |
121 | (setq major-mode 'asm-mode) | |
122 | (setq local-abbrev-table asm-mode-abbrev-table) | |
788a3a2b SM |
123 | (make-local-variable 'font-lock-defaults) |
124 | (setq font-lock-defaults '(asm-font-lock-keywords)) | |
dfc42f38 SM |
125 | (set (make-local-variable 'indent-line-function) 'asm-indent-line) |
126 | ;; Stay closer to the old TAB behavior (was tab-to-tab-stop). | |
127 | (set (make-local-variable 'tab-always-indent) nil) | |
f45a220a | 128 | |
0231f2dc | 129 | (run-hooks 'asm-mode-set-comment-hook) |
f45a220a RS |
130 | ;; Make our own local child of asm-mode-map |
131 | ;; so we can define our own comment character. | |
132 | (use-local-map (nconc (make-sparse-keymap) asm-mode-map)) | |
133 | (local-set-key (vector asm-comment-char) 'asm-comment) | |
c7d565f4 SM |
134 | (set-syntax-table (make-syntax-table asm-mode-syntax-table)) |
135 | (modify-syntax-entry asm-comment-char "<") | |
136 | ||
137 | (make-local-variable 'comment-start) | |
dfc42f38 SM |
138 | (setq comment-start (string asm-comment-char)) |
139 | (make-local-variable 'comment-add) | |
140 | (setq comment-add 1) | |
c7d565f4 SM |
141 | (make-local-variable 'comment-start-skip) |
142 | (setq comment-start-skip "\\(?:\\s<+\\|/\\*+\\)[ \t]*") | |
143 | (make-local-variable 'comment-end-skip) | |
dfc42f38 | 144 | (setq comment-end-skip "[ \t]*\\(\\s>\\|\\*+/\\)") |
0231f2dc JB |
145 | (make-local-variable 'comment-end) |
146 | (setq comment-end "") | |
0231f2dc | 147 | (setq fill-prefix "\t") |
dfc42f38 SM |
148 | (run-mode-hooks 'asm-mode-hook)) |
149 | ||
150 | (defun asm-indent-line () | |
151 | "Auto-indent the current line." | |
152 | (interactive) | |
153 | (let* ((savep (point)) | |
154 | (indent (condition-case nil | |
155 | (save-excursion | |
156 | (forward-line 0) | |
157 | (skip-chars-forward " \t") | |
158 | (if (>= (point) savep) (setq savep nil)) | |
159 | (max (asm-calculate-indentation) 0)) | |
160 | (error 0)))) | |
161 | (if savep | |
162 | (save-excursion (indent-line-to indent)) | |
163 | (indent-line-to indent)))) | |
164 | ||
165 | (defun asm-calculate-indentation () | |
166 | (or | |
167 | ;; Flush labels to the left margin. | |
168 | (and (looking-at "\\(\\sw\\|\\s_\\)+:") 0) | |
169 | ;; Same thing for `;;;' comments. | |
170 | (and (looking-at "\\s<\\s<\\s<") 0) | |
171 | ;; Simple `;' comments go to the comment-column. | |
172 | (and (looking-at "\\s<\\(\\S<\\|\\'\\)") comment-column) | |
173 | ;; The rest goes at the first tab stop. | |
174 | (or (car tab-stop-list) tab-width))) | |
175 | ||
0231f2dc JB |
176 | (defun asm-colon () |
177 | "Insert a colon; if it follows a label, delete the label's indentation." | |
178 | (interactive) | |
dfc42f38 SM |
179 | (let ((labelp nil)) |
180 | (save-excursion | |
181 | (skip-syntax-backward "w_") | |
182 | (skip-syntax-backward " ") | |
183 | (if (setq labelp (bolp)) (delete-horizontal-space))) | |
184 | (call-interactively 'self-insert-command) | |
185 | (when labelp | |
186 | (delete-horizontal-space) | |
187 | (tab-to-tab-stop)))) | |
188 | ||
189 | ;; Obsolete since Emacs-21.4. | |
190 | (defalias 'asm-newline 'newline-and-indent) | |
0231f2dc JB |
191 | |
192 | (defun asm-comment () | |
193 | "Convert an empty comment to a `larger' kind, or start a new one. | |
194 | These are the known comment classes: | |
195 | ||
196 | 1 -- comment to the right of the code (at the comment-column) | |
197 | 2 -- comment on its own line, indented like code | |
198 | 3 -- comment on its own line, beginning at the left-most column. | |
199 | ||
200 | Suggested usage: while writing your code, trigger asm-comment | |
201 | repeatedly until you are satisfied with the kind of comment." | |
202 | (interactive) | |
dfc42f38 SM |
203 | (comment-normalize-vars) |
204 | (let (comempty comment) | |
205 | (save-excursion | |
206 | (beginning-of-line) | |
207 | (setq comment (comment-search-forward (line-end-position) t)) | |
208 | (setq comempty (looking-at "[ \t]*$"))) | |
209 | ||
0231f2dc JB |
210 | (cond |
211 | ||
212 | ;; Blank line? Then start comment at code indent level. | |
dfc42f38 SM |
213 | ;; Just like `comment-dwim'. -stef |
214 | ((save-excursion (beginning-of-line) (looking-at "^[ \t]*$")) | |
215 | (indent-according-to-mode) | |
216 | (insert asm-comment-char asm-comment-char ?\ )) | |
217 | ||
218 | ;; Nonblank line w/o comment => start a comment at comment-column. | |
219 | ;; Also: point before the comment => jump inside. | |
220 | ((or (null comment) (< (point) comment)) | |
0231f2dc JB |
221 | (indent-for-comment)) |
222 | ||
dfc42f38 SM |
223 | ;; Flush-left or non-empty comment present => just insert character. |
224 | ((or (not comempty) (save-excursion (goto-char comment) (bolp))) | |
0231f2dc JB |
225 | (insert asm-comment-char)) |
226 | ||
dfc42f38 SM |
227 | ;; Empty code-level comment => upgrade to next comment level. |
228 | ((save-excursion (goto-char comment) (skip-chars-backward " \t") (bolp)) | |
229 | (goto-char comment) | |
230 | (insert asm-comment-char) | |
231 | (indent-for-comment)) | |
0231f2dc | 232 | |
dfc42f38 | 233 | ;; Empty comment ends non-empty code line => new comment above. |
0231f2dc | 234 | (t |
dfc42f38 SM |
235 | (goto-char comment) |
236 | (skip-chars-backward " \t") | |
237 | (delete-region (point) (line-end-position)) | |
238 | (beginning-of-line) (insert "\n") (backward-char) | |
239 | (asm-comment))))) | |
0231f2dc | 240 | |
896546cd RS |
241 | (provide 'asm-mode) |
242 | ||
ab5796a9 | 243 | ;;; arch-tag: 210e695f-f338-4376-8913-a4c5c72ac848 |
0231f2dc | 244 | ;;; asm-mode.el ends here |