Scripting magit timing problems - emacs

C-x v = vc-diff is good. However, I can work with the diff
directly if the diff was shown in a magit-status buffer.
I've tried to do just that here:
(defvar le::vc-diff-data nil)
(defun le::magit-go-to-change-once ()
(destructuring-bind (filename orig-buff relative-name) le::vc-diff-data
(pop-to-buffer "*magit: magit*")
(goto-char (point-min))
(if (and (search-forward-regexp "^Changes:$" nil t)
;; WIP fix
(progn (magit-show-level-2) t)
(search-forward relative-name nil t))
(progn (recenter-top-bottom 0)
;; WIP fix me here
(magit-show-level-4)
)
;; no diff
(pop-to-buffer orig-buf)
(message "no diff found.")))
(remove-hook 'magit-refresh-status-hook #'le::magit-go-to-change-once))
(defadvice vc-diff (around magit-redirect activate compile)
"redirect to magit"
(let* ((vc-info (vc-deduce-fileset t))
(filename (buffer-file-name))
(orig-buf (current-buffer))
(relative-name (replace-regexp-in-string
(concat "\\`"
(regexp-quote (expand-file-name (locate-dominating-file filename ".git"))))
"" filename)))
(if (string-equal "Git" (car vc-info))
(progn
(setq le::vc-diff-data (list filename orig-buf relative-name))
(add-hook 'magit-refresh-status-hook #'le::magit-go-to-change-once)
(call-interactively 'magit-status))
ad-do-it)))
However the "magit-show-level*" function fails. It works when I use
eval-expression in the magit buffer though. So maybe this is a timing issue
and I have to hook in somewhere else.

As said in my comment, the hook is called at a time when section related function will not work. You could try magit from there: https://github.com/vanicat/magit/tree/t/refresh-stasus-hook, your code should work with it.
Bye the way your proposition is interesting, and integrate it into magit contrib's proposition could be great.

Related

emacs function after asynchonous command

I currently use emacs for making and editing LaTeX documents. When compiling, I use an external program to compile into pdf. Right now, with the following code in my .emacs file, emacs will start compiling the document into a pdf whenever I save the file.
(defun auto-compile-latex ()
(save-window-excursion
(async-shell-command (format "cd %s; scons -u" default-directory))))
(add-hook 'LaTeX-mode-hook '(lambda ()
(add-hook 'after-save-hook 'auto-compile-latex nil 'make-it-local)))
I prefer this over M-x compile, because I am more in the habit of saving, and it launches in the background, allowing me to continue working. However, I do not get a prominent notification when the compilation process finishes.
Ideally, I would want to run the following function whenever a compilation process finishes.
(defun latex-compilation-status (exit-code)
(if (/= exit-code 0)
(setq mode-name (propertize mode-name 'face 'font-lock-warning-face))
(setq mode-name (propertize mode-name 'face 'mode-line-highlight))))
That way, I can have the color in the mode line automatically change depending on whether the compilation was successful or not. However, looking through the emacs documentation, I have not found any mention of a hook that gets run after async-shell-command completes. I know that there is the message in the minibuffer stating the exit status of the subprocess, but if I am typing at the time, it is often hard to notice.
Alternatively, I could wait for the shell command to complete, then change the color immediately. However, this then makes the entirety of emacs freeze while compiling, which is not desired.
How would I go about having this indication applied at the end of the compilation, without having emacs freeze during the process?
You should try the command async-start from https://github.com/jwiegley/emacs-async
Thank you.
I ended up getting it using a modified version of lawlist's code.
This now has changes the color when starting to compile,
then changes it again to indicate success or failure.
;Automatically compile any latex documents when saved.
(defun auto-compile-latex ()
(setq mode-name (propertize mode-name 'face 'font-lock-string-face))
(set-process-sentinel
(start-process-shell-command "latex-compile" "latex-compile"
(format "cd %s; scons -u" default-directory))
'latex-compile-sentinel))
;Change the color after compilation. Still need to find the right hook to add it to.
(defun latex-compile-sentinel (process event)
(if (string-equal event "finished\n")
(setq mode-name (propertize mode-name 'face 'mode-line-highlight))
(setq mode-name (propertize mode-name 'face 'font-lock-warning-face))))
;Hooks for latex-mode
(add-hook 'LaTeX-mode-hook '(lambda ()
(add-hook 'after-save-hook 'auto-compile-latex nil 'make-it-local)))
As an aside, the emacs-async package did not work for this use.
I assume that this is because emacs-async starts the secondary functions in a separate process,
with some of the variables not propagated back to the parent process.
Here is another option using set-process-sentinel based upon a prior helpful answer by Francesco. Please do be careful, however, about using a let binding to define a start-process (which [in my lay opinion] is a definite "no no") because that would cause the process to begin immediately before its time. [FYI: Nicolas has gotten me out of quite a few jams in the past, so please be sure to take a good hard look at his solution also.]
Emacs: if latexmk finishes okay, then show pdf, else display errors
You would just add your function to the last part of the function dealing with success -- i.e., (when (= 0 (process-exit-status p)) . . .:
(defun latexmk ()
".latexmkrc contains the following entries (WITHOUT the four backslashes):
$pdflatex = 'pdflatex -file-line-error -synctex=1 %O %S && (cp \"%D\" \"%R.pdf\")';
$pdf_mode = 1;
$out_dir = '/tmp';"
(interactive)
(setq tex-file buffer-file-name)
(setq pdf-file (concat "/tmp/" (car (split-string
(file-name-nondirectory buffer-file-name) "\\.")) ".pdf"))
(setq line (format "%d" (line-number-at-pos)))
(setq skim "/Applications/Skim.app/Contents/SharedSupport/displayline")
(setq tex-output (concat "*" (file-name-nondirectory buffer-file-name) "*") )
(setq latexmk "/usr/local/texlive/2012/texmf-dist/scripts/latexmk/latexmk.pl")
(setq latexmkrc "/Users/HOME/.0.data/.0.emacs/.latexmkrc")
(if (buffer-modified-p)
(save-buffer))
(delete-other-windows)
(set-window-buffer (split-window-horizontally) (get-buffer-create tex-output))
(with-current-buffer tex-output (erase-buffer))
(set-process-sentinel
(start-process "compile" tex-output latexmk "-r" latexmkrc tex-file)
(lambda (p e) (when (= 0 (process-exit-status p))
(start-process "displayline" nil skim "-b" line pdf-file tex-file)
(switch-to-buffer (get-file-buffer tex-file))
(if (get-buffer-process (get-buffer tex-output))
(process-kill-without-query (get-buffer-process (get-buffer tex-output))))
(kill-buffer tex-output)
(delete-other-windows)))))
You could also arrange for your auto-compile-latex to run compile.

Check if major mode is equal one of several emacs

I found a snippet to close all dired buffers, which I want to use in sunrise commander:
(defun er/kill-all-dired-buffers()
"Kill all dired buffers."
(interactive)
(save-excursion
(let((count 0))
(dolist(buffer (buffer-list))
(set-buffer buffer)
(when (equal major-mode 'sr-mode)
(or (equal major-mode 'dired-mode))
(setq count (1+ count))
(kill-buffer buffer)))
(message "Killed %i dired buffer(s)." count ))))
(setq sr-quit-hook 'er/kill-all-dired-buffers)
Issue being, I can't make it work both for sr-mode and dired-mode together.
How do I check "if major mode is sr-mode OR dired-mode"?
EDIT:
Just a syntax error.
Should be
(when (or (equal major-mode 'dired-mode) (equal major-mode 'sr-mode))
Have to admit it's not too intuitive.
The canonical way would be (when (derived-mode-p 'sr-mode 'dired-mode) ...).
I tried some things and found this to work on my emacs-ielm - perhaps it might help also:
(if (member major-mode '(fsharp-mode c-mode java-mode inferior-emacs-lisp-mode))
(message "yeah right"))
Maybe the correct check function is:
(derived-mode-p &rest MODES)
See 'subr.el'.

Setting default-directory in .emacs

I don't really know what to say.
I've been working on customizing my emacs, and I noticed that it isn't actually loading my .emacs on startup. According the(http://www.gnu.org/software/emacs/manual/html_node/emacs/Find-Init.html#Find-Init), emacs looks in the HOME directory (~/) for the initialization file first...
When I start emacs, my .emacs file seems to be read correctly - when I visit a .notes file, for example, the hooks are evaluated and such and such. What surprises me: the default-directory isn't set - a command in the same load file. I can either evaluate it manually or simply execute (load "~/.emacs") and it will work fine.
I guess the question can be summarized: If the load command works as expected when executed manually, why isn't it working on startup automatically?
Full (except for commented-out functions) .emacs file:
; http://stackoverflow.com/a/13946304/1443496
(defvar auto-minor-mode-alist ()
"Alist of filename patterns vs correpsonding minor mode functions,
see `auto-mode-alist'. All elements of this alist are checked,
meaning you can enable multiple minor modes for the same regexp.")
(defun enable-minor-mode-based-on-extension ()
"check file name against auto-minor-mode-alist to enable minor modes
the checking happens for all pairs in auto-minor-mode-alist"
(when buffer-file-name
(let ((name buffer-file-name)
(remote-id (file-remote-p buffer-file-name))
(alist auto-minor-mode-alist))
;; Remove backup-suffixes from file name.
(setq name (file-name-sans-versions name))
;; Remove remote file name identification.
(when (and (stringp remote-id)
(string-match-p (regexp-quote remote-id) name))
(setq name (substring name (match-end 0))))
(while (and alist (caar alist) (cdar alist))
(if (string-match (caar alist) name)
(funcall (cdar alist) 1))
(setq alist (cdr alist))))))
(add-hook 'find-file-hook 'enable-minor-mode-based-on-extension)
;; the wrapping up of the two loads make sure
;; auctex is loaded only when editing tex files.
(eval-after-load "tex-mode"
'(progn
(load "auctex.el" nil nil t)
(load "preview-latex.el" nil nil t)
)
)
; Sets my default directory to my dropbox (platform-dependent)
(setq default-directory
(concat
(if (eq system-type 'windows-nt)
"t:" "~")
"/Dropbox/Public/School/TeX/"))
; Set up the .notes extension
(setq auto-mode-alist
(cons '("\\.notes\\'" . text-mode)
auto-mode-alist))
(setq auto-minor-mode-alist
(cons '("\\.notes\\'" . auto-fill-mode)
auto-minor-mode-alist))
;; AUCTeX replaces latex-mode-hook with LaTeX-mode-hook
(add-hook 'LaTeX-mode-hook
(lambda ()
(setq TeX-auto-save t)
(setq TeX-parse-self t)
;; (setq-default TeX-master nil)
(reftex-mode t)
(TeX-fold-mode t)))
Default-directory is buffer local. Your .emacs is loading just fine; the value of default-directory is (re)set for each new buffer you open. When you re-load your .emacs, it changes the value of default-directory for the buffer you are in only.

How to execute emacs grep-find link in the same window?

When I use grep-find it opens another window (area in the frame) with a list of results that I can select. When I select one it opens the target file in a different window than grep-find is in.
How can I get the target file to open in the same window as the grep results (replacing the grep results window with what I am actually looking for).
How can I keep grep-find from opening a separate window (have it so it opens in the current window). My goal is I look for something, I find it, I go to it, all within the same window. I would like to add this to my .emacs file.
It doesn't look like there is any way to configure the compile package to do what you're asking. And there's no easy way to use advice to tweak the behavior. I think you have to resort to editing the function which actually jumps to the error, which you can do with the following addition to your .emacs (tested in Emacs 23.1):
(eval-after-load "compile"
'(defun compilation-goto-locus (msg mk end-mk)
"Jump to an error corresponding to MSG at MK.
All arguments are markers. If END-MK is non-nil, mark is set there
and overlay is highlighted between MK and END-MK."
;; Show compilation buffer in other window, scrolled to this error.
(let* ((from-compilation-buffer (eq (window-buffer (selected-window))
(marker-buffer msg)))
;; Use an existing window if it is in a visible frame.
(pre-existing (get-buffer-window (marker-buffer msg) 0))
(w (if (and from-compilation-buffer pre-existing)
;; Calling display-buffer here may end up (partly) hiding
;; the error location if the two buffers are in two
;; different frames. So don't do it if it's not necessary.
pre-existing
(let ((display-buffer-reuse-frames t)
(pop-up-windows t))
;; Pop up a window.
(display-buffer (marker-buffer msg)))))
(highlight-regexp (with-current-buffer (marker-buffer msg)
;; also do this while we change buffer
(compilation-set-window w msg)
compilation-highlight-regexp)))
;; Ideally, the window-size should be passed to `display-buffer' (via
;; something like special-display-buffer) so it's only used when
;; creating a new window.
(unless pre-existing (compilation-set-window-height w))
(switch-to-buffer (marker-buffer mk))
;; was
;; (if from-compilation-buffer
;; ;; If the compilation buffer window was selected,
;; ;; keep the compilation buffer in this window;
;; ;; display the source in another window.
;; (let ((pop-up-windows t))
;; (pop-to-buffer (marker-buffer mk) 'other-window))
;; (if (window-dedicated-p (selected-window))
;; (pop-to-buffer (marker-buffer mk))
;; (switch-to-buffer (marker-buffer mk))))
;; If narrowing gets in the way of going to the right place, widen.
(unless (eq (goto-char mk) (point))
(widen)
(goto-char mk))
(if end-mk
(push-mark end-mk t)
(if mark-active (setq mark-active)))
;; If hideshow got in the way of
;; seeing the right place, open permanently.
(dolist (ov (overlays-at (point)))
(when (eq 'hs (overlay-get ov 'invisible))
(delete-overlay ov)
(goto-char mk)))
(when highlight-regexp
(if (timerp next-error-highlight-timer)
(cancel-timer next-error-highlight-timer))
(unless compilation-highlight-overlay
(setq compilation-highlight-overlay
(make-overlay (point-min) (point-min)))
(overlay-put compilation-highlight-overlay 'face 'next-error))
(with-current-buffer (marker-buffer mk)
(save-excursion
(if end-mk (goto-char end-mk) (end-of-line))
(let ((end (point)))
(if mk (goto-char mk) (beginning-of-line))
(if (and (stringp highlight-regexp)
(re-search-forward highlight-regexp end t))
(progn
(goto-char (match-beginning 0))
(move-overlay compilation-highlight-overlay
(match-beginning 0) (match-end 0)
(current-buffer)))
(move-overlay compilation-highlight-overlay
(point) end (current-buffer)))
(if (or (eq next-error-highlight t)
(numberp next-error-highlight))
;; We want highlighting: delete overlay on next input.
(add-hook 'pre-command-hook
'compilation-goto-locus-delete-o)
;; We don't want highlighting: delete overlay now.
(delete-overlay compilation-highlight-overlay))
;; We want highlighting for a limited time:
;; set up a timer to delete it.
(when (numberp next-error-highlight)
(setq next-error-highlight-timer
(run-at-time next-error-highlight nil
'compilation-goto-locus-delete-o)))))))
(when (and (eq next-error-highlight 'fringe-arrow))
;; We want a fringe arrow (instead of highlighting).
(setq next-error-overlay-arrow-position
(copy-marker (line-beginning-position)))))))
The eval-afer-load portion just ensures that you re-define it after Emacs defined it, so that your change takes hold.
You can add a binding (e.g. Alt-m) and do the following
(define-key grep-mode-map "\M-m" (lambda()
(interactive)
(compile-goto-error)
(delete-other-windows)
(kill-buffer "*grep*")))
I didn't find a way to replace the standard "Enter" / Mouse-click binding with a custom function
There is an another approach:
(defun eab/compile-goto-error ()
(interactive)
(let ((cwc (current-window-configuration)))
(funcall
`(lambda ()
(defun eab/compile-goto-error-internal ()
(let ((cb (current-buffer))
(p (point)))
(set-window-configuration ,cwc)
(switch-to-buffer cb)
(goto-char p ))))))
(compile-goto-error)
(run-with-timer 0.01 nil 'eab/compile-goto-error-internal))
I had the same question, and found this answer over at emacs.stackexchange https://emacs.stackexchange.com/a/33908/20000
(defun my-compile-goto-error-same-window ()
(interactive)
(let ((display-buffer-overriding-action
'((display-buffer-reuse-window
display-buffer-same-window)
(inhibit-same-window . nil))))
(call-interactively #'compile-goto-error)))
(defun my-compilation-mode-hook ()
(local-set-key (kbd "o") #'my-compile-goto-error-same-window))
(add-hook 'compilation-mode-hook #'my-compilation-mode-hook)
Pressing o in the *grep* buffer will open the location and file in the same frame.
I found this an elegant solution without deleting frames or too much lisp code and just hooking into compilation-mode-hook.

"Diff, save or kill" when killing buffers in Emacs

When trying to kill a buffer that contains changes in Emacs, the message:
" Buffer [buffer] modified; kill anyway? (yes or no)" is displayed.
Instead of this I'd like to have Emacs ask me if I want to:
1. View a diff of what changed,
2. Save the buffer,
3. Kill the buffer.
How?
The answer lies in using advice, because the hooks normally run when killing buffers run after the "buffer modified" prompt you want to change.
The following advice does what you want (I think). A couple of notes:
When running the diff, the original buffer is marked as not modified - but you'll really need to save it.
The other buffer in the diff doesn't get deleted
(defadvice kill-buffer (around my-kill-buffer-check activate)
"Prompt when a buffer is about to be killed."
(let* ((buffer-file-name (buffer-file-name))
backup-file)
;; see 'backup-buffer
(if (and (buffer-modified-p)
buffer-file-name
(file-exists-p buffer-file-name)
(setq backup-file (car (find-backup-file-name buffer-file-name))))
(let ((answer (completing-read (format "Buffer modified %s, (d)iff, (s)ave, (k)ill? " (buffer-name))
'("d" "s" "k") nil t)))
(cond ((equal answer "d")
(set-buffer-modified-p nil)
(let ((orig-buffer (current-buffer))
(file-to-diff (if (file-newer-than-file-p buffer-file-name backup-file)
buffer-file-name
backup-file)))
(set-buffer (get-buffer-create (format "%s last-revision" (file-name-nondirectory file-to-diff))))
(buffer-disable-undo)
(insert-file-contents file-to-diff nil nil nil t)
(set-buffer-modified-p nil)
(setq buffer-read-only t)
(ediff-buffers (current-buffer) orig-buffer)))
((equal answer "k")
(set-buffer-modified-p nil)
ad-do-it)
(t
(save-buffer)
ad-do-it)))
ad-do-it)))
You'll want to write some code to put in the kill-buffer-hooks and write-file-functions lists. Conceptually, what you want to do is
test if the buffer has been modified
display your message and get a
response, and do what's requested
then clear the modified flag so the
normal kill-buffer doesn't come back
and ask again.