Add arch taglines
[bpt/emacs.git] / lisp / progmodes / asm-mode.el
1 ;;; asm-mode.el --- mode for editing assembler code
2
3 ;; Copyright (C) 1991, 2003 Free Software Foundation, Inc.
4
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Maintainer: FSF
7 ;; Keywords: tools, languages
8
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
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 the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
29 ;; inspired by an earlier asm-mode by Martin Neitzel.
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
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.
41 ;; C-j, C-m newline and tab to tab stop
42 ;;
43 ;; Code is indented to the first tab stop level.
44
45 ;; This mode runs two hooks:
46 ;; 1) An asm-mode-set-comment-hook before the part of the initialization
47 ;; depending on asm-comment-char, and
48 ;; 2) an asm-mode-hook at the end of initialization.
49
50 ;;; Code:
51
52 (defgroup asm nil
53 "Mode for editing assembler code."
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 ">" st)
64 (modify-syntax-entry ?/ ". 14b" st)
65 (modify-syntax-entry ?* ". 23b" 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 map)
81 "Keymap for Asm mode.")
82
83 (defconst asm-font-lock-keywords
84 '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\(\\.\\sw+\\)*\\)?"
85 (1 font-lock-function-name-face) (3 font-lock-keyword-face nil t))
86 ;; label started from ".".
87 ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>:"
88 1 font-lock-function-name-face)
89 ("^\\((\\sw+)\\)?\\s +\\(\\(\\.?\\sw\\|\\s_\\)+\\(\\.\\sw+\\)*\\)"
90 2 font-lock-keyword-face)
91 ;; directive started from ".".
92 ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>[^:]?"
93 1 font-lock-keyword-face))
94 "Additional expressions to highlight in Assembler mode.")
95
96 ;;;###autoload
97 (defun asm-mode ()
98 "Major mode for editing typical assembler code.
99 Features a private abbrev table and the following bindings:
100
101 \\[asm-colon]\toutdent a preceding label, tab to next tab stop.
102 \\[tab-to-tab-stop]\ttab to next tab stop.
103 \\[asm-newline]\tnewline, then tab to next tab stop.
104 \\[asm-comment]\tsmart placement of assembler comments.
105
106 The character used for making comments is set by the variable
107 `asm-comment-char' (which defaults to `?\\;').
108
109 Alternatively, you may set this variable in `asm-mode-set-comment-hook',
110 which is called near the beginning of mode initialization.
111
112 Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
113
114 Special commands:
115 \\{asm-mode-map}"
116 (interactive)
117 (kill-all-local-variables)
118 (setq mode-name "Assembler")
119 (setq major-mode 'asm-mode)
120 (setq local-abbrev-table asm-mode-abbrev-table)
121 (make-local-variable 'font-lock-defaults)
122 (setq font-lock-defaults '(asm-font-lock-keywords))
123 (set (make-local-variable 'indent-line-function) 'asm-indent-line)
124 ;; Stay closer to the old TAB behavior (was tab-to-tab-stop).
125 (set (make-local-variable 'tab-always-indent) nil)
126
127 (run-hooks 'asm-mode-set-comment-hook)
128 ;; Make our own local child of asm-mode-map
129 ;; so we can define our own comment character.
130 (use-local-map (nconc (make-sparse-keymap) asm-mode-map))
131 (local-set-key (vector asm-comment-char) 'asm-comment)
132 (set-syntax-table (make-syntax-table asm-mode-syntax-table))
133 (modify-syntax-entry asm-comment-char "<")
134
135 (make-local-variable 'comment-start)
136 (setq comment-start (string asm-comment-char))
137 (make-local-variable 'comment-add)
138 (setq comment-add 1)
139 (make-local-variable 'comment-start-skip)
140 (setq comment-start-skip "\\(?:\\s<+\\|/\\*+\\)[ \t]*")
141 (make-local-variable 'comment-end-skip)
142 (setq comment-end-skip "[ \t]*\\(\\s>\\|\\*+/\\)")
143 (make-local-variable 'comment-end)
144 (setq comment-end "")
145 (setq fill-prefix "\t")
146 (run-mode-hooks 'asm-mode-hook))
147
148 (defun asm-indent-line ()
149 "Auto-indent the current line."
150 (interactive)
151 (let* ((savep (point))
152 (indent (condition-case nil
153 (save-excursion
154 (forward-line 0)
155 (skip-chars-forward " \t")
156 (if (>= (point) savep) (setq savep nil))
157 (max (asm-calculate-indentation) 0))
158 (error 0))))
159 (if savep
160 (save-excursion (indent-line-to indent))
161 (indent-line-to indent))))
162
163 (defun asm-calculate-indentation ()
164 (or
165 ;; Flush labels to the left margin.
166 (and (looking-at "\\(\\sw\\|\\s_\\)+:") 0)
167 ;; Same thing for `;;;' comments.
168 (and (looking-at "\\s<\\s<\\s<") 0)
169 ;; Simple `;' comments go to the comment-column.
170 (and (looking-at "\\s<\\(\\S<\\|\\'\\)") comment-column)
171 ;; The rest goes at the first tab stop.
172 (or (car tab-stop-list) tab-width)))
173
174 (defun asm-colon ()
175 "Insert a colon; if it follows a label, delete the label's indentation."
176 (interactive)
177 (let ((labelp nil))
178 (save-excursion
179 (skip-syntax-backward "w_")
180 (skip-syntax-backward " ")
181 (if (setq labelp (bolp)) (delete-horizontal-space)))
182 (call-interactively 'self-insert-command)
183 (when labelp
184 (delete-horizontal-space)
185 (tab-to-tab-stop))))
186
187 ;; Obsolete since Emacs-21.4.
188 (defalias 'asm-newline 'newline-and-indent)
189
190 (defun asm-comment ()
191 "Convert an empty comment to a `larger' kind, or start a new one.
192 These are the known comment classes:
193
194 1 -- comment to the right of the code (at the comment-column)
195 2 -- comment on its own line, indented like code
196 3 -- comment on its own line, beginning at the left-most column.
197
198 Suggested usage: while writing your code, trigger asm-comment
199 repeatedly until you are satisfied with the kind of comment."
200 (interactive)
201 (comment-normalize-vars)
202 (let (comempty comment)
203 (save-excursion
204 (beginning-of-line)
205 (setq comment (comment-search-forward (line-end-position) t))
206 (setq comempty (looking-at "[ \t]*$")))
207
208 (cond
209
210 ;; Blank line? Then start comment at code indent level.
211 ;; Just like `comment-dwim'. -stef
212 ((save-excursion (beginning-of-line) (looking-at "^[ \t]*$"))
213 (indent-according-to-mode)
214 (insert asm-comment-char asm-comment-char ?\ ))
215
216 ;; Nonblank line w/o comment => start a comment at comment-column.
217 ;; Also: point before the comment => jump inside.
218 ((or (null comment) (< (point) comment))
219 (indent-for-comment))
220
221 ;; Flush-left or non-empty comment present => just insert character.
222 ((or (not comempty) (save-excursion (goto-char comment) (bolp)))
223 (insert asm-comment-char))
224
225 ;; Empty code-level comment => upgrade to next comment level.
226 ((save-excursion (goto-char comment) (skip-chars-backward " \t") (bolp))
227 (goto-char comment)
228 (insert asm-comment-char)
229 (indent-for-comment))
230
231 ;; Empty comment ends non-empty code line => new comment above.
232 (t
233 (goto-char comment)
234 (skip-chars-backward " \t")
235 (delete-region (point) (line-end-position))
236 (beginning-of-line) (insert "\n") (backward-char)
237 (asm-comment)))))
238
239 (provide 'asm-mode)
240
241 ;;; arch-tag: 210e695f-f338-4376-8913-a4c5c72ac848
242 ;;; asm-mode.el ends here