home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / shell-comp.el < prev    next >
Encoding:
Text File  |  1990-07-22  |  6.0 KB  |  168 lines

  1. ;From utkcs2!emory!swrinde!ucsd!tut.cis.ohio-state.edu!sgtp.apple.juice.or.jp!shin Thu Jun 21 08:48:16 EDT 1990
  2. ;Article 2122 of gnu.emacs.bug:
  3. ;Path: utkcs2!emory!swrinde!ucsd!tut.cis.ohio-state.edu!sgtp.apple.juice.or.jp!shin
  4. ;>From: shin@sgtp.apple.juice.or.jp (Shinichirou Sugou)
  5. ;Newsgroups: gnu.emacs.bug
  6. ;Subject: file completion in shell-mode (final version)
  7. ;Message-ID: <9006200854.AA13102@sgtp.apple.juice.or.jp>
  8. ;Date: 20 Jun 90 08:54:41 GMT
  9. ;Sender: daemon@tut.cis.ohio-state.edu
  10. ;Distribution: gnu
  11. ;Organization: GNUs Not Usenet
  12. ;Lines: 152
  13. ;
  14. ;It seems that posting to gnu.emacs from my site fail.  I try to post to
  15. ;another one, gnu.emacs.bug.  I'll give up if this trial fails.
  16. ;
  17. ;
  18. ;Hi.  I posted file completion program a week ago, and this is the final
  19. ;version.
  20. ;
  21. ;What has changed is,
  22. ;
  23. ;(1) Fixed a bug (sorry).  In the previous program, the message
  24. ;
  25. ;        "[Complete, but not unique]"
  26. ;
  27. ;    has never appeared.
  28. ;
  29. ;(2) The messages now appeares not in the minibuffer but in the position where
  30. ;    the cursor lies.  Don't worry!  That message is automatically removed and
  31. ;    will NOT be sent to the shell process.
  32. ;
  33. ;    For example, in the following situation (assume '#' shows the cursor),
  34. ;
  35. ;        foo% ls -l ~/Makefile# [Complete, but not unique]
  36. ;
  37. ;    If you are satisfied with 'Makefile', please ignore "[Complete, but..."
  38. ;    message and type merely <CR>.  The correct contents
  39. ;
  40. ;        "ls -l ~/Makefile"
  41. ;
  42. ;    will be sent to the shell process.
  43. ;
  44. ;If you define the following 'setq' in your '.emacs' file,
  45. ;
  46. ;(setq shell-mode-hook
  47. ;      '(lambda ()
  48. ;         (define-key shell-mode-map "\C-c\C-i" 'my-shell-complete)
  49. ;         (define-key shell-mode-map "\C-c\?" 'my-shell-completion-help)))
  50. ;
  51. ;^c^i works just like ^i in the minibuffer.
  52. ;
  53. ;^c?  works just like ? in the minibuffer.
  54. ;
  55. ;Enjoy.
  56.  
  57. ;; Revised 90/6/14 final version
  58. ;; File-completion-in-shell-mode by Shinichirou Sugou 90/6/8
  59. ;;        shin%sgtp.apple.juice.or.jp@uunet.uu.net
  60. ;;
  61. ;; GNU Emacs is distributed in the hope that it will be useful,
  62. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  63. ;; accepts responsibility to anyone for the consequences of using it
  64. ;; or for whether it serves any particular purpose or works at all,
  65. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  66. ;; License for full details.
  67.  
  68. ;; Everyone is granted permission to copy, modify and redistribute
  69. ;; GNU Emacs, but only under the conditions described in the
  70. ;; GNU Emacs General Public License.   A copy of this license is
  71. ;; supposed to have been given to you along with GNU Emacs so you
  72. ;; can know your rights and responsibilities.  It should be in a
  73. ;; file named COPYING.  Among other things, the copyright notice
  74. ;; and this notice must be preserved on all copies.
  75.  
  76. (defun my-shell-complete ()
  77.   (interactive)
  78.   (let* ((beg  (save-excursion
  79.                  (re-search-backward "\\s ")
  80.                  (1+ (point))))
  81.          (end (point))
  82.          (file (file-name-nondirectory (buffer-substring beg end)))
  83.          (dir (or (file-name-directory (buffer-substring beg end)) ""))
  84.          (lpc (file-name-completion file dir))
  85.          (akin (file-name-all-completions file dir))
  86.          (echo-keystrokes 0))           ; inhibit prefix key echo
  87.     (cond ((eq lpc t)
  88.            (my-momentary-string-display " [Sole completion]" (point) ?\0 ""))
  89.           ((eq lpc nil)
  90.            (ding t)
  91.            (my-momentary-string-display " [No match]" (point) ?\0 ""))
  92.           ((and (string= lpc file) (my-member lpc akin 'equal))
  93.            (my-momentary-string-display " [Complete, but not unique]" (point) ?\0 ""))
  94.           ((string= lpc file)
  95.            (my-shell-completion-help akin))
  96.           (t
  97.            (delete-region beg end)
  98.            (insert dir lpc)))))
  99.  
  100. (defun my-member (item list &optional testf)
  101.   "Compare using TESTF predicate, or use 'eql' if TESTF is nil."
  102.   (setq testf (or testf 'eql))
  103.   (catch 'bye
  104.     (while (not (null list))
  105.       (if (funcall testf item (car list))
  106.           (throw 'bye list))
  107.       (setq list (cdr list)))
  108.     nil))
  109. (defun my-shell-completion-help (&optional akin)
  110.   (interactive)
  111.   (if (null akin)
  112.       (let* ((beg  (save-excursion
  113.                      (re-search-backward "\\s ")
  114.                      (1+ (point))))
  115.              (end (point))
  116.              (file (file-name-nondirectory (buffer-substring beg end)))
  117.              (dir (or (file-name-directory (buffer-substring beg end)) "")))
  118.         (message "Making completion list...")
  119.         (setq akin (file-name-all-completions file dir))))
  120.   (if akin
  121.       (with-output-to-temp-buffer " *Completions*"
  122.         (display-completion-list (sort akin 'string-lessp)))
  123.     (ding t)
  124.     (let ((echo-keystrokes 0))
  125.       (my-momentary-string-display " [No completion]" (point) ?\0 ""))))
  126.  
  127. (defun my-momentary-string-display (string pos &optional exit-char message) 
  128.   "Emacs original momentary-string-display but the cursor positions at the
  129. beginning of the STRING."
  130.   (or exit-char (setq exit-char ?\ ))
  131.   (let ((buffer-read-only nil)
  132.     (modified (buffer-modified-p))
  133.     (name buffer-file-name)
  134.     insert-end
  135.         cur-pos)
  136.     (unwind-protect
  137.     (progn
  138.           (goto-char pos)
  139.           ;; defeat file locking... don't try this at home, kids!
  140.           (setq buffer-file-name nil)
  141.           (setq cur-pos (point))
  142.           (insert-before-markers string)
  143.           (setq insert-end (point))
  144.           (goto-char cur-pos)
  145.       (message (or message "Type %s to continue editing.")
  146.            (single-key-description exit-char))
  147.       (let ((char (read-char)))
  148.         (or (eq char exit-char)
  149.         (setq unread-command-char char))))
  150.       (if insert-end
  151.       (save-excursion
  152.         (delete-region pos insert-end)))
  153.       (setq buffer-file-name name)
  154.       (set-buffer-modified-p modified))))
  155.  
  156. ;CAUTION:
  157. ;  (1) Reply-command of your mail system may NOT generate my address correctly.
  158. ;      Please use the following address instead.
  159. ;
  160. ;        shin%sgtp.apple.juice.or.jp@uunet.uu.net
  161. ;
  162. ;  (2) I have no relation to Apple Computer Inc. :-)
  163. ;
  164. ;-----
  165. ;  Shin'ichirou Sugou   shin%sgtp.apple.juice.or.jp@uunet.uu.net
  166.  
  167.  
  168.