Merge two hooks for the same mode into one - emacs

How can I merge this two lines into just one hook?
(add-hook 'web-mode-hook #'(lambda () (yas-activate-extra-mode 'html-mode)))
(add-hook 'web-mode-hook #'(lambda () (yas-activate-extra-mode 'css-mode))))
Everything I've tried so far didn't work.
Thanks

mapcar is, I believe, what you want:
(add-hook 'web-mode-hook (lambda ()
(mapcar #'yas-activate-extra-mode
'(html-mode css-mode))))
(I'm using an old version of yasnippet so haven't been able to test this.)

(add-hook 'web-mode-hook (lambda ()
(yas-activate-extra-mode 'html-mode)
(yas-activate-extra-mode 'css-mode) ))

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).

Disable '\C-x\C-s" in multi-term mode

There is no need to use C-x C-s to save buffer in term mode. So I want to disable it. I tried to define a new binding in term mode to replace it, but failed.
(add-hook 'term-mode-hook
#'(lambda ()
(define-key term-mode-map "\C-x\C-s"
#'(lambda ()
(interactive)
(message "NO!")))))
Any help is appreciated. Thanks
Update: I use multi-term which based on term mode.
As wvxvw said, I tried this and it works. Add below to term-bind-key-alist. Thanks everyone.
("\C-x\C-s" . (lambda ()
(interactive)
(message "NO!")))
Take off the #' from your code. It should work. But you will still be able to save the buffer with "M-x save-buffer"
UPDATE:
This code is working for me
(add-hook 'term-mode-hook
(lambda ()
(define-key term-mode-map (kbd "C-x C-s")
(lambda ()
(interactive)
(message "hello world")))))
You will need to reload your term buffer after you setup the hook. It will not work for existing buffers

Logging Emacs file open/save commands

I'm looking to keep a history of file operations in Emacs, in particular file open and save commands, with associated timestamps and pid, has anyone done something like this?
You can easily implement it by yourself using emacs standard hooks.
Something like this:
(setq *find-file-list* '())
(setq *save-file-list* '())
(add-hook 'find-file-hook (lambda ()
(push (list (buffer-file-name) (time-stamp-string)) *find-file-list*)))
(add-hook 'after-save-hook (lambda ()
(push (list (buffer-file-name) (time-stamp-string)) *save-file-list*)))

Why are my mode specific .emacs hooks running for all modes?

I'm running the following code in my .emacs file:
(defun load-hooks ()
(add-hook 'after-save-hook 'my-hook))
(add-hook 'c-mode-hook 'load-hooks)
(defun my-hook () ... )
However, the content in my-hook is running on save even when I'm in a different mode. Am I missing a step?
You should use the LOCAL argument to add-hook, which will make sure that the hook only affects the current buffer:
(defun load-hooks ()
(add-hook 'after-save-hook 'my-hook nil t))
(add-hook 'c-mode-hook 'load-hooks)
(defun my-hook () ...)
I think that calling (add-hook 'after-save-hook 'my-hook) in load-hooks adds the hook to all modes. That is, once that function is called, after-save-hook is modified for every other buffer as well.
I suspect that your hook would not be run unless you open a c file. Try opening some file without having opened any c files and see if anything is run. If it isn't it just means that the function that runs for c files modifies the save hook for everything else.
Tikhon was correct about the 'after-save-hook affecting all modes - I am now relying on a check using the following functions:
(defun in-c-mode? ()
(string= (current-major-mode) "c-mode"))
(defun current-major-mode ()
(with-current-buffer (current-buffer) major-mode))

Redefining ENTER key in Emacs

I don't know elisp, but I'm trying to do something like the following:
(add-hook
'scala-mode-hook
(lambda ()
(define-key scala-mode-map (kbd "RET") (lambda ()
(scala-newline)
(scala-indent-line)))))
Goal is to call the two functions each time I hit the ENTER key. How do I actually do this?
I do essentially this in so many modes that I've squashed them all together:
(mapcar (lambda (hooksym)
(add-hook hooksym
(lambda ()
(local-set-key (kbd "C-m") 'newline-and-indent)
)))
'(
clojure-mode-hook
emacs-lisp-mode-hook
erlang-mode-hook
java-mode-hook
js-mode-hook
lisp-interaction-mode-hook
lisp-mode-hook
makefile-mode-hook
nxml-mode-hook
python-mode-hook
ruby-mode-hook
scheme-mode-hook
sh-mode-hook
))
Just stick scala-mode-hook in there somewhere and it'll work for you too :)
You need an (interactive) form after the lambda in your define-key.
EDIT:
To be clear, the inner form should look like:
(lambda ()
(interactive)
(scala-newline)
(scala-indent-line))
In hook you can use local-set-key, for example
(add-hook 'scala-mode-hook
(lambda ()
(local-set-key [return]
(lambda ()
(scala-newline)
(scala-indent-line)))))
although, maybe it will be easier to use something like standard newline-and-indent?
(add-hook 'scala-mode-hook
(lambda ()
(local-set-key [return] 'newline-and-indent)))
Just type C-j it will call the newline-and-indent command and do exactly what you ask.