all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Lennart Borgman <lennart.borgman@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 6871@debbugs.gnu.org
Subject: bug#6871: Please make linum-mode per buffer, not per major mode
Date: Thu, 19 Aug 2010 06:29:42 +0200	[thread overview]
Message-ID: <AANLkTi=mAT0G6C3-Bvt=G75N5jNdGtK8VO7NAsqs8xGS@mail.gmail.com> (raw)
In-Reply-To: <jwvhbis5qyi.fsf-monnier+emacs@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 834 bytes --]

On Wed, Aug 18, 2010 at 9:19 AM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
>>>>> I don't think that's going to be sufficient: you'll probably at least
>>>>> also want to remove the
>>>>>        (add-hook 'change-major-mode-hook 'linum-delete-overlays nil t)
> [...]
>>> minor-modes should be made permanent-local), but we need a proper patch
>>> for it.
>> How about also removing adding linum to change-major-mode-hook?
>
> Yes, that's what I suggested, isn't it?

I was not quite sure.

> Please write a patch, test it (after removing the workarounds you
> currently use), and send it here.


Attached the patch. There are some other changes to linum.el too.

The handling in after-change-functions was not optimal. Maybe the doc
string for these hook should explain a bit more about how to use it?

[-- Attachment #2: linum-multi-major.diff --]
[-- Type: application/octet-stream, Size: 9007 bytes --]

;;; linum.el --- display line numbers in the left margin

;; Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.

;; Author: Markus Triska <markus.triska@gmx.at>
;; Maintainer: FSF
;; Keywords: convenience

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Display line numbers for the current buffer.
;;
;; Toggle display of line numbers with M-x linum-mode.  To enable
;; line numbering in all buffers, use M-x global-linum-mode.

;;; Code:

(defconst linum-version "0.9x")

(defvar linum-overlays nil "Overlays used in this buffer.")
(defvar linum-available nil "Overlays available for reuse.")
(defvar linum-before-numbering-hook nil
  "Functions run in each buffer before line numbering starts.")

(mapc #'make-variable-buffer-local '(linum-overlays linum-available))
(put 'linum-overlays  'permanent-local t)
(put 'linum-available 'permanent-local t)

(defgroup linum nil
  "Show line numbers in the left margin."
  :group 'convenience)

;;;###autoload
(defcustom linum-format 'dynamic
  "Format used to display line numbers.
Either a format string like \"%7d\", `dynamic' to adapt the width
as needed, or a function that is called with a line number as its
argument and should evaluate to a string to be shown on that line.
See also `linum-before-numbering-hook'."
  :group 'linum
  :type 'sexp)

(defface linum
  '((t :inherit (shadow default)))
  "Face for displaying line numbers in the display margin."
  :group 'linum)

(defcustom linum-delay nil
  "Delay updates to give Emacs a chance for other changes."
  :group 'linum
  :type 'boolean)

;;;###autoload
(define-minor-mode linum-mode
  "Toggle display of line numbers in the left margin."
  :lighter ""                           ; for desktop.el
  (if linum-mode
      (progn
        (add-hook 'post-command-hook (if linum-delay
                                         'linum-schedule
                                       'linum-update-current-buffer) nil t)
        (add-hook 'before-change-functions 'linum-before-change nil t)
        (add-hook 'after-change-functions 'linum-after-change nil t)
        (add-hook 'window-scroll-functions 'linum-after-scroll nil t)
        ;; Using both window-size-change-functions and
        ;; window-configuration-change-hook seems redundant. --Stef
        ;; (add-hook 'window-size-change-functions 'linum-after-size nil t)
        (add-hook 'window-configuration-change-hook
                  ;; Update just the selected window
                  'linum-update-selected-window nil t)
        (setq linum-change-beg 1)
        (linum-update-current-buffer))
    (remove-hook 'post-command-hook 'linum-update-current-buffer t)
    (remove-hook 'post-command-hook 'linum-schedule t)
    ;; (remove-hook 'window-size-change-functions 'linum-after-size t)
    (remove-hook 'window-scroll-functions 'linum-after-scroll t)
    (remove-hook 'before-change-functions 'linum-before-change t)
    (remove-hook 'after-change-functions 'linum-after-change t)
    (remove-hook 'window-configuration-change-hook 'linum-update-selected-window t)
    (linum-delete-overlays)))
(put 'linum-mode      'permanent-local t)

;;;###autoload
(define-globalized-minor-mode global-linum-mode linum-mode linum-on)

(defun linum-on ()
  (unless (minibufferp)
    (linum-mode 1)))

(defun linum-delete-overlays ()
  "Delete all overlays displaying line numbers for this buffer."
  (mapc #'delete-overlay linum-overlays)
  (setq linum-overlays nil)
  (dolist (w (get-buffer-window-list (current-buffer) nil t))
    (set-window-margins w 0 (cdr (window-margins w)))))

(defun linum-update-current-buffer ()
  "Update line numbers for the current buffer."
  (linum-update (current-buffer)))
(put 'linum-update-current-buffer 'permanent-local-hook t)

(defun linum-update (buffer)
  "Update line numbers for all windows displaying BUFFER."
  (with-current-buffer buffer
    (when (and linum-mode
               linum-change-beg)
      (setq linum-available linum-overlays)
      (setq linum-overlays nil)
      (save-excursion
        (mapc #'linum-update-window
              (get-buffer-window-list buffer nil 'visible)))
      (mapc #'delete-overlay linum-available)
      (setq linum-change-beg nil)
      (setq linum-available nil))))

(defun linum-update-selected-window ()
  "Update line numbers for the selected window."
  (let ((here (window-point))
        ;; We might have scrolled or changed win config
        (linum-change-beg 1))
    (linum-update-window (selected-window))
    (goto-char here)))
(put 'linum-update-selected-window 'permanent-local-hook t)

(defun linum-update-window (win)
  "Update line numbers for the portion visible in window WIN."
  (goto-char (window-start win))
  (let ((line (line-number-at-pos))
        (limit (window-end win t))
        (fmt (cond ((stringp linum-format) linum-format)
                   ((eq linum-format 'dynamic)
                    (let ((w (length (number-to-string
                                      (count-lines (point-min) (point-max))))))
                      (concat "%" (number-to-string w) "d")))))
        (width 0))
    (when (<= linum-change-beg limit)
      (run-hooks 'linum-before-numbering-hook)
      ;; Create an overlay (or reuse an existing one) for each
      ;; line visible in this window, if necessary.
      (while (and (not (eobp)) (<= (point) limit))
        (let* ((str (if fmt
                        (propertize (format fmt line) 'face 'linum)
                      (funcall linum-format line)))
               (visited (catch 'visited
                          (dolist (o (overlays-in (point) (point)))
                            (when (equal-including-properties
                                   (overlay-get o 'linum-str) str)
                              (unless (memq o linum-overlays)
                                (push o linum-overlays))
                              (setq linum-available (delq o linum-available))
                              (throw 'visited t))))))
          (setq width (max width (length str)))
          (unless visited
            (let ((ov (if (null linum-available)
                          (make-overlay (point) (point))
                        (move-overlay (pop linum-available) (point) (point)))))
              (push ov linum-overlays)
              (overlay-put ov 'before-string
                           (propertize " " 'display `((margin left-margin) ,str)))
              (overlay-put ov 'linum-str str))))
        ;; Text may contain those nasty intangible properties, but that
        ;; shouldn't prevent us from counting those lines.
        (let ((inhibit-point-motion-hooks t))
          (forward-line))
        (setq line (1+ line)))
      (set-window-margins win width (cdr (window-margins win))))))

(defvar linum-change-beg nil
  "Position of change beginning, recorded after change.")
(make-variable-buffer-local 'linum-change-beg)
(put linum-change-beg 'permanent-local t)

(defvar linum-changed-region-has-newline nil)
(defun linum-before-change (beg end)
  ;; Record new lines in changed region for check in after change function.
  (when (string-match-p "\n" (buffer-substring-no-properties beg end))
    (setq linum-changed-region-has-newline t)))
(put 'linum-before-change 'permanent-local-hook t)

(defun linum-after-change (beg end len)
  ;; update overlays after newlines are delete or inserted
  (when (or linum-changed-region-has-newline
            (string-match-p "\n" (buffer-substring-no-properties beg end)))
    (setq linum-changed-region-has-newline nil)
    (setq linum-change-beg beg)))
(put 'linum-after-change 'permanent-local-hook t)

(defun linum-after-scroll (win start)
  (with-selected-window win
    (linum-update-selected-window)))
(put 'linum-after-scroll 'permanent-local-hook t)

;; (defun linum-after-size (frame)
;;   (linum-after-config))

(defun linum-schedule ()
  ;; schedule an update; the delay gives Emacs a chance for display changes
  (run-with-idle-timer 0 nil #'linum-update-current-buffer))
(put 'linum-schedule 'permanent-local-hook t)

;; (defun linum-after-config ()
;;   (walk-windows (lambda (w) (linum-update (window-buffer w))) nil 'visible))

(defun linum-unload-function ()
  "Unload the Linum library."
  (global-linum-mode -1)
  ;; continue standard unloading
  nil)

(provide 'linum)

;; arch-tag: dea45631-ed3c-4867-8b49-1c41c80aec6a
;;; linum.el ends here

  reply	other threads:[~2010-08-19  4:29 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-17  2:06 bug#6871: Please make linum-mode per buffer, not per major mode Lennart Borgman
2010-08-17  8:59 ` Stefan Monnier
2010-08-17 11:23   ` Lennart Borgman
2010-08-17 12:11     ` Stefan Monnier
2010-08-17 12:35       ` Lennart Borgman
2010-08-18  7:19         ` Stefan Monnier
2010-08-19  4:29           ` Lennart Borgman [this message]
2010-08-19 12:13             ` Juanma Barranquero
2010-08-19 13:25               ` Lennart Borgman
2010-08-19 13:38                 ` Juanma Barranquero
2010-08-19 14:00                   ` Lennart Borgman
2010-08-19 14:40                     ` Juanma Barranquero
2010-08-19 21:23                       ` Lennart Borgman
2010-08-19 21:57                         ` Juanma Barranquero
2010-08-19 22:21                           ` Lennart Borgman
2010-08-20  9:05                             ` Eli Zaretskii
2010-08-19 22:43                         ` Stefan Monnier
2010-08-20  1:11                           ` Lennart Borgman
2010-08-20 12:59                             ` Stefan Monnier
2010-08-19 14:45                 ` Stefan Monnier
2010-08-19 21:19                   ` Lennart Borgman
2010-08-17 23:47 ` MON KEY
2010-08-18  0:14   ` Lennart Borgman
2010-08-18  7:20   ` Stefan Monnier
2010-08-19 17:49     ` MON KEY
2010-08-19 21:18       ` Lennart Borgman
2020-09-19 15:52 ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='AANLkTi=mAT0G6C3-Bvt=G75N5jNdGtK8VO7NAsqs8xGS@mail.gmail.com' \
    --to=lennart.borgman@gmail.com \
    --cc=6871@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.