view lisp/progmodes/asm-mode.el @ 83376:de684325b343

Merged in changes from CVS trunk. Patches applied: * lorentey@elte.hu--2004/emacs--cvs-trunk--0--patch-6 Update from CVS * lorentey@elte.hu--2004/emacs--cvs-trunk--0--patch-7 Sync with miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-539. * lorentey@elte.hu--2004/emacs--cvs-trunk--0--version-0 Branch closed. Use miles@gnu.org--gnu-2005/emacs--cvs-trunk--0 instead. * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-475 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-476 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-477 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-478 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-479 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-480 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-481 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-482 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-483 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-484 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-485 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-486 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-487 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-488 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-489 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-490 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-491 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-492 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-493 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-494 Update from CVS: lisp/startup.el (command-line): Fix typo. * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-495 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-496 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-497 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-498 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-499 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-500 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-501 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-502 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-503 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-504 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-505 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-506 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-507 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-508 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-509 Update from CVS: .cvsignore: Add `lock'. * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-510 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-511 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-512 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-513 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-514 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-515 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-516 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-517 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-518 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-519 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-520 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-521 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-522 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-523 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-524 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-525 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-526 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-527 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-528 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-529 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-530 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-531 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-532 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-533 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-534 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-535 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-536 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-537 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-538 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-539 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-88 Merge from emacs--cvs-trunk--0 * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-89 Merge from emacs--cvs-trunk--0 * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-90 Update FSF's address in GPL notices * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-91 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-92 Merge from emacs--cvs-trunk--0 * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-93 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-94 Merge from emacs--cvs-trunk--0 * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-95 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-96 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-97 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-98 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-99 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-100 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-101 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-102 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-103 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-104 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-105 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-106 Merge from emacs--cvs-trunk--0 * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-107 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-108 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-109 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-110 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-111 Merge from emacs--cvs-trunk--0 * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-112 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-113 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-114 Update from CVS git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-416
author Karoly Lorentey <lorentey@elte.hu>
date Sun, 11 Sep 2005 08:23:50 +0000
parents 629afbe74e61
children a11fdee52c05 edf295560b5a
line wrap: on
line source

;;; asm-mode.el --- mode for editing assembler code

;; Copyright (C) 1991, 2001, 2002, 2003, 2004, 2005
;; Free Software Foundation, Inc.

;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
;; Maintainer: FSF
;; Keywords: tools, languages

;; 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
;; the Free Software Foundation; either version 2, or (at your option)
;; 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, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; 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
  "Mode for editing assembler code."
  :group 'languages)

(defcustom asm-comment-char ?\;
  "*The comment-start character assumed by Asm mode."
  :type 'character
  :group 'asm)

(defvar asm-mode-syntax-table
  (let ((st (make-syntax-table)))
    (modify-syntax-entry ?\n ">" st)
    (modify-syntax-entry ?/  ". 14b" st)
    (modify-syntax-entry ?*  ". 23b" st)
    st)
  "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
  (let ((map (make-sparse-keymap)))
    ;; Note that the comment character isn't set up until asm-mode is called.
    (define-key map ":"		'asm-colon)
    (define-key map "\C-c;"	'comment-region)
    (define-key map "\C-j"	'newline-and-indent)
    (define-key map "\C-m"	'newline-and-indent)
    map)
  "Keymap for Asm mode.")

(defconst asm-font-lock-keywords
 '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\(\\.\\sw+\\)*\\)?"
    (1 font-lock-function-name-face) (3 font-lock-keyword-face nil t))
   ;; label started from ".".
   ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>:"
    1 font-lock-function-name-face)
   ("^\\((\\sw+)\\)?\\s +\\(\\(\\.?\\sw\\|\\s_\\)+\\(\\.\\sw+\\)*\\)"
    2 font-lock-keyword-face)
   ;; directive started from ".".
   ("^\\(\\.\\(\\sw\\|\\s_\\)+\\)\\>[^:]?"
    1 font-lock-keyword-face)
   ;; %register
   ("%\\sw+" . font-lock-variable-name-face))
 "Additional expressions to highlight in Assembler mode.")

;;;###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)
  (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))
  (set (make-local-variable 'indent-line-function) 'asm-indent-line)
  ;; Stay closer to the old TAB behavior (was tab-to-tab-stop).
  (set (make-local-variable 'tab-always-indent) nil)

  (run-hooks 'asm-mode-set-comment-hook)
  ;; Make our own local child of asm-mode-map
  ;; so we can define our own comment character.
  (use-local-map (nconc (make-sparse-keymap) asm-mode-map))
  (local-set-key (vector asm-comment-char) 'asm-comment)
  (set-syntax-table (make-syntax-table asm-mode-syntax-table))
  (modify-syntax-entry	asm-comment-char "<")

  (make-local-variable 'comment-start)
  (setq comment-start (string asm-comment-char))
  (make-local-variable 'comment-add)
  (setq comment-add 1)
  (make-local-variable 'comment-start-skip)
  (setq comment-start-skip "\\(?:\\s<+\\|/\\*+\\)[ \t]*")
  (make-local-variable 'comment-end-skip)
  (setq comment-end-skip "[ \t]*\\(\\s>\\|\\*+/\\)")
  (make-local-variable 'comment-end)
  (setq comment-end "")
  (setq fill-prefix "\t")
  (run-mode-hooks 'asm-mode-hook))

(defun asm-indent-line ()
  "Auto-indent the current line."
  (interactive)
  (let* ((savep (point))
	 (indent (condition-case nil
		     (save-excursion
		       (forward-line 0)
		       (skip-chars-forward " \t")
		       (if (>= (point) savep) (setq savep nil))
		       (max (asm-calculate-indentation) 0))
		   (error 0))))
    (if savep
	(save-excursion (indent-line-to indent))
      (indent-line-to indent))))

(defun asm-calculate-indentation ()
  (or
   ;; Flush labels to the left margin.
   (and (looking-at "\\(\\sw\\|\\s_\\)+:") 0)
   ;; Same thing for `;;;' comments.
   (and (looking-at "\\s<\\s<\\s<") 0)
   ;; Simple `;' comments go to the comment-column.
   (and (looking-at "\\s<\\(\\S<\\|\\'\\)") comment-column)
   ;; The rest goes at the first tab stop.
   (or (car tab-stop-list) tab-width)))

(defun asm-colon ()
  "Insert a colon; if it follows a label, delete the label's indentation."
  (interactive)
  (let ((labelp nil))
    (save-excursion
      (skip-syntax-backward "w_")
      (skip-syntax-backward " ")
      (if (setq labelp (bolp)) (delete-horizontal-space)))
    (call-interactively 'self-insert-command)
    (when labelp
      (delete-horizontal-space)
      (tab-to-tab-stop))))

;; Obsolete since Emacs-22.1.
(defalias 'asm-newline 'newline-and-indent)

(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)
  (comment-normalize-vars)
  (let (comempty comment)
    (save-excursion
      (beginning-of-line)
      (with-no-warnings
	(setq comment (comment-search-forward (line-end-position) t)))
      (setq comempty (looking-at "[ \t]*$")))

  (cond

   ;; Blank line?  Then start comment at code indent level.
   ;; Just like `comment-dwim'.  -stef
   ((save-excursion (beginning-of-line) (looking-at "^[ \t]*$"))
    (indent-according-to-mode)
    (insert asm-comment-char asm-comment-char ?\ ))

   ;; Nonblank line w/o comment => start a comment at comment-column.
   ;; Also: point before the comment => jump inside.
   ((or (null comment) (< (point) comment))
    (indent-for-comment))

   ;; Flush-left or non-empty comment present => just insert character.
   ((or (not comempty) (save-excursion (goto-char comment) (bolp)))
    (insert asm-comment-char))

   ;; Empty code-level comment => upgrade to next comment level.
   ((save-excursion (goto-char comment) (skip-chars-backward " \t") (bolp))
    (goto-char comment)
    (insert asm-comment-char)
    (indent-for-comment))

   ;; Empty comment ends non-empty code line => new comment above.
   (t
    (goto-char comment)
    (skip-chars-backward " \t")
    (delete-region (point) (line-end-position))
    (beginning-of-line) (insert "\n") (backward-char)
    (asm-comment)))))

(provide 'asm-mode)

;;; arch-tag: 210e695f-f338-4376-8913-a4c5c72ac848
;;; asm-mode.el ends here