--- /dev/null
+;;; asm-mode.el --- mode for editing assembler code
+
+;;; Commentary:
+
+;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
+;; 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
+;; comment char place or move comment
+;; asm-comment-char specifies which character this is;
+;; you can use a different character in different
+;; Asm mode buffers.
+;; C-j, C-m newline and tab to tab stop
+;;
+;; Code is indented to the first tab stop level.
+
+;; This mode runs two hooks:
+;; 1) An asm-mode-set-comment-hook before the part of the initialization
+;; depending on asm-comment-char, and
+;; 2) an asm-mode-hook at the end of initialization.
+
+;;; Code:
+
+(defgroup asm nil
+ "X16 Assembler programming"
+ :group 'languages)
+
+
+(defcustom asm-comment-char ?*
+ "*The comment-start character assumed by Asm mode."
+ :type 'sexp
+ :group 'asm)
+
+;; XEmacs change (This is the primary difference, why was this
+;; feature removed? -sb)
+(defcustom asm-support-c-comments-p nil
+ "*Support C style comments. If t C style comments will be
+supported. This is mainly for the benefit of font-lock."
+ :type 'boolean
+ :group 'asm)
+
+(defcustom asm-mode-syntax-table nil
+ "Syntax table used while in Asm mode.")
+
+(defvar asm-mode-abbrev-table nil
+ "Abbrev table used while in Asm mode.")
+(define-abbrev-table 'asm-mode-abbrev-table ())
+
+(defvar asm-mode-map nil
+ "Keymap for Asm mode.")
+
+;;(if asm-mode-map
+;; nil
+ ;; XEmacs change
+ (setq asm-mode-map (make-sparse-keymap 'asm-mode-map))
+ ;; Note that the comment character isn't set up until asm-mode is called.
+ (define-key asm-mode-map ":" 'asm-colon)
+ (define-key asm-mode-map "\C-h" 'asm-backspace)
+ (define-key asm-mode-map "\C-l" 'asm-halfdelim)
+ (define-key asm-mode-map "\M-l" 'asm-fulldelim)
+ (define-key asm-mode-map "\C-i" 'asm-tabulator)
+;;(define-key asm-mode-map "\C-a" 'asm-comment-region)
+ (define-key asm-mode-map "\C-a" 'beginning-of-line)
+ (define-key asm-mode-map "\C-j" 'asm-newline)
+ (define-key asm-mode-map "\C-m" 'asm-newline)
+;; )
+
+(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.")
+
+;; XEmacs change
+(put 'asm-mode 'font-lock-defaults '(asm-font-lock-keywords))
+(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.
+Features a private abbrev table and the following bindings:
+
+\\[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
+`asm-comment-char' (which defaults to `?;').
+
+Alternatively, you may set this variable in `asm-mode-set-comment-hook',
+which is called near the beginning of mode initialization.
+
+Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
+
+Special commands:
+\\{asm-mode-map}
+"
+ (interactive)
+
+;; (make-local-variable 'asm-comment-space)
+
+ (setq asm-comment-space " ")
+
+ (kill-all-local-variables)
+ (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))
+ (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)
+ ;; Make our own local child of asm-mode-map
+ ;; so we can define our own comment character.
+ ;; XEmacs change
+ (let ((ourmap (make-sparse-keymap)))
+ (set-keymap-parents ourmap (list asm-mode-map))
+ (use-local-map ourmap))
+ (local-set-key (vector asm-comment-char) 'asm-comment)
+ ;; XEmacs change
+
+
+ (if asm-support-c-comments-p
+ (progn
+ (modify-syntax-entry ?/ ". 14" asm-mode-syntax-table)
+ (modify-syntax-entry ?* ". 23" asm-mode-syntax-table)
+ (modify-syntax-entry asm-comment-char "< b" asm-mode-syntax-table)
+ (modify-syntax-entry ?\n "> b" asm-mode-syntax-table))
+ (progn
+ (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 asm-comment-space))
+ (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)
+ (if (is-comment-line) (progn (asm-comment-line) (tab-to-tab-stop))
+ (cond
+
+ ;; unindent input
+ ( (save-excursion (beginning-of-line) (looking-at "\**[ \t]+\\(\\sw\\|\\s_\\)+$"))
+ (save-excursion (beginning-of-line) (delete-horizontal-space))
+ (tab-to-tab-stop)
+ )
+
+ (t
+ (insert ":")
+ )
+ )
+ ))
+
+(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)
+
+ (setq comment-start "")
+
+ (cond
+
+ ;; Blank line? Then start comment.
+ ((asm-line-matches "^[ \t]*$")
+ (delete-horizontal-space)
+;; (insert asm-comment-char comment-start)
+ (insert asm-comment-char asm-comment-space)
+ )
+
+ ;; Nonblank line with no comment chars in it?
+ ;; Then start a comment at the current comment column
+ ;;( (if (is-comment-line) nil t)
+ ;; (indent-for-comment)
+;;)
+
+ ;; Flush-left comment present? Just insert character.
+;; ((asm-line-matches asm-flush-left-empty-comment-pattern)
+;; (insert asm-comment-char))
+
+ ;; Fresh comment line??
+ ( (asm-line-matches (format "^%c%s$" asm-comment-char asm-comment-space))
+ (end-of-line)
+ (delete-backward-char (length asm-comment-space))
+ (insert asm-comment-char 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))
+
+;; ****************************************************************************
+
+
+(defun pos-in-line ()
+ (- (point) (save-excursion (beginning-of-line) (point))))
+
+
+(defun at-begin-of-line ()
+ (interactive)
+ (setq pos (point) )
+(save-excursion
+ (beginning-of-line)
+ (if (= pos (point)) t nil)
+ )
+)
+
+(defun is-comment-line ()
+ (if (asm-line-matches (format "^%c" asm-comment-char))
+ t
+ nil)
+)
+
+(defun in-comment-spacing ()
+ (if (> (current-column) (length (format "%c%s" asm-comment-char asm-comment-space))) nil t)
+)
+
+(defun asm-comment-line()
+ (interactive)
+ (if (is-comment-line)
+ (save-excursion
+ (beginning-of-line)
+ (delete-char 1)
+ (if (looking-at "^ ")
+ (delete-char 1)
+ )
+ )
+ (save-excursion (beginning-of-line) (insert asm-comment-char asm-comment-space))
+ )
+)
+
+(defun line-start()
+ (save-excursion
+ (beginning-of-line)
+ (point)
+ )
+)
+
+(defun asm-comment-region()
+ (interactive)
+
+ (if (not (region-active-p)) (asm-comment-line)
+ (save-excursion
+ (goto-char (region-end))
+ (setq lastline (line-number))
+ (goto-char (region-beginning))
+ (while (< (line-number) lastline)
+ (asm-comment-line)
+ (next-line 1)
+ ))))
+
+
+(defun asm-backspace ()
+ (interactive)
+ (if (and (is-comment-line) (in-comment-spacing))
+ (delete-region (- (line-start) 1) (point))
+ (delete-backward-char 1)
+ )
+;;delete-backward-char (length (format "%c%s" asm-comment-char asm-comment-space))) (tab-to-tab-stop))
+;; (delete-backward-char 1)
+ )
+
+(defun asm-fulldelim ()
+ (interactive)
+ (make-local-variable 'count)
+ (setq count 0)
+ (if (asm-line-matches (format "^%c%s$" asm-comment-char asm-comment-space))
+ (progn (setq count 1)
+ (end-of-line)
+ (delete-backward-char (length asm-comment-space)))
+ )
+
+ (while (< count 80)
+ (insert asm-comment-char)
+ (setq count ( 1+ count)))
+)
+
+
+(defun asm-halfdelim ()
+ (interactive)
+ (make-local-variable 'count)
+ (setq count 0)
+ (if (asm-line-matches (format "^%c%s$" asm-comment-char asm-comment-space))
+ (progn (setq count 1)
+ (end-of-line)
+ (delete-backward-char (length asm-comment-space)))
+ )
+
+ (while (< count 40)
+ (insert asm-comment-char)
+ (setq count ( 1+ count)))
+
+)
+
+
+(defun asm-tabulator ()
+ (interactive)
+ (if (is-comment-line)
+ (progn
+ (setq target (prog2 (insert "\t") (current-column) (delete-backward-char 1) ))
+ (while (< (current-column) target) (insert " "))
+ )
+
+ (tab-to-tab-stop)
+ )
+)
+
+
+(defun asm-newline ()
+ "Insert LFD + fill-prefix, to bring us back to code-indent level."
+ (interactive)
+
+ (if (is-comment-line)
+ (progn
+ (if (at-begin-of-line) (goto-char (+ (point) 1)))
+ (insert "\n" asm-comment-char asm-comment-space))
+
+ (if (eolp)
+ (progn (delete-horizontal-space)
+ (insert "\n")
+ (tab-to-tab-stop)
+ )
+ (insert "\n")
+
+ )
+ )
+)
+
+
+;; XEmacs addition
+;;;###autoload(add-to-list 'auto-mode-alist '("\\.[sS]\\'" . asm-mode))
+;;;###autoload(add-to-list 'auto-mode-alist '("\\.asm\\'" . asm-mode))
+
+;;; asm-mode.el ends here