]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/nlinum/nlinum.el
Merge remote-tracking branch 'ztree/master'
[gnu-emacs-elpa] / packages / nlinum / nlinum.el
index 82e274c7a90bc295762581d636652e2075bc1585..98c9cbc7e05247f55e5b87f3f286a8a41e33a03f 100644 (file)
@@ -1,10 +1,10 @@
 ;;; nlinum.el --- Show line numbers in the margin  -*- lexical-binding: t -*-
 
-;; Copyright (C) 2012, 2014  Free Software Foundation, Inc.
+;; Copyright (C) 2012, 2014, 2015  Free Software Foundation, Inc.
 
 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
 ;; Keywords: convenience
-;; Version: 1.5
+;; Version: 1.6
 
 ;; This program is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
@@ -54,6 +54,7 @@ Linum mode is a buffer-local minor mode."
   :lighter nil ;; (" NLinum" nlinum--desc)
   (jit-lock-unregister #'nlinum--region)
   (remove-hook 'window-configuration-change-hook #'nlinum--setup-window t)
+  (remove-hook 'text-scale-mode-hook #'nlinum--setup-window t)
   (remove-hook 'after-change-functions #'nlinum--after-change t)
   (kill-local-variable 'nlinum--line-number-cache)
   (remove-overlays (point-min) (point-max) 'nlinum t)
@@ -63,6 +64,7 @@ Linum mode is a buffer-local minor mode."
     ;; FIXME: Another approach would be to make the mode permanent-local,
     ;; which might indeed be preferable.
     (add-hook 'change-major-mode-hook (lambda () (nlinum-mode -1)))
+    (add-hook 'text-scale-mode-hook #'nlinum--setup-window nil t)
     (add-hook 'window-configuration-change-hook #'nlinum--setup-window nil t)
     (add-hook 'after-change-functions #'nlinum--after-change nil t)
     (jit-lock-register #'nlinum--region t))
@@ -71,17 +73,45 @@ Linum mode is a buffer-local minor mode."
 (defun nlinum--face-height (face)
   (aref (font-info (face-font face)) 2))
 
+(defun nlinum--face-width (face)        ;New info only in Emacs>=25.
+  (let ((fi (font-info (face-font face))))
+    (when (> (length fi) 11)
+      (let ((width (aref fi 11)))
+        (if (<= width 0)
+            (aref fi 10)
+          width)))))
+
 (defun nlinum--setup-window ()
-  (let ((width (if (display-graphic-p)
-                   (ceiling
-                    ;; We'd really want to check the widths rather than the
-                    ;; heights, but it's a start.
-                    (/ (* nlinum--width 1.0
-                          (nlinum--face-height 'linum))
-                       (frame-char-height)))
-                 nlinum--width)))
-    (set-window-margins nil (if nlinum-mode width)
-                        (cdr (window-margins)))))
+  ;; FIXME: The interaction between different uses of the margin is
+  ;; problematic.  We should have a way for different packages to indicate (and
+  ;; change) their preference independently.
+  (let* ((width (if (display-graphic-p)
+                    (ceiling
+                     (let ((width (nlinum--face-width 'linum)))
+                       (if width
+                           (/ (* nlinum--width 1.0 width)
+                              (frame-char-width))
+                         (/ (* nlinum--width 1.0
+                               (nlinum--face-height 'linum))
+                            (frame-char-height)))))
+                  nlinum--width))
+         (cur-margins (window-margins))
+         (cur-margin (car cur-margins))
+         ;; (EXT . OURS) keeps track of the size of the margin, where EXT is the
+         ;; size chosen by external code and OURS is the size we last set.
+         ;; OURS is used to detect when someone else modifies the margin.
+         (margin-settings (window-parameter nil 'linum--margin)))
+    (if margin-settings
+        (unless (eq (cdr margin-settings) cur-margin)
+          ;; Damn!  The margin is not what it used to be!  => Update EXT!
+          (setcar margin-settings cur-margin))
+      (set-window-parameter nil 'linum--margin
+                            (setq margin-settings (list cur-margin))))
+    (and (car margin-settings) width
+         (setq width (max width (car margin-settings))))
+    (setcdr margin-settings width)
+    (set-window-margins nil (if nlinum-mode width (car margin-settings))
+                        (cdr cur-margins))))
 
 (defun nlinum--setup-windows ()
   (dolist (win (get-buffer-window-list nil nil t))