Skip to content

Instantly share code, notes, and snippets.

@mlt
Last active March 30, 2021 07:55
Show Gist options
  • Save mlt/7c2030c90ac40a210b47 to your computer and use it in GitHub Desktop.
Save mlt/7c2030c90ac40a210b47 to your computer and use it in GitHub Desktop.
CR1 editing mode for CRBasic used in LoggerNet software from Campbell Scientific
;;; crbasic-mode.el --- CR1 editing mode
;; Copyright (C) 2014 Mikhail Titov
;; Author: Mikhail Titov <mlt@gmx.us>
;; URL: https://
;; Keywords: crbasic, cr1
;; Version: 1.0.0
;; This program 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 3 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;; Major mode for editing CR1 files with CRBasic for LoggerNet
;; software from Campbell Scientific.
;;
;; It is heavily based on mode tutorial from Emacs
;; wiki. http://www.emacswiki.org/emacs/ModeTutorial
(defvar crbasic-mode-hook nil)
(defvar crbasic-mode-map
(let ((map (make-keymap)))
(define-key map "\C-j" 'newline-and-indent)
map)
"Keymap for crbasic major mode")
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.cr1\\'" . crbasic-mode))
(defvar crbasic-font-lock-keywords
(list
'("'.*$" . font-lock-comment-face)
'("\\<[#]\\(If\\|Else\\|EndIf\\)\\>" . font-lock-preprocessor-face)
'("\\<\\(If\\|Then\\|Else\\|EndIf\\|For\\|To\\|\\(Next\\)?\\(Sub\\)?Scan\\|Next\\|\\(Begin\\|End\\)Prog\\|\\(Data\\|End\\)Table\\)\\>" . font-lock-builtin-face)
'("\\<\\(Dim\\|Alias\\|As\\|Const\\|Public\\|Units\\|CallTable\\)\\>\\|[()<>=*]+" . font-lock-keyword-face)
'("\\<\\(DataInterval\\|Minimum\\|Maximum\\|Average\\|Sample\\|Totalize\\|WindVector\\|PortSet\\|Delay\\|SerialFlush\\|Serial\\(In\\|Open\\)\\|SplitStr\\|Battery\\|PanelTemp\\|VoltDiff\\|PulseCount\\)\\>" . font-lock-function-name-face)
'("\\<\\(IEEE4\\|mV[[:digit:]]+\\|Min\\|m?Sec\\|_60Hz\\|Com[[:digit:]]\\)\\>" . font-lock-constant-face)
'("\\<Long\\>" . font-lock-type-face)
;; '("\\('\\w*'\\)" . font-lock-variable-name-face)
)
"Default highlighting expressions for CRBasic mode")
(defun crbasic-indent-line ()
"Indent current line as CRBasic code"
(interactive)
(beginning-of-line)
(if (bobp) ; Check for rule 1
(indent-line-to 0)
(let ((not-indented t) cur-indent)
(if (looking-at "^[ \t]*\\(End\\|NextScan\\|Next\\)") ; Check for rule 2
(progn
(save-excursion
(forward-line -1)
(setq cur-indent (- (current-indentation) tab-width)))
(if (< cur-indent 0)
(setq cur-indent 0)))
(save-excursion
(while not-indented
(forward-line -1)
(if (looking-at "^[ \t]*\\(End\\|Next\\(Sub\\)?Scan\\|Next\\)") ; Check for rule 3
(progn
(setq cur-indent (current-indentation))
(setq not-indented nil))
; Check for rule 4
(if (looking-at "^[ \t]*\\(BeginProg\\|If.+Then[ \t]*\\($\\|'\\)\\|For\\|DataTable\\)")
(progn
(setq cur-indent (+ (current-indentation) tab-width))
(setq not-indented nil))
(if (bobp) ; Check for rule 5
(setq not-indented nil)))))))
(if cur-indent
(indent-line-to cur-indent)
(indent-line-to 0))))) ; If we didn't see an indentation hint, then allow no indentation
(defvar crbasic-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?' "< b" st)
(modify-syntax-entry ?\n "> b" st)
(modify-syntax-entry ?_ "w" st)
st)
"Syntax table for crbasic-mode")
(define-derived-mode crbasic-mode fundamental-mode "CRBasic"
"Major mode for editing CRBasic files."
(setq tab-width 2)
(setq comment-start "'")
(set (make-local-variable 'font-lock-defaults) '(crbasic-font-lock-keywords))
(set (make-local-variable 'indent-line-function) 'crbasic-indent-line))
(provide 'crbasic-mode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment