home *** CD-ROM | disk | FTP | other *** search
- ;;;; edit.jl -- High-level editing functions
- ;;; Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
-
- ;;; This file is part of Jade.
-
- ;;; Jade is free software; you can redistribute it and/or modify it
- ;;; under the terms of the GNU General Public License as published by
- ;;; the Free Software Foundation; either version 2, or (at your option)
- ;;; any later version.
-
- ;;; Jade is distributed in the hope that it will be useful, but
- ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
- ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ;;; GNU General Public License for more details.
-
- ;;; You should have received a copy of the GNU General Public License
- ;;; along with Jade; see the file COPYING. If not, write to
- ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
-
- (defvar word-regexp "[a-zA-Z0-9]"
- "Regular expression which defines a character in a word.")
- (defvar word-not-regexp "[^a-zA-Z0-9]|$"
- "Regular expression which defines anything that is not in a word.")
- (defvar paragraph-regexp "^ *$"
- "Regular expression which matches a paragraph-separating piece of text.")
-
- (set-buffer-variable 'word-regexp)
- (set-buffer-variable 'word-not-regexp)
- (set-buffer-variable 'paragraph-regexp)
-
- (defvar auto-mark (make-mark)
- "Mark which some commands use to track the previous cursor position.")
-
- (defun forward-word (&optional number pos)
- "(forward-word [NUMBER] [POS])
- Move to the first character after the end of this word.
- NUMBER is the number of words to move, negative values mean go backwards."
- (unless number
- (setq number 1))
- (unless pos
- (setq pos (cursor-pos)))
- (cond
- ((< number 0)
- ;; go backwards
- (while (/= number 0)
- (setq pos (prev-char pos))
- (when (looking-at word-not-regexp pos)
- ;; not in word
- (unless (setq pos (find-prev-regexp word-regexp pos))
- (error "Start of buffer")))
- ;; in middle of word
- (unless (setq pos (find-prev-regexp word-not-regexp pos))
- (error "Start of buffer"))
- (setq
- pos (find-next-regexp word-regexp pos)
- number (1+ number)))
- pos)
- (t
- ;; forwards
- (while (/= number 0)
- (when (looking-at word-not-regexp pos)
- ;; already at end of a word
- (unless (setq pos (find-next-regexp word-regexp pos))
- (error "End of buffer")))
- (unless (setq pos (find-next-regexp word-not-regexp pos))
- (error "End of buffer"))
- (setq number (1- number)))
- pos)))
-
- (defun backward-word (&optional number pos)
- "(backward-word [NUMBER] [POS])
- Basically `(forward-word -NUMBER POS)'"
- (forward-word (if number (- number) -1) pos))
-
- (defun word-start (&optional pos)
- (when (looking-at word-regexp pos)
- (unless (find-prev-regexp word-not-regexp pos)
- (error "Start of buffer"))
- (find-next-regexp word-regexp find-last-end-pos)))
-
- (defun in-word-p (&optional pos)
- "(in-word-p [POS])
- Returns t if POS is inside a word."
- (when (looking-at word-regexp pos)
- t))
-
- (defun mark-word (&optional pos)
- "(mark-word [POS])
- Marks the word at POS."
- (block-kill)
- (block-start (if pos pos (cursor-pos)))
- (block-end (forward-word pos)))
-
- (defun next-paragraph (&optional pos buf)
- "(next-paragraph [POS] [BUFFER])
- Returns the position of the start of the next paragraph."
- (unless (find-next-regexp paragraph-regexp (next-char (if pos (dup-pos pos) (cursor-pos))) buf)
- (file-end)))
-
- (defun prev-paragraph (&optional pos buf)
- "(prev-paragraph [POS] [BUFFER])
- Returns the start of the previous paragraph."
- (unless (find-prev-regexp paragraph-regexp (prev-char (if pos (dup-pos pos) (cursor-pos))) buf)
- (file-start)))
-
- (defun mark-paragraph ()
- "(mark-paragraph)
- Set the block-marks to the current paragraph."
- (block-kill)
- (let
- ((par (next-paragraph)))
- (block-end par)
- (block-start (prev-paragraph par))))
-
- (defun copy-block (&aux rc)
- "(copy-block)
- If a block is marked in the current window, return the text it contains and
- unmark the block."
- (when (blockp)
- (setq rc (funcall (if (rect-blocks-p) 'copy-rect 'copy-area) (block-start) (block-end)))
- (block-kill))
- rc)
-
- (defun cut-block (&aux rc)
- "(cut-block)
- Similar to `copy-block' except the block is cut from the buffer."
- (when (blockp)
- (setq rc (funcall (if (rect-blocks-p) 'cut-rect 'cut-area) (block-start) (block-end)))
- (block-kill))
- rc)
-
- (defun delete-block ()
- "(delete-block)
- Deletes the block marked in the current window (if one exists)."
- (when (blockp)
- (funcall (if (rect-blocks-p) 'delete-rect 'delete-area) (block-start) (block-end))
- (block-kill)))
-
- (defun insert-block (&optional pos)
- "(insert-block [POS])
- If a block is marked in the current window, copy it to position POS, then
- unmark the block."
- (when (blockp)
- (if (rect-blocks-p)
- (insert-rect (copy-rect (block-start) (block-end)) pos)
- (insert (copy-area (block-start) (block-end)) pos))
- (block-kill)))
-
- (defun toggle-rect-blocks ()
- (set-rect-blocks nil (not (rect-blocks-p))))
-
- (defvar search-regexp nil)
- (defvar replace-regexp nil)
-
- (defun search-forward (&optional unask)
- (unless unask
- (setq search-regexp (prompt "Find forwards: " search-regexp)))
- (when search-regexp
- (set-auto-mark)
- (goto (find-next-regexp search-regexp (next-char)))))
-
- (defun search-backward (&optional unask)
- (unless unask
- (setq search-regexp (prompt "Find backwards: " search-regexp)))
- (when search-regexp
- (set-auto-mark)
- (goto (find-prev-regexp search-regexp (prev-char)))))
-
- (defun simple-replace (&optional unask)
- (if (not unask)
- (setq replace-regexp (prompt "Replace with: " replace-regexp)))
- (when (and replace-regexp (replace-regexp search-regexp replace-regexp))
- (set-auto-mark)
- (goto (find-next-regexp search-regexp))))
-
- (defun upcase-area (start end &optional buf)
- "(upcase-area START-POS END-POS [BUFFER])
- Makes all alpha characters in the specified region of text upper case."
- (translate-area start end upcase-table buf))
-
- (defun downcase-area (start end &optional buf)
- "(downcase-area START-POS END-POS [BUFFER])
- Makes all alpha characters in the specified region of text lower case."
- (translate-area start end downcase-table buf))
-
- (defun upcase-block ()
- "(upcase-block)
- Makes all characters in the currently marked block upper case."
- (when (blockp)
- (upcase-area (block-start) (block-end))
- (block-kill)))
-
- (defun downcase-block ()
- "(downcase-block)
- Makes all characters in the currently marked block lower case."
- (when (blockp)
- (downcase-area (block-start) (block-end))
- (block-kill)))
-
- (defun upcase-word ()
- "(upcase-word)
- Makes the word under the cursor upper case."
- (let
- ((pos (forward-word)))
- (upcase-area (cursor-pos) pos)
- (goto pos)))
-
- (defun downcase-word ()
- "(downcase-word)
- Makes the word under the cursor lower case."
- (let
- ((pos (forward-word)))
- (downcase-area (cursor-pos) pos)
- (goto pos)))
-
- (defun mark-region ()
- "(mark-region)
- Sets the block-marks to the are between the cursor position and the auto-mark"
- (block-kill)
- (when (eq (mark-file auto-mark) (current-buffer))
- (block-kill)
- (let
- ((curs (cursor-pos)))
- (cond
- ((> curs (mark-pos auto-mark))
- (block-start (mark-pos auto-mark))
- (block-end curs))
- (t
- (block-start curs)
- (block-end (mark-pos auto-mark)))))))
-
- (defun abort-recursive-edit (&optional ret-val)
- "(abort-recursive-edit [VALUE])
- Exits the innermost recursive edit with a value of VALUE."
- (throw 'exit ret-val))
-
- (defun top-level ()
- "(top-level)
- Exit all recursive-edits."
- (throw 'top-level nil))
-