🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.emacs.help
Article #188808

Re: how to get mode line to display current line

#188808
From: Paul Filipski
Date: Thu, 11 Jul 1991 19:45
280 lines
9681 bytes
In article <1991Jul9.205827.22681@cats.com> andy@cats.com (Andy Davidson) writes:
>I would like to display both the percentage, and the line number of
>the current line  [in the mode line]
>
>thanks in advance
>andy

What makes this difficult is that emacs is *not* a line based editor,
so it really has no concept of what line the cursor is on.  I'm
posting a set of elisp functions that almost give this functionality,
but it still "falls out of sync" at times.  Then you must type a C-n
or C-p to get the correct line number again.

The way the package works is to define wrapper functions for most of
the motion commands that can affect the line number.  These wrappers
execute the motion command and then update a global variable,
line-number, which is set to appear in the mode line.

Enjoy,
Paul.


---------cut me----------------------------------------------------

;;;
;;;linenumbers.el

(defvar global-line-numbers t
  "TRUE if we want line-numbers to be shown in every mode")

(setq mode-line-buffer-identification '("Emacs: %1b" "--" line-number "--"))

(setq global-mode-string '(""  display-time-string))

(defun what-line* ()
  "RETURN the current line number (in the buffer) of point."
  (interactive)
  (save-restriction
    (widen)
    (save-excursion
      (beginning-of-line)
      (concat " " (1+ (count-lines 1 (point))) " "))))

(setq line-number (what-line*))


(cond
  (global-line-numbers
    (or
;    (global-set-key "\C-?"             'backward-delete-char-untabify*)
     (global-set-key "\C-o"             'open-line*)
     (global-set-key "\e\C-o"           'split-line*)
     (global-set-key "\C-x\C-o"         'delete-blank-lines*)
     (global-set-key "\C-j"             'newline-and-indent*)
     (global-set-key "\C-cg"            'goto-line*)
     (global-set-key "\C-k"             'kill-line*)
     (global-set-key "\C-m"             'newline*)
     (global-set-key "\C-b"             'backward-char*)
     (global-set-key "\C-w"             'kill-region*)
     (global-set-key "\C-y"             'yank*)
     (global-set-key "\C-x["            'backward-page*)
     (global-set-key "\C-p"             'previous-line*)
     (global-set-key "\eb"              'backward-word*)
     (global-set-key "\e<"              'beginning-of-buffer*)
     (global-set-key "\ev"              'scroll-down*)
     (global-set-key "\ey"              'yank-pop*)
     (global-set-key "\C-v"             'scroll-up*)
     (global-set-key "\C-n"             'next-line*)
     (global-set-key "\C-f"             'forward-char*)
     (global-set-key "\C-x]"            'forward-page*)
     (global-set-key "\e>"             'end-of-buffer*)
     (global-set-key "\ef"             'forward-word*)))
  (t
    (or
;    (local-set-key "\C-?"             'backward-delete-char-untabify*)
     (local-set-key "\C-o"             'open-line*)
     (local-set-key "\e\C-o"           'split-line*)
     (local-set-key "\C-x\C-o"         'delete-blank-lines*)
     (local-set-key "\C-j"             'newline-and-indent*)
     (local-set-key "\C-cg"            'goto-line*)
     (local-set-key "\C-k"             'kill-line*)
     (local-set-key "\C-m"             'newline*)
     (local-set-key "\C-b"             'backward-char*)
     (local-set-key "\C-w"             'kill-region*)
     (local-set-key "\C-y"             'yank*)
     (local-set-key "\C-x["            'backward-page*)
     (local-set-key "\C-p"             'previous-line*)
     (local-set-key "\eb"              'backward-word*)
     (local-set-key "\e<"              'beginning-of-buffer*)
     (local-set-key "\ev"              'scroll-down*)
     (local-set-key "\ey"              'yank-pop*)
     (local-set-key "\C-v"             'scroll-up*)
     (local-set-key "\C-n"             'next-line*)
     (local-set-key "\C-f"             'forward-char*)
     (local-set-key "\C-x]"            'forward-page*)
     (local-set-key "\e>"             'end-of-buffer*)
     (local-set-key "\ef"             'forward-word*))))


(defun backward-char* (arg)
  "same as \\[backward-char] , but display line-number also."
  (interactive "p")
  (backward-char arg)
  (setq line-number (what-line*)))

(defun backward-page* (arg)
  "See backward-page for documentation.  "
  (interactive "p")
  (backward-page arg)
  (setq line-number (what-line*)))

(defun previous-line* (arg)
  "See previous-line for documentation. "
  (interactive "p")
  (previous-line arg)
  (setq line-number (what-line*)))

(defun backward-word* (arg)
  "See backward-word for documentation. "
  (interactive "p")
  (backward-word arg)
  (setq line-number (what-line*)))

(defun beginning-of-buffer* (arg)
  "See beginning-of-buffer for documentation. "
  (interactive "P")
  (beginning-of-buffer arg)
  (setq line-number (what-line*)))

(defun scroll-down* (arg)
  "See scroll-down for documentation. "
  (interactive "P")
  (scroll-down arg)
  (setq line-number (what-line*)))

(defun scroll-up* (arg)
  "See scroll-up for documentation. "
  (interactive "P")
  (scroll-up arg)
  (setq line-number (what-line*)))

(defun next-line* (arg)
  "See next-line for documentation. "
  (interactive "p")
  (next-line arg)
  (setq line-number (what-line*)))


(defun forward-char* (arg)
  "See forward-char for documentation. "
  (interactive "p")
  (forward-char arg)
  (setq line-number (what-line*)))

(defun forward-page* (arg)
  "See forward-page for documentation. "
  (interactive "p")
  (forward-page arg)
  (setq line-number (what-line*)))

(defun end-of-buffer* (arg)
  "See end-of-buffer for documentation. "
  (interactive "P")
  (message (concat " " arg))
  (end-of-buffer arg)
  (setq line-number (what-line*)))

(defun forward-word* (arg)
  "See forward-word for documentation. "
  (interactive "p")
  (forward-word arg)
  (setq line-number (what-line*)))

;(defun newline+ (arg)
;  " Redefine \\[backward-delete-char-untabify] , and then redefine itself"
;  (interactive "p")
;(cond
;  (global-line-numbers
;    (or 
;      (global-set-key "\C-?" 'backward-delete-char-untabify*)
;      (global-set-key "\C-m" 'newline*)))
;  (t 
;    (or
;      (local-set-key "\C-?" 'backward-delete-char-untabify*)
;      (local-set-key "\C-m" 'newline*))))
;  (global-set-key "\C-m" 'newline*)
;  (newline* arg))


(defun newline* (arg)
  "See newline for documentation. "
  (interactive "p")
  (newline arg)
  (setq line-number (what-line*)))



(defun open-line* (arg)
  " See open-line for documentation "
  (interactive "*p")
  (open-line arg)
  (setq line-number (what-line*)))

(defun split-line* (arg)
  "See split-line for documentation. "
  (interactive "*")
  (split-line arg)
  (setq line-number (what-line*)))

(defun delete-blank-lines* (arg)
  "See delete-blank-lines for documentation. "
  (interactive "*" )
  (delete-blank-lines arg)
  (setq line-number (what-line*)))

(defun newline-and-indent* ()
  "See newline-and-indent for documentation. "
  (interactive "*" )
  (newline-and-indent)
  (setq line-number (what-line*)))

(defun goto-line* (arg)
  "See goto-line for documentation. "
  (interactive "Ngoto? ")
  (goto-line arg)
  (setq line-number (what-line*)))

(defun kill-line* (arg)
  "See kill-line for documentation. "
  (interactive "*P")
  (setq this-command 'kill-line)
  (kill-line arg)
  (setq line-number (what-line*)))

(defun kill-region* (beg end)
  "See kill-region for documentation. "
  (interactive "*r")
  (setq this-command 'kill-region)
  (kill-region beg end)
  (setq line-number (what-line*)))

(defun yank-pop* (arg)
  "See yank-pop for documentation. "
  (interactive "*p")
  (setq this-command 'yank-pop)
  (yank-pop arg)
  (setq line-number (what-line*)))

(defun yank* (arg)
  "See yank for documentation. "
  (interactive "*P")
  (setq this-command 'yank)
  (yank arg)
  (setq line-number (what-line*)))

;;(defun backward-delete-char-untabify* (arg &optional killp)
;;  "See backward-delete-char-untabify for documentation. "
;;  (interactive "*p\nP")
;;  (backward-delete-char-untabify arg killp)
;;  (setq line-number (what-line*)))

(defun backward-delete-char-untabify (arg &optional killp)
  "Delete characters backward, changing tabs into spaces, and display line-number
Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
Interactively, ARG is the prefix arg (default 1)
and KILLP is t if prefix arg is was specified."
  (interactive "*p\nP")
  (let ((count arg))
    (save-excursion
      (while (and (> count 0) (not (bobp)))
	(if (= (preceding-char) ?\t)
	    (let ((col (current-column)))
	      (forward-char -1)
	      (setq col (- col (current-column)))
	      (insert-char ?\ col)
	      (delete-char 1)))
	(forward-char -1)
	(setq count (1- count)))))
  (delete-backward-char arg killp)
  (setq line-number (what-line*)))
Technical Details
Message-ID: <82932@eerie.acsu.Buffalo.EDU>
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!zaphod.mps.ohio-state.edu!ub!acsu.buffalo.edu
References: <1991Jul9.205827.22681@cats.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 103b8b,21ccfc7be6558e19
X-Google-Attributes: gid103b8b,public
X-Google-ArrivalTime: 1991-07-11 16:21:21 PST
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!zaphod.mps.ohio-state.edu!ub!acsu.buffalo.edu
From: filipski@acsu.buffalo.edu (Paul Filipski)
Newsgroups: gnu.emacs.help
Subject: Re: how to get mode line to display current line
Message-ID: <82932@eerie.acsu.Buffalo.EDU>
Date: 11 Jul 91 19:45:40 GMT
References: <1991Jul9.205827.22681@cats.com>
Sender: news@acsu.Buffalo.EDU
Organization: State University of New York at Buffalo/Comp Sci
Lines: 280
Nntp-Posting-Host: procyon.cs.buffalo.edu