Looking forward a way to make cursor blinks like a heartbeat in Emacs - emacs

How can I make the cursor in Emacs blink like a heartbeat. Like the LED on the front panel of laptop when the computer is suspended.
There is variable blink-cursor-alist that control the blink of cursor, but I do not know how to use it to satisfy my requirement.
Is it possible?

This simple minor mode implements a heartbeat-style blinking cursor. You can tweak heartbeat-cursor-colors to get different hue or variations thereof.
The code is tested in Emacs 24.2.1, but would be easy to port to older Emacsen.
(require 'cl)
(require 'color)
(defvar heartbeat-fps 16)
(defvar heartbeat-period 5)
(defun heartbeat-range (from to cnt)
(let ((step (/ (- to from) (float cnt))))
(loop for i below cnt collect (+ from (* step i)))))
(defun heartbeat-cursor-colors ()
(let ((cnt (* heartbeat-period heartbeat-fps)))
(mapcar (lambda (r)
(color-rgb-to-hex r 0 0))
(nconc (heartbeat-range .2 1 (/ cnt 2))
(heartbeat-range 1 .2 (/ cnt 2))))))
(defvar heartbeat-cursor-timer nil)
(defvar heartbeat-cursor-old-color)
(define-minor-mode heartbeat-cursor-mode
"Change cursor color with the heartbeat effect."
nil "" nil
:global t
(when heartbeat-cursor-timer
(cancel-timer heartbeat-cursor-timer)
(setq heartbeat-cursor-timer nil)
(set-face-background 'cursor heartbeat-cursor-old-color))
(when heartbeat-cursor-mode
(setq heartbeat-cursor-old-color (face-background 'cursor)
heartbeat-cursor-timer
(run-with-timer
0 (/ 1 (float heartbeat-fps))
(lexical-let ((colors (heartbeat-cursor-colors)) tail)
(lambda ()
(setq tail (or (cdr tail) colors))
(set-face-background 'cursor (car tail))))))))

You guess that the option has something to do with the word "blink".
So you press C-h a (for apropos) and type "blink".
On my emacs, I get two options: blink-cursor-mode and blink-matching-open.
The first one looks right. The description says: "Toggle blinking cursor mode".
The shortcut on my emacs says: <menu-bar> <options> <blink-cursor-mode>.
So I guess that the option is somewhere in the menu, maybe under "options".
I open the Options menu, and there it is: "Blinking Cursor" with a tick box.
This also sounds like an option that can be customised.
So I type M-x customize-option and then blink-cursor-mode. This allows me to toggle the value and also save it for future sessions.
EDIT: To set the interval between ON and OFF for the cursor, there is a variable called blink-cursor-interval. You can use M-x customize-variable and then blink-cursor-interval to set the interval. The variable blink-cursor-alist matches an OFF state cursor type to the ON state cursor type, and is not related to the speed of blinking.
EDIT2: As far as I know there is no way to make the cursor gradually turn off and on, because the shape of the cursor for the ON state could be different to the shape in the OFF state (so a gradual change in shape will be required).

Related

How to hide the symbol $ when highlighting LaTeX equations in Emacs Org-mode?

I know that in Org-mode, the emphasis markers like *, ~, =, +, _ can be hidden by this setting: (setq org-hide-emphasis-markers t).
I wonder if there is any option that enables hiding the markers like $ or \(, \) of LaTeX?
This will be useful when LaTeX math equations can be highlighted by setting:
(setq org-highlight-latex-and-related '(latex script entities))
Update 1
I tried the solution proposed by #Thomas on a fresh emacs -q but somehow the symbol $ is still not hidden, while other markers like *,+ are. \
Not sure if there is something wrong with my Emacs? I'm using Org 9.3 of Emacs 27.1
Update 2
The solution of Thomas does work for Emacs 25.2!
But somehow, there is a major change in Emacs 26.2, 26.3, 27.1 that breaks this feature... :(
Update 3
Since the solution suggested by Thomas doesn't work with recent Emacs (26 or newer), I finally came up with a quick solution by customizing the function org-do-latex-and-related of Org-mode.
(defun org-do-latex-and-related (_limit)
"Highlight LaTeX snippets and environments, entities and sub/superscript.
Stop at first highlighted object, if any. Return t if some
highlighting was done, nil otherwise."
(when (org-string-nw-p org-latex-and-related-regexp)
(catch 'found
(while (re-search-forward org-latex-and-related-regexp
nil t) ;; on purpose, we ignore LIMIT
(unless (cl-some (lambda (f) (memq f '(org-code org-verbatim underline
org-special-keyword)))
(save-excursion
(goto-char (1+ (match-beginning 0)))
(face-at-point nil t)))
(let* ((offset (if (memq (char-after (1+ (match-beginning 0)))
'(?_ ?^))
1
0))
(start (+ offset (match-beginning 0)))
(end (match-end 0)))
(if (memq 'native org-highlight-latex-and-related)
(org-src-font-lock-fontify-block "latex" start end)
(font-lock-prepend-text-property start end
'face 'org-latex-and-related))
;;<<<<<<<<<<<<<<<<<<<<<
;; my code starts here
(when (and org-hide-emphasis-markers (< (+ start 4) end))
(cond ((member (buffer-substring start (+ start 2)) '("$$" "\\("))
(add-text-properties start (+ start 2) '(invisible org-link)))
((string= (buffer-substring (1+ start) (+ start 2)) "$")
(add-text-properties (1+ start) (+ start 2) '(invisible org-link))))
(cond ((member (buffer-substring end (- end 2)) '("$$" "\\)"))
(add-text-properties end (- end 2) '(invisible org-link)))
((string= (buffer-substring (1- end) (- end 2)) "$")
(add-text-properties (1- end) (- end 2) '(invisible org-link)))))
;; my code ends here
;;>>>>>>>>>>>>>>>>>>>>>
(add-text-properties (+ offset (match-beginning 0)) (match-end 0)
'(font-lock-multiline t)))
(throw 'found t)))
nil)))
If anyone interested in this feature, you can place the above function somewhere in your Emacs configuration file, after Org-mode is loaded, so that the new org-do-latex-and-related will override the original one of Org-mode.
Here is what I obtained using the code above:
An alternative approach is to not hide the $ symbol at all and instead use org-fragtog. It automatically toggles org-mode latex fragment previews as the cursor enters and exits them.
However, when you open a file with equations it will only toggle the preview of an equation as the cursor enters and leaves the equation. I also have a keybinding (f5) for org-latex-preview. If I open an org-mode file with many equations I can type C-u C-u f5 to toggle latex preview on all equations in the buffer. If I want to change some equation I can just move the cursor there and org-fragtog-mode will turn-off the preview. Then I can edit the equation and the cursor leaves the equation the preview will be automatically enabled again.
This also works correctly when org-highlight-latex-and-related is set to any of the possible choices as well as when you use prettify-symbols-mode.
Customize the variable org-emphasis-alist and add $ as an additional maker with the default values for all customization options. To do so, type
M-x customize-variable [RET] org-emphasis-alist [RET]
Then go to the bottom of the customization buffer, click on the last INS, and insert a dollar sign under Marker character:.
Finally, click on State to make this change permanent either for the current editing session only or also for all future sessions.
NOTE that you have to restart org-mode by typing M-x org-mode again in order for this change to take effect.

Maximize Emacs Windows Horizonatally or Vertically

I'm aware of C-x 1, which will maxamize the current window both horizontally and vertically.
However, my quesiton is, is it possible to expand the current window to the edge of the frame on one direction only?
So in the below I want to expand window A to the right border of the frame, taking up the space currently occupied by B and C. But I want D and E to remain untouched.... and I want to do it in a single command.
Excuse the terrible attempt at ASCII art!
_______________________
| AAAAAA |BBBBBB |CCCC |
|________|_______|______|
| DDDDD | EEEEEEEEEE |
|________|______________|
I know you can move horizontally 1 char at a time, and that you can use the repeat n times command to do this many times, but both are clunky, when what I really want to say is expand until the right hand border, I don't care how far that is.
The nearest I've come up with this to go to each frame occupying the space you want and calling C-X 0, but this is still a bit clunky.
I need this to work in terminal mode (emacs -nw) rather than graphical/X-Windows mode.
Any ideas?
You can do this with library frame-cmds.el (description).
It provides these commands:
maximize-frame-horizontally, maximize-frame-vertically, and max-frame
restore-frame-horizontally, restore-frame-vertically, and restore-frame
The "restore" commands are actually toggles that alternate between maximizing and restoring. (They are aliased to commands toggle-max-frame*.)
Commands maximize-frame and restore-frame are general, and can act like the horizontal and vertical commands by giving them a prefix arg: negative for horizontally, non-negative for vertically.
I have the same problem: I would like to maximize a window horizontally but not vertically. After searching on the internet with no avail, I decided to write my own function. And I would like to share it here in case it might help someone in the future.
(require 'cl-lib)
(defun durand-maximize-window-horizontally (&optional window)
"Make WINDOW have the same width as the frame.
WINDOW defaults to the selected window.
Other windows are retained, but moved to the top."
(let* ((window (or window (selected-window)))
;; the list of windows to the left
(left-windows-list (let ((temp-window window)
window-list)
(cl-loop while (window-in-direction
'left temp-window t)
do (let ((left-win
(window-in-direction
'left temp-window t)))
(push left-win window-list)
(setf temp-window left-win)))
window-list))
;; the list of windows to the right
(right-windows-list (let ((temp-window window)
window-list)
(cl-loop while (window-in-direction
'right temp-window t)
do (let ((right-win
(window-in-direction
'right temp-window t)))
(setf window-list
(append window-list
(list right-win)))
(setf temp-window right-win)))
window-list))
;; the list of windows to the left and to the right
;; the order is from left to the right.
(same-level-list (append left-windows-list right-windows-list))
;; save all parameters: the car is the buffer, and the cadr is a list
;; of parameters.
(window-parameters-list
(cl-loop for win in same-level-list
collect (list (window-buffer win)
;; (window-width win)
(window-parameters win)))))
(cl-loop for win in same-level-list
do (delete-window win))
;; now our window is the only window horizontally speaking.
;; now we shall create them once again, if they exist.
(when same-level-list
(let* ((split-base-window
(split-window window nil 'above))
(new-windows (list split-base-window))
newly-created-window)
(cl-loop
for ind from 1 to (1- (length same-level-list))
do
(setf newly-created-window
(split-window split-base-window
nil 'right)
;; NOTE: it is important this list also follows the order
;; of going from the left to the right
new-windows (append new-windows
(list newly-created-window))
split-base-window newly-created-window))
(cl-loop for index from 0 to (1- (length same-level-list))
do
(let ((buf (car (nth index window-parameters-list)))
(paras (cadr (nth index window-parameters-list))))
(set-window-buffer
(nth index new-windows) buf)
(cl-loop for para-pair in paras
do (set-window-parameter
(nth index new-windows)
(car para-pair)
(cdr para-pair)))))))))

Why the function name that starts with "do-" gets highlighted in emacs?

I am using zenburn.el color-scheme in emacs 23. The function name that starts with "do-" gets highlighted, as given in the figure below --
How do I fix it? Any idea?
I do not observe this with Emacs 24.3 (the current development version).
The code which would have resulted in this highlighting is commented out in lisp-mode.el:
;; This is too general -- rms.
;; A user complained that he has functions whose names start with `do'
;; and that they get the wrong color.
;; ;; CL `with-' and `do-' constructs
;;("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
So, what you need to do is:
(dolist (s (apropos-internal "lisp.*-font-lock" #'boundp))
(set s (cl-remove-if (lambda (l)
(let ((re (car l)))
(and (stringp re)
(string-match re "do-something"))))
(symbol-value s))))
or just edit lisp-mode.el, comment out the appropriate regexp, and rebuild.

Emacs: How to yank the last yanked text regardless of subsequent kills?

I often find myself repeatedly yanking something after doing some kills and it becomes a process like:
C-y
C-y M-y
C-y M-y M-y
C-y M-y M-y M-y
Each time I kill some text it pushes the first kill back in the kill ring so that I need to cycle through all the kills to return to text I want to yank. What I want to do is repeatedly yank the same text while killing text in-between yanks. Is this possible?
Don't use the kill ring; put the text into a register instead. C-x r s a to store the region's text into (say) register "a"; then C-x r i a to insert it elsewhere.
This is a strange hack, but may help.
The first time you use M-y you normally get an error (no previous yank). So the idea is that this first time you get the last yank instead of the last kill.
For storing that last yank I use the 'Y' register in this example.
These 2 functions would wrap around yank and yank-pop. You expect bugs, I expect suggestions.
(defun jp/yank (&optional arg)
"Yank and save text to register Y"
(interactive)
(set-register ?Y (current-kill 0 t))
(yank arg))
(defun jp/yank-pop (&optional arg)
"If yank-pop fails, then insert register Y"
(interactive)
(condition-case nil
(yank-pop arg)
(error (insert (get-register ?Y)))))
(global-set-key (kbd "M-y") (quote jp/yank-pop))
(global-set-key (kbd "C-y") (quote jp/yank))
You could use M-x delete-region instead to kill the text, possibly binding it to a key if you want to use it a lot.
If you want to repeatedly yank the same text, use the secondary selection instead of the region or killed text.
What's missing from vanilla Emacs is a key binding to yank the secondary selection. I use C-M-y for that (see library second-sel.el).
To get direct access to any kills in the kill ring, use M-y with Browse Kill Ring or with Icicles.
In both cases, M-y at top level gives you access to all entries in the kill ring.
And if you use library second-sel.el then, in addition to the kill ring, you have access to a ring of your past secondary selections.
And if you use library second-sel.el and Icicles then M-y yanks an entry from the the ring you last yanked from (kill ring or secondary-selection ring).
And if you use library browse-kill-ring+.el then the kill-ring browser gives you access to an alternative ring also (which, by default, is the ring of secondary selections if you use library second-sel.el).
I'll try to use delete-region more, but I know the kill commands better. A trick that requires no programming or advance planning is to use M-w after a particularly annoying string of M-y. This puts a more accessible copy of the final yank into the kill ring.
A Better Yank-Pop Implementation
This defines a better yank-pop implementation which tries to fix the increasing yank-pop problem.
It only overrides the function "current-kill". Due to the modular design of yank, yank-pop, and the kill ring variables and functions in Emacs, it turns out that overriding "current-kill" is all that is necessary to get the behavior we want.
The desired behavior is that (1) killing something still puts it at the front of the kill ring, but now (2) yanking or yank-popping something also puts it at the front of the kill ring (3) we preserve the ability of yank-pop to give the appearance of moving through the kill ring by incrementing a global variable and using this to replace the last yank-pop'ped item back where it was. This also means that (4) items which are transitionally yanked (i.e. items placed by yank or yank-pop commands, where the next command is a yank-pop) ultimately get to stay where they are in the kill ring.
;; Example:
;; (setq kill-ring '("a" "b" "c" "d" "e"))
;;
;; keystroke kill ring contents value of kill-ring-yank-index
;; C-y ("a" "b" "c" "d" "e") 0
;; M-y ("b" "a" "c" "d" "e") 1
;; M-y ("c" "a" "b" "d" "e") 2
;; M-y ("d" "a" "b" "c" "e") 3
;; C-y ("d" "a" "b" "c" "e") 0
;; M-y ("a" "d" "b" "c" "e") 1
;; M-d ("x" "a" "d" "b" "c" "e")
;; etc.
the code:
;; ----------------------------------------------------------------
;; helper functions
(defun list-insert-before (l n x)
(if (<= n 0) (cons x l)
(cons (car l) (list-insert-before (cdr l) (- n 1) x))))
(defun list-prepend-nth (l n)
(if (<= n 0) l
(let* ((lx (list-prepend-nth (cdr l) (- n 1))))
(cons (car lx) (cons (car l) (cdr lx))))))
(defun list-insert-car-at (l n)
(list-insert-before (cdr l) n (car l)))
;; ----------------------------------------------------------------
;; overriding current-kill
(defvar kill-ring-yank-index 0
"Index into kill-ring of last yank-pop. The item yank-popped
will be at the head of the kill ring, but if the next command
is also yank-pop, it will be returned here first before this
variable is incremented.")
(defun current-kill (n)
"Replaces standard 'current-kill' function. This version tries
to fix the increasing yank-pop problem.
TODO:
- respect second argument of original function
- deal with 'interprogram-{cut,paste}-function'
"
(if (eq 0 n) ;; looks like we're doing a yank; reset
;; kill-ring-yank-index to 0 to indicate that the
;; current head of the list is useful to the user
(progn (setq kill-ring-yank-index 0)
(car kill-ring))
;; otherwise put the head of kill-ring back where we had
;; previously found it, and fetch the next element
(setq kill-ring
(list-insert-car-at kill-ring kill-ring-yank-index))
(setq kill-ring-yank-index (+ kill-ring-yank-index n))
(when (>= kill-ring-yank-index (- (length kill-ring) 1))
(setq kill-ring-yank-index (- (length kill-ring) 1))
(message "Reached end of kill-ring"))
(when (< kill-ring-yank-index 0)
(setq kill-ring-yank-index 0)
(message "Reached beginning of kill-ring"))
(setq kill-ring (list-prepend-nth kill-ring kill-ring-yank-index))
(car kill-ring)))
;; ----------------------------------------------------------------
;; new key binding
;; Here's an auxiliary function and key binding that makes it easy to
;; go back and forth in the kill-ring while we're yank-popping
(defun yank-pop-back () "" (interactive "*")
(yank-pop -1))
(global-set-key "\C-\M-y" 'yank-pop-back)
I am trying to hack along the line of using a minor mode. Let's call this delete-mode. Once you get into delete mode, kill commands (kill-line, kill-paragraph, kill-word, ...) will change their behavior so that the kill-region part of their commands will be replaced by delete-region, and new material will not be added to the kill ring. While in this mode, the kill ring will stay constant. When you switch back out of this mode, the behaviour returns to normal.
The following is an incomplete code attempting to implement what I wrote above. It works correctly in switching to delete mode, but it has problem switching back (turning the minor mode off). Any help fixing this would be appreciated.
(defvar delete-mode nil)
(defun delete-mode ()
"delete minor-mode"
(interactive)
(setq delete-mode (not delete-mode))
(if delete-mode
(defalias 'kill-region 'delete-region)
(defalias 'kill-region 'original-kill-region)
)
)
(if (not (assq 'delete-mode minor-mode-alist))
(setq minor-mode-alist
(cons '(delete-mode "Delete mode on") minor-mode-alist)
)
(defalias 'original-kill-region 'kill-region)
)
I use
M-x browse-kill-ring
with keybinding 'M-y'. There is also helm support.
https://www.emacswiki.org/emacs/BrowseKillRing
offers more solutions.

ESS-Emacs23 question: How to automate a few basic steps after loading an R script

I use ESS-Emacs to edit my R scripts.
Whenever I load a R script it is always followed by:
C-x 3 (I prefer this split)
M-x R (Opens R)
Is there a way to automate steps 1. and 2. every time I type on the terminal:
emacs misc_r_file.r
Just to make myself clear - I don't want either 1 or 2 if I have already opened a R script in emacs. Steps 1 and 2 should be executed only when I open a fresh emacs process.
Thank you for your help in advance.
Is there a way to do this? Of course, it's just programming.
(defvar r-file-loaded-p nil
"non-nil if an R file has been opened")
(defun maybe-setup-r ()
(when (not r-file-loaded-p)
(split-window-horizontally)
(R)
(setf r-file-loaded-p t)))
(add-hook 'r-mode-hook #'maybe-setup-r)
Why don't you persist your Emacs session, though?
You might be interested in this code by FelipeCsaszar which uses Shift-Enter to do what you want upon loading an R file, plus a little more besides once it's loaded ( If R is running and a region is highlighted, shift-enter sends the region over to R to be evaluated. If R is running and no region is highlighted, shift-enter sends the current line over to R. Repeatedly hitting shift-enter in an R file steps through each line (sending it to R), skipping commented lines. The cursor is also moved down to the bottom of the R buffer after each evaluation.)
;; Use shift-enter to split window & launch R (if not running), execute highlighted
;; region (if R running & area highlighted), or execute current line
;; (and move to next line, skipping comments). Nice.
;; See http://www.emacswiki.org/emacs/EmacsSpeaksStatistics,
;; FelipeCsaszar. Adapted to spilit vertically instead of
;; horizontally.
(setq ess-ask-for-ess-directory nil)
(setq ess-local-process-name "R")
(setq ansi-color-for-comint-mode 'filter)
(setq comint-scroll-to-bottom-on-input t)
(setq comint-scroll-to-bottom-on-output t)
(setq comint-move-point-for-output t)
(defun my-ess-start-R ()
(interactive)
(if (not (member "*R*" (mapcar (function buffer-name) (buffer-list))))
(progn
(delete-other-windows)
(setq w1 (selected-window))
(setq w1name (buffer-name))
(setq w2 (split-window w1 nil t))
(R)
(set-window-buffer w2 "*R*")
(set-window-buffer w1 w1name))))
(defun my-ess-eval ()
(interactive)
(my-ess-start-R)
(if (and transient-mark-mode mark-active)
(call-interactively 'ess-eval-region)
(call-interactively 'ess-eval-line-and-step)))
(add-hook 'ess-mode-hook
'(lambda()
(local-set-key [(shift return)] 'my-ess-eval)))
(add-hook 'inferior-ess-mode-hook
'(lambda()
(local-set-key [C-up] 'comint-previous-input)
(local-set-key [C-down] 'comint-next-input)))
(require 'ess-site)
The split done in this code is the C-x-3 one you want --- I always forget that in Emacs horizontal/vertical splitting is backwards from how many people (including me) understand it, referring to the direction the split line "moves in" from and not the orientation of the split line itself.