1 ;;; asm-mode.el --- mode for editing assembler code
5 ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
6 ;; inspired by an earlier asm-mode by Martin Neitzel.
8 ;; This minor mode is based on text mode. It defines a private abbrev table
9 ;; that can be used to save abbrevs for assembler mnemonics. It binds just
12 ;; TAB tab to next tab stop
13 ;; : outdent preceding label, tab to tab stop
14 ;; comment char place or move comment
15 ;; asm-comment-char specifies which character this is;
16 ;; you can use a different character in different
18 ;; C-j, C-m newline and tab to tab stop
20 ;; Code is indented to the first tab stop level.
22 ;; This mode runs two hooks:
23 ;; 1) An asm-mode-set-comment-hook before the part of the initialization
24 ;; depending on asm-comment-char, and
25 ;; 2) an asm-mode-hook at the end of initialization.
30 "X16 Assembler programming"
34 (defcustom asm-comment-char ?*
35 "*The comment-start character assumed by Asm mode."
39 ;; XEmacs change (This is the primary difference, why was this
40 ;; feature removed? -sb)
41 (defcustom asm-support-c-comments-p nil
42 "*Support C style comments. If t C style comments will be
43 supported. This is mainly for the benefit of font-lock."
47 (defcustom asm-mode-syntax-table nil
48 "Syntax table used while in Asm mode.")
50 (defvar asm-mode-abbrev-table nil
51 "Abbrev table used while in Asm mode.")
52 (define-abbrev-table 'asm-mode-abbrev-table ())
54 (defvar asm-mode-map nil
55 "Keymap for Asm mode.")
60 (setq asm-mode-map (make-sparse-keymap 'asm-mode-map))
61 ;; Note that the comment character isn't set up until asm-mode is called.
62 (define-key asm-mode-map ":" 'asm-colon)
63 (define-key asm-mode-map "\C-h" 'asm-backspace)
64 (define-key asm-mode-map "\C-l" 'asm-halfdelim)
65 (define-key asm-mode-map "\M-l" 'asm-fulldelim)
66 (define-key asm-mode-map "\C-i" 'asm-tabulator)
67 ;;(define-key asm-mode-map "\C-a" 'asm-comment-region)
68 (define-key asm-mode-map "\C-a" 'beginning-of-line)
69 (define-key asm-mode-map "\C-j" 'asm-newline)
70 (define-key asm-mode-map "\C-m" 'asm-newline)
73 (defconst asm-font-lock-keywords
74 '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\)?"
75 (1 font-lock-function-name-face) (3 font-lock-keyword-face nil t))
76 ("^\\s +\\(\\(\\sw\\|\\s_\\)+\\)" 1 font-lock-keyword-face))
78 "Additional expressions to highlight in Assembler mode.")
81 (put 'asm-mode 'font-lock-defaults '(asm-font-lock-keywords))
82 (defvar asm-code-level-empty-comment-pattern nil)
83 (defvar asm-flush-left-empty-comment-pattern nil)
84 (defvar asm-inline-empty-comment-pattern nil)
89 "Major mode for editing typical assembler code.
90 Features a private abbrev table and the following bindings:
92 \\[asm-colon]\toutdent a preceding label, tab to next tab stop.
93 \\[tab-to-tab-stop]\ttab to next tab stop.
94 \\[asm-newline]\tnewline, then tab to next tab stop.
95 \\[asm-comment]\tsmart placement of assembler comments.
97 The character used for making comments is set by the variable
98 `asm-comment-char' (which defaults to `?;').
100 Alternatively, you may set this variable in `asm-mode-set-comment-hook',
101 which is called near the beginning of mode initialization.
103 Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
110 ;; (make-local-variable 'asm-comment-space)
112 (setq asm-comment-space " ")
114 (kill-all-local-variables)
115 (setq mode-name "Assembler")
116 (setq major-mode 'asm-mode)
117 (setq local-abbrev-table asm-mode-abbrev-table)
118 (make-local-variable 'font-lock-defaults)
119 (setq font-lock-defaults '(asm-font-lock-keywords))
120 (make-local-variable 'asm-mode-syntax-table)
121 (setq asm-mode-syntax-table (make-syntax-table))
122 (set-syntax-table asm-mode-syntax-table)
124 (run-hooks 'asm-mode-set-comment-hook)
125 ;; Make our own local child of asm-mode-map
126 ;; so we can define our own comment character.
128 (let ((ourmap (make-sparse-keymap)))
129 (set-keymap-parents ourmap (list asm-mode-map))
130 (use-local-map ourmap))
131 (local-set-key (vector asm-comment-char) 'asm-comment)
135 (if asm-support-c-comments-p
137 (modify-syntax-entry ?/ ". 14" asm-mode-syntax-table)
138 (modify-syntax-entry ?* ". 23" asm-mode-syntax-table)
139 (modify-syntax-entry asm-comment-char "< b" asm-mode-syntax-table)
140 (modify-syntax-entry ?\n "> b" asm-mode-syntax-table))
142 (modify-syntax-entry asm-comment-char
143 "<" asm-mode-syntax-table)
144 (modify-syntax-entry ?\n
145 ">" asm-mode-syntax-table)))
148 (let ((cs (regexp-quote (char-to-string asm-comment-char))))
149 (make-local-variable 'comment-start)
150 (setq comment-start (concat cs asm-comment-space))
151 (make-local-variable 'comment-start-skip)
152 (setq comment-start-skip (concat cs "+[ \t]*"))
153 (setq asm-inline-empty-comment-pattern (concat "^.+" cs "+ *$"))
154 (setq asm-code-level-empty-comment-pattern (concat "^[\t ]+" cs cs " *$"))
155 (setq asm-flush-left-empty-comment-pattern (concat "^" cs cs cs " *$"))
157 (make-local-variable 'comment-end)
158 (setq comment-end "")
159 (make-local-variable 'comment-column)
160 (setq comment-column 32)
161 (setq fill-prefix "\t")
162 (run-hooks 'asm-mode-hook))
165 "Insert a colon; if it follows a label, delete the label's indentation."
167 (if (is-comment-line) (progn (asm-comment-line) (tab-to-tab-stop))
171 ( (save-excursion (beginning-of-line) (looking-at "\**[ \t]+\\(\\sw\\|\\s_\\)+$"))
172 (save-excursion (beginning-of-line) (delete-horizontal-space))
182 (defun asm-line-matches (pattern &optional withcomment)
185 (looking-at pattern)))
187 (defun asm-pop-comment-level ()
188 ;; Delete an empty comment ending current line. Then set up for a new one,
189 ;; on the current line if it was all comment, otherwise above it
191 (delete-horizontal-space)
192 (while (= (preceding-char) asm-comment-char)
193 (delete-backward-char 1))
194 (delete-horizontal-space)
202 (defun asm-comment ()
203 "Convert an empty comment to a `larger' kind, or start a new one.
204 These are the known comment classes:
206 1 -- comment to the right of the code (at the comment-column)
207 2 -- comment on its own line, indented like code
208 3 -- comment on its own line, beginning at the left-most column.
210 Suggested usage: while writing your code, trigger asm-comment
211 repeatedly until you are satisfied with the kind of comment."
214 (setq comment-start "")
218 ;; Blank line? Then start comment.
219 ((asm-line-matches "^[ \t]*$")
220 (delete-horizontal-space)
221 ;; (insert asm-comment-char comment-start)
222 (insert asm-comment-char asm-comment-space)
225 ;; Nonblank line with no comment chars in it?
226 ;; Then start a comment at the current comment column
227 ;;( (if (is-comment-line) nil t)
228 ;; (indent-for-comment)
231 ;; Flush-left comment present? Just insert character.
232 ;; ((asm-line-matches asm-flush-left-empty-comment-pattern)
233 ;; (insert asm-comment-char))
235 ;; Fresh comment line??
236 ( (asm-line-matches (format "^%c%s$" asm-comment-char asm-comment-space))
238 (delete-backward-char (length asm-comment-space))
239 (insert asm-comment-char asm-comment-char)
243 ;; Empty code-level comment already present?
244 ;; Then start flush-left comment, on line above if this one is nonempty.
245 ;; ((asm-line-matches asm-code-level-empty-comment-pattern)
246 ;; (asm-pop-comment-level)
247 ;; (insert asm-comment-char asm-comment-char comment-start))
249 ;; Empty comment ends line?
250 ;; Then make code-level comment, on line above if this one is nonempty.
251 ;; ((asm-line-matches asm-inline-empty-comment-pattern)
252 ;; (asm-pop-comment-level)
254 ;; (insert asm-comment-char comment-start))
256 ;; If all else fails, insert character
258 (insert asm-comment-char))
263 ;; ****************************************************************************
266 (defun pos-in-line ()
267 (- (point) (save-excursion (beginning-of-line) (point))))
270 (defun at-begin-of-line ()
275 (if (= pos (point)) t nil)
279 (defun is-comment-line ()
280 (if (asm-line-matches (format "^%c" asm-comment-char))
285 (defun in-comment-spacing ()
286 (if (> (current-column) (length (format "%c%s" asm-comment-char asm-comment-space))) nil t)
289 (defun asm-comment-line()
291 (if (is-comment-line)
295 (if (looking-at "^ ")
299 (save-excursion (beginning-of-line) (insert asm-comment-char asm-comment-space))
310 (defun asm-comment-region()
313 (if (not (region-active-p)) (asm-comment-line)
315 (goto-char (region-end))
316 (setq lastline (line-number))
317 (goto-char (region-beginning))
318 (while (< (line-number) lastline)
324 (defun asm-backspace ()
326 (if (and (is-comment-line) (in-comment-spacing))
327 (delete-region (- (line-start) 1) (point))
328 (delete-backward-char 1)
330 ;;delete-backward-char (length (format "%c%s" asm-comment-char asm-comment-space))) (tab-to-tab-stop))
331 ;; (delete-backward-char 1)
334 (defun asm-fulldelim ()
336 (make-local-variable 'count)
338 (if (asm-line-matches (format "^%c%s$" asm-comment-char asm-comment-space))
339 (progn (setq count 1)
341 (delete-backward-char (length asm-comment-space)))
345 (insert asm-comment-char)
346 (setq count ( 1+ count)))
350 (defun asm-halfdelim ()
352 (make-local-variable 'count)
354 (if (asm-line-matches (format "^%c%s$" asm-comment-char asm-comment-space))
355 (progn (setq count 1)
357 (delete-backward-char (length asm-comment-space)))
361 (insert asm-comment-char)
362 (setq count ( 1+ count)))
367 (defun asm-tabulator ()
369 (if (is-comment-line)
371 (setq target (prog2 (insert "\t") (current-column) (delete-backward-char 1) ))
372 (while (< (current-column) target) (insert " "))
380 (defun asm-newline ()
381 "Insert LFD + fill-prefix, to bring us back to code-indent level."
384 (if (is-comment-line)
386 (if (at-begin-of-line) (goto-char (+ (point) 1)))
387 (insert "\n" asm-comment-char asm-comment-space))
390 (progn (delete-horizontal-space)
402 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.[sS]\\'" . asm-mode))
403 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.asm\\'" . asm-mode))
405 ;;; asm-mode.el ends here