home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / prim / register.el < prev    next >
Encoding:
Text File  |  1995-07-24  |  9.5 KB  |  263 lines

  1. ;;; register.el --- register commands for XEmacs.
  2.  
  3. ;; Copyright (C) 1985, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: internal
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Synched up with: FSF 19.28.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; This package of functions emulates and somewhat extends the venerable
  29. ;; TECO's `register' feature, which permits you to save various useful
  30. ;; pieces of buffer state to named variables.  The entry points are
  31. ;; documented in the XEmacs Reference Manual.
  32.  
  33. ;;; Code:
  34.  
  35. (defvar register-alist nil
  36.   "Alist of elements (NAME . CONTENTS), one for each Emacs register.
  37. NAME is a character (a number).  CONTENTS is a string, number,
  38. frame configuration, mark or list.
  39. A list of strings represents a rectangle.
  40. A list of the form (file . NAME) represents the file named NAME.")
  41.  
  42. (defun get-register (char)
  43.   "Return contents of Emacs register named CHAR, or nil if none."
  44.   (cdr (assq char register-alist)))
  45.  
  46. (defun set-register (char value)
  47.   "Set contents of Emacs register named CHAR to VALUE.  Returns VALUE.
  48. See the documentation of the variable `register-alist' for possible VALUEs."
  49.   (let ((aelt (assq char register-alist)))
  50.     (if aelt
  51.     (setcdr aelt value)
  52.       (setq aelt (cons char value))
  53.       (setq register-alist (cons aelt register-alist)))
  54.     value))
  55.  
  56. (defun point-to-register (char)
  57.   "Store current location of point in register REGISTER.
  58. Argument is a character, naming the register."
  59.   (interactive "cPoint to register: ")
  60.   (set-register char (point-marker)))
  61. ;(defun point-to-register (char &optional arg)
  62. ;  "Store current location of point in register REGISTER.
  63. ;With prefix argument, store current frame configuration.
  64. ;Use \\[jump-to-register] to go to that location or restore that configuration.
  65. ;
  66. ;Argument is a character, naming the register."
  67. ;  (interactive "cPoint to register: \nP")
  68. ;  (set-register char (if arg (current-frame-configuration) (point-marker))))
  69.  
  70. (defun window-configuration-to-register (char)
  71.   "Store the window configuration of the selected frame in register REGISTER.
  72. Use \\[jump-to-register] to restore the configuration.
  73. Argument is a character, naming the register."
  74.   (interactive "cWindow configuration to register: ")
  75.   (set-register char (current-window-configuration)))
  76.  
  77. ;(defun frame-configuration-to-register (char)
  78. ;  "Store the window configuration of all frames in register REGISTER.
  79. ;Use \\[jump-to-register] to restore the configuration.
  80. ;Argument is a character, naming the register."
  81. ;  (interactive "cFrame configuration to register: ")
  82. ;  (set-register char (current-frame-configuration)))
  83.  
  84.  
  85. (define-function 'register-to-point 'jump-to-register)
  86. (defun jump-to-register (char)
  87.   "Move point to location stored in a register.
  88. If the register contains a file name, find that file.
  89.  \(To put a file name in a register, you must use `set-register'.)
  90. If the register contains a window configuration (one frame) or a frame
  91. configuration (all frames), restore that frame or all frames accordingly.
  92. Argument is a character, naming the register."
  93. ;Optional second arg non-nil (interactively, prefix argument) says to
  94. ;delete any existing frames that the frame configuration doesn't mention.
  95. ;\(Otherwise, these frames are iconified.\)
  96.   (interactive "cJump to register: ")
  97.   (let ((val (get-register char)))
  98.     (cond
  99. ;;   ((and (fboundp 'frame-configuration-p)
  100. ;;         (frame-configuration-p val))
  101. ;;    (set-frame-configuration val (not delete)))
  102.      ((window-configuration-p val)
  103.       (set-window-configuration val))
  104.      ((markerp val)
  105.       (or (marker-buffer val)
  106.       (error "That register's buffer no longer exists"))
  107.       (switch-to-buffer (marker-buffer val))
  108.       (goto-char val))
  109.      ((and (consp val) (eq (car val) 'file))
  110.       (find-file (cdr val)))
  111.      (t
  112.       (error "Register doesn't contain a buffer position or configuration")))))
  113.  
  114. ;(defun number-to-register (arg char)
  115. ;  "Store a number in a register.
  116. ;Two args, NUMBER and REGISTER (a character, naming the register).
  117. ;If NUMBER is nil, digits in the buffer following point are read
  118. ;to get the number to store.
  119. ;Interactively, NUMBER is the prefix arg (none means nil)."
  120. ;  (interactive "P\ncNumber to register: ")
  121. ;  (set-register char 
  122. ;        (if arg
  123. ;            (prefix-numeric-value arg)
  124. ;          (if (looking-at "[0-9][0-9]*")
  125. ;              (save-excursion
  126. ;               (save-restriction
  127. ;            (narrow-to-region (point)
  128. ;                      (progn (skip-chars-forward "0-9")
  129. ;                         (point)))
  130. ;            (goto-char (point-min))
  131. ;            (read (current-buffer))))
  132. ;            0))))
  133.  
  134. ;(defun increment-register (arg char)
  135. ;  "Add NUMBER to the contents of register REGISTER.
  136. ;Interactively, NUMBER is the prefix arg (none means nil)." 
  137. ;  (interactive "p\ncNumber to register: ")
  138. ;  (or (integerp (get-register char))
  139. ;      (error "Register does not contain a number"))
  140. ;  (set-register char (+ arg (get-register char))))
  141.  
  142. (defun view-register (char)
  143.   "Display what is contained in register named REGISTER.
  144. REGISTER is a character."
  145.   (interactive "cView register: ")
  146.   (let ((val (get-register char)))
  147.     (if (null val)
  148.     (message "Register %s is empty"
  149.          (single-key-description char))
  150.       (with-output-to-temp-buffer "*Output*"
  151.     (princ (format "Register %s contains "
  152.                (single-key-description char)))
  153.     (cond 
  154.           ((integerp val)
  155.            (princ val))
  156.  
  157.           ((markerp val)
  158.            (let ((buf (marker-buffer val)))
  159.              (if (null buf)
  160.                  (princ "a marker in no buffer")
  161.                  (princ (format
  162.                           "a buffer position:\nbuff %s, position %s"
  163.                           (buffer-name (marker-buffer val))
  164.                           (marker-position val))))))
  165.  
  166.      ((window-configuration-p val)
  167.       (princ "a window configuration."))
  168.  
  169.  
  170. ;;       ((frame-configuration-p val)
  171. ;;        (princ "a frame configuration."))
  172.  
  173.      ((and (consp val) (eq (car val) 'file))
  174.       (princ "the file ")
  175.       (prin1 (cdr val))
  176.       (princ "."))
  177.  
  178.          ((consp val)
  179.           (princ "the rectangle:\n")
  180.           (while val
  181.             (princ (car val))
  182.             (terpri)
  183.             (setq val (cdr val))))
  184.  
  185.      ((stringp val)
  186.       (princ "the text:\n")
  187.       (princ val))
  188.  
  189.          (t
  190.       (princ "Garbage:\n")
  191.       (prin1 val)))))))
  192.  
  193. (defun insert-register (char &optional arg)
  194.   "Insert contents of register REG.  REG is a character.
  195. Normally puts point before and mark after the inserted text.
  196. If optional second arg is non-nil, puts mark before and point after.
  197. Interactively, second arg is non-nil if prefix arg is supplied."
  198.   (interactive "*cInsert register: \nP")
  199.   (push-mark)
  200.   (let ((val (get-register char)))
  201.     (cond ((consp val)
  202.            (insert-rectangle val))
  203.           ((stringp val)
  204.            (insert val))
  205.           ((integerp val)
  206.            (princ val (current-buffer)))
  207.           ((and (markerp val) (marker-position val))
  208.            (princ (marker-position val) (current-buffer)))
  209.           (t
  210.            (error "Register does not contain text"))))
  211.   ;; XEmacs: don't activate the region.  It's annoying.
  212.   (if (not arg) (exchange-point-and-mark t)))
  213.  
  214. (defun copy-to-register (char start end &optional delete-flag)
  215.   "Copy region into register REG.
  216. With prefix arg, delete as well.
  217. Called from program, takes four args:
  218. REG, START, END and DELETE-FLAG.
  219. START and END are buffer positions indicating what to copy."
  220.   (interactive "cCopy to register: \nr\nP")
  221.   (set-register char (buffer-substring start end))
  222.   (if delete-flag (delete-region start end)))
  223.  
  224. (defun append-to-register (char start end &optional delete-flag)
  225.   "Append region to text in register REG.
  226. With prefix arg, delete as well.
  227. Called from program, takes four args:
  228. REG, START, END and DELETE-FLAG.
  229. START and END are buffer positions indicating what to append."
  230.   (interactive "cAppend to register: \nr\nP")
  231.   (or (stringp (get-register char))
  232.       (error "Register does not contain text"))
  233.   (set-register char (concat (get-register char)
  234.                  (buffer-substring start end)))
  235.   (if delete-flag (delete-region start end)))
  236.  
  237. (defun prepend-to-register (char start end &optional delete-flag)
  238.   "Prepend region to text in register REG.
  239. With prefix arg, delete as well.
  240. Called from program, takes four args:
  241. REG, START, END and DELETE-FLAG.
  242. START and END are buffer positions indicating what to prepend."
  243.   (interactive "cPrepend to register: \nr\nP")
  244.   (or (stringp (get-register char))
  245.       (error "Register does not contain text"))
  246.   (set-register char (concat (buffer-substring start end)
  247.                  (get-register char)))
  248.   (if delete-flag (delete-region start end)))
  249.  
  250. (defun copy-rectangle-to-register (char start end &optional delete-flag)
  251.   "Copy rectangular region into register REG.
  252. With prefix arg, delete as well.
  253. Called from program, takes four args:
  254. REG, START, END and DELETE-FLAG.
  255. START and END are buffer positions giving two corners of rectangle."
  256.   (interactive "cCopy rectangle to register: \nr\nP")
  257.   (set-register char
  258.         (if delete-flag
  259.             (delete-extract-rectangle start end)
  260.           (extract-rectangle start end))))
  261.  
  262. ;;; register.el ends here
  263.