;;; array.el --- array editing commands for GNU Emacs
-;; Copyright (C) 1987, 2000 Free Software Foundation, Inc.
+;; Copyright (C) 1987, 2000-2014 Free Software Foundation, Inc.
-;; Author David M. Brown
-;; Maintainer: FSF
+;; Author: David M. Brown
+;; Maintainer: emacs-devel@gnu.org
;; Keywords: extensions
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs 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.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
\f
;;; Code:
-(eval-when-compile
- (defvar array-max-column)
- (defvar array-columns-per-line)
- (defvar array-buffer-column)
- (defvar array-line-length)
- (defvar array-buffer-line)
- (defvar array-lines-per-row)
- (defvar array-max-row)
- (defvar array-field-width)
- (defvar array-row)
- (defvar array-column)
- (defvar array-rows-numbered)
- (defvar array-copy-string)
- (defvar array-init-field)
- (defvar array-respect-tabs))
+(defvar array-max-column nil "Number of columns in the array.")
+(defvar array-columns-per-line nil "Number of array columns per line.")
+(defvar array-buffer-column nil "Current column number of point in the buffer.")
+(defvar array-line-length nil "Length of a line in the array.")
+(defvar array-buffer-line nil "Current line number of point in the buffer.")
+(defvar array-lines-per-row nil "Number of lines per array row.")
+(defvar array-max-row nil "Number of rows in the array.")
+(defvar array-field-width nil "Width of a field in the array.")
+(defvar array-row nil "Current array row location of point.")
+(defvar array-column nil "Current array column location of point.")
+(defvar array-rows-numbered nil "Are rows numbered in the buffer?")
+(defvar array-copy-string nil "Current field string being copied.")
+(defvar array-respect-tabs nil "Should TAB conversion be prevented?")
;;; Internal information functions.
array-column (or a-column (array-current-column))))
(defun array-update-buffer-position ()
- "Set array-buffer-line and array-buffer-column to their current values."
+ "Set `array-buffer-line' and `array-buffer-column' to their current values."
(setq array-buffer-line (current-line)
array-buffer-column (current-column)))
(interactive)
;; If there is a conflict between array-field-width and init-string, resolve it.
(let ((check t)
- (len))
+ (len)
+ init-field)
(while check
- (setq array-init-field (read-string "Initial field value: "))
- (setq len (length array-init-field))
+ (setq init-field (read-string "Initial field value: "))
+ (setq len (length init-field))
(if (/= len array-field-width)
(if (y-or-n-p (format "Change field width to %d? " len))
(progn (setq array-field-width len)
(setq check nil)))
- (setq check nil))))
- (goto-char (point-min))
- (message "Working...")
- (let ((this-row 1))
- ;; Loop through the rows.
- (while (<= this-row array-max-row)
- (if array-rows-numbered
- (insert (format "%d:\n" this-row)))
- (let ((this-column 1))
- ;; Loop through the columns.
- (while (<= this-column array-max-column)
- (insert array-init-field)
- (if (and (zerop (% this-column array-columns-per-line))
- (/= this-column array-max-column))
- (newline))
- (setq this-column (1+ this-column))))
- (setq this-row (1+ this-row))
- (newline)))
- (message "Working...done")
+ (setq check nil)))
+ (goto-char (point-min))
+ (message "Working...")
+ (let ((this-row 1))
+ ;; Loop through the rows.
+ (while (<= this-row array-max-row)
+ (if array-rows-numbered
+ (insert (format "%d:\n" this-row)))
+ (let ((this-column 1))
+ ;; Loop through the columns.
+ (while (<= this-column array-max-column)
+ (insert init-field)
+ (if (and (zerop (% this-column array-columns-per-line))
+ (/= this-column array-max-column))
+ (newline))
+ (setq this-column (1+ this-column))))
+ (setq this-row (1+ this-row))
+ (newline)))
+ (message "Working...done"))
(array-goto-cell 1 1))
(defun array-reconfigure-rows (new-columns-per-line new-rows-numbered)
"Reconfigure the state of `array-rows-numbered' and `array-columns-per-line'.
NEW-COLUMNS-PER-LINE is the desired value of `array-columns-per-line' and
NEW-ROWS-NUMBERED (a character, either ?y or ?n) is the desired value
-of array-rows-numbered."
+of `array-rows-numbered'."
(interactive "nColumns per line: \ncRows numbered? (y or n) ")
;; Check on new-columns-per-line
(let ((check t))
(let ((inhibit-quit t))
(set-buffer main-buffer)
(erase-buffer)
- (insert-buffer temp-buffer)
+ (insert-buffer-substring temp-buffer)
;; Update local variables.
(setq array-columns-per-line new-columns-per-line)
(setq array-rows-numbered new-rows-numbered)
(defun current-line ()
"Return the current buffer line at point. The first line is 0."
- (save-excursion
- (beginning-of-line)
- (count-lines (point-min) (point))))
+ (count-lines (point-min) (line-beginning-position)))
(defun move-to-column-untabify (column)
"Move to COLUMN on the current line, untabifying if necessary.
(move-to-column column)))))
(defun untabify-backward ()
- "Untabify the preceding tab."
+ "Untabify the preceding TAB."
(save-excursion
(let ((start (point)))
(backward-char 1)
;;; Array mode.
-(defvar array-mode-map nil
+(defvar array-mode-map
+ (let ((map (make-keymap)))
+ (define-key map "\M-ad" 'array-display-local-variables)
+ (define-key map "\M-am" 'array-make-template)
+ (define-key map "\M-ae" 'array-expand-rows)
+ (define-key map "\M-ar" 'array-reconfigure-rows)
+ (define-key map "\M-a=" 'array-what-position)
+ (define-key map "\M-ag" 'array-goto-cell)
+ (define-key map "\M-af" 'array-fill-rectangle)
+ (define-key map "\C-n" 'array-next-row)
+ (define-key map "\C-p" 'array-previous-row)
+ (define-key map "\C-f" 'array-forward-column)
+ (define-key map "\C-b" 'array-backward-column)
+ (define-key map "\M-n" 'array-copy-down)
+ (define-key map "\M-p" 'array-copy-up)
+ (define-key map "\M-f" 'array-copy-forward)
+ (define-key map "\M-b" 'array-copy-backward)
+ (define-key map "\M-\C-n" 'array-copy-row-down)
+ (define-key map "\M-\C-p" 'array-copy-row-up)
+ (define-key map "\M-\C-f" 'array-copy-column-forward)
+ (define-key map "\M-\C-b" 'array-copy-column-backward)
+ map)
"Keymap used in array mode.")
-(if array-mode-map
- ()
- (setq array-mode-map (make-keymap))
- ;; Bind keys.
- (define-key array-mode-map "\M-ad" 'array-display-local-variables)
- (define-key array-mode-map "\M-am" 'array-make-template)
- (define-key array-mode-map "\M-ae" 'array-expand-rows)
- (define-key array-mode-map "\M-ar" 'array-reconfigure-rows)
- (define-key array-mode-map "\M-a=" 'array-what-position)
- (define-key array-mode-map "\M-ag" 'array-goto-cell)
- (define-key array-mode-map "\M-af" 'array-fill-rectangle)
- (define-key array-mode-map "\C-n" 'array-next-row)
- (define-key array-mode-map "\C-p" 'array-previous-row)
- (define-key array-mode-map "\C-f" 'array-forward-column)
- (define-key array-mode-map "\C-b" 'array-backward-column)
- (define-key array-mode-map "\M-n" 'array-copy-down)
- (define-key array-mode-map "\M-p" 'array-copy-up)
- (define-key array-mode-map "\M-f" 'array-copy-forward)
- (define-key array-mode-map "\M-b" 'array-copy-backward)
- (define-key array-mode-map "\M-\C-n" 'array-copy-row-down)
- (define-key array-mode-map "\M-\C-p" 'array-copy-row-up)
- (define-key array-mode-map "\M-\C-f" 'array-copy-column-forward)
- (define-key array-mode-map "\M-\C-b" 'array-copy-column-backward))
(put 'array-mode 'mode-class 'special)
;;;###autoload
-(defun array-mode ()
+(define-derived-mode array-mode fundamental-mode "Array"
"Major mode for editing arrays.
Array mode is a specialized mode for editing arrays. An array is
The array MUST reside at the top of the buffer.
TABs are not respected, and may be converted into spaces at any time.
-Setting the variable 'array-respect-tabs to non-nil will prevent TAB conversion,
+Setting the variable `array-respect-tabs' to non-nil will prevent TAB conversion,
but will cause many functions to give errors if they encounter one.
Upon entering array mode, you will be prompted for the values of
\\[array-display-local-variables] Display the current values of local variables.
Entering array mode calls the function `array-mode-hook'."
-
- (interactive)
- (kill-all-local-variables)
- ;; Number of rows in the array.
- (make-local-variable 'array-max-row)
- ;; Number of columns in the array.
- (make-local-variable 'array-max-column)
- ;; Number of array columns per line.
- (make-local-variable 'array-columns-per-line)
- ;; Width of a field in the array.
- (make-local-variable 'array-field-width)
- ;; Are rows numbered in the buffer?
- (make-local-variable 'array-rows-numbered)
- ;; Length of a line in the array.
- (make-local-variable 'array-line-length)
- ;; Number of lines per array row.
- (make-local-variable 'array-lines-per-row)
- ;; Current line number of point in the buffer.
(make-local-variable 'array-buffer-line)
- ;; Current column number of point in the buffer.
(make-local-variable 'array-buffer-column)
- ;; Current array row location of point.
(make-local-variable 'array-row)
- ;; Current array column location of point.
(make-local-variable 'array-column)
- ;; Current field string being copied.
(make-local-variable 'array-copy-string)
- ;; Should TAB conversion be prevented?
- (make-local-variable 'array-respect-tabs)
- (setq array-respect-tabs nil)
- (array-init-local-variables)
- (setq major-mode 'array-mode)
- (setq mode-name "Array")
+ (set (make-local-variable 'array-respect-tabs) nil)
+ (set (make-local-variable 'array-max-row)
+ (read-number "Number of array rows: "))
+ (set (make-local-variable 'array-max-column)
+ (read-number "Number of array columns: "))
+ (set (make-local-variable 'array-columns-per-line)
+ (read-number "Array columns per line: "))
+ (set (make-local-variable 'array-field-width)
+ (read-number "Field width: "))
+ (set (make-local-variable 'array-rows-numbered)
+ (y-or-n-p "Rows numbered? "))
+ (set (make-local-variable 'array-line-length)
+ (* array-field-width array-columns-per-line))
+ (set (make-local-variable 'array-lines-per-row)
+ (+ (floor (1- array-max-column) array-columns-per-line)
+ (if array-rows-numbered 2 1)))
+ (message "")
(force-mode-line-update)
- (make-local-variable 'truncate-lines)
- (setq truncate-lines t)
- (setq overwrite-mode 'overwrite-mode-textual)
- (use-local-map array-mode-map)
- (run-mode-hooks 'array-mode-hook))
+ (set (make-local-variable 'truncate-lines) t)
+ (setq overwrite-mode 'overwrite-mode-textual))
\f
-;;; Initialization functions. These are not interactive.
-
-(defun array-init-local-variables ()
- "Initialize the variables associated with the array in this buffer."
- (array-init-max-row)
- (array-init-max-column)
- (array-init-columns-per-line)
- (array-init-field-width)
- (array-init-rows-numbered)
- (array-init-line-length)
- (array-init-lines-per-row)
- (message ""))
-
-(defun array-init-max-row (&optional arg)
- "Initialize the value of `array-max-row'."
- (setq array-max-row
- (or arg (string-to-number (read-string "Number of array rows: ")))))
-
-(defun array-init-max-column (&optional arg)
- "Initialize the value of `array-max-column'."
- (setq array-max-column
- (or arg (string-to-number (read-string "Number of array columns: ")))))
-
-(defun array-init-columns-per-line (&optional arg)
- "Initialize the value of `array-columns-per-line'."
- (setq array-columns-per-line
- (or arg (string-to-number (read-string "Array columns per line: ")))))
-
-(defun array-init-field-width (&optional arg)
- "Initialize the value of `array-field-width'."
- (setq array-field-width
- (or arg (string-to-number (read-string "Field width: ")))))
-
-(defun array-init-rows-numbered (&optional arg)
- "Initialize the value of `array-rows-numbered'."
- (setq array-rows-numbered
- (or arg (y-or-n-p "Rows numbered? "))))
-
-(defun array-init-line-length (&optional arg)
- "Initialize the value of `array-line-length'."
- (setq array-line-length
- (or arg
- (* array-field-width array-columns-per-line))))
-
-(defun array-init-lines-per-row (&optional arg)
- "Initialize the value of `array-lines-per-row'."
- (setq array-lines-per-row
- (or arg
- (+ (floor (1- array-max-column) array-columns-per-line)
- (if array-rows-numbered 2 1)))))
-
(provide 'array)
-;;; arch-tag: 0086605d-79fe-4a1a-992a-456417261f80
;;; array.el ends here