Emacs——修改 `fill-column-indicator.el` 以在空白处显示

标签 emacs elisp

我正在寻找一些帮助,请修改 fill-column-indicator.el由 Alp Aker 设计,以便垂直线在空白处可见(无论空白模式是否处于事件状态)。

这里是该库的 Github 存储库的链接:

https://github.com/alpaker/Fill-Column-Indicator/blob/master/fill-column-indicator.el

目前,垂直线在空白处不可见。

以下代码与 fill-column-indicator.el 组合时, 创建一条跟踪当前光标列位置的垂直线。我检查了 fill-column-indicator.el 的代码,但是,我无法找到在存在空格时阻止垂直线出现的代码部分。

(defun fci-mode-current-column ()
  (setq fill-column (current-column))
  (setq cursor-type '(hbar . 2))
  (fci-mode t))

(add-hook 'post-command-hook 'fci-mode-current-column)

Example
(来源:lawlist.com)

最佳答案

2014 年 5 月 1 日:创建了第一份工作草稿。

2014 年 5 月 2 日:修改了几个 fci-mode 函数,并且包含了之前草案的部分内容。

2014 年 5 月 3 日:修复了选项卡右侧列的突出显示。合并了一个准后命令 Hook ,以精确控制何时使用垂直标尺重绘缓冲区,并删除了 fci-mode 之前使用的 Hook 。

2014 年 5 月 4 日:使用 (setq whitespace-style '(face space-mark tab-mark newline -标记))tab-marknewline-markforeground 被突出显示。然而,背景没有突出显示,因为每个标记跨越一列以上(但的宽度只等于一)。


;;;;;;;;;;;;;;;;;;;;;;;;; LAWLIST MODIFICATIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; http://stackoverflow.com/a/23418459/2112489
;;
;; In order to use these modifications to enable vertical highlighting of the
;; current column, the library `fill-column-indicator.el` by Alp Aker is needed.
;;   https://github.com/alpaker/Fill-Column-Indicator
;; The code set forth hereinbelow is intended to replace functions or variables
;; within the aforementioned library that contain the same names.  There are
;; also a few new functions and variables below.

(define-minor-mode fci-mode
  :group 'fci-mode
  :lighter " fci"
  :global nil
  :init-value nil
  (if fci-mode
      ;; Enabling.
      (condition-case error
          (progn
            (fci-check-user-options)
            (fci-process-display-table)
            (fci-set-local-vars)
            (fci-get-frame-dimens)
            ;; (dolist (hook fci-hook-assignments)
            ;;   (add-hook (car hook) (nth 1 hook) nil (nth 2 hook)))
            (setq fci-column (current-column))
            (setq fci-tab-width tab-width)
            (setq fci-limit
              (if fci-newline
                (1+ (- fci-column (length fci-saved-eol)))
                fci-column))
            (fci-make-overlay-strings)
            (fci-update-all-windows t)
            (if linum-mode
            (linum-update-current)))
        (error
         (fci-mode 0)
         (signal (car error) (cdr error))))
    ;; Disabling.
    (fci-restore-display-table)
    (fci-restore-local-vars)
    (dolist (hook fci-hook-assignments)
      (remove-hook (car hook) (nth 1 hook) (nth 2 hook)))
    (fci-delete-overlays-buffer)
    (dolist (var fci-internal-vars)
      (set var nil))))

(defvar my-column-overlay nil
  "The overlays used in this buffer.")
(make-variable-buffer-local 'my-column-overlay)

(defvar my-cursor-point nil
"Point used to prevent the formation of a cursor overlay.
It must be set within the function `fci-redraw-region`.")
(make-variable-buffer-local 'my-cursor-point)

(defun fci-put-overlays-region (start end)
"Place overlays displaying the fill-column rule between START and END."
  (let* (my-last-column fci-overlay my-overlay-beg my-overlay-end)
    (setq cursor-type 'hollow)
    (goto-char end)
    (while (re-search-backward "\n" start t)
      (setq my-last-column (current-column))
      (setq fci-overlay (make-overlay (match-beginning 0) (match-beginning 0)))
      (overlay-put fci-overlay 'fci t)
      (cond
       ((< my-last-column fci-limit)
        (overlay-put fci-overlay 'after-string fci-pre-limit-string))
       ((> my-last-column fci-limit)
        (overlay-put fci-overlay 'after-string fci-post-limit-string))
       (t
        (overlay-put fci-overlay 'after-string fci-at-limit-string)))
      (setq my-overlay-beg (progn (move-to-column fci-column) (point)))
      (setq my-overlay-end (+ 1 my-overlay-beg))
      (setq my-column-overlay (make-overlay my-overlay-beg my-overlay-end ))
      (cond
        ;; text, excluding tabs
        ((and
            (not (save-excursion (move-to-column (+ 1 fci-column))
              (eq (preceding-char) 9)))
            (not (eq my-cursor-point my-overlay-beg))
            (< fci-column my-last-column))
          (overlay-put my-column-overlay 'face
            '(:background "DarkRed") ) )
        ;; tab with text to the right
        ((and
            (not (bobp)) ;; do NOT try to go beyond the beginning of the buffer
            (not (not (save-excursion (move-to-column fci-column)
              (backward-char 1) (eq (char-after (point)) 9))))
            (not (save-excursion (move-to-column (+ 1 fci-column))
              (eq (char-after (point)) 9)))
            (save-excursion (move-to-column fci-column)
              (eq (char-after (point)) 9))
            (not (eq my-cursor-point my-overlay-beg))
            (< fci-column my-last-column))
          (overlay-put my-column-overlay 'face
            '(:foreground "Red" :weight bold) ) )
        ;; tab with text to the left
        ((and
            (not (bobp)) ;; do NOT try to go beyond the beginning of the buffer
            (not (save-excursion (move-to-column fci-column)
              (backward-char 1) (eq (char-after (point)) 9)))
            (save-excursion (move-to-column fci-column)
              (eq (char-after (point)) 9))
            (not (eq my-cursor-point my-overlay-beg))
            (< fci-column my-last-column))
          (overlay-put my-column-overlay 'face
            '(:foreground "Red" :weight bold) ) )
        ;; tab sandwiched between a tab on each side
        ((and
            (not (bobp)) ;; do NOT try to go beyond the beginning of the buffer
            (save-excursion (move-to-column fci-column)
              (eq (char-after (point)) 9))
            (not (eq
              (save-excursion (move-to-column fci-column)
                (re-search-backward "\t" (point-at-bol) t) (point))
              (save-excursion (move-to-column (+ fci-column 1))
                (re-search-backward "\t" (point-at-bol) t) (point))))
            (not (eq my-cursor-point my-overlay-beg))
            (< fci-column my-last-column))
          (overlay-put my-column-overlay 'face
            '(:foreground "Red" :weight bold) ) )
        ;; end of line
        ((= fci-column my-last-column)
          (overlay-put my-column-overlay 'face
            '(:foreground "Red" :weight bold) ) ) 
        ;; cursor
        ((and
            (eq my-cursor-point my-overlay-beg)
            (not (eq (preceding-char) 9))
            (< fci-column my-last-column))
          (overlay-put my-column-overlay 'face
            '(:weight bold) ) )) )))

(defun fci-delete-overlays-region (start end)
  "Delete overlays displaying the fill-column rule between START and END."
  (mapc #'(lambda (o) (if (overlay-get o 'fci) (delete-overlay o)))
        (overlays-in start end))
  (let ((ovs (overlays-in start end)))
    (dolist (ov ovs)
      (unless (member ov (list hl-line-overlay))
        (delete-overlay ov)))) )

(defun fci-redraw-region (start end _ignored)
  "Erase and redraw the fill-column rule between START and END."
  ;; Needed to prevent and then ultimately create a cursor overlay.
  (setq my-cursor-point (point))
  (save-match-data
    (save-excursion
      (let ((inhibit-point-motion-hooks t))
        (goto-char end)
        (setq end (line-beginning-position 2))
        (fci-delete-overlays-region start end)
        (fci-put-overlays-region start end)))))

(defvar quasi-this-command-functions '(next-line previous-line left-char right-char
  self-insert-command newline delete-backward-char delete-forward-char
  indent-for-tab-command mwheel-scroll lawlist-mwheel-scroll end-of-visual-line
  beginning-of-visual-line end-of-buffer beginning-of-buffer lawlist-forward-entity
  lawlist-backward-entity left-word right-word forward-word backward-word
  lawlist-forward-element lawlist-backward-element)
"Variable list of functions that trigger the `fci-quasi-post-command-hook`.")

(defvar fci-quasi-major-mode-inclusions '(text-mode emacs-lisp-mode perl-mode
  js-mode css-mode dired-mode lawlist-tex-mode c-mode html-mode snippet-mode)
"Variable list of major modes where the `fci-quasi-post-command-hook` operates.")

(defun fci-quasi-post-command-hook ()
  (unless (minibufferp)
    (when
      (and
        (memq major-mode fci-quasi-major-mode-inclusions)
        (memq this-command quasi-this-command-functions))
      (fci-mode 1))))

(add-hook 'post-command-hook 'fci-quasi-post-command-hook)

(add-hook 'change-major-mode-hook 'fci-quasi-post-command-hook)

(add-hook 'window-configuration-change-hook 'fci-quasi-post-command-hook)

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

Example
(来源:lawlist.com)

关于Emacs——修改 `fill-column-indicator.el` 以在空白处显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23398957/

相关文章:

file - Emacs:将缓冲区写入新文件,但保持此文件打开

emacs - emacs 最好的键盘?

emacs - 在哪里可以找到最流行的 Emacs 设置?

emacs - 如何在 emacs org-mode 中将函数限制为子树?

emacs - 在Emacs中,如何使用Tramp SSH作为其他组ID进行编辑?

emacs - 如何读取Emacs语法表中的范围?

python - 将 virtualenv 与 Anaconda 一起使用需要更改 LD_LIBRARY_PATH 但会破坏 Emacs

emacs - 如何使用 <escape>(有条件地)作为修饰键

git - Emacs:为什么 shell 命令 "git log"有效,但 "git shortlog"无效?

emacs 口齿不清 : how to add to link/hyperlink to another file just like that in *H e l p*