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