> On 2019-Apr-24, at 23:57, Damon Permezel <perme...@me.com> wrote:
> 
> Just noticed a bug as I was reading my post.  Just if argument-prefix is 
> applied, it will keep trying a line with no tabs (or less than 2) and not 
> advance the line.  I’m sure there are more….
> 

This version is somewhat better.  It skips comment lines and org headers lines, 
plus fixes a few bugs, while introducing an equal measure of new ones + 1.

(defun txt-to-TSV ()
  "turn a tab-separated text line into TSV"
  (interactive)
  (save-mark-and-excursion
    (save-restriction
      (let ((beg (progn (beginning-of-line) (point)))
            (end (progn (end-of-line) (point)))
            (tsv (vector))
            (prev nil))

        (narrow-to-region beg end)
        (goto-char beg)
        (when (and (not (= (following-char) ?#))
                   (not (= (following-char) ?*)))
          (setq prev beg)
          (while (< (point) end)
            (when (looking-at "\t")
              (setq tsv  (vconcat tsv (vector (buffer-substring prev (point)))))
              (setq prev (+ 1 (point))))
            (forward-word))

          (setq tsv (vconcat tsv (vector (buffer-substring prev (point))))))
        tsv
    ))))

(defun txt-to-drill (arg)
  "convert TSV to drill"
  (interactive "p")

  (let ((query (concat
                "*** Query      :drill:\n"
                "    :PROPERTIES:\n"
                "    :END:\n"))
        (answer (concat
                 "**** Answer\n")))
    (while (> arg 0)
      (setq arg (- arg 1))
      (let ((tsv (txt-to-TSV)))
        (if (> (length tsv) 1)
            (progn
              (beginning-of-line)
              (insert "\n" query (aref tsv 0))
              (insert "\n" answer (aref tsv 1))
              (let ((i 2))
                (while (< i (length tsv))
                  (insert "\n" (aref tsv i))
                  (setq i (+ 1 i))))
              (insert "\n")
              (insert "# ")
              (beginning-of-line))))
      (forward-line))))

Attachment: signature.asc
Description: Message signed with OpenPGP

Reply via email to