Bagaimana saya bisa membuka shell di buffer saat ini?


8

Dalam fungsi "shell" baris ini menampilkan buffer shell dan hampir selalu membuka shell baru dalam bingkai acak. Dan saya harus menukar buffer, itu menjengkelkan.

...
(pop-to-buffer buffer)
...

Dalam dokumen tertulis:

Jika jendela itu berada pada bingkai grafis yang berbeda, bingkai itu diberikan fokus input jika memungkinkan.

Saya tidak mengerti bagaimana saya bisa menampilkan buffer dalam bingkai target, saya tidak cukup baik di elisp. Bagaimana saya bisa melakukan itu? Terima kasih jika ada yang bisa membantu.


2
ini membuatku gila, apakah Anda menemukan solusi?
Nisba


Anda harus mencoba ibuffer, lihat ini emacs.stackexchange.com/questions/38659/… .
Pierre ALBARÈDE

Jawaban:


5

Saya berasumsi bahwa poster asli berarti menargetkan jendela yang saat ini dipilih dalam bingkai yang sama. Jika poster asli berkeinginan menargetkan jendela tertentu dalam bingkai yang berbeda, maka lihat utas terkait ini untuk contoh kompleks: /programming/18346785/how-to-intercept-a-file- sebelum-itu-membuka-dan-memutuskan-bingkai mana

Saya menyalin fungsi shellbawaan dan membuat yang baru dipanggil shell-get-buffer-create, yang menggunakan with-current-buffer ...alih-alih pop-to-buffer. Fungsi baru ini menghasilkan atau menempatkan *shell*buffer tanpa memilihnya di jendela apa pun.

Untuk menampilkan *shell*buffer di jendela yang dipilih saat ini gunakan:

(switch-to-buffer (shell-get-buffer-create))

atau

(pop-to-buffer-same-window (shell-get-buffer-create))

atau

(set-window-buffer (selected-window) (shell-get-buffer-create))

Saya membuat fungsi khusus yang disebut my-display-bufferyang dapat ditampilkan dalam empat arah - kiri, kanan, atas, atau di bawah. Ada tiga kemungkinan: (1) Jika jendela pada bingkai sudah menampilkan buffer target, maka gunakan kembali jendela yang sama. (2) Jika sudah ada jendela dalam arah yang ditentukan sehubungan dengan jendela yang dipilih, maka tampilkan buffer target di jendela tersebut. (3) Jika tidak ada jendela di arah yang ditentukan, lalu buat satu di arah itu dan tampilkan buffer target di jendela tersebut.

Penggunaan sampel :

(my-display-buffer (shell-get-buffer-create) nil 'left)

atau

(my-display-buffer (shell-get-buffer-create) nil 'right)

atau

(my-display-buffer (shell-get-buffer-create) nil 'above)

atau

(my-display-buffer (shell-get-buffer-create) nil 'below)

Kode :

(require 'shell)

(defun shell-get-buffer-create (&optional buffer)
  "Run an inferior shell, with I/O through BUFFER (which defaults to `*shell*').
Interactively, a prefix arg means to prompt for BUFFER.
If `default-directory' is a remote file name, it is also prompted
to change if called with a prefix arg.

If BUFFER exists but shell process is not running, make new shell.
If BUFFER exists and shell process is running, just switch to BUFFER.
Program used comes from variable `explicit-shell-file-name',
 or (if that is nil) from the ESHELL environment variable,
 or (if that is nil) from `shell-file-name'.
If a file `~/.emacs_SHELLNAME' exists, or `~/.emacs.d/init_SHELLNAME.sh',
it is given as initial input (but this may be lost, due to a timing
error, if the shell discards input when it starts up).
The buffer is put in Shell mode, giving commands for sending input
and controlling the subjobs of the shell.  See `shell-mode'.
See also the variable `shell-prompt-pattern'.

To specify a coding system for converting non-ASCII characters
in the input and output to the shell, use \\[universal-coding-system-argument]
before \\[shell].  You can also specify this with \\[set-buffer-process-coding-system]
in the shell buffer, after you start the shell.
The default comes from `process-coding-system-alist' and
`default-process-coding-system'.

The shell file name (sans directories) is used to make a symbol name
such as `explicit-csh-args'.  If that symbol is a variable,
its value is used as a list of arguments when invoking the shell.
Otherwise, one argument `-i' is passed to the shell.

\(Type \\[describe-mode] in the shell buffer for a list of commands.)"
  (interactive
   (list
    (and current-prefix-arg
   (prog1
       (read-buffer "Shell buffer: "
        ;; If the current buffer is an inactive
        ;; shell buffer, use it as the default.
        (if (and (eq major-mode 'shell-mode)
           (null (get-buffer-process (current-buffer))))
            (buffer-name)
          (generate-new-buffer-name "*shell*")))
     (if (file-remote-p default-directory)
         ;; It must be possible to declare a local default-directory.
               ;; FIXME: This can't be right: it changes the default-directory
               ;; of the current-buffer rather than of the *shell* buffer.
         (setq default-directory
         (expand-file-name
          (read-directory-name
           "Default directory: " default-directory default-directory
           t nil))))))))
  (setq buffer (if (or buffer (not (derived-mode-p 'shell-mode))
                       (comint-check-proc (current-buffer)))
                   (get-buffer-create (or buffer "*shell*"))
                 ;; If the current buffer is a dead shell buffer, use it.
                 (current-buffer)))

  ;; On remote hosts, the local `shell-file-name' might be useless.
  (if (and (called-interactively-p 'any)
     (file-remote-p default-directory)
     (null explicit-shell-file-name)
     (null (getenv "ESHELL")))
      (with-current-buffer buffer
  (set (make-local-variable 'explicit-shell-file-name)
       (file-remote-p
        (expand-file-name
         (read-file-name
    "Remote shell path: " default-directory shell-file-name
    t shell-file-name))
        'localname))))

  ;; The buffer's window must be correctly set when we call comint (so
  ;; that comint sets the COLUMNS env var properly).
  (with-current-buffer buffer
    (unless (comint-check-proc buffer)
      (let* ((prog (or explicit-shell-file-name
           (getenv "ESHELL") shell-file-name))
       (name (file-name-nondirectory prog))
       (startfile (concat "~/.emacs_" name))
       (xargs-name (intern-soft (concat "explicit-" name "-args"))))
        (unless (file-exists-p startfile)
    (setq startfile (concat user-emacs-directory "init_" name ".sh")))
        (apply 'make-comint-in-buffer "shell" buffer prog
         (if (file-exists-p startfile) startfile)
         (if (and xargs-name (boundp xargs-name))
       (symbol-value xargs-name)
           '("-i")))
        (shell-mode))))
  buffer)

(defun my-display-buffer (buffer 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 ((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))

Dan, ini adalah eshell-get-buffer-createyang berfungsi sama seperti di atas ...

(require 'cl)
(require 'eshell)

(defun eshell-get-buffer-create (&optional arg)
"Create an interactive Eshell buffer.  Return the Eshell buffer,
creating it if needed.  The buffer used for Eshell sessions is
determined by the value of `eshell-buffer-name'.  A numeric prefix
arg (as in `C-u 42 M-x eshell RET') switches to the session with
that number, creating it if necessary.  A nonnumeric prefix arg
means to createa new session.  Returns the buffer selected (or created)."
  (interactive "P")
  (cl-assert eshell-buffer-name)
  (let ((buf (cond ((numberp arg)
        (get-buffer-create (format "%s<%d>"
                 eshell-buffer-name
                 arg)))
       (arg
        (generate-new-buffer eshell-buffer-name))
       (t
        (get-buffer-create eshell-buffer-name)))))
    (cl-assert (and buf (buffer-live-p buf)))
    (with-current-buffer buf
      (unless (derived-mode-p 'eshell-mode)
        (eshell-mode)))
    buf))

4

Bukan jawaban langsung untuk pertanyaan Anda, tapi mungkin ini solusi untuk Anda.

M-x install-package shell-pop

Untuk menginstal paket shell-pop dari melpa atau stabil-melpa. Kemudian evaluasi (untuk uji coba *scratch*melalui C-x C-e, nanti di Anda .emacs)

(global-set-key (kbd "<C-M-return>") 'shell-pop)

Ini memunculkan shell di bagian bawah bingkai Anda, dalam hal ini saat menekan ctrl-meta-ret. Mengulangi urutan keyboard kembali ke tempat Anda sebelumnya.

Lihat https://github.com/kyagi/shell-pop-el untuk detailnya.

Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.