How can I get flyspell to ignore TeX codes in MathJax? - emacs

So, in emacs auctex mode, it is easy to have flyspell ignore TeX math codes by adding a couple of lines to one's .emacs (or other initialization) file. At the moment I'm working on a long HTML document that contains a ton of embedded TeX code, which is being rendered by MathJax. Since it's an HTML file, I'm using nxml mode to write it (mostly to get nice syntax coloring and nice formatting). But since I'm not in auctex (or other LaTeX) mode, flyspell sees all the TeX math codes as misspellings. Is there a way to get flyspell to ignore them the way it does in auctex mode? Thanks.

Related

Is there a way to disable indentation for one specific org-mode src block at a time?

I'm using most of the standard features of org-mode that come with the spacemacs develop branch, but I haven't been able to find a way to disable the automatic indentation for source code blocks on a case by case basis. I use tangle and I'm writing Dockerfile's in the same file that I'm writing groovy code or javascript for example. The Dockerfile's are the only ones I want not to be indented so I can get syntax highlighting. Here's what it looks like without the indentation:
And here's what it looks like with the indentation that automatically happens if I edit the text:
The automatic indentation is fine for groovy for example, so I have no issue with the automatic indentation here. (in fact, if I still got the syntax highlighting for Dockerfile's I probably wouldn't mind too much except the weird word wrapping not respecting the background face). Here's the example with groovy:
As you can see, I tried a :noindent property I found in the org-mode docs that's usually in a #+STARTUP directive. I also searched stack overflow, but I didn't find anything fruitful that didn't disable indenting for all source blocks or for the entire file.
In my practice (I'm not sure it is a good practice or not, just share it to you):
I setup org-src-tab-acts-natively to true for using the language’s major-mode indentation. In your case, it will format the src block with the formatting rules of dockerfile-mode.
I created a new major-mode plain-mode for the content which should not be indented automatically. Personally, I use this mode for something like:
shell outputs
ASCII diagrams
...
#+begin_src plain
this will not
be
auto
indented
#+end_src
Above src block will not be indented when you select them as region, and TAB on them.
In this way, the src blocks will always behavior as expected when formatting them.
Aside, the example code of plain-mode:
(define-derived-mode plain-mode
clean-mode "Plain"
"Major mode for plain text."
;; preserve the auto indentation on line
(setq-local indent-line-function 'indent-relative)
;; disable auto indentation on region
(setq-local indent-region-function (lambda (start end))))

How to use emacs/elisp to highlight parts of font-locked source code

I have some log files that contain the directory paths and file names (and line numbers) for C++, C, Java and C# source code files. I have written a regex to search for these file names and line numbers and open the source code file and position the insertion point at the specified line number (kinda like the next-error function when used with the compile command).
Given a file name that appears in the log file multiple times I want to add highlighting (and selectively remove highlighting) to the source code file display window/buffer.
I can do this with functions like add-text-properties, remove-text-properties and add-face-text-property (where is remove-face-text-property?) if there is no font-lock (keyword color coding). These functions don't work if font-lock is turned on!
How do I do this if the font-lock is turned on? I see that the incremental search feature does it so it is possible to add and remove highlighting with out messing up the font-lock coding.
Thanks
Siegfried
Use overlays instead of text-properties. E.g. to highlight with face bold the text between BEG and END, do something like:
(let ((ol (make-overlay BEG END)))
(overlay-put ol 'face 'bold))
Any highlighting that uses text property face is overruled by font-lock highlighting -- font-lock wants to win. In many cases you can still highlight text, but sooner or later font-lock erases that highlighting when it refontifies the buffer.
This does not apply to highlighting that uses overlays –- font-lock has no effect on overlays. So one answer is to just use overlays. However, if that does not work for your use case (there are some downsides to using overlays) there is still hope.
To prevent the interference of font-lock with other highlighting, the typical Emacs approach is to fool font-lock into thinking that it is font-lock highlighting, even when it does not involve font-lock-keywords.
But this has the effect that such highlighting is turned off when font-lock-mode is turned off. Whether this is a good thing or bad depends on your use case.
In vanilla Emacs you have no choice about this. Either the highlighting is not recognized by font-lock, which overrules it, or it is recognized as “one of its own”, in which case it is turned off when font-lock highlighting is turned off.
If you don't need your special highlighting when font-lock-mode is turned off, then you can just use text property font-lock-face instead of property face.
If you use library highlight.el to implement your highlighting then you can do that just by leaving option hlt-face-prop at its default value of font-lock-face. (Value font-lock-face means that the highlighting is controlled by font-lock. Value face means that font-lock does not recognize the highlighting.)
For the case where the option value is face, if you also use library font-lock+.el then there is no interference by font-lock –- the highlighting is independent of font-lock.
Library font-lock+.el is loaded automatically by highlight.el, if it is in your load-path. It prevents font-locking from removing any highlighting face properties that you apply using the commands defined here.
See Highlight Library for more information.

yasnippets always puts new snippets in c-mode instead of php-mode -- how do I change this?

The yasnippets module for emacs allows the dynamic expansion of words as snippets much like abbrev mode, but more powerful, and is similar to the snippet system used in TextMate on OS X.
I'm hoping there is another php author out there using this module. When I try to create a new snippet in php-mode, it instead puts it in c-mode, as php-mode.el says it is a derived mode of c-mode. yasnippet isn't acting incorrectly, exactly, as there really is no php-mode defined here, just c-mode. However, I'd really like it if I could still have new snippets made under php-mode.
What's kind of odd is that yasnippets will read and process snippets under php-mode directories in my snippets directory. However, it also shows whatever c-mode snippets might be defined as well.
You could add a file named .yas-parents under the php-mode snippets dir and put text-mode in it. That should sever the connection with cc-mode (if that's really what you wish). I guess that some c snippets might be useful in php-mode as well.

Emacs modes for flex and bison, or removing auto indent for these modes?

Emacs has poor handling of auto-indentation in Flex and Bison. In fact, it seems to have no support for flex mode. So, how does an emacs user cope with these? I like VIm but I would prefer not to switch because I am much faster and more comfortable in Emacs.
I had a third party elisp module for Bison a few months ago but when its indentation broke, it would never get fixed. In short, it was a bad hack.
Or is there a way I can turn off auto indentation for .l and .y files (so pressing would do one indent)? How would I also change this elisp setting for just emacs?
A nice and concise guide for elisp would be very helpful too. I wouldn't mind spending a few days to write my own flex and bison modes if I had the right documentation.
Emacs chooses the major mode mainly based on the file name extension. .l is a contended extension: some people use it for lex, others for lisp (and there are a few other rarer uses). Emacs associates .l with lisp, and .lex with lex (for which it uses C mode).
If the .l files you work with are more often lex than lisp, you can change what .l files are associated with the following line in your .emacs:
(add-to-list 'auto-mode-alist '("\\.l\\'" . c-mode))
You can also declare inside a file what mode you want Emacs to use when it opens the file. Put the following snippet on the first line of the file (typically in a comment):
-*-mode: c-mode-*-
This is a more general feature, offering other syntaxes and other possibilities; see “File Variables” in the Emacs manual for more information.
If you would like to get started with Emacs Lisp, read the Emacs Lisp intro (which may be included in your Emacs or OS distribution). Once you've played around with the basics of the language a bit, you can turn to the chapter on modes in the Emacs Lisp reference manual.
Additional tip: You might decide that what you want is Emacs' generic behavior -- what it uses when it doesn't have any special mode for a file format. That's called Fundamental mode in emacs lingo: so you can request it on the fly with M-x fundamental-mode, or put -*- mode: fundamental -*- on the first line of the file, or customize auto-mode-alist like so:
(add-to-list 'auto-mode-alist '("\\.l\\'" . fundamental-mode))
Another thing to try might be indented-text-mode (probably with auto-fill disabled).

Using Emacs, how could I indent/format a code segment in a TXT file?

I am writing a document with Emacs. As you know, there are some code segments in the text file I am working with. Typically, when I open this file, emacs will get into text-mode automatically. And it works fine for me to edit the ordinary paragraphs. But for those code segments, how could I indent them into gnu or linux style just like what I could do in c-mode (by c-set-style && do Ctrl-Alt-\ in certain region)?
BTW, actually, I could turn the buffer into c-mode by invoking M-x c-mode to do this, however, I think there should be much a graceful way to do this in text-mode.
orgmode manages to do it by copying the code out to a temporary buffer where you edit & format it, and updating the changed text when you're done.
If switching to orgmode is an option, then you do it like this:
#+BEGIN_SRC emacs-lisp
(defun org-xor (a b)
"Exclusive or."
(if a (not b) b))
#+END_SRC
and start and finish editing with C-c '.
Edit: Emacswiki has a list of multiple modes.
You might be able to mark the region, then narrow the view to the region, change the mode, indent, return to text-mode, and return to the full buffer again. I forget the exact shortcuts at the moment, but it should be fairly easy to turn into a function.