快速打开/隐藏eshell

 仿照shell即开即关的程序(shell-toggle.el),自己改写了一个eshell即开机关的功能。

安装方法:把下面的代码以"eshell-toggle.el”为文件名,放到您的加载目录中;并在.emacs中添加如下代码

  1. (autoload 'shell-toggle "eshell-toggle" 
  2.   "Toggles between the *shell* buffer and whatever buffer you are editing."
  3.   t)
  4. (autoload 'shell-toggle-cd "eshell-toggle" 
  5.   "Pops up a shell-buffer and insert a /"cd <file-dir>/" command." t)
  6. (global-set-key [M-f1] 'shell-toggle)
  7. (global-set-key [C-f1] 'shell-toggle-cd)

eshell-toggle.el文件的内容:

  1. ;;; eshell-toggle.el --- Toggle to and from the *eshell* buffer
  2. ;;; Version 1.2 - 98-11-19
  3. ;;; Copyright (C) 1997, 1998 Mikael Sj鰀in (mic@docs.uu.se)
  4. ;;;
  5. ;;; Author: Mikael Sj鰀in  --  mic@docs.uu.se
  6. ;;;
  7. ;;; This file is NOT part of GNU Emacs.
  8. ;;; You may however redistribute it and/or modify it under the terms of the GNU
  9. ;;; General Public License as published by the Free Software Foundation; either
  10. ;;; version 2, or (at your option) any later version.
  11. ;;;
  12. ;;; The file is distributed in the hope that it will be useful,
  13. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;;; GNU General Public License for more details.
  16. ;;; ----------------------------------------------------------------------
  17. ;;; Description:
  18. ;;;
  19. ;;; Provides the command shell-toggle which toggles between the
  20. ;;; *eshell* buffer and whatever buffer you are editing.
  21. ;;;
  22. ;;; This is done in an "intelligent" way.  Features are:
  23. ;;; o Starts a shell if non is existing.
  24. ;;; o Minimum distortion of your window configuration.
  25. ;;; o When done in the shell-buffer you are returned to the same window
  26. ;;;   configuration you had before you toggled to the shell.
  27. ;;; o If you desire, you automagically get a "cd" command in the shell to the
  28. ;;;   directory where your current buffers file exists; just call
  29. ;;;   shell-toggle-cd instead of shell-toggle.
  30. ;;; o You can convinently choose if you want to have the shell in another
  31. ;;;   window or in the whole frame.  Just invoke shell-toggle again to get the
  32. ;;;   shell in the whole frame.
  33. ;;;
  34. ;;; This file has been tested under Emacs 20.2.
  35. ;;;
  36. ;;; This file can be obtained from http://www.docs.uu.se/~mic/emacs.html 
  37. ;;; ----------------------------------------------------------------------
  38. ;;; Installation:
  39. ;;;
  40. ;;; o Place this file in a directory in your 'load-path.
  41. ;;; o Put the following in your .emacs file:
  42. ;;;   (autoload 'shell-toggle "eshell-toggle" 
  43. ;;;    "Toggles between the *eshell* buffer and whatever buffer you are editing."
  44. ;;;    t)
  45. ;;;   (autoload 'shell-toggle-cd "eshell-toggle" 
  46. ;;;    "Pops up a shell-buffer and insert a /"cd <file-dir>/" command." t)
  47. ;;;   (global-set-key [M-f1] 'shell-toggle)
  48. ;;;   (global-set-key [C-f1] 'shell-toggle-cd)
  49. ;;; o Restart your Emacs.  To use shell-toggle just hit M-f1 or C-f1
  50. ;;;
  51. ;;; For a list of user options look in code below.
  52. ;;;
  53. ;;; ----------------------------------------------------------------------
  54. ;;; BUGS:
  55. ;;;  No reported bugs as of today
  56. ;;; ----------------------------------------------------------------------
  57. ;;; Thanks to:
  58. ;;;   Christian Stern <Christian.Stern@physik.uni-regensburg.de> for helpful
  59. ;;;   sugestions.
  60. ;;; ======================================================================
  61. ;;; User Options:
  62. (defvar shell-toggle-goto-eob t
  63.   "*If non-nil `shell-toggle' will move point to the end of the shell-buffer
  64. whenever the `shell-toggle' switched to the shell-buffer.
  65. When `shell-toggle-cd' is called the point is allways moved to the end of the
  66. shell-buffer")
  67. (defvar shell-toggle-automatic-cd t
  68.   "*If non-nil `shell-toggle-cd' will send the /"cd/" command to the shell.
  69. If nil `shell-toggle-cd' will only insert the /"cd/" command in the 
  70. shell-buffer.  Leaving it to the user to press RET to send the command to 
  71. the shell.")
  72. ;;; ======================================================================
  73. ;;; Commands:
  74. (defun shell-toggle-cd ()
  75.   "Calls `shell-toggle' with a prefix argument.  Se command `shell-toggle'"
  76.   (interactive)
  77.   (shell-toggle t))
  78. (defun shell-toggle (make-cd)
  79.   "Toggles between the *eshell* buffer and whatever buffer you are editing.
  80. With a prefix ARG also insert a /"cd DIR/" command into the shell, where DIR is
  81. the directory of the current buffer.
  82. Call twice in a row to get a full screen window for the *eshell* buffer.
  83. When called in the *eshell* buffer returns you to the buffer you were editing
  84. before caling the first time.
  85. Options: `shell-toggle-goto-eob'"
  86.   (interactive "P")
  87.   ;; Try to descide on one of three possibilities:
  88.   ;; If not in shell-buffer, switch to it.
  89.   ;; If in shell-buffer and called twice in a row, delete other windows
  90.   ;; If in shell-buffer and not called twice in a row, return to state before
  91.   ;;  going to the shell-buffer 
  92.   (if (eq major-mode 'eshell-mode)
  93.       (if (and (or (eq last-command 'shell-toggle)
  94.            (eq last-command 'shell-toggle-cd))
  95.            (not (eq (count-windows) 1)))
  96.       (delete-other-windows)
  97.     (shell-toggle-buffer-return-from-shell))
  98.     (shell-toggle-buffer-goto-shell make-cd)))
  99. ;;; ======================================================================
  100. ;;; Internal functions and declarations
  101. (defvar shell-toggle-pre-shell-win-conf nil
  102.   "Contains the window configuration before the *eshell* buffer was selected")
  103. (defun shell-toggle-buffer-return-from-shell ()
  104.   "Restores the window configuration used before switching the *eshell* buffer.
  105. If no configuration has been stored, just burry the *eshell* buffer."
  106.   (if (window-configuration-p shell-toggle-pre-shell-win-conf)
  107.       (progn
  108.     (set-window-configuration shell-toggle-pre-shell-win-conf)
  109.     (setq shell-toggle-pre-shell-win-conf nil)
  110.     (bury-buffer (get-buffer "*eshell*")))
  111.     (bury-buffer))
  112.   )
  113. (defun shell-toggle-buffer-goto-shell (make-cd)
  114.   "Switches other window to the *eshell* buffer.  If no *eshell* buffer exists
  115. start a new shell and switch to it in other window.  If argument MAKE-CD is
  116. non-nil, insert a /"cd DIR/" command into the shell, where DIR is the directory
  117. of the current buffer.
  118. Stores the window cofiguration before creating and/or switching window."
  119.   (setq shell-toggle-pre-shell-win-conf (current-window-configuration))
  120.   (let ((shell-buffer (get-buffer "*eshell*"))
  121.     (cd-command
  122.      ;; Find out which directory we are in (the method differs for
  123.      ;; different buffers)
  124.      (or (and make-cd 
  125.           (buffer-file-name)
  126.           (file-name-directory (buffer-file-name))
  127.           (concat "cd " (file-name-directory (buffer-file-name))))
  128.          (and make-cd
  129.           list-buffers-directory
  130.           (concat "cd " list-buffers-directory)))))
  131.     ;; Switch to an existin shell if one exists, otherwise switch to another
  132.     ;; window and start a new shell
  133.     (if shell-buffer
  134.     (switch-to-buffer-other-window shell-buffer)
  135.       (shell-toggle-buffer-switch-to-other-window)
  136.       ;; Sometimes an error is generated when I call `eshell'
  137.       ;; (it has to do with my shell-mode-hook which inserts text into the
  138.       ;; newly created shell-buffer and thats not allways a good idea).
  139.       (condition-case the-error
  140.       (eshell)
  141.     (error (switch-to-buffer "*eshell*"))))
  142.     (if (or cd-command shell-toggle-goto-eob)
  143.     (goto-char (point-max)))
  144.     (if cd-command
  145.     (progn
  146.       (insert cd-command)
  147.       (if shell-toggle-automatic-cd
  148.           (comint-send-input))
  149.       ))))
  150. (defun shell-toggle-buffer-switch-to-other-window ()
  151.   "Switches to other window.  If the current window is the only window in the
  152. current frame, create a new window and switch to it.
  153. /(This is less intrusive to the current window configuration then 
  154. `switch-buffer-other-window')"
  155.   (let ((this-window (selected-window)))
  156.     (other-window 1)
  157.     ;; If we did not switch window then we only have one window and need to
  158.     ;; create a new one.
  159.     (if (eq this-window (selected-window))
  160.     (progn
  161.       (split-window-vertically)
  162.           (other-window 1)))))
  163.     
  164. (provide 'shell-toggle)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值