*** empty log message ***
[h316.git] / resources / dap16-mode.el
diff --git a/resources/dap16-mode.el b/resources/dap16-mode.el
new file mode 100644 (file)
index 0000000..52f1b8b
--- /dev/null
@@ -0,0 +1,489 @@
+;;; dap16-mode.el --- mode for editing DAP-16 assembler code
+;;; Commentary:
+
+;; This mode was written by Philipp Hachtmann <hachti@hachti.de>.
+;; It is based on the asm mode by Eric S. Raymond <esr@snark.thyrsus.com>
+
+;; 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
+;;                     dap16-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 dap16-mode-set-comment-hook before the part of the initialization
+;; depending on dap16-comment-char, and
+;;   2) an dap16-mode-hook at the end of initialization.
+
+;;; Code:
+
+(defgroup dap16 nil
+  "DAP-16 Assembler programming"
+  :group 'languages)
+
+(defcustom dap16-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 dap16-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 dap16-mode-syntax-table nil
+  "Syntax table used while in Asm mode.")
+
+(defvar dap16-mode-abbrev-table nil
+  "Abbrev table used while in Asm mode.")
+(define-abbrev-table 'dap16-mode-abbrev-table ())
+
+(defvar dap16-mode-map nil
+  "Keymap for Asm mode.")
+
+;;(if dap16-mode-map
+;;    nil
+  ;; XEmacs change
+  (setq dap16-mode-map (make-sparse-keymap 'dap16-mode-map))
+  ;; Note that the comment character isn't set up until dap16-mode is called.
+  (define-key dap16-mode-map ":"       'dap16-colon)
+  (define-key dap16-mode-map "\C-h"       'dap16-backspace)
+  (define-key dap16-mode-map "\C-l"       'dap16-halfdelim)
+  (define-key dap16-mode-map "\M-l"       'dap16-fulldelim)
+  (define-key dap16-mode-map "\C-i"       'dap16-tabulator)
+  (define-key dap16-mode-map "\C-cc"   'dap16-comment-region)
+  (define-key dap16-mode-map "\C-c\C-c"        'dap16-comment-region)
+  (define-key dap16-mode-map "\C-a"       'beginning-of-line)
+  (define-key dap16-mode-map "\C-j"    'dap16-newline)
+  (define-key dap16-mode-map "\C-m"    'dap16-newline)
+;;  )
+
+;;(defconst dap16-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 'dap16-mode 'font-lock-defaults '(dap16-font-lock-keywords))
+
+;;(defvar dap16-code-level-empty-comment-pattern nil)
+;;(defvar dap16-flush-left-empty-comment-pattern nil)
+;;(defvar dap16-inline-empty-comment-pattern nil)
+
+;;;###autoload
+(defun dap16-mode ()
+  "Major mode for editing Honeywell DAP-16 assembler code.
+Features a private abbrev table and the following bindings:
+
+\\[dap16-colon]\toutdent a preceding label, tab to next tab stop.
+\\[tab-to-tab-stop]\ttab to next tab stop.
+\\[dap16-newline]\tnewline, then tab to next tab stop.
+\\[dap16-comment]\tsmart placement of assembler comments.
+
+Turning on Asm mode runs the hook `dap16-mode-hook' at the end of initialization.
+
+Special commands:
+\\{dap16-mode-map}
+"
+  (interactive)
+
+  (kill-all-local-variables)
+  
+  ;; The space after a comment char at the beginning of a line
+  (make-local-variable 'dap16-comment-space)
+  (setq dap16-comment-space " ")
+
+  (setq mode-name "DAP-16")
+  (setq major-mode 'dap16-mode)
+  (setq local-abbrev-table dap16-mode-abbrev-table)
+  
+;;  (make-local-variable 'tab-stop-list)
+;;  (setq tab-stop-list '(6 12 30))
+
+;;(defconst dap16-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.")
+
+  (setq dap16-font-lock-keywords
+       '(
+         (dap16-comment-matcher . font-lock-comment-face)
+;;       ("\\<[0-9]+H[^ \t\n]*\\>" . font-lock-keyword-face)
+         
+         ("^\\(\\(\\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))
+)
+       
+       
+   )
+
+
+  (make-local-variable 'dap16-mode-syntax-table)
+  (setq dap16-mode-syntax-table (make-syntax-table))
+  (set-syntax-table dap16-mode-syntax-table)
+
+  (make-local-variable 'font-lock-defaults)
+  
+  (setq font-lock-defaults '( 
+       dap16-font-lock-keywords
+       nil
+       nil
+       )
+  )
+
+  
+
+  (run-hooks 'dap16-mode-set-comment-hook)
+  ;; Make our own local child of dap16-mode-map
+  ;; so we can define our own comment character.
+
+  ;; XEmacs change
+  (let ((ourmap (make-sparse-keymap)))
+    (set-keymap-parents ourmap (list dap16-mode-map))
+    (use-local-map ourmap))
+  (local-set-key (vector dap16-comment-char) 'dap16-comment)
+  ;; XEmacs change
+
+;;  (modify-syntax-entry dap16-comment-char "<" dap16-mode-syntax-table)
+;;  (modify-syntax-entry ?\n ">" dap16-mode-syntax-table)
+
+;;  (modify-syntax-entry ?a "w" dap16-mode-syntax-table)
+
+
+;;  (let ((cs (regexp-quote (char-to-string dap16-comment-char))))
+;;    (make-local-variable 'comment-start)
+;;    (setq comment-start (concat cs dap16-comment-space))
+;;    (make-local-variable 'comment-start-skip)
+;;    (setq comment-start-skip (concat cs "+[ \t]*"))
+;;    (setq dap16-inline-empty-comment-pattern (concat "^.+" cs "+ *$"))
+;;    (setq dap16-code-level-empty-comment-pattern (concat "^[\t ]+" cs cs " *$"))
+;;    (setq dap16-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 'dap16-mode-hook))
+
+(defun dap16-colon ()
+  "Insert a colon; if it follows a label, delete the label's indentation."
+  (interactive)
+  (cond
+   ( (and (is-comment-line) (= (current-column) (length '(dap16-comment-char dap16-comment-space))))
+     (dap16-comment-line)
+     (tab-to-tab-stop)
+     )
+
+   ( (and (not (is-comment-line)) (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 col ()
+  (+ 1 (current-column))
+)
+
+(defun fc-pos()
+  (save-excursion 
+    (end-of-line)
+    (setq cstart (col))
+    (while (> (col) 16)
+      (if (or (= (following-char) ?  ) (= (following-char) ?\t) (= (following-char) ?\n))
+         (setq cstart (col)))
+      (backward-char 1))
+      )
+  cstart
+)
+
+(defun dap16-comment-matcher (limit)
+;;(setq p1 (point))
+;;(setq p2 (+ 2 (point)))
+
+  (setq success nil) 
+  (setq end-of-buffer-exit nil)
+  
+  ;; Consume Newline characters if present.
+  (while (and (< (point) limit) (= (following-char) ?\n) ) (forward-char))
+  
+  ;; Save first location to highlight
+  (setq p1 (point))
+  (while  (and (< (point) limit) (not (and (= (following-char) ?\n) success)) (not end-of-buffer-exit))
+    (if  (or (> (col) (fc-pos)) (is-comment-line))  
+       (progn  (setq p2 (+ 1 (point))) 
+               (if success nil (progn (setq success t) (setq p1 (- (point) 0))))))
+    
+    (forward-char))
+  
+  (if success (set-match-data (list (- p1 0) (- p2 0))))
+  success
+)
+           
+
+;_____________________________________________________________________________________________
+
+(defun dap16-line-matches (pattern &optional withcomment)
+  (save-excursion
+    (beginning-of-line)
+    (looking-at pattern)))
+
+(defun dap16-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) dap16-comment-char)
+    (delete-backward-char 1))
+  (delete-horizontal-space)
+  (if (bolp)
+      nil
+    (beginning-of-line)
+    (open-line 1))
+  )
+
+
+(defun dap16-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 dap16-comment
+repeatedly until you are satisfied with the kind of comment."
+  (interactive)
+ (setq comment-start "")
+
+  (cond
+
+   ;; Blank line?  Then start comment.
+   ((dap16-line-matches "^[ \t]*$")
+    (delete-horizontal-space)
+    (insert dap16-comment-char dap16-comment-space) 
+    )
+
+   ;; Nonblank line with no comment chars in it?
+   ;; Then start a comment at the current comment column
+   ((and (= (col) 1) (is-comment-line))
+    (dap16-comment-line)
+    )
+
+   ;; Flush-left comment present?  Just insert character.
+;;   ((dap16-line-matches dap16-flush-left-empty-comment-pattern)
+;;    (insert dap16-comment-char))
+
+   ;; Fresh comment line??
+   ((dap16-line-matches (format "^%c%s$" dap16-comment-char dap16-comment-space))
+    (end-of-line)
+    (delete-backward-char (length dap16-comment-space))
+    (insert dap16-comment-char dap16-comment-char)
+    (end-of-line)
+   )
+
+   
+   ;; Empty code-level comment already present?
+   ;; Then start flush-left comment, on line above if this one is nonempty. 
+;;   ((dap16-line-matches dap16-code-level-empty-comment-pattern)
+;;    (dap16-pop-comment-level)
+;;    (insert dap16-comment-char dap16-comment-char comment-start))
+
+   ;; Empty comment ends line?
+   ;; Then make code-level comment, on line above if this one is nonempty. 
+;;   ((dap16-line-matches dap16-inline-empty-comment-pattern)
+;;    (dap16-pop-comment-level)
+;;    (tab-to-tab-stop)
+;;    (insert dap16-comment-char comment-start))
+
+   ;; If all else fails, insert character
+   (t
+    (insert dap16-comment-char))
+
+   )
+
+ )
+
+;; ****************************************************************************
+
+
+(defun pos-in-line ()
+  (- (point) (save-excursion (beginning-of-line) (point))))
+
+
+;; Muss verbessert werden!
+(defun at-begin-of-line ()
+;;  (interactive)
+;;    (setq pos (point) )
+;;(save-excursion  
+;;  (beginning-of-line)
+;;    (if (= pos (point)) t nil)
+;;    )
+;;)
+  (interactive)
+  (if (= 0 (current-column)) t nil))
+
+
+(defun is-comment-line ()
+  (if (dap16-line-matches (format "^%c" dap16-comment-char))
+    t
+    nil)
+)
+
+(defun in-comment-spacing ()
+ (if (> (current-column) (length (format "%c%s" dap16-comment-char dap16-comment-space))) nil t)
+)
+
+(defun dap16-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 dap16-comment-char dap16-comment-space)) 
+  )
+)
+
+(defun line-start()
+  (save-excursion
+    (beginning-of-line)
+    (point)
+    )
+)
+
+
+(defun line-end()
+  (save-excursion
+    (end-of-line)
+    (point)
+    )
+)
+
+(defun dap16-comment-region()
+ (interactive)
+ (if (not (region-active-p)) (dap16-comment-line)
+   (save-excursion 
+     (goto-char (region-end))
+     (setq lastline (line-number))
+     (goto-char (region-beginning))
+     (while (< (line-number) lastline)
+       (dap16-comment-line)
+       (next-line 1)
+       ))))
+    
+   
+(defun dap16-backspace ()
+  (interactive)
+  (if (and (is-comment-line) (in-comment-spacing))
+      (delete-region (max (- (line-start) 1) 1) (point))
+     (delete-backward-char 1)
+  )
+;;delete-backward-char (length (format "%c%s" dap16-comment-char dap16-comment-space))) (tab-to-tab-stop))
+;; (delete-backward-char 1)
+  )
+
+(defun dap16-fulldelim ()
+  (interactive)
+  (make-local-variable 'count)
+  (setq count 0)
+  (if (dap16-line-matches (format "^%c%s$" dap16-comment-char dap16-comment-space)) 
+       (progn (setq count 1)
+       (end-of-line)
+       (delete-backward-char (length dap16-comment-space)))
+  )
+  
+  (while (< count 80) 
+    (insert dap16-comment-char) 
+    (setq count ( 1+ count)))
+)
+
+
+(defun dap16-halfdelim ()
+  (interactive)
+  (make-local-variable 'count)
+  (setq count 0)
+  (if (dap16-line-matches (format "^%c%s$" dap16-comment-char dap16-comment-space)) 
+       (progn (setq count 1)
+       (end-of-line)
+       (delete-backward-char (length dap16-comment-space)))
+  )
+  
+  (while (< count 40) 
+    (insert dap16-comment-char) 
+    (setq count ( 1+ count)))
+
+)
+
+
+(defun dap16-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 dap16-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" dap16-comment-char dap16-comment-space))
+    ;; Not comment line
+    (if (eolp) 
+        (progn (delete-horizontal-space)
+                (insert "\n")  
+               (tab-to-tab-stop)
+         )
+      (insert "\n")  
+     ;; (tab-to-tab-stop)
+     )
+     )
+)
+  
+
+;; XEmacs addition
+;;;###autoload(add-to-list 'auto-mode-alist '("\\.[sS]\\'" . dap16-mode))
+;;;###autoload(add-to-list 'auto-mode-alist '("\\.asm\\'" . dap16-mode))
+
+;;; dap16-mode.el ends here