Pseudo-WYSIWYG Emacs - emacs

With just the few lines in a file file.adoc
////
-*-iimage-mode-*-
////
.A PNG smiley
image::smiley.png[]
I can generate HTML using asciidoctor and MS-word .doc using pandoc.
This toolchain doesn't handle SVG, and so it's not perfect, but it works well, and
it's free.
file.html
There is a snag though:
I'd like iimage-mode to be enabled when I load file.adoc. My attempt (the first three lines in the source above) doesn't work. Can you think of a way that will?
.emacs
(set-foreground-color "white")
(set-background-color "black")
(require 'adoc-mode)
(setq auto-mode-alist (cons '("\\.adoc\\'" . adoc-mode) auto-mode-alist))
Comments
I am writing these comments here to be able to typeset.
Asciidoctor
(I'm using to the superior implementation of the asciidoc syntax written in Ruby) does have (now?) line comments in addition to block comments.
Using
// -- mode: iimage --
on the first line works (thanks, Nick). But it replaces the adoc major mode (thanks, phils).
Adding at the bottom of the file:
//// Local Variables:
eval: (iimage-mode 1)
End:
////
switches to minor mode iimage while the major mode remains adoc.
phils's suggestion to use my-adoc-mode-hook works well.
I'm forking the second part to a separate question. This discussion is already long enough for one question.
Update
Using asciidoctor + pandoc may produce a .doc file that is handled correctly with a recent version of MS-Word. Comments welcome. MS-Word 2011 on OS X opens the resulting .doc file fine, but shows an error message in the place of SVG images.
If this works, it will (finally) be a way to send a .doc file to all those people who insist on them, while editing in an environment as flexible as Emacs.

I tried with a JPG that I had lying around and I don't get any artifacts at all.
The spec is -*- mode: iimage -*- and it's got to be on the first line of the file (it can be on the second if the first line specifies an interpreter, e.g. #! /bin bash).
If that does not work for you, you can use a file variable at the end of the file:
//// Local Variables:
//// mode: iimage
//// End:
See File Variables.
EDIT: See #phils's answer for a more correct description of how to handle minor modes.

Nick's answer is mostly correct, but iimage-mode is a minor mode, and using mode:... in local variables is (nowadays) intended for major modes only. Using it to enable minor modes is deprecated (even if it still works in some situations1).
Instead use eval:... to call the minor mode function like so2:
//// -*- eval: (iimage-mode 1); -*-
or
//// Local Variables:
//// eval: (iimage-mode 1)
//// End:
Note also that -*- iimage -*- is equivalent to -*- mode: iimage -*-, and that for both forms Emacs appends a -mode suffix to get the actual mode name.
Hence -*-iimage-mode-*- is asking to use iimage-mode-mode -- and in fact Emacs should have produced a message "Ignoring unknown mode `iimage-mode-mode'".
Finally, if you want iimage-mode enabled for all adoc-mode files, then you can use a mode hook in your init file instead of using file-local variables:
(add-hook 'adoc-mode-hook 'my-adoc-mode-hook)
(defun my-adoc-mode-hook ()
"Custom `adoc-mode' behaviours."
(iimage-mode 1))
1 In this case, if you had used -*-iimage-*- or -*-mode: iimage-*- as the only local variable mode spec, Emacs would treat it as a major mode, and your intended adoc-mode major mode would not be used.
2 I'm following Nick's answer in assuming that //// is the correct line-comment prefix, but the question makes it look more like a block comment syntax, so I don't know if it's correct.

Related

Export code block names in cweb or noweb style?

When writing a literate program in Org mode, exporting is analogous to weaving in earlier literate programming tools such as cweb or noweb. Those tools would add a code block name to the woven (exported) output. In Org mode, it would look something like this:
Org file:
#+NAME: mycodeblock
#+BEGIN_SRC language
[Source code here]
#+END_SRC
Exported output:
<mycodeblock>=
[Source code here]
I am wondering if there is any support in Org mode for exporting names of code blocks in this style. If not, is there any way to at least output the name of the code block as a label of some kind?
I have seen hints that names of code blocks can be exported, but I have failed to find the exact syntax.
You could experiment with:
;; template used to export the body of code blocks
(setq org-babel-exp-code-template
;; (concat "\n=%name=:\n"
org-babel-exp-code-template)
;; )
)
Though, that's not as nice as the results of NuWeb / NoWeb; see http://lists.gnu.org/archive/html/emacs-orgmode/2009-12/msg00170.html for a comparison of them vs Org (though the PDF links aren't accessible anymore).

What to use (instead of C-x C-f) to open a UTF-8-encoded file?

I know that including a directive like -*- coding: utf-8 -*- near the top of a file tells Emacs to assume a UTF-8 encoding, but more often than not, I want to open UTF-8-encoded files that do not include such a directive. (In some cases, modifying the file to add the coding directive would not be a big deal, but even then this is a very unsatisfactory solution.)
In such situations, what should I use (instead of plain C-x C-f) to open a file using UTF-8 as coding system?
EDIT: to be clear: even "modern Emacs" sometimes guesses the wrong coding system (possibly due to an error or corruption in the file); I want to tell Emacs: when opening this file, ignore your heuristics, assume a UTF-8 coding. Basically, I want the equivalent of a -*- coding: utf-8 -*- directive that does not require me to modify the file.
Try prefixing your command with C-xRETc to manually specify the coding system. For example:
C-xRETcutf-8RET (tell emacs that the following command should use utf-8)
C-xC-fpath/to/your/fileRET (open the file)
But as noted in the comments, this should normally not be necessary if your system is well configured.
As already said, with modern Emacs you should not have to do anything for opening correctly an utf-8 encoded file.
That said look for set-language-environment in your .emacs, You could try to remove it, or to write:
(set-language-environment "utf-8")
For configuring Emacs to use utf-8 as default encoding.

How can I make Org-mode export to LaTeX with a specific preamble?

When I do C-c C-e l to export an Org file to LaTeX it produces a document with a particular preamble. Instead of this particular preamble I would like it to use a preamble of my choice. Say that I want it to use the following preamble:
% Don't forget to qpdf --linearize the final copy
\RequirePackage[l2tabu,orthodox]{nag}% Old habits die hard. All the same, there are commands, classes and packages which are outdated and superseded. nag provides routines to warn the user about the use of those.
\immediate\write18{sh ./vc}
\input{vc}% Version control macros (for \VCDateISO in \date) http://www.ctan.org/pkg/vc
\documentclass[a4paper,12pt]{article}% pt? doublepage?
%\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}% Latin Modern (derivate of Knuth's CM)
\usepackage{fixltx2e}% \textsubscript and bugfixes for LaTeX
\usepackage{microtype}
\usepackage[strict=true]{csquotes}% Context-sensistive quotes. \enquote "" \enquote* ''. Use the integrated commands \textcquote and \blockcquote rather than biblatex internal commands to get contex sensistive quotes for them too. s/babel/autostyle in new version.
\usepackage[bookmarks,pdfborder={0 0 0}]{hyperref}% links and pdfinfo. MUST BE LOADED LAST!
\hypersetup{% Setup for hyperref
pdftitle = {[Title from #+TITLE]},
pdfauthor = {[Author from #+AUTHOR]}
}
I know that you can manipulate which packages are used on a per file basis as described in the manual but I want this preamble to be used for all files unless ) specify otherwise. The preamble I want to use includes the following:
deactivated packages (such as geometry above)
packages loaded by RequirePackage
input macros
\immediate\write18 macros
comments after usepackage macros
a hypersetup macro that recognizes #+TITLE and #+AUTHOR from Org-mode files
Deactivated packages (such as geometry above)
Org-mode recognizes LaTeX syntax inside LaTeX code-blocks, as well as when including LaTeX files in the content. (See Quoting LaTeX code.)
Packages loaded by RequirePackage
As above.
Input macros
As above.
\immediate\write18 macros
I believe this should also be as above, however there is an alternate method of dealing with this. If you create a source code block of type sh with the command within it, Org will evaluate it on export and produce the desired behaviour. You have to enable sh as a babel language type for it to work however.
(require 'ob-shell)
You can also include sh as one of the languages loaded by babel by adding it to org-babel-load-languages
(acons 'sh 't org-babel-load-languages)
Then use a code block similar to the following to run your ./vc
#+name: Test
#+begin_src sh :results output silent :exports results
./vc
#+end_src
As long as this comes before your \input{vc} line it should run the code and then include it. Simply follow the code-block with
#+LATEX: \input{vc}
And your content should be included.
Comments after usepackage macros
If the code is within a LaTeX block it should recognize it as LaTeX.
A hypersetup macro that recognizes #+TITLE and #+AUTHOR from Org-mode files.
This will have to be included within each document rather than separate. The following will provide what you desire for your macros. It will not be within the preamble, however it will end up at the top of the document and the export does behave as expected (however it will not behave as expected if added through #+INCLUDE: from org.
#+begin_latex
\hypersetup{% Setup for hyperref
pdftitle = {{{{TITLE}}}}, %Org macro to take from #+TITLE
pdfauthor = {{{{AUTHOR}}}} %Org macro to take from #+AUTHOR
}
#+end_latex
Creating your own Latex export class
If you follow the instructions in the worg tutorials (See Org Latex Export) you can create your own export-class. If you want to have full control over the packages in the preamble you would simply need to:
(add-to-list 'org-export-latex-classes
'("<CLASS NAME>"
"\\documentclass{article}
[NO-DEFAULT-PACKAGES]
[NO-PACKAGES]"
<insert desired sectioning configuration>))
You can also add in your desired packages between the \\documentclass and [NO-DEFAULT-PACKAGES] lines. The alternative would be to add them to the file itself using:
#+LATEX_CLASS: <CLASS NAME>
#+LATEX_HEADER: \usepackage{package}
...
As a third option, you can simply create a custom .sty file with the desired packages etc and include it as a single #+LATEX_HEADER:.
This doesn't answer your question, but it does allow you to do what you want.
(defun headless-latex ()
"exports to a .tex file without any preamble"
(interactive)
(org-export-as-latex 3 nil nil nil t nil)
)
This function exports the content of your ORG-mode file without any preamble. You can then \input it into a file with your desired preamble. Further reading.
I use a different method to get things done :
Define a class (I call it per-file-class for some strange reason. You can call it something else). Put this code in your .emacs :
;; per-file-class with minimal packages
(unless (find "per-file-class" org-export-latex-classes :key 'car
:test 'equal)
(add-to-list 'org-export-latex-classes
'("per-file-class"
"\\documentclass{article}
[NO-DEFAULT-PACKAGES]
[EXTRA]"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}"))))
Use this class in your org file :
#+LaTeX_CLASS: per-file-class
#+LaTeX_CLASS_OPTIONS: [10pt, a4paper]
I'm not allowed to comment (bad reputation or something ;-)) so I will post an answer. A previous answer had a very nice snippet of code for a headless export function. That code needs updating for later versions of org:
(defun headless-latex ()
"exports to a .tex file without any preamble"
(interactive)
(org-latex-export-as-latex nil nil nil t nil)
)

How do i prevent emacs from adding coding information in the first line?

In some files, especially .rb I have a problem where emacs forces me to have the following encoding line.
# -*- coding: utf-8 -*-
Even if I delete the line it will get written again once I save my file.
Is it possible to disable this feature?
It looks like this is part of the ruby-mode in emacs.
I found a link to an article that shows how to edit the ruby-mode.el file. Not sure if it works, but there is also a comment on that article that may work better:
(setq ruby-insert-encoding-magic-comment nil)
If instead of using ruby-mode your are using enh-ruby-mode you should set this variable:
(setq enh-ruby-add-encoding-comment-on-save nil)
Links:
Fix: Emacs/Aquamacs keeps adding encoding comments to my files
Also, semi-related question but pertinent answer by Michael Kohl: How can I avoid putting the magic encoding comment on top of every UTF-8 file in Ruby 1.9?
Enh-ruby-mode comment encoding line
If you've copied/pasted code from the Internet into an Emacs buffer, you might actually have non-ASCII characters. The # -*- coding: utf-8 -*- is Emacs way of telling you that it can't simply encode the file as ASCII. Scan the code for potentially offending characters (in my case it was "smart quotes" copied from http://matteomelani.wordpress.com/2011/10/17/authentication-for-mobile-devices/. Of course Ruby barfed on them.
It is the default behaviour in Emacs 28 not to write this line if the encoding is UTF-8.
At the time of writing, Emacs 28 has not yet been released, but, when it is, perhaps in 2022, upgrading should solve the problem.

How do I use autoload to correctly load custom configuration?

I use the following structure in my emacs config: For each programming mode I use, I maintain configuration in a file called programming-mode-config.el. (So python configuration will go into python-mode-config.el etc).
Earlier, I used to require each of these files in my init.el. The drawback of this approach was that my start-up time was huge. So this weekend, I sat down and converted all the requires into autoloads. Now my init file looks like this:
(autoload 'python-mode "python-mode-config" "Load python config" t)
Thus python config will not be loaded until I open a python file. This helped bring down my start-up time to about 1 second, but it doesn't work properly in all cases. For example,
(autoload 'erc "erc-mode-config" "Load configuration for ERC" t)
does not load my erc tweaks at all. Looking at the autoload documentation, it states that:
Define FUNCTION to autoload from FILE.
...
If FUNCTION is already defined other than as an autoload,
this does nothing and returns nil.
So I'm guessing that the erc config is not loaded because ERC comes 'in-built' with emacs whereas python-mode is a plugin I use. Is there any way I can get my erc configuration to load only when I actually use erc? The only other alternative I see is using eval-after-load, but it would be rather painful to put every tiny bit of my customization into an eval-after-load.
I'm afraid it might also be that I haven't grokked autoloads properly. Any help would be appreciated.
autoload is intended to be used to load functions from a certain file, not to load additional functionality - which is what it looks like you're trying to do.
Use eval-after-load instead:
(eval-after-load "erc" '(load "erc-mode-config"))
That tells Emacs to load the erc-mode-config library after the "erc" file has been loaded - which is what you want. You could also use '(require 'erc-mode-config) if you have a provide statement inside of it.
The correct use of autoload is to load the actual file that contains the symbol. So, by having
(autoload 'erc "erc-mode-config" "Load configuration for ERC" t)
You were telling Emacs to find the function erc by loading the "erc-mode-config" library, which isn't where the erc function is defined. Also, the docstring is for the function in question, so the autoload statement above makes the help string for erc be "Load configuration for ERC" - which is also incorrect.
I'm guessing your first autoload example works because you have a (require 'python) statement in your config file... but that's just a guess.