代码之家  ›  专栏  ›  技术社区  ›  unutbu

在elisp中操作数字列

  •  1
  • unutbu  · 技术社区  · 14 年前

    我有如下表格的文本文件:

       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 你能就如何改进这个代码提出建议吗?

    编辑:

    1 回复  |  直到 14 年前
        1
  •  3
  •   viam0Zah    14 年前