Generic right-align function

為{幸葍}努か 提交于 2020-01-01 17:00:38

问题


Now that I know how to right-align columns of numeric values in Emacs, I have two issues with this solution:

  • it is hard to remember
  • it is not flexible

For instance, it doesn't work when some of the values in the first column contains a number.

And unfortunately it inserts tabs.

Now I use rectangle functions (C-xrk) quite a lot. That made me think: would it be possible to have a function that right-aligns all text in the selected rectangle?


回答1:


(defun right-justify-rectangle (start end)
  (interactive "r")
  (apply-on-rectangle (lambda (c0 c1)
                        (move-to-column c1 t)
                        (let ((start (- (point) (- c1 c0)))
                              (end (point)))
                          (when (re-search-backward "\\S-" start t)
                            (transpose-regions start (match-end 0)
                                               (match-end 0) end))))
                      start end))

To avoid Tab, customize the variable indent-tabs-mode.

Edit:

Here is a version that deals with indent-tabs-mode more reasonably:

(defun right-justify-rectangle (start end)
  (interactive "r")
  (let ((indent-tabs-mode nil))
    (apply-on-rectangle (lambda (c0 c1)
                          (move-to-column c1 t)
                          (let ((start (- (point) (- c1 c0)))
                                (end (point)))
                            (when (re-search-backward "\\S-" start t)
                              (transpose-regions start (match-end 0)
                                                 (match-end 0) end))))
                        start end))
  (when indent-tabs-mode (tabify start end)))


来源:https://stackoverflow.com/questions/10914813/generic-right-align-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!