dirtrack.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. ;;; dirtrack.el --- Directory Tracking by watching the prompt
  2. ;; Copyright (C) 1996, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Peter Breton <pbreton@cs.umb.edu>
  4. ;; Created: Sun Nov 17 1996
  5. ;; Keywords: processes
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Shell directory tracking by watching the prompt.
  19. ;;
  20. ;; This is yet another attempt at a directory-tracking package for
  21. ;; Emacs shell-mode. However, this package makes one strong assumption:
  22. ;; that you can customize your shell's prompt to contain the
  23. ;; current working directory. Most shells do support this, including
  24. ;; almost every type of Bourne and C shell on Unix, the native shells on
  25. ;; Windows95 (COMMAND.COM) and Windows NT (CMD.EXE), and most 3rd party
  26. ;; Windows shells. If you cannot do this, or do not wish to, this package
  27. ;; will be useless to you.
  28. ;;
  29. ;; Installation:
  30. ;;
  31. ;; 1) Set your shell's prompt to contain the current working directory.
  32. ;; You may need to consult your shell's documentation to find out how to
  33. ;; do this.
  34. ;;
  35. ;; Note that directory tracking is done by matching regular expressions,
  36. ;; therefore it is *VERY IMPORTANT* for your prompt to be easily
  37. ;; distinguishable from other output. If your prompt regexp is too general,
  38. ;; you will see error messages from the dirtrack filter as it attempts to cd
  39. ;; to non-existent directories.
  40. ;;
  41. ;; 2) Set the variable `dirtrack-list' to an appropriate value. This
  42. ;; should be a list of two elements: the first is a regular expression
  43. ;; which matches your prompt up to and including the pathname part.
  44. ;; The second is a number which tells which regular expression group to
  45. ;; match to extract only the pathname. If you use a multi-line prompt,
  46. ;; add 't' as a third element. Note that some of the functions in
  47. ;; 'comint.el' assume a single-line prompt (eg, comint-bol).
  48. ;;
  49. ;; Determining this information may take some experimentation. Using
  50. ;; `dirtrack-debug-mode' may help; it causes the directory-tracking
  51. ;; filter to log messages to the buffer `dirtrack-debug-buffer'.
  52. ;;
  53. ;; 3) Activate `dirtrack-mode'. You may wish to turn ordinary shell
  54. ;; tracking off by calling `shell-dirtrack-mode'.
  55. ;;
  56. ;; Examples:
  57. ;;
  58. ;; 1) On Windows NT, my prompt is set to emacs$S$P$G.
  59. ;; 'dirtrack-list' is set to (list "^emacs \\([a-zA-Z]:.*\\)>" 1)
  60. ;;
  61. ;; 2) On Solaris running bash, my prompt is set like this:
  62. ;; PS1="\w\012emacs@\h(\!) [\t]% "
  63. ;; 'dirtrack-list' is set to (list "^\\([/~].*\\)\nemacs@[^%]+% *" 1 t)
  64. ;;
  65. ;; I'd appreciate other examples from people who use this package.
  66. ;;
  67. ;; Here's one from Stephen Eglen:
  68. ;;
  69. ;; Running under tcsh:
  70. ;; (setq-default dirtrack-list '("^%E \\([^ ]+\\)" 1))
  71. ;;
  72. ;; It might be worth mentioning in your file that emacs sources start up
  73. ;; files of the form: ~/.emacs_<SHELL> where <SHELL> is the name of the
  74. ;; shell. So for example, I have the following in ~/.emacs_tcsh:
  75. ;;
  76. ;; set prompt = "%%E %~ %h% "
  77. ;;
  78. ;; This produces a prompt of the form:
  79. ;; %E /var/spool 10%
  80. ;;
  81. ;; This saves me from having to use the %E prefix in other non-emacs
  82. ;; shells.
  83. ;;
  84. ;; A final note:
  85. ;;
  86. ;; I run LOTS of shell buffers through Emacs, sometimes as different users
  87. ;; (eg, when logged in as myself, I'll run a root shell in the same Emacs).
  88. ;; If you do this, and the shell prompt contains a ~, Emacs will interpret
  89. ;; this relative to the user which owns the Emacs process, not the user
  90. ;; who owns the shell buffer. This may cause dirtrack to behave strangely
  91. ;; (typically it reports that it is unable to cd to a directory
  92. ;; with a ~ in it).
  93. ;;
  94. ;; The same behavior can occur if you use dirtrack with remote filesystems
  95. ;; (using telnet, rlogin, etc) as Emacs will be checking the local
  96. ;; filesystem, not the remote one. This problem is not specific to dirtrack,
  97. ;; but also affects file completion, etc.
  98. ;;; Code:
  99. (eval-when-compile
  100. (require 'comint)
  101. (require 'shell))
  102. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  103. ;; Customization Variables
  104. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  105. (defgroup dirtrack nil
  106. "Directory tracking by watching the prompt."
  107. :prefix "dirtrack-"
  108. :group 'shell)
  109. (defcustom dirtrack-list (list "^emacs \\([a-zA-Z]:.*\\)>" 1)
  110. "List for directory tracking.
  111. First item is a regexp that describes where to find the path in a prompt.
  112. Second is a number, the regexp group to match."
  113. :group 'dirtrack
  114. :type '(sexp (regexp :tag "Prompt Expression")
  115. (integer :tag "Regexp Group"))
  116. :version "24.1")
  117. (make-variable-buffer-local 'dirtrack-list)
  118. (defcustom dirtrack-debug nil
  119. "If non-nil, the function `dirtrack' will report debugging info."
  120. :group 'dirtrack
  121. :type 'boolean)
  122. (defcustom dirtrack-debug-buffer "*Directory Tracking Log*"
  123. "Buffer in which to write directory tracking debug information."
  124. :group 'dirtrack
  125. :type 'string)
  126. (defcustom dirtrack-directory-function
  127. (if (memq system-type '(ms-dos windows-nt cygwin))
  128. 'dirtrack-windows-directory-function
  129. 'file-name-as-directory)
  130. "Function to apply to the prompt directory for comparison purposes."
  131. :group 'dirtrack
  132. :type 'function)
  133. (defcustom dirtrack-canonicalize-function
  134. (if (memq system-type '(ms-dos windows-nt cygwin))
  135. 'downcase 'identity)
  136. "Function to apply to the default directory for comparison purposes."
  137. :group 'dirtrack
  138. :type 'function)
  139. (defcustom dirtrack-directory-change-hook nil
  140. "Hook that is called when a directory change is made."
  141. :group 'dirtrack
  142. :type 'hook)
  143. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  144. ;; Functions
  145. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  146. (defun dirtrack-windows-directory-function (dir)
  147. "Return a canonical directory for comparison purposes.
  148. Such a directory is all lowercase, has forward-slashes as delimiters,
  149. and ends with a forward slash."
  150. (file-name-as-directory (downcase (subst-char-in-string ?\\ ?/ dir))))
  151. (defun dirtrack-cygwin-directory-function (dir)
  152. "Return a canonical directory taken from a Cygwin path for comparison purposes."
  153. (if (string-match "/cygdrive/\\([A-Z]\\)\\(.*\\)" dir)
  154. (concat (match-string 1 dir) ":" (match-string 2 dir))
  155. dir))
  156. ;;;###autoload
  157. (define-minor-mode dirtrack-mode
  158. "Toggle directory tracking in shell buffers (Dirtrack mode).
  159. With a prefix argument ARG, enable Dirtrack mode if ARG is
  160. positive, and disable it otherwise. If called from Lisp, enable
  161. the mode if ARG is omitted or nil.
  162. This method requires that your shell prompt contain the current
  163. working directory at all times, and that you set the variable
  164. `dirtrack-list' to match the prompt.
  165. This is an alternative to `shell-dirtrack-mode', which works by
  166. tracking `cd' and similar commands which change the shell working
  167. directory."
  168. nil nil nil
  169. (if dirtrack-mode
  170. (add-hook 'comint-preoutput-filter-functions 'dirtrack nil t)
  171. (remove-hook 'comint-preoutput-filter-functions 'dirtrack t)))
  172. (define-obsolete-function-alias 'dirtrack-toggle 'dirtrack-mode "23.1")
  173. (define-obsolete-variable-alias 'dirtrackp 'dirtrack-mode "23.1")
  174. (define-minor-mode dirtrack-debug-mode
  175. "Toggle Dirtrack debugging.
  176. With a prefix argument ARG, enable Dirtrack debugging if ARG is
  177. positive, and disable it otherwise. If called from Lisp, enable
  178. the mode if ARG is omitted or nil."
  179. nil nil nil
  180. (if dirtrack-debug-mode
  181. (display-buffer (get-buffer-create dirtrack-debug-buffer))))
  182. (define-obsolete-function-alias 'dirtrack-debug-toggle 'dirtrack-debug-mode
  183. "23.1")
  184. (define-obsolete-variable-alias 'dirtrack-debug 'dirtrack-debug-mode "23.1")
  185. (defun dirtrack-debug-message (msg1 msg2)
  186. "Insert strings at the end of `dirtrack-debug-buffer'."
  187. (when dirtrack-debug-mode
  188. (with-current-buffer (get-buffer-create dirtrack-debug-buffer)
  189. (goto-char (point-max))
  190. (insert msg1 msg2 "\n"))))
  191. ;;;###autoload
  192. (defun dirtrack (input)
  193. "Determine the current directory from the process output for a prompt.
  194. This filter function is used by `dirtrack-mode'. It looks for
  195. the prompt specified by `dirtrack-list', and calls
  196. `shell-process-cd' if the directory seems to have changed away
  197. from `default-directory'."
  198. (when (and dirtrack-mode
  199. (not (eq (point) (point-min)))) ; there must be output
  200. (save-excursion ; What's this for? -- cyd
  201. (if (not (string-match (nth 0 dirtrack-list) input))
  202. ;; No match
  203. (dirtrack-debug-message
  204. "Input failed to match `dirtrack-list': " input)
  205. (let ((prompt-path (match-string (nth 1 dirtrack-list) input))
  206. temp)
  207. (cond
  208. ;; Don't do anything for empty string
  209. ((string-equal prompt-path "")
  210. (dirtrack-debug-message "Prompt match gives empty string: " input))
  211. ;; If the prompt contains an absolute file name, call
  212. ;; `shell-process-cd' if the directory has changed.
  213. ((file-name-absolute-p prompt-path)
  214. ;; Transform prompts into canonical forms
  215. (let ((orig-prompt-path (funcall dirtrack-directory-function
  216. prompt-path))
  217. (current-dir (funcall dirtrack-canonicalize-function
  218. default-directory)))
  219. (setq prompt-path (shell-prefixed-directory-name orig-prompt-path))
  220. ;; Compare them
  221. (if (or (string-equal current-dir prompt-path)
  222. (string-equal (expand-file-name current-dir)
  223. (expand-file-name prompt-path)))
  224. (dirtrack-debug-message "Not changing directory: " current-dir)
  225. ;; It's possible that Emacs thinks the directory
  226. ;; doesn't exist (e.g. rlogin buffers)
  227. (if (file-accessible-directory-p prompt-path)
  228. ;; `shell-process-cd' adds the prefix, so we need
  229. ;; to give it the original (un-prefixed) path.
  230. (progn
  231. (shell-process-cd orig-prompt-path)
  232. (run-hooks 'dirtrack-directory-change-hook)
  233. (dirtrack-debug-message "Changing directory to "
  234. prompt-path))
  235. (dirtrack-debug-message "Not changing to non-existent directory: "
  236. prompt-path)))))
  237. ;; If the file name is non-absolute, try and see if it
  238. ;; seems to be up or down from where we were.
  239. ((string-match "\\`\\(.*\\)\\(?:/.*\\)?\n\\(.*/\\)\\1\\(?:/.*\\)?\\'"
  240. (setq temp
  241. (concat prompt-path "\n" default-directory)))
  242. (shell-process-cd (concat (match-string 2 temp)
  243. prompt-path))
  244. (run-hooks 'dirtrack-directory-change-hook)))))))
  245. input)
  246. (provide 'dirtrack)
  247. ;;; dirtrack.el ends here