(mh-forward): Cosmetics on prompt when draft exists.
[bpt/emacs.git] / lisp / progmodes / asm-mode.el
CommitLineData
f961a17c
ER
1;;; asm-mode.el --- mode for editing assembler code
2
034babe1
NR
3;; Copyright (C) 1991, 2001, 2002, 2003, 2004, 2005
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
1bd60093 14;; the Free Software Foundation; either version 2, 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)))
65 (modify-syntax-entry ?\n ">" st)
66 (modify-syntax-entry ?/ ". 14b" st)
67 (modify-syntax-entry ?* ". 23b" st)
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)
c7d565f4 82 map)
1d928d69 83 "Keymap for Asm mode.")
0231f2dc 84
a4d8ed20 85(defconst asm-font-lock-keywords
769c4c63 86 '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\(\\.\\sw+\\)*\\)?"
a4d8ed20 87 (1 font-lock-function-name-face) (3 font-lock-keyword-face nil t))
c1b55932
MY
88 ;; label started from ".".
89 ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>:"
90 1 font-lock-function-name-face)
91 ("^\\((\\sw+)\\)?\\s +\\(\\(\\.?\\sw\\|\\s_\\)+\\(\\.\\sw+\\)*\\)"
92 2 font-lock-keyword-face)
93 ;; directive started from ".".
94 ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>[^:]?"
f4733d22
NR
95 1 font-lock-keyword-face)
96 ;; %register
97 ("%\\sw+" . font-lock-variable-name-face))
a4d8ed20
RS
98 "Additional expressions to highlight in Assembler mode.")
99
0231f2dc
JB
100;;;###autoload
101(defun asm-mode ()
102 "Major mode for editing typical assembler code.
1d928d69 103Features a private abbrev table and the following bindings:
0231f2dc
JB
104
105\\[asm-colon]\toutdent a preceding label, tab to next tab stop.
106\\[tab-to-tab-stop]\ttab to next tab stop.
107\\[asm-newline]\tnewline, then tab to next tab stop.
108\\[asm-comment]\tsmart placement of assembler comments.
109
110The character used for making comments is set by the variable
1ce965fe 111`asm-comment-char' (which defaults to `?\\;').
0231f2dc 112
148d2a8d
RS
113Alternatively, you may set this variable in `asm-mode-set-comment-hook',
114which is called near the beginning of mode initialization.
0231f2dc 115
1d928d69 116Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
0231f2dc 117
18e3ab41 118Special commands:
c7d565f4 119\\{asm-mode-map}"
0231f2dc
JB
120 (interactive)
121 (kill-all-local-variables)
0231f2dc
JB
122 (setq mode-name "Assembler")
123 (setq major-mode 'asm-mode)
124 (setq local-abbrev-table asm-mode-abbrev-table)
788a3a2b
SM
125 (make-local-variable 'font-lock-defaults)
126 (setq font-lock-defaults '(asm-font-lock-keywords))
dfc42f38
SM
127 (set (make-local-variable 'indent-line-function) 'asm-indent-line)
128 ;; Stay closer to the old TAB behavior (was tab-to-tab-stop).
129 (set (make-local-variable 'tab-always-indent) nil)
f45a220a 130
0231f2dc 131 (run-hooks 'asm-mode-set-comment-hook)
f45a220a
RS
132 ;; Make our own local child of asm-mode-map
133 ;; so we can define our own comment character.
134 (use-local-map (nconc (make-sparse-keymap) asm-mode-map))
135 (local-set-key (vector asm-comment-char) 'asm-comment)
c7d565f4
SM
136 (set-syntax-table (make-syntax-table asm-mode-syntax-table))
137 (modify-syntax-entry asm-comment-char "<")
138
139 (make-local-variable 'comment-start)
dfc42f38
SM
140 (setq comment-start (string asm-comment-char))
141 (make-local-variable 'comment-add)
142 (setq comment-add 1)
c7d565f4
SM
143 (make-local-variable 'comment-start-skip)
144 (setq comment-start-skip "\\(?:\\s<+\\|/\\*+\\)[ \t]*")
145 (make-local-variable 'comment-end-skip)
dfc42f38 146 (setq comment-end-skip "[ \t]*\\(\\s>\\|\\*+/\\)")
0231f2dc
JB
147 (make-local-variable 'comment-end)
148 (setq comment-end "")
0231f2dc 149 (setq fill-prefix "\t")
dfc42f38
SM
150 (run-mode-hooks 'asm-mode-hook))
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
0231f2dc
JB
178(defun asm-colon ()
179 "Insert a colon; if it follows a label, delete the label's indentation."
180 (interactive)
dfc42f38
SM
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
bf247b6e 191;; Obsolete since Emacs-22.1.
dfc42f38 192(defalias 'asm-newline 'newline-and-indent)
0231f2dc
JB
193
194(defun asm-comment ()
195 "Convert an empty comment to a `larger' kind, or start a new one.
196These 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
202Suggested usage: while writing your code, trigger asm-comment
203repeatedly until you are satisfied with the kind of comment."
204 (interactive)
dfc42f38
SM
205 (comment-normalize-vars)
206 (let (comempty comment)
207 (save-excursion
208 (beginning-of-line)
434fc2d3
RS
209 (with-no-warnings
210 (setq comment (comment-search-forward (line-end-position) t)))
dfc42f38
SM
211 (setq comempty (looking-at "[ \t]*$")))
212
0231f2dc
JB
213 (cond
214
215 ;; Blank line? Then start comment at code indent level.
dfc42f38
SM
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))
0231f2dc
JB
224 (indent-for-comment))
225
dfc42f38
SM
226 ;; Flush-left or non-empty comment present => just insert character.
227 ((or (not comempty) (save-excursion (goto-char comment) (bolp)))
0231f2dc
JB
228 (insert asm-comment-char))
229
dfc42f38
SM
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))
0231f2dc 235
dfc42f38 236 ;; Empty comment ends non-empty code line => new comment above.
0231f2dc 237 (t
dfc42f38
SM
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)))))
0231f2dc 243
896546cd
RS
244(provide 'asm-mode)
245
ab5796a9 246;;; arch-tag: 210e695f-f338-4376-8913-a4c5c72ac848
0231f2dc 247;;; asm-mode.el ends here