Commit | Line | Data |
---|---|---|
f961a17c ER |
1 | ;;; asm-mode.el --- mode for editing assembler code |
2 | ||
1bd60093 ER |
3 | ;; Copyright (C) 1991 Free Software Foundation, Inc. |
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 JB |
60 | |
61 | (defvar asm-mode-syntax-table nil | |
1d928d69 | 62 | "Syntax table used while in Asm mode.") |
0231f2dc JB |
63 | |
64 | (defvar asm-mode-abbrev-table nil | |
1d928d69 | 65 | "Abbrev table used while in Asm mode.") |
0231f2dc JB |
66 | (define-abbrev-table 'asm-mode-abbrev-table ()) |
67 | ||
68 | (defvar asm-mode-map nil | |
1d928d69 | 69 | "Keymap for Asm mode.") |
0231f2dc JB |
70 | |
71 | (if asm-mode-map | |
72 | nil | |
73 | (setq asm-mode-map (make-sparse-keymap)) | |
f45a220a | 74 | ;; Note that the comment character isn't set up until asm-mode is called. |
0231f2dc | 75 | (define-key asm-mode-map ":" 'asm-colon) |
16bba9ca | 76 | (define-key asm-mode-map "\C-c;" 'comment-region) |
0231f2dc | 77 | (define-key asm-mode-map "\C-i" 'tab-to-tab-stop) |
f961a17c ER |
78 | (define-key asm-mode-map "\C-j" 'asm-newline) |
79 | (define-key asm-mode-map "\C-m" 'asm-newline) | |
80 | ) | |
0231f2dc | 81 | |
a4d8ed20 | 82 | (defconst asm-font-lock-keywords |
6f9a5d90 | 83 | '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\(\\.[lLwWbBsS]\\)?\\)?" |
a4d8ed20 | 84 | (1 font-lock-function-name-face) (3 font-lock-keyword-face nil t)) |
6f9a5d90 | 85 | ("^\\s +\\(\\(\\sw\\|\\s_\\)+\\(\\.[lLwWbBsS]\\)?\\)" 1 font-lock-keyword-face)) |
a4d8ed20 RS |
86 | "Additional expressions to highlight in Assembler mode.") |
87 | ||
0231f2dc JB |
88 | (defvar asm-code-level-empty-comment-pattern nil) |
89 | (defvar asm-flush-left-empty-comment-pattern nil) | |
90 | (defvar asm-inline-empty-comment-pattern nil) | |
91 | ||
92 | ;;;###autoload | |
93 | (defun asm-mode () | |
94 | "Major mode for editing typical assembler code. | |
1d928d69 | 95 | Features a private abbrev table and the following bindings: |
0231f2dc JB |
96 | |
97 | \\[asm-colon]\toutdent a preceding label, tab to next tab stop. | |
98 | \\[tab-to-tab-stop]\ttab to next tab stop. | |
99 | \\[asm-newline]\tnewline, then tab to next tab stop. | |
100 | \\[asm-comment]\tsmart placement of assembler comments. | |
101 | ||
102 | The character used for making comments is set by the variable | |
1ce965fe | 103 | `asm-comment-char' (which defaults to `?\\;'). |
0231f2dc | 104 | |
148d2a8d RS |
105 | Alternatively, you may set this variable in `asm-mode-set-comment-hook', |
106 | which is called near the beginning of mode initialization. | |
0231f2dc | 107 | |
1d928d69 | 108 | Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization. |
0231f2dc | 109 | |
18e3ab41 RS |
110 | Special commands: |
111 | \\{asm-mode-map} | |
0231f2dc JB |
112 | " |
113 | (interactive) | |
114 | (kill-all-local-variables) | |
0231f2dc JB |
115 | (setq mode-name "Assembler") |
116 | (setq major-mode 'asm-mode) | |
117 | (setq local-abbrev-table asm-mode-abbrev-table) | |
788a3a2b SM |
118 | (make-local-variable 'font-lock-defaults) |
119 | (setq font-lock-defaults '(asm-font-lock-keywords)) | |
0231f2dc JB |
120 | (make-local-variable 'asm-mode-syntax-table) |
121 | (setq asm-mode-syntax-table (make-syntax-table)) | |
122 | (set-syntax-table asm-mode-syntax-table) | |
f45a220a | 123 | |
0231f2dc | 124 | (run-hooks 'asm-mode-set-comment-hook) |
f45a220a RS |
125 | ;; Make our own local child of asm-mode-map |
126 | ;; so we can define our own comment character. | |
127 | (use-local-map (nconc (make-sparse-keymap) asm-mode-map)) | |
128 | (local-set-key (vector asm-comment-char) 'asm-comment) | |
129 | ||
0231f2dc JB |
130 | (modify-syntax-entry asm-comment-char |
131 | "<" asm-mode-syntax-table) | |
132 | (modify-syntax-entry ?\n | |
133 | ">" asm-mode-syntax-table) | |
134 | (let ((cs (regexp-quote (char-to-string asm-comment-char)))) | |
135 | (make-local-variable 'comment-start) | |
6f9a5d90 | 136 | (setq comment-start (concat (char-to-string asm-comment-char) " ")) |
0231f2dc JB |
137 | (make-local-variable 'comment-start-skip) |
138 | (setq comment-start-skip (concat cs "+[ \t]*")) | |
139 | (setq asm-inline-empty-comment-pattern (concat "^.+" cs "+ *$")) | |
140 | (setq asm-code-level-empty-comment-pattern (concat "^[\t ]+" cs cs " *$")) | |
141 | (setq asm-flush-left-empty-comment-pattern (concat "^" cs cs cs " *$")) | |
142 | ) | |
143 | (make-local-variable 'comment-end) | |
144 | (setq comment-end "") | |
145 | (make-local-variable 'comment-column) | |
146 | (setq comment-column 32) | |
0231f2dc | 147 | (setq fill-prefix "\t") |
f45a220a | 148 | (run-hooks 'asm-mode-hook)) |
0231f2dc JB |
149 | \f |
150 | (defun asm-colon () | |
151 | "Insert a colon; if it follows a label, delete the label's indentation." | |
152 | (interactive) | |
153 | (save-excursion | |
154 | (beginning-of-line) | |
155 | (if (looking-at "[ \t]+\\(\\sw\\|\\s_\\)+$") | |
156 | (delete-horizontal-space))) | |
157 | (insert ":") | |
158 | (tab-to-tab-stop) | |
159 | ) | |
160 | ||
161 | (defun asm-newline () | |
162 | "Insert LFD + fill-prefix, to bring us back to code-indent level." | |
163 | (interactive) | |
164 | (if (eolp) (delete-horizontal-space)) | |
165 | (insert "\n") | |
166 | (tab-to-tab-stop) | |
167 | ) | |
168 | ||
169 | (defun asm-line-matches (pattern &optional withcomment) | |
170 | (save-excursion | |
171 | (beginning-of-line) | |
172 | (looking-at pattern))) | |
173 | ||
174 | (defun asm-pop-comment-level () | |
175 | ;; Delete an empty comment ending current line. Then set up for a new one, | |
176 | ;; on the current line if it was all comment, otherwise above it | |
177 | (end-of-line) | |
178 | (delete-horizontal-space) | |
179 | (while (= (preceding-char) asm-comment-char) | |
180 | (delete-backward-char 1)) | |
181 | (delete-horizontal-space) | |
182 | (if (bolp) | |
183 | nil | |
184 | (beginning-of-line) | |
185 | (open-line 1)) | |
186 | ) | |
187 | ||
188 | ||
189 | (defun asm-comment () | |
190 | "Convert an empty comment to a `larger' kind, or start a new one. | |
191 | These are the known comment classes: | |
192 | ||
193 | 1 -- comment to the right of the code (at the comment-column) | |
194 | 2 -- comment on its own line, indented like code | |
195 | 3 -- comment on its own line, beginning at the left-most column. | |
196 | ||
197 | Suggested usage: while writing your code, trigger asm-comment | |
198 | repeatedly until you are satisfied with the kind of comment." | |
199 | (interactive) | |
200 | (cond | |
201 | ||
202 | ;; Blank line? Then start comment at code indent level. | |
203 | ((asm-line-matches "^[ \t]*$") | |
204 | (delete-horizontal-space) | |
205 | (tab-to-tab-stop) | |
206 | (insert asm-comment-char comment-start)) | |
207 | ||
208 | ;; Nonblank line with no comment chars in it? | |
209 | ;; Then start a comment at the current comment column | |
24768ef9 | 210 | ((asm-line-matches (format "^[^%c\n]+$" asm-comment-char)) |
0231f2dc JB |
211 | (indent-for-comment)) |
212 | ||
213 | ;; Flush-left comment present? Just insert character. | |
214 | ((asm-line-matches asm-flush-left-empty-comment-pattern) | |
215 | (insert asm-comment-char)) | |
216 | ||
217 | ;; Empty code-level comment already present? | |
218 | ;; Then start flush-left comment, on line above if this one is nonempty. | |
219 | ((asm-line-matches asm-code-level-empty-comment-pattern) | |
220 | (asm-pop-comment-level) | |
221 | (insert asm-comment-char asm-comment-char comment-start)) | |
222 | ||
223 | ;; Empty comment ends line? | |
224 | ;; Then make code-level comment, on line above if this one is nonempty. | |
225 | ((asm-line-matches asm-inline-empty-comment-pattern) | |
226 | (asm-pop-comment-level) | |
227 | (tab-to-tab-stop) | |
228 | (insert asm-comment-char comment-start)) | |
229 | ||
230 | ;; If all else fails, insert character | |
231 | (t | |
232 | (insert asm-comment-char)) | |
233 | ||
234 | ) | |
235 | (end-of-line)) | |
236 | ||
896546cd RS |
237 | (provide 'asm-mode) |
238 | ||
0231f2dc | 239 | ;;; asm-mode.el ends here |