How to load module only in linux? - emacs

Here is my try:
(if (eq system-type 'gnu/linux)
(load "/usr/share/emacs/site-lisp/site-gentoo")
(require 'site-gentoo))
But anyways I receive error on windows :
/.emacs':
File error: Cannot open load file, site-gentoo

Your problem is in the way you use if: its documentation says it's
(if COND THEN ELSE...)
I.e. your (require 'site-gentoo) gets executed if and only if it's not a GNU/Linux system.
Use when instead, that should do what you intend.
Also, there should actually no need to use both load and require, their usage should have the same result. The differences are mostly that require will search the load-path and don't load something again that was already loaded before.

It should be:
(if (eq system-type 'gnu/linux)
(progn
(load "/usr/share/emacs/site-lisp/site-gentoo")
(require 'site-gentoo)))
or
(when (eq system-type 'gnu/linux)
(load "/usr/share/emacs/site-lisp/site-gentoo")
(require 'site-gentoo))
Instead of (load "/usr/share/emacs/site-lisp/site-gentoo") you should add the folder containing the load file to the load-path:
(add-to-list 'load-path "/usr/share/emacs/site-lisp/")
That should do the trick. require only works for files on the load-path, load on the other hand simply evaluates the lisp file it was given as parameter.

Rörd and Bozhidar Batsov have already provided the answer as to how to resolve it, but just to add in the reason why your original code was failing.
(if COND THEN ELSE...) only accepts a single THEN command. To be able to have it evaluate multiple commands when it returns true you have to wrap the commands in (progn BODY...).
Your code was stating:
If on linux: (load "/usr/share/emacs/site-lisp/site-gentoo")
If not on linux: (require 'site-gentoo)
Using (when ...) or wrapping in (progn ...) will both provide the desired solution.

Related

Unable to run emacs in `magit-status-mode` with using custom initialization file

I like to use terminal tools and the one of them is 'magit' - awesome Git client implemented as an Emacs package. I use it to control Git projects. I have a script which automatically start emacs at computer boot (this same me a time with routine work). But also I'm looking for a way to run emacs in magit-status mode (without manual executing M-x magit-status... each time). Emacs provide a possibility to configure it's environment in init configuration file. To make emacs run magit at boot I created special magit.el file and run emacs from command line
$ emacs -q --load ~/.emacs.d/magit.el
Unfortunately I unable to switch emacs in magic-status-mode - something wrong with init file. Emacs remains in lisp-interaction-mode after boot. The content of init file is below:
;; disable welcome screen at launch
(setq inhibit-startup-screen t)
(setq visible-bell t)
; Disable tabs indent
(setq-default c-basic-offset 4
tab-width 4
indent-tabs-mode nil)
(global-set-key "\C-h" 'delete-backward-char)
;; Makes *scratch* empty.
(setq initial-scratch-message "")
;; Removes *scratch* from buffer after the mode has been set.
(defun remove-scratch-buffer ()
(if (get-buffer "*scratch*")
(kill-buffer "*scratch*")))
;(add-hook 'after-change-major-mode-hook 'remove-scratch-buffer)
;; Removes *messages* from the buffer.
;(setq-default message-log-max nil)
;(kill-buffer "*Messages*")
;; Removes *Completions* from buffer after you've opened a file.
;(add-hook 'minibuffer-exit-hook
; '(lambda ()
; (let ((buffer "*Completions*"))
; (and (get-buffer buffer)
; (kill-buffer buffer)))))
;; Don't show *Buffer list* when opening multiple files at the same time.
(setq inhibit-startup-buffer-menu t)
;; Show only one active window when opening multiple files at the same time.
;(add-hook 'window-setup-hook 'delete-other-windows)
;; Tell emacs where is your personal elisp lib dir (magit)
(add-to-list 'load-path "~/.emacs.d/lisp/")
(load "git") ;; best not to include the ending “.el” or “.elc”
;; activate installed packages
(package-initialize)
(setq-default major-mode 'magit-status-mode)
(add-hook 'after-init-hook #'magit-status-mode)
(if after-init-time
(add-hook 'after-init-hook #'magit-status-mode))
Try this:
(call-interactively 'magit-status)
Instead of all of this:
(setq-default major-mode 'magit-status-mode)
(add-hook 'after-init-hook #'magit-status-mode)
(if after-init-time
(add-hook 'after-init-hook #'magit-status-mode))
Using after-init-hook would make sense in an init file, but with -q you're explicitly not using an init file (using --load is not the same thing), and that hook has already run by the time your custom magit.el file is loaded, so nothing you add to the hook at that stage will ever be processed.
Note that you don't want to call magit-status-mode at all. That's not a major mode you would ever be expected to invoke manually, as you would never want that mode for any buffer other than the one created by the magit-status command.

"malformed function" warning and require "Cannot open load file:" error

I am getting these two issues :
In toplevel form:
init.el:28:1:Warning: `(add-path (p) (add-to-list (quote load-path) (concat
emacs-root p)))' is a malformed function
init.el:42:1:Error: Cannot open load file: exec-path-from-shell
during compiling the following elisp:
(eval-when-compile (require 'cl))
;; root of all emacs-related stuff
(eval-when-compile
(defvar emacs-root
(if (or (eq system-type 'cygwin)
(eq system-type 'gnu/linux)
(eq system-type 'linux)
(eq system-type 'darwin))
"~/.emacs.d/" "z:/.emacs.d/")
"Path to where EMACS configuration root is."))
(eval-when-compile
(defvar emacs-root "~/.emacs.d"
"Path to where EMACS configuration root is."))
;; path to where plugins are kept
(defvar plugin-path (concat emacs-root "el-get")
"*Path to el-get plugins.")
;; for portability with < 24.3 EMACS
(unless (fboundp 'cl-labels) (fset 'cl-labels 'labels))
;; add paths to various configuration modes
(cl-labels
((add-path (p)
(add-to-list 'load-path
(concat emacs-root p))))
(add-path ".")
(add-path "settings")
(add-path "site-lisp")
(add-path "erlang")
(add-path "exec-path-from-shell"))
;; set PATH, because we don't load .bashrc
(require 'exec-path-from-shell) ;; <- Error: Cannot open load file: exec-path-from-shell
both these issues are very puzzling to me.
I don't see why this fund considered "malformed"
`(add-path (p) (add-to-list (quote load-path) (concat
emacs-root p)))'
and secondly why "require" is not able to load file.
these issues only happen during compilation, not compiled code works ok
would really appreciate any pointers
Regards, Roman
cl-labels is provided by cl-lib, not by cl. So you need (require 'cl-lib) (which you can also wrap in eval-when-compile).
You need to have "cl-lib" loaded at compile time:
(eval-when-compile (require 'cl-lib))
Also, as Stefan explained in his comments in your other question, you should not define variables in eval-when-compile. Just use defvar.

Erlang flymake with nested folders in src cannot find includes folder

Sorry for my poor English.
I'm configuring my emacs with erlang flymake. Source files in src's nested folders report 'can't find include file', but files in src/folder can find the include file.
My emacs settings for erlang:
;; erlang-mode
(setq load-path (cons "/usr/local/Cellar/erlang/R15B02/lib/erlang/lib/tools-2.6.8/emacs" load-path))
(setq erlang-root-dir "/usr/local/Cellar/erlang/R15B02/lib/erlang")
(setq exec-path (cons "/usr/local/Cellar/erlang/R15B02/lib/erlang/bin" exec-path))
(require 'erlang-start)
;; distel
(add-to-list 'load-path "/usr/local/share/distel/elisp/")
(require 'distel)
(distel-setup)
;; erlang-flymake
(require 'erlang-flymake)
(erlang-flymake-only-on-save)
My erlang application folder is like following:
app/src/ (source code)
src/mod
src/lib
app/include/ (hrls)
app/ebin/ (compiled code)
...etc
In erlang-flymake there are 2 variables (erlang-flymake-get-include-dirs-function and erlang-flymake-get-code-path-dirs-function), that specify functions to search include & ebin directories. Right now, they're pointing to the functions erlang-flymake-get-include-dirs and erlang-flymake-get-code-path-dirs that simply return current dir + include and ebin correspondingly. For example, you can use following code to do this:
(defun get-erlang-app-dir ()
(let* ((src-path (file-name-directory (buffer-file-name)))
(pos (string-match "/src/" src-path)))
(if pos
(substring src-path 0 (+ 1 pos))
src-path)))
(setq erlang-flymake-get-code-path-dirs-function
(lambda ()
(concat (get-erlang-app-dir) "ebin")))
(setq erlang-flymake-get-code-include-dirs-function
(lambda ()
(concat (get-erlang-app-dir) "include")))
P.S. Are you using rebar to maintain your project?
If you use erlang-flymake you might want to look at https://github.com/ten0s/syntaxerl. It's a syntax checker for Erlang. It uses erlang-flymake under the hood, but instead of `erlc' it uses a custom syntax checker that can evaluate .erl, .hrl, .config, .rel, .app, .app.src, .escript files. The syntax checker is rebar aware, but also works with standard Erlang/OTP directory structure. Emacs's setup is also there.

How can I access the path to the current directory in an emacs directory variable file?

According to the Emacs documentation, Directory Variables apply to all files below a directory that contains an .dir-locals.el file.
How can I, in that file, set a variable to the full path that contains the file? For example:
((nil . ((indent-tabs-mode . t)
(my-project-path **THIS_DIRECTORY**))))
I asked myself the same question and found no solution on the web, so I think this answer may help. Actually, it turns out we can reuse dir-locals-find-file to get the directory containing the .dir-locals.el file. So here's what I found for, e.g, setting up an aspell personal dictionary dedicated to a whole directory:
((nil . ((eval . (setq ispell-personal-dictionary
(expand-file-name
".aspell_words"
(file-name-directory
(let ((d (dir-locals-find-file ".")))
(if (stringp d) d (car d))))))))))
Also, it seems entries are evaluated in the order they are specified, so the following code should work:
((nil . ((eval . (set (make-local-variable 'my-project-path)
(file-name-directory
(let ((d (dir-locals-find-file ".")))
(if (stringp d) d (car d))))))
(eval . (message "Project directory set to `%s'." my-project-path)))))
Emacs will complain about unsafe local variables (due to the eval construct), yet one can still permanently mark it safe.
Update: Since Emacs ≥ 26.3 (and maybe older versions as well), it appears that one needs to use (dir-locals-find-file "./") instead of (dir-locals-find-file ".").
I think (file-name-directory (or load-file-name buffer-file-name)) should give you the directory path.
See Link
Edit: Except it won't, because any eval expressions are evaluated in the context of the buffer whose variables are being hacked.
In my case, I wanted to locate a file that was relative to my current working directory for my repository, and the .dir-locals.el file was checked into the root, so a file "local" to the .dir-locals.el was also a file "local" to the project root.
pajato0's answer above worked for some cases, but it was also breaking other modes (like magit). I got around the issue by using the projectile package's projectile-project-root function to find my the base path for me:
((nil . ((eval . (setq cmake-ide-build-dir
(concat (projectile-project-root) "/build-make"))
))))
I've found the locate-dominating-file procedure, which comes out-of-the-box with Emacs, useful to retreive the current directory of a known file. The example below sets the guix-directory variable to the topmost directory of the project containing a .dir-locals.el file.
((nil . ((eval . (setq guix-directory
(locate-dominating-file default-directory
".dir-locals.el"))))))
It's not a safe .dir-locals.el setting, due to relying on eval, but it gets the job done.
In case it still matters, to the OP or some other, I would suggest you create a function to generate the .dir-locals.el file. Then one could write something like:
(let ((path default-directory)
file)
(setq file (format "%s/.dir-locals.el" path))
(with-temp-buffer
(insert (format "((nil . ((indent-tabs-mode . t)
(my-project-path \"%s\"))))" path))
(when (file-writable-p file)
(write-region (point-min)
(point-max)
file))))
to be executed within the project home directory.
hack-local-variables is the main function for processing all local variables, and it calls hack-dir-local-variables to deal with the .dir-locals.el file (or a dir local class variable, if you're not using that file).
The code for establishing the directory is not isolated in its own function, so we'll have to copy it out into a new function (this from GNU Emacs 24.0.95.1):
(defun my-dir-locals-dir ()
"Return the directory local variables directory.
Code taken from `hack-dir-local-variables'."
(let ((variables-file (dir-locals-find-file (or (buffer-file-name) default-directory)))
(dir-name nil))
(cond
((stringp variables-file)
(setq dir-name (file-name-directory variables-file)))
((consp variables-file)
(setq dir-name (nth 0 variables-file))))
dir-name))
If you are working on *nix, you might get the work directory by the following elisp code,
(defun get-working-directory ()
(getenv "PWD))
Btw, I have to mentioned that, (shell-command "pwd") will result in the directory where file (which is corresponding to the buffer you are currently editing).

Different setup in .emacs for PC/Mac

I need to have different setup in .emacs depending on my system (Mac or PC).
This post teaches how to know the system that my emacs is running.
How can I check the variable 'system-type' is to set what in emacs?
What code should I have in .emacs to have different setup for PC and Mac?
???
(when (eq system-type 'windows-nt')
)
You can do this:
(if (equal system-type 'windows-nt)
(progn
(... various windows-nt stuff ...)))
(if (equal system-type 'darwin)
(progn
(... various mac stuff ...)))
What I do in my .emacs is set a variable (I call it this-config) based on machine type and name. Then I use the same .emacs everywhere.
Using this code, I can pull the machine name out:
(defvar this-machine "default")
(if (getenv "HOST")
(setq this-machine (getenv "HOST")))
(if (string-match "default" this-machine)
(if (getenv "HOSTNAME")
(setq this-machine (getenv "HOSTNAME"))))
(if (string-match "default" this-machine)
(setq this-machine system-name))
You can then set this-config based on system-type and/or machine name.
Then I use this code:
(cond ((or (equal this-machine "machineX")
(equal this-machine "machineY"))
(do some setup for machineX and machineY))
Edit: system-type returns a symbol, not a string
My emacs says darwin, which is the name for the open OS that OSX is built on. To see the values do a describe-variable on system-type.
Note that the mac also has several possible window types so you might need to make more decisions.
Do this :
(if (eq window-system 'w32)
(progn
... your functions here for Microsoft Windows ...
))
window-system is a function and returns the name of the window system.
system-type is a variable. Do C-h v system-type RET to have the list of supported system-types for your case :
From the help :
`gnu' compiled for a GNU Hurd system.
`gnu/linux' compiled for a GNU/Linux system.
`gnu/kfreebsd' compiled for a GNU system with a FreeBSD kernel.
`darwin' compiled for Darwin (GNU-Darwin, Mac OS X, ...).
`ms-dos' compiled as an MS-DOS application.
`windows-nt' compiled as a native W32 application.
`cygwin' compiled using the Cygwin library.
Anything else (in Emacs 23.1, the possibilities are: aix, berkeley-unix,
hpux, irix, lynxos 3.0.1, usg-unix-v) indicates some sort of
Unix system.