在emacs中直观添加每行信息的最佳方法? [英] Best way to add per-line information visually in emacs?

查看:113
本文介绍了在emacs中直观添加每行信息的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为emacs写一个次要模式,至少它将为缓冲区中的每一行计算一个数值。我想在每一行之前整齐地显示出来。



我知道一些小的模式可以画到边缘,我知道叠加也是一个选择(这些是相关的?),但是我找不到我想要的任何地方的一个很好的例子。



基本上,我想要像linum-mode这样的行号,每次修改缓冲区时都需要更改(实际上,只有当它们的行更改时)。像每一行的字符计数器就像一个很好的例子。我想要它不会破坏linum模式,但是不要依赖它等等。

解决方案

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
;; Mx char-count-mode

(defvar char-count-p nil
当char-count-p不是nil时,叠加层存在。)
(make-variable-buffer-local'char-count-p)

(defvar char-count-this-command nil
这个局部变量设置在`post -command-hook`;而
也由`window-scroll-functions`钩子使用。)
(make-variable-buffer-local'char-count-this-command)

(defvar char-count-overlay-list nil
用于存储重叠的列表,直到它们被删除。)
(make-variable-buffer-local'char-count -overlay-list)

(defun char-count-post-command-hook()
Doc-string
(setq char-count-this-command this -command)
(character-count-function))

(defun character-count-window-scroll-functions(win _start)
Doc-string
(character-count-function))

(defun等于包含属性 - 删除覆盖(请求结束名称val)
的eq。
(when(and beg end name val)
(overlay-recenter end)
(dolist(o(overlay-in beg end))
(when(equal-includ-属性(overlay-get o name)val)
(delete-overlay o)))))

(defun character-count-function()
Doc-string for字符计数功能
(当
(和
char-count-mode
char-count-this-command
(window-live-p (get-buffer-window(current-buffer)))
(not(minibufferp))
(pos-visible-in-window-p(point)
(get-buffer-window (当前缓冲区)(选择框))t))
(remove-char-count-overlays)
(save-excursion
(let *(
counter
(selected-window(selected-window))
(window-start(window-start selected-window))
(window-end(window-end selected-window t)))
(goto-char window-end)
(catch'done
(而t $ b $) b(当计数器
(re-search-backward\window-start t))
(当(不是计数器)
(setq counter t))
let *(
(pbol(point-at-bol))
(peol(point-at-eol))
(raw-char-count(abs( - peol pbol)))
(开始列
(属性(char-to-string?\\\)
'显示
`((空格:对齐到1) )))
(color-char-count
(propertize(number-to-string raw-char-count)
'face'(:backgroundgray50:foregroundblack ))
(one-spacer
(属性(char-to-string?\\\))
'显示
`((space:width 1))) b $ b(双垫片
(特性(char-to-string?\\\))
'显示
`((space:width 2))))
(final-char-count
(cond
((和
raw-char-count 100)
(> raw-char-count 9))
(concat one-spacer colored-char-count)
((< raw-char-计数10)
(concat two-spacer color-char-count))
(t colored-char-count)))
(ov-string(concat starting-column final-char-计数两个间隔符)))
(push ov-string char-count-overlay-list)
(overlay-put(make-overlay pbol pbol)'before-string ov-string)
(when(< = pbol window-start)
(throw'done nil)))))
(setq char-count-p t)))
(setq char-count -this-command nil)))

(defun remove-char-count-overlays()
(当char-count-p
(require'cl)
(setq char-count-overlay-list
(remove-duplicates char-count-overlay-list
:test(lambda(xy)(或(null y)(equal-including-properties xy) ))
:from-e nd t))
(dolist(描述char-count-overlay-list)
(等于包含属性 - 删除覆盖(point-min)(point-max) )
(setq char-count-p nil)))

(defun turn-off-char-count-mode()
(char-count-mode -1 )

(define-minor-mode char-count-mode
将字符计数置于行开头的次模式
:init-值nil
:更轻的Char-Count
:keymap nil
:全局nil
:group nil
(cond
(char-count-mode
(setq scroll-conservatively 101)
(add-hook'post-command-hook'char-count-post-command-hook tt)
(add-hook'window-scroll-函数
'character-count-window-scroll-functions tt)
(add-hook'change-major-mode-hook'turn-off-char-count-mode nil t)
(消息Turned ON`char-count-mode`。))
(t
(remove-char-count-overlays)
(remove-ho ok'post-command-hook'char-count-post-command-hook t)
(remove-hook'window-scroll-functions
'character-count-window-scroll-functions t)
(remove-hook'change-major-mode-hook'off-char-count-mode t)
(kill-local-variable'scroll-conservatively)
(messageTurned OFF`char-count-mode`。))))

(提供'char-count)

;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;


I'm writing a minor mode for emacs which, at the very least, will calculate a numeric value for each line in a buffer. I want to display this visually, preferable neatly before each line.

I know some minor modes draw to the fringe, and I know overlays are an option too (are these related?), but I can't find a good example of what I want anywhere.

Basically, I want to have something like the line numbers from linum-mode, but they will need to change every time the buffer is modified (actually, only whenever the line they're on changes). Something like a character counter for each line would be a good example. And I'd like it to not break linum-mode, but not depend on it, etc, if possible.

解决方案

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; M-x char-count-mode

(defvar char-count-p nil
"When `char-count-p` is non-`nil`, the overlays are present.")
(make-variable-buffer-local 'char-count-p)

(defvar char-count-this-command nil
"This local variable is set within the `post-command-hook`; and,
is also used by the `window-scroll-functions` hook.")
(make-variable-buffer-local 'char-count-this-command)

(defvar char-count-overlay-list nil
"List used to store overlays until they are removed.")
(make-variable-buffer-local 'char-count-overlay-list)

(defun char-count-post-command-hook ()
"Doc-string."
  (setq char-count-this-command this-command)
  (character-count-function))

(defun character-count-window-scroll-functions (win _start)
"Doc-string."
  (character-count-function))

(defun equal-including-properties--remove-overlays (beg end name val)
  "Remove the overlays using `equal`, instead of `eq`."
  (when (and beg end name val)
    (overlay-recenter end)
    (dolist (o (overlays-in beg end))
      (when (equal-including-properties (overlay-get o name) val)
        (delete-overlay o)))))

(defun character-count-function ()
"Doc-string for the character-count-function."
  (when
      (and
        char-count-mode
        char-count-this-command
        (window-live-p (get-buffer-window (current-buffer)))
        (not (minibufferp))
        (pos-visible-in-window-p (point)
          (get-buffer-window (current-buffer) (selected-frame)) t) )
    (remove-char-count-overlays)
    (save-excursion
      (let* (
          counter
          (selected-window (selected-window))
          (window-start (window-start selected-window))
          (window-end (window-end selected-window t)) )
        (goto-char window-end)
        (catch 'done
          (while t
            (when counter
              (re-search-backward "\n" window-start t))
            (when (not counter)
              (setq counter t))
          (let* (
              (pbol (point-at-bol))
              (peol (point-at-eol))
              (raw-char-count (abs (- peol pbol)))
              (starting-column
                (propertize (char-to-string ?\uE001)
                  'display
                  `((space :align-to 1) (space :width 0))))
              (colored-char-count
                (propertize (number-to-string raw-char-count)
                  'face '(:background "gray50" :foreground "black")))
              (one-spacer
                (propertize (char-to-string ?\uE001)
                  'display
                  `((space :width 1))))
              (two-spacers
                (propertize (char-to-string ?\uE001)
                  'display
                  `((space :width 2))))
              (final-char-count
                (cond
                  ((and
                      (< raw-char-count 100)
                      (> raw-char-count 9))
                    (concat one-spacer colored-char-count))
                  ((< raw-char-count 10)
                    (concat two-spacers colored-char-count))
                  (t colored-char-count)))
              (ov-string (concat starting-column final-char-count two-spacers)) )
            (push ov-string char-count-overlay-list)
            (overlay-put (make-overlay pbol pbol) 'before-string ov-string) 
            (when (<= pbol window-start)
              (throw 'done nil)) )))
        (setq char-count-p t)))
     (setq char-count-this-command nil) ))

(defun remove-char-count-overlays ()
  (when char-count-p
    (require 'cl)
    (setq char-count-overlay-list
      (remove-duplicates char-count-overlay-list
        :test (lambda (x y) (or (null y) (equal-including-properties x y)))
        :from-end t))
    (dolist (description char-count-overlay-list)
      (equal-including-properties--remove-overlays (point-min) (point-max) 'before-string description))
    (setq char-count-p nil) ))

(defun turn-off-char-count-mode ()
  (char-count-mode -1))

(define-minor-mode char-count-mode
"A minor-mode that places the character count at the beginning of the line."
  :init-value nil
  :lighter " Char-Count"
  :keymap nil
  :global nil
  :group nil
  (cond
    (char-count-mode
      (setq scroll-conservatively 101)
      (add-hook 'post-command-hook 'char-count-post-command-hook t t)
      (add-hook 'window-scroll-functions
        'character-count-window-scroll-functions t t)
      (add-hook 'change-major-mode-hook 'turn-off-char-count-mode nil t)
      (message "Turned ON `char-count-mode`."))
    (t
      (remove-char-count-overlays)
      (remove-hook 'post-command-hook 'char-count-post-command-hook t)
      (remove-hook 'window-scroll-functions
        'character-count-window-scroll-functions t)
      (remove-hook 'change-major-mode-hook 'turn-off-char-count-mode t)
      (kill-local-variable 'scroll-conservatively)
      (message "Turned OFF `char-count-mode`.") )))

(provide 'char-count)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

这篇关于在emacs中直观添加每行信息的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆