how to alias external commands in emacs 27 - emacs

I am trying to get Emacs 27 working for javascript + flow.
There are many steps/packages/configs to get this to work.
One particular step I am currently stuck on.
I cannot install npm packages globally (because our monorepo uses different versions of node_module packages for different apps within the repo).
So we cannot install flow, typescript and other things with npm -g
Instead, we need to point emacs to ./node_modules/.bin/flow (as an example).
Here is a concrete error message I am getting when starting emacs
Command "javascript-typescript-stdio" is not present on the path.
Command "typescript-language-server --stdio" is not present on the path.
Command "flow lsp" is not present on the path.
So, I would like, if possible to define in my .emacs
javacript-typescript-stdio
typescript-language-server
flow
such that they will point to /node_modules/.bin/
where is directory from which I start emacs
When searched for this topic, most queries came back with something about aliasing internal emacs functions, but that's not what I am looking for.
My .emacs section relevant to this:
;; lsp-javascript specific start
;; https://github.com/emacs-lsp/lsp-mode/issues/489
(use-package js2-mode
:mode "\\.js\\'"
:init
(add-hook 'js2-mode-hook #'js2-imenu-extras-mode)
(setf js2-mode-indent-inhibit-undo t)
:config
(with-eval-after-load "lsp-javascript-typescript"
(add-hook 'js2-mode-hook #'lsp)))
;; for flow start
(add-hook 'js2-mode-hook 'flow-minor-enable-automatically)
;; for flow end
;; JSON
(use-package json-mode
:defer t)

The exec-path holds locations to search for executables, so adding an entry there should work,
(add-to-list 'exec-path "./node_modules/.bin")
In this case, the path is relative to default-directory, which see.

Related

emacs can't find its things in its own load path

the following is output from a terminal session demonstrating that I, hopefully actually set this up right.
~ $cat .emacs
(require 'package)
(custom-set-variables
;;lots of comments generated by computer
'(package-archives (quote(("gnu" . "http://elpa.gnu.org/packages")
("marmalade" . "http://marmalade-repo.org/packages")
("melpa" . "http://melpa.milkbox.net/packages/")
("org" . "http://orgmode.org/elpa")))))
(custom-set-faces
;;again lots of comments added by the computer
)
(add-to-list 'load-path "/usr/share/emacs/24.3/site-lisp/mu4e")
~ $ ls /usr/share/emacs/24.3/site-lisp/mu43
#there are a lot of files here, but I am only going to show 2 right now
mu4e.elc
mu4e.el
... and yet emacs M-x mu4e returns [no match]. I have checked to load-path variable, and it is there. What am I doing wrong?
You need to add one more thing so that mu4e is loaded. There are two different ways to do this.
First, you could add (require 'mu4e) after you've added the path to your load-path. This will immediately load mu4e.
Alternatively, you could add the following:
(autoload 'mu4e "mu4e" "Launch mu4e and show the main window" t)
This will tell Emacs to load it lazily (i.e. not until you actually use it). Autoloading is documented here. (This is essentially done for you for packages installed via package.el - it's the same mechanism, you just don't need to specify it yourself).
The benefit of autoloads is that Emacs's initial startup is faster, since rather than loading every package right away it waits until you use them.

Let .emacs.d behaves just like a .d folder

I want to solve my “.emacs bankruptcy” issue, and I've gone through
https://help.ubuntu.com/community/EmacsHowto
http://www.emacswiki.org/emacs/DotEmacsBankruptcy
http://www.emacswiki.org/emacs/DotEmacsDotD
http://www.gnu.org/software/emacs/manual/html_node/emacs/Init-File.html
and it is still unclear to me whether the .emacs.d folder is the solution. I.e., whether it will behave just like a normal .d folder, e.g., /etc/profile.d/, where you drop you scripts and they will be picked up by the system auto-magically. Please confirm.
If not, can someone give me a script that does that, or give me a solution please?
Thanks
The essential content of my ~/.emacs file is:
(require 'cl)
(loop for src in (directory-files "~/.emacs.d" 'full-path "[0-9].*\\.el$") do
(let ((byte (concat src "c")))
(when (file-newer-than-file-p src byte)
(byte-compile-file src))
(message "Loading %s.elc" byte)
(load-file byte)))
It loads configuration files from ~/.emacs.d which start with a number. If the source file (extension .el) is newer than the byte-compiled version (extension .elc) then it byte-compiles the source. Afterwards it loads the byte compiled file.
Here's my ~/.emacs:
;; base dirs
(defvar dropbox.d "~/Dropbox/")
(defvar emacs.d (concat dropbox.d "source/site-lisp/"))
;; load path
(add-to-list 'load-path emacs.d)
(defun add-subdirs-to-load-path (dir)
(let ((default-directory dir))
(normal-top-level-add-subdirs-to-load-path)))
(add-subdirs-to-load-path emacs.d)
(load "init")
All my other scripts are loaded by ~/Dropbox/source/site-lisp/init.el
and are themselves located in ~/Dropbox/source/site-lisp.
That's how I have the same config on multiple machines.
And here's how .../site-lisp/hooks.el is loaded from init.el:
(load "hooks")
My init.el is about 100 lines, .emacs about 20 lines.
The rest 8000 lines of scripts are sliced into around 20 files.
~/.emacs.d/ does not work like /etc/profile.d/ or /etc/modules-load.d/ or similar directories, i.e. Emacs does not automatically load any Emacs Lisp file in this directory.
In fact, Emacs explicitly advises against placing Emacs Lisp libraries in ~/.emacs.d/. The byte compiler emits a warning if you add ~/.emacs.d/ to the load-path.
Instead, create a new sub-directory, e.g. ~/.emacs.d/lisp. Add this directory to your load-path explicitly, with the following code in init.el:
(add-to-list 'load-path (locate-user-emacs-file "lisp"))
Then, place your Emacs Lisp files in this directory, e.g. ~/.emacs.d/lisp/foo.el, and load them in your init.el:
(load "foo" nil 'no-message)
The best approach to avoid the dreaded .emacs bankruptcy is to actually avoid large customizations! Most notably, try to avoid any custom functions and commands.
Instead, try to a find an ELPA package that comes closest to what you want, and either try to get used to it, or customize it to your needs. If you don't find any, first try to write your own and distribute it on Github, Marmalade or MELPA.
Don't be afraid of maintaining a package in the public. You'll have to maintain your customization anyway, whether in your init.el or not, so you can just as well let other Emacs users help you with this job.
Adding code to your init.el should be your very last resort!

How to install a Emacs plugin (many times it's a .el file) on Windows platform?

I'm new to Emacs. I found many emacs plugins are released as an .el file. I'm not sure how to install them. Can I just put them in my emacs installation directory?
After placing it, say myplugin.el to your ~/.emacs.d/ directory, add the following in your .emacs file:
(add-to-list 'load-path "~/.emacs.d/")
(load "myplugin.el")
Also, in many cases you would need the following instead of the second line:
(require 'myplugin)
In any case, you should consult the documentation of the package you are trying to install on which one you should use.
If you are unsure where your ~ directory is, you may see it by typing C-x d ~/ and pressing Enter.
As already stated, you'll need the location of the file to be in Emacs' load path.
Read the comments at the top of the file to see if it has any particular installation or usage instructions. Authors often provide this information, and there isn't one single correct way to do it, so it's sensible to look.
Failing that, if the file contains a (provide 'some-name) line (typically at the end of the file), then you would be expected to use (require 'some-name) to load it.
You may also wish to byte-compile the library for speed (but that's a different question).
Many times, an emacs plugin will consist of a directory of elisp files that need to be accessible from the load path. A simple way to ensure that all individual elisp files as well as subdirectories of elisp files are included in the load path and accessible is to do something similar to the following:
Create a directory called ~/.emacs.d/site-lisp.
Install any single elisp files in the ~/.emacs.d/site-lisp directory.
Install any packages that consist of multiple elisp files in a subdirectory under your ~/.emacs.d/site-lisp directory.
Add the following code to your ~/.emacs file to ensure that Emacs "sees" all the elisp files that you have installed:
(add-to-list 'load-path "~/.emacs.d/site-lisp")
(progn (cd "~/.emacs.d/site-lisp")
(normal-top-level-add-subdirs-to-load-path))
This will ensure that all elisp files that are located either in either the ~/.emacs.d/site-lisp directory or in a subdirectory under that directory are accessible.
Some supplementary information:
MATLAB.el comes from http://matlab-emacs.sourceforge.net/
On windows, use the load path that looks like this:
(add-to-list 'load-path' "C:\\Dropbox\\Portable\\emacs\\matlab-emacs")
If you want FULL MATLAB functionality you should use:
;;MATLAB Mode:
(add-to-list 'load-path' "C:\\Dropbox\\Portable\\emacs\\matlab-emacs")
(require 'matlab-load)
if you just want to edit text files:
;;MATLAB Mode:
(add-to-list 'load-path' "C:\\Dropbox\\Portable\\emacs\\matlab-emacs")
(autoload 'matlab-mode "matlab" "Enter MATLAB mode." t)
(setq auto-mode-alist (cons '("\\.m\\'" . matlab-mode) auto-mode-alist))
(autoload 'matlab-shell "matlab" "Interactive MATLAB mode." t)

.emacs, automake and cmake

A long time ago,when I wrote my .emacs setup[1], I used a shell script to compile and join the whole thing. The thing is now very old and "crusty", so I am now rewriting it to replace things such as:
(defmacro make-new-comment( mode face strcom color1 color2)
(list 'progn
`(make-face ',face)
`(if (not (assoc ,strcom ,(intern (concat (symbol-name mode) "-comments-alist"))))
(setf ,(intern (concat (symbol-name mode) "-comments-alist"))
(append ,(intern (concat (symbol-name mode) "-comments-alist")) '((,strcom . ,face)))
)
)
`(modify-face ',face ,color1 ,color2 nil t nil nil nil nil)
)
)
and something occured to me. When compiling I access several environmental variables giving information about the system, for example[2], the full name of most programs called by some mode that uses comint[3]. Rather then reading environmental variables, i could use some autoconf like tool to tweak the .emacs files and then compile them.
The problem is that autoconf is just plain ugly. I considered cmake, but the documentation is very poor especially on constructing your own build system. I'm not familar with alternate systems.
Suggestions?
[1]: To make clear, by .emacs setup I mean the 30 or so files and two subdirs of code that I have. Not to mention several packages that ( well at the time of inclusion ) are not part of the standard emacs distribution.
[2] I've replaced eg with "" since apparently many people do not know that eg means for example. Either that or they don't know what an example is.
[3] Such as diff-mode, and ruby-mode.
which diff?
More details would be useful here. Are these environment variables that you set yourself? or things provided by your distro?
Somewhat ironically, it sounds suspiciously like emacs' incredibly powerful built-in scripting is what you're looking for.
I agree with jkerian: why are you assembling your .emacs from parts? Here is what I do: break it down by language or feature and use require and provide. My .emacs looks like:
; -*- emacs-lisp -*-
(add-to-list 'load-path "~/elisp/personal")
(require 'jdk-generic)
(require 'jdk-haskell)
(require 'jdk-keywiz)
(require 'jdk-lua)
(require 'jdk-ocaml)
(require 'jdk-org)
(require 'jdk-php)
(require 'jdk-tex)
(require 'jdk-text)
(require 'jdk-whitespace)
Each individual file in ~/elisp/personal then sets up support for a language or whatever, then provides jdk-whatever. Here is jdk-lua.el:
(add-to-list 'load-path "~/elisp/packages/lua-mode-20071122")
(add-to-list 'auto-mode-alist '("\\.lua$" . lua-mode))
(autoload 'lua-mode "lua-mode" "Lua editing mode." t)
(provide 'jdk-lua)
Notice that I keep all elisp packages in ~/elisp/packages. This means I can copy my .emacs and ~/elisp directory just about anywhere and have it work straight away.
From what I understand, you want a script to autodetect where are your tools (like diff, grep...) instead of manually telling your .emacs where they are through environment variables.
If you are on a unix-like platform, all your tools like diff, grep, should already be on your PATH and emacs should have no problem finding them. So, in your .emacs you should not use any environment variable and put directly tools name in your configuration.
If your goal is to make a portable .emacs that could be executed on Windows for example, then you should put all the gnuwin32 tools in your PATH too, so that emacs find them without problem. But for Windows, you'll have to do many other tiny arrangements for emacs commands to work properly as on a unix system.
Using a tool like autoconf is something very time-consuming for something that could be well handled by customizing one single .emacs file. If you have specific things to do for a particular system, you could write elisp code like this :
(if (eq window-system 'w32)
(progn ... ))
Also, if you want to automate the byte compilation of all your .el files, you could use a command like this on your shell :
emacs --batch -f batch-byte-compile *.el

How do I set up Aquamacs for Clojure development?

I've tried to migrate to Emacs several times for Clojure development, following a variety of blogposts, screencast and tutorials, but somewhere along the way something always went wrong - keybindings that didn't work, incompatible versions, etc, and I found myself scrambling back to Vim. But I know I want Paredit and SLIME.
So, I'm going to try again, this time backed by the powerful Stack Overflow™ community.
I hope that the answer to this question will remain up-to-date, and can serve as a reference for tentative converts like me.
What I'd like is:
The latest stable release of Clojure
Aquamacs (if it's good enough for Rich Hickey, it's good enough for me), a recent version
Clojure Mode
SLIME/SWANK
Paredit
Anything else that's indispensible?
Step-by-step instructions to install the above would be excellent - preferably in shell script format. I'd also like some hints on how to get started with the most common Clojure-related actions (including key-bindings), including links to documentation and cheatsheets.
These are the steps I took to set them up without using ELPA. Hope this helps.
Get SLIME using MacPorts
sudo port -v install slime
Get paredit
curl -O http://mumble.net/~campbell/emacs/paredit.el
Get clojure & clojure-contrib
Either using MacPorts
sudo port -v install clojure clojure-contrib
Or downloading directly
curl -O http://build.clojure.org/snapshots/org/clojure/clojure/1.1.0-master-SNAPSHOT/clojure-1.1.0-master-20091202.150145-1.jar
curl -O http://build.clojure.org/snapshots/org/clojure/clojure-contrib/1.1.0-master-SNAPSHOT/clojure-contrib-1.1.0-master-20091212.205045-1.jar
Get clojure-mode and swank-clojure (Emacs side)
git clone http://github.com/technomancy/clojure-mode.git
git clone http://github.com/technomancy/swank-clojure.git
Get swank-clojure (Clojure side)
Either downloading pre-built jar file
curl -O http://repo.technomancy.us/swank-clojure-1.1.0.jar
Or building from source (assuming lein is installed)
cd path/to/dir/swank-clojure
lein jar
Put clojure, clojure-contrib and swank-clojure .jar files in ~/.swank-clojure or ~/.clojure (the default places where swank-clojure.el searches for them).
Add to either ~/.emacs or ~/Library/Preferences/Aquamacs Emacs/customization.el (change paths to match your own settings)
(add-to-list 'load-path "/opt/local/share/emacs/site-lisp/slime/")
(add-to-list 'load-path "/opt/local/share/emacs/site-lisp/slime/contrib/")
;; Change these paths to match your settings
(add-to-list 'load-path "path/to/dir/clojure-mode/")
(add-to-list 'load-path "path/to/dir/swank-clojure/")
(add-to-list 'load-path "path/to/dir/paredit/")
;; Customize swank-clojure start-up to reflect possible classpath changes
;; M-x ielm `slime-lisp-implementations RET or see `swank-clojure.el' for more info
(defadvice slime-read-interactive-args (before add-clojure)
(require 'assoc)
(aput 'slime-lisp-implementations 'clojure
(list (swank-clojure-cmd) :init 'swank-clojure-init)))
(require 'slime)
(require 'paredit)
(require 'clojure-mode)
(require 'swank-clojure)
(eval-after-load "slime"
'(progn
;; "Extra" features (contrib)
(slime-setup
'(slime-repl slime-banner slime-highlight-edits slime-fuzzy))
(setq
;; Use UTF-8 coding
slime-net-coding-system 'utf-8-unix
;; Use fuzzy completion (M-Tab)
slime-complete-symbol-function 'slime-fuzzy-complete-symbol)
;; Use parentheses editting mode paredit
(defun paredit-mode-enable () (paredit-mode 1))
(add-hook 'slime-mode-hook 'paredit-mode-enable)
(add-hook 'slime-repl-mode-hook 'paredit-mode-enable)))
;; By default inputs and results have the same color
;; Customize result color to differentiate them
;; Look for `defface' in `slime-repl.el' if you want to further customize
(custom-set-faces
'(slime-repl-result-face ((t (:foreground "LightGreen")))))
(eval-after-load "swank-clojure"
'(progn
;; Make REPL more friendly to Clojure (ELPA does not include this?)
;; The function is defined in swank-clojure.el but not used?!?
(add-hook 'slime-repl-mode-hook
'swank-clojure-slime-repl-modify-syntax t)
;; Add classpath for Incanter (just an example)
;; The preferred way to set classpath is to use swank-clojure-project
(add-to-list 'swank-clojure-classpath
"path/to/incanter/modules/incanter-app/target/*")))
Download and install Aquamacs.
Download and install ELPA (http://tromey.com/elpa/install.html)
Do M-x package-list-packages
Mark the lines called "clojure-mode" and "swank-clojure" with "I" then press "X".
Done.
Here's a blog post that mentions Aquamacs: Setting up Clojure, Incanter, Emacs, Slime, Swank, and Paredit
There seems to be a fairly easy way to set up Aquamacs 2.4 and SLIME for clojure:
Install Clojure
Install Aquamacs 2.4 from here "http://aquamacs.org/"
Install the Aquamacs SLIME package from here "http://aquamacs.org/download.shtml"
This will not work so...
Get the latest version of SLIME from here "http://common-lisp.net/project/slime/#downloading" - you want the CVS snapshot tar file
Unpack the SLIME tar file and copy it into
/Library/Application Support/Aquamacs Emacs/SLIME
Seems to work OK for me...
I know the OP wants to use Emacs for Clojure dev. I'm an emacs fan myself, but I found using Enclojure (http://www.enclojure.org/home) to be a great way to get started quickly with hacking Clojure.
Today I would head for https://github.com/tehcurtis/aquamacs-emacs-starter-kit/network
this is for ruby and wont work at first but anyway. git clone and copy things to Preferences.el according to readme. Fix the brokenness by edit the ~/Library/Preferences/Aquamacs Emacs/ and comment out almost everything in modes.el (I have only (setq-default indent-tabs-mode nil) left in the file)
The good part: you have installed elpa-package-manager with less hassle
now: use
M-x package-list-packages
go to
clojure-mode (press I)
slime (press I)
slime-repl (press I)
Press X to install
done.
Caveat: clojure-jack-in wont work so you have to
M-x slime-connect
and press enter twice and y to start.