I want to set different intervals for blinking ON and for blinking OFF. I mean, I want the cursor to stay visible for 1 second and OFF for 0.2 second.
I read the cursor documentation but closest I found is the blink-cursor-interval that changes both ON and OFF blinking.
How can I customize this in Emacs?
There's no such functionality built into Emacs, but you can hack it by adding the following lines to your .emacs file:
(defvar blink-cursor-interval-visible 1)
(defvar blink-cursor-interval-invisible 0.2)
(defadvice internal-show-cursor (before unsymmetric-blink-cursor-interval)
(when blink-cursor-timer
(setf (timer--repeat-delay blink-cursor-timer)
(if (internal-show-cursor-p)
blink-cursor-interval-visible
blink-cursor-interval-invisible))))
(ad-activate 'internal-show-cursor)
Emacs implements the blinking of the cursor with a toggle function called by a timer. Each time the function is called, it either hides the cursor if it is currently visible, or shows it if it is invisible. Unfortunately, the timer calls this function at a fixed interval.
In order to realize different delay times depending on the state of the cursor, the above code advises the internal function that shows or hides the cursor. Each time that function is called, the advice changes the delay time of the timer to either 1 or 0.2, depending on whether the cursor is visible or not. That is, every time the cursor is hidden or shown, the timer's delay time is changed.
Quite hackish, but it does the trick.
I was able to modify the blink-cursor-timer-function function to support what you want I believe.
First, you'll need to modify the value of blink-cursor-interval to .2
then this code should do the trick:
blink-cursor-timer-function is called every blink-cursor-interval seconds.
So this function will be called every .2 seconds, It will keep the cursor ON for 5 calls then turn it off for 1. So 5 calls at .2 seconds per call will give you 1 second of ON time, then only .2 seconds of OFF time.
;; change the interval time to .2
(setq blink-cursor-interval .2)
;; create a variable that counts the timer ticks
(defvar blink-tick-counter 0)
;; this function will be called every .2 seconds
(defun blink-cursor-timer-function ()
"Timer function of timer `blink-cursor-timer'."
(if (internal-show-cursor-p)
(progn
(if (> blink-tick-counter 4)
(progn
(internal-show-cursor nil nil)
(setq blink-tick-counter 0))
(setq blink-tick-counter (1+ blink-tick-counter))))
(internal-show-cursor nil t)))
Related
I am trying to test if a certain position of an Emacs window is visible thus is neither overlapped by another window nor somehow obscured by decoration facilities. To this end I set the mouse position to a certain point and then compare the set values to (mouse-position). However, I get somewhat different values.
How does the actual (mouse-position) differ from the set value?
(Provided that mouse is not moved by the user, indeed).
To test it quickly C-x C-e
(list (set-mouse-position (selected-frame) 4 4) (mouse-position))
As for pos-visible-in-window-p, this does not perform an actual test. To see this
(progn (sleep-for 5) (pos-visible-in-window-p 1))
with C-u C-x C-e and lower, hide the window. Alas, it still is true.
It would seem that pos-visible-in-window-p should do what you want.
Note that your expression moves the actual mouse pointer, i.e., it is visible to the user.
I want to track the "current word" in the current buffer. which another (networked) application will be aware of.
I could do this by sending a request every time the cursor moves, whether from clicking or scrolling or arrowing or etc. i.e. any time the cursor's position in the buffer changes.
e.g.
(add-hook 'cursor-move-hook 'post-request-with-current-word)
Use a post-command-hook this will run after every command, including movement commands.
Obviously this will fire more often than you want, so in your hook, you could do something like keep track of the last position you were at when the hook was run and only fire the network request if the current point differs from the last like so:
(defvar last-post-command-position 0
"Holds the cursor position from the last run of post-command-hooks.")
(make-variable-buffer-local 'last-post-command-position)
(defun do-stuff-if-moved-post-command ()
(unless (equal (point) last-post-command-position)
(let ((my-current-word (thing-at-point 'word)))
;; replace (message ...) with your code
(message "%s" my-current-word)))
(setq last-post-command-position (point)))
(add-to-list 'post-command-hook #'do-stuff-if-moved-post-command)
I have added a lambda() function to run-with-idle-timer like this:
(run-with-idle-timer my-configurable-idle-time t
(lambda ()
;;; do something
) )
Is it possible to remove this function at a later point again from the idle timer trigger?
Yes, run-with-idle-timer returns a timer object which you can pass to cancel-timer.
If you did not keep the timer object around, you can modify timer-idle-list by hand.
See also Getting a list of running Emacs timers.
I also came across a similar situation, where I wanted to kill a timer I started with:
(setq my-timer (run-with-timer 5 5 'my-func))
However,
(cancel-timer my-timer)
was not working because it said my-timer was not set (don't know why this was happening).
In addition to the method of the first poster, it can be killed with:
(cancel-function-timers 'my-func)
This cancels all the timers calling function 'my-func.
To kill it by altering timer-list, which I also tested, I did the following:
(length timer-list) ;; I had two timers..one good, one bad
(cdr timer-list) ;; I verified the last was the one I wanted to keep
(setq timer-list (cdr timer-list)) ;; I reset timer-list
Obviously, this list structure will vary, so you'll have to adjust accordingly. Substitute "timer-idle-list" if you started your timer with (run-with-idle-timer)
This should also work if you started your timer with 'gamegrid-start-timer and 'gamegrid-kill-timer is not working, since 'gamegrid-start-timer is essentially just a wrapper for 'run-with-timer
One more solution: if you're out of luck and can't actually cancel the timer, you can always use
(defvar my-timer-enabler t)
(run-with-idle-timer my-configurable-idle-time t
(lambda ()
(when my-timer-enabler
;;; do something
)))
So you can disable the timer by setting my-timer-enabler to nil. And you can later re-enable the timer by simply setting the var back to t.
I would like some copycat function, that takes the previous input and repeats it (like repeat), but does not get written over when something else is done, and thus remains repeatable. Anyone has any ideas?
EDIT: The way I intend this is to have some mode in which D keypress will act exactly like repeat (if some other input has been done, repeat that), while d will repeat the last thing assigned to the last D key press.
EDIT2: If I would yank, and then press C-x z (in my mode also bound to D), then it will repeat the yank. However, when I would move the cursor down, and I try to press D, it then repeats the down cursor. In this case, I would like the small d to do the behavior of the last repeat (that is, yank) while D would repeat the down cursor command. So, d would store the last repeated command, while D would repeat the last command.
This was just too long for a comment:
It feels like you essentially want a shorter version of keyboard macros? I'll try to explain briefly, and see if it is close:
C-x ( - start recording the macro.
Do whatever you want (may be just a single command). For example, yank something, i.e. M-d
C-x ) - finish recording the macro.
Now you can C-x e to replay the macro (you can do other stuff after you've recorded the macro, C-x e will do what you have previously recorded (i.e. M-d in this case).
Maybe you can create a shorthand version of start-macro end-macro recording, if you are sure there will be only one command, but these are really minor improvements. Once you get used to macros, you'll do it unconsciously, so that one keystroke saved won't matter really.
Also, if I didn't guess what you were after, this may prove to be interesting to you: http://www.gnu.org/software/emacs/manual/html_node/elisp/Command-History.html
My best attempt. It works, though it didn't incorporate all the error handling that repeat has.
(defun Navi-repeat ()
;; Checks whether the last repeatable command is the same as repeat var.
;; If yes, set repeat-navi to that command, and call it.
;; If no, check whether the Navi-repeat variable has been set before:
;; If bound, call it.
;; If not bound,
;; give it the value of the last-repeatable command, and call it.
(interactive)
(if (eq last-repeatable-command 'repeat)
(progn (setq repeat-navi repeat-previous-repeated-command)
(call-interactively repeat-navi))
(if (boundp 'repeat-navi)
(call-interactively repeat-navi)
(progn (setq repeat-navi last-repeatable-command)
(call-interactively repeat-navi))
)
)
)
Is there any way to write something like this without taking over emacs?
(defun dumb-wait (seconds)
(let ((done (+ (second (current-time)) seconds)))
(while (< (second (current-time)) done)
(message "waiting"))))
(dump-wait 5) will block emacs from 5 seconds. Is there anyway to write this so it doesn't block? I just want to be in a loop and check some condition from time to time, and still be able to use emacs.
Thanks!
(run-at-time time repeat function &rest args) should do it. nil as time means now.
(setq my-timer
(run-at-time nil 5 (lambda () (message "waiting")))) ; returns timer object
;; or
(setq my-timer
(run-at-time nil 5 'message "waiting"))
(cancel-timer my-timer) ; use timer object from above
Edit:
The parameter repeat expects a number as seconds, however there's a function timer-duration, which you can use instead of the number. It returns the number of seconds as provided with a string parameter. This is somewhat easier to read for big intervals.
(timer-duration "3 hours 2 seconds 1 millisec") ; => 10802.001
Possible word you can use are defined in the variable timer-duration-words.
On a related note, there's no general way to write Emacs Lisp code that doesn't block because the language doesn't have features like coroutines, continuations and threading (yet). Instead, you've got to look for asynchronous inbuilts that do something closest to what you want. Example: url-retrieve and async-shell-command. Some applications like SLIME have managed to work around this issue and have implemented a sort of threading on their own, while others like gnus are waiting for Emacs Lisp to improve.