Skip to content

Commit 2b203c7

Browse files
committed
exwm-xim: Refactor exwm-xim--on-request for readability
Instead of binding a `req' variable around the entire function, decode and bind request slots directly where they're used. * exwm-xim.el (exwm-xim--on-request): Refactor for readability.
1 parent 1da38fe commit 2b203c7

File tree

1 file changed

+94
-86
lines changed

1 file changed

+94
-86
lines changed

exwm-xim.el

Lines changed: 94 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ The actual XIM request is in client message data or a property."
305305
(let ((opcode (elt data 0))
306306
;; Let-bind `xim:lsb' to make pack/unpack functions work correctly.
307307
(xim:lsb (elt (plist-get exwm-xim--server-client-plist server-xwin) 2))
308-
req replies)
308+
replies)
309309
(cond ((= opcode xim:opcode:error)
310310
(exwm--log "ERROR: %s" data))
311311
((= opcode xim:opcode:connect)
@@ -314,15 +314,16 @@ The actual XIM request is in client message data or a property."
314314
;; Store byte-order.
315315
(setf (elt (plist-get exwm-xim--server-client-plist server-xwin) 2)
316316
xim:lsb)
317-
(setq req (xcb:unmarshal-new 'xim:connect data))
318-
(if (and (= (slot-value req 'major-version) 1)
319-
(= (slot-value req 'minor-version) 0)
320-
;; Do not support authentication.
321-
(= (slot-value req 'number) 0))
322-
;; Accept the connection.
323-
(push (make-instance 'xim:connect-reply) replies)
324-
;; Deny it.
325-
(push exwm-xim--default-error replies)))
317+
(with-slots (major-version minor-version number)
318+
(xcb:unmarshal-new 'xim:connect data)
319+
(if (and (= major-version 1)
320+
(= minor-version 0)
321+
;; Do not support authentication.
322+
(= number 0))
323+
;; Accept the connection.
324+
(push (make-instance 'xim:connect-reply) replies)
325+
;; Deny it.
326+
(push exwm-xim--default-error replies))))
326327
((memq opcode (list xim:opcode:auth-required
327328
xim:opcode:auth-reply
328329
xim:opcode:auth-next
@@ -367,55 +368,56 @@ The actual XIM request is in client message data or a property."
367368
xcb:EventMask:NoEvent))))
368369
((= opcode xim:opcode:close)
369370
(exwm--log "CLOSE")
370-
(setq req (xcb:unmarshal-new 'xim:close data))
371-
(push (make-instance 'xim:close-reply
372-
:im-id (slot-value req 'im-id))
373-
replies))
371+
(with-slots (im-id) (xcb:unmarshal-new 'xim:close data)
372+
(push (make-instance 'xim:close-reply :im-id im-id)
373+
replies)))
374374
((= opcode xim:opcode:trigger-notify)
375375
(exwm--log "TRIGGER-NOTIFY")
376376
;; Only static event flow modal is supported.
377377
(push exwm-xim--default-error replies))
378378
((= opcode xim:opcode:encoding-negotiation)
379379
(exwm--log "ENCODING-NEGOTIATION")
380-
(setq req (xcb:unmarshal-new 'xim:encoding-negotiation data))
381-
(let ((index (cl-position "COMPOUND_TEXT"
382-
(mapcar (lambda (i) (slot-value i 'name))
383-
(slot-value req 'names))
384-
:test #'equal)))
385-
(unless index
386-
;; Fallback to portable character encoding (a subset of ASCII).
387-
(setq index -1))
388-
(push (make-instance 'xim:encoding-negotiation-reply
389-
:im-id (slot-value req 'im-id)
390-
:category
391-
xim:encoding-negotiation-reply-category:name
392-
:index index)
393-
replies)))
380+
(with-slots (names im-id)
381+
(xcb:unmarshal-new 'xim:encoding-negotiation data)
382+
(let ((index (cl-position "COMPOUND_TEXT"
383+
(mapcar (lambda (i) (slot-value i 'name))
384+
names)
385+
:test #'equal)))
386+
(unless index
387+
;; Fallback to portable character encoding (a subset of ASCII).
388+
(setq index -1))
389+
(push (make-instance 'xim:encoding-negotiation-reply
390+
:im-id im-id
391+
:category
392+
xim:encoding-negotiation-reply-category:name
393+
:index index)
394+
replies))))
394395
((= opcode xim:opcode:query-extension)
395396
(exwm--log "QUERY-EXTENSION")
396-
(setq req (xcb:unmarshal-new 'xim:query-extension data))
397-
(push (make-instance 'xim:query-extension-reply
398-
:im-id (slot-value req 'im-id)
399-
;; No extension support.
400-
:length 0
401-
:extensions nil)
402-
replies))
397+
(with-slots (im-id)
398+
(xcb:unmarshal-new 'xim:query-extension data)
399+
(push (make-instance 'xim:query-extension-reply
400+
:im-id im-id
401+
;; No extension support.
402+
:length 0
403+
:extensions nil)
404+
replies)))
403405
((= opcode xim:opcode:set-im-values)
404406
(exwm--log "SET-IM-VALUES")
405407
;; There's only one possible input method attribute.
406-
(setq req (xcb:unmarshal-new 'xim:set-im-values data))
407-
(push (make-instance 'xim:set-im-values-reply
408-
:im-id (slot-value req 'im-id))
409-
replies))
408+
(with-slots (im-id)
409+
(xcb:unmarshal-new 'xim:set-im-values data)
410+
(push (make-instance 'xim:set-im-values-reply
411+
:im-id im-id)
412+
replies)))
410413
((= opcode xim:opcode:get-im-values)
411414
(exwm--log "GET-IM-VALUES")
412-
(let (im-attributes-id)
413-
(setq req (xcb:unmarshal-new 'xim:get-im-values data))
414-
(setq im-attributes-id (slot-value req 'im-attributes-id))
415+
(with-slots (im-attributes-id im-id)
416+
(xcb:unmarshal-new 'xim:get-im-values data)
415417
(if (cl-notevery (lambda (i) (= i 0)) im-attributes-id)
416418
;; Only support one IM attributes.
417419
(push (make-instance 'xim:error
418-
:im-id (slot-value req 'im-id)
420+
:im-id im-id
419421
:ic-id 0
420422
:flag xim:error-flag:invalid-ic-id
421423
:error-code xim:error-code:bad-something
@@ -425,45 +427,49 @@ The actual XIM request is in client message data or a property."
425427
replies)
426428
(push
427429
(make-instance 'xim:get-im-values-reply
428-
:im-id (slot-value req 'im-id)
430+
:im-id im-id
429431
:length nil
430432
:im-attributes exwm-xim--default-attributes)
431433
replies))))
432434
((= opcode xim:opcode:create-ic)
433435
(exwm--log "CREATE-IC")
434-
(setq req (xcb:unmarshal-new 'xim:create-ic data))
435-
;; Note: The ic-attributes slot is ignored.
436-
(setq exwm-xim--ic-id (if (< exwm-xim--ic-id #xffff)
437-
(1+ exwm-xim--ic-id)
438-
1))
439-
(push (make-instance 'xim:create-ic-reply
440-
:im-id (slot-value req 'im-id)
441-
:ic-id exwm-xim--ic-id)
442-
replies))
436+
(with-slots (im-id)
437+
(xcb:unmarshal-new 'xim:create-ic data)
438+
;; Note: The ic-attributes slot is ignored.
439+
(setq exwm-xim--ic-id (if (< exwm-xim--ic-id #xffff)
440+
(1+ exwm-xim--ic-id)
441+
1))
442+
(push (make-instance 'xim:create-ic-reply
443+
:im-id im-id
444+
:ic-id exwm-xim--ic-id)
445+
replies)))
443446
((= opcode xim:opcode:destroy-ic)
444447
(exwm--log "DESTROY-IC")
445-
(setq req (xcb:unmarshal-new 'xim:destroy-ic data))
446-
(push (make-instance 'xim:destroy-ic-reply
447-
:im-id (slot-value req 'im-id)
448-
:ic-id (slot-value req 'ic-id))
449-
replies))
448+
(with-slots (im-id ic-id)
449+
(xcb:unmarshal-new 'xim:destroy-ic data)
450+
(push (make-instance 'xim:destroy-ic-reply
451+
:im-id im-id
452+
:ic-id ic-id)
453+
replies)))
450454
((= opcode xim:opcode:set-ic-values)
451455
(exwm--log "SET-IC-VALUES")
452-
(setq req (xcb:unmarshal-new 'xim:set-ic-values data))
453-
;; We don't distinguish between input contexts.
454-
(push (make-instance 'xim:set-ic-values-reply
455-
:im-id (slot-value req 'im-id)
456-
:ic-id (slot-value req 'ic-id))
457-
replies))
456+
(with-slots (im-id ic-id)
457+
(xcb:unmarshal-new 'xim:set-ic-values data)
458+
;; We don't distinguish between input contexts.
459+
(push (make-instance 'xim:set-ic-values-reply
460+
:im-id im-id
461+
:ic-id ic-id)
462+
replies)))
458463
((= opcode xim:opcode:get-ic-values)
459464
(exwm--log "GET-IC-VALUES")
460-
(setq req (xcb:unmarshal-new 'xim:get-ic-values data))
461-
(push (make-instance 'xim:get-ic-values-reply
462-
:im-id (slot-value req 'im-id)
463-
:ic-id (slot-value req 'ic-id)
464-
:length nil
465-
:ic-attributes exwm-xim--default-attributes)
466-
replies))
465+
(with-slots (im-id ic-id)
466+
(xcb:unmarshal-new 'xim:get-ic-values data)
467+
(push (make-instance 'xim:get-ic-values-reply
468+
:im-id im-id
469+
:ic-id ic-id
470+
:length nil
471+
:ic-attributes exwm-xim--default-attributes)
472+
replies)))
467473
((= opcode xim:opcode:set-ic-focus)
468474
(exwm--log "SET-IC-FOCUS")
469475
;; All input contexts are the same.
@@ -474,28 +480,30 @@ The actual XIM request is in client message data or a property."
474480
)
475481
((= opcode xim:opcode:forward-event)
476482
(exwm--log "FORWARD-EVENT")
477-
(setq req (xcb:unmarshal-new 'xim:forward-event data))
478-
(exwm-xim--handle-forward-event-request req xim:lsb conn
479-
client-xwin))
483+
(exwm-xim--handle-forward-event-request
484+
(xcb:unmarshal-new 'xim:forward-event data)
485+
xim:lsb conn client-xwin))
480486
((= opcode xim:opcode:sync)
481487
(exwm--log "SYNC")
482-
(setq req (xcb:unmarshal-new 'xim:sync data))
483-
(push (make-instance 'xim:sync-reply
484-
:im-id (slot-value req 'im-id)
485-
:ic-id (slot-value req 'ic-id))
486-
replies))
488+
(with-slots (im-id ic-id)
489+
(xcb:unmarshal-new 'xim:sync data)
490+
(push (make-instance 'xim:sync-reply
491+
:im-id im-id
492+
:ic-id ic-id)
493+
replies)))
487494
((= opcode xim:opcode:sync-reply)
488495
(exwm--log "SYNC-REPLY"))
489496
((= opcode xim:opcode:reset-ic)
490497
(exwm--log "RESET-IC")
491498
;; No context-specific data saved.
492-
(setq req (xcb:unmarshal-new 'xim:reset-ic data))
493-
(push (make-instance 'xim:reset-ic-reply
494-
:im-id (slot-value req 'im-id)
495-
:ic-id (slot-value req 'ic-id)
496-
:length 0
497-
:string "")
498-
replies))
499+
(with-slots (im-id ic-id)
500+
(xcb:unmarshal-new 'xim:reset-ic data)
501+
(push (make-instance 'xim:reset-ic-reply
502+
:im-id im-id
503+
:ic-id ic-id
504+
:length 0
505+
:string "")
506+
replies)))
499507
((memq opcode (list xim:opcode:str-conversion-reply
500508
xim:opcode:preedit-start-reply
501509
xim:opcode:preedit-caret-reply))

0 commit comments

Comments
 (0)