How do I use autoload to correctly load custom configuration? - emacs

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.

Related

How and where to set the default load location for sbcl / slime / emacs on Ubuntu 18

I have emacs / sbcl / slime working.
I'm going through a tutorial and hit the following example:
CL-USER> (load "hello.lisp")
; Loading /home/peter/my-lisp-programs/hello.lisp
The author doesn't specify how or where he set things up to default the load location to his example.
I've tried creating the EMACSLOADPATH environment variable and have tried a setq for load-path all with no positive results.
If I load a .lisp file using the entire path as in /home/bill/lisp/hello.lisp, it load and I can run it. Id like to know how and where to set the default to "~/lisp" so I can avoid an absolute path reference.
Before the answer, EMACSLOADPATH (or load-path) isn't related to common-lisp or sbcl (which is a common-lisp implementation). It is an emacs related variable. The root of the confusion is understandable, as emacs is a tool build on its own variant of lisp, elisp, and you write elisp code to extend or configure emacs. Emacs tool has its own elisp engine that runs those elips commands. And then you start using emacs as an editor to write common-lisp code, using plugins like slime to make it easier to interact with sbcl interpreter (or any other slime compatible common-lisp interpreter, for that matter).
For the default load location of common-lisp, load function of sbcl uses *default-pathname-defaults* to form the pathname from the argument, and it is generally set to the current directory (by slime at least - check swank:set-default-directory).
However, if you want an approach similar to python's import, where the function uses a list of directories to search for, I believe there are two options to start with.
Use quicklisp or asdf, as they are equivalent to python-import, but that probably means you define a 'system' (python: 'module') and use asdf:load-system. Here is an answer for this solution: example of using external libs in common-lisp
Write a load function yourself, and search for a predefined list of directories for the file name, and form an absolute path then call cl:load with the absolute path you decide.

Do we need to add (require 'package_name.el) in init file if we installed packages with package manager?

As titled , I'm confused whether we should add (require 'package_name) in init file if we have already used package manager rather than installed packages manually.
Some packages, like yasnippet or auto-complete, even if I don't add (require 'yasnippet) it still works fine, but with package like alpha (a package in Marmalade), if I don't add (require 'alpha), then Emacs cannot recognize the hot key C->, which is used to increase the transparency, and I get the error message C-> is undefined, so what is the require command actually doing and when should we add it?
require loads the library in question, unless it had already been loaded.
Loading libraries takes time, so autoloading is often preferred, as this defers the load until it is needed.
package.el facilitates autoloading for ELPA packages in general, so most of the time you do not need to require these -- however it is ultimately dependent on (a) the package author, and (b) exactly how you are trying to use it.
What you describe sounds simply like a packaging bug: i.e. that alpha package from Marmalade is poorly packaged. (require '<foo>) should not change key bindings, for example, since <foo> might be loaded "by accident" for all kinds of reasons.
Instead it should probably define an alpha-mode minor mode which is autoloaded, so instead of (require 'alpha) you'd use (alpha-mode 1) (or the equivalent from Customize).

How do I use dired-read-file-name in Emacs?

I was looking for a method to show a list of filenames in a directory, and then select one of them.
I found this:
https://github.com/lawlist/dired-read-file-name and it seemed promising, so I copied it to my emacs direcory and tried:
(require 'dired-read-file-name)
but I get error
error: Required feature `dired-read-file-name' was not provided
require is not going to work because the source of dired-read-file-name.el does not have a provides expression. You might want to add:
(provide 'dired-read-file-name)
add the end of the file.
Alternatively, you can just load or more low-level load-file.
Also have a look at this.

What is the purpose of off-loading definitions with autoload in emacs? Why not autoloading is slow?

In ELisp, you can skip the evaluation of a definition with the autoload cookie. The definition is evaluated only once it's used.
;; File foo.el
;;;###autoload
(defun foo ()
"Doc"
42)
(defun bar ()
"Doc"
43)
So, if I understand correctly the autoload functionnality is a hack to load file faster. But when I load foo.el, in order to skip the definition of foo the interpreter still has to read the whole form. I don't understand why it's faster.
The simplest way to load the content of the file foo.el is to do a load.
However, this is costly because you have to read the whole file.
With autoload you are allowed to tell emacs: "function foo is defined in file foo.el".
This way emacs does not read the file and you do not pay the loading price. When you use the function foo for the first time, emacs will find the definition for you by reading foo.el.
The ;;;###autoload comment in your file is not doing anything for autoload by itself.
You need to use a program that will grab all of these definitions and put them in a file foo-autoloads.el (or any other name).
For each function it will put a line telling emacs which file contains it.
Then in your .emacs you will load foo-autoloads.el instead of foo.el.
foo.el will be read by emacs the first time you use function foo.
Note: require can also be used instead of load in the explanation above.
Where did you get that "understanding" from? Certainly not the manual.
The autoload cookies are used to pull out skeletal function/variable definitions in a separate autoload file. This is usually done at compile time.
Without going through that compilation step the autoload cookies have no effect.
The autoload forms have to be loaded separatly. They can be generated from the autoload cookies, but you can also write them manually. For example, in your .emacs you can put autoload forms for functions which you only use occasionally, so they are only loaded on demand.
For example:
(autoload 'ahk-mode "ahk-mode")

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