From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Masatake YAMATO Newsgroups: gmane.emacs.devel Subject: Re: asm-mode patch to allow per-file comment character setting from file locals Date: Tue, 13 Jun 2006 21:19:40 +0900 (JST) Message-ID: <20060613.211940.19660215.jet@gyve.org> References: <2E2BAEFF-FEAD-4616-87CD-3B77D2734256@alastairs-place.net> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: sea.gmane.org 1150201237 11020 80.91.229.2 (13 Jun 2006 12:20:37 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 13 Jun 2006 12:20:37 +0000 (UTC) Cc: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Jun 13 14:20:34 2006 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1Fq7sg-00063O-23 for ged-emacs-devel@m.gmane.org; Tue, 13 Jun 2006 14:20:26 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Fq7sf-0007Jy-G4 for ged-emacs-devel@m.gmane.org; Tue, 13 Jun 2006 08:20:25 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Fq7sP-0007GF-S9 for emacs-devel@gnu.org; Tue, 13 Jun 2006 08:20:09 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Fq7sN-0007DX-KH for emacs-devel@gnu.org; Tue, 13 Jun 2006 08:20:08 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Fq7sN-0007DR-HN for emacs-devel@gnu.org; Tue, 13 Jun 2006 08:20:07 -0400 Original-Received: from [66.187.233.31] (helo=mx1.redhat.com) by monty-python.gnu.org with esmtp (Exim 4.52) id 1Fq81K-00065d-0W for emacs-devel@gnu.org; Tue, 13 Jun 2006 08:29:22 -0400 Original-Received: from int-mx1.corp.redhat.com (int-mx1.corp.redhat.com [172.16.52.254]) by mx1.redhat.com (8.12.11.20060308/8.12.11) with ESMTP id k5DCJvp0007827; Tue, 13 Jun 2006 08:19:57 -0400 Original-Received: from pobox.tokyo.redhat.com (pobox.tokyo.redhat.com [172.16.33.225]) by int-mx1.corp.redhat.com (8.12.11.20060308/8.12.11) with ESMTP id k5DCJuaI017426; Tue, 13 Jun 2006 08:19:57 -0400 Original-Received: from localhost (gls07.tokyo.redhat.com [172.16.32.104]) by pobox.tokyo.redhat.com (8.12.8/8.12.8) with ESMTP id k5DCJmTr027816; Tue, 13 Jun 2006 21:19:51 +0900 Original-To: alastair@alastairs-place.net In-Reply-To: <2E2BAEFF-FEAD-4616-87CD-3B77D2734256@alastairs-place.net> X-Mailer: Mew version 4.2.53 on Emacs 22.0.51 / Mule 5.0 (SAKAKI) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:55853 Archived-At: > Hi there, > > Here's a short patch to asm-mode.el that I use so that I can easily > vary the comment style used for assembly language files on a per-file > basis. This is particularly useful on Mac OS X, where the Intel and > PowerPC versions of the system assembler use different comment > characters (I believe the PowerPC version actually accepts both ';' > and '#', but the Intel one certainly requires '#'). > > An example of its use: > > ### > ### Local Variables: > ### asm-comment-char: ?\# > ### End: > ### > > Here's the patch: Interesting. I have met with the same situation when I read linux/arch/*/*.S files. Font lock rules of asm-mode are not enough for the situation. So I tried to write modes derived from asm-mode for each architecture/asm syntax. I refer the info file of gas to know the asm syntaxes, so I called the derived modes gas-*-mode, here * is a name of architecture. You can do like: M-x gas-i386-mode , M-x gas-ppc-mode or M-x gas-arm-mode if you want to hack iPod:-P If your asm file is at /foo/bar/i386/baz.s or /foo/bar/ppc/baz.s, gas-i386-mode or gas-ppc-mode is automatically selected. Masatake YAMATO (require 'asm-mode) (require 'assoc) (defgroup gas nil "Architecture specific mode for editing assembler code." :group 'asm) (defcustom gas-mode-hook nil "*Hook that gets run after the gas mode ." :type 'hook :group 'asm) (defvar gas-mode-architecture-history nil) (defun gas-mode (&optional architecture) "Wrapper for gas-*-mode." (interactive (list (let ((md (gas-choose-mode-automatically))) (if current-prefix-arg (completing-read "Architecture: " gas-machine-dependents nil t md 'gas-mode-architecture-history md) (gas-choose-mode-automatically))))) (unless (interactive-p) (setq architecture (gas-choose-mode-automatically))) (if architecture (let* ((machine-dependent (aget gas-machine-dependents architecture)) (mode-func (nth 0 machine-dependent)) (asm-comment-chars (car (nth 1 machine-dependent)))) (call-interactively mode-func)) (asm-mode)) (run-mode-hooks 'gas-mode-hook)) (defvar gas-machine-dependents nil) (defun gas-register-machine-dependent (name comment-chars register-prefix-chars immediate-prefix-chars extra-symbol-chars statement-separator-char auto-mode) (let ((mode-func (intern (format "gas-%s-mode" name)))) (aput 'gas-machine-dependents name (list mode-func comment-chars auto-mode)) (eval `(define-derived-mode ,mode-func asm-mode ,(format "Gas/%s" name) ,(format "%s specific asm mode" name) :syntax-table (gas-make-syntax-table ',comment-chars ',register-prefix-chars ',immediate-prefix-chars ',extra-symbol-chars ',statement-separator-char) (make-local-variable 'font-lock-defaults) (setq font-lock-defaults (list (gas-make-font-lock-keywords ',comment-chars ',register-prefix-chars ',immediate-prefix-chars ',extra-symbol-chars ',statement-separator-char))))))) (defun gas-choose-mode-automatically () (catch 'found (dolist (md gas-machine-dependents) (let ((auto-mode (nth 3 md))) (when (eq auto-mode t) (setq auto-mode (car md))) (cond ((and (stringp auto-mode) (buffer-file-name)) (when (string-match auto-mode (buffer-file-name)) (throw 'found (car md)))) ((functionp auto-mode) (when (funcall 'auto-mode) (throw 'found (car md))))))))) (defun gas-make-syntax-table (comment-chars register-prefix-chars immediate-prefix-chars extra-symbol-chars statement-separator-char) (let ((st (copy-syntax-table asm-mode-syntax-table))) (mapc (lambda (c) (modify-syntax-entry c "< b" st)) comment-chars) (mapc (lambda (c) (modify-syntax-entry c "'" st)) register-prefix-chars) (mapc (lambda (c) (modify-syntax-entry c "'" st)) immediate-prefix-chars) (mapc (lambda (c) (modify-syntax-entry c "w" st)) ; "_"? extra-symbol-chars) (mapc (lambda (c) (modify-syntax-entry c " " st)) statement-separator-char) st)) (defun gas-make-font-lock-keywords (comment-chars register-prefix-chars immediate-prefix-chars extra-symbol-chars statement-separator-chars) (let ((rp (when register-prefix-chars (cons (concat "[" (mapconcat 'char-to-string register-prefix-chars "") "]" "\\sw+") font-lock-variable-name-face))) (ip (when immediate-prefix-chars (cons (concat "[" (mapconcat 'char-to-string immediate-prefix-chars "") "]" "\\(?:\\s_\\|\\sw\\)+") font-lock-constant-face))) (pattern)) (when rp (push rp pattern)) (when ip (push ip pattern)) (append pattern asm-font-lock-keywords))) ;; c:comment-char ;; r:register-prefix-chars ;; i:immediate-prefix-chars ;; sym:extra-symbol-chars ;; sep:statement-separator-char ;; am: regexp for auto mode selection. ;; name c r i sym sep am (gas-register-machine-dependent "alpha" '(?#) '(?$) nil nil '(?\;) t) (gas-register-machine-dependent "arm" '(?@) nil '(?# ?$) '(?_ ?\\) '(?\;) t) (gas-register-machine-dependent "cris" '(?\; ?#) '(?$) nil nil '(?\@) t) ;; frv??? (gas-register-machine-dependent "frv" '(?\; ?#) nil nil nil nil t) (gas-register-machine-dependent "h8300" '(?\;) '(?@) '(?#) '(?_) '(?\$) t) ;; #include doesn't work well. (gas-register-machine-dependent "i386" '(?#) '(?%) '(?$) nil '(?\;) "i386\\|x86_64") ;; comment is broken (gas-register-machine-dependent "ia64" nil nil nil nil '(?\;) t) (gas-register-machine-dependent "m32r" '(?\;) '(?@) '(?#) '(?+) nil t) (gas-register-machine-dependent "m68k" '(?\|) '(?%) '(?#) '(?@) nil "68k") (gas-register-machine-dependent "mips" '(?#) '(?$) nil '(?@) nil t) (gas-register-machine-dependent "parisc" '(?\;) '(?%) nil '(?$) '(?!) t) (gas-register-machine-dependent "ppc" nil nil nil nil '(?\;) "powerpc\\|ppc") (gas-register-machine-dependent "s390" '(?#) '(?%) nil nil nil t) (gas-register-machine-dependent "sh" '(?!) '(?@) '(?#) '(?$ ?-) '(?\;) t) (gas-register-machine-dependent "sparc" '(?!) '(?%) nil nil '(?\;) t) (gas-register-machine-dependent "v850" '(?#) nil nil nil '(?\;) t) (gas-register-machine-dependent "xtensa" '(?#) '(?$) nil nil '(?\;) t) (add-to-list 'auto-mode-alist '("\\.[sS]\\'" . gas-mode)) (provide 'gas-mode) ;; gas-mode.el ends here