Skip to content

Instantly share code, notes, and snippets.

@jwiegley
Created June 12, 2025 22:35

Revisions

  1. jwiegley created this gist Jun 12, 2025.
    33 changes: 33 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    *Response*:
    Here's the Emacs Lisp code to set the *second column (DRAFT)* to =8000= for rows where it's empty:

    #+begin_src emacs-lisp
    (defun set-draft-column ()
    "Set empty DRAFT column to 8000 in current CSV buffer."
    (interactive)
    (goto-char (point-min))
    (forward-line 1) ; Skip header
    (while (not (eobp))
    (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
    (fields (split-string line "," nil)))
    (when (and (>= (length fields) 2) ; Ensure DRAFT exists
    (string-match-p "^\\s-*$" (nth 1 fields)))
    (setf (nth 1 fields) "8000")
    (let ((new-line (mapconcat 'identity fields ",")))
    (delete-region (point-at-bol) (point-at-eol))
    (insert new-line))))
    (forward-line 1)))
    #+end_src

    *Usage*:
    1. Open the =models.csv= buffer.
    2. Evaluate this code (e.g., with =M-x eval-buffer=).
    3. Run =M-x set-draft-column= to process the buffer.

    This code:
    - Skips the header row.
    - For each line, splits fields by commas (=","=) without omitting empty entries.
    - Checks if the second column (DRAFT) is empty.
    - Replaces it with =8000= and reconstructs the line.

    *Note*: This strictly follows your request to target the /second column/. If you meant the /third column (NUM_CTX)/ instead, adjust =(nth 1 fields)= to =(nth 2 fields)=.