Mercurial > hg > xemacs-beta
comparison lisp/modes/asm-mode.el @ 0:376386a54a3c r19-14
Import from CVS: tag r19-14
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:45:50 +0200 |
parents | |
children | ac2d302a0011 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:376386a54a3c |
---|---|
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 | |
23 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | |
25 ;;; Synched up with: FSF 19.30. | |
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 (defvar asm-comment-char ?; | |
54 "*The comment-start character assumed by Asm mode.") | |
55 | |
56 (defvar asm-support-c-comments-p t | |
57 "*Support C style comments. If t C style comments will be | |
58 supported. This is mainly for the benefit of font-lock.") | |
59 | |
60 (defvar asm-mode-syntax-table nil | |
61 "Syntax table used while in Asm mode.") | |
62 | |
63 (defvar asm-mode-abbrev-table nil | |
64 "Abbrev table used while in Asm mode.") | |
65 (define-abbrev-table 'asm-mode-abbrev-table ()) | |
66 | |
67 (defvar asm-mode-map nil | |
68 "Keymap for Asm mode.") | |
69 | |
70 (if asm-mode-map | |
71 nil | |
72 (setq asm-mode-map (make-sparse-keymap 'asm-mode-map)) | |
73 ;; Note that the comment character isn't set up until asm-mode is called. | |
74 (define-key asm-mode-map ":" 'asm-colon) | |
75 (define-key asm-mode-map "\C-i" 'tab-to-tab-stop) | |
76 (define-key asm-mode-map "\C-j" 'asm-newline) | |
77 (define-key asm-mode-map "\C-m" 'asm-newline) | |
78 ) | |
79 | |
80 (defvar asm-code-level-empty-comment-pattern nil) | |
81 (defvar asm-flush-left-empty-comment-pattern nil) | |
82 (defvar asm-inline-empty-comment-pattern nil) | |
83 | |
84 (defconst asm-font-lock-keywords | |
85 '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\)?" | |
86 (1 font-lock-function-name-face) (3 font-lock-keyword-face nil t)) | |
87 ("^\\s +\\(\\(\\sw\\|\\s_\\)+\\)" 1 font-lock-keyword-face)) | |
88 "Additional expressions to highlight in Assembler mode.") | |
89 | |
90 (put 'asm-mode 'font-lock-defaults '(asm-font-lock-keywords)) | |
91 | |
92 ;;;###autoload | |
93 (defun asm-mode () | |
94 "Major mode for editing typical assembler code. | |
95 Features a private abbrev table and the following bindings: | |
96 | |
97 \\[asm-colon]\toutdent a preceding label, tab to next tab stop. | |
98 \\[tab-to-tab-stop]\ttab to next tab stop. | |
99 \\[asm-newline]\tnewline, then tab to next tab stop. | |
100 \\[asm-comment]\tsmart placement of assembler comments. | |
101 | |
102 The character used for making comments is set by the variable | |
103 `asm-comment-char' (which defaults to `?;'). | |
104 | |
105 Alternatively, you may set this variable in `asm-mode-set-comment-hook', | |
106 which is called near the beginning of mode initialization. | |
107 | |
108 Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization. | |
109 | |
110 Special commands:\\{asm-mode-map} | |
111 " | |
112 (interactive) | |
113 (kill-all-local-variables) | |
114 (setq mode-name "Assembler") | |
115 (setq major-mode 'asm-mode) | |
116 (setq local-abbrev-table asm-mode-abbrev-table) | |
117 (make-local-variable 'asm-mode-syntax-table) | |
118 (setq asm-mode-syntax-table (make-syntax-table)) | |
119 (set-syntax-table asm-mode-syntax-table) | |
120 | |
121 (run-hooks 'asm-mode-set-comment-hook) | |
122 ;; Make our own local child of asm-mode-map | |
123 ;; so we can define our own comment character. | |
124 (let ((ourmap (make-sparse-keymap))) | |
125 (set-keymap-parents ourmap (list asm-mode-map)) | |
126 (use-local-map ourmap)) | |
127 (local-set-key (vector asm-comment-char) 'asm-comment) | |
128 (if asm-support-c-comments-p | |
129 (progn | |
130 (modify-syntax-entry ?/ ". 14" asm-mode-syntax-table) | |
131 (modify-syntax-entry ?* ". 23" asm-mode-syntax-table) | |
132 (modify-syntax-entry asm-comment-char "< b" asm-mode-syntax-table) | |
133 (modify-syntax-entry ?\n "> b" asm-mode-syntax-table)) | |
134 (progn | |
135 (modify-syntax-entry asm-comment-char | |
136 "<" asm-mode-syntax-table) | |
137 (modify-syntax-entry ?\n | |
138 ">" 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 cs " ")) | |
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 " *$")) | |
147 ) | |
148 (make-local-variable 'comment-end) | |
149 (setq comment-end "") | |
150 (make-local-variable 'comment-column) | |
151 (setq comment-column 32) | |
152 (setq fill-prefix "\t") | |
153 (run-hooks 'asm-mode-hook)) | |
154 | |
155 (defun asm-colon () | |
156 "Insert a colon; if it follows a label, delete the label's indentation." | |
157 (interactive) | |
158 (save-excursion | |
159 (beginning-of-line) | |
160 (if (looking-at "[ \t]+\\(\\sw\\|\\s_\\)+$") | |
161 (delete-horizontal-space))) | |
162 (insert ":") | |
163 (tab-to-tab-stop) | |
164 ) | |
165 | |
166 (defun asm-newline () | |
167 "Insert LFD + fill-prefix, to bring us back to code-indent level." | |
168 (interactive) | |
169 (if (eolp) (delete-horizontal-space)) | |
170 (insert "\n") | |
171 (tab-to-tab-stop) | |
172 ) | |
173 | |
174 (defun asm-line-matches (pattern &optional withcomment) | |
175 (save-excursion | |
176 (beginning-of-line) | |
177 (looking-at pattern))) | |
178 | |
179 (defun asm-pop-comment-level () | |
180 ;; Delete an empty comment ending current line. Then set up for a new one, | |
181 ;; on the current line if it was all comment, otherwise above it | |
182 (end-of-line) | |
183 (delete-horizontal-space) | |
184 (while (= (preceding-char) asm-comment-char) | |
185 (delete-backward-char 1)) | |
186 (delete-horizontal-space) | |
187 (if (bolp) | |
188 nil | |
189 (beginning-of-line) | |
190 (open-line 1)) | |
191 ) | |
192 | |
193 | |
194 (defun asm-comment () | |
195 "Convert an empty comment to a `larger' kind, or start a new one. | |
196 These are the known comment classes: | |
197 | |
198 1 -- comment to the right of the code (at the comment-column) | |
199 2 -- comment on its own line, indented like code | |
200 3 -- comment on its own line, beginning at the left-most column. | |
201 | |
202 Suggested usage: while writing your code, trigger asm-comment | |
203 repeatedly until you are satisfied with the kind of comment." | |
204 (interactive) | |
205 (cond | |
206 | |
207 ;; Blank line? Then start comment at code indent level. | |
208 ((asm-line-matches "^[ \t]*$") | |
209 (delete-horizontal-space) | |
210 (tab-to-tab-stop) | |
211 (insert asm-comment-char comment-start)) | |
212 | |
213 ;; Nonblank line with no comment chars in it? | |
214 ;; Then start a comment at the current comment column | |
215 ((asm-line-matches (format "^[^%c\n]+$" asm-comment-char)) | |
216 (indent-for-comment)) | |
217 | |
218 ;; Flush-left comment present? Just insert character. | |
219 ((asm-line-matches asm-flush-left-empty-comment-pattern) | |
220 (insert asm-comment-char)) | |
221 | |
222 ;; Empty code-level comment already present? | |
223 ;; Then start flush-left comment, on line above if this one is nonempty. | |
224 ((asm-line-matches asm-code-level-empty-comment-pattern) | |
225 (asm-pop-comment-level) | |
226 (insert asm-comment-char asm-comment-char comment-start)) | |
227 | |
228 ;; Empty comment ends line? | |
229 ;; Then make code-level comment, on line above if this one is nonempty. | |
230 ((asm-line-matches asm-inline-empty-comment-pattern) | |
231 (asm-pop-comment-level) | |
232 (tab-to-tab-stop) | |
233 (insert asm-comment-char comment-start)) | |
234 | |
235 ;; If all else fails, insert character | |
236 (t | |
237 (insert asm-comment-char)) | |
238 | |
239 ) | |
240 (end-of-line)) | |
241 | |
242 ;;; asm-mode.el ends here |