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