在特定窗口中从dired打开文件

时间:2013-03-15 20:56:29

标签: emacs dired

当我们在dired中使用 o 时,它会在“其他窗口”中打开一个文件。但是,如果有多个其他窗口可见,则它似乎随机选择。有没有办法选择打开该文件的窗口?

2 个答案:

答案 0 :(得分:1)

我也被那种直率的行为所困扰。我喜欢ace-window让你轻松选择只有一个按键的窗口的方式,所以写了一个函数,让我用它在我选择的窗口中从dired打开一个文件:

(require 'ace-window)
(defun find-file-ace-window ()
  "Use ace window to select a window for opening a file from dired."
  (interactive)
  (let ((file (dired-get-file-for-visit)))
    (if (> (length (aw-window-list)) 1)
        (aw-select "" (lambda (window)
                        (aw-switch-to-window window)
                        (find-file file)))
      (find-file-other-window file))))

替换dired标准find-file-other-window命令add

(define-key dired-mode-map "o" 'find-file-ace-window)

答案 1 :(得分:0)

我怀疑这种行为是随机的,但您可以通过挂钩display-buffer并绑定自己的命令来改变它。

请注意,大多数代码都是按区域对列表进行排序。

(defvar le::dired-chosen-window nil
  "current chosen window to show buffers for `le::dired-choose-window-action'")

(defun le::window-list-for-completion ()
  "Return alist of (BUFFER-NAME . WINDOW) sorted by size,
  suitable for completion."
  ;; Schwartzian transform
  (mapcar
   (lambda (cell)
     (let ((window (car cell)))
       (cons (concat
              (when (and (boundp 'window-numbering-mode)
                         window-numbering-mode)
                (format "%s: "
                        (window-numbering-get-number-string window)))
              (buffer-name (window-buffer window)))
             window)))
   (sort (loop with boost-alist = (if (eq major-mode 'dired-mode)
                                      (list (cons (selected-window) -1000000))
                                    nil)
               for window in (window-list nil 0 (window-at 0 0))
               for index from 0
               collect (cons window (+
                                     (* (window-total-width window)
                                        (window-total-height window)
                                        100)
                                     index
                                     (let ((boost (assq window boost-alist)))
                                       (if boost
                                           (cdr boost)
                                         0)))))
         (lambda (a b)
           (> (cdr a) (cdr b))))))

(defun le::dired-choose-window (window)
  "Choose window to display buffer in.
Reset selection with universal prefix (C-u)."
  (interactive (let ((completions (le::window-list-for-completion)))
                 (list (unless current-prefix-arg
                         (cdr (assoc (completing-read "window holding buffer: " completions) completions))))))
  (setq le::dired-chosen-window window))

(defun le::dired-choose-window-action (buffer alist)
  "action to display buffer in `le::dired-chosen-window'"
  (window--display-buffer buffer le::dired-chosen-window 'reuse))

(defun le::dired-find-file-specified-window ()
  "Dired command to display buffer in chosen window"
  (interactive)
  (let ((display-buffer-overriding-action '((le::dired-choose-window-action))))
    (dired-find-file-other-window)))


(define-key dired-mode-map [remap dired-find-file-other-window] 'le::dired-find-file-specified-window)
  1. 评估缓冲区。
  2. 选择le::dired-choose-window-action
  3. 窗口
  4. 按下 o 从dired到所选窗口中的打开文件。