]> code.delx.au - gnu-emacs/blob - lisp/progmodes/asm-mode.el
bc0275f5ecbc5f67b61a694398dc7e1e5ab302ce
[gnu-emacs] / lisp / progmodes / asm-mode.el
1 ;;; asm-mode.el --- mode for editing assembler code
2
3 ;; Copyright (C) 1991, 2001-2011
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Maintainer: FSF
8 ;; Keywords: tools, languages
9
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
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) 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
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
28 ;; inspired by an earlier asm-mode by Martin Neitzel.
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
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.
40 ;; C-j, C-m newline and tab to tab stop
41 ;;
42 ;; Code is indented to the first tab stop level.
43
44 ;; This mode runs two hooks:
45 ;; 1) An asm-mode-set-comment-hook before the part of the initialization
46 ;; depending on asm-comment-char, and
47 ;; 2) an asm-mode-hook at the end of initialization.
48
49 ;;; Code:
50
51 (defgroup asm nil
52 "Mode for editing assembler code."
53 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
54 :group 'languages)
55
56 (defcustom asm-comment-char ?\;
57 "*The comment-start character assumed by Asm mode."
58 :type 'character
59 :group 'asm)
60
61 (defvar asm-mode-syntax-table
62 (let ((st (make-syntax-table)))
63 (modify-syntax-entry ?\n "> b" st)
64 (modify-syntax-entry ?/ ". 124b" st)
65 (modify-syntax-entry ?* ". 23" st)
66 st)
67 "Syntax table used while in Asm mode.")
68
69 (defvar asm-mode-abbrev-table nil
70 "Abbrev table used while in Asm mode.")
71 (define-abbrev-table 'asm-mode-abbrev-table ())
72
73 (defvar asm-mode-map
74 (let ((map (make-sparse-keymap)))
75 ;; Note that the comment character isn't set up until asm-mode is called.
76 (define-key map ":" 'asm-colon)
77 (define-key map "\C-c;" 'comment-region)
78 (define-key map "\C-j" 'newline-and-indent)
79 (define-key map "\C-m" 'newline-and-indent)
80 (define-key map [menu-bar asm-mode] (cons "Asm" (make-sparse-keymap)))
81 (define-key map [menu-bar asm-mode comment-region]
82 '(menu-item "Comment Region" comment-region
83 :help "Comment or uncomment each line in the region"))
84 (define-key map [menu-bar asm-mode newline-and-indent]
85 '(menu-item "Insert Newline and Indent" newline-and-indent
86 :help "Insert a newline, then indent according to major mode"))
87 (define-key map [menu-bar asm-mode asm-colon]
88 '(menu-item "Insert Colon" asm-colon
89 :help "Insert a colon; if it follows a label, delete the label's indentation"))
90 map)
91 "Keymap for Asm mode.")
92
93 (defconst asm-font-lock-keywords
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.")
109
110 ;;;###autoload
111 (define-derived-mode asm-mode prog-mode "Assembler"
112 "Major mode for editing typical assembler code.
113 Features a private abbrev table and the following bindings:
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
121 `asm-comment-char' (which defaults to `?\\;').
122
123 Alternatively, you may set this variable in `asm-mode-set-comment-hook',
124 which is called near the beginning of mode initialization.
125
126 Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
127
128 Special commands:
129 \\{asm-mode-map}"
130 (setq local-abbrev-table asm-mode-abbrev-table)
131 (set (make-local-variable 'font-lock-defaults) '(asm-font-lock-keywords))
132 (set (make-local-variable 'indent-line-function) 'asm-indent-line)
133 ;; Stay closer to the old TAB behavior (was tab-to-tab-stop).
134 (set (make-local-variable 'tab-always-indent) nil)
135
136 (run-hooks 'asm-mode-set-comment-hook)
137 ;; Make our own local child of asm-mode-map
138 ;; so we can define our own comment character.
139 (use-local-map (nconc (make-sparse-keymap) asm-mode-map))
140 (local-set-key (vector asm-comment-char) 'asm-comment)
141 (set-syntax-table (make-syntax-table asm-mode-syntax-table))
142 (modify-syntax-entry asm-comment-char "< b")
143
144 (set (make-local-variable 'comment-start) (string asm-comment-char))
145 (set (make-local-variable 'comment-add) 1)
146 (set (make-local-variable 'comment-start-skip)
147 "\\(?:\\s<+\\|/[/*]+\\)[ \t]*")
148 (set (make-local-variable 'comment-end-skip) "[ \t]*\\(\\s>\\|\\*+/\\)")
149 (set (make-local-variable 'comment-end) "")
150 (setq fill-prefix "\t"))
151
152 (defun asm-indent-line ()
153 "Auto-indent the current line."
154 (interactive)
155 (let* ((savep (point))
156 (indent (condition-case nil
157 (save-excursion
158 (forward-line 0)
159 (skip-chars-forward " \t")
160 (if (>= (point) savep) (setq savep nil))
161 (max (asm-calculate-indentation) 0))
162 (error 0))))
163 (if savep
164 (save-excursion (indent-line-to indent))
165 (indent-line-to indent))))
166
167 (defun asm-calculate-indentation ()
168 (or
169 ;; Flush labels to the left margin.
170 (and (looking-at "\\(\\sw\\|\\s_\\)+:") 0)
171 ;; Same thing for `;;;' comments.
172 (and (looking-at "\\s<\\s<\\s<") 0)
173 ;; Simple `;' comments go to the comment-column.
174 (and (looking-at "\\s<\\(\\S<\\|\\'\\)") comment-column)
175 ;; The rest goes at the first tab stop.
176 (or (car tab-stop-list) tab-width)))
177
178 (defun asm-colon ()
179 "Insert a colon; if it follows a label, delete the label's indentation."
180 (interactive)
181 (let ((labelp nil))
182 (save-excursion
183 (skip-syntax-backward "w_")
184 (skip-syntax-backward " ")
185 (if (setq labelp (bolp)) (delete-horizontal-space)))
186 (call-interactively 'self-insert-command)
187 (when labelp
188 (delete-horizontal-space)
189 (tab-to-tab-stop))))
190
191 ;; Obsolete since Emacs-22.1.
192 (defalias 'asm-newline 'newline-and-indent)
193
194 (defun asm-comment ()
195 "Convert an empty comment to a `larger' kind, or start a new one.
196 These are the known comment classes:
197
198 1 -- comment to the right of the code (at the comment-column)
199 2 -- comment on its own line, indented like code
200 3 -- comment on its own line, beginning at the left-most column.
201
202 Suggested usage: while writing your code, trigger asm-comment
203 repeatedly until you are satisfied with the kind of comment."
204 (interactive)
205 (comment-normalize-vars)
206 (let (comempty comment)
207 (save-excursion
208 (beginning-of-line)
209 (with-no-warnings
210 (setq comment (comment-search-forward (line-end-position) t)))
211 (setq comempty (looking-at "[ \t]*$")))
212
213 (cond
214
215 ;; Blank line? Then start comment at code indent level.
216 ;; Just like `comment-dwim'. -stef
217 ((save-excursion (beginning-of-line) (looking-at "^[ \t]*$"))
218 (indent-according-to-mode)
219 (insert asm-comment-char asm-comment-char ?\ ))
220
221 ;; Nonblank line w/o comment => start a comment at comment-column.
222 ;; Also: point before the comment => jump inside.
223 ((or (null comment) (< (point) comment))
224 (indent-for-comment))
225
226 ;; Flush-left or non-empty comment present => just insert character.
227 ((or (not comempty) (save-excursion (goto-char comment) (bolp)))
228 (insert asm-comment-char))
229
230 ;; Empty code-level comment => upgrade to next comment level.
231 ((save-excursion (goto-char comment) (skip-chars-backward " \t") (bolp))
232 (goto-char comment)
233 (insert asm-comment-char)
234 (indent-for-comment))
235
236 ;; Empty comment ends non-empty code line => new comment above.
237 (t
238 (goto-char comment)
239 (skip-chars-backward " \t")
240 (delete-region (point) (line-end-position))
241 (beginning-of-line) (insert "\n") (backward-char)
242 (asm-comment)))))
243
244 (provide 'asm-mode)
245
246 ;;; asm-mode.el ends here