org-mode 图片排版问题

想改善一下图片操作流程,希望能降低用 M-x org-toggle-inline-image 的频率,咨询下这两种操作该从哪里下手?

  1. 光标在数字上的时候,按+号增加100
  2. 当编辑了数值的时候,立即刷新下一行的图片

翻了翻org的hook列表,没找到 attr / inline image 关键字和这个需求明显相关的。

你可以试试下面我经常用的命令和快捷键,在#+ATTR_ORG行按Ctrl +实现了1的功能。2手动编辑数据立即刷新功能上可以实现,你可以试着修改扩展下。

命令夹带了一些其他功能:

  1. 鼠标定位在图片上时,按+-放大缩小当前图片,需要高版本EMACS支持。
  2. 当鼠标定位在其他位置时,正常放大缩小字体。
  3. 安装了cn-fonts的话,按照cn-fonts逻辑改变字体大小。

不需要的功能删除cond分支就行。

(defvar org-dwim-resize-factor 100)

(defun org-dwim-resize (resize-func)
  "Resize images or adjust font size based on context.

If the cursor is within an org-mode image comment, it adjusts the image width.
If the cursor is on an image, it uses the provided RESIZE-FUNC to modify the image size.
If on Windows and the 'cnfont' feature is available, it adjusts the font size.
Otherwise, it adjusts the text scale.

Parameters:
- RESIZE-FUNC: The function to use for resizing images.

Interactive:
- This function is meant to be called interactively.

Returns: Nothing."
  (interactive)
  (save-excursion
    (let ((org-at-image-comment
           (progn
             (beginning-of-line)
             (looking-at "^[ \t]*#\\+[attr_\\|caption].*?: ")))
          (sign (if (eq resize-func 'image-increase-size)
                    '+
                  '-)))
      (cond
       (org-at-image-comment
        (let* ((case-fold-search t)
               (re "^[ \t]*#\\+\[attr_\\|caption\]+.*?: +.*?:width +\\(\\S-+\\)")
               begin end width)
          (org-remove-inline-images)
          (if (not (re-search-forward re (line-end-position) t))
              (progn (end-of-line)
                     (insert (concat " :width "
                                     (number-to-string
                                      (funcall sign (car org-image-actual-width)
                                               org-dwim-resize-factor)))))
            (setq width (string-to-number (match-string 1))
                  begin (match-beginning 1)
                  end (match-end 1))
            (setf (buffer-substring begin end)
                  (number-to-string (funcall sign
                                     width org-dwim-resize-factor)))
            (org-display-subtree-inline-images t))))
       ((image-at-point-p) (funcall resize-func))
       ((and (eq system-type 'windows-nt) (featurep 'cnfont))
        (call-interactively (if (eq resize-func 'image-increase-size)
                                'cnfonts-increase-fontsize
                              'cnfonts-decrease-fontsize)))
       (t (call-interactively (if (eq resize-func 'image-increase-size)
                                  'text-scale-increase
                                'text-scale-decrease)))))))

(defun org-dwim-increase ()
  "Increase the size of images or adjust font size based on context."
  (interactive)
  (org-dwim-resize 'image-increase-size))

(defun org-dwim-decrease ()
  "Decrease the size of images or adjust font size based on context."
  (interactive)
  (org-dwim-resize 'image-decrease-size))

(defun org-image-increase-size-at-point ()
  (interactive)
  (if (image-at-point-p)
	  (image-increase-size)
	(self-insert-command 1)))

(defun org-image-decrease-size-at-point ()
  (interactive)
  (if (image-at-point-p)
	  (image-decrease-size)
	(self-insert-command 1)))

(with-eval-after-load 'org
  (bind-key "<C-wheel-up>" 'org-dwim-increase org-mode-map)
  (bind-key "<C-wheel-down>" 'org-dwim-decrease  org-mode-map)
  (bind-key "C-+" 'org-dwim-increase org-mode-map)
  (bind-key "+" 'org-image-increase-size-at-point org-mode-map)
  (bind-key "-" 'org-image-decrease-size-at-point org-mode-map)
  (bind-key "C--" 'org-dwim-decrease org-mode-map))
(bind-key "C-0" (lambda () (interactive) (text-scale-set 0)))

7 个赞

试了下很好用~

org-display-subtree-inline-images 这个函数方便也贴下吗?想看看怎么实现的

2 个赞