简体   繁体   English

如何制作 emacs 快捷方式在相关文件之间切换?

[英]How do I make an emacs shortcut to switch between related files?

For example, I have the file model/user.py open and I want to have a shortcut that opens controller/user.py.例如,我打开了文件 model/user.py,我想要一个打开 controller/user.py 的快捷方式。 Or I want to switch to test/model/testUser.py (contrived example)或者我想切换到 test/model/testUser.py (人为的例子)

I'd like to make an emacs shortcut which given a file currently open, opens files related in various ways.我想做一个 emacs 快捷方式,给定当前打开的文件,以各种方式打开相关文件。

If the "related files" follow some kind of pattern, I think it's trivial to write some elisp functions to do the task.如果“相关文件”遵循某种模式,我认为编写一些 elisp 函数来完成任务是微不足道的。 Let's say you have a model and need to open his associated controller, you will need to do something like this:假设您有一个 model 并且需要打开他关联的 controller,您需要执行以下操作:

(defun my-open-related-controller ()
  (interactive)
  (let* ((name (buffer-file-name))) ;gets the filename of the current buffer
    ;; Of course, this is only an example. The point here is that you need
    ;; to "discover" the name of the related file based on the current one.
    (setf name (replace-regexp-in-string "model" "controller" name))

    ;; Now you will open the file(if it isn't open already) and switch to it
    (find-file name)))

Then you can bind the function to, say, F5:然后您可以将 function 绑定到 F5:

(define-key name-of-the-mode-map [f5] 'my-open-related-controller)

If you want to crate this binding globally, use:如果要全局创建此绑定,请使用:

(global-set-key [f5] 'my-open-related-controller)

Of course, this is just a crude example(since you didn't give many specific details), but should be enough to get you started.当然,这只是一个粗略的示例(因为您没有提供很多具体细节),但应该足以让您入门。 Hope it helps!希望能帮助到你!

If you don't fancy writing this yourself and would rather customize an exisiting library, you may like to look at toggle.el .如果您不喜欢自己编写此代码并希望自定义现有库,您可能希望查看toggle.el It's designed to do what you're asking for.它旨在满足您的要求。

There is also the jump.el that rinari uses for this purpose (except for Ruby on Rails projects).还有rinari用于此目的的jump.el (Ruby on Rails 项目除外)。 I gave the second link, because rinari.el in this project contains settings that manage jumps from one place to another (controller to view, model, migrations, etc.).我给出了第二个链接,因为这个项目中的 rinari.el 包含管理从一个地方到另一个地方的跳转的设置(控制器到视图、model、迁移等)。

It looks like you can get jump.el to jump to a particular method in a file - but that may take a bit of effort.看起来您可以让 jump.el 跳转到文件中的特定方法 - 但这可能需要一些努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM