首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >emacs shell:使用ido更改目录

emacs shell:使用ido更改目录
EN

Stack Overflow用户
提问于 2014-01-06 15:13:15
回答 1查看 1.3K关注 0票数 7

我越来越多地使用emacs shell模式,我希望可以改进一些东西:更改目录时的完成。我很想用idoprojectile-find-dir来做这个。

我的工作流

从今天起,我尽我所能在emacs的外壳之外,尽可能地使用emacs的功能(使用ido访问文件,用projectile在项目中查找文件,探索dired,…中的树)。。

我不常听cd。当我在不同的项目中工作时,我会打开另一个shell缓冲区。但是当我必须这样做的时候,我真的很想念ido或者fasd shell实用程序(它可以工作,但是没有它的完成界面-它与zsh很好,而且它没有ido的使用强大到可以是https://github.com/clvv/fasd)。

如何用elisp ?连接

我知道我们可以给ido-completing-read一个列表;

在shell中,键入cd ../<TAB>将打开一个新的*Completions*缓冲区。它使用comint-dynamic-completion,但是如何在elisp列表中,而不是在缓冲区中获得该列表?

  • 是否有可能将完成列表连接到ido中?(或抛射物、头盔或其他东西)
  • 如果你能把我和准确的文档联系起来,我也会很感激(有很多,很难知道什么对我有用)
  • 或者有解决办法了吗?

谢谢!

编辑:下面是最近访问目录的另一种很好的方式,使用fasd实用程序和ido完成:https://gitlab.com/emacs-stuff/fasd-shell/blob/master/README.org

another SO question

ps:一些shell脚本不能很好地工作,我想保持shell模式。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-19 18:07:11

尝试一下,这是一个快速和肮脏的黑客,在某些情况下可能会失败,但一般情况下应该有效。也请原谅我的私奔

代码语言:javascript
复制
(require 'ido)
(require 'cl-lib)
(require 'shell)

(defvar my-dir-selected nil "Flag to indicate that user has selected the directory")

(defun my-filter-cd-input (current-input)
  "Takes current user input for `cd' the a list
    whose car is the 'maximum possible directory path'
    and cdr is remaining string.

    Examples:
    '~/.emacs.d/in => ('~./emacs.d/' 'in')
    '/home/gue' => ('/home/' 'gue')
    '~/../' => ('~/../' '')"
  (let* ((unquoted-input (shell-unquote-argument current-input))
     (components (split-string unquoted-input "/"))
         (directory-parts (butlast components))
         (possible-prefix (car (last components))))
    (list (if (string= possible-prefix "")
              unquoted-input
            (concat (mapconcat 'identity directory-parts "/")
                    (when directory-parts "/")))
          possible-prefix)))

(defun my-complete-directory-name (directory current-input)
  "Prompts user for directories in `directory', `current-input'
    is the string entered by the user till now"
  (let* ((filtered-input (my-filter-cd-input current-input))
         (directory-path (car filtered-input))
         (partial-input (cadr filtered-input))
         (directory-choices (mapcar 'file-name-nondirectory
                                    (condition-case nil
                                        (cl-remove-if-not 'file-directory-p
                                                          (directory-files (concat directory directory-path) t))
                                      ('file-error (list)))))
         (selected-name (ido-completing-read "Directory: "
                                             directory-choices
                                             nil nil partial-input)))
    (comint-delete-input)
    (insert (concat "cd " 
            (shell-quote-argument (concat directory-path selected-name "/"))))))

(defun my-prompt-for-dir-or-fallback ()
  "If current shell command is `cd' prompt for directory
    using ido otherwise fallback to normal completion"
  (interactive)
  (let* ((user-input (buffer-substring-no-properties (comint-line-beginning-position)
                                                     (point-max))))
    (if (and (>= (length user-input) 3)
             (string= (substring user-input 0 3) "cd "))
        (progn 
          (setq my-dir-selected nil)
          (while (not my-dir-selected)
            (my-complete-directory-name default-directory 
                    (buffer-substring-no-properties (+ (comint-line-beginning-position) 3) 
                                    (point-max))))
          (comint-send-input))
      (call-interactively 'completion-at-point))))

(define-key shell-mode-map (kbd "<tab>") 'my-prompt-for-dir-or-fallback)

(add-hook 'ido-setup-hook 'ido-my-keys)

(defun ido-my-keys ()
  "Add my keybindings for ido."
  (define-key ido-completion-map (kbd "<C-return>") (lambda ()
                                                        (interactive)
                                                        (setq my-dir-selected t)
                                                        (ido-exit-minibuffer))))

如果当前输入的命令是<tab>,则按shell中的cd将提示使用ido的目录可用,否则将回到默认完成状态,退出按C-RET

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20952995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档