gmj-shell.el 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (defun gmj-shell (arg)
  2. "Start a shell in a buffer named *shell*<HOSTNAME>
  3. An attempt is made to parse HOSTNAME out of the
  4. filename of tramp buffers such as /ssh:FOO@HOSTNAME:/path/path...
  5. When preceded with 1 `universal-argument' (^u),
  6. try to create *shell*<HOSTNAME#>a
  7. where # in 1,2,3,4
  8. When preceded with 2 `universal-argument' (^u^u),
  9. prompt for BUFFER name and try to create *shell*<HOSTNAME#>
  10. "
  11. (interactive "p")
  12. (progn
  13. (message "in gmj-shell")
  14. ;
  15. ; get the hostname
  16. ;
  17. (setq hostname (system-name))
  18. (if (string-prefix-p "/ssh:" (buffer-file-name))
  19. (progn
  20. (setq file-name (buffer-file-name))
  21. (setq hostname (replace-regexp-in-string "/ssh:" "" file-name))
  22. (setq hostname (replace-regexp-in-string ":.*" "" hostname))))
  23. ;
  24. ; determine the shell buffer name
  25. ;
  26. ; if arg is 1, just create (or jump to) *shell*<HOSTNAME>
  27. (if (= arg 1)
  28. (setq shell-name (format "*shell*<%s>" hostname))
  29. ; if arg > 4, prompt for buffer name
  30. (if (> arg 4)
  31. (setq shell-name (concat "*shell*<" (read-string (format "shell name (%s):" "shell4foo") nil nil "shell4foo") ">"))
  32. ; if arg is 4 (one universal arg)
  33. ; try creating new numbered shell "*<shell>*<HOSTNAME#> for # in 1,2,3,4
  34. (progn
  35. (let ((x 1) (lookingForUnusedBuffer t))
  36. (while (and (<= x 4) lookingForUnusedBuffer)
  37. (setq tryThis (format "*shell*<%s-%d>" hostname x))
  38. (unless (get-buffer tryThis)
  39. (progn
  40. (setq lookingForUnusedBuffer nil)
  41. (setq shell-name tryThis)))
  42. (setq x (+ x 1)))))))
  43. ;
  44. ; Try to create the shell
  45. ;
  46. (message (concat "shell name: " shell-name))
  47. (shell shell-name)))