Font-locking for SQL-MODE inside of ORG-MODE not font-locking - emacs

Related to question: org-mode: fontify code blocks natively
I've got the latest org-mode and emacs versions as of November 1, 2012 (org stored in org-20121105).
I've also got the sql-mode that comes with emacs-24.
I've got fontification turned one:
;; fontify code in code blocks
(setq org-src-fontify-natively t)
Yet this does not fontify in my org documents. Java, bash, etc. all work.
#+BEGIN_SRC SQL
SELECT foo FROM bar
#+END_SRC
When I open a file foobar.sql, the mode indicator says SQL[ANSI] (which I also tried as the source type), and font-locking works.
Thanks in advance for any tips.

Firstly, the name of the SRC block mode is case-sensitive. It should be sql instead of SQL.
#+BEGIN_SRC sql
SELECT foo FROM bar
#+END_SRC
Secondly, the initial font-lock of sql-mode seams not to highlight SQL keywords, (at least to me, it looks no difference no matter you turn it on or off). Therefore, you need to specify which kind of SQL you want to highlight. If you are using MySQL for example:
(add-hook 'sql-mode-hook
(lambda ()
(sql-highlight-mysql-keywords)))
Then Restart Emacs. It should work now.

Oh, wait, try putting #+BEGIN_SRC sql in lower case. See here for identifiers.
Try refreshing the display, by making the block be reparsed (break the syntax and undo, or something). It often happens to me with python or bibtex blocks, but this fixes it.
I can't see why it wouldn't fontify inline if it finds the right mode when you C-c '.
Also, I'm afraid fontification, while being one of org-mode's nicer features, is not exactly perfectly handled. From the mailing list :
The fontification engine is not very powerful and get easily fooled.

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))))

Emacs - How to wrap selection in org-mode source code block?

I have a bunch of elisp and other code with some notes i wanted to reformat to be more organized, and i found that having to type
#+BEGIN_SRC emacs-lisp ... #+END_SRC
all the time around what i want, is taking a bit longer than expected...
So what i wanted to do instead is to wrap/or put the selected content (with C-space) and put it in a template source code block for org-mode (in my case it's mostly elisp code, but i plan to use it for other things maybe)
How could i do this in emacs or in elisp?
There is a new templating mechanism in recent Org mode (>= 9.0 IIRC) that allows you tor wrap a region in a block: after selecting the region in the usual manner, you say C-c C-, s. You still have to type the emacs-lisp part though. That's the disadvantage. The advantage is that it is general enough to allow you to wrap a region in any kind of block. In your case, I think the disadvantage outweighs the advantage, so I would go with the wrap-region method in the other answer, but this one is good to know as well.
You can try wrap-region. It will allow you to define what type of string you want to wrap around a selection.
Put this in your init.el and evaluate it.
(wrap-region-global-mode t)
(wrap-region-add-wrapper "#+BEGIN_SRC emacs-lisp\n" "#+END_SRC" "#" 'org-mode)
Then, while you are editing your org files, you can select a block of text and type #, which will wrap it with your string. You can change the # to another character that will do the wrapping.
There is a feature in org-mode to do exactly that. It's like a snippet of some sort where you enter <eland hit TAB, the < char is here to say we're gonna use a template and the el part tells which template to use. But of course, you have to configure it first.
For that, you can just add this to an org-mode file or to your init.el file :
#+begin_src emacs-lisp
;; This is needed as of Org 9.2
(require 'org-tempo)
(add-to-list `org-structure-template-alist `("sh" . "src shell"))
(add-to-list `org-structure-template-alist `("el" . "src emacs-lisp"))
(add-to-list `org-structure-template-alist `("py" . "src python"))
#+end_src
There a bunch of way to use this, it's actually more useful than just use it as a template, you can go check the documentation here.

modify-syntax-entry is not loaded in emacs init?

Since I don't want underline char _ to break a word, I put the script:
(modify-syntax-entry ?_ "w")
in the Emacs init file. The purpose is that for words like test_test, to double click the word will select the whole word before and after _.
But for some reason, the script is not working in initialization.
If I run
M-x eval-buffer
Then it will start to be effective. Why is that?
BTW, all other setups in init are working well, it is only the modify-syntax-entry that requires an additional evaluation after the initial launch.
See the Elisp manual, node Syntax Basics. There you will see this:
Typically, each major mode has its own syntax table, which
it installs in all buffers that use that mode. For example, the
variable emacs-lisp-mode-syntax-table holds the syntax table used by
Emacs Lisp mode, and c-mode-syntax-table holds the syntax table used
by C mode. Changing a major mode’s syntax table alters the syntax in
all of that mode’s buffers, as well as in any buffers subsequently put
in that mode.
You need to change the syntax table of the current buffer (which will
typically make the change for all buffers in the same mode).
The current syntax table, that is, the syntax table of the current buffer, is returned by function syntax-table. So that is what you pass to modify-syntax:
;; Modify the CURRENT syntax table (not `standard-syntax-table').
(modify-syntax-entry ?_ "w" (syntax-table))
Function syntax-table is described in the Elisp manual, node Syntax Table Functions.
However, you seem to want to make this change for all buffers, whatever the mode. Is that right? That's probably a mistake.
What you should probably do is this:
Decide which modes you want to modify this way.
For those modes, put the call to modify-syntax-entry in a function that you add to the mode hook of each of those modes (using add-hook).
For example, to modify the syntax of, say, foo-mode:
(defun my-syntax () (modify-syntax-entry ?_ "w" (syntax-table)))
(add-hook 'foo-mode-hook 'my-syntax)
A sideways solution: replace your problematic line of code with
(global-superword-mode 1)
This will tell Emacs that you like your "word movements" to actually operate on identifiers rather than what Emacs traditionally defines as words. The advantage is that it keeps the normal definition of words for internal use, so it should in theory break less code than your approach.
Every buffer has its own syntax table, which is usually dependent on the major mode of the buffer. When you run your code in the Emacs init file, it just modifies the syntax table of the temporary buffer that's used while loading the file, not your regular buffers.
Most syntax tables inherit from the standard syntax table, so you can make your change to that table by giving another argument to modify-syntax-entry:
(modify-syntax-entry ?_ "w" (standard-syntax-table))

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.