home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / electric / electric.el < prev    next >
Encoding:
Text File  |  1993-03-14  |  7.3 KB  |  199 lines

  1. ;; electric -- Window maker and Command loop for `electric' modes.
  2. ;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
  3. ;; Principal author K. Shane Hartman
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  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 2, or (at your option)
  10. ;; any later version.
  11.  
  12. ;; GNU Emacs 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. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  19. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  
  22. (provide 'electric)                           ; zaaaaaaap
  23.  
  24. ;; perhaps this should be in subr.el...
  25. (defun shrink-window-if-larger-than-buffer (window)
  26.   (if (not (and (one-window-p)
  27.         (eq (selected-window) window)))
  28.       (save-excursion
  29.     (set-buffer (window-buffer window))
  30.     (let ((w (selected-window))    ;save-window-excursion can't win
  31.           (buffer-file-name buffer-file-name)
  32.           (p (point))
  33.           (n 0)
  34.           (window-min-height 0)
  35.           (buffer-read-only nil)
  36.           (modified (buffer-modified-p))
  37.           (buffer (current-buffer)))
  38.       (unwind-protect
  39.           (progn
  40.         (select-window window)
  41.         (goto-char (point-min))
  42.         (while (pos-visible-in-window-p (point-max))
  43.           ;; defeat file locking... don't try this at home, kids!
  44.           (setq buffer-file-name nil)
  45.           (insert ?\n) (setq n (1+ n)))
  46.         (if (> n 0) (shrink-window (1- n))))
  47.         (delete-region (point-min) (point))
  48.         (set-buffer-modified-p modified)
  49.         (goto-char p)
  50.         (select-window w)
  51.         ;; Make sure we unbind buffer-read-only
  52.         ;; with the proper current buffer.
  53.         (set-buffer buffer))))))
  54.       
  55. ;; This loop is the guts for non-standard modes which retain control
  56. ;; until some event occurs.  It is a `do-forever', the only way out is to
  57. ;; throw.  It assumes that you have set up the keymap, window, and
  58. ;; everything else: all it does is read commands and execute them -
  59. ;; providing error messages should one occur (if there is no loop
  60. ;; function - which see).  The required argument is a tag which should
  61. ;; expect a value of nil if the user decides to punt. The
  62. ;; second argument is a prompt string (defaults to "->").  Given third
  63. ;; argument non-nil, it INHIBITS quitting unless the user types C-g at
  64. ;; toplevel.  This is so user can do things like C-u C-g and not get
  65. ;; thrown out.  Fourth argument, if non-nil, should be a function of two
  66. ;; arguments which is called after every command is executed.  The fifth
  67. ;; argument, if provided, is the state variable for the function.  If the
  68. ;; loop-function gets an error, the loop will abort WITHOUT throwing
  69. ;; (moral: use unwind-protect around call to this function for any
  70. ;; critical stuff).  The second argument for the loop function is the
  71. ;; conditions for any error that occurred or nil if none.
  72.  
  73. (defun Electric-command-loop (return-tag
  74.                   &optional prompt inhibit-quit
  75.                     loop-function loop-state)
  76.   (if (not prompt) (setq prompt "->"))
  77.   (let (cmd events err)
  78.     (while t
  79.       (setq events (read-key-sequence
  80.             (if (stringp prompt) prompt (funcall prompt))))
  81.       (or prefix-arg (setq last-command this-command))
  82.       (setq last-command-event (aref events (1- (length events)))
  83.         current-mouse-event
  84.           (and (or (button-press-event-p last-command-event)
  85.                (button-release-event-p last-command-event)
  86.                (menu-event-p last-command-event))
  87.            last-command-event)
  88.         this-command (if (menu-event-p last-command-event)
  89.                  last-command-event
  90.                (key-binding events))
  91.         cmd this-command)
  92.       (if (or (prog1 quit-flag (setq quit-flag nil))
  93.           (eq (event-to-character last-input-event) interrupt-char))
  94.       (progn (setq unread-command-event nil
  95.                prefix-arg nil)
  96.          ;; If it wasn't cancelling a prefix character, then quit.
  97.          (if (or (= (length (this-command-keys)) 1)
  98.              (not inhibit-quit)) ; safety
  99.              (progn (ding)
  100.                 (message "Quit")
  101.                 (throw return-tag nil))
  102.            (setq cmd nil))))
  103.       (setq current-prefix-arg prefix-arg)
  104.       (if cmd
  105.       (condition-case conditions
  106.           (progn (if (eventp cmd)
  107.              (progn
  108.                (let ((b (current-buffer)))
  109.                  (dispatch-event cmd)
  110.                  (if (not (eq b (current-buffer)))
  111.                  (throw return-tag (current-buffer)))))
  112.                (command-execute cmd))
  113.              (if (or (prog1 quit-flag (setq quit-flag nil))
  114.                  (eq (event-to-character last-input-event)
  115.                  interrupt-char))
  116.              (progn (setq unread-command-event nil)
  117.                 (if (not inhibit-quit)
  118.                     (progn (ding)
  119.                        (message "Quit")
  120.                        (throw return-tag nil))
  121.                   (ding)))))
  122.         (buffer-read-only (if loop-function
  123.                   (setq err conditions)
  124.                 (ding)
  125.                 (message "Buffer is read-only")
  126.                 (sit-for 2)))
  127.         (beginning-of-buffer (if loop-function
  128.                      (setq err conditions)
  129.                    (ding)
  130.                    (message "Beginning of Buffer")
  131.                    (sit-for 2)))
  132.         (end-of-buffer (if loop-function
  133.                    (setq err conditions)
  134.                  (ding)
  135.                  (message "End of Buffer")
  136.                  (sit-for 2)))
  137.         (error (if loop-function
  138.                (setq err conditions)
  139.              (ding)
  140.              (message "Error: %s"
  141.                   (if (eq (car conditions) 'error)
  142.                   (car (cdr conditions))
  143.                 (prin1-to-string conditions)))
  144.              (sit-for 2))))
  145.     (ding))
  146.       (if loop-function (funcall loop-function loop-state err))))
  147.   (ding)
  148.   (throw return-tag nil))
  149.  
  150. ;; This function is like pop-to-buffer, sort of. 
  151. ;; The algorithm is
  152. ;; If there is a window displaying buffer
  153. ;;     Select it
  154. ;; Else if there is only one window
  155. ;;     Split it, selecting the window on the bottom with height being
  156. ;;     the lesser of max-height (if non-nil) and the number of lines in
  157. ;;      the buffer to be displayed subject to window-min-height constraint.
  158. ;; Else
  159. ;;     Switch to buffer in the current window.
  160. ;;
  161. ;; Then if max-height is nil, and not all of the lines in the buffer
  162. ;; are displayed, grab the whole screen.
  163. ;;
  164. ;; Returns selected window on buffer positioned at point-min.
  165.  
  166. (defun Electric-pop-up-window (buffer &optional max-height)
  167.   (let* ((win (or (get-buffer-window buffer) (selected-window)))
  168.      (buf (get-buffer buffer))
  169.      (one-window (one-window-p t))
  170.      (pop-up-windows t)
  171.      (target-height)
  172.      (lines))
  173.     (if (not buf)
  174.     (error "Buffer %s does not exist" buffer)
  175.       (save-excursion
  176.     (set-buffer buf)
  177.     (setq lines (count-lines (point-min) (point-max)))
  178.     (setq target-height
  179.           (min (max (if max-height (min max-height (1+ lines)) (1+ lines))
  180.             window-min-height)
  181.            (save-window-excursion
  182.              (delete-other-windows)
  183.              (1- (window-height (selected-window)))))))
  184.       (cond ((and (eq (window-buffer win) buf)) (select-window win))
  185.         (one-window
  186.          (split-window)
  187.          (goto-char (window-start win))
  188.          (switch-to-buffer buffer)
  189.          (setq win (selected-window))
  190.          (enlarge-window (- target-height (window-height win))))
  191.         (t
  192.          (switch-to-buffer buf)))
  193.       (if (and (not max-height)
  194.            (> target-height (window-height (selected-window))))
  195.       (progn (goto-char (window-start win))
  196.          (enlarge-window (- target-height (window-height win)))))
  197.       (goto-char (point-min))
  198.       win)))
  199.