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