DrRacket. Keybinding to duplicate selected lines - racket

How can I create a keybinding in Dr Racket to duplicate selected lines?
#lang s-exp framework/keybinding-lang
(keybinding "m:d" (λ (editor evt) ...))

Here's one that will duplicate the selected text at a point immediately after the end of the selection (So if an entire line(s) is selected, it'll be duplicated):
#lang s-exp framework/keybinding-lang
(keybinding "m:d"
(lambda (editor evt)
(let* ([text-begin (send editor get-start-position)]
[text-end (send editor get-end-position)]
[selected-text (send editor get-text text-begin text-end)])
(send editor insert selected-text text-end))))
Pretty simple; it just gets the positions of the current selection, gets the text in that range, and then inserts that text.

I ended up with this solution:
#lang s-exp framework/keybinding-lang
(keybinding
"m:d" ; Duplicate lines
(λ (editor evt)
(define pos1 (send editor get-start-position))
(define pos2 (send editor get-end-position))
(define line1 (send editor position-line pos1))
(define line2 (send editor position-line pos2))
(define text-begin (send editor line-start-position line1))
(define text-end (send editor line-end-position line2))
(define selected-lines (send editor get-text text-begin text-end))
(send editor insert (format "\n~a" selected-lines) text-end)
(send editor set-position pos1 pos2)))
Add the file to DrRacket:
Edit > Keybindings > Add User-defined Keybindings.. ;
Then restart the program.

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)))))))))

Looking forward a way to make cursor blinks like a heartbeat in 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).

Disable help-menu on mouse button at a right side (with x-coord > 0x7F)

When I click, or scroll mouse wheel, and it's pointer is at the right part of the screen, where there no text, emacs shows help-menu like this:
Press PageUp key to reach this buffer from the minibuffer.
Alternatively, you can use Up/Down keys (or your History keys) to change
--/.../--
In this buffer, type RET to select the completion near point.
Possible completions are:
e==>Emacs Tutorial E==>Emacs Tutorial (choose language)...
--/.../--
a==>About Emacs A==>About GNU
=================================================================================
;; here my work buffer
=================================================================================
Help (up/down to change, PgUp to menu): e==>Emacs Tutorial
It is very disturbing, i have to press C-g to return to edit.
How to disable this feature?
P. S. I tried to press C-h k < LMB at right>, but it doesn't show the code of the key, it shows that menu again.
edit(in response to Stefan):
after pressing: aaa <LMB at right> C-g C-h l i see the following sequence:
... a a a ESC
[ M SPC \300\256 6 ESC [ M # \300\256 6 C-g C-h l
As I found, those symbols: \300\256 and 6 are dependent on the position of the cursor when i click. \300\256 is a smth like horisontal coordinate, 6 is a vertical coordinate.
I investigated it further: the menu appears when y coordinate from an ordinary symbol like a or } or DEL(actually it is the last ordinary symbol) becomes a code like \300\200( this is the first value, which cause the help-menu). As a unicode symbol table tells, DEL code is U+007F(0b01111111), and the next value will be U+00800(0b10000000) (Padding Character) which is in another table: C1 controls and Latin-1 supplement
I use GNU emacs 23.2.1, over PuTTy ssh client.
That does not sound like any of the features of Emacs in its default configuration. I suggest you try: a a a <LMB at right> and then C-h l which should show you the last few events that Emacs received. Try to figure out among these which events correspond to (they will appear right after the a a a) to see what the OS sends to Emacs when you do this .
This is actually a bug.
Here is a workaround for it:
;; Disable menubar
(menu-bar-mode -1)
;; Workaround for "mouse on menu-bar" bug
(defun transform-to-start-event (event)
(let ((y0 (second (window-inside-edges (selected-window))))
(x0 (first (window-inside-edges (selected-window)))))
(let ((x (- (car (third (second event))) #x3fff00 x0))
(y (- (cdr (third (second event))) y0))
(time (fourth (second event))))
`(down-mouse-1 ,(posn-at-x-y x y (window-at 0 0) t)))))
(defun tmm-menubar-mouse (event)
(interactive "e")
(let ((evt (transform-to-start-event event)))
(push (cons 'up-mouse-1 (cdr evt)) unread-command-events)
(mouse-drag-track evt t)))
It deactivates the menu bar, but i didn't need it anyway.
Also it doesn't handles selection, and scrolling properly, but at least, it positions point correctly.

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.