how to use updated .erlang file without reopening emacs? - emacs

In my .emacs, It contains the following two parts of codes.
I am debugging two programs together, one is server, and the other is client.
Everytime switching the debuged program, it is necessary to modify the .erlang for switching the code:add_paths statements and reopening the emacs. (In server and client programs, 3 modules are same, if not switching, the module conflict.
Because reopening emacs is much time-costing ( many files have to be reopened), is it possible for make two .erlang file effective with reopening emacs.
(erlang-shell)
(add-hook 'erlang-mode-hook
(lambda ()
(setq inferior-erlang-machine-options '("-name" "emacs"))
;(setq inferior-erlang-machine-options '("-name" "emacs""-mnesia dir"
"/Users/yuchen/Documents
/Project/mnesia_db") )
(set (make-local-variable 'compile-command) (format "make -f %s"
(get-closest-pathname)))
(imenu-add-to-menubar "imenu")))

You just need to finish your erlang session (switch to *erlang* buffer, enter q(). and press ENTER), edit ~/.erlang and start erlang again with erlang-shell

Related

Getting the buffer / file name inside of emacs-ipython-notebook

I'm playing around with notebooks in emacs. My current setup is EIN (emacs-ipython-notebook) for interactive support, and jupytext for converting .ipynb files to .py, which is useful for diffs and code reviews. Right now I have to run a jupytext shell command to do the sync, but I would like to do this automatically on save, similarly to how jupytext supports this out of the box if you're using Jupyter. I tried the following
(defun sync-jupytext ()
"Sync linked files via jupytext."
(shell-command-to-string (format "jupytext --sync %s" buffer-file-name)))
(add-hook 'after-save-hook #'sync-jupytext)
Unfortunately this doesn't work as buffer-file-name seems to be nil once the ein mode is activated. (It works if I don't C-c C-o to start interactive mode though.) My e-lisp isn't good enough to figure out what variable or code to write instead to get the file name. Can someone help with this?
Ein uses polymode to support multiple major modes in a buffer, and as a result the working buffer isn't associated with the notebook file directly.
The notebook path, which should work in place of buffer-file-name, can be accessed via (ein:$notebook-notebook-name (ein:get-notebook)).
I think for syncing with jupytext you could add an advice around ein:notebook-save-notebook-success to sync the notebook (there might be a cleaner way using the ein:events-on mechanism, but I'm not sure how).
(defun my#sync-jupytext (orig-fn notebook &rest args)
(apply orig-fn notebook args)
(message "[jupytext] %s"
(shell-command-to-string
(format "jupytext --sync %s"
(expand-file-name (ein:$notebook-notebook-name notebook))))))
(advice-add 'ein:notebook-save-notebook-success :around #'my#sync-jupytext)
Did not test this. But maybe the:
(buffer-name)
command might work and you could construct the missing file name:
(concat (buffer-name) ".ipynb")
In emacs there is the RAM buffer and a matching disk file. It goes back to the old timey days where crashes were frequent and they wanted a disk backup.

How to open terminal in current directory?

When i use M-x shell to open a new terminal, it will sometimes set the current directory to the file in. But sometimes it won't. So is there a function to always open a new terminal in current directory?
There's the package shell-here available in ELPA: M-x list-packages, look for shell-here, mark for install (i) and execute (x).
An excerpt of the readme:
Open a shell buffer in (or relative to) default-directory,
e.g. whatever directory the current buffer is in. If you have
find-file-in-project installed, you can also move around relative
to the root of the current project.
I use Emacs shell buffers for everything, and shell-here is great
for getting where you need to quickly. The =find-file-in-project=
integration makes it very easy to manage multiple shells and
maintain your path / history / scrollback when switching between
projects.
github home: https://github.com/ieure/shell-here
And I like shell-pop too, to pop up and pop out a shell buffer window with one easily. And there's maybe more in ELPA !
M-x shell will switch to an existing shell if there's already one running, which may be your problem. If you don't mind creating lots of shell buffers, the command below will generate new buffers whenever it can't find one visiting the given directory:
(require 'cl-lib)
(defun shell-at-dir (dir)
"Open a shell at DIR.
If a shell buffer visiting DIR already exists, show that one."
(interactive (list default-directory))
(let ((buf (car (cl-remove-if-not
(lambda (it)
(with-current-buffer it
(and (derived-mode-p 'shell-mode)
(equal default-directory dir))))
(buffer-list)))))
(if buf
(switch-to-buffer buf)
(shell (generate-new-buffer-name "*shell*")))))

Emacs: disable Ido completion in Tramp mode

I often use ido for auto-completion and tramp to access remote server via ssh. My .emacs includes the following lines:
(require 'tramp)
(setq tramp-default-method "ssh")
(ido-mode 1)
(setq ido-enable-flex-matching t)
(setq ido-everywhere t)
I want to disable Ido completion, when i'm browsing contents of remote server. Note that variable ido-enable-tramp-completion has nothing to do with my problem. Consider line /root#site.com#1234:/var/www/file.txt. I need Ido not to deduct the part after the colon (remote file path), i don't care about the part before the colon. I use ssh, and Ido makes Emacs lag for a few seconds every time i run ido-find-file, and when ssh timeout is over, Tramp tries to reconnect, asks me for a password and so on. This behavior is undesirable.
Emacs version - 24.0.94.1
Edit (20.03.12): After contact with Ido author I tried to change the ido-file-name-all-completions-1 to the following:
(defun ido-file-name-all-completions-1 (dir)
(cond
((ido-nonreadable-directory-p dir) '())
;; do not check (ido-directory-too-big-p dir) here.
;; Caller must have done that if necessary.
((and ido-enable-tramp-completion
(or (fboundp 'tramp-completion-mode-p)
(require 'tramp nil t))
(string-match "\\`/[^/]+[:#]\\'" dir))
;; TRAMP RELATED CODE DELETED
nil)
(t
(file-name-all-completions "" dir))))
No success. I then changed regex to
"\\`/[^/]+[:#]"
and it worked - Ido was disabled, when minibuffer contained that match. However as Ido couldn't see files on a remote server, it started calling ido-make-merged-file-list to search for files in other directories every time i enter something. This made working with Ido on remote servers even more pain.
I also tried setting up variables ido-slow-ftp-hosts and ido-slow-ftp-host-regexps to /root#site.com#1234, didn't help.
If you enter C-x C-f again you temporally disable ido-find and fall back to the default find-file.
For more information C-h f ido-find-file RET
To do this every time ido found a colon, I guess you have to write your own function for that.

Dired copy asynchronously

Is there a way to modify/tell dired to copy files asynchronously? If you mark multiple files in dired and then use 'C' to copy them, emacs locks up until every file is copied. I instead want this copy to be started, and for me to continue editing as it goes on in the background. Is there a way to get this behaviour?
EDIT: Actually, C calls 'dired-do-copy' in dired-aux, not in dired itself. Sorry for any confusion.
I think emacs is mostly limited to a single thread - so this may not be directly possible through standard dired commands such as 'C' copy.
However, there is a dired command "dired-do-shell-command" which calls out to a shell to do the work in the background. If you select the files you want to copy and then use key '!' (this runs dired-do-shell-command) then type 'cp ? [destination]' (possibly can use 'copy' if you are on windows). I haven't tested this - so see help on "dired-do-shell-command" for full details.
See also the Emacs function dired-do-async-shell-command.
For an even more generic solution see https://github.com/jwiegley/emacs-async with which you also can evaluate arbitrary Emacs Lisp code through call to a separate Emacs process (which of course incurs a bit of extra latency). More specifically regard file operations see the file dired-async.el in this repo.
Also note that there is work on threading in Emacs under the working name Concurrent Emacs but it's not there yet. See http://www.emacswiki.org/emacs/ConcurrentEmacs for details.
I found this answer quite helpful: https://emacs.stackexchange.com/a/13802/10761. Reading that answer shows that you can make it so that dired will copy with the scp method instead of the ssh method (the latter initially encodes the file with gzip and that can be quite slow). The scp method will only copy with the scp program when the file is larger than tramp-copy-size-limit (which is 10240 by default). Using this scp method in conjunction with dired-async-mode is very nice, as it will not only copy quickly with scp, but it will also do it asynchronously and out of your way.
Also, I think this is useful: https://oremacs.com/2016/02/24/dired-rsync/. It provides this snippet of code to use rsync to copy files in dired:
;;;###autoload
(defun ora-dired-rsync (dest)
(interactive
(list
(expand-file-name
(read-file-name
"Rsync to:"
(dired-dwim-target-directory)))))
;; store all selected files into "files" list
(let ((files (dired-get-marked-files
nil current-prefix-arg))
;; the rsync command
(tmtxt/rsync-command
"rsync -arvz --progress "))
;; add all selected file names as arguments
;; to the rsync command
(dolist (file files)
(setq tmtxt/rsync-command
(concat tmtxt/rsync-command
(shell-quote-argument file)
" ")))
;; append the destination
(setq tmtxt/rsync-command
(concat tmtxt/rsync-command
(shell-quote-argument dest)))
;; run the async shell command
(async-shell-command tmtxt/rsync-command "*rsync*")
;; finally, switch to that window
(other-window 1)))
(define-key dired-mode-map "Y" 'ora-dired-rsync)

Open a file with su/sudo inside Emacs

Suppose I want to open a file in an existing Emacs session using su or sudo, without dropping down to a shell and doing sudoedit or sudo emacs. One way to do this is
C-x C-f /sudo::/path/to/file
but this requires an expensive round-trip through SSH. Is there a more direct way?
[EDIT] #JBB is right. I want to be able to invoke su/sudo to save as well as open. It would be OK (but not ideal) to re-authorize when saving. What I'm looking for is variations of find-file and save-buffer that can be "piped" through su/sudo.
The nice thing about Tramp is that you only pay for that round-trip to SSH when you open the first file. Sudo then caches your credentials, and Emacs saves a handle, so that subsequent sudo-opened files take much less time.
I haven't found the extra time it takes to save burdening, either. It's fast enough, IMO.
Tramp does not round-trip sudo via SSH, it uses a subshell. See the manual: https://www.gnu.org/software/tramp/#Inline-methods
Therefore, I recommend that you stick with TRAMP.
If you use helm, helm-find-files supports opening a file as root with C-c r.
Not really an answer to the original question, but here's a helper function to make doing the tramp/sudo route a bit easier:
(defun sudo-find-file (file-name)
"Like find file, but opens the file as root."
(interactive "FSudo Find File: ")
(let ((tramp-file-name (concat "/sudo::" (expand-file-name file-name))))
(find-file tramp-file-name)))
Your example doesn't start ssh at all, at least not with my version of TRAMP ("2.1.13-pre"). Both find-file and save-buffer work great.
At least for saving, a sudo-save package was written exactly for that kind of problem.
I recommend you to use advising commands. Put this function in your ~/.emacs
(defadvice ido-find-file (after find-file-sudo activate)
"Find file as root if necessary."
(unless (and buffer-file-name
(file-writable-p buffer-file-name))
(find-alternate-file (concat "/sudo:root#localhost:" buffer-file-name))))
(works only locally. Need to be updated to work correctly via tramp)
A little bit extended Burton's answer:
(defun sudo-find-file (file-name)
"Like find file, but opens the file as root."
(interactive "FSudo Find File: ")
(let ((tramp-file-name (concat "/sudo::" (expand-file-name file-name))))
(find-file tramp-file-name)))
(add-hook 'dired-mode-hook
(lambda ()
;; open current file as sudo
(local-set-key (kbd "C-x <M-S-return>") (lambda()
(interactive)
(message "!!! SUDO opening %s" (dired-file-name-at-point))
(sudo-find-file (dired-file-name-at-point))
))
)
)
Ugh. Perhaps you could open a shell in Emacs and exec sudo emacs.
The problem is that you presumably don't just want to open the file. You want to be able to save it later. Thus you need your root privs to persist, not just exist for opening the file.
Sounds like you want Emacs to become your window manager. It's bloated enough without that. :)
I find sudo edit function very useful for that. After opening a file, press s-e to have sudo access to edit/save the file.