How to highlight tab characters for files from specific project in Emacs - emacs

I have this code to highlight tab characters and want to disable them using project-specifics macro:
(require 'highlight-chars)
(make-variable-buffer-local 'prevent-highlight-tabs)
(setq highlight-chars-disable '(term-mode erc-mode fundamental-mode))
(setq-default prevent-highlight-tabs nil)
(add-hook 'font-lock-mode-hook
(lambda()
(message "lock")
(when (and (null (memql major-mode highlight-chars-disable))
(not prevent-highlight-tabs))
(message "%s" prevent-highlight-tabs)
(hc-highlight-tabs))))
(custom-set-faces '(hc-tab ((t (:background "red")))))
and project-specifics is a macro that define add find-file-hook and dired-after-readin-hook from this question
(project-specifics "projects/test"
(message "specific")
(setq prevent-highlight-tabs t)
(setq indent-tabs-mode t))
What I wanted to do is disable red tabs (I want them because in most projects I want only spaces, and want to see tabs) for files in project/test, but I have a problem because the code from font-lock-mode-hook is executing before project-specifics (find-file-hook), and prevent-highlight-tabs is always nil in font-lock-mode-hook. Why is that, and how to fix it?

I suggest that you place your settings in .dir-locals.el as described in Per-Directory Local Variables.

What you are doing seems convoluted.
And it is partly undefined (here) - e.g., what is the major mode highlight-chars-disable? That is not defined in library highlight-chars.el, and it doesn't sound like something that would be a good candidate for a major mode.
See the Commentary of library highlight-chars.el for suggestions.
Something like this, perhaps (and you would put property prevent-highlight-tabs on any major-mode symbols you like:
(add-hook 'font-lock-mode-hook
(lambda () (unless (get major-mode 'prevent-highlight-tabs)
(hc-highlight-tabs))))
Or something like this (from the Commentary):
(add-hook 'change-major-mode-hook
(lambda ()
(add-hook 'font-lock-mode-hook 'hc-highlight-tabs)))
(add-hook 'after-change-major-mode-hook
(lambda ()
(when (eq major-mode 'THE-MODE)
(remove-hook 'font-lock-mode-hook 'hc-highlight-tabs)
(hc-dont-highlight-tabs)))
'APPEND)

Related

add a hook in prog-mode except a particular mode

i have a function
(defun a--before-test-save-hook()
"Test of before save hook"
(message "foobar"))
and i want to run it in prog-mode except python-mode,
but i have no clue now
and i just add-hook in prog-mode including python-mode
(add-hook 'prog-mode (lambda ()
(add-hook 'before-save-hook 'a-test-before-save-hook t t)))
i have try
(defun a-test-before-save-hook()
"Test of before save hook"
(unless (eq major-mode 'python-mode)
(message "foobar")))
but i want a better try,any solution will be appreciated.
Not sure if it's better, but you could do:
(add-hook 'prog-mode-hook
(lambda ()
(unless (derived-mode-p 'python-mode)
(add-hook 'before-save-hook
#'a-test-before-save-hook t t))))
Of course, my own reflex is to ask "what makes Python special?". The answer might let you replace the (derived-mode-p 'python-mode) test with something that goes more directly at the heart of the issue (e.g. maybe the issue is related to indentation-significance and would hence also apply to Coffeescript and Haskell and maybe you could check electric-indent-inhibit instead).

emacs custom php tags highlight syntax

I want to set custom color background in tags, but other do not touch
(define-derived-mode php-mode fundamental-mode
"php-mode"
:syntax-table php-syntax-table
(setq font-lock-defaults '(php-keywords)))
(defvar php-syntax-table (make-syntax-table) "Syntax table for php-mode")
(make-face 'php-region-face)
(set-face-background 'php-region-face "red")
(setq php-keywords '(("<\\?php[[:ascii:]]*?\\?>" 0 'php-region-face t)))
But highlight background tags is not correctly, see below:
You'll want to setup multiline font-lock and define the boundaries of the font-lock search (so it doesn't take too much time). My information (and this code) comes from this SO question.
Here, I define multiline font-locking to take place within tags (< ... >). If this is added to your define-derived mode, it works as you describe.
(set (make-local-variable 'font-lock-multiline) t)
(add-hook 'font-lock-extend-region-functions
'test-font-lock-extend-region)
(defun test-font-lock-extend-region ()
"Extend the search region to include an entire SGML tag."
;; Avoid compiler warnings about these global variables from font-lock.el.
;; See the documentation for variable `font-lock-extend-region-functions'.
(eval-when-compile (defvar font-lock-beg) (defvar font-lock-end))
(save-excursion
(goto-char font-lock-beg)
(let ((found (or (re-search-backward "<" nil t) (point-min))))
(goto-char font-lock-end)
(when (re-search-forward ">" nil t)
(beginning-of-line)
(setq font-lock-end (point)))
(setq font-lock-beg found))))
EDIT: for some reason, SO doesn't like my code formatting.

In emacs, how do I save without running save hooks?

I have various things set up in my 'before-save-hook. For example, I run 'delete-trailing-whitespace. This is what I want in almost all occasions.
But sometimes, I'm working on files that are shared with other people, and the file already has a bunch of trailing whitespace. If I save the file, I'll get a big diff that's pretty confusing, as my change is buried in dozens or hundreds of meaningless changes. Yes, everyone could just tell their diff tool to not show whitespace changes, but that's something that everyone has to do every time they look at the diff. I'd rather not even have the whitespace change.
Is there anything I can do to save the file without the whitespace changes, short of starting a new instance of Emacs with no init.el file, or with a modified init.el that doesn't have the hook?
Here is how I save without triggering delete-trailing-whitespace:
C-x C-q C-x C-s C-x C-q: read-only, save, revert read-only
A simpler solution I came up with is that my fundamental-mode has no hooks installed, because I want it to be as plain as possible. Thus if I want to save a file without running hooks, I temporarily switch to fundamental-mode.
Based on a comment discussion with #Stefan, here are two possible (untested) solutions:
Use let:
(defun save-buffer-without-dtw ()
(interactive)
(let ((b (current-buffer))) ; memorize the buffer
(with-temp-buffer ; new temp buffer to bind the global value of before-save-hook
(let ((before-save-hook (remove 'delete-trailing-whitespace before-save-hook)))
(with-current-buffer b ; go back to the current buffer, before-save-hook is now buffer-local
(let ((before-save-hook (remove 'delete-trailing-whitespace before-save-hook)))
(save-buffer)))))))
Use unwind-protect:
(defun save-buffer-without-dtw ()
(interactive)
(let ((restore-global
(memq 'delete-trailing-whitespace (default-value before-save-hook)))
(restore-local
(and (local-variable-p 'before-save-hook)
(memq 'delete-trailing-whitespace before-save-hook))))
(unwind-protect
(progn
(when restore-global
(remove-hook 'before-save-hook 'delete-trailing-whitespace))
(when restore-local
(remove-hook 'before-save-hook 'delete-trailing-whitespace t))
(save-buffer))
(when restore-global
(add-hook 'before-save-hook 'delete-trailing-whitespace))
(when restore-local
(add-hook 'before-save-hook 'delete-trailing-whitespace nil t)))))
The problem with the second solution is that the order of functions in the before-save-hook may change.
Here's another solution:
(defvar my-inhibit-dtw nil)
(defun my-delete-trailing-whitespace ()
(unless my-inhibit-dtw (delete-trailing-whitespace)))
(add-hook 'before-save-hook 'my-delete-trailing-whitespace)
and then
(defun my-inhibit-dtw ()
(interactive)
(set (make-local-variable 'my-inhibit-dtw) t))
so you can M-x my-inhibit-dtw RET in the buffers where you don't want to trim whitespace.
I wrote a command inspired by Nicholas Douma's solution.
(defun olav-save-buffer-as-is ()
"Save file \"as is\", that is in read-only-mode."
(interactive)
(if buffer-read-only
(save-buffer)
(read-only-mode 1)
(save-buffer)
(read-only-mode 0)))
What we need to do is remove 'delete-trailing-whitespace from before-save-hook, save the buffer, then add it back.
This code will do that, but only remove and add it if it's there to begin with.
;; save the buffer, removing and readding the 'delete-trailing-whitespace function
;; to 'before-save-hook if it's there
(defun save-buffer-no-delete-trailing-whitespace ()
(interactive)
(let ((normally-should-delete-trailing-whitespace (memq 'delete-trailing-whitespace before-save-hook)))
(when normally-should-delete-trailing-whitespace
(remove-hook 'before-save-hook 'delete-trailing-whitespace))
(save-buffer)
(when normally-should-delete-trailing-whitespace
(add-hook 'before-save-hook 'delete-trailing-whitespace))))
(global-set-key (kbd "C-c C-s") 'save-buffer-no-delete-trailing-whitespace)
It also binds the command to (kbd C-c C-s), for convenience.

major-mode hook configuration affects other buffers

Let me start by saying I'm very new to emacs.
I'm attempting to create customizations for major-modes. While my settings are functioning correctly, I'm observing that when I open a new buffer, that buffers major-mode customization is being applied to other buffers of a different type.
For instance, if I open a file named 'Makefile', makefile-mode is used and my customizations are applied. If I then open another file such as 'test.c', c-mode is used but customizations from makefile-mode are merged with customizations from c-mode.
The relevant portions of my .emacs file can be seen below:
(defun c-mode-settings ()
(c-set-style "bsd")
(set-buffer-file-coding-system 'utf-8-unix)
(show-paren-mode 1)
(setq c-basic-offset 4)
(setq tab-width 4)
(setq indent-tabs-mode nil)
(setq c-tab-always-indent t)
(setq require-final-newline t)
)
(defun makefile-mode-settings ()
(setq whitespace-style '(tabs spaces space-mark tab-mark face lines-tail))
(whitespace-mode t)
(show-paren-mode 1)
(setq tab-width 4)
(setq require-final-newline t)
)
(add-hook 'c-mode-hook 'c-mode-settings)
(add-hook 'makefile-mode-hook 'makefile-mode-settings)
How can I keep these mode hooks from affecting other buffers in different modes?
Thanks!
Andrew
You need to take into account, that some variables are becoming local to buffer when set, while some are global. Usually they have corresponding comment in their description (use C-h v var-name to get this description.
In some cases, you can force that any variable become local to buffer, using the
(set (make-local-variable 'var-name) var-value)
but you need to be careful with this.

Emacs: Best-practice for lazy loading modes in .emacs?

Is there a best practice around lazily loading modes when encountering a relevant file extension?
At this point I have roughly 25 different Emacs modes installed, and startup has become slow. For example, although it's great to have clojure-mode at the ready, I rarely use it, and I want to avoid loading it at all unless I open a file with extension .clj. Such a "lazy require" functionality seems like the right way do mode configuration in general..
I found nothing online, so I've taken a crack at it myself.
Instead of:
(require 'clojure-mode)
(require 'tpl-mode)
I have this:
(defun lazy-require (ext mode)
(add-hook
'find-file-hook
`(lambda ()
(when (and (stringp buffer-file-name)
(string-match (concat "\\." ,ext "\\'") buffer-file-name))
(require (quote ,mode))
(,mode)))))
(lazy-require "soy" 'soy-mode)
(lazy-require "tpl" 'tpl-mode)
This seems to work (I'm an elisp newbie so comments are welcome!), but I'm unnerved about finding nothing written about this topic online. Is this a reasonable approach?
The facility you want is called autoloading. The clojure-mode source file, clojure-mode.el, includes a comment for how to arrange this:
;; Add these lines to your .emacs:
;; (autoload 'clojure-mode "clojure-mode" "A major mode for Clojure" t)
;; (add-to-list 'auto-mode-alist '("\\.clj$" . clojure-mode))
This is one way,
(provide 'my-slime)
(eval-after-load "slime"
'(progn
(setq slime-lisp-implementations
'((sbcl ("/usr/bin/sbcl"))
(clisp ("/usr/bin/clisp")))
common-lisp-hyperspec-root "/home/sujoy/documents/hyperspec/")
(slime-setup '(slime-asdf
slime-autodoc
slime-editing-commands
slime-fancy-inspector
slime-fontifying-fu
slime-fuzzy
slime-indentation
slime-mdot-fu
slime-package-fu
slime-references
slime-repl
slime-sbcl-exts
slime-scratch
slime-xref-browser))
(slime-autodoc-mode)
(setq slime-complete-symbol*-fancy t)
(setq slime-complete-symbol-function 'slime-fuzzy-complete-symbol)
(add-hook 'lisp-mode-hook (lambda () (slime-mode t)))))
(require 'slime)
along with,
;; slime mode
(autoload 'slime "my-slime" "Slime mode." t)
(autoload 'slime-connect "my-slime" "Slime mode." t)