;;; collection of oddments used by some of my emacs code ;;; Time-stamp: <2002-01-24 13:56:20 jcgs> (defvar my-tty-name nil "The name of the terminal device on which this Emacs is running; nil if this isn't yet known. Always use the function \"my-tty\" to find this out, as that will ask Unix if the name is still nil.") (defun my-tty () "Return the name of the terminal device on which this Emacs is running; asks Unix if it hasn't yet been found out." (interactive) (if (null my-tty-name) (save-window-excursion (set-buffer (get-buffer-create " *my tty name*")) (shell-command "tty" t) ; *not right* - want *login* terminal! (setq my-tty-name (first-line-of-buffer)) (kill-buffer (current-buffer)))) (if (interactive) (message "You are using %s" my-tty-name)) my-tty-name) (defun reverse-lines-in-region (a b) "Send the lines of text in the region through the Unix pipeline \"tail -r\" and put them back into place." (interactive "r") (shell-command-on-region a b "tail -r" t)) (defun remove-string (substring string) ; From: israel@BRILLIG.UMD.EDU (Bruce Israel) "Remove the first occurrence of SUBSTRING from STRING." (let ((loc (string-match substring string))) (if loc (concat (substring string 0 loc) (substring string (+ (length substring) loc))) string))) (defun find-unbalanced-parenthesis () ; From: larus@paris.Berkeley.EDU.berkeley.edu (James Larus) "Verifies that parentheses in the current Lisp buffer are balanced." (interactive) (let ((started-here (point))) (goto-char (point-min)) (while (re-search-forward "^(" nil t) (backward-char 1) (forward-sexp 1) (if (looking-at ")") (error "Extra `)'"))) (goto-char started-here) (message "All parentheses appear to be balanced."))) (defun find-file-read-only-other-window (filename) ; From: john@xanth.UUCP John Owens, Old Dominion University, Norfolk Va. "Like find-file-read-only, but does it in another window." (interactive "FFind file read-only in other window: ") (switch-to-buffer-other-window (find-file-noselect filename)) (setq buffer-read-only t)) (defun view-cleanup-backspaces () ; From: David Gudeman 27 Jul 87 "Execute cleanup-backspaces even if the buffer is read only. Leave buffer-modified-p as it was upon entry to this function." (interactive) (let ((buffer-read-only)(buf-mod (buffer-modified-p))) (cleanup-backspaces) (set-buffer-modified-p buf-mod))) (defun read-silent (prompt) ; jr@bbn.com or jr@bbnccv.uucp "Read a string with prompting but no echo." (message prompt) (let ((chr (read-char)) (ans "")) (while (not (eq chr ?\r)) (message prompt) (setq ans (concat ans (char-to-string chr))) (setq chr (read-char))) ans)) ;;; put a directory onto the list of places to look for emacs modules (defun load-from (emacs-library) "Add EMACS-LIBRARY to the head of load-path" (setq load-path (cons (expand-file-name emacs-library) (delequal emacs-library load-path)))) (defun file-base-name (path-name) "Return the base name of PATHNAME, that is, from after the last slash to before the last dot." (let ((file-name (file-name-nondirectory path-name))) (substring file-name 0 (string-match "\\." file-name)))) (defun first-readable-file (file-name-list) "Return (expanded) the first file name in FILE-NAME-LIST that names a readable file." (if (null file-name-list) nil (let ((first-file-name (expand-file-name (car file-name-list)))) (if (file-readable-p first-file-name) first-file-name (first-readable-file (cdr file-name-list)))))) (defun purge (directory) "Purge backup files, etcetera, in DIRECTORY and any sub-directories of it." (interactive "DDirectory to purge: ") (shell-command (concat "find " directory " \( -name core -o -name a.out -o -name '*~' -o -name '#*' \)" " -exec rm -f {} \;"))) (defun find-alternate-file (filename &optional prefix) ; From: Dale Worley, Cullinet Software ARPA: cullvax!drw@eddie.mit.edu "Find file FILENAME, select its buffer, kill previous buffer. If the current buffer now contains an empty file that you just visited \(presumably by mistake), use this command to visit the file you really want. If second argument is non-nil (or prefix argument is given interactively), the current filename is appended to FILENAME (with a / if necessary) before it is selected. If you visited a file but gave the wrong directory, use this command with a prefix argument to visit the right file, but you only have to give the correct directory" (interactive "FFind alternate file: P") (and (buffer-modified-p) (not buffer-read-only) (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? " (buffer-name)))) (error "Aborted")) (if prefix (progn (if (not (string-equal (substring filename -1) "/")) (setq filename (concat filename "/"))) (setq filename (concat filename (file-name-nondirectory buffer-file-name))))) (let ((obuf (current-buffer)) (ofile buffer-file-name) (oname (buffer-name))) (rename-buffer " **lose**") (setq buffer-file-name nil) (unwind-protect (progn (unlock-buffer) (find-file filename)) (cond ((eq obuf (current-buffer)) (setq buffer-file-name ofile) (lock-buffer) (rename-buffer oname)))) (kill-buffer obuf))) (defun space-report () "Perform a garbage collection, and report how much space is used." (interactive) (with-output-to-temp-buffer "*Help*" (let ((space-details (garbage-collect))) (princ (format "%d conses\n" (car (nth 0 space-details)))) (princ (format "%d symbols\n" (car (nth 1 space-details)))) (princ (format "%d markers\n" (car (nth 2 space-details)))) (princ (format "%d string characters\n" (nth 3 space-details))) (princ (format "%d vector slots\n" (nth 4 space-details))))) nil) (defun walk-windows-for-balancing (proc &optional no-mini) ; Ashwin Ram (Ram-Ashwin@cs.yale.edu) "Applies PROC to each visible window (after selecting it, for convenience). Optional arg NO-MINI non-nil means don't apply PROC to the minibuffer even if it is active." (let ((start (selected-window)) (current (next-window (selected-window) no-mini))) (funcall proc start) (while (not (eq current start)) (select-window current) (funcall proc current) (setq current (next-window current no-mini))) (select-window start))) (setq completion-ignored-extensions (cons "fasl" completion-ignored-extensions)) (setq next-line-add-newlines nil find-file-existing-other-name (if (eq system-type 'Windows-NT) nil t)) (defun buses () "Display the bus timetable" (interactive) (switch-to-buffer-other-window (get-buffer-create "*Buses*")) (erase-buffer) (insert-file-contents "~/buses.txt")) (add-hook 'find-file-hooks 'auto-insert) (setq auto-insert-directory "~/inserts/") ; end of misc.el