error with "other-window" with "emacs-startup-hook" - emacs

When I open emacs, I would like to see the following:
2 windows, left window is for editing the document and the right windows run the program "multi-term"
I tried to edit my ~/.emacs with:
(add-hook 'emacs-startup-hook 'other-window)
(add-hook 'emacs-startup-hook 'multi-term)
(add-hook 'emacs-startup-hook 'split-window-horizontally)
the last two commands work, i.e I get 2 windows, one in left and one in right and the left one runs multi-term. (Althought I wanna the converse). But the command
(add-hook 'emacs-startup-hook 'other-window)
doesn't work. I get
wrong number of arguments: other-window, 0
Why? I think I can do everything if I type a correct function name, if this function really works if I type it in Emacs with M-x function_name.
How could I resolve this problem?

The command other-window takes an argument, which you're not providing. When you hit the keys C-x n, the argument is filled in for you automatically. Try:
(add-hook 'emacs-startup-hook (lambda () (other-window 1)))
Or, you could mimic the keystroke by doing:
(add-hook 'emacs-startup-hook (lambda () (call-interactively 'other-window)))

Related

emacs init - splitting window and opening files next to each other

I am trying to customize my Emacs init file in such a way that Emacs opens with two windows split and the ansi-term opened in one side and my init file on the other side. Now, the function I wrote (switch-to-next-window) works perfectly if Emacs is open already.
I was hoping to make the cursor switch to the other window and then open my init file there. However, if I try to run this upon start-up (actually after start up, at least this is what I am thinking) I get the following error: window-live-p, nil
I am gessing that there is no "next window". But I just don't know a work around here since I do think that I am only calling my function after Emacs has fully started up? If anyone could point me to where I am going wrong in my logic, that would be great!
(split-window-horizontally)
(setq initial-buffer-choice "*ansi-term*")
(defun switch-to-next-window ()
(interactive)
(let* ((next-window (get-buffer-window (other-buffer (current-buffer) t))))
(select-window next-window)))
(add-hook 'emacs-startup-hook (lambda ()(ansi-term "/bin/bash")))
(with-eval-after-load "~/.emacs.d/init.el"
(switch-to-next-window)
(setq initial-buffer-choice "~/.emacs.d/init.el"))
Changing initial-buffer-choice after the initial buffer has been opened won't have any effect.
What helps is putting everything into the emacs-startup-hook and using the find-file-other-window function:
(add-hook 'emacs-startup-hook
(lambda ()
(ansi-term "/bin/bash")
(split-window-horizontally)
(find-file-other-window "~/.emacs.d/init.el")))

Rebuild cscope in emacs

I have cscope built-in in emacs.
When ever I change the code using emacs. The code change causes cscope to not behave the way I want it to.
eg.
Due to code change If I want to jump to the function definition. cscope does not take me to the definition of the func, instead it takes me to some other line.
Please tell if there is a way to rebuild cscope without closing the emacs window.
You will need https://github.com/dkogan/xcscope.el
and configuration :
(defun my-c-mode-common-hook ()
(require 'xcscope)
(cscope-setup)
(setq cscope-initial-directory "path to the cscope directory"))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
and then
C-c s L (or M-x cscope-create-list-of-files-to-index)
C-c s I (or M-x cscope-index-files) => build or rebuild
Hope this help
I use the following function to build/rebuild cscope database:
(require 'xcscope)
(cscope-setup)
(setq cscope-option-use-inverted-index t)
(defadvice cscope-bury-buffer (after cscope-bury-buffer activate)
"Kill the *cscope* window after hitting q or Q instead of leaving it open."
(delete-window))
(defun cscope-create-database (top-directory)
"Create cscope* files in one step containing, do this before using cscope:
1. C-c s L
2. C-c s I
3. C-c s a
"
(interactive "DCreate cscope* database files in directory: ")
(progn
(cscope-create-list-of-files-to-index top-directory)
(cscope-index-files top-directory)
(setq cscope-initial-directory top-directory)
(sit-for 2)
(delete-windows-on "*cscope-indexing-buffer*")
(kill-buffer "*cscope-indexing-buffer*")
))
(bind-keys*
("C-c s r" . cscope-create-database))

whitespace-mode in minibuffer?

Is there a way I can turn on whitespace-mode in a minibuffer? I'm not sure I'll stick to it - but sometimes I need it. I tried to add-hook all the M-x apropos RET minibuffer hook RET:
(mapc
(lambda (language-mode-hook)
(add-hook language-mode-hook
(lambda () (interactive) (whitespace-mode 1))))
'(minibuffer-setup-hook
icicle-minibuffer-setup-hook
icomplete-minibuffer-setup-hook
ido-minibuffer-setup-hook
minibuffer-inactive-mode-hook))
but it doesn't work.
It looks like you've got something that should work, this worked for me:
(defun enable-ws-mode ()
(whitespace-mode 1))
(add-hook 'minibuffer-inactive-mode-hook 'enable-ws-mode)
as did the code you posted. Have you tried running Emacs without your initialization files?
emacs -q
emacs -q --no-site-init
and evaluating the code you had?
Perhaps this is only because it's been six years, but . . . with Emacs 26.3 I was able to add the regular whitespace mode command to the hook with no intermediating function:
(add-hook 'minibuffer-inactive-mode-hook 'whitespace-mode)
No fuss, no muss, works here.

In Emacs, can we make one keystroke to do different command?

I want to make one keystroke, say C-F12, to do delete-other-windows or winner-undo. I think it's easy if I already learning Emacs Lisp programming, and set a boolean flag. That is, if previously it run delete-other-window, now it'll run winner-undo.
How do you do that in Emacs Lisp?
Thanks
Try something like this
(setq c-f12-winner-undo t)
(define-key (current-global-map) [C-f12]
(lambda()
(interactive)
(if c-f12-winner-undo
(winner-undo)
(delete-other-windows))
(setq c-f12-winner-undo (not c-f12-winner-undo))))
(defun swdev-toggle-sole-window ()
(interactive)
(if (cdr (window-list))
(delete-other-windows)
(winner-undo)))
(global-set-key (kbd "<C-f12>") 'swdev-toggle-sole-window)
The first line starts the declaration of a function called swdev-toggle-sole-window, taking no argument.
This function is declared as interactive, i.e. it can be called with M-x or through a key binding.
If the window list contains more than one element, i.e. if there is more than one window, …
… then delete other windows …
… else undo the window deletion.
Bind the function to the key C-f12.
Here's a solution using the approach taken by Emacs' recenter-top-bottom function:
(defun delete-other-window-or-winner-undo ()
"call delete-other-window on first invocation and winner-undo on subsequent invocations"
(interactive)
(if (eq this-command last-command)
(winner-undo)
(delete-other-windows)))
(global-set-key (kbd "<C-f12>") 'delete-other-window-or-winner-undo)

function to call same shell command in dired

i'd like to be able to call the same shell command on the marked files in dired without the need for emacs to prompt the command input as the command will always be the same. in particular, the command is "open" (for mac os x).
i tried to hack the function dired-do-shell-command in dired-aux.el but i don't understand the interactive line.
at the end of the day, i'd like to be able to bind this function to C-o for dired-mode so that i don't have to use mac os x's Finder to navigate files and open them. this will allow me to move to emacs entirely.
thanks.
(defun dired-open ()
(interactive)
(dired-do-async-shell-command
"open" current-prefix-arg
(dired-get-marked-files t current-prefix-arg)))
(define-key dired-mode-map (kbd "C-o") 'dired-open)
Edit:
We may use save-window-excursion to protect the existing window configuration from being messed up by the output buffer:
(defun dired-open ()
(interactive)
(save-window-excursion
(dired-do-async-shell-command
"open" current-prefix-arg
(dired-get-marked-files t current-prefix-arg))))