I am looking to add an additional single line style of comments !* to the Fortran mode on emacs, I'd add this to my init.el file.
From what I can see this should be doable using the modify-syntax-entry command, but I am struggling to succeed and there doesn't seem to be a fortran-mode-syntax-table so I can't see how I'd hook it to the mode.
My current effort (which causes an error).
(modify-syntax-entry ?\!\* "< \n")
(modify-syntax-entry ?\n "< \!\*")
The error reads An error occurred while loading 'init.el':
Invalid read syntax: ?
I finally figured out how to do this, and it's worth mentioning that with a normal Fortran setup ! causes comments, but not in mine.
So what I add to my init.el is
(add-hook 'fortran-mode-hook
(lambda ()
(modify-syntax-entry ?\! ". 1")
(modify-syntax-entry ?\* ". 2")
(modify-syntax-entry ?\n ">") ))
The first two modify-syntax-entry use the numeric syntax flags for a two character comment start sequence !* and > is the syntax class for comment ended, for which I have used \n to end the comment with a newline.
See https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-Flags.html and https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-Class-Table.html#Syntax-Class-Table for more details
Related
In my font-lock-defaults I have:
("\\(^\\| \\|\t\\)\\(![^\n]+\\)\n" 2 'factor-font-lock-comment)
The comment character is ! and this makes it so comments get the right face. This works mostly, except when there is a competing font-locked entity inside the comment, like a string (delimited by double quotes):
! this line is font-locked fine
! this one is "not" because "strings"
How do you get font-lock to understand that the comment is already font-locked fine and it doesn't need to try to font-lock any strings inside of it? The obvious way is to add ! to the comment starter class in the syntax table:
(modify-syntax-entry ?! "< 2b" table)
This solution is not possible because function names and other symbols containing ! are legal such as map! filter! and foo!bar. And adding ! would cause code containing such names to be highlighted incorrectly.
Generally, it's a bad idea to highlight comments using a font-lock keyword. It's better to use the syntactic phase for this.
Even though the syntax table isn't powerful enough to describe the syntax of your language, it's still possible to highlight comments using in the syntactic font-lock phase. The solution is to provide a custom function to assign syntactic properties to the ! characters that should start a comment. This is done using the variable syntax-propertize-function.
See the elisp manual for details. Also, this tutorial covers this in great detail.
Update: The following is a simple example that define ! to be comment start character, but not within identifiers. A real world example might need a more refined way to check if something is an identifier.
(defun exmark-syntax-propertize (start end)
(funcall (syntax-propertize-rules
("[[:alnum:]_]\\(!\\)"
(1 "_")))
start
end))
(defvar exmark-mode-syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?\n "> " table)
(modify-syntax-entry ?! "< " table)
table))
(define-derived-mode exmark-mode prog-mode "!-Mark"
"Major mode for !-mark."
(set (make-local-variable 'syntax-propertize-function)
'exmark-syntax-propertize))
The new prettify-symbols-mode in Emacs works beautifully for translating:
lambda something -> λ something
I'd also like to make:
lambda.something -> λsomething
Sadly, prettify-symbols-mode only recognizes spaces as word/symbol separators by default.
Any ideas on how to use '.' as a token separator?
The code that does the actual substitution is prettify-symbols--compose-symbol in prog-mode.el. It excludes matches if the character before or after the word has the character type word or symbol. In many mode, for example emacs-lisp-mode the . character has symbol type.
You could either change the syntax code for . in the major mode, you could tell font-lock to use a different character code when highlighting (see the variable font-lock-defaults for details), or you could do ju-jutsu on the prettify-symbols--compose-symbol mode like modifying it using defadvice or simply replace it with your own.
Thanks to Lindydancer, I ended up doing this:
(add-hook 'python-mode-hook
(lambda ()
(push '("self" . ?◎) prettify-symbols-alist)
(modify-syntax-entry ?. ".")))
which results in a reasonable compromise output of:
◎.method
I am trying to create an emacs syntax highlighting for a language in which the comments are written as
; A single line comment
;; This comment has
multipline lines ;;
To do this I need to modify the entries in the syntax table. I have found that the following works perfectly for comments on multiple lines:
(modify-syntax-entry ?\; ". 1234" sbgl-mode-syntax-table)
And the following works perfectly for single line comments:
(modify-syntax-entry ?\; "< b" sbgl-mode-syntax-table)
(modify-syntax-entry ?\n "> b" sbgl-mode-syntax-table)
Does anybody know of a way to combine these?
If you can survive adding a space after each semicolon starting a single-line comment, you can treat it as an second character for one of the comment-start sequences and then here's a snippet that works for me:
(define-derived-mode sbgl-mode prog-mode "sbgl"
(set (make-local-variable 'font-lock-defaults)
'(nil ;; keywords
nil ;; keywords-only
nil ;; case-fold
((?\; . ". 1234b")
(?\n . ">")
(?\ . "- 2")))))
If not, then you always have an option to do the syntactic analysis prior to fontification via syntax-propertize-function variable (or font-lock-syntactic-keywords variable for pre-Emacs24).
You can try something like:
(modify-syntax-entry ?\; "< 1234b" sbgl-mode-syntax-table)
(modify-syntax-entry ?\n ">" sbgl-mode-syntax-table)
I have check-parens set to a save hook for my files, my Markdown files in particular, to alert me about unbalanced parentheses. They are almost always errors, and this has saved me from a great many errors involving Markdown links inside parenthetical asides:
;In Markdown files, there are few excuses for unbalanced delimiters
(add-hook 'markdown-mode-hook
(lambda ()
(when buffer-file-name
(add-hook 'after-save-hook
'check-parens
nil t))))
I've noticed that I have similar issues with quoting - I will drop a trailing quote, or I will forget to convert single and doubles appropriately, etc. (This sometimes overlaps with link errors when I put paper titles into the tooltip.) There's little more reason for imbalanced "s than there are for (s or )s, and it's the same task check-parens is already doing. So naturally I'd like to have check-parens cover quotes as well.
But I can't seem to do so! The right way seems to involve hacking the Markdown syntax table, but nothing I try seems to work –
(modify-syntax-entry ?\" "(\"" markdown-mode-syntax-table)
(modify-syntax-entry ?\" ")\"" markdown-mode-syntax-table)
(modify-syntax-entry ?\" "$\"" markdown-mode-syntax-table)
(modify-syntax-entry ?\" "^\"" markdown-mode-syntax-table)
(modify-syntax-entry ?\" ".\"" markdown-mode-syntax-table)
(modify-syntax-entry ?' "\"" markdown-mode-syntax-table)
etc etc etc. They all either do nothing or cause check-parens to spit out errors early in the file where as far as I can tell everything works just fine.
I've read through a number of links on the topic and the C-h f documentation for modify-syntax-entry:
http://www.emacswiki.org/emacs/ParenthesisMatching#toc1
https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-for-Strings.html
http://www.emacswiki.org/emacs/EmacsSyntaxTable
http://zvon.org/other/elisp/Output/SEC561.html
http://www.chemie.fu-berlin.de/chemnet/use/info/elisp/elisp_32.html
http://www.delorie.com/gnu/docs/elisp-manual-21/elisp_350.html
and asked on #emacs, to no avail.
(The version is Emacs 24.0.93.1 on Debian unstable.)
Try
(modify-syntax-entry ?\" "\"" markdown-mode-syntax-table)
How does one make M-( the default behavior for typing an opening "(" character? I want Emacs to automatically insert the closing ")" after the cursor when I type a "(" character regardless of whether it's part of an M-key combination. Additionaly, I want to extend this behavior to quotes, subquotes, brackets and braces. Typing M-( is a pain, and there don't appear to be any comparable forms for those other characters.
I don't write much elisp myself, but this is something I cribbed off somebody. The code goes into your .emacs.
(setq skeleton-pair t)
(setq skeleton-pair-on-word t) ; apply skeleton trick even in front of a word.
(global-set-key "[" 'skeleton-pair-insert-maybe)
(global-set-key "{" 'skeleton-pair-insert-maybe)
(global-set-key "(" 'skeleton-pair-insert-maybe)
(global-set-key "\"" 'skeleton-pair-insert-maybe)
Check out paredit.el which keeps parens/braces/quotes balanced as you wish, and also does offers many other features to assist with s-exp manipulation. If you're going to be writing Lisp code (as your name implies) you will probably want to use this library eventually.
"(" is bound to self-insert-command while M-'(' is insert-parenthesis. You can reverse that simply by using global-set-key or define-key to bind "(" to insert-parenthesis.