1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-27 07:37:33 +00:00
emacs/lisp/progmodes/asm-mode.el

226 lines
7.3 KiB
EmacsLisp
Raw Normal View History

1992-07-17 07:10:46 +00:00
;;; asm-mode.el --- mode for editing assembler code
1992-07-24 21:00:38 +00:00
;; Copyright (C) 1991 Free Software Foundation, Inc.
1992-07-17 07:10:46 +00:00
;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
1995-03-22 21:31:08 +00:00
;; Maintainer: FSF
1992-07-17 07:10:46 +00:00
;; Keywords: tools, languages
1991-12-21 08:23:15 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
1992-07-24 21:00:38 +00:00
;; the Free Software Foundation; either version 2, or (at your option)
1991-12-21 08:23:15 +00:00
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
1992-07-17 07:10:46 +00:00
;;; Commentary:
;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
1991-12-21 08:23:15 +00:00
;; inspired by an earlier asm-mode by Martin Neitzel.
;; This minor mode is based on text mode. It defines a private abbrev table
;; that can be used to save abbrevs for assembler mnemonics. It binds just
;; five keys:
;;
;; TAB tab to next tab stop
;; : outdent preceding label, tab to tab stop
;; ; place or move comment
;; C-j, C-m newline and tab to tab stop
;;
;; Code is indented to the first tab stop level.
;; The ; key inserts copies of the value of asm-comment-char at an
;; appropriate spot.
1992-07-17 07:10:46 +00:00
1991-12-21 08:23:15 +00:00
;; This mode runs two hooks:
1995-03-22 21:31:08 +00:00
;; 1) An asm-mode-set-comment-hook before the part of the initialization
1991-12-21 08:23:15 +00:00
;; depending on asm-comment-char, and
;; 2) an asm-mode-hook at the end of initialization.
1992-07-17 07:10:46 +00:00
;;; Code:
1991-12-21 08:23:15 +00:00
(defvar asm-comment-char ?;
1993-05-05 22:47:54 +00:00
"*The comment-start character assumed by Asm mode.")
1991-12-21 08:23:15 +00:00
(defvar asm-mode-syntax-table nil
1993-05-05 22:47:54 +00:00
"Syntax table used while in Asm mode.")
1991-12-21 08:23:15 +00:00
(defvar asm-mode-abbrev-table nil
1993-05-05 22:47:54 +00:00
"Abbrev table used while in Asm mode.")
1991-12-21 08:23:15 +00:00
(define-abbrev-table 'asm-mode-abbrev-table ())
(defvar asm-mode-map nil
1993-05-05 22:47:54 +00:00
"Keymap for Asm mode.")
1991-12-21 08:23:15 +00:00
(if asm-mode-map
nil
(setq asm-mode-map (make-sparse-keymap))
(define-key asm-mode-map ";" 'asm-comment)
(define-key asm-mode-map ":" 'asm-colon)
(define-key asm-mode-map "\C-i" 'tab-to-tab-stop)
1992-07-17 07:10:46 +00:00
(define-key asm-mode-map "\C-j" 'asm-newline)
(define-key asm-mode-map "\C-m" 'asm-newline)
)
1991-12-21 08:23:15 +00:00
(defconst asm-font-lock-keywords
'(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\)?"
(1 font-lock-function-name-face) (3 font-lock-keyword-face nil t))
("^\\s +\\(\\(\\sw\\|\\s_\\)+\\)" 1 font-lock-keyword-face))
"Additional expressions to highlight in Assembler mode.")
1991-12-21 08:23:15 +00:00
(defvar asm-code-level-empty-comment-pattern nil)
(defvar asm-flush-left-empty-comment-pattern nil)
(defvar asm-inline-empty-comment-pattern nil)
;;;###autoload
(defun asm-mode ()
"Major mode for editing typical assembler code.
1993-05-05 22:47:54 +00:00
Features a private abbrev table and the following bindings:
1991-12-21 08:23:15 +00:00
\\[asm-colon]\toutdent a preceding label, tab to next tab stop.
\\[tab-to-tab-stop]\ttab to next tab stop.
\\[asm-newline]\tnewline, then tab to next tab stop.
\\[asm-comment]\tsmart placement of assembler comments.
The character used for making comments is set by the variable
1993-05-05 22:47:54 +00:00
`asm-comment-char' (which defaults to `?;').
1991-12-21 08:23:15 +00:00
1995-03-22 21:31:08 +00:00
Alternatively, you may set this variable in `asm-mode-set-comment-hook',
which is called near the beginning of mode initialization.
1991-12-21 08:23:15 +00:00
1993-05-05 22:47:54 +00:00
Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
1991-12-21 08:23:15 +00:00
1994-03-22 20:48:30 +00:00
Special commands:
\\{asm-mode-map}
1991-12-21 08:23:15 +00:00
"
(interactive)
(kill-all-local-variables)
(use-local-map asm-mode-map)
(setq mode-name "Assembler")
(setq major-mode 'asm-mode)
(setq local-abbrev-table asm-mode-abbrev-table)
(make-local-variable 'font-lock-defaults)
(setq font-lock-defaults '(asm-font-lock-keywords))
1991-12-21 08:23:15 +00:00
(make-local-variable 'asm-mode-syntax-table)
(setq asm-mode-syntax-table (make-syntax-table))
(set-syntax-table asm-mode-syntax-table)
(run-hooks 'asm-mode-set-comment-hook)
(modify-syntax-entry asm-comment-char
"<" asm-mode-syntax-table)
(modify-syntax-entry ?\n
">" asm-mode-syntax-table)
(let ((cs (regexp-quote (char-to-string asm-comment-char))))
(make-local-variable 'comment-start)
(setq comment-start (concat cs " "))
(make-local-variable 'comment-start-skip)
(setq comment-start-skip (concat cs "+[ \t]*"))
(setq asm-inline-empty-comment-pattern (concat "^.+" cs "+ *$"))
(setq asm-code-level-empty-comment-pattern (concat "^[\t ]+" cs cs " *$"))
(setq asm-flush-left-empty-comment-pattern (concat "^" cs cs cs " *$"))
)
(make-local-variable 'comment-end)
(setq comment-end "")
(make-local-variable 'comment-column)
(setq comment-column 32)
(setq fill-prefix "\t")
(run-hooks 'asm-mode-hook)
)
(defun asm-colon ()
"Insert a colon; if it follows a label, delete the label's indentation."
(interactive)
(save-excursion
(beginning-of-line)
(if (looking-at "[ \t]+\\(\\sw\\|\\s_\\)+$")
(delete-horizontal-space)))
(insert ":")
(tab-to-tab-stop)
)
(defun asm-newline ()
"Insert LFD + fill-prefix, to bring us back to code-indent level."
(interactive)
(if (eolp) (delete-horizontal-space))
(insert "\n")
(tab-to-tab-stop)
)
(defun asm-line-matches (pattern &optional withcomment)
(save-excursion
(beginning-of-line)
(looking-at pattern)))
(defun asm-pop-comment-level ()
;; Delete an empty comment ending current line. Then set up for a new one,
;; on the current line if it was all comment, otherwise above it
(end-of-line)
(delete-horizontal-space)
(while (= (preceding-char) asm-comment-char)
(delete-backward-char 1))
(delete-horizontal-space)
(if (bolp)
nil
(beginning-of-line)
(open-line 1))
)
(defun asm-comment ()
"Convert an empty comment to a `larger' kind, or start a new one.
These are the known comment classes:
1 -- comment to the right of the code (at the comment-column)
2 -- comment on its own line, indented like code
3 -- comment on its own line, beginning at the left-most column.
Suggested usage: while writing your code, trigger asm-comment
repeatedly until you are satisfied with the kind of comment."
(interactive)
(cond
;; Blank line? Then start comment at code indent level.
((asm-line-matches "^[ \t]*$")
(delete-horizontal-space)
(tab-to-tab-stop)
(insert asm-comment-char comment-start))
;; Nonblank line with no comment chars in it?
;; Then start a comment at the current comment column
1994-05-07 01:58:40 +00:00
((asm-line-matches (format "^[^%c\n]+$" asm-comment-char))
1991-12-21 08:23:15 +00:00
(indent-for-comment))
;; Flush-left comment present? Just insert character.
((asm-line-matches asm-flush-left-empty-comment-pattern)
(insert asm-comment-char))
;; Empty code-level comment already present?
;; Then start flush-left comment, on line above if this one is nonempty.
((asm-line-matches asm-code-level-empty-comment-pattern)
(asm-pop-comment-level)
(insert asm-comment-char asm-comment-char comment-start))
;; Empty comment ends line?
;; Then make code-level comment, on line above if this one is nonempty.
((asm-line-matches asm-inline-empty-comment-pattern)
(asm-pop-comment-level)
(tab-to-tab-stop)
(insert asm-comment-char comment-start))
;; If all else fails, insert character
(t
(insert asm-comment-char))
)
(end-of-line))
;;; asm-mode.el ends here