File local variables in Emacs - emacs

I edit a LaTeX file "file.tex" in Emacs+AUCTeX. It has file local variables set at the end of the file:
%%% Local Variables:
%%% mode: latex
%%% coding: utf-8
%%% TeX-master: "file-main"
%%% End:
In this situation the master file's buffer fails to open when "file.tex" is loaded. But if I have:
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "file-main"
%%% End:
then the buffer "file-main" opens automatically as expected.
Anyone knows how to make Emacs open the master file when the "coding" is set?

Related

Share variables between different jupyter notebooks

I have two different Jupyter notebooks, running on the same server. What I would like to do is to access some (only a few of them) of the variables of one notebook through the other notebook (I have to compare if the two different versions of the algorithm give the same results, basically). Is there a way to do this?
Thanks
Between 2 jupyter notebook, you can use the %store command.
In the first jupyter notebook:
data = 'string or data-table to pass'
%store data
del data # This will DELETE the data from the memory of the first notebook
In the second jupyter notebook:
%store -r data
data
You can find more information at here.
If you only need something quick'n dirty, you can use the pickle module to make the data persistent (save it to a file) and then have it picked up by your other notebook. For example:
import pickle
a = ['test value','test value 2','test value 3']
# Choose a file name
file_name = "sharedfile"
# Open the file for writing
with open(file_name,'wb') as my_file_obj:
pickle.dump(a,my_file_obj)
# The file you have just saved can be opened in a different session
# (or iPython notebook) and the contents will be preserved.
# Now select the (same) file to open (e.g. in another notebook)
file_name = "sharedfile"
# Open the file for reading
file_object = open(file_Name,'r')
# load the object from the file into var b
b = pickle.load(file_object)
print(b)
>>> ['test value','test value 2','test value 3']
You can use same magic commands to do this.The Cell magic: %%cache in the IPython notebook can be used to cache results and outputs of long-lasting computations in a persistent pickle file. Useful when some computations in a notebook are long and you want to easily save the results in a file.
To use it in your notebook, you need to install the module ipycache first as this Cell magic command is not a built-in magic command.
then load the module in your notebook:
%load_ext ipycache
Then, create a cell with:
%%cache mycache.pkl var1 var2
var1 = 1 # you can put any code you want at there,
var2 = 2 # just make sure this cell is not empty.
When you execute this cell the first time, the code is executed, and the variables var1 and var2 are saved in mycache.pkl in the current directory along with the outputs. Rich display outputs are only saved if you use the development version of IPython. When you execute this cell again, the code is skipped, the variables are loaded from the file and injected into the namespace, and the outputs are restored in the notebook.
Alternatively use $file_name instead of mycache.pkl, where file_name is a variable holding the path to the file used for caching.
Use the --force or -f option to force the cell's execution and overwrite the file.
Use the --read or -r option to prevent the cell's execution and always load the variables from the cache. An exception is raised if the file does not exist.
ref:
The github repository of ipycache and the example notebook

How to add a reftex-default-bibliography to latex as a local variable?

I have a main reftex-default-bibliography but I want to use a sepecific bib file in my tex file. I am using Emacs-AucTeX-RefTeX.
At the end of my Tex file I wrote:
%%% Local Variables:
%%% reftex-default-bibliography: "~/texmf/tex/latex/demo2LatexEmacs/mini.bib"
%%% End:
When I try to use the Ref command \cite I can only search my main bib file and not the local mini.bib file. Is there anything wrong with the way I wrote the code for the Local Variables?
The start of the docstring for reftex-default-bibliography says:
List of BibTeX database files which should be used if none are specified.
In other words, it looks like it's expecting a list, so you'll need to enclose your file in parentheses to make it a list of one element:
%%% Local Variables:
%%% reftex-default-bibliography: ("~/texmf/tex/latex/demo2LatexEmacs/mini.bib")
%%% End:
The answer from #Dan is correct. I would just like to indicate some essential points.
The issue was solved by using the local variables, including the following code at the end of the file:
%%% Local Variables:
%%% reftex-default-bibliography: ("/path/to/Reference")
%%% End:
The reftex-default-bibliography variable indicates to reftex where to find the bibliography file. However, there is not required to give the full path. In case that your file is in the same enclosure folder as the main file, just add:
%%% Local Variables:
%%% reftex-default-bibliography: ("./Reference")
%%% End:
If the file is one folder up(hierarchically) put:
%%% Local Variables:
%%% reftex-default-bibliography: ("../Reference")
%%% End:
Finally, you have to execute the command revert-buffer to changes take effect .

Open zsh scripts in sh-mode in emacs

*.zsh files open in the default mode (text-mode for me). However, sh-mode is actually multiple modes including behaviours for zsh, bash, etc. How can I tell emacs to open *.zsh files specifically in the zsh flavor of sh-mode?
The flavor of sh-mode is autodetected from the shebang line (first line of your script). If you have "#!/bin/zsh", zsh will be assumed and (for instance) autoload will be recognized as a keyword. autoload will be not recognized as such if first line is "#!/bin/bash"
To make emacs recognize *.zsh files as shell scripts, just add this to your init file:
(add-to-list 'auto-mode-alist '("\\.zsh\\'" . sh-mode))
A programmatic way of selecting a flavor when you don't want to use the shebang is doing this in a sh-mode buffer:
(sh-set-shell "zsh")
So in your case what you need (unless you use shebang) is to update the auto-mode-alist as above and
(add-hook 'sh-mode-hook
(lambda ()
(if (string-match "\\.zsh$" buffer-file-name)
(sh-set-shell "zsh"))))
Whether your file has a #! shebang or not, you can always use a file mode line or a local variables section to set shell-script mode. Having one of these in your script will allow Emacs to do the right thing even if you haven't updated the auto-mode-alist, so is recommended for any non-standard file extension.
The Emacs file mode line for shell scripts is -*- mode: sh -*-. It should be in a comment, and must appear on the first line (or the second line if the first one is a shebang line).
If you can't put it on the first (second) line for some reason, you can create a local variables section at the end of the file (in the last 3000 characters of the file, and on the last page, according to the manual):
# Local Variables:
# mode: sh
# End:
Note that just setting the Emacs mode will still rely on a shebang line for shell type autodetection, and if no shebang line is detected will default to the current SHELL environment variable or the value of sh-shell-file if set).
If you can't have a shebang line, but want the correct shell type to be selected, the only way to do this is with an eval in the mode line or local variables section. Adding this will generate a confirmation prompt every time the file is loaded into Emacs, so this is not generally recommended, but may be acceptable in some cases.
The mode line would be -*- mode: sh; eval: (sh-set-shell "zsh") -*-, and the local variables form would be:
# Local Variables:
# mode: sh
# eval: (sh-set-shell "zsh")
# End:
If you use the shebang method, a more robust form is
#!/usr/bin/env zsh
# env will search the path for zsh. Some distros may put it a different place.
# env is pretty much guaranteed to be in /usr/bin

Recognize a file with a shebang and no extension

I know that emacs can recognize a file by the extension, a -*- mode -*- first line, and even by the shebang line, but what do I do if I want to override the shebang?
For example, a script that starts with
#!/usr/bin/env python2.7
...
won't be recognized by the shebang line alone. I also can't add in a -*-python-*- line, because then the shell tries to parse it. How do I deal with this?
You put the -*- mode: python -*- in the second line (special exception, added specifically for the shebang thingies).
You can try putting something like
(add-to-list 'interpreter-mode-alist '("python2.7" . python-mode))
in your .emacs. See “Choosing File Modes” for more info.
Like this:
#!/usr/bin/env python2.7
print "test"
# Local Variables:
# mode: python
# End:
This information comes from Specifying File Variables node of info.
Use f1 i to enter info.
Use g (emacs) to jump to emacs info.
Use g Specifying File Variables to jump to the page.
You can use tab to complete node names.

Emacs opening and saving encoding

I have a Perl source file in utf-8 encoding, LF ending. It contains English and Chinese characters. The questions are:
1.When I open file, the encoding is windows-1251-unix. I have to run these commands:
Alt-x revert-buffer-with-coding-system
> Coding system for visited file (default nil):
utf-8-auto-unix
> Revert buffer from file file_name.pl?
y
How to automatically open it in utf-8-auto-unix?
2.When I edit the file and try to save it, Emacs gives me a question:
> Select coding system (default raw-text):
utf-8-auto-unix
How to automatically save the file in utf-8-auto-unix? And get rid of the question.
You could add this comment to the top of the file:
# -*- coding: utf-8 -*-
Use describe-variable(C-h v) to examine the variable current-language-environment; follow the customize link and set it to "UTF-8".