Warn against using `length' to compute display width of a string.
[emacs.git] / lisp / progmodes / asm-mode.el
blobcfa977d90225f27e93b4c87861711675acbc0df9
1 ;;; asm-mode.el --- mode for editing assembler code
3 ;; Copyright (C) 1991, 2001-2012 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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
27 ;; inspired by an earlier asm-mode by Martin Neitzel.
29 ;; This minor mode is based on text mode. It defines a private abbrev table
30 ;; that can be used to save abbrevs for assembler mnemonics. It binds just
31 ;; five keys:
33 ;; TAB tab to next tab stop
34 ;; : outdent preceding label, tab to tab stop
35 ;; comment char place or move comment
36 ;; asm-comment-char specifies which character this is;
37 ;; you can use a different character in different
38 ;; Asm mode buffers.
39 ;; C-j, C-m newline and tab to tab stop
41 ;; Code is indented to the first tab stop level.
43 ;; This mode runs two hooks:
44 ;; 1) An asm-mode-set-comment-hook before the part of the initialization
45 ;; depending on asm-comment-char, and
46 ;; 2) an asm-mode-hook at the end of initialization.
48 ;;; Code:
50 (defgroup asm nil
51 "Mode for editing assembler code."
52 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
53 :group 'languages)
55 (defcustom asm-comment-char ?\;
56 "*The comment-start character assumed by Asm mode."
57 :type 'character
58 :group 'asm)
60 (defvar asm-mode-syntax-table
61 (let ((st (make-syntax-table)))
62 (modify-syntax-entry ?\n "> b" st)
63 (modify-syntax-entry ?/ ". 124b" st)
64 (modify-syntax-entry ?* ". 23" st)
65 st)
66 "Syntax table used while in Asm mode.")
68 (defvar asm-mode-abbrev-table nil
69 "Abbrev table used while in Asm mode.")
70 (define-abbrev-table 'asm-mode-abbrev-table ())
72 (defvar asm-mode-map
73 (let ((map (make-sparse-keymap)))
74 ;; Note that the comment character isn't set up until asm-mode is called.
75 (define-key map ":" 'asm-colon)
76 (define-key map "\C-c;" 'comment-region)
77 (define-key map "\C-j" 'newline-and-indent)
78 (define-key map "\C-m" 'newline-and-indent)
79 (define-key map [menu-bar asm-mode] (cons "Asm" (make-sparse-keymap)))
80 (define-key map [menu-bar asm-mode comment-region]
81 '(menu-item "Comment Region" comment-region
82 :help "Comment or uncomment each line in the region"))
83 (define-key map [menu-bar asm-mode newline-and-indent]
84 '(menu-item "Insert Newline and Indent" newline-and-indent
85 :help "Insert a newline, then indent according to major mode"))
86 (define-key map [menu-bar asm-mode asm-colon]
87 '(menu-item "Insert Colon" asm-colon
88 :help "Insert a colon; if it follows a label, delete the label's indentation"))
89 map)
90 "Keymap for Asm mode.")
92 (defconst asm-font-lock-keywords
93 (append
94 '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\(\\.\\sw+\\)*\\)?"
95 (1 font-lock-function-name-face) (3 font-lock-keyword-face nil t))
96 ;; label started from ".".
97 ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>:"
98 1 font-lock-function-name-face)
99 ("^\\((\\sw+)\\)?\\s +\\(\\(\\.?\\sw\\|\\s_\\)+\\(\\.\\sw+\\)*\\)"
100 2 font-lock-keyword-face)
101 ;; directive started from ".".
102 ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>[^:]?"
103 1 font-lock-keyword-face)
104 ;; %register
105 ("%\\sw+" . font-lock-variable-name-face))
106 cpp-font-lock-keywords)
107 "Additional expressions to highlight in Assembler mode.")
109 ;;;###autoload
110 (define-derived-mode asm-mode prog-mode "Assembler"
111 "Major mode for editing typical assembler code.
112 Features a private abbrev table and the following bindings:
114 \\[asm-colon]\toutdent a preceding label, tab to next tab stop.
115 \\[tab-to-tab-stop]\ttab to next tab stop.
116 \\[asm-newline]\tnewline, then tab to next tab stop.
117 \\[asm-comment]\tsmart placement of assembler comments.
119 The character used for making comments is set by the variable
120 `asm-comment-char' (which defaults to `?\\;').
122 Alternatively, you may set this variable in `asm-mode-set-comment-hook',
123 which is called near the beginning of mode initialization.
125 Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
127 Special commands:
128 \\{asm-mode-map}"
129 (setq local-abbrev-table asm-mode-abbrev-table)
130 (set (make-local-variable 'font-lock-defaults) '(asm-font-lock-keywords))
131 (set (make-local-variable 'indent-line-function) 'asm-indent-line)
132 ;; Stay closer to the old TAB behavior (was tab-to-tab-stop).
133 (set (make-local-variable 'tab-always-indent) nil)
135 (run-hooks 'asm-mode-set-comment-hook)
136 ;; Make our own local child of asm-mode-map
137 ;; so we can define our own comment character.
138 (use-local-map (nconc (make-sparse-keymap) asm-mode-map))
139 (local-set-key (vector asm-comment-char) 'asm-comment)
140 (set-syntax-table (make-syntax-table asm-mode-syntax-table))
141 (modify-syntax-entry asm-comment-char "< b")
143 (set (make-local-variable 'comment-start) (string asm-comment-char))
144 (set (make-local-variable 'comment-add) 1)
145 (set (make-local-variable 'comment-start-skip)
146 "\\(?:\\s<+\\|/[/*]+\\)[ \t]*")
147 (set (make-local-variable 'comment-end-skip) "[ \t]*\\(\\s>\\|\\*+/\\)")
148 (set (make-local-variable 'comment-end) "")
149 (setq fill-prefix "\t"))
151 (defun asm-indent-line ()
152 "Auto-indent the current line."
153 (interactive)
154 (let* ((savep (point))
155 (indent (condition-case nil
156 (save-excursion
157 (forward-line 0)
158 (skip-chars-forward " \t")
159 (if (>= (point) savep) (setq savep nil))
160 (max (asm-calculate-indentation) 0))
161 (error 0))))
162 (if savep
163 (save-excursion (indent-line-to indent))
164 (indent-line-to indent))))
166 (defun asm-calculate-indentation ()
168 ;; Flush labels to the left margin.
169 (and (looking-at "\\(\\sw\\|\\s_\\)+:") 0)
170 ;; Same thing for `;;;' comments.
171 (and (looking-at "\\s<\\s<\\s<") 0)
172 ;; Simple `;' comments go to the comment-column.
173 (and (looking-at "\\s<\\(\\S<\\|\\'\\)") comment-column)
174 ;; The rest goes at the first tab stop.
175 (or (car tab-stop-list) tab-width)))
177 (defun asm-colon ()
178 "Insert a colon; if it follows a label, delete the label's indentation."
179 (interactive)
180 (let ((labelp nil))
181 (save-excursion
182 (skip-syntax-backward "w_")
183 (skip-syntax-backward " ")
184 (if (setq labelp (bolp)) (delete-horizontal-space)))
185 (call-interactively 'self-insert-command)
186 (when labelp
187 (delete-horizontal-space)
188 (tab-to-tab-stop))))
190 ;; Obsolete since Emacs-22.1.
191 (defalias 'asm-newline 'newline-and-indent)
193 (defun asm-comment ()
194 "Convert an empty comment to a `larger' kind, or start a new one.
195 These are the known comment classes:
197 1 -- comment to the right of the code (at the comment-column)
198 2 -- comment on its own line, indented like code
199 3 -- comment on its own line, beginning at the left-most column.
201 Suggested usage: while writing your code, trigger asm-comment
202 repeatedly until you are satisfied with the kind of comment."
203 (interactive)
204 (comment-normalize-vars)
205 (let (comempty comment)
206 (save-excursion
207 (beginning-of-line)
208 (with-no-warnings
209 (setq comment (comment-search-forward (line-end-position) t)))
210 (setq comempty (looking-at "[ \t]*$")))
212 (cond
214 ;; Blank line? Then start comment at code indent level.
215 ;; Just like `comment-dwim'. -stef
216 ((save-excursion (beginning-of-line) (looking-at "^[ \t]*$"))
217 (indent-according-to-mode)
218 (insert asm-comment-char asm-comment-char ?\ ))
220 ;; Nonblank line w/o comment => start a comment at comment-column.
221 ;; Also: point before the comment => jump inside.
222 ((or (null comment) (< (point) comment))
223 (indent-for-comment))
225 ;; Flush-left or non-empty comment present => just insert character.
226 ((or (not comempty) (save-excursion (goto-char comment) (bolp)))
227 (insert asm-comment-char))
229 ;; Empty code-level comment => upgrade to next comment level.
230 ((save-excursion (goto-char comment) (skip-chars-backward " \t") (bolp))
231 (goto-char comment)
232 (insert asm-comment-char)
233 (indent-for-comment))
235 ;; Empty comment ends non-empty code line => new comment above.
237 (goto-char comment)
238 (skip-chars-backward " \t")
239 (delete-region (point) (line-end-position))
240 (beginning-of-line) (insert "\n") (backward-char)
241 (asm-comment)))))
243 (provide 'asm-mode)
245 ;;; asm-mode.el ends here