用于从组织表中删除所选行的宏

时间:2014-03-03 11:37:15

标签: emacs org-mode

我有一些org表,包含5列和100行。

我想用field $3="away"删除所有这些行。

以下是应该执行以下操作的功能。不幸的是,我不知道如何将使用(org)创建的org-table-to-lisp表格转换回我的缓冲区。

(defun org-table-tests ()
  "acts on balance table itself"
  (interactive)
  (save-excursion
    (unless (org-table-p)
      (error
       "You are not in an org-table."))
    (goto-char (org-table-begin))
    (let (out-tbl
          (tbl-list (org-table-to-lisp)))
      (while
          (let ((row-list (car tbl-list)))
            (cond ((and
                    (listp row-list)
                    ;; do not copy "away" (3rd column) lines containing folliwng:
                    (string-match "away" (nth 2 row-list)))
                   ;; just skip copying to new table
                   )
                  (t (setq out-tbl (cons row-list out-tbl))))
            (setq tbl-list (cdr tbl-list))))
      ;;(orgtbl-to-generic out-tbl)  <---------- QUESTION: which function to use to convert back to table in text format.
      )))

...或者使用orgtbl模式执行此类任务有更好的方法吗?

3 个答案:

答案 0 :(得分:1)

看看org-listtable-to-string。它与org-table-to-lisp相反。

另请查看this screencast以了解具体方法 我在不到一分钟的时间内找到了解决方案(基本上只是在org的代码库中为了“to table”)。

答案 1 :(得分:1)

由于您的主要问题已得到解答,现在这里的解决方案不依赖于转换为lisp并返回:

(defun org-table-tests ()
  (interactive)
  (save-excursion
    (unless (org-table-p)
      (error "You are not in an org-table."))
    (goto-char (org-table-begin))
    (while (org-table-p)
      (if (string-match "away" (save-excursion (org-table-get-field 3)))
          (delete-region (point) (progn (forward-line) (point)))
        (forward-line)))))

答案 2 :(得分:1)

如果您只想删除$ 3 ==“离开”的行,则以下命令很有用。但我不知道它为什么会起作用。

ESC M-<

M-x flush-lines

RET

^[|][^|]*[|][^|
]*[|] away [|]

RET

输入:

|    1 |    2 |    3 |    4 |    5 |
|------+------+------+------+------|
| away |   12 |   13 |   14 | away |
|   21 |   22 | away |   24 |   25 |
|   31 | away |   33 | away |   35 |

输出:

|    1 |    2 |    3 |    4 |    5 |
|------+------+------+------+------|
| away |   12 |   13 |   14 | away |
|   31 | away |   33 | away |   35 |