我的文本文件包含这样的表:
Investment advisory and
related fees receivable (161,570 ) (71,739 ) (73,135 )
Net purchases of trading
investments (93,261 ) (30,701 ) (11,018 )
Other receivables 61,216 (10,352 ) (69,313 )
Restricted cash 20,658 (20,658 ) -
Other current assets (39,643 ) 14,752 64
Other non-current assets 71,896 (26,639 ) (26,330 )
由于这些是会计编号,括号内的数字表示负数。 破折号代表0或没有数字。
我希望能够标记一个矩形区域,例如上面的第三列,
调用函数(format-thousands-column
),并自动拥有
-73.135-11.018-69.313+0.064-26.330
坐在我的杀戮戒指中。
这就是我想出来的:
(defun div_by_1000 (astr)
(number-to-string
(/ (string-to-number astr) 1000.0))
)
(defun format-column-base (format-hook)
"format accounting numbers in a rectangular column. format-column puts the result
in the kill-ring"
(copy-rectangle-to-register 0 (min (mark) (point)) (max (mark) (point)) nil)
(with-temp-buffer
(insert-register 0)
(replace-regexp "[^0-9.+( \n]" "" nil (point-min) (point-max))
(goto-char (point-min))
(while (search-forward "(" nil t)
(replace-match "-" nil t)
(just-one-space)
(delete-backward-char 1)
)
(kill-new
(replace-regexp-in-string
"+-" "-" (mapconcat format-hook
(split-string (buffer-substring (point-min) (point-max))) "+")))))
(defun format-column ()
(interactive)
(format-column-base 'identity)
)
(defun format-thousands-column ()
(interactive)
(format-column-base 'div_by_1000)
)
(global-set-key "\C-c\C-f" 'format-thousands-column)
(global-set-key "\C-c\C-g" 'format-column)
虽然它似乎有用,但我怀疑这个功能编码不好。
您是否看到了更好的方式来撰写format-column-base
,或者禁止这样做,
你能就如何改进这段代码提出建议吗?
编辑:我做了一些改进;它现在可以对数字进行简单的处理,例如将每个数字除以1000.每个数字的处理也可以使用format-hook进行自定义。