sysrpl-mode.el 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ;;; -*- mode: emacs-lisp; lexical-binding: t -*-
  2. ;;; sysrpl-mode.el -- Major mode for the SysRPL programming language
  3. ;; Copyright (C) 2014 Paul Onions
  4. ;; Author: Paul Onions <paul.onions@acm.org>
  5. ;; Keywords: RPL, SysRPL, HP48, HP49, HP50, calculator
  6. ;; This file is free software, see the LICENCE file in this directory
  7. ;; for copying terms.
  8. ;;; Commentary:
  9. ;; A major mode for the SysRPL language, the system programming
  10. ;; language of HP48/49/50-series calculators.
  11. ;;; Code:
  12. (require 'cl-lib)
  13. (require 'rpl-base)
  14. (require 'rpl-edb)
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16. ;; Customizations
  17. ;;
  18. (defcustom rpl-sysrpl-default-calculator :HP50G
  19. "Default calculator type for SysRPL mode."
  20. :type '(radio :HP38G :HP39G :HP48G :HP49G :HP50G)
  21. :group 'rpl)
  22. (defface sysrpl-name '((t :foreground "darkblue"))
  23. "Face used for displaying SysRPL names (e.g DROP)."
  24. :group 'rpl)
  25. (defface sysrpl-keyword '((t :foreground "purple"))
  26. "Face used for displaying SysRPL keywords (e.g. :: ;)."
  27. :group 'rpl)
  28. (defface sysrpl-comment '((t :foreground "darkgreen"))
  29. "Face used for displaying SysRPL comments."
  30. :group 'rpl)
  31. (defcustom sysrpl-font-lock-name-face 'sysrpl-name
  32. "Name of face to use for displaying SysRPL names."
  33. :type 'symbol
  34. :group 'rpl)
  35. (defcustom sysrpl-font-lock-keyword-face 'sysrpl-keyword
  36. "Name of face to use for displaying SysRPL keywords."
  37. :type 'symbol
  38. :group 'rpl)
  39. (defcustom sysrpl-font-lock-comment-face 'sysrpl-comment
  40. "Name of face to use for displaying SysRPL comments."
  41. :type 'symbol
  42. :group 'rpl)
  43. (defun sysrpl-edb-calculator (calculator)
  44. "Map SysRPL calculator identifier to EDB identifier."
  45. (cond ((eql calculator :HP38G) :38G)
  46. ((eql calculator :HP39G) :39G)
  47. ((eql calculator :HP48G) :48G)
  48. ((eql calculator :HP49G) :49G)
  49. ((eql calculator :HP50G) :49G)))
  50. (defvar sysrpl-mode-syntax-table
  51. (let ((table (make-syntax-table prog-mode-syntax-table)))
  52. (modify-syntax-entry ?: "w" table)
  53. (modify-syntax-entry ?\; "w" table)
  54. (modify-syntax-entry ?! "w" table)
  55. (modify-syntax-entry ?@ "w" table)
  56. (modify-syntax-entry ?# "w" table)
  57. (modify-syntax-entry ?$ "w" table)
  58. (modify-syntax-entry ?% "w" table)
  59. (modify-syntax-entry ?^ "w" table)
  60. (modify-syntax-entry ?& "w" table)
  61. (modify-syntax-entry ?\? "w" table)
  62. (modify-syntax-entry ?- "w" table)
  63. (modify-syntax-entry ?_ "w" table)
  64. (modify-syntax-entry ?= "w" table)
  65. (modify-syntax-entry ?+ "w" table)
  66. (modify-syntax-entry ?* "w" table)
  67. (modify-syntax-entry ?/ "w" table)
  68. (modify-syntax-entry ?< "w" table)
  69. (modify-syntax-entry ?> "w" table)
  70. (modify-syntax-entry ?| "w" table)
  71. table)
  72. "The SysRPL syntax table.")
  73. (defun sysrpl-font-lock-compile-keywords (names)
  74. "Construct a list of keyword matcher clauses suitable for `font-lock-keywords'."
  75. (append (list (list "^\\*.*$" (list 0 'sysrpl-font-lock-comment-face))
  76. (list "(.*)" (list 0 'sysrpl-font-lock-comment-face))
  77. (list (concat "\\<" (regexp-opt '("::" ";")) "\\>") (list 0 'sysrpl-font-lock-keyword-face)))
  78. (mapcar (lambda (str) (list (concat "\\<" (regexp-quote str) "\\>")
  79. (list 0 'sysrpl-font-lock-name-face)))
  80. names)))
  81. (defvar sysrpl-font-lock-keywords
  82. (sysrpl-font-lock-compile-keywords (rpl-edb-all-names (sysrpl-edb-calculator rpl-sysrpl-default-calculator))))
  83. (defvar sysrpl-selected-calculator rpl-sysrpl-default-calculator
  84. "Currently selected calculator model.")
  85. (defun sysrpl-select-hp38g ()
  86. "Set the currently selected calculator model to be the HP38G."
  87. (interactive)
  88. (setq sysrpl-selected-calculator :HP38G)
  89. (setq sysrpl-font-lock-keywords
  90. (sysrpl-font-lock-compile-keywords (rpl-edb-all-names (sysrpl-edb-calculator :HP38G))))
  91. (sysrpl-mode))
  92. (defun sysrpl-select-hp39g ()
  93. "Set the currently selected calculator model to be the HP39G."
  94. (interactive)
  95. (setq sysrpl-selected-calculator :HP39G)
  96. (setq sysrpl-font-lock-keywords
  97. (sysrpl-font-lock-compile-keywords (rpl-edb-all-names (sysrpl-edb-calculator :HP39G))))
  98. (sysrpl-mode))
  99. (defun sysrpl-select-hp48g ()
  100. "Set the currently selected calculator model to be the HP48G."
  101. (interactive)
  102. (setq sysrpl-selected-calculator :HP48G)
  103. (setq sysrpl-font-lock-keywords
  104. (sysrpl-font-lock-compile-keywords (rpl-edb-all-names (sysrpl-edb-calculator :HP48G))))
  105. (sysrpl-mode))
  106. (defun sysrpl-select-hp49g ()
  107. "Set the currently selected calculator model to be the HP49G."
  108. (interactive)
  109. (setq sysrpl-selected-calculator :HP49G)
  110. (setq sysrpl-font-lock-keywords
  111. (sysrpl-font-lock-compile-keywords (rpl-edb-all-names (sysrpl-edb-calculator :HP49G))))
  112. (sysrpl-mode))
  113. (defun sysrpl-select-hp50g ()
  114. "Set the currently selected calculator model to be the HP50G."
  115. (interactive)
  116. (setq sysrpl-selected-calculator :HP50G)
  117. (setq sysrpl-font-lock-keywords
  118. (sysrpl-font-lock-compile-keywords (rpl-edb-all-names (sysrpl-edb-calculator :HP50G))))
  119. (sysrpl-mode))
  120. (defun sysrpl-get-eldoc-message ()
  121. (interactive)
  122. (rpl-edb-get-stack-effect (sysrpl-edb-calculator sysrpl-selected-calculator)
  123. (thing-at-point 'word)))
  124. (defun sysrpl-apropos-thing-at-point (name)
  125. "Show information about NAME in a popup buffer.
  126. When called interactively NAME defaults to the word around
  127. point."
  128. (interactive (list (completing-read "Apropos: " (rpl-edb-all-names (sysrpl-edb-calculator sysrpl-selected-calculator))
  129. nil nil (thing-at-point 'word))))
  130. (let ((bufname (format "*SysRPL: %s*" name)))
  131. (with-current-buffer (get-buffer-create bufname)
  132. (setq buffer-read-only nil)
  133. (erase-buffer)
  134. (insert (rpl-edb-get-stack-effect (sysrpl-edb-calculator sysrpl-selected-calculator) name))
  135. (newline)
  136. (insert (rpl-edb-get-description (sysrpl-edb-calculator sysrpl-selected-calculator) name))
  137. (newline)
  138. (insert (format "Address: %s" (rpl-edb-get-address (sysrpl-edb-calculator sysrpl-selected-calculator) name)))
  139. (newline)
  140. (insert (format "Flags: %s" (rpl-edb-get-flags (sysrpl-edb-calculator sysrpl-selected-calculator) name)))
  141. (newline)
  142. (end-of-buffer)
  143. (help-mode)
  144. (set-buffer-modified-p nil)
  145. (setq buffer-read-only t))
  146. (fit-window-to-buffer (display-buffer bufname))))
  147. (defvar sysrpl-mode-map
  148. (let ((map (make-sparse-keymap))
  149. (menu-map (make-sparse-keymap)))
  150. (set-keymap-parent map rpl-common-keymap)
  151. ;; Menu items
  152. (define-key map [menu-bar rpl-menu] (cons "RPL" menu-map))
  153. (define-key menu-map [sysrpl-menu-separator-1]
  154. '(menu-item "--"))
  155. (define-key menu-map [sysrpl-menu-select-hp50g]
  156. '(menu-item "HP50G" sysrpl-select-hp50g
  157. :button (:radio . (eql :HP50G sysrpl-selected-calculator))))
  158. (define-key menu-map [sysrpl-menu-select-hp49g]
  159. '(menu-item "HP49G" sysrpl-select-hp49g
  160. :button (:radio . (eql :HP49G sysrpl-selected-calculator))))
  161. (define-key menu-map [sysrpl-menu-select-hp48g]
  162. '(menu-item "HP48G" sysrpl-select-hp48g
  163. :button (:radio . (eql :HP48G sysrpl-selected-calculator))))
  164. (define-key menu-map [sysrpl-menu-select-hp39g]
  165. '(menu-item "HP39G" sysrpl-select-hp39g
  166. :button (:radio . (eql :HP39G sysrpl-selected-calculator))))
  167. (define-key menu-map [sysrpl-menu-select-hp38g]
  168. '(menu-item "HP38G" sysrpl-select-hp38g
  169. :button (:radio . (eql :HP38G sysrpl-selected-calculator))))
  170. map)
  171. "The SysRPL mode local keymap.")
  172. (defvar sysrpl-mode-hook nil
  173. "Hook for customizing SysRPL mode.")
  174. (define-derived-mode sysrpl-mode prog-mode "SysRPL"
  175. "Major mode for the SysRPL language."
  176. :group 'rpl
  177. (make-local-variable 'eldoc-documentation-function)
  178. (setq eldoc-documentation-function 'sysrpl-get-eldoc-message)
  179. (setq font-lock-defaults (list 'sysrpl-font-lock-keywords))
  180. (setq rpl-menu-compile-file-enable nil))
  181. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  182. ;; End of file
  183. ;;
  184. (provide 'sysrpl-mode)