How to generate mouse event programmatically? - events

There are two very useful functions in SLIME: slime-copy-or-inspect-presentation-at-mouse and slime-presentation-menu. But they must be called with an event argument. In order to generate an even I'd need to do a lot of logistics (calculating the region occupied by a printed representation of the object the "mouse" is currently pointing at, lines and columns - something I don't really want to do because, certainly there's something in Emacs that calculates all that stuff already.
So, is there an easy way to generate "fake" mouse event? Because using mouse for only two things in an editor that otherwise doesn't require mouse feels... well, not very smart. :)
EDIT
Here's what I've tried:
(define-key lisp-mode-map (kbd "C-x ?")
#'(lambda ()
(interactive)
(message "called")
(slime-copy-or-inspect-presentation-at-mouse
`(mouse-2 ;; button
(,(selected-window) ;; window
,(point) ;; position
(0 . 0) ;; window-relative pixel
0 ;; timestamp
nil ;; object
,(point) ;; text position
(,(current-column) . ;; column
,(line-number-at-pos (point))) ;; line
nil ;; image
(0 . 0) ;; object-relative pixel
(1 . 1))))))
(define-key lisp-mode-map (kbd "C-x SPC")
#'(lambda ()
(interactive)
(message "called")
(slime-presentation-menu
`(mouse-3 ;; button
(,(selected-window) ;; window
,(point) ;; position
(0 . 0) ;; window-relative pixel
0 ;; timestamp
nil ;; object
,(point) ;; text position
(,(current-column) . ;; column
,(line-number-at-pos (point))) ;; line
nil ;; image
(0 . 0) ;; object-relative pixel
(1 . 1))))))
And this actually can get me to the point where it displays menu, but when slime-presentation-menu calls any of the functions bound to menu items they somehow see that they weren't called with a "real" mouse event, throw and exit. :(
EDIT2
I actually think this is because of the x-popup-menu, which dispatches different events when you select a menu item with mouse or with keyboard... arrrrrgh.

Commands that expect to be bound to a mouse click take an event parameter. This is a list full of stuff like the button clicked, the position, the window, etc. See 21.7.4 Click Events in the Elisp manual.
Here's a simple example to fake a scroll wheel event:
(mwheel-scroll `(mouse-5 ;; button
(,(selected-window) ;; window
,(point) ;; position
(0 . 0) ;; window-relative pixel
0 ;; timestamp
nil ;; object
,(point) ;; text position
(,(current-column) . ;; column
,(line-number-at-pos (point))) ;; line
nil ;; image
(0 . 0) ;; object-relative pixel
(1 . 1)))) ;; object size
Here's an example of a real event:
'(mouse-4
(#<window 374 on *scratch*>
120
(6 . 10)
1301935153
nil
120
(0 . 0)
nil
(6 . 10)
(7 . 15)))

posn-at-point does almost the right thing. Here's a way to compare its output to a real mouse event:
(defun my/test-posn (e)
(interactive "e")
(message "%S\n%S" e `(down-mouse-3 ,(posn-at-point))))
(global-set-key [down-mouse-3] #'my/test-posn)
Output:
(down-mouse-3 (#<window 73 on blah> 3152 (0 . 594) 145393072 nil 3152 (0 . 33) nil (1 . 4) (8 . 18)))
(down-mouse-3 (#<window 73 on blah> 3152 (0 . 594) 0 nil 3152 (0 . 33) nil (0 . 0) (8 . 18)))

Related

List the cartesian product of the list with itself [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
(defn slist-cartesian [lst]
(slist-iter
lst
(fn [x]
(slist-iter
lst
(fn [y]
(println x y))))))
So i have an entire code to explain but this part doesn't make sense to me, can someone please explain it to me ? I know list-iter is used when you'd like to see something done with each element of the list but i have trouble following the path here.
Let's assume that slist-iter does what you explained as list-iter behaviour, which is "'used' when you'd like to see something done with each element of the list"...
Now, I'm going to present you another way to do the cartesian product using Clojure core functions, so I can point you the key element that could be confusing you...
(defn slist-cartesian [lst]
(doseq [x lst
y lst]
(println x y)))
That is what doseq does. You can read it something like for each x in lst, and for each y in lst, print x and y.
Now, the key part here is that you are repeating lst both for x and y. That way we get the Cartesian product of lst with lst.
That is exactly what the code you show is doing. It is using lst as the list to be processed by both of the slist-iter calls.
Understand that it is needed to process the list each time from the beginning, to get what you are asking: The Cartesian product.
And now understand that you can do it both with the doseq expression and within the slist-iter functions.
Probably you need to understand why you can do it in the slist-iter form.
It is possible because fn creates a function and also that function is a Closure "which means that 'it' can access all the variables that were in scope when the function was created".
That means that that function slist-cartesian is creating with fn when calling the first slist-iter, has access to the parameter lst. Let's refer to that function as the anonymous one.
That anonymous function will be called again each time the first slist-iter begins to process a new element of lst, but lst is going to be the same slist-cartesian received and used when the later created the former.
I hope this answered your question.
If you still want to think and dig more in the subject, there is another way to think about this is by constructing the function one step at a time.
So in your own words ...
(defn slist-cartesian [lst]
(slist-iter
lst ;; The list with the elements
,,, ;; The thing to be done to the elements to the list
))
The thing to be done is a function that receive 1 parameter. That function will be called passing as an argument an element of the list, one element at a time for all the elements in the list.
If we make println the function, it will call it many times, one per element of the list, with such element as argument. Which means that it would print the whole list, one item at a time:
(defn slist-cartesian [lst]
(slist-iter
lst
println
))
(slist-cartesian '(1 2 3))
;; 1
;; 2
;; 3
What happens when you need to add more information to what is printed? You need a more complex function than simply println.
Let's create a function that will prefix a text to each item on the list and print them. Let's prefix the text "number: ". Because we have slist-iter we just only need to solve the problem of prefixing the text to a single number, with a function with only one parameter so it can be used by slist-iter and so solving the problem for all elements in the list. Let's make that solution a function:
(defn prefix-text-number-and-print [x]
(println "number: " x))
And now let's use it to build a function that prefix "number: " to all elements of a list and print them. Let's call that function prefix-text-number-to-each:
(defn prefix-text-number-to-each [lst]
(slist-iter
lst
prefix-text-number-and-print))
(prefix-text-number-to-each '(1 2 3))
;; number: 1
;; number: 2
;; number: 3
Cool. But what happen if we need to change the text of the prefix? That means that the function prefix-text-number-and-print needs to be generalized by adding a parameter for such prefix. But that will make that function useless for slist-iter. What then?
What we will do is to create the generalized function, and then somehow derive from it an specialized function we need on the precise moment.
(defn prefix-text-and-print [prefix, x] ;; Generalized function
(println prefix x))
So for the soon to be function prefix-text-to-each, we would do something like:
(defn prefix-text-to-each [prefix, x]
(slist-iter
lst
(somehow-derive-a-function prefix-text-and-print prefix))) ;; ATTENTION
There are many ways to create functions on the spot. You already used one tool for that in the code you show. You are using the function fn to build a function in place. Another way to do it is with the function partial. Let's explore the partial option first.
For that, I will do to prefix-text-and-print the opposite of what I did to prefix-text-number-and-print. I will specialize it. I will define prefix-text-number-and-print in terms of prefix-text-and-print. First by hand, and then by using partial:
;; By hand
(defn prefix-text-number-and-print [x]
(prefix-text-and-print "number: " x))
;; Using partial
(def prefix-text-number-and-print
(partial prefix-text-and-print "number: "))
Both definitions produce the same result: a function. Note the last one uses def instead of defn.
The definition using partial is simple a def. So it is simply defining prefix-text-number-and-print with whatever value is created by the call to partial.
partial creates a function. That new function will simply call the first parameter received by partial (in this case prefix-text-and-print) with the rest of the arguments partial received as arguments (in this case only "number: ") followed by the arguments the function itself receives.
Try it, it doesn't matter what version you used, the result will be the same:
(prefix-text-number-and-print 123)
;; number: 123
You could have done it in place of the call, with the same result
((partial prefix-text-and-print "number: ") 123)
;; number: 123
Now that you now how to use partial, let's use it to build a generalized function prefix-text-to-each
(defn prefix-text-to-each [prefix, lst]
(slist-iter
lst
(partial prefix-text-and-print prefix))) ;; ATTENTION
Let's try it:
(prefix-text-to-each "hola: " '(1 2 3)
;; hola: 1
;; hola: 2
;; hola: 3
Now we could think on making a function that combines not a text, but a list, with each element of another list. Again, we begin with combining the thing (a list) to a single element:
(def sufix-list-and-print [a-lst x] ;; Yeap, for practical reasons, let's suffix the thing (the list) instead of prefixing.
(slist-iter
a-lst
(partial prefix-text-and-print x ;; we are reusing this function, why not?
)))
(sufix-list-and-print '(1 2 3) 88)
;; 88 1
;; 88 2
;; 88 3
With that function, we can now create the function that combines 2 lists:
(def print-2-lsts-combined [a-lst b-lst]
(slist-iter
b-lst
(partial prefix-list-and-print a-lst)))
Let's try that:
(print-2-lsts-combined '(1 2 3) '(10 20 30))
;; 1 10
;; 1 20
;; 1 30
;; 2 10
;; 2 20
;; 2 30
;; 3 10
;; 3 20
;; 3 30
It happens that if the 2 lists are the same, you will have the Cartesian product. So you can define the Cartesian product in terms of this function:
(defn slist-cartesian [lst]
(print-2-lst-combined lst lst))
Or by itself in a similar way to the print-2-lst-combined function, which resembles the code you show originally:
(def slist-cartesian [lst]
(slist-iter
lst
(partial prefix-list-and-print lst) ;; ATENTION
))
Now, the original code uses fn instead of partial. fn and partial have the same job: to create a function.
Do you see it?
The problem in the list commented with ATTENTION was to give somehow the list lst to a function that receives only 1 parameter and you couldn't use that parameter for such purpose.
So the solution with partial was to derive such function.
The solution with fn was to express that function in place, and "say" the word lst when needed, because it could due to it being a Closure, as explained before. That is an anonymous function and I invite you to read more about it.

no mouse-wheel event when shift key is down (shift+mouse-wheel events?)

I'm trying to manage some events in lisp with lispbuilder-sdl.
Thus far I got this.
;; Load package :
(ql:quickload "lispbuilder-sdl")
;; main definition:
(defun main (argv)
(defparameter *ticks* 0)
(sdl:with-init ()
(sdl:window 100 100 :title-caption "test")
(sdl:with-events ()
(setf (sdl:frame-rate) 60)
(:quit-event () (progn (sdl:quit-image) (exit) t))
(:mouse-button-down-event
(:button button :x x :y y)
(format t "~&LSHIFT: ~a RSHIFT: ~a BUTTON: ~a X: ~d Y: ~d"
(sdl:get-key-state :sdl-key-lshift)
(sdl:get-key-state :sdl-key-rshift)
button x y))
(:key-down-event
(:key key)
(format t "~& KEY: ~a" key))
(:idle ()))))
;; Entrypoint :
(sb-int:with-float-traps-masked (:invalid :inexact :overflow) (main *posix-argv*))
If I launch this, a window appear, I can click and roll, I got an output to describe the state of the keys and buttons pressed. Same if I press a key down. Fine.
But something weird occurs when I keep a shift key down.
If I do so, I still have the output when clicking. But not when rolling (mouse wheel events).
So I guess mouse-wheel events simply arent triggered when a shift (right or left) is down. But only shift keys, and I don't even know why.
So I can't for instance handle shift+mouse-wheel events.
Any idea?
NB : The version of SBCL I use on OSX is 1.2.11 but it works with both 1.3.2 and 1.2.11 on Ubuntu.

lisp iterate through list

I just started GIMP script-fu scripting yesterday for a script I need but I have no idea how automate the adding of the layers in the local variables and calling the function for adding the layers to the image. I tried iterating over lists but failed gracefully. Can someone push me in the right direction?
(define (script-fu-gifslot inText inFont inFontSize inTextColor)
(let*
((theImageWidth 700) ; define our local variables
(theImageHeight 100) ; create a new image:
(theImage)
(layerX)
(inOffset)
(theImage (car (gimp-image-new theImageWidth theImageHeight RGB)))
(theText) ;a declaration for the text
(theLayer (car (gimp-layer-new theImage theImageWidth theImageHeight RGB-IMAGE "layer 1" 100 NORMAL)))
(theLayer2 (car (gimp-layer-new theImage theImageWidth theImageHeight RGB-IMAGE "layer 2" 100 NORMAL)))
(theLayer3 (car (gimp-layer-new theImage theImageWidth theImageHeight RGB-IMAGE "layer 3" 100 NORMAL)))
(theLayer4 (car (gimp-layer-new theImage theImageWidth theImageHeight RGB-IMAGE "layer 4" 100 NORMAL))))
(define (yannis-add-new-layer layerX inOffset)
(gimp-image-add-layer theImage layerX 0)
(gimp-context-set-background '(255 255 255))
(gimp-context-set-foreground inTextColor)
(gimp-drawable-fill layerX BACKGROUND-FILL)
(set! theText (car (gimp-text-fontname theImage layerX 0 0 inText 0 TRUE inFontSize PIXELS "Sans")))
(set! theImageHeight (car (gimp-drawable-height theText)))
(gimp-layer-resize layerX theImageWidth theImageHeight 0 0)
(gimp-layer-set-offsets theText 0 (- 0 inOffset)))
(yannis-add-new-layer theLayer 0)
(yannis-add-new-layer theLayer2 10)
(yannis-add-new-layer theLayer3 20)
(yannis-add-new-layer theLayer4 30)
(gimp-display-new theImage)
(list theImage layerX theText)))
(script-fu-register
"script-fu-gifslot"
"_GGGIF WORD Slotmachine..."
"Creates an image with a user specified text string."
"Yannis De Cleene <yannisdcl#gmail.com>"
"Yannis De Cleene"
"July, 2014"
""
SF-TEXT "Text" "PASTE\nMULTILINE TEXT\nIN HERE"
SF-FONT "Font" "Sans"
SF-ADJUSTMENT "Font size (pixels)" '(100 2 1000 1 10 0 1)
SF-COLOR "Color" '(0 0 0))
(script-fu-menu-register "script-fu-gifslot" "<Image>/File/Create")
Use either map or for-each or do.
If you want to add 1 to a list you could use (map 1+ '(1 2 3)). Or if you want to define the function on the fly, use lambda: (map (lambda (x) (+ 1 x)) '(1 2 3))
If you want a more imperative style use do. You can read about it here.
You can search for functions (and other bound symbols) using the function apropos. e.j. (apropos "for") (Apparently not in gimp to my surpise)
However I sense your problem is larger than just iterate over a list. For example does it make sense to define the function over the let? why are you not explicitly passing the image to the add-new-layer-function?
To make your code more readable I would drop the 'the', use '-' instead of camelCase and don't leave one parentesis in each line. it hurts readability. I can see however it is not your fault as that style is used in the gimp's tutorial. e.j. theImage becomes image and TheLayer1 becomes layer-1.
If you want to learn more about scheme you should download racket and read how to design programs which is a good introduction to programming.

(Random) in Common Lisp Not So Random?

Okay, final question and I'll have finished my number guessing game in Common Lisp! :D Whenever the game starts (or a new game begins after the first game), the following function is called.
;;; Play the game
(defun play ()
;; If it's their first time playing this session,
;; make sure to greet the user.
(unless (> *number-of-guesses* 0)
(welcome-user))
;; Reset their remaining guesses
(setq *number-of-guesses* 0)
;; Set the target value
(setq *target*
;; Random can return float values,
;; so we must round the result to get
;; an integer value.
(round
;; Add one to the result, because
;; (random 100) yields a number between
;; 0 and 99, whereas we want a number
;; from 1 to 100 inclusive.
(+ (random 100) 1)))
(if (eql (prompt-for-guess) t)
(play)
(quit)))
So supposedly, each time the player starts a game, *target* should be set to a new random integer between 1-100. However, each time, *target* defaults to 82. How do I make (random) act... randomly?
You need to seed the random state at the start of the program.
(setf *random-state* (make-random-state t))
;; # this initializes the global random state by
;; "some means" (e.g. current time.)
I think that if you define a function with a random number in it, it is not called when you call the function, actually, it will be determined when you load in the file and when it runs this definition it is fixed to that value. Then you are calling the function each time, the number will always be the same. When I passed in a variable to the function with a random each time it was called, then it was random each time. At least, that what I experienced in my program
In Gimp Scheme (Lisp derivative) I found that I needed to use the following:
(set! *seed* (car (gettimeofday)))
This is the Random Number Seed From Clock (seed is global)
You may also need:
(random-next)
This generates the next Random Number Using Seed
The non-truly random method is to set the seed:
(srand 12345)
; used in cases where the result is to be repeatable - useful in graphic design contexts.

A+ and Emacs (but *not* XEmacs)

I have installed A+ and set up XEmacs in Debian, using the aplus-fsf-dev and aplus-fsf-el packages; XEmacs was installed as a dependency.
I checked the A+ site (http://www.aplusdev.org/), and there seems to be nothing about running A+ on plain Emacs (and not XEmacs).
Does anyone know if there are elisp files somewhere for setting up A+ on plain (FSF) Emacs?
Thanks!
PS: So, the elisp files for XEmacs do not run on Emacs. I tried converting them, but I had to go farther and farther into the code, so I gave up.
PS2: In Emacs, when I do (require 'aplus), this is what I get:
Debugger entered--Lisp error: (wrong-type-argument arrayp (super 97))
define-key((keymap) (super 97) a-self-insert)
(let ((key ...) (apl ...)) (define-key a-minor-map (append a-modifier-list ...) (quote a-self-insert)) (define-key a-minor-map (vector ... key) (quote a-self-insert)) (aset a-key-string (char-to-int key) apl))
a-insert-map((97 . 193))
mapcar(a-insert-map ((97 . 193) (98 . 194) (99 . 195) (100 . 196) (101 . 197) (102 . 95) (103 . 199) (104 . 200) (105 . 201) (106 . 202) (107 . 39) (108 . 204) (109 . 124) (110 . 206) (111 . 207) (112 . 42) (113 . 63) (114 . 210) (115 . 211) (116 . 126) (117 . 213) (118 . 214) (119 . 215) (120 . 216) (121 . 217) (122 . 218) (49 . 161) (50 . 162) (51 . 60) (52 . 164) (53 . 61) (54 . 166) (55 . 62) (56 . 168) (57 . 169) (48 . 94) (45 . 171) (61 . 223) (92 . 220) (96 . 254) (44 . 172) (91 . 251) (93 . 253) (59 . 219) (39 . 221) (46 . 220) (47 . 175) (33 . 224) (64 . 230) (35 . 231) ...))
eval-buffer(#<buffer *load*<3>> nil "/usr/share/emacs/site-lisp/aplus-fsf-el/keyb.el" nil t) ; Reading at buffer position 3754
load-with-code-conversion("/usr/share/emacs/site-lisp/aplus-fsf-el/keyb.el" "/usr/share/emacs/site-lisp/aplus-fsf-el/keyb.el" nil t)
require(keyb)
eval-buffer(#<buffer *load*<2>> nil "/usr/share/emacs/site-lisp/aplus-fsf-el/xa.el" nil t) ; Reading at buffer position 16
load-with-code-conversion("/usr/share/emacs/site-lisp/aplus-fsf-el/xa.el" "/usr/share/emacs/site-lisp/aplus-fsf-el/xa.el" nil t)
load("xa" nil t)
(if aplus-setup-global-bindings (load "xa" nil t))
eval-buffer(#<buffer *load*> nil "/usr/share/emacs/site-lisp/aplus-fsf-el/aplus.el" nil t) ; Reading at buffer position 1373
load-with-code-conversion("/usr/share/emacs/site-lisp/aplus-fsf-el/aplus.el" "/usr/share/emacs/site-lisp/aplus-fsf-el/aplus.el" nil t)
require(aplus)
eval((require (quote aplus)))
eval-last-sexp-1(nil)
eval-last-sexp(nil)
call-interactively(eval-last-sexp nil nil)
This is because in keyb.el, there is this function:
(defun a-insert-map (akeydef)
(let ((key (car akeydef))
(apl (cdr akeydef)))
(define-key a-minor-map (append a-modifier-list (list key)) 'a-self-insert)
(define-key a-minor-map (vector '(control c) key) 'a-self-insert)
(aset a-key-string (char-to-int key) apl)))
I changed append to vconcat, and then I got an error on the last line of that function, because Emacs does not have an char-to-int function. I removed the function call and replaced by the argument ("key") itself, since I understand that Emacs will already treat that character as a number.
Then There were other not so obvious errors in other functions; most of them dealing with define-key and keymaps.
I suppose Emacs and XEmacs deal with keymaps differently?
Let the answers begin. SO wasn't really designed for a running debugging session, but here goes.
Depending on whether you want to make the same .el files loadable by both Emacs and XEmacs, you'll have to figure how you want to isolate the differences.
The most(?) portable way to define keys in Emacs is using the 'kbd macro. So, the 'define-key invocation should look something like:
(define-key a-minor-map (kbd (format "C-c %c" key)) 'a-self-insert)
I don't know what the a-modifier-list is for, but it probably can be massaged into a string to pass to 'kbd. A good introduction to 'kbd or 'read-kbd-macro can be found here. A long document on Emacs keybindings can be found here. It covers all sorts of notations for keybindings, and perhaps it would be of use to decode some of the XEmacs things.

Resources