.
[emacs.git] / lisp / progmodes / asm-mode.el
blob96b14596f6b629f548b9a3eb24bfd4f317e78dee
1 ;;; asm-mode.el --- mode for editing assembler code
3 ;; Copyright (C) 1991, 2003 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Maintainer: FSF
7 ;; Keywords: tools, languages
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
28 ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
29 ;; inspired by an earlier asm-mode by Martin Neitzel.
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:
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
43 ;; Code is indented to the first tab stop level.
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.
50 ;;; Code:
52 (defgroup asm nil
53 "Mode for editing assembler code."
54 :group 'languages)
56 (defcustom asm-comment-char ?\;
57 "*The comment-start character assumed by Asm mode."
58 :type 'character
59 :group 'asm)
61 (defvar asm-mode-syntax-table nil
62 "Syntax table used while in Asm mode.")
64 (defvar asm-mode-abbrev-table nil
65 "Abbrev table used while in Asm mode.")
66 (define-abbrev-table 'asm-mode-abbrev-table ())
68 (defvar asm-mode-map nil
69 "Keymap for Asm mode.")
71 (if asm-mode-map
72 nil
73 (setq asm-mode-map (make-sparse-keymap))
74 ;; Note that the comment character isn't set up until asm-mode is called.
75 (define-key asm-mode-map ":" 'asm-colon)
76 (define-key asm-mode-map "\C-c;" 'comment-region)
77 (define-key asm-mode-map "\C-i" 'tab-to-tab-stop)
78 (define-key asm-mode-map "\C-j" 'asm-newline)
79 (define-key asm-mode-map "\C-m" 'asm-newline)
82 (defconst asm-font-lock-keywords
83 '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\(\\.\\sw+\\)*\\)?"
84 (1 font-lock-function-name-face) (3 font-lock-keyword-face nil t))
85 ("^\\((\\sw+)\\)?\\s +\\(\\(\\sw\\|\\s_\\)+\\(\\.\\sw+\\)*\\)"
86 2 font-lock-keyword-face))
87 "Additional expressions to highlight in Assembler mode.")
89 (defvar asm-code-level-empty-comment-pattern nil)
90 (defvar asm-flush-left-empty-comment-pattern nil)
91 (defvar asm-inline-empty-comment-pattern nil)
93 ;;;###autoload
94 (defun asm-mode ()
95 "Major mode for editing typical assembler code.
96 Features a private abbrev table and the following bindings:
98 \\[asm-colon]\toutdent a preceding label, tab to next tab stop.
99 \\[tab-to-tab-stop]\ttab to next tab stop.
100 \\[asm-newline]\tnewline, then tab to next tab stop.
101 \\[asm-comment]\tsmart placement of assembler comments.
103 The character used for making comments is set by the variable
104 `asm-comment-char' (which defaults to `?\\;').
106 Alternatively, you may set this variable in `asm-mode-set-comment-hook',
107 which is called near the beginning of mode initialization.
109 Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
111 Special commands:
112 \\{asm-mode-map}
114 (interactive)
115 (kill-all-local-variables)
116 (setq mode-name "Assembler")
117 (setq major-mode 'asm-mode)
118 (setq local-abbrev-table asm-mode-abbrev-table)
119 (make-local-variable 'font-lock-defaults)
120 (setq font-lock-defaults '(asm-font-lock-keywords))
121 (make-local-variable 'asm-mode-syntax-table)
122 (setq asm-mode-syntax-table (make-syntax-table))
123 (set-syntax-table asm-mode-syntax-table)
125 (run-hooks 'asm-mode-set-comment-hook)
126 ;; Make our own local child of asm-mode-map
127 ;; so we can define our own comment character.
128 (use-local-map (nconc (make-sparse-keymap) asm-mode-map))
129 (local-set-key (vector asm-comment-char) 'asm-comment)
131 (modify-syntax-entry asm-comment-char
132 "< b" asm-mode-syntax-table)
133 (modify-syntax-entry ?\n
134 "> b" asm-mode-syntax-table)
136 (modify-syntax-entry ?/ ". 14" asm-mode-syntax-table)
137 (modify-syntax-entry ?* ". 23" asm-mode-syntax-table)
139 (let ((cs (regexp-quote (char-to-string asm-comment-char))))
140 (make-local-variable 'comment-start)
141 (setq comment-start (concat (char-to-string asm-comment-char) " "))
142 (make-local-variable 'comment-start-skip)
143 (setq comment-start-skip (concat cs "+[ \t]*"))
144 (setq asm-inline-empty-comment-pattern (concat "^.+" cs "+ *$"))
145 (setq asm-code-level-empty-comment-pattern (concat "^[\t ]+" cs cs " *$"))
146 (setq asm-flush-left-empty-comment-pattern (concat "^" cs cs cs " *$"))
148 (make-local-variable 'comment-end)
149 (setq comment-end "")
150 (setq fill-prefix "\t")
151 (run-hooks 'asm-mode-hook))
153 (defun asm-colon ()
154 "Insert a colon; if it follows a label, delete the label's indentation."
155 (interactive)
156 (save-excursion
157 (beginning-of-line)
158 (if (looking-at "[ \t]+\\(\\sw\\|\\s_\\)+$")
159 (delete-horizontal-space)))
160 (insert ":")
161 (tab-to-tab-stop)
164 (defun asm-newline ()
165 "Insert LFD + fill-prefix, to bring us back to code-indent level."
166 (interactive)
167 (if (eolp) (delete-horizontal-space))
168 (insert "\n")
169 (tab-to-tab-stop)
172 (defun asm-line-matches (pattern &optional withcomment)
173 (save-excursion
174 (beginning-of-line)
175 (looking-at pattern)))
177 (defun asm-pop-comment-level ()
178 ;; Delete an empty comment ending current line. Then set up for a new one,
179 ;; on the current line if it was all comment, otherwise above it
180 (end-of-line)
181 (delete-horizontal-space)
182 (while (= (preceding-char) asm-comment-char)
183 (delete-backward-char 1))
184 (delete-horizontal-space)
185 (if (bolp)
187 (beginning-of-line)
188 (open-line 1))
192 (defun asm-comment ()
193 "Convert an empty comment to a `larger' kind, or start a new one.
194 These are the known comment classes:
196 1 -- comment to the right of the code (at the comment-column)
197 2 -- comment on its own line, indented like code
198 3 -- comment on its own line, beginning at the left-most column.
200 Suggested usage: while writing your code, trigger asm-comment
201 repeatedly until you are satisfied with the kind of comment."
202 (interactive)
203 (cond
205 ;; Blank line? Then start comment at code indent level.
206 ((asm-line-matches "^[ \t]*$")
207 (delete-horizontal-space)
208 (tab-to-tab-stop)
209 (insert asm-comment-char comment-start))
211 ;; Nonblank line with no comment chars in it?
212 ;; Then start a comment at the current comment column
213 ((asm-line-matches (format "^[^%c\n]+$" asm-comment-char))
214 (indent-for-comment))
216 ;; Flush-left comment present? Just insert character.
217 ((asm-line-matches asm-flush-left-empty-comment-pattern)
218 (insert asm-comment-char))
220 ;; Empty code-level comment already present?
221 ;; Then start flush-left comment, on line above if this one is nonempty.
222 ((asm-line-matches asm-code-level-empty-comment-pattern)
223 (asm-pop-comment-level)
224 (insert asm-comment-char asm-comment-char comment-start))
226 ;; Empty comment ends line?
227 ;; Then make code-level comment, on line above if this one is nonempty.
228 ((asm-line-matches asm-inline-empty-comment-pattern)
229 (asm-pop-comment-level)
230 (tab-to-tab-stop)
231 (insert asm-comment-char comment-start))
233 ;; If all else fails, insert character
235 (insert asm-comment-char))
238 (end-of-line))
240 (provide 'asm-mode)
242 ;;; asm-mode.el ends here