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)
Related
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
See the following abbrev-table for emacs:
(define-abbrev-table 'global-abbrev-table '(
("8in" "∈")
("(x)" "⊗")
))
(setq-default abbrev-mode t)
If I evaluate the region above and then type in "8in", this string is abbreviated to ∈ in my emacs buffer. Great. However, if I type in "(x)", this is not abbreviated to anything. I was hoping for an abbreviation to ⊗. What have I got wrong in my global-abbrev-table definition? I have also tried with \(x\) and \\(x\\), but neither result in an abbreviation of "(x)".
(let ((syntab (copy-syntax-table)))
(modify-syntax-entry ?\( "w" syntab)
(modify-syntax-entry ?\) "w" syntab)
(set-syntax-table syntab))
(define-abbrev-table 'global-abbrev-table '(("(x)" "⊗")))
Of course, you might not want ( and ) to have word-constituent syntax in general...
Seems Emacs currently only accepts word syntax for characters composing an abbreviation.
BTW can't see a reason for that.
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 am trying to make underscores get treated as part of the word for the forward/backward-word function as described here and here. I am specifically trying to get this to work for nxhtml mode, but would really like it to work like this for all modes.
I have modified my site-start.el file a number of different ways but to no avail. But if I manually execute the command M-x modify-syntax-table in the buffer, it works just fine. I just can't get this to be the default behavior.
Here is what I tried putting in my site-start.el file:
;; 1
;; thought this would apply it to all modes - no error, but did not work
(modify-syntax-entry ?_ "w")
;; 2
;; thought this would automatically set it on a mode change - no error, but did not work
(defun change-major-mode-hook ()
(modify-syntax-entry ?_ "w"))
;; 3
;; thought this would apply it to all modes - no error, but did not work
(modify-syntax-entry ?_ "w")
;; 4
;; this produced a symbol's value as variable is void error
(modify-syntax-entry ?_ "w" nxhtml-mode-syntax-table)
What am I missing?
Have you tried modifying the nxhtml-mode-hook directly? Like so:
(add-hook 'nxhtml-mode-hook
(lambda () (modify-syntax-entry ?_ "w")))
Trey's answered your very specific question.
But Emacs already has a level of character organization that does what you ask: sexp. If you learned sexp commands instead of breaking word commands, then you can delete by "word" or by "symbol" as the situation fits.
As event_jr, I recommend you learn to use the sexp-based commands like C-M-f and C-M-b. But if you really want to only jump over identifiers (and ignore things like parentheses), then rather than try and change all syntax tables in all major modes (which may end up having unforeseen consequences), I recommend you simply use new symbol-based movement commands.
E.g. (global-set-key [remap forward-word] 'forward-symbol). For some commands (like kill-word) you'll need to write kill-symbol yourself, but it should be easy to do.
Instead of adding hooks for each major mode, as suggested by Trey Jackson, you can modify the standard syntax table. The different modes inherit from the standard syntax table so your modification will hopefully apply to all modes:
(modify-syntax-entry ?_ "w" (standard-syntax-table))
This will affect regular text, XML, Python and others.
Some modes are more stubborn, and modifying the standard syntax table won't affect them as they override some symbols during initialization. One example is cc-mode. You'll still have to modify their tables explicitly.
Is there a way to prevent automatic expansion of an abbreviation in the built-in abbrev-mode after certain symbols? E.g. I want my abbrev to expand after whitespace, newline, comma etc., but not after a dash or underscore.
I know I can hit C-q before typing the (say) underscore, but an automatic solution would be much nicer since this occurs for me very often.
There are some abbrev hooks in the manual, but since I am a total beginner with Elisp I don't see an obvious solution...
Thank you very much!
Make underscore be a word-constituent character for the current mode. From the Emacs manual, node Expanding Abbrevs:
[A]ny character that is not a word constituent expands an abbrev, and any word-constituent character can be part of an abbrev.
Use function modify-syntax-entry to modify the syntax class of _, to make it a word constituent:
(modify-syntax-entry ?_ "w")
This solution is useful only if it is not otherwise bothersome for _ to be a word-constituent character. Do you want _ to act as if it is a part of a word or not? That's the first question.
OK, so a hint of the solution was already in the question itself. This is what works for me:
(defun protect-underscore ()
(interactive)
(insert "_"))
(defun protect-dash ()
(interactive)
(insert "-"))
(defun protect-equal ()
(interactive)
(insert "="))
(global-set-key (kbd "_") 'protect-underscore)
(global-set-key (kbd "-") 'protect-dash)
(global-set-key (kbd "=") 'protect-equal)
I am sure there has to be a more elegant solution... thanks goes to Magnar.