0

I have written the following bit of emacs lisp that allows hl-line to extend past end of buffer to the rest of the line.

Problem is, this only works if I use (eval-buffer) after start-up and not when I have it saved inside my .emacs file.

I have also modified linum.el so that every line the cursor is on displays the line number (based on the relevant portion from emacs-wiki).

However,

I doubt that could cause this sort of difficulty.

Basically I am trying to make the current highlight line act more like contemporary editors.

    (defvar-local hl-line-after-cursor nil
  "Overlay used by Global-Hl-Line mode to highlight the current line.")


(defun my-hl-line-after-current-line()  
    (if (eq (line-end-position) (point-max))
        (progn
            (setq hl-line-after-cursor (make-overlay (line-end-position) (line-end-position)))
                (overlay-put hl-line-after-cursor 'after-string
                     (propertize (make-string (- (- (window-text-width) (- (line-end-position) (line-beginning-position))) 2) (string-to-char " "))
                     'face hl-line-face 'cursor 1))
            (move-overlay hl-line-after-cursor (line-end-position) (line-end-position))
        ))) 



(defun my-hl-line-after-unhighlight ()
"Deactivate the Hl-Line overlay on the current line."
(when hl-line-after-cursor
(delete-overlay hl-line-after-cursor)))

(add-hook 'pre-command-hook #'my-hl-line-after-unhighlight nil t)
(add-hook 'post-command-hook #'my-hl-line-after-current-line nil t)

my init file

(debug)
(setq user-full-name "YOUR NAME")
(setq user-mail-address "YOUR EMAIL ADDRESS")

;;
;; Basic Setup
;;
(prefer-coding-system 'utf-8)
;; Shorten yes-or-no prompts
(fset 'yes-or-no-p 'y-or-n-p)
;; This code stashes backups in ~/.emacs.d/backups
(setq backup-directory-alist '(("." . "~/.emacs.d/backups")))
;; This code removes the ugly tool bar
(tool-bar-mode -1)
;; Matching parenthesis highlight
(show-paren-mode 1)
;; Show line numbers
(global-linum-mode 1)
;; Set window title to full file name  
(setq frame-title-format '("Emacs @ " system-name ": %b %+%+ %f"))
;; Set up smooth scrolling
(setq redisplay-dont-pause t
      scroll-margin 1
      scroll-step 1
      scroll-conservatively 10000
      scroll-preserve-screen-position 1)
;; get rid of the scroll bar boxes
(scroll-bar-mode -1)
;; Set the tab width to 2 by default
(setq-default tab-width 2)
;; No dinging during exceptions
(setq visible-bell 1)
;; Enable keybinding to Caps Lock key
(setq w32-enable-caps-lock nil)
;; Highlight current line
(global-hl-line-mode +1)

(require 'package)
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
                        ("marmalade" . "http://marmalade-repo.org/packages/")
                        ("melpa" . "http://melpa.org/packages/")))
(package-initialize)

(add-to-list 'custom-theme-load-path "~/.emacs.d/themes/")
(load-theme 'solarized t)

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(custom-safe-themes
     (quote
        ("a8245b7cc985a0610d71f9852e9f2767ad1b852c2bdea6f4aadc12cce9c4d6d0" default))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

;; The default "C-x c" is quite close to "C-x C-c", which quits Emacs.
;; Changed to "C-c h". Note: We must set "C-c h" globally, because we
;; cannot change `helm-command-prefix-key' once `helm-config' is loaded.
(global-set-key (kbd "C-c h") 'helm-command-prefix)
(global-unset-key (kbd "C-x c"))
(global-set-key (kbd "M-x") 'helm-M-x)


    ; (add-to-list 'load-path "~/site-lisp/wolfram-mode-master/")   
    ; (require wolfram-mode)
     ; (autoload 'wolfram-mode "wolfram-mode" nil t)
 ; (autoload 'run-wolfram "wolfram-mode" nil t)
 ; (setq wolfram-program "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Wolfram Mathematica")
 ; (add-to-list 'auto-mode-alist '("\\.m$" . wolfram-mode))
 ; (setq wolfram-path "C:\Program Files\Wolfram Research\Mathematica\10.4")  ;; e.g. on Linux "~/.Mathematica/Applications"


 ;(overlay-put (make-overlay (line-end-position) (point-max)) 'after-string (propertize " " 'face '(:background "gray50" :foreground "black")))
 (defvar-local hl-line-after-cursor nil
  "Overlay used by Global-Hl-Line mode to highlight the current line.")


            (defun my-hl-line-after-current-line()  
            (if (eq (line-end-position) (point-max))
            (progn
                (setq hl-line-after-cursor (make-overlay (line-end-position) (line-end-position)))
                (overlay-put hl-line-after-cursor 'after-string
                     (propertize (make-string (- (- (window-text-width) (- (line-end-position) (line-beginning-position))) 2) (string-to-char " "))
                     'face hl-line-face 'cursor 1))
            (move-overlay hl-line-after-cursor (line-end-position) (line-end-position))
        ;(overlay-put hl-line-after-cursor 'priority -50)
        ))) 



        (defun my-hl-line-after-unhighlight ()
          "Deactivate the Hl-Line overlay on the current line."
          (when hl-line-after-cursor
            (delete-overlay hl-line-after-cursor)))

        (add-hook 'pre-command-hook #'my-hl-line-after-unhighlight nil t)
        (add-hook 'post-command-hook #'my-hl-line-after-current-line nil t)

UPDATE: Below is my best attempt, stefans tip definitely helped as did Drews but its not quite bullet-proof yet.

    (defun my-hl-line-after-current-line()
    (unless (eq (current-buffer) (window-buffer (minibuffer-window)))
        ; (if (eq (car (window-margins)) nil)
        ; (setq linum/margin 0)
        ; (setq linum/margin (car (window-margins))))
    (setq hl-line-after-cursor (make-overlay (line-end-position) (line-end-position)))
    (let ((win (- (window-width) (if (= 0 (or (cdr fringe-mode) 1)) 1 0)))
    ;(let ((win (- (window-text-width) linum/margin))
    (line (- (line-end-position) (line-beginning-position))))
    (overlay-put hl-line-after-cursor 'after-string 
    (if (> win line) 
    (cond
        ((eq (line-end-position) (line-beginning-position)) ;; Sub-if-then
            (goto-char (line-beginning-position))

            (propertize (make-string (- win line) (string-to-char " "))
            'face hl-line-face 'cursor 1)
            ) 
        ((eq (line-end-position) (point-max))   ;; Sub-if-then

            (propertize (make-string (- win line) (string-to-char " "))
            'face hl-line-face 'cursor 1)

            ;(move-overlay hl-line-after-cursor (line-end-position) (line-end-position))
            )           
        (t  ;; Else     
            (propertize (make-string (- win line 1) (string-to-char " "))
            'face hl-line-face 'cursor 1)

            )) 
    nil
        )))
    ))

    (defun my-hl-line-after-unhighlight ()
      "Deactivate the Hl-Line overlay on the current line."
      (when hl-line-after-cursor
        (delete-overlay hl-line-after-cursor)))

    (add-hook 'pre-command-hook #'my-hl-line-after-unhighlight)
    (add-hook 'post-command-hook #'my-hl-line-after-current-line) 

I had to utilize a "cond" statement to get around some weird issues that were popping up, I'd go into detail if my wife wasn't rushing me to pack. (to see it, just comment out the first block of the cond and look at the right margin). But the above works for the most part.

Things I have been unable to solve are:

  • proper scaling with text (zoom and un-zoom)
  • automatically updating after a window size change.

any help/suggestions for that part are greatly appreciated!

-update 2: I also noticed that typing "f" causes the line to bink for a moment, none of the other letters typed do this. Also for whatever reason I notice that the above works in my spacemacs config on my usb drive but not my installed config on my laptop, (I have to comment out first part of "cond")

  • Seems to work for me, if I put it in a file and load it (without the missing line-number thing). Maybe try adding (debug) near the file top, to debug when loading. Also, when do you turn on hl-line-mode? – Drew Jun 30 '16 at 20:34
  • (BTW, is there a reason you don't want to set require-final-newline to t and not have to worry about highlighting past eob?) – Drew Jun 30 '16 at 21:07
  • added my init file, cant honestly say i understand the debuggers output, but I do not see any errors. :/ – skyfire Jun 30 '16 at 21:07
  • I'm just pretty nitpicky, it has been bothering me that I cannot I cannot get at least this base level functionality to mimic other editors. would rather have line number + highlight no matter where cursor is. – skyfire Jun 30 '16 at 21:10
  • The point of the debug was so you could step through the debugger, to see what happens. You can move (debug) just before the code you are interested in. But without debugging, try turning on global-hl-line-mode after your code that tweaks that highlighting, not before. – Drew Jun 30 '16 at 21:11
1

The problem is coming from:

(add-hook 'pre-command-hook #'my-hl-line-after-unhighlight nil t)
(add-hook 'post-command-hook #'my-hl-line-after-current-line nil t)

The nil t at the end means that you only add those to the local part of the hook, i.e. it will only apply to commands executed in "the current buffer" where "current" means "current when the add-hook is executed, IOW current when the ~/.emacs is read (usually *scratch*). Just remove those nil t and my crystal ball says it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.