Display-line-number.el (displaying row numbers in Emacs)

Source: Internet
Author: User
; Display-line-number.el --- line number mode for Emacs

; Copyright (c) 2004 Free Software Foundation, Inc.

; Author: chunye Wang
; Keywords: Tools

; This file is free software; you can redistribute it and/or modify
; It under the terms of the GNU General Public License as published
; The Free Software Foundation; either version 2, or (at your option)
; Any later version.

; This file is distributed in the hope that it will be useful,
; But without any warranty; without even the implied warranty
; Merchantability or fitness for a special purpose. See
; GNU General Public License for more details.

; You shoshould have written ed a copy of the GNU General Public License
; Along with GNU Emacs; see the file copying. If not, write
; The Free Software Foundation, inc., 59 temple place-Suite 330,
; Boston, MA 02111-1307, USA.

; Commentary:

; Installation:

; Put the display-line-number.el in your load-Path
; (Require 'display-line-number)
; M-X Display-line-number-Mode
; M-X Display-line-number-mode-on
; M-X Display-line-number-mode-off
; Code:

(Defvar display-line-number-mode Nil
"Non-nil if display-line-number-mode is active in the current buffer .")
(Make-variable-buffer-local 'display-line-number-mode)

(Defvar display-line-number-face 'font-lock-keyword-face)

(Defvar global-display-line-number-mode Nil
"Non-nil if display-line-number-mode is active in the all buffer .")

(Defvar display-line-number-format "% 5d"
"String suitable for 'format' that will generate a line number string.
'Format' will be called with this string and one other argument
Which will be an integer, the line number .")

(Defun display-line-number-mode (& optional Arg)
"Toggle display-line-number-mode.
With prefix argument, turn display-line-number-mode on If argument is positive.
When display-line-number-mode is enabled, a line number will appear at the left
Margin of each line ."
(Interactive "p ")
(If (null Arg) (setq Arg 0)
(Setq Arg (prefix-Numeric-value Arg )))
(Cond
(> Arg 0) (setq display-line-number-mode t ))
(= Arg 0) (setq display-line-number-Mode
(Not display-line-number-mode )))
(<Arg 0) (setq display-line-number-mode nil )))
(If display-line-number-Mode
(Display-line-number-mode-on)
(Display-line-number-mode-off )))

(Defun global-display-line-number-mode (& optional Arg)
"Toggle display-line-number-mode.
With prefix argument, turn display-line-number-mode on If argument is positive.
When display-line-number-mode is enabled, a line number will appear at the left
Margin of each line ."
(Interactive "p ")
(If (null Arg) (setq Arg 0)
(Setq Arg (prefix-Numeric-value Arg )))
(Cond
(> Arg 0) (setq global-display-line-number-mode t ))
(= Arg 0) (setq global-display-line-number-Mode
(Not global-display-line-number-mode )))
(<Arg 0) (setq global-display-line-number-mode nil )))
(If global-display-line-number-Mode
(Add-hook 'Find-file-hooks
'Display-line-number-mode-on)
(Remove-hook 'Find-file-hooks
'Display-line-number-mode-on ))
(Message "Global display line number mode is % s" (if global-display-line-number-mode "on" "off ")))

; (Global-display-line-number-mode (if global-display-line-number-mode 1 0 ))

(Defun display-line-number-mode-on ()
(Interactive)
(Setq display-line-number-mode t)
(DLN-undisplay)
(DLN-display)
(Make-local-variable 'after-change-functions)
(Make-local-variable 'window-scroll-functions)
(Make-local-variable 'window-configuration-change-hook)
(Add-to-list 'after-change-Functions
'Dln-after-change-function)
(Add-to-list 'window-configuration-change-hook
'Dln-window-configuration-change-function)
(Add-to-list 'window-scroll-Functions
'Dln-window-scroll-function ))

(Defun display-line-number-mode-off ()
(Interactive)
(DLN-undisplay)
(Setq after-change-functions (remq
'Dln-after-change-Function
After-change-Functions ))
(Setq window-configuration-change-hook (remq
'Dln-window-configuration-change-Function
Window-configuration-change-hook ))
(Setq window-scroll-functions (remq
'Dln-window-scroll-Function
Window-scroll-Functions )))

(Defun DLN-core (start end Str)
(Let (E (make-overlay start end )))
(Overlay-put e 'before-string (propertize STR 'face display-line-number-face ))
(Overlay-put e 'name' DLN-core )))

(Defun DLN-Display (& optional Arg)
(Interactive "p ")
; (Setq left-margin-width 5)
; (Set-window-margins (selected-window) 5 0)
(Let (line-number 1)
(START (window-start (selected-window )))
(End (window-end (selected-window) T ))
(N 0)
(Flag 0 ))
(Save-excursion
(Save-Restriction
(Widen)
(Goto-Char start)
(Beginning-of-line)
(Setq N (count-lines 1 (point )))
(While (= Flag 0)
(Setq N (1 + n ))
(DLN-core (line-beginning-position)
(Line-end-position)
(Format display-line-number-format n
))
(Forward-line 1)
(If (or (> = (point) end)
(> = (Point) (point-max ))
(Not (equal (point) (line-beginning-position ))))
(Setq flag 1 )))))))

(Defun DLN-undisplay ()
(Interactive)
(Save-excursion
(Save-Restriction
(Widen)
(Let (ES (overlays-in 1 (1 + (point-max )))))
(Mapc (lambda (E)
(If (eq (overlay-Get e'name) 'dln-core)
(Delete-overlay E )))
Es )))))

(Defun DLN-after-change-function (& optional start end length)
(DLN-undisplay)
(DLN-display ))

(Defun DLN-window-scroll-function (& optional win POS)
(DLN-undisplay)
(DLN-display ))

(Defun DLN-window-configuration-change-function (& optional win POS)
(DLN-undisplay)
(DLN-display ))

(Provide 'display-line-number)
; Display-line-number.el ends here

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.