【问题标题】:Changing margin for emacs text-mode更改 emacs 文本模式的边距
【发布时间】:2014-03-10 23:02:32
【问题描述】:

我发现改变emacs中的边距而不会让事情变得有趣的唯一方法是:

(add-hook 'window-configuration-change-hook
          (lambda ()
            (set-window-margins (car (get-buffer-window-list (current-buffer) nil t)) 24 24)))

我希望仅在文本模式下调用此设置,并在我更改为其他模式时更改回来。我有点天真地尝试了这个:

(add-hook 'text-mode-hook
          (lambda ()
            (set-window-margins (car (get-buffer-window-list (current-buffer) nil t)) 24 24)))

但它不起作用。什么是正确的代码,只在文本模式下改变缓冲区的边距?

【问题讨论】:

标签: emacs elisp hook margins


【解决方案1】:

即使您可以使用set-window-margins 设置边距,但一旦您以任何方式更改窗口,它们就会丢失。更好的解决方案是设置变量left-margin-widthright-margin-width。例如:

(defun my-set-margins ()
  "Set margins in current buffer."
  (setq left-margin-width 24)
  (setq right-margin-width 24))

(add-hook 'text-mode-hook 'my-set-margins)

【讨论】:

    【解决方案2】:

    这里有一些代码可以让你的 markdown 和文本文件在 80 个字符内居中。这会自动调整所有帧的边距。

    ;; Add left and right margins, when file is markdown or text.
    (defun center-window (window) ""
      (let* ((current-extension (file-name-extension (or (buffer-file-name) "foo.unknown")))
             (max-text-width 80)
             (margin (max 0 (/ (- (window-width window) max-text-width) 2))))
        (if (and (not (string= current-extension "md"))
                 (not (string= current-extension "txt")))
            ;; Do nothing if this isn't an .md or .txt file.
            ()
          (set-window-margins window margin margin))))
    
    ;; Adjust margins of all windows.
    (defun center-windows () ""
      (walk-windows (lambda (window) (center-window window)) nil 1))
    
    ;; Listen to window changes.
    (add-hook 'window-configuration-change-hook 'center-windows)
    

    【讨论】:

      【解决方案3】:

      这样的事情怎么样?您的问题可能源于许多主要模式继承text-mode 的事实。您可以删除您的window-configuration-change-hook,也可以将您的新函数major-briggs 与它一起使用——我个人只会将text-mode-hookmajor-briggs 函数一起使用。

      (add-hook 'text-mode-hook (lambda ()
        (major-briggs)
        ;; insert additional stuff if so desired
        ))
      
      (defun major-briggs ()
        (when (eq major-mode 'text-mode)
          (set-window-margins
            (car (get-buffer-window-list (current-buffer) nil t)) 24 24) ))
      

      【讨论】:

        【解决方案4】:

        使用以下代码:

        (defvar-local my-text-width nil
          "Text area width for current buffer, or nil if no attention needed.")
        (put 'my-text-width 'safe-local-variable #'integerp)
        
        (defun my--margin-setup ()
          "Handle settings of `my-text-width'."
          (walk-windows
           (lambda (window)
             (with-current-buffer (window-buffer window)
               (let ((margin (and my-text-width
                                  (/ (max 0 (- (window-total-width)
                                               my-text-width))
                                     2))))
                 (when (or (not (equal margin left-margin-width))
                           (not (equal margin right-margin-width)))
                   (setq left-margin-width margin)
                   (setq right-margin-width margin)
                   (set-window-buffer window (current-buffer))))))))
        
        (add-hook 'window-configuration-change-hook #'my--margin-setup)
        

        您可以在文件本地或目录本地变量中设置my-text-width,Emacs 会自动设置相应的边距以获得该文本宽度。即使您调整框架大小或引入拆分,它也能正常工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-13
          相关资源
          最近更新 更多