windrg00
windrg00

Reputation: 457

Emacs auto-display of things at point in a right window

I'd like to implement a function it shows relevant code paragraph in its right window when Emacs cursor is prompting on a certain word.

Let's assume that I have my own log format and I have corresponding database which I can find where a log message came from then I have the pointer on a certain log message. If so, then I like to let Emacs open the corresponding source file in a right side window of that log buffer. Now I can query and get the location of source file thru my own db and emacs. But I still don't know how to control the right window.

If I opened a right window once, then Emacs would open an another one again, I don't want to let it do but want to let it use the previous, existing window. How could I implement this? Please advise me, or share an example you might have.

Thanks.

Upvotes: 0

Views: 416

Answers (1)

lawlist
lawlist

Reputation: 13467

In addition to the examples below using the custom function my-display-buffer, keep in mind that BUFFER can be obtained by means other than find-file-noselect; e.g., current-buffer if so desired. In terms of finding your location in the other window, you may find it helpful to select-window or with-selected-window or set-window-point, etc. If the window is selected with the target buffer visible in said window, then simple things like goto-char will suffice to go visually to a particular location. The example my-display-buffer function has a doc-string which describes generally what it was designed to do; i.e., "There are three possibilities ...".

Display buffer to the left:

(let ((buffer (find-file-noselect "~/foo.py")))
  (with-current-buffer buffer
    (message "major-mode:  %s" major-mode))
  (my-display-buffer buffer nil 'left))

Display buffer to the right:

(let ((buffer (find-file-noselect "~/foo.py")))
  (with-current-buffer buffer
    (message "major-mode:  %s" major-mode))
  (my-display-buffer buffer nil 'right))

Display buffer above:

(let ((buffer (find-file-noselect "~/foo.py")))
  (with-current-buffer buffer
    (message "major-mode:  %s" major-mode))
  (my-display-buffer buffer nil 'above))

Display buffer below:

(let ((buffer (find-file-noselect "~/foo.py")))
  (with-current-buffer buffer
    (message "major-mode:  %s" major-mode))
  (my-display-buffer buffer nil 'below))
(defun my-display-buffer (buffer-or-name alist direction &optional size pixelwise)
"BUFFER:  The buffer that will be displayed.
ALIST:  See the doc-string of `display-buffer' for more information.
DIRECTION:  Must use one of these symbols:  'left 'right 'below 'above
SIZE:  See the doc-string for `split-window'.
PIXELWISE:  See the doc-string for `split-window'.
There are three possibilities:
-  (1) If a window on the frame already displays the target buffer,
then just reuse the same window.
-  (2) If there is already a window in the specified direction in relation
to the selected window, then display the target buffer in said window.
-  (3) If there is no window in the specified direction, then create one
in that direction and display the target buffer in said window."
  (let* ((buffer
           (if (bufferp buffer-or-name)
             buffer-or-name
             (get-buffer buffer-or-name)))
         (window
           (cond
             ((get-buffer-window buffer (selected-frame)))
             ((window-in-direction direction))
             (t
               (split-window (selected-window) size direction pixelwise)))))
    (window--display-buffer buffer window 'window alist display-buffer-mark-dedicated)
    window))

Upvotes: 2

Related Questions