0
|
1 ;;; asm-mode.el --- mode for editing assembler code
|
|
2
|
|
3 ;; Copyright (C) 1991 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
|
|
6 ;; Maintainer: FSF
|
|
7 ;; Keywords: tools, languages
|
|
8
|
|
9 ;; This file is part of XEmacs.
|
|
10
|
|
11 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
12 ;; 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.
|
|
15
|
|
16 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
2
|
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
24 ;; 02111-1307, USA.
|
0
|
25
|
2
|
26 ;;; Synched up with: FSF 19.34.
|
0
|
27
|
|
28 ;;; Commentary:
|
|
29
|
|
30 ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
|
|
31 ;; inspired by an earlier asm-mode by Martin Neitzel.
|
|
32
|
|
33 ;; This minor mode is based on text mode. It defines a private abbrev table
|
|
34 ;; that can be used to save abbrevs for assembler mnemonics. It binds just
|
|
35 ;; five keys:
|
|
36 ;;
|
|
37 ;; TAB tab to next tab stop
|
|
38 ;; : outdent preceding label, tab to tab stop
|
|
39 ;; comment char place or move comment
|
|
40 ;; asm-comment-char specifies which character this is;
|
|
41 ;; you can use a different character in different
|
|
42 ;; Asm mode buffers.
|
|
43 ;; C-j, C-m newline and tab to tab stop
|
|
44 ;;
|
|
45 ;; Code is indented to the first tab stop level.
|
|
46
|
|
47 ;; This mode runs two hooks:
|
|
48 ;; 1) An asm-mode-set-comment-hook before the part of the initialization
|
|
49 ;; depending on asm-comment-char, and
|
|
50 ;; 2) an asm-mode-hook at the end of initialization.
|
|
51
|
|
52 ;;; Code:
|
|
53
|
|
54 (defvar asm-comment-char ?;
|
|
55 "*The comment-start character assumed by Asm mode.")
|
|
56
|
2
|
57 ;; XEmacs change (This is the primary difference, why was this
|
|
58 ;; feature removed? -sb)
|
0
|
59 (defvar asm-support-c-comments-p t
|
|
60 "*Support C style comments. If t C style comments will be
|
|
61 supported. This is mainly for the benefit of font-lock.")
|
|
62
|
|
63 (defvar asm-mode-syntax-table nil
|
|
64 "Syntax table used while in Asm mode.")
|
|
65
|
|
66 (defvar asm-mode-abbrev-table nil
|
|
67 "Abbrev table used while in Asm mode.")
|
|
68 (define-abbrev-table 'asm-mode-abbrev-table ())
|
|
69
|
|
70 (defvar asm-mode-map nil
|
|
71 "Keymap for Asm mode.")
|
|
72
|
|
73 (if asm-mode-map
|
|
74 nil
|
2
|
75 ;; XEmacs change
|
0
|
76 (setq asm-mode-map (make-sparse-keymap 'asm-mode-map))
|
|
77 ;; Note that the comment character isn't set up until asm-mode is called.
|
|
78 (define-key asm-mode-map ":" 'asm-colon)
|
2
|
79 (define-key asm-mode-map "\C-c;" 'comment-region)
|
0
|
80 (define-key asm-mode-map "\C-i" 'tab-to-tab-stop)
|
|
81 (define-key asm-mode-map "\C-j" 'asm-newline)
|
|
82 (define-key asm-mode-map "\C-m" 'asm-newline)
|
|
83 )
|
|
84
|
|
85 (defconst asm-font-lock-keywords
|
|
86 '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\)?"
|
|
87 (1 font-lock-function-name-face) (3 font-lock-keyword-face nil t))
|
|
88 ("^\\s +\\(\\(\\sw\\|\\s_\\)+\\)" 1 font-lock-keyword-face))
|
|
89 "Additional expressions to highlight in Assembler mode.")
|
|
90
|
2
|
91 ;; XEmacs change
|
0
|
92 (put 'asm-mode 'font-lock-defaults '(asm-font-lock-keywords))
|
2
|
93 (defvar asm-code-level-empty-comment-pattern nil)
|
|
94 (defvar asm-flush-left-empty-comment-pattern nil)
|
|
95 (defvar asm-inline-empty-comment-pattern nil)
|
0
|
96
|
|
97 ;;;###autoload
|
|
98 (defun asm-mode ()
|
|
99 "Major mode for editing typical assembler code.
|
|
100 Features a private abbrev table and the following bindings:
|
|
101
|
|
102 \\[asm-colon]\toutdent a preceding label, tab to next tab stop.
|
|
103 \\[tab-to-tab-stop]\ttab to next tab stop.
|
|
104 \\[asm-newline]\tnewline, then tab to next tab stop.
|
|
105 \\[asm-comment]\tsmart placement of assembler comments.
|
|
106
|
|
107 The character used for making comments is set by the variable
|
|
108 `asm-comment-char' (which defaults to `?;').
|
|
109
|
|
110 Alternatively, you may set this variable in `asm-mode-set-comment-hook',
|
|
111 which is called near the beginning of mode initialization.
|
|
112
|
|
113 Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
|
|
114
|
2
|
115 Special commands:
|
|
116 \\{asm-mode-map}
|
0
|
117 "
|
|
118 (interactive)
|
|
119 (kill-all-local-variables)
|
|
120 (setq mode-name "Assembler")
|
|
121 (setq major-mode 'asm-mode)
|
|
122 (setq local-abbrev-table asm-mode-abbrev-table)
|
2
|
123 (make-local-variable 'font-lock-defaults)
|
|
124 (setq font-lock-defaults '(asm-font-lock-keywords))
|
0
|
125 (make-local-variable 'asm-mode-syntax-table)
|
|
126 (setq asm-mode-syntax-table (make-syntax-table))
|
|
127 (set-syntax-table asm-mode-syntax-table)
|
|
128
|
|
129 (run-hooks 'asm-mode-set-comment-hook)
|
|
130 ;; Make our own local child of asm-mode-map
|
|
131 ;; so we can define our own comment character.
|
2
|
132 ;; XEmacs change
|
0
|
133 (let ((ourmap (make-sparse-keymap)))
|
|
134 (set-keymap-parents ourmap (list asm-mode-map))
|
|
135 (use-local-map ourmap))
|
|
136 (local-set-key (vector asm-comment-char) 'asm-comment)
|
2
|
137 ;; XEmacs change
|
0
|
138 (if asm-support-c-comments-p
|
|
139 (progn
|
|
140 (modify-syntax-entry ?/ ". 14" asm-mode-syntax-table)
|
|
141 (modify-syntax-entry ?* ". 23" asm-mode-syntax-table)
|
|
142 (modify-syntax-entry asm-comment-char "< b" asm-mode-syntax-table)
|
|
143 (modify-syntax-entry ?\n "> b" asm-mode-syntax-table))
|
|
144 (progn
|
|
145 (modify-syntax-entry asm-comment-char
|
|
146 "<" asm-mode-syntax-table)
|
|
147 (modify-syntax-entry ?\n
|
|
148 ">" asm-mode-syntax-table)))
|
|
149 (let ((cs (regexp-quote (char-to-string asm-comment-char))))
|
|
150 (make-local-variable 'comment-start)
|
|
151 (setq comment-start (concat cs " "))
|
|
152 (make-local-variable 'comment-start-skip)
|
|
153 (setq comment-start-skip (concat cs "+[ \t]*"))
|
|
154 (setq asm-inline-empty-comment-pattern (concat "^.+" cs "+ *$"))
|
|
155 (setq asm-code-level-empty-comment-pattern (concat "^[\t ]+" cs cs " *$"))
|
|
156 (setq asm-flush-left-empty-comment-pattern (concat "^" cs cs cs " *$"))
|
|
157 )
|
|
158 (make-local-variable 'comment-end)
|
|
159 (setq comment-end "")
|
|
160 (make-local-variable 'comment-column)
|
|
161 (setq comment-column 32)
|
|
162 (setq fill-prefix "\t")
|
|
163 (run-hooks 'asm-mode-hook))
|
|
164
|
|
165 (defun asm-colon ()
|
|
166 "Insert a colon; if it follows a label, delete the label's indentation."
|
|
167 (interactive)
|
|
168 (save-excursion
|
|
169 (beginning-of-line)
|
|
170 (if (looking-at "[ \t]+\\(\\sw\\|\\s_\\)+$")
|
|
171 (delete-horizontal-space)))
|
|
172 (insert ":")
|
|
173 (tab-to-tab-stop)
|
|
174 )
|
|
175
|
|
176 (defun asm-newline ()
|
|
177 "Insert LFD + fill-prefix, to bring us back to code-indent level."
|
|
178 (interactive)
|
|
179 (if (eolp) (delete-horizontal-space))
|
|
180 (insert "\n")
|
|
181 (tab-to-tab-stop)
|
|
182 )
|
|
183
|
|
184 (defun asm-line-matches (pattern &optional withcomment)
|
|
185 (save-excursion
|
|
186 (beginning-of-line)
|
|
187 (looking-at pattern)))
|
|
188
|
|
189 (defun asm-pop-comment-level ()
|
|
190 ;; Delete an empty comment ending current line. Then set up for a new one,
|
|
191 ;; on the current line if it was all comment, otherwise above it
|
|
192 (end-of-line)
|
|
193 (delete-horizontal-space)
|
|
194 (while (= (preceding-char) asm-comment-char)
|
|
195 (delete-backward-char 1))
|
|
196 (delete-horizontal-space)
|
|
197 (if (bolp)
|
|
198 nil
|
|
199 (beginning-of-line)
|
|
200 (open-line 1))
|
|
201 )
|
|
202
|
|
203
|
|
204 (defun asm-comment ()
|
|
205 "Convert an empty comment to a `larger' kind, or start a new one.
|
|
206 These are the known comment classes:
|
|
207
|
|
208 1 -- comment to the right of the code (at the comment-column)
|
|
209 2 -- comment on its own line, indented like code
|
|
210 3 -- comment on its own line, beginning at the left-most column.
|
|
211
|
|
212 Suggested usage: while writing your code, trigger asm-comment
|
|
213 repeatedly until you are satisfied with the kind of comment."
|
|
214 (interactive)
|
|
215 (cond
|
|
216
|
|
217 ;; Blank line? Then start comment at code indent level.
|
|
218 ((asm-line-matches "^[ \t]*$")
|
|
219 (delete-horizontal-space)
|
|
220 (tab-to-tab-stop)
|
|
221 (insert asm-comment-char comment-start))
|
|
222
|
|
223 ;; Nonblank line with no comment chars in it?
|
|
224 ;; Then start a comment at the current comment column
|
|
225 ((asm-line-matches (format "^[^%c\n]+$" asm-comment-char))
|
|
226 (indent-for-comment))
|
|
227
|
|
228 ;; Flush-left comment present? Just insert character.
|
|
229 ((asm-line-matches asm-flush-left-empty-comment-pattern)
|
|
230 (insert asm-comment-char))
|
|
231
|
|
232 ;; Empty code-level comment already present?
|
|
233 ;; Then start flush-left comment, on line above if this one is nonempty.
|
|
234 ((asm-line-matches asm-code-level-empty-comment-pattern)
|
|
235 (asm-pop-comment-level)
|
|
236 (insert asm-comment-char asm-comment-char comment-start))
|
|
237
|
|
238 ;; Empty comment ends line?
|
|
239 ;; Then make code-level comment, on line above if this one is nonempty.
|
|
240 ((asm-line-matches asm-inline-empty-comment-pattern)
|
|
241 (asm-pop-comment-level)
|
|
242 (tab-to-tab-stop)
|
|
243 (insert asm-comment-char comment-start))
|
|
244
|
|
245 ;; If all else fails, insert character
|
|
246 (t
|
|
247 (insert asm-comment-char))
|
|
248
|
|
249 )
|
|
250 (end-of-line))
|
|
251
|
|
252 ;;; asm-mode.el ends here
|