`insert-line-number` - 给代码添加纯文本行号


(defvar insert-line-number-separator "|"
  "The fancy separator used for `insert-line-number'.")

(defvar insert-line-number-alias-right t
  "If ture, alias numbers to right.")

(defvar insert-line-number-prefix ""
  "The optional prefix.")

;;;###autoload
(defun insert-line-number (beg end &optional arg)
  "Insert line numbers into buffer."
  (interactive "r")
  (if (called-interactively-p 'any)
      (if (and current-prefix-arg (= current-prefix-arg 0))
          (insert-line-number (point-min) (point-max))
        (save-excursion
          (insert-line-number (progn (goto-char beg) (backward-paragraph)
                                     (unless (= (point) (point-min))
                                         (beginning-of-line 2))
                                     (point))
                              (progn (goto-char end) (forward-paragraph)
                                     (point))
                              current-prefix-arg)))
    (save-excursion
      (let* ((max (count-lines beg end))
             (line (or arg 1))
             (string (concat insert-line-number-prefix
                             "%" (unless insert-line-number-alias-right "-")
                             (number-to-string
                              (length (number-to-string (+ (1- line) max))))
                             "d" insert-line-number-separator))
             (counter 1))
        (goto-char beg)
        (while (<= counter max)
          (insert (format string line))
          (beginning-of-line 2)
          (incf line)
          (incf counter))))))

;;;###autoload
(defun strip-regular-expression-string (regular-expression)
  "Strip all string that match REGULAR-EXPRESSION in select area of buffer.
If not select any area, then strip current buffer"
  (interactive "sRegexp:")
  (let (begin
        end)
    (if mark-active
        (setq begin (region-beginning)
              end (region-end))
      (setq begin (point-min)))
    (save-excursion
      (goto-char (or end (point-max)))
      (while (and (> (point) begin)
                  (re-search-backward regular-expression nil t))
        (replace-match "" t t)))))

;;;###autoload
(defun strip-blank-lines()
  "Strip all blank lines in select area of buffer,
if not select any area, then strip all blank lines of buffer."
  (interactive)
  (strip-regular-expression-string "^[ \t]*\n")
  (message "Have strip blanks line. ^_^"))

;;;###autoload
(defun strip-line-number()
  "Strip all line number in select area of buffer,
if not select any area, then strip all line number of buffer."
  (interactive)
  (strip-regular-expression-string (concat (if insert-line-number-alias-right
                                               "^ ?[0-9]+"
                                             "^[0-9]+ ?")
                                           insert-line-number-separator))
  (message "Have strip line number. ^_^"))

用来在电子邮件里面发代码,或者用 C-u num 决定开始的行号:

这个代码片段里面用了一些不错的技巧。

1 个赞

看起来 C-x r N (rectangle-number-lines) 有着类似的功能

one
two
three

选中后(开不开 C-x SPC (rectangle-mark-mode) 都行),C-u C-x r N 1 RET %1d| RET

1|one
2|two
3|three

跟你的 insert-line-number 不同在于它需要手动设置行号数字的宽度,比如上面设置宽度为 1。

感觉 (emacs) Rectangles 的功能就那么几个,但我几乎每天都有用到,尤其是 C-x r t (string-rectangle)。

2 个赞

差不多的功能,不过我这个是自动设置宽度,然后默认选择整段(我习惯鼠标随便划一下来选中)。

然后赠送了个去除行号的功能。

嗯。如果行号的宽度一样的话,用 Rectangle Mark mode 也很容易删除它。C-x SPC (rectangle-mark-mode) 选中后 C-w (kill-region) 就行了,非常直观。