Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can emacs window be "partially" dedicated?

Tags:

emacs

I'm using dedicated.el which is very similar to dedicate-windows-manually.el to manually mark certain windows as "dedicated" to their buffer and also inelegible for splitting by various commands that open new buffers (grep, compilation, etc.).

But I really only want the second part.

In other words, I want find-file, kill-buffer, switch-buffer and so on to work as if the current window was not dedicated. But I want other commands (grep, compile) to honor the dedicated status and not split that window.

I made a small attempt at this, but it doesn't work properly and seems like a misguided way to go about it, likely missing many cases.

(One of the problems is that the "dedicated" minor mode is associated with a buffer while set-window-dedicated-p applies to a window; this could be overcome, but I still feel there must be a better way to accomplish what I want...)

(defun with-undedicated (f)
  (interactive)
  (let ((was-dedicated (bound-and-true-p dedicated-mode)))
    (progn
      (if was-dedicated (dedicated-mode))
      (command-execute f)
      (if was-dedicated (dedicated-mode)))))

(defun undedicated-find-file ()
  (interactive)
  (with-undedicated 'ido-find-file))

(defun undedicated-kill-buffer ()
  (interactive)
  (with-undedicated 'ido-kill-buffer))

(defun undedicated-switch-buffer ()
  (interactive)
  (with-undedicated 'ido-switch-buffer))

(global-set-key (kbd "C-x C-f") 'undedicated-find-file)
(global-set-key (kbd "C-x k") 'undedicated-kill-buffer)
(global-set-key (kbd "C-x b") 'undedicated-switch-buffer)

edit

The following was performed to test Drew's answer:

(defun make-weak-dedicated ()
  (interactive)
  (set-window-dedicated-p (selected-window) "weak"))

(defun dedicated-status ()
  (interactive)
  (minibuffer-message "window-dedicated-p: %s" (window-dedicated-p (selected-window))))

Making the window "weak"ly dedicated does indeed do what I want: grep and other popup buffers do not replace the buffer in the weakly dedicated window. However, in that weakly dedicated window, find-file seems to reset window-dedicated-p to nil as tested by my dedicated-status function, so I still don't have a technique to prevent popup buffers from using or splitting a window and allowing find-file etc. to work as normal within that window.

clarification

Sorry for not being clear. Here's what I want to happen:

  • I mark a window as [weakly] dedicated (or whatever equivalent may exist)
  • I invoke grep, compilation, and so on, and they do not split that window nor replace the buffer in that window (instead splitting some other window that is not marked dedicated)
  • Within the weakly dedicated window, I invoke find-file, which replaces the buffer in the window but leaves the window weakly dedicated to the now changed buffer

It's that last bit that isn't working how I had hoped: after find-file, the window is no longer weakly dedicated (window-dedicated-p is nil). Since the property in question is associated with the window, I find it surprising that changing the buffer affected that window property, but so it is.

like image 618
Patrick Avatar asked Nov 01 '22 12:11

Patrick


1 Answers

See the Elisp manual, node Dedicated Windows. If you use function set-window-dedicated-p to give a window a non-nil and non-t value then function set-window-buffer will respect it as a weakly dedicated window. That may be what you are looking for.

like image 124
Drew Avatar answered Dec 07 '22 18:12

Drew