Function: csv-interactive-args

csv-interactive-args is a natively compiled function defined in csv-mode.el.

Signature

(csv-interactive-args &optional TYPE)

Documentation

Get arg or field(s) and region interactively, offering sensible defaults.

Signal an error if the buffer is read-only. If TYPE is noarg then return a list (beg end). Otherwise, return a list (arg beg end), where arg is:
  the raw prefix argument by default;
  a single field index if TYPE is single;
  a list of field indices or index ranges if TYPE is multiple.
Field defaults to the current prefix arg; if not set, prompt user.

A field index list consists of positive or negative integers or ranges, separated by any non-integer characters. A range has the form m-n, where m and n are positive or negative integers, m < n, and n defaults to the last field index if omitted.

In transient mark mode, if the mark is not active then automatically select and highlight CSV records around point, and query user. The default field when read interactively is the current field.

Source Code

;; Defined in /nix/store/3gmjnyw9n226b7k7jamikx980vf8267p-emacs-packages-deps/share/emacs/site-lisp/elpa/csv-mode-1.27/csv-mode.el
(defun csv-interactive-args (&optional type)
  "Get arg or field(s) and region interactively, offering sensible defaults.
Signal an error if the buffer is read-only.
If TYPE is noarg then return a list (beg end).
Otherwise, return a list (arg beg end), where arg is:
  the raw prefix argument by default;
  a single field index if TYPE is single;
  a list of field indices or index ranges if TYPE is multiple.
Field defaults to the current prefix arg; if not set, prompt user.

A field index list consists of positive or negative integers or ranges,
separated by any non-integer characters.  A range has the form m-n,
where m and n are positive or negative integers, m < n, and n defaults
to the last field index if omitted.

In transient mark mode, if the mark is not active then automatically
select and highlight CSV records around point, and query user.
The default field when read interactively is the current field."
  ;; Must be run interactively to activate mark!
  (let* ((arg current-prefix-arg) (default-field 1)
	 (region
	  (if (not (use-region-p))
	      ;; Set region automatically:
	      (save-excursion
                (if arg
                    (beginning-of-line)
                  (let ((lbp (line-beginning-position)))
                    (while (re-search-backward csv-separator-regexp lbp 1)
                      ;; Move as far as possible, i.e. to beginning of line.
                      (setq default-field (1+ default-field)))))
                (if (csv-not-looking-at-record)
                    (error "Point must be within CSV records"))
		(let ((startline (point)))
		  ;; Set mark at beginning of region:
		  (while (not (or (bobp) (csv-not-looking-at-record)))
		    (forward-line -1))
		  (if (csv-not-looking-at-record) (forward-line 1))
		  ;; Skip header lines:
		  (forward-line csv-header-lines)
		  (set-mark (point))	; OK since in save-excursion
		  ;; Move point to end of region:
		  (goto-char startline)
		  (beginning-of-line)
		  (while (not (or (eobp) (csv-not-looking-at-record)))
		    (forward-line 1))
		  ;; Show mark briefly if necessary:
		  (unless (and (pos-visible-in-window-p)
			       (pos-visible-in-window-p (mark)))
		    (exchange-point-and-mark)
		    (sit-for 1)
		    (exchange-point-and-mark))
                  (when csv-confirm-region
                    (or (y-or-n-p "Region OK? ")
                        (error "Action aborted by user"))
                    (message nil))      ; clear y-or-n-p message
		  (list (region-beginning) (region-end))))
	    ;; Use region set by user:
	    (list (region-beginning) (region-end)))))
    (setq default-field (number-to-string default-field))
    (cond
     ((eq type 'multiple)
      (if arg
	  ;; Ensure that field is a list:
	  (or (consp arg)
	      (setq arg (list (prefix-numeric-value arg))))
	;; Read field interactively, ignoring non-integers:
	(setq arg
	      (mapcar
	       (lambda (x)
		 (if (string-match "-" x 1) ; not first character
		     ;; Return a range as a pair - the cdr may be nil:
		     (let ((m (substring x 0 (match-beginning 0)))
			   (n (substring x (match-end 0))))
		       (cons (car (read-from-string m))
			     (and (not (string= n ""))
				  (car (read-from-string n)))))
		   ;; Return a number as a number:
		   (car (read-from-string x))))
	       (split-string
		(read-string
		 "Fields (sequence of integers or ranges): " default-field)
		"[^-+0-9]+")))))
     ((eq type 'single)
      (if arg
	  (setq arg (prefix-numeric-value arg))
	(while (not (integerp arg))
	  (setq arg (eval-minibuffer "Field (integer): " default-field))))))
    (if (eq type 'noarg) region (cons arg region))))