タブ幅より長い項目を詰めたい

何かデータを出力するとき、見やすいように整形して出力するのは割と面倒である。


タブはその手間を軽減してくれるが、タブより長い項目があると表示が崩れて読みにくい。
そこで、たまに長いデータが入るような表を簡単に見やすくするために、タブ幅より長い項目を削って表示するコマンドを書いてみた。

(defun shorten-to-tab-column (&optional n)
  "タブ幅より長い項目を詰める"
  (interactive "*p")
  (when n (set-tab-columns n (selected-buffer)))
  (let* ((c (1- (tab-columns (selected-buffer))))
         (regexp (compile-regexp (format nil "\\([^\t\n]\\{~D,\\}\\)" c))))
    (save-excursion
      (goto-char (point-min))
      (while (scan-buffer regexp :tail t)
        (let ((s (abbreviate-string-column (match-string 1) c)))
          (delete-region (match-beginning 1) (match-end 1))
          (insert s))))))
(例)
1	22	333	4444	55555
666666	7777777	88888888	999999999

(shorten-to-tab-column 4)

1	22	333	444	555
666	777	888	999

C-uでタブの長さも切り替えられるようにしたので、データを見ながら調整するのには便利である。