【问题标题】:How to create a right-click context shell shortcut "edit with Emacs"?如何创建右键单击上下文 shell 快捷方式“使用 Emacs 编辑”?
【发布时间】:2010-10-02 02:08:21
【问题描述】:

Notepad++ 会自动添加一个 shell 快捷方式,这样当您在 Windows 资源管理器中时,您可以右键单击文件并选择“使用 Notepad++ 编辑”。我怎样才能对 emacs 做同样的事情?我正在使用适用于 Windows 的 GNU Emacs 22.3。

【问题讨论】:

  • 想要(主要)鼠标驱动的方法来启动重键盘的编辑器似乎很奇怪!你不只是使用控制台吗?
  • 一点也不。良好的生产力来自正确混合键盘和鼠标。能够从 Windows 资源管理器启动 Emacs 对许多人来说很有用。

标签: windows emacs windows-shell windows-explorer


【解决方案1】:

这就是我所拥有的 - 类似于其他一些答案。在某处创建一个名为 emacs-conextmenu.reg(或任何你想要的任何东西)的新文本文件,然后将以下内容粘贴到:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell]
[HKEY_CLASSES_ROOT\*\shell\openwemacs]
@="&Edit with Emacs"
[HKEY_CLASSES_ROOT\*\shell\openwemacs\command]
@="Absolute\\Path\\to\\your\\emacs\\bin\\emacsclientw.exe -n \"%1\""
[HKEY_CLASSES_ROOT\Directory\shell\openwemacs]
@="Edit &with Emacs"
[HKEY_CLASSES_ROOT\Directory\shell\openwemacs\command]
@="Absolute\\Path\\to\\your\\emacs\\bin\\emacsclientw.exe -n \"%1\""

把路径改成你的emacs安装路径;记住要转义“\”(只要有 \,就将其更改为 \\)。

现在您需要做的就是在资源管理器中双击此 *.reg 文件,您将拥有一个适用于任何文件和任何目录的 emacs 上下文菜单条目(如果您是一个狂热的粉丝!)。

请注意,要使其正常工作,必须启动 emacs 并且还必须启动 emacs-server (M-x server-start)。我建议使用 Windows 启动 emacs 并将 (server-start) 放入您的 .emacs 文件中。

作为奖励,当您在 Windows 资源管理器中按 ctrl-shift-enter 时,自动热键 (http://www.autohotkey.com/) 的以下 sn-p 将启动在 emacs 中选择的文件。如果您在 emacs 中编辑大量文件但不一定要导航到 emacs 本身中的文件,这可能会更方便。

#IfWinActive ahk_class CabinetWClass 
^+Enter::
  GetText(tmpvar)
  If (tmpvar != "")
     Run, d:/path/to/your/emacs/bin/dir/emacsclientw.exe -n "%tmpvar%"
Return
Return

【讨论】:

  • 您可能希望将 %1 替换为 %L,否则 Windows 将为 Emacs 提供文件的短名称。看到这个问题:stackoverflow.com/questions/892934/…
  • 很好的答案!但是,一些 Emacs 专家能否更进一步,展示如何在 Emacs 中构建和执行 .reg 文件。包括 emacs 本身的绝对路径?我还是个新手,但我敢打赌 Emacs pro 可以让这变得非常简单。我有说过我有多喜欢 Emacs 吗?
  • 另外值得一提的是,您可能需要重新启动或停止/重新启动 explorer.exe,如此处所述pctools.com/forum/…
  • 如果要在路径中包含环境变量(如%HOME%),则注册表项的类型必须为REG_EXPAND_SZ 而不是REG_SZ
  • 这对任何人都适用于 Windows 10 吗? (对我来说没有)
【解决方案2】:

就像 polyglot 的答案一样,但不需要启动服务器或任何混乱。

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell]
[HKEY_CLASSES_ROOT\*\shell\openwemacs]
@="&Edit with Emacs"
[HKEY_CLASSES_ROOT\*\shell\openwemacs\command]
@="C:\\Program Files (x86)\\Emacs\\bin\\emacsclientw.exe --alternate-editor=\"C:\\Program Files (x86)\\Emacs\\bin\\runemacs.exe\" -n \"%1\""
[HKEY_CLASSES_ROOT\Directory\shell\openwemacs]
@="Edit &with Emacs"
[HKEY_CLASSES_ROOT\Directory\shell\openwemacs\command]
@="C:\\Program Files (x86)\\Emacs\\bin\\emacsclientw.exe --alternate-editor=\"C:\\Program Files (x86)\\Emacs\\bin\\runemacs.exe\" -n \"%1\""

【讨论】:

  • 您也可以通过在@="Edit &with Emacs" 行下方添加行"icon"="C:\\Program Files (x86)\\Emacs\\bin\\emacsclientw.exe" 来指定图标
【解决方案3】:

如果您不准备探索注册表,您可以从 Windows 资源管理器中执行此操作(任何编辑器的说明,而不仅仅是 Emacs):

  1. 打开 Windows 资源管理器并选择 Tools\Folder Options... 菜单项。

  2. 单击File Types 选项卡并选择要与编辑器关联的文件类型的扩展名。对于本例,我将使用 TXT 扩展。

  3. 点击详情框中的Advanced按钮,弹出Edit File Type对话框。

  4. 您可以更改当前的open 操作,或者指定一个新的操作,例如Open with MyEditor

    如果您选择编辑,请单击Edit... 按钮并在Application used to peform action 文本框中输入编辑器的完整路径,然后输入"%1"。例如,C:\SciTe\wscite\SciTE.exe "%1"

    如果您想创建一个新动作,请单击 New... 按钮,为动作命名,并提供编辑器的完整路径,然后是 "%1"

  5. 如果您想将某个操作设为该文件类型的默认操作,请选择该操作,然后单击Set Default 按钮。

  6. OK 在所有对话框中。

另一种选择是将编辑器可执行文件的快捷方式放在Send To 文件夹%USERSPROFILE%\SendTo 中。我通常在这里创建一个名为“Editors”的新文件夹,并为我使用的各种编辑器放置快捷方式。

【讨论】:

    【解决方案4】:

    加上一点点,也可以通过点击背景在 emacs 中打开当前目录。

    <<<Code as posted by polyglot>>>
    
    [HKEY_CLASSES_ROOT\Directory\Background\shell\openwemacs]
    @="Open &with Emacs"
    [HKEY_CLASSES_ROOT\Directory\Background\shell\openwemacs\command]
    @="C:\\Program Files\\emacs-24.2\\bin\\runemacs.exe \"%V\""
    

    这里%V 是当前目录。在这种情况下使用 %1 不起作用。

    【讨论】:

      【解决方案5】:

      这是做同样事情的另一种方法。适用于 WinXP 和 Vista。

      将此添加到您的注册表中:

      edit-with-emacs.reg

      Windows Registry Editor Version 5.00
      [HKEY_CLASSES_ROOT\*\shell\Emacs]
      @="Edit With &Emacs"
      [HKEY_CLASSES_ROOT\*\shell\Emacs\command]
      @="Wscript.exe C:\\emacs\\emacs-22.3\\bin\\launch-emacs-client.vbs \"%1\""
      

      将此文件放在您的 emacs bin 目录中:

      launch-emacs-client.vbs

      Set objShell = WScript.CreateObject("WScript.Shell")
      Set fso = CreateObject("Scripting.FileSystemObject")
      
      If WScript.Arguments.Count = 1 Then
      
        strComputer = "."
      
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
      
        Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
      
        Dim isRunning
        isRunning = False
      
        For Each objItem in colItems
          If InStr(objItem.CommandLine, "emacs.exe") Then
            isRunning = True
          End If
        Next
      
        If isRunning Then
          objShell.Run(fso.GetParentFolderName(WScript.ScriptFullName) & "/emacsclientw.exe -n """ & WScript.Arguments(0) & """")
        Else
          objShell.Run(fso.GetParentFolderName(WScript.ScriptFullName) & "/runemacs.exe """ & WScript.Arguments(0) & """")
        End If
      
      Else
        objShell.Run(fso.GetParentFolderName(WScript.ScriptFullName) & "/runemacs.exe")
      End If
      

      注意:W32 installer 在安装时运行类似的脚本。

      【讨论】:

        【解决方案6】:

        This site 解释了如何使用另一个应用程序进行操作。只需更改路径即可。

        创建此键/值:

        [HKEY_CLASSES_ROOT\*\shell\Edit with AppName\command]
        @=”\”C:\\Program Files\\Notepad2\\Notepad2.exe\” \”%1\”"
        

        这里是another reference,比较容易理解。

        【讨论】:

          【解决方案7】:

          查看带有 Windows 集成的 Emacs 发行版:http://ourcomments.org/Emacs/EmacsW32.html

          它的安装程序会创建一个资源管理器菜单项来执行您想要的操作。

          【讨论】:

            【解决方案8】:

            我想在上面的 polyglot 的答案中添加 - 他提到的 AutoHotkey 快捷方式非常方便,但代码不完整:GetText() 函数是由 AutoHotkey 论坛 (http://www.autohotkey.com/forum/viewtopic.php?p=279576#279576) 上的某个人编写的,它是:

            GetText(ByRef MyText = "")
            {
               SavedClip := ClipboardAll
               Clipboard =
               Send ^c
               ClipWait 0.1
               If ERRORLEVEL
               {
                  Clipboard := SavedClip
                  MyText =
                  ERRORLEVEL := 1
                  Return
               }
               MyText := Clipboard
               Clipboard := SavedClip
               Return MyText
            }
            
            ;to test: 
            #k::
            GetText(FileName)
            msgbox clipboard=%FileName%
            return
            

            请注意,可能需要修改 ClipWait 延迟:我必须将其设置为 2!

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-04-23
              • 1970-01-01
              • 1970-01-01
              • 2012-12-09
              • 1970-01-01
              相关资源
              最近更新 更多