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