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

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.

Related

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

how to use updated .erlang file without reopening 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

P4CONFIG with emacs

I would like to see examples of how to setup perforce, using the config file functionality where emacs is used as the diff and merge programs (P4DIFF and P4MERGE settings). Even better if this is on Windows.
I'm also struggling with getting the P4EDITOR to work correctly when using emacsclientw, specifically specifying the alternate-editor functionality.
Any tips, suggestions, example configs are very welcome.
Here's a different trick I used to use. It adds a few command line options to emacs so that you can do diffs and merges in a new emacs instance (again using ediff).
;; -diff
(defun command-line-diff (switch)
(let ((file1 (pop command-line-args-left))
(file2 (pop command-line-args-left)))
(ediff file1 file2)))
(add-to-list 'command-switch-alist '("-diff" . command-line-diff))
;; -merge
(defun command-line-merge (switch)
(let ((base (pop command-line-args-left))
(sccs (pop command-line-args-left))
(mine (pop command-line-args-left))
(merg (pop command-line-args-left)))
(ediff-merge-with-ancestor sccs mine base () merg)))
(add-to-list 'command-switch-alist '("-merge" . command-line-merge))
Just put that in your .emacs file. Then you can set your P4DIFF program to be emacs -diff and your P4MERGE program to be emacs -merge.
I'm assuming you're already using p4.el.
Here's a function that will allow you to set your p4-client-config easily:
(defun p4-go (config)
(interactive
(list (read-file-name "P4 Config file: "
(concat (getenv "HOME") "/etc/perforce/")
""
t)))
(p4-set-client-config (expand-file-name config))
t)
Then I just run M-x p4-go <RET> conf <RET>.
My ~/etc/perforce/conf file looks like:
P4CLIENT=ewarmenhoven-ppd
P4PORT=perforce.netflix.com:1666
P4USER=ewarmenhoven
P4EDITOR=emacsclient
P4DIFF=diff -dupU8
P4MERGE=~/bin/emerge
The emerge merge program is just a short little shell script that calls emacsclient appropriately:
#!/bin/bash
base=$1
sccs=$2
mine=$3
merg=$4
emacsclient -e "(ediff-merge-files-with-ancestor \"$base\" \"$sccs\" \"$mine\" () \"$merg\")"
emacsclient "$merg"
If you're using cygwin it should work just fine.
For doing diffs, if it's running from the shell then I want the output in the shell, hence just using normal diff. If it's not, I use p4-ediff, which is bound to C-x p - by default.
The awesome answer by Eric doesn't work properly in latest emacs because of welcome screen. In order to hide the welcome screen (so that you may get the diff properly) please refer Unable to hide welcome screen in Emacs.
Another nifty setting which opens the diff in regular vertical mode is setting the below config variable
(custom-set-variables
;; custom-set-variables was added by Custom -- don't edit or cut/paste it!
;; Your init file should contain only one such instance.
'(ediff-split-window-function (quote split-window-horizontally)))

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.