akermu / emacs-libvterm

Emacs libvterm integration
GNU General Public License v3.0
1.69k stars 135 forks source link

Nushell integration #679

Open herbertjones opened 1 year ago

herbertjones commented 1 year ago

I've got integration working for Nushell 0.83. However there are multiple sections for multiple editors in the README now. It might be best to move them all in a doc/ directory and just have links. I can make a PR if you think that's a good idea. Also, Nushell isn't 1.0 yet (but close), so it may just be better to leave this as an issue until then.

.config/nushell/emacs-config.nu

source ~/.config/nushell/config.nu
source ~/.config/nushell/vterm.nu

.config/nushell/vterm.nu

module vterm {
  # Escape a command for outputting by vterm send
  def escape-for-send [
    to_escape: string # The data to escape
  ] {
    $to_escape | str replace --all '\\' '\\' | str replace --all '"' '\"'
  }

  # Send a command to vterm
  export def send [
    command: string # Command to pass to vterm
    ...args: string # Arguments to pass to vterm
  ] {
    print --no-newline "\e]51;E"
    print --no-newline $"\"(escape-for-send $command)\" "
    for arg in $args {
      print --no-newline $"\"(escape-for-send $arg)\" "
    }
    print --no-newline "\e\\"
  }

  # Clear the terminal window
  export def clear [] {
    send vterm-clear-scrollback
    tput clear
  }

  # Open a file in Emacs
  export def open [
    filepath: path # File to open
  ] {
    send "find-file" $filepath
  }
}

module vprompt {
  # Complete escape sequence based on environment
  def complete-escape-by-env [
    arg: string # argument to send
  ] {
    let tmux: string = (if ($env.TMUX? | is-empty) { '' } else { $env.TMUX })
    let term: string = (if ($env.TERM? | is-empty) { '' } else { $env.TERM })
    if $tmux =~ "screen|tmux" {
      # tell tmux to pass the escape sequences through
      $"\ePtmux;\e\e]($arg)\a\e\\"
    } else if $term =~ "screen.*" {
      # GNU screen (screen, screen-256color, screen-256color-bce)
      $"\eP\e]($arg)\a\e\\"
    } else {
      $"\e]($arg)\e\\"
    }
  }

  # Output text prompt that vterm can use to track current directory
  export def left-prompt-track-cwd [] {
    $"(create_left_prompt)(complete-escape-by-env $'51;A(whoami)@(hostname):(pwd)')"
  }
}

use vterm
use vprompt
$env.PROMPT_COMMAND = {|| vprompt left-prompt-track-cwd }

Emacs config

(setf vterm-shell "nu --config ~/.config/nushell/emacs-config.nu")

Examples

Autocomplete

~| vterm
vterm send   Send a command to vterm
vterm open   Open a file in Emacs
vterm clear  Clear the terminal window
joostkremers commented 12 months ago

I'm trying out nushell and this is very helpful, so thanks!

I do have one question: if I understand correctly, you're defining an open command to open files in Emacs, but how do I use it without conflicting with nushell's built-in open command?

[edit] Ok, I think I found it, I need to use vterm open...

Quick tip for anyone else reading this: you can add an alias to emacs-config.nu to access this command with fewer keystrokes:

alias ff = vterm open
anquegi commented 5 months ago

Thanks @herbertjones for this configuration, it works pretty well, I think that this should be added to the official README.

I It works well

Screenshot_20240414_095001

But I have some weird symbols it seems this problemrevert-buffer-with-coding-system but it didn't work

herbertjones commented 5 months ago

@anquegi I don't have that problem. Perhaps the font you are using is missing those glyphs.

joostkremers commented 5 months ago

@anquegi That's definitely an encoding issue. I'm not sure what you hoped to get from the page you linked to, but revert-buffer-with-coding-system is not the answer. You'll need to make sure the encodings match up. I can't help with that, though. I use UTF-8 throughout my system just to avoid such issues...

anquegi commented 4 months ago

Thanks for reading this and sorry for my too late answer. Maybe the problem in my case is thar I'm not using a valid locale for emacs xlib:

 emacs --debug-init                                                                                
Loading /home/toni/.config/emacs/lisp/doom.el (source)...
* 0.008225:*:load: doom-start nil
* 0.009674:*:hook:doom-before-init-hook: run doom--begin-init-h
* 0.009707::context: +init (t)
* 0.009723:*:init:hook:doom-before-init-hook: run doom--reset-custom-dont-initialize-h
* 0.009749:*:init:hook:doom-before-init-hook: run doom--reset-load-suffixes-h
* 0.010025:*:init:load: ~/.config/doom/init t
Warning: locale not supported by Xlib, locale set to C
locale                                                                                   
LANG=ca_ES@valencia
LC_CTYPE="ca_ES@valencia"
LC_NUMERIC=es_ES.UTF-8
LC_TIME=es_ES.UTF-8
LC_COLLATE="ca_ES@valencia"
LC_MONETARY=es_ES.UTF-8
LC_MESSAGES="ca_ES@valencia"
LC_PAPER=es_ES.UTF-8
LC_NAME=es_ES.UTF-8
LC_ADDRESS=es_ES.UTF-8
LC_TELEPHONE=es_ES.UTF-8
LC_MEASUREMENT=es_ES.UTF-8
LC_IDENTIFICATION=es_ES.UTF-8
LC_ALL=

may be should I change to a locale with better support