0

一直在使用 modus-vivendi 主题,一段时间以来我为所有内容添加了粗体字体。

我想为评论引入一个特殊的定制,使用正常的重量,并有一个改变评论颜色和背景之间对比度的键绑定。

已经开始编写次要模式,但有些人建议使用派生模式,作为自定义注释的适当方式。我可以使用任何示例吗?

在 elisp 手册中,我找到了定义派生模式。由于新的 customisotion 是为了评论,我推测一个密切相关的继承模式将是 prog-mode。

但是我没有关于如何成功制作派生模式的经验。

(defvar richkov-annotation-contrast 2
  "Sets the colour contrast (against background) for comments.")

(defvar richkov-annotation-chroma

  ;; Using two Association Lists, each compased of a Chroma Intensity
  ;; Key (low, mid, high) associated with an RGB Hex-Code.  Emacs
  ;; Minibuffer Command `M-x list-colors-display' lists colour names
  ;; and hex-codes.
  
  '( (dark .  ((low  . "#8300E0")     ; indigo (Blu:63% Red:37% Grn:0%)
               (mid  . "#AA33FF")     ; indigo (Blu:54% Red:36% Grn:11%)
               (high . "#C370FF")))   ; indigo (Blu:45% Red:35% Grn:20%)

     (light . ((low  . "#C16BFF")     ; indigo (Blu:46% Red:35% Grn:19%)
               (mid  . "#AA33FF")     ; indigo (Blu:54% Red:36% Grn:11%)
               (high . "#8000DB"))) ) ; indigo (Blu:63% Red:37% Grn:0%)

  "Colour contrast for comments, indigo on dark and light background.")

(defun richkov-annotation-font-weight ()
  "Use normal weight typeface for comments."
  (set-face-attribute 'font-lock-comment-face nil :weight 'normal))

(princ "Weight: ")
(princ (face-attribute 'default :weight))

(defun richkov-annotation-typeface (chroma)
  "Set the foreground colour for comments.

CHROMA  Intensity Key used for setting colour of comments ."

  (message "richkov-annotation-typeface ")
  (let* ( (colors richkov-annotation-chroma)

          (levels
       (alist-get (frame-parameter nil 'background-mode) colors)) )
    
    ;; make comment colour change buffer-local
    (face-remap-add-relative 'font-lock-comment-face
       `(:foreground ,(alist-get chroma levels)))

    (message "richkov-annotation: %s contrast" chroma)) )

(defun richkov-annotation-sweep ()
   "Cycles through the colour chroma for comments.
Colours are determined by `richkov-annotation'."
   (interactive)

   (pcase richkov-annotation-contrast
     (1
        (richkov-annotation-typeface 'low)
        (setq richkov-annotation-contrast 2))
     (2
        (richkov-annotation-typeface 'mid)
        (setq richkov-annotation-contrast 3))
     (_
        (richkov-annotation-typeface 'high)
        (setq richkov-annotation-contrast 1)) ))

(defun richkov-annotation-low-contrast ()
  (when richkov-minor-mode
    (richkov-annotation-typeface 'low)))

(defun richkov-annotation-keytrigger ()
  "Key trigger for rapid execution of richkov commands"
  (interactive)
  (global-set-key (kbd "H-;") #'richkov-annotation-sweep))

(defun richkov-annotation-tools ()
  "Aggregates annotation tools for comments."
  (richkov-annotation-font-weight)
  (richkov-annotation-low-contrast)
  (richkov-annotation-keytrigger))

;;;###autoload
(define-minor-mode richkov-minor-mode
  "Colour Brace Marks according to their depth." 
  :lighter "richkov"  ; indicator in mode-line
  
  (font-lock-remove-keywords nil richkov-font-lock)
  (set-face-attribute 'font-lock-comment-face nil
     :weight (face-attribute 'default :weight))

  (when richkov-minor-mode
    (font-lock-add-keywords nil richkov-font-lock 'append)
    (set (make-local-variable 'jit-lock-contextually) t)
    (richkov-annotation-tools))
  
  (when font-lock-mode
    (if (fboundp 'font-lock-flush)
        (font-lock-flush)
      (with-no-warnings (font-lock-fontify-buffer)) )) )

;;;###autoload
(defun richkov-minor-mode-enable ()
  "Enable `richkov-minor-mode'."
  (richkov-minor-mode 1))

;;;###autoload
(defun richkov-minor-mode-disable ()
  "Disable `richkov-minor-mode'."
  (richkov-minor-mode 0))
4

0 回答 0