Mercurial > emacs
annotate lisp/progmodes/asm-mode.el @ 88303:e70350ac56a3
*** empty log message ***
author | Henrik Enberg <henrik.enberg@telia.com> |
---|---|
date | Mon, 30 Jan 2006 11:31:22 +0000 |
parents | d7ddb3e565de |
children |
rev | line source |
---|---|
810
80303373daae
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
803
diff
changeset
|
1 ;;; asm-mode.el --- mode for editing assembler code |
80303373daae
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
803
diff
changeset
|
2 |
88155 | 3 ;; Copyright (C) 1991, 2001, 2002, 2003, 2004, 2005 |
4 ;; Free Software Foundation, Inc. | |
869 | 5 |
810
80303373daae
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
803
diff
changeset
|
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com> |
11098 | 7 ;; Maintainer: FSF |
810
80303373daae
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
803
diff
changeset
|
8 ;; Keywords: tools, languages |
80303373daae
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
803
diff
changeset
|
9 |
473 | 10 ;; This file is part of GNU Emacs. |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
869 | 14 ;; the Free Software Foundation; either version 2, or (at your option) |
473 | 15 ;; any later version. |
16 | |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
14169 | 23 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
88155 | 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02110-1301, USA. | |
473 | 26 |
810
80303373daae
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
803
diff
changeset
|
27 ;;; Commentary: |
80303373daae
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
803
diff
changeset
|
28 |
80303373daae
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
803
diff
changeset
|
29 ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>, |
473 | 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 | |
11105
8963d6de6f07
(asm-mode): Make a per-buffer child keymap here.
Richard M. Stallman <rms@gnu.org>
parents:
11098
diff
changeset
|
38 ;; comment char place or move comment |
8963d6de6f07
(asm-mode): Make a per-buffer child keymap here.
Richard M. Stallman <rms@gnu.org>
parents:
11098
diff
changeset
|
39 ;; asm-comment-char specifies which character this is; |
8963d6de6f07
(asm-mode): Make a per-buffer child keymap here.
Richard M. Stallman <rms@gnu.org>
parents:
11098
diff
changeset
|
40 ;; you can use a different character in different |
8963d6de6f07
(asm-mode): Make a per-buffer child keymap here.
Richard M. Stallman <rms@gnu.org>
parents:
11098
diff
changeset
|
41 ;; Asm mode buffers. |
473 | 42 ;; C-j, C-m newline and tab to tab stop |
43 ;; | |
44 ;; Code is indented to the first tab stop level. | |
810
80303373daae
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
803
diff
changeset
|
45 |
473 | 46 ;; This mode runs two hooks: |
11098 | 47 ;; 1) An asm-mode-set-comment-hook before the part of the initialization |
473 | 48 ;; depending on asm-comment-char, and |
49 ;; 2) an asm-mode-hook at the end of initialization. | |
50 | |
810
80303373daae
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
803
diff
changeset
|
51 ;;; Code: |
80303373daae
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
803
diff
changeset
|
52 |
20781 | 53 (defgroup asm nil |
54 "Mode for editing assembler code." | |
88155 | 55 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces) |
20781 | 56 :group 'languages) |
57 | |
23312 | 58 (defcustom asm-comment-char ?\; |
20781 | 59 "*The comment-start character assumed by Asm mode." |
60 :type 'character | |
61 :group 'asm) | |
473 | 62 |
88155 | 63 (defvar asm-mode-syntax-table |
64 (let ((st (make-syntax-table))) | |
65 (modify-syntax-entry ?\n ">" st) | |
66 (modify-syntax-entry ?/ ". 14b" st) | |
67 (modify-syntax-entry ?* ". 23b" st) | |
68 st) | |
2670 | 69 "Syntax table used while in Asm mode.") |
473 | 70 |
71 (defvar asm-mode-abbrev-table nil | |
2670 | 72 "Abbrev table used while in Asm mode.") |
473 | 73 (define-abbrev-table 'asm-mode-abbrev-table ()) |
74 | |
88155 | 75 (defvar asm-mode-map |
76 (let ((map (make-sparse-keymap))) | |
77 ;; Note that the comment character isn't set up until asm-mode is called. | |
78 (define-key map ":" 'asm-colon) | |
79 (define-key map "\C-c;" 'comment-region) | |
80 (define-key map "\C-j" 'newline-and-indent) | |
81 (define-key map "\C-m" 'newline-and-indent) | |
82 map) | |
2670 | 83 "Keymap for Asm mode.") |
473 | 84 |
9385
297f0781c8ae
(asm-font-lock-keywords): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
7384
diff
changeset
|
85 (defconst asm-font-lock-keywords |
88155 | 86 (append |
87 '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\(\\.\\sw+\\)*\\)?" | |
88 (1 font-lock-function-name-face) (3 font-lock-keyword-face nil t)) | |
89 ;; label started from ".". | |
90 ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>:" | |
91 1 font-lock-function-name-face) | |
92 ("^\\((\\sw+)\\)?\\s +\\(\\(\\.?\\sw\\|\\s_\\)+\\(\\.\\sw+\\)*\\)" | |
93 2 font-lock-keyword-face) | |
94 ;; directive started from ".". | |
95 ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>[^:]?" | |
96 1 font-lock-keyword-face) | |
97 ;; %register | |
98 ("%\\sw+" . font-lock-variable-name-face)) | |
99 cpp-font-lock-keywords) | |
100 "Additional expressions to highlight in Assembler mode.") | |
473 | 101 |
102 ;;;###autoload | |
103 (defun asm-mode () | |
104 "Major mode for editing typical assembler code. | |
2670 | 105 Features a private abbrev table and the following bindings: |
473 | 106 |
107 \\[asm-colon]\toutdent a preceding label, tab to next tab stop. | |
108 \\[tab-to-tab-stop]\ttab to next tab stop. | |
109 \\[asm-newline]\tnewline, then tab to next tab stop. | |
110 \\[asm-comment]\tsmart placement of assembler comments. | |
111 | |
112 The character used for making comments is set by the variable | |
23312 | 113 `asm-comment-char' (which defaults to `?\\;'). |
473 | 114 |
11098 | 115 Alternatively, you may set this variable in `asm-mode-set-comment-hook', |
116 which is called near the beginning of mode initialization. | |
473 | 117 |
2670 | 118 Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization. |
473 | 119 |
6469 | 120 Special commands: |
88155 | 121 \\{asm-mode-map}" |
473 | 122 (interactive) |
123 (kill-all-local-variables) | |
124 (setq mode-name "Assembler") | |
125 (setq major-mode 'asm-mode) | |
126 (setq local-abbrev-table asm-mode-abbrev-table) | |
9472
06623e3543db
* asm-mode.el: (asm-mode): Set font-lock-defaults.
Simon Marshall <simon@gnu.org>
parents:
9385
diff
changeset
|
127 (make-local-variable 'font-lock-defaults) |
06623e3543db
* asm-mode.el: (asm-mode): Set font-lock-defaults.
Simon Marshall <simon@gnu.org>
parents:
9385
diff
changeset
|
128 (setq font-lock-defaults '(asm-font-lock-keywords)) |
88155 | 129 (set (make-local-variable 'indent-line-function) 'asm-indent-line) |
130 ;; Stay closer to the old TAB behavior (was tab-to-tab-stop). | |
131 (set (make-local-variable 'tab-always-indent) nil) | |
11105
8963d6de6f07
(asm-mode): Make a per-buffer child keymap here.
Richard M. Stallman <rms@gnu.org>
parents:
11098
diff
changeset
|
132 |
473 | 133 (run-hooks 'asm-mode-set-comment-hook) |
11105
8963d6de6f07
(asm-mode): Make a per-buffer child keymap here.
Richard M. Stallman <rms@gnu.org>
parents:
11098
diff
changeset
|
134 ;; Make our own local child of asm-mode-map |
8963d6de6f07
(asm-mode): Make a per-buffer child keymap here.
Richard M. Stallman <rms@gnu.org>
parents:
11098
diff
changeset
|
135 ;; so we can define our own comment character. |
8963d6de6f07
(asm-mode): Make a per-buffer child keymap here.
Richard M. Stallman <rms@gnu.org>
parents:
11098
diff
changeset
|
136 (use-local-map (nconc (make-sparse-keymap) asm-mode-map)) |
8963d6de6f07
(asm-mode): Make a per-buffer child keymap here.
Richard M. Stallman <rms@gnu.org>
parents:
11098
diff
changeset
|
137 (local-set-key (vector asm-comment-char) 'asm-comment) |
88155 | 138 (set-syntax-table (make-syntax-table asm-mode-syntax-table)) |
139 (modify-syntax-entry asm-comment-char "<") | |
49402
99a31f21f1c9
(asm-mode): Added syntax table entries for /* */ comments.
Juanma Barranquero <lekktu@gmail.com>
parents:
31983
diff
changeset
|
140 |
88155 | 141 (make-local-variable 'comment-start) |
142 (setq comment-start (string asm-comment-char)) | |
143 (make-local-variable 'comment-add) | |
144 (setq comment-add 1) | |
145 (make-local-variable 'comment-start-skip) | |
146 (setq comment-start-skip "\\(?:\\s<+\\|/\\*+\\)[ \t]*") | |
147 (make-local-variable 'comment-end-skip) | |
148 (setq comment-end-skip "[ \t]*\\(\\s>\\|\\*+/\\)") | |
473 | 149 (make-local-variable 'comment-end) |
150 (setq comment-end "") | |
151 (setq fill-prefix "\t") | |
88155 | 152 (run-mode-hooks 'asm-mode-hook)) |
153 | |
154 (defun asm-indent-line () | |
155 "Auto-indent the current line." | |
156 (interactive) | |
157 (let* ((savep (point)) | |
158 (indent (condition-case nil | |
159 (save-excursion | |
160 (forward-line 0) | |
161 (skip-chars-forward " \t") | |
162 (if (>= (point) savep) (setq savep nil)) | |
163 (max (asm-calculate-indentation) 0)) | |
164 (error 0)))) | |
165 (if savep | |
166 (save-excursion (indent-line-to indent)) | |
167 (indent-line-to indent)))) | |
168 | |
169 (defun asm-calculate-indentation () | |
170 (or | |
171 ;; Flush labels to the left margin. | |
172 (and (looking-at "\\(\\sw\\|\\s_\\)+:") 0) | |
173 ;; Same thing for `;;;' comments. | |
174 (and (looking-at "\\s<\\s<\\s<") 0) | |
175 ;; Simple `;' comments go to the comment-column. | |
176 (and (looking-at "\\s<\\(\\S<\\|\\'\\)") comment-column) | |
177 ;; The rest goes at the first tab stop. | |
178 (or (car tab-stop-list) tab-width))) | |
179 | |
473 | 180 (defun asm-colon () |
181 "Insert a colon; if it follows a label, delete the label's indentation." | |
182 (interactive) | |
88155 | 183 (let ((labelp nil)) |
184 (save-excursion | |
185 (skip-syntax-backward "w_") | |
186 (skip-syntax-backward " ") | |
187 (if (setq labelp (bolp)) (delete-horizontal-space))) | |
188 (call-interactively 'self-insert-command) | |
189 (when labelp | |
190 (delete-horizontal-space) | |
191 (tab-to-tab-stop)))) | |
473 | 192 |
88155 | 193 ;; Obsolete since Emacs-22.1. |
194 (defalias 'asm-newline 'newline-and-indent) | |
473 | 195 |
196 (defun asm-comment () | |
197 "Convert an empty comment to a `larger' kind, or start a new one. | |
198 These are the known comment classes: | |
199 | |
200 1 -- comment to the right of the code (at the comment-column) | |
201 2 -- comment on its own line, indented like code | |
202 3 -- comment on its own line, beginning at the left-most column. | |
203 | |
204 Suggested usage: while writing your code, trigger asm-comment | |
205 repeatedly until you are satisfied with the kind of comment." | |
206 (interactive) | |
88155 | 207 (comment-normalize-vars) |
208 (let (comempty comment) | |
209 (save-excursion | |
210 (beginning-of-line) | |
211 (with-no-warnings | |
212 (setq comment (comment-search-forward (line-end-position) t))) | |
213 (setq comempty (looking-at "[ \t]*$"))) | |
214 | |
473 | 215 (cond |
216 | |
217 ;; Blank line? Then start comment at code indent level. | |
88155 | 218 ;; Just like `comment-dwim'. -stef |
219 ((save-excursion (beginning-of-line) (looking-at "^[ \t]*$")) | |
220 (indent-according-to-mode) | |
221 (insert asm-comment-char asm-comment-char ?\ )) | |
473 | 222 |
88155 | 223 ;; Nonblank line w/o comment => start a comment at comment-column. |
224 ;; Also: point before the comment => jump inside. | |
225 ((or (null comment) (< (point) comment)) | |
473 | 226 (indent-for-comment)) |
227 | |
88155 | 228 ;; Flush-left or non-empty comment present => just insert character. |
229 ((or (not comempty) (save-excursion (goto-char comment) (bolp))) | |
473 | 230 (insert asm-comment-char)) |
231 | |
88155 | 232 ;; Empty code-level comment => upgrade to next comment level. |
233 ((save-excursion (goto-char comment) (skip-chars-backward " \t") (bolp)) | |
234 (goto-char comment) | |
235 (insert asm-comment-char) | |
236 (indent-for-comment)) | |
473 | 237 |
88155 | 238 ;; Empty comment ends non-empty code line => new comment above. |
473 | 239 (t |
88155 | 240 (goto-char comment) |
241 (skip-chars-backward " \t") | |
242 (delete-region (point) (line-end-position)) | |
243 (beginning-of-line) (insert "\n") (backward-char) | |
244 (asm-comment))))) | |
473 | 245 |
18383 | 246 (provide 'asm-mode) |
247 | |
88155 | 248 ;;; arch-tag: 210e695f-f338-4376-8913-a4c5c72ac848 |
473 | 249 ;;; asm-mode.el ends here |