Run sgml-pretty-print when opening an xml file in emacs? - emacs

I can currently use sgml-pretty-print to pretty print an xml file in emacs, but it's a manual process:
M-<
C-space
M->
M-x sgml-pretty-print
I'd like this to happen automatically (or at least have some option to do so). I'm new to emacs/elisp, and do not understand how:
emacs knows what code to run when you open a file (does this start in files.el?)
If you wanted to override that code with your own, how to do that

This should do the trick for you:
(add-hook 'find-file-hook 'my-sgml-find-file-hook)
(defun my-sgml-find-file-hook ()
"run sgml pretty-print on the file when it's opened (if it's sgml)"
(when (eq major-mode 'sgml-mode)
(sgml-pretty-print (point-min) (point-max))))
The key pieces of information are the find-file-hook, point-min (-max), and major-mode.
If you want to learn more about elisp, you can take a look at this question, which gives some pointers on how to figure things out.

A slightly simpler alternative to Trey Jackson's answer. Just add this to your ~/.emacs file:
(add-hook 'sgml-mode-hook #'(lambda ()
(sgml-pretty-print (point-min) (point-max))))

Related

Stopping tex-shell from opening when compiling Latex from Emacs

How can I prevent the *tex-shell* buffer from opening when I compile Latex from Emacs? It splits the window in half, and I always just use C-x 1 to get rid of it immediately.
The solution is possibly related to
(setq special-display-buffer-names ("*tex-shell*"))
which makes the new buffer take up the whole frame instead of just half (not what I want).
I tried the following, but it has no effect for Latex:
(defadvice compilation-start
(around inhidbit-display (command &optional mode name-function highlight-regexp))
(flet (display-buffer) (fset 'display-buffer 'ignore) ad-do-it))
(ad-activate 'compilation-start)
(ad-deactivate 'compilation-start)
Well, you really should be using AUCTeX since it's much better. Nevertheless if you type C-hk and then a key sequence Emacs will tell you what would be run. In this case, for C-cC-f, it's tex-file so you will have to advise tex-file, or maybe (digging down into the source a little bit) tex-start-shell.
I use the following defun:
(defun tex-without-changing-windows ()
(interactive)
(save-buffer)
(save-window-excursion (tex-file)))
I bind it to C-c C-f to replace tex-file.

How to run hook depending on file location

I am involved in python project where tabs are used, however i am not using them in every other code i write, it is vital to use them in that particular project. Projects are located in one directory under specific directories. I.E:
\main_folder
\project1
\project2
\project3
...etc
I have couple functions/hooks on file open and save that untabify and tabify whole buffer i work on.
;; My Functions
(defun untabify-buffer ()
"Untabify current buffer"
(interactive)
(untabify (point-min) (point-max)))
(defun tabify-buffer ()
"Tabify current buffer"
(interactive)
(tabify (point-min) (point-max)))
;; HOOKS
; untabify buffer on open
(add-hook 'find-file-hook 'untabify-buffer)
; tabify on save
(add-hook 'before-save-hook 'tabify-buffer)
If i put it in .emacs file it is run on every .py file i open which is not what i want. What i`d like to have is to have these hooks used only in one particular folder with respective subfolders. Tried .dir_locals but it works only for properties not hooks. I can not use hooks in specific modes (i.e. python-mode) as almost all projects are written in python. To be honest i tried writing elisp conditional save but failed.
A very easy solution is to just add a configuration variable that can be used to disable the hooks. For example:
(defvar tweak-tabs t)
(add-hook 'find-file-hook
(lambda () (when tweak-tabs (untabify (point-min) (point-max)))))
(add-hook 'before-save-hook
(lambda () (when tweak-tabs (tabify (point-min) (point-max)))))
Now you can add a .dir-locals.el file in the relevant directories, setting tweak-tabs to nil, disabling this feature there.
(But another problem is that this is a pretty bad way to deal with tabs. For example, after you save a file you do see the tabs in it.)
Just for the record, to answer the literal question in the title (as I reached this question via a web search): one way to add a hook that depends on file location is to make it a function that checks for buffer-file-name. (Idea from this answer.)
For example, for the exact same problem (turn on tabs only in a particular directory, and leave tabs turned off elsewhere), I'm currently doing something like (after having installed the package smart-tabs-mode with M-x package-install):
(smart-tabs-insinuate 'python) ; This screws up all Python files (inserts tabs)
(add-hook 'python-mode-hook ; So we need to un-screw most of them
(lambda ()
(unless (and (stringp buffer-file-name)
(string-match "specialproject" buffer-file-name))
(setq smart-tabs-mode nil)))
t) ; Add this hook to end of the list
(This is a bit inverted, as smart-tabs-insinuate itself modifies python-mode-hook and then we're modifying it back, but it should do as an example.)

Automatically byte-compiling on save

Everytime I save a file in emacs lisp mode, I want it to be automatically byte-compiled. Can someone come up with a function that does byte-compile-file on the current file if the current major mode is emacs lisp mode? I want to add-hook that function to after-save-hook.
I found an answer here. The following does it all. It is a copy from the linked site.
(add-hook 'after-save-hook
(lambda ()
(if (eq major-mode 'emacs-lisp-mode)
(save-excursion (byte-compile-file buffer-file-name)))))

is there an emacs tool to show a directory structure as a tree?

I am aware of Speedbar (I prefer the structure in the same frame as the rest of my work), and dired shows too much information. I'm after something like the svn-status tree representation. Is there anything like that?
Thank you.
EDIT: Here's what I found the most intuitive:
I am using Speedbar with the following hack from here. I had to use the commented "try this" part, FYI.
(require 'speedbar)
(defconst my-speedbar-buffer-name "SPEEDBAR")
; (defconst my-speedbar-buffer-name " SPEEDBAR") ; try this if you get "Wrong type argument: stringp, nil"
(defun my-speedbar-no-separate-frame ()
(interactive)
(when (not (buffer-live-p speedbar-buffer))
(setq speedbar-buffer (get-buffer-create my-speedbar-buffer-name)
speedbar-frame (selected-frame)
dframe-attached-frame (selected-frame)
speedbar-select-frame-method 'attached
speedbar-verbosity-level 0
speedbar-last-selected-file nil)
(set-buffer speedbar-buffer)
(speedbar-mode)
(speedbar-reconfigure-keymaps)
(speedbar-update-contents)
(speedbar-set-timer 1)
(make-local-hook 'kill-buffer-hook)
(add-hook 'kill-buffer-hook
(lambda () (when (eq (current-buffer) speedbar-buffer)
(setq speedbar-frame nil
dframe-attached-frame nil
speedbar-buffer nil)
(speedbar-set-timer nil)))))
(set-window-buffer (selected-window)
(get-buffer my-speedbar-buffer-name)))
So I wanted to just print a tree quickly and all Emacs based solutions seemed either rather old or massive like (ECB/CEDET) which I don't know enough about, so for quick fix I did
sudo apt-get install tree
then from dired buffer
M-! tree --dirsfirst RET
voilĂ ! directory tree without any specific libs, as an added bonus using tree -f outputs tree with full paths, making it obvious to jump to file with C-x C-f directly from the tree output buffer.
There is a tree extension for emacs mode Sunrise Commander. Sunrise Commander is like Midnight Commander.
There's the Emacs Code Browser. Last I heard using it with a current emacs may be tricky or difficult because it makes use of some tools that were recently integrated into Emacs via CEDET.
I'd recommend NeoTree. It's easy to install and easy to use.

How to get equivalent of Vim's :Texplore in Emacs?

I know about M-x dire, but would like to customize it. I would like to hit one key (for example F2) and get dire buffer open. When I navigate across the directory hierarchy it shouldn't open new buffers.
And when I finally open the file it also shouldn't open new buffer for it (not strictly necessary, but strongly preferred).
Of course this behavior can be global, i.e. for all dire buffers/invocations.
Check out dired-single, which pretty much does what you want (except that last bit, where it reuses the dired buffer for the newly visted file).
Caveat Lector: I wrote it, so I'm biased towards its usefulness.
Some alternatives - EmacsWiki: DiredReuseDirectoryBuffer, and this short snippet from an awkwardly-formatted blog-entry.
caveat: haven't tried them, myself.
I know this is very old but All you have to do is press 'a' on a dir or file to get this functionality. It's already there.
Here's what I finally used:
(require 'dired)
(global-set-key [(f2)] 'my-dired)
(defun my-dired ()
(interactive)
(dired (file-name-directory (buffer-file-name))))
(defadvice dired-advertised-find-file (around dired-subst-directory activate)
"Replace current buffer if file is a directory."
(interactive)
(let ((orig (current-buffer)) (filename (dired-get-filename :no-error-if-not-filep t)))
ad-do-it
(when (not (eq (current-buffer) orig)) (kill-buffer orig))))