I have to edit lots of grammar files in .bnf format. Is there a mode for this in Emacs?
I've looked at CEDET's semantic package, and it seems that it USED to have a bnf-mode, but not any more. This snippet is googlable, but semantic-bnf-mode doesn't seem to exist:
(autoload 'semantic-bnf-mode "semantic-bnf" "Mode for Bovine Normal Form." t)
(add-to-list 'auto-mode-alist '("\\.bnf$" . semantic-bnf-mode))
Thanks Don. I improved the code very slightly, here's a new version.
(define-generic-mode 'bnf-mode
() ;; comment char: inapplicable because # must be at start of line
nil ;; keywords
'(
("^#.*" . 'font-lock-comment-face) ;; comments at start of line
("^<.*?>" . 'font-lock-function-name-face) ;; LHS nonterminals
("<.*?>" . 'font-lock-builtin-face) ;; other nonterminals
("::=" . 'font-lock-const-face) ;; "goes-to" symbol
("\|" . 'font-lock-warning-face) ;; "OR" symbol
("\{:\\|:\}" . 'font-lock-keyword-face) ;; special pybnf delimiters
)
'("\\.bnf\\'" "\\.pybnf\\'") ;; filename suffixes
nil ;; extra function hooks
"Major mode for BNF highlighting.")
The Semantic bnf mode was for its own internal parser format. The original 'bnf' name was a pun that ended up confusing people.
The existing Semantic modes such as wisent-grammar-mode and bovine-grammar-mode are for the grammars used by CEDET, and the original bnf-mode was similar, and did not represent a real BNF style grammar.
You are probably more interested in ebnf2ps, which translates ebnf grammars (yacc, etc) into syntax charts, though I haven't used it myself.
To be more readable and findable as an answer, jmmcd answered his own question with the following. You can find more in the emacs Help > elisp > 23.2.6 Generic Modes.
"I put this in my .emacs and it seems to work."
(define-generic-mode 'bnf-mode
'("#")
nil
'(("^<.*?>" . 'font-lock-variable-name-face)
("<.*?>" . 'font-lock-keyword-face)
("::=" . 'font-lock-warning-face)
("\|" . 'font-lock-warning-face))
'("\\.bnf\\.pybnf\\'")
nil
"Major mode for BNF highlighting.")
I just created a GNU Emacs major mode for editing BNF grammars.
Currently provides basic syntax and font-locking for BNF files. EBNF and ABNF are in my plans for the near future.
Related
I am trying to run emacs with proof generale to open Coq files. However, when I open emacs I get the following error message:
Symbol's value as variable is void: “/Users/myusername/.opam/default/bin/coqtop”
My emacs file is as follows:
(setq coq-prog-name “/Users/username/.opam/default/bin/coqtop”)
(require 'package) ;; (setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3") ; see remark below (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t) (package-initialize)
(custom-set-variables ;; custom-set-variables was added by Custom. ;; If you edit it by hand, you could mess it up, so be careful. ;; Your init file should contain only one such instance. ;; If there is more than one, they won't work right. '(package-selected-packages '(proof-general))) (custom-set-faces ;; custom-set-faces was added by Custom. ;; If you edit it by hand, you could mess it up, so be careful. ;; Your init file should contain only one such instance. ;; If there is more than one, they won't work right. )
Any advice on how to make my emacs work with coqtop?
Emacs treats “/Users/myusername/.opam/default/bin/coqtop” as a symbol because it's a sequence of ordinary characters. It does not start with an (ASCII) double quote, it starts with the character “ and ends with the character ”. They non-ASCII left and right double quotes. Use the ASCII quote ", which is the string delimiter in Emacs Lisp (and many other programming languages).
(setq coq-prog-name "/Users/username/.opam/default/bin/coqtop")
Don't edit source code with word processors that insert “smart quotes” and other features meant for text read by humans. The best place to edit your Emacs configuration is Emacs itself. Emacs knows what type of file you're editing and won't do such substitutions in programming modes (unless you've gone out of your way to configure it to do so, in which case, don't).
I'm putting the parsing techniques I'm learning into practice. I'm trying to write a programming mode in Emacs,so I can take advantage of syntax highlighting and the like. unfortunately, font-lock isn't working. Searching on Google and following the tutorials found there yielded no results. Below is my code. Any advice is appreciated.
;;;###autoload
(defgroup use-mode nil
"Mode for editing Use source files."
:group 'languages)
;;;###autoload
(defcustom use-mode-hook nil
"Hook run when use-mode is started.")
(defvar use-mode-map (make-sparse-keymap)
"Keymap for use-mode.")
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.use\\'" . use-mode))
(defvar use-keywords-1 '("use" . font-lock-keyword-face)
"First level of font-lock in Use")
(defvar use-font-lock-keywords use-keywords-1
"Code highlighting.")
;;;###autoload
(define-derived-mode use-mode prog-mode "Use"
"Major mode for editing Use source files."
(setq font-lock-defaults '(use-font-lock-keywords)))
(provide 'use-mode)
When you define font-lock keywords, you should supply a list of entries. So, if you replace:
(defvar use-keywords-1 '("use" . font-lock-keyword-face)
"First level of font-lock in Use")
With the following, it will work:
(defvar use-keywords-1 '(("use" . font-lock-keyword-face))
"First level of font-lock in Use")
Note that unless you plan to write a really advanced system, you don't need to provide different levels, so you can drop the *-1 variables.
(Just in case you haven't seen it already, I would like to recommend one of my packages, font-lock-studio. It is a interactive debugger for font-lock keywords.)
I am trying to write an Emacs major mode but it is not working; specifically, my keywords are not being displayed in keyword font face. I have tried to follow the tutorials but I must be doing something wrong. I know a little Lisp but I do not know Emacs scripting.
My Emacs mode script:
;; emacs mode for OldRope
;; does no work for some reason
(setq oldrope-directive-keywords-regexp (regexp-opt '("page" "link" "goto" "act" "end" "div" "span" "include")))
(defvar oldrope-font-lock-defaults '((
(oldrope-directive-keywords-regexp . font-lock-keyword-face))))
(define-derived-mode oldrope-mode fundamental-mode
"oldrope mode"
"Major mode for editing OldRope games"
(setq comment-start "/*")
(setq comment-end "*/")
(setq font-lock-defaults oldrope-font-lock-defaults))
(provide 'oldrope-mode)
Test file:
$[page start]$ Hello $[link]$ Click me $[act]$ That is right. $[end]$
(For context, this is part of https://github.com/martinellison/oldrope but that is not really relevant to the question).
You need this - the rest is OK:
(defvar oldrope-font-lock-defaults
`(((,oldrope-directive-keywords-regexp . font-lock-keyword-face))))
By simply quoting the list you were not evaluating oldrope-directive-keywords-regexp - your quoted list just had that symbol as its car.
Using either backquote (`) plus comma (,) or (list (list (cons oldrope-directive-keywords-regexp 'font-lock-keyword-face))) you evaluate that variable and use its value in the resulting list.
This question already has an answer here:
Code folding for LaTeX in Emacs
(1 answer)
Closed 8 years ago.
I recently write a lot of tables in my tex documents. I would like to hide the table block when I am editing otherwise it will be messy.
here is the documents:
\documentclass{article}
\begin{document}
\begin{table}[htbp]
\centering
\begin{tabular}{c|c|c}
\hline
My & Name & is \\
What's & your & name\\
\hline
\end{tabular}
\caption{My table}
\end{table}
\end{document}
Since I am using Emacs with AucTeX (under windows7), I would like to hide table block as below:
\begin{table}...\end{table}
I am try to define
(add-to-list 'hs-special-modes-alist
'(LaTeX-mode
"\\\\begin" ;; regexp for start block
"\\\\end" ;; regexp for end block
"%" ;; regexp for comment start
nil
nil))
But I didn't get what I want. Can anyone help here? thanks a lot!
muede's right, auctex's folding functionality works very good. you can add environments to be folded customizing TeX-fold-env-spec-list, and give a string that should be shown as replacement, for example [table] (or even have it show on of the environment's arguments, so not all look the same).
There is a similar feature already in Auctex, called folding.
(info "(auctex) Folding")
muede
hs-grok-mode-type expects "latex" as major-mode. Also move-forward needs it's slot.
(add-to-list 'hs-special-modes-alist
'(latex-mode
"\\\\begin" ;; regexp for start block
"\\\\end" ;; regexp for end block
"%" ;; regexp for comment start
(lambda (arg)(search-forward "\\end"))
nil))
I know nothing about Emacs Lisp (or any Lisp, for that matter). I want to do something that seems very simple, yet I have had no luck with online guides. I want to create "packet-mode.el" for .packet files. I want to do the following:
Enable C++ mode
Make packet a keyword, while leaving the rest of C++ mode unchanged
(define-derived-mode packet-mode fundamental-mode
(font-lock-add-keywords 'c++-mode `(("packet" . font-lock-keyword-face)))
(c++-mode))
(add-to-list 'auto-mode-alist '("\\.packet\\'" . packet-mode)
(provide 'packet-mode)
I've also tried switching the order of the statements in packet mode, but then the C++ highlighting breaks.
I would like packet to behave like struct in the sense that
packet foo {
int bar;
}
is highlighted the same way it would be if struct were used in place of packet.
Here is what you need to put into packet-mode.el:
(defvar packet-mode-font-lock-keywords
'(("\\<packet\\>" . font-lock-keyword-face)))
(define-derived-mode packet-mode c++-mode "Packet"
"A major mode to edit GNU ld script files."
(font-lock-add-keywords nil packet-mode-font-lock-keywords))
(add-to-list 'auto-mode-alist '("\\.packet\\'" . packet-mode))
(provide 'packet-mode)
Place packet-mode.el into a directory in your load-path and
(optionally) byte compile it.
Now, add (require 'packet-mode) into your .emacs.el.