LoginSignup
11
10

More than 5 years have passed since last update.

さっとEmacsのフォント・行間のサイズを変更する

Posted at

カレントバッファのフォントサイズを大きく(Ctrl+↑)、小さく(Ctrl+↓)するのはtext-scale-adjustで。

;バッファのフォントサイズを大きく
(define-key global-map [(C up)]   'text-scale-increase)
;バッファのフォントサイズを小さく
(define-key global-map [(C down)] 'text-scale-decrease)

しかし、カレントバッファだけフォントサイズが大きくなっても、その他のバッファのフォントサイズは変わらない…。
Emacs全体のフォントサイズも変更したい!と思ったのですが、いいelispが見つからず。
フレームのフォントサイズを大きく(Alt+↑)、小さく(Alt+↓)するelispを作りました。
ただ、勢い良く変更するとフレームサイズ自体が変わります…。(直したいけれど分からない)
普段elispは書かないので、おかしな所があれば御指摘などいただけると嬉しいです。
※動作確認はWindows7 / GNU Emacs24.3 のみで行なっています。

;=======================================
; 初期設定
;=======================================
(defconst FONT_FAMILY "MS ゴシック") ;; デフォルトフォント
(defconst FONT_SIZE 9)                 ;;デフォルトフォントサイズ

;=======================================
; キー定義
;=======================================
;デフォルトのフォントサイズを大きく
(define-key global-map [(M up)]   'increase-frame-font-size)
;デフォルトのフォントサイズを小さく
(define-key global-map [(M down)] 'decrease-frame-font-size)

;=======================================
; フォント
;=======================================
(set-frame-font (concat FONT_FAMILY "-" (format "%s" FONT_SIZE)))

;;=======================================
;; フレームのフォントサイズを変更する
;;;=======================================
(defun increase-frame-font-size ()
    "increase current font size"
    (interactive)
    (change-font-size t)
)

(defun decrease-frame-font-size ()
    "decrease current font size"
    (interactive)
    (change-font-size nil)
)

;;フォントサイズの変更
(defun change-font-size (toIncrease)
    "change font size."
    (interactive)
    (save-size)
    (if toIncrease 
        (setq FONT_SIZE (+ FONT_SIZE 1) )
        (setq FONT_SIZE (- FONT_SIZE 1) )
    )
    (set-frame-font (concat FONT_FAMILY "-" (format "%.1f" FONT_SIZE )))
    (message (format "font size : %d" FONT_SIZE))
    ;; フレームサイズの再設定する
    (setq col (round (/ fw (frame-char-width) ) ))
    (setq row (round (/ fh (frame-char-height) ) ))
    (set-frame-size (selected-frame) col row)
)

;;現在のフレームサイズを保存
(defun save-size ()
     ""
    (setq fw (* (frame-width) (frame-char-width)))
    (setq fh (* (frame-height) (frame-char-height)))
)

;;フレームサイズが変更されたときは保存する
(add-hook 'window-size-change-hook '(lambda ()(save-size)))

おまけ:行間を広げる(Ctrl+Alt+↑)、狭める(Ctrl+Alt+↓)。

;=======================================
; 初期設定
;=======================================
(defconst LINE_SPACING 0.2)            ;; デフォルト行間

;=======================================
; キー定義
;=======================================
(define-key global-map [(C M up)] 'increase-line-spacing);行間を広げる
(define-key global-map [(C M down)] 'decrease-line-spacing);行間を狭める

;=======================================
; 行間設定
;====================================
;;整数で指定するとピクセル数で
;;少数で指定すると行の高さに対して相対値で設定される
(setq-default line-spacing LINE_SPACING)

;=======================================
; 行間を変更する
;=======================================
(defun increase-line-spacing ()
    "increase current line spacing"
    (interactive)
    (change-line-spacing t)
)

(defun decrease-line-spacing ()
    "decrease current line spacing"
    (interactive)
    (change-line-spacing nil)
)

(defun change-line-spacing (toIncrease)
    "change line spacing size."
    (interactive )
    (if toIncrease
        (setq LINE_SPACING (+ LINE_SPACING 0.1) )
        (setq LINE_SPACING (- LINE_SPACING 0.1) )
    )
    (setq-default line-spacing LINE_SPACING)
    (message "line spacing : %.1f" LINE_SPACING )
)
11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10