home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / MouseAndMenuEmacs / utilities.el < prev    next >
Encoding:
Text File  |  1990-05-31  |  5.4 KB  |  156 lines

  1. ;;;; File of random useful functions used elsewhere.
  2. ;;;; Russell Ritchie, Scottish HCI Centre, <russell@uk.ac.strath.hci>.
  3. ;;;; Tue Oct 11 10:55:53 1988
  4.  
  5. (provide 'utilities)
  6.  
  7. (defun auto-save-file-name-p (filename)
  8.   "Return t if FILENAME can be yielded by make-auto-save-file-name.
  9. FILENAME should lack slashes.
  10. You can redefine this for customization.
  11. Redefined so that last # is optional so TeX temp files can be snarfed by dired."
  12.   (string-match "^#.*#?$" filename))    
  13.  
  14. (defun switch-to-existing-buffer-other-window (buffer)
  15.   "Select existing buffer BUFFER in another window."
  16.   (interactive "BSwitch to buffer in other window: ")
  17.   (switch-aux buffer))
  18.  
  19. (defun switch-aux (buffer)
  20.   (if (buffer-existsp buffer)
  21.       (let ((pop-up-windows t))
  22.     (pop-to-buffer buffer t))
  23.     (switch-aux (read-buffer "Buffer: " (other-buffer)))))
  24.  
  25. (defun buffer-existsp (name)
  26.   "Returns true if NAME is a buffer."
  27.   (get-buffer name))
  28.  
  29. (defun member (x list)
  30.   "True if X is equal to some member of LIST"
  31.   (while (and list (not (equal x (car list))))
  32.     (setq list (cdr list)))
  33.   list)
  34.  
  35. (defun append-member (item set)
  36.   "Returns [ITEM] + SET i.e. adds ITEM to SET iff (not (member ITEM SET))."
  37.   (if (member item set)
  38.       set
  39.     (append (list item) set)))
  40.  
  41. (defun append-memq (item set)
  42.   "Returns [ITEM] + SET i.e. adds ITEM to SET iff (not (memq ITEM SET))."
  43.   (if (memq item set)
  44.       set
  45.     (append (list item) set)))
  46.  
  47. (defun file-exists-path-p (file path)
  48.   "Return the full path name of first instance of FILE found on PATH."
  49.   (catch 'file
  50.     (while path
  51.       (let ((try (concat (car path) "/" file)))
  52.     (if (file-exists-p try)
  53.         (throw 'file try)))
  54.       (setq path (cdr path)))))
  55.  
  56. (defun read-existing-program-name (&optional prompt)
  57.   (let* ((name (read-string (or prompt "Which program: ")))
  58.      (program (file-exists-path-p name exec-path)))
  59.     (or program
  60.     (read-existing-program-name
  61.      (concat "Sorry, " name " doesn't exist, try another program: ")))))
  62.  
  63. (defun new-bindings (map key-binding-list)
  64.   "First arg is map to change, second is key-binding-list.
  65. Map over key-binding-list (of form '((key binding)...)) making KEY in MAP
  66. do BINDING."
  67.   (while key-binding-list
  68.     (let* ((key-binding (car key-binding-list))
  69.        (key (car key-binding))
  70.        (command (car (cdr key-binding))))
  71.       (define-key map key command)
  72.       (setq key-binding-list (cdr key-binding-list)))))
  73.  
  74. (defun lpq ()
  75.   "Check the Laserwriter queue in a typeout window."
  76.   (interactive)
  77.   (require 'typeout)
  78.   (typeout-buffer "Laserwriter Queue" "lpq" nil "-Plw"))
  79.  
  80. (defun white-spacep (string)
  81.   "Return t if STRING consists solely of white-space characters i.e. SPACE and TAB."
  82.   (if (not (string= string ""))
  83.       ;; For now at least, null strings are NOT whitespace.
  84.       (if (string-match "[ \t]*" string)
  85.       (= (match-end 0) (length string)))))
  86.  
  87. (defun skip-whitespace (&optional and-newlines)
  88.   "Skip over whitespace characters (space and tab). 
  89. Count newlines as whitespace if AND-NEWLINES is non-nil"
  90.   (skip-chars-forward (if and-newlines " \t\n" " \t")))
  91.  
  92. (defconst cursor-pause-seconds 1
  93.   "*Number of seconds to display alternate cursor (usually the mark)")
  94.   
  95. (defun indicate-region (&optional beg end pause)
  96.   "Bounce cursor to mark for cursor-pause-seconds and back again.
  97. Optional args:
  98.  BEG (start point of region to indicate, default (mark))
  99.  END (end point of region to indicate, default (point))
  100.  PAUSE (time to wait in seconds, default cursor-pause-seconds)"
  101.   (or pause (setq pause cursor-pause-seconds))
  102.   (or beg (setq beg (mark)))
  103.   (let ((point (point)))
  104.     (goto-char beg)
  105.     (sit-for pause)
  106.     (if (not end)
  107.     (goto-char point)        ; no end point, just go back to start.
  108.       (goto-char end)
  109.       (sit-for pause)
  110.       (goto-char point))))
  111.  
  112. (defun new-buffer-other-window (mode &optional suffix force)
  113.   "Create a new buffer, put it into MODE, and visit in another window.
  114. Optional 2nd argument SUFFIX is a suffix to append to file-name.
  115. If optional 3rd argument FORCE is non-nil do not use auto-mode-alist
  116. for file mode, but use MODE instead."
  117.   (let ((buffer-name
  118.      (if suffix (concat "NewFile" suffix) (generate-new-buffer "New Buffer"))))
  119.     (if suffix
  120.     ;; If we have a file suffix, visit the file to guarantee auto-saving
  121.     (find-file-other-window buffer-name)
  122.       (switch-to-buffer-other-window buffer-name))
  123.     (set-buffer buffer-name)
  124.     (if (or force
  125.         (null suffix)
  126.         (not (assoc (concat (regexp-quote suffix) "$") auto-mode-alist)))
  127.     ;; Do not override user's choices.
  128.     (funcall mode))
  129.     (if (not (memq mode '(fundamental-mode text-mode lisp-interaction-mode)))    
  130.     ;; Insert the appropriate commented mode set-up string 
  131.     (let ((mode-identification-string
  132.         (format "%s -*- %s -*-%s\n"
  133.             (or comment-start "")
  134.             (substring (symbol-name mode) 0 -5) ; Take off "-mode" 
  135.             comment-end)))
  136.       (if (not (looking-at (regexp-quote mode-identification-string)))
  137.           (insert mode-identification-string))))
  138.     (set-buffer-modified-p nil)))    ; Mark the buffer as not modified.
  139.  
  140. (defun continue-shell-subjob ()
  141.   "Continue (restart) this shell's current subjob."
  142.   (interactive)
  143.   (let ((buff-proc (get-buffer-process (current-buffer))))
  144.     (if (eq (process-status buff-proc) 'run)
  145.     ;; Assume we have a stopped shell subjob: use 'fg'.
  146.     (save-excursion
  147.       (goto-char (point-max))
  148.       (insert "fg")
  149.       (shell-send-input))
  150.       (continue-process buff-proc))))
  151.   
  152. (defun indent-defform ()
  153.   "Indent the surrounding or previous 'top-level' form."
  154.   (save-excursion
  155.     (beginning-of-defun 1)
  156.     (indent-sexp)))