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