| 1 | ;;; asm-mode.el --- mode for editing assembler code |
| 2 | |
| 3 | ;;; Commentary: |
| 4 | |
| 5 | ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>, |
| 6 | ;; inspired by an earlier asm-mode by Martin Neitzel. |
| 7 | |
| 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 |
| 10 | ;; five keys: |
| 11 | ;; |
| 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 |
| 17 | ;; Asm mode buffers. |
| 18 | ;; C-j, C-m newline and tab to tab stop |
| 19 | ;; |
| 20 | ;; Code is indented to the first tab stop level. |
| 21 | |
| 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. |
| 26 | |
| 27 | ;;; Code: |
| 28 | |
| 29 | (defgroup asm nil |
| 30 | "X16 Assembler programming" |
| 31 | :group 'languages) |
| 32 | |
| 33 | |
| 34 | (defcustom asm-comment-char ?* |
| 35 | "*The comment-start character assumed by Asm mode." |
| 36 | :type 'sexp |
| 37 | :group 'asm) |
| 38 | |
| 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." |
| 44 | :type 'boolean |
| 45 | :group 'asm) |
| 46 | |
| 47 | (defcustom asm-mode-syntax-table nil |
| 48 | "Syntax table used while in Asm mode.") |
| 49 | |
| 50 | (defvar asm-mode-abbrev-table nil |
| 51 | "Abbrev table used while in Asm mode.") |
| 52 | (define-abbrev-table 'asm-mode-abbrev-table ()) |
| 53 | |
| 54 | (defvar asm-mode-map nil |
| 55 | "Keymap for Asm mode.") |
| 56 | |
| 57 | ;;(if asm-mode-map |
| 58 | ;; nil |
| 59 | ;; XEmacs change |
| 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) |
| 71 | ;; ) |
| 72 | |
| 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)) |
| 77 | |
| 78 | "Additional expressions to highlight in Assembler mode.") |
| 79 | |
| 80 | ;; XEmacs change |
| 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) |
| 85 | |
| 86 | |
| 87 | ;;;###autoload |
| 88 | (defun asm-mode () |
| 89 | "Major mode for editing typical assembler code. |
| 90 | Features a private abbrev table and the following bindings: |
| 91 | |
| 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. |
| 96 | |
| 97 | The character used for making comments is set by the variable |
| 98 | `asm-comment-char' (which defaults to `?;'). |
| 99 | |
| 100 | Alternatively, you may set this variable in `asm-mode-set-comment-hook', |
| 101 | which is called near the beginning of mode initialization. |
| 102 | |
| 103 | Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization. |
| 104 | |
| 105 | Special commands: |
| 106 | \\{asm-mode-map} |
| 107 | " |
| 108 | (interactive) |
| 109 | |
| 110 | ;; (make-local-variable 'asm-comment-space) |
| 111 | |
| 112 | (setq asm-comment-space " ") |
| 113 | |
| 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) |
| 123 | |
| 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. |
| 127 | ;; XEmacs change |
| 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) |
| 132 | ;; XEmacs change |
| 133 | |
| 134 | |
| 135 | (if asm-support-c-comments-p |
| 136 | (progn |
| 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)) |
| 141 | (progn |
| 142 | (modify-syntax-entry asm-comment-char |
| 143 | "<" asm-mode-syntax-table) |
| 144 | (modify-syntax-entry ?\n |
| 145 | ">" asm-mode-syntax-table))) |
| 146 | |
| 147 | |
| 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 " *$")) |
| 156 | ) |
| 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)) |
| 163 | |
| 164 | (defun asm-colon () |
| 165 | "Insert a colon; if it follows a label, delete the label's indentation." |
| 166 | (interactive) |
| 167 | (if (is-comment-line) (progn (asm-comment-line) (tab-to-tab-stop)) |
| 168 | (cond |
| 169 | |
| 170 | ;; unindent input |
| 171 | ( (save-excursion (beginning-of-line) (looking-at "\**[ \t]+\\(\\sw\\|\\s_\\)+$")) |
| 172 | (save-excursion (beginning-of-line) (delete-horizontal-space)) |
| 173 | (tab-to-tab-stop) |
| 174 | ) |
| 175 | |
| 176 | (t |
| 177 | (insert ":") |
| 178 | ) |
| 179 | ) |
| 180 | )) |
| 181 | |
| 182 | (defun asm-line-matches (pattern &optional withcomment) |
| 183 | (save-excursion |
| 184 | (beginning-of-line) |
| 185 | (looking-at pattern))) |
| 186 | |
| 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 |
| 190 | (end-of-line) |
| 191 | (delete-horizontal-space) |
| 192 | (while (= (preceding-char) asm-comment-char) |
| 193 | (delete-backward-char 1)) |
| 194 | (delete-horizontal-space) |
| 195 | (if (bolp) |
| 196 | nil |
| 197 | (beginning-of-line) |
| 198 | (open-line 1)) |
| 199 | ) |
| 200 | |
| 201 | |
| 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: |
| 205 | |
| 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. |
| 209 | |
| 210 | Suggested usage: while writing your code, trigger asm-comment |
| 211 | repeatedly until you are satisfied with the kind of comment." |
| 212 | (interactive) |
| 213 | |
| 214 | (setq comment-start "") |
| 215 | |
| 216 | (cond |
| 217 | |
| 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) |
| 223 | ) |
| 224 | |
| 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) |
| 229 | ;;) |
| 230 | |
| 231 | ;; Flush-left comment present? Just insert character. |
| 232 | ;; ((asm-line-matches asm-flush-left-empty-comment-pattern) |
| 233 | ;; (insert asm-comment-char)) |
| 234 | |
| 235 | ;; Fresh comment line?? |
| 236 | ( (asm-line-matches (format "^%c%s$" asm-comment-char asm-comment-space)) |
| 237 | (end-of-line) |
| 238 | (delete-backward-char (length asm-comment-space)) |
| 239 | (insert asm-comment-char asm-comment-char) |
| 240 | ) |
| 241 | |
| 242 | |
| 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)) |
| 248 | |
| 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) |
| 253 | ;; (tab-to-tab-stop) |
| 254 | ;; (insert asm-comment-char comment-start)) |
| 255 | |
| 256 | ;; If all else fails, insert character |
| 257 | (t |
| 258 | (insert asm-comment-char)) |
| 259 | |
| 260 | ) |
| 261 | (end-of-line)) |
| 262 | |
| 263 | ;; **************************************************************************** |
| 264 | |
| 265 | |
| 266 | (defun pos-in-line () |
| 267 | (- (point) (save-excursion (beginning-of-line) (point)))) |
| 268 | |
| 269 | |
| 270 | (defun at-begin-of-line () |
| 271 | (interactive) |
| 272 | (setq pos (point) ) |
| 273 | (save-excursion |
| 274 | (beginning-of-line) |
| 275 | (if (= pos (point)) t nil) |
| 276 | ) |
| 277 | ) |
| 278 | |
| 279 | (defun is-comment-line () |
| 280 | (if (asm-line-matches (format "^%c" asm-comment-char)) |
| 281 | t |
| 282 | nil) |
| 283 | ) |
| 284 | |
| 285 | (defun in-comment-spacing () |
| 286 | (if (> (current-column) (length (format "%c%s" asm-comment-char asm-comment-space))) nil t) |
| 287 | ) |
| 288 | |
| 289 | (defun asm-comment-line() |
| 290 | (interactive) |
| 291 | (if (is-comment-line) |
| 292 | (save-excursion |
| 293 | (beginning-of-line) |
| 294 | (delete-char 1) |
| 295 | (if (looking-at "^ ") |
| 296 | (delete-char 1) |
| 297 | ) |
| 298 | ) |
| 299 | (save-excursion (beginning-of-line) (insert asm-comment-char asm-comment-space)) |
| 300 | ) |
| 301 | ) |
| 302 | |
| 303 | (defun line-start() |
| 304 | (save-excursion |
| 305 | (beginning-of-line) |
| 306 | (point) |
| 307 | ) |
| 308 | ) |
| 309 | |
| 310 | (defun asm-comment-region() |
| 311 | (interactive) |
| 312 | |
| 313 | (if (not (region-active-p)) (asm-comment-line) |
| 314 | (save-excursion |
| 315 | (goto-char (region-end)) |
| 316 | (setq lastline (line-number)) |
| 317 | (goto-char (region-beginning)) |
| 318 | (while (< (line-number) lastline) |
| 319 | (asm-comment-line) |
| 320 | (next-line 1) |
| 321 | )))) |
| 322 | |
| 323 | |
| 324 | (defun asm-backspace () |
| 325 | (interactive) |
| 326 | (if (and (is-comment-line) (in-comment-spacing)) |
| 327 | (delete-region (- (line-start) 1) (point)) |
| 328 | (delete-backward-char 1) |
| 329 | ) |
| 330 | ;;delete-backward-char (length (format "%c%s" asm-comment-char asm-comment-space))) (tab-to-tab-stop)) |
| 331 | ;; (delete-backward-char 1) |
| 332 | ) |
| 333 | |
| 334 | (defun asm-fulldelim () |
| 335 | (interactive) |
| 336 | (make-local-variable 'count) |
| 337 | (setq count 0) |
| 338 | (if (asm-line-matches (format "^%c%s$" asm-comment-char asm-comment-space)) |
| 339 | (progn (setq count 1) |
| 340 | (end-of-line) |
| 341 | (delete-backward-char (length asm-comment-space))) |
| 342 | ) |
| 343 | |
| 344 | (while (< count 80) |
| 345 | (insert asm-comment-char) |
| 346 | (setq count ( 1+ count))) |
| 347 | ) |
| 348 | |
| 349 | |
| 350 | (defun asm-halfdelim () |
| 351 | (interactive) |
| 352 | (make-local-variable 'count) |
| 353 | (setq count 0) |
| 354 | (if (asm-line-matches (format "^%c%s$" asm-comment-char asm-comment-space)) |
| 355 | (progn (setq count 1) |
| 356 | (end-of-line) |
| 357 | (delete-backward-char (length asm-comment-space))) |
| 358 | ) |
| 359 | |
| 360 | (while (< count 40) |
| 361 | (insert asm-comment-char) |
| 362 | (setq count ( 1+ count))) |
| 363 | |
| 364 | ) |
| 365 | |
| 366 | |
| 367 | (defun asm-tabulator () |
| 368 | (interactive) |
| 369 | (if (is-comment-line) |
| 370 | (progn |
| 371 | (setq target (prog2 (insert "\t") (current-column) (delete-backward-char 1) )) |
| 372 | (while (< (current-column) target) (insert " ")) |
| 373 | ) |
| 374 | |
| 375 | (tab-to-tab-stop) |
| 376 | ) |
| 377 | ) |
| 378 | |
| 379 | |
| 380 | (defun asm-newline () |
| 381 | "Insert LFD + fill-prefix, to bring us back to code-indent level." |
| 382 | (interactive) |
| 383 | |
| 384 | (if (is-comment-line) |
| 385 | (progn |
| 386 | (if (at-begin-of-line) (goto-char (+ (point) 1))) |
| 387 | (insert "\n" asm-comment-char asm-comment-space)) |
| 388 | |
| 389 | (if (eolp) |
| 390 | (progn (delete-horizontal-space) |
| 391 | (insert "\n") |
| 392 | (tab-to-tab-stop) |
| 393 | ) |
| 394 | (insert "\n") |
| 395 | |
| 396 | ) |
| 397 | ) |
| 398 | ) |
| 399 | |
| 400 | |
| 401 | ;; XEmacs addition |
| 402 | ;;;###autoload(add-to-list 'auto-mode-alist '("\\.[sS]\\'" . asm-mode)) |
| 403 | ;;;###autoload(add-to-list 'auto-mode-alist '("\\.asm\\'" . asm-mode)) |
| 404 | |
| 405 | ;;; asm-mode.el ends here |