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