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

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

Related

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.

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.

How to keep cpp-mode indenting while I use another mode in Emacs?

I use a custom mode for a certain type of files just for syntax highlighting. But c-style indentation does not work when I load that mode. I don't want to write the rules for indentation for this mode.
Here is the link for the mode I'm using
How can I use the Emacs default c-style indentation in this custom mode?
Thanks.
c-mode installs its own parser to calculate indentation. Therefore, switching on c-indentation is not as simple as it seems to be (you cannot simply set indent-line-function and indent-region-function).
You can try the following:
(add-hook 'ned-mode-hook (lambda ()
(c-init-language-vars c-mode)
(c-basic-common-init 'c-mode "user")))
I noticed regular expressions for comments and strings in ned-font-lock-keywords. As far as I understand font-lock, this is wrong. Comments and strings are handled by the Parse-Partial-Sexp parser (see syntax-ppss) and not via regular expressions.
Furthermore, ned-mode should be derived from prog-mode and not from text-mode.

Prevent Auctex indentation with (fill-paragraph)

I prefer not to use indentation in my LaTeX documents because it makes cooperating with users of other editors more difficult. This is typically pretty easy to achieve, except that Auctex likes to automatically indent whenever I use (fill-paragraph).
For example, if I have something like:
\begin{abstract}
Some series of sentences here.
\end{abstract}
When I run (fill-paragraph) I get something like
\begin{abstract}
Some series of
sentences here.
\end{abstract}
I don't want the filled text to be indented.
Note that I don't want to disable this functionality Emacs-wide, because I like being able to fill indented comments and such in other modes. I just don't want it to happen in Auctex.
How can I achieve this?
I've already seen this question [1], but it only has a partial solution for latex-mode, not Auctex.
[1] Emacs: Turn off indentation when doing a paragraph fill in LaTeX mode
If you set the customization variables LaTeX-indent-level and LaTeX-item-indent to 0 you'll get rid of most or all of AucTex's indentation.
You could do something like
(add-hook 'LaTeX-mode-hook
(lambda ()
(kill-local-variable 'line-indent-function)))
so as to disable LaTeX's indentation algorithm.

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

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.