Move and resize XMonad window - RationalRect call compiler error - haskell

to send the focused window to the center of the screen I have the following configuration
main = do
xmonad $ docks def
{ manageHook = myManageHook <+> manageHook def
, layoutHook = avoidStruts $ layoutHook def
, logHook = dynamicLogWithPP xmobarPP
, terminal = myTerminal
} `additionalKeys`
[ ((myModkey , xK_space), spawn myTerminal )
, ((myModkey , xK_0), withFocused (keysMoveWindowTo (512,384) (0, 0)))
]
I would remove the call to keysMoveWindowTo because it does not allow to set the window size (...) but only specify dx and dy; than I would like to use:
((myModkey , xK_0), withFocused (doRectFloat (RationalRect (1 % 4) (1 % 4) (1 % 2) (1 % 2))))
but the compiler says:
xmonad.hs:87:58: error:
Data constructor not in scope:
RationalRect
:: Ratio a0
87 |, ((myModkey , xK_0), withFocused (doRectFloat (RationalRect (1 % 4) (1 % 4) (1 % 2) (1 % 2))))
What is the correct way to bind keys with doRectFloat function?
Thanks
Nello

doRectFloat does not provide an X operation needed by withFocused.
Enhancing your previous solution, you could add keysResizeWindow to do the resizing, e.g.
, ((myModkey , xK_0), withFocused (
keysMoveWindowTo (512,384) (1%2, 1%2) >> keysResizeWindow (512, 384) (1%2, 1%2)
))

Related

xmonad NamedScratchpad not working as expected

I added NamedScratchpad configs but calling the scratchpad with key bindings is not displaying workspace and the syslog shows
Aug 28 20:20:52 username /usr/libexec/gdm-x-session[2206]: Prelude.head: empty list
my xmonad configs are:
myScratchPads :: [NamedScratchpad]
myScratchPads = [ NS "terminal" spawnTerm findTerm manageTerm
, NS "mocp" spawnMocp findMocp manageMocp
, NS "qalculate-qt" spawnCalc findCalc manageCalc
]
where
spawnTerm = myTerminal ++ " -n scratchpad"
findTerm = resource =? "scratchpad"
manageTerm = customFloating $ W.RationalRect l t w h
where
h = 0.9
w = 0.9
t = 0.95 -h
l = 0.95 -w
spawnMocp = myTerminal ++ " -n mocp 'mocp'"
findMocp = resource =? "mocp"
manageMocp = customFloating $ W.RationalRect l t w h
where
h = 0.9
w = 0.9
t = 0.95 -h
l = 0.95 -w
-- spawnCalc = "qalculate-gtk"
spawnCalc = "qalculate-qt"
-- findCalc = className =? "Qalculate-gtk"
findCalc = className =? "qalculate-qt"
manageCalc = customFloating $ W.RationalRect l t w h
where
h = 0.5
w = 0.4
t = 0.75 -h
l = 0.70 -w
myManageHook :: XMonad.Query (Data.Monoid.Endo WindowSet)
myManageHook =
def
<+> manageSmart
<+> manageDialog
<+> manageScratchPad
<+> manageWindows
where
manageSmart = placeHook simpleSmart
manageDialog = composeOne [ isDialog -?> doCenterFloat ]
manageScratchPad = namedScratchpadManageHook myScratchPads
manageWindows = composeAll
[ className =? "Google-chrome" --> doShift ( myWorkspaces !! 1 )
, className =? "Cypress" --> doShift ( myWorkspaces !! 1)
, className =? "Gimp" --> doShift ( myWorkspaces !! 8 )
, className =? "Gimp" --> doFloat
, title =? "Oracle VM VirtualBox Manager" --> doFloat
, className =? "VirtualBox Manager" --> doShift ( myWorkspaces !! 4 )
]
.....
--- shortcut keys
, ("M-t t", namedScratchpadAction myScratchPads "terminal")
, ("M-t m", namedScratchpadAction myScratchPads "mocp")
, ("M-t c", namedScratchpadAction myScratchPads "qalculate-qt")
I am using Debian 11 OS and everything else in xmonad configs is working fine.

Clickable workspaces

I'm using XMonad in combination with two xmobar instances, and I'm using IndependentScreens because I have a dual monitor setup. I'm having an issue with clickable workspaces ever since I introduced the second monitor. The thing is, IndependentScreens labels workspaces as 0_1, 1_1, 0_2, 1_2, ..., and the code I had worked only based on WorkspaceId, not ScreenId. I've compiled xmonad and xmonad-contrib from source so that I can use XMonad.Util.ClickableWorkspaces, however, the documentation is obscure and I couldn't find an example of proper usage anywhere. I've tried various things not worth mentioning, IMO.
This is my config:
import System.IO
import XMonad
import XMonad.Hooks.DynamicLog
import XMonad.Hooks.ManageDocks
import XMonad.Hooks.ServerMode
import XMonad.Hooks.SetWMName
import XMonad.Layout.IndependentScreens
import XMonad.Layout.Gaps
import XMonad.Layout.Spacing
import XMonad.Util.EZConfig (additionalKeysP)
import XMonad.Util.Run (spawnPipe)
import Data.Ord
import qualified XMonad.StackSet as W
import qualified Data.Map as M
import XMonad.Util.WorkspaceCompare
import XMonad.Util.ClickableWorkspaces
myLayout = gaps [(U, 10), (R, 10), (L, 10), (D, 10)] $ spacingRaw True (Border 0 10 10 10) True (Border 10 10 10 10) True $
layoutHook def
myWorkspaces =
[ (xK_1, "1")
, (xK_2, "2")
, (xK_3, "3")
, (xK_4, "4")
, (xK_5, "5")
, (xK_6, "6")
, (xK_7, "7")
, (xK_8, "8")
, (xK_9, "9")
, (xK_0, "10")
, (xK_minus, "11")
, (xK_equal, "12")
]
myKeys conf#(XConfig {XMonad.modMask = modMask}) = M.fromList $
[ ((modMask, key), windows $ onCurrentScreen W.greedyView ws)
| (key, ws) <- myWorkspaces
]
++
[ ((modMask .|. shiftMask, key), windows $ onCurrentScreen W.shift ws)
| (key, ws) <- myWorkspaces
]
++
[
-- Spawn the terminal
((modMask .|. shiftMask, xK_Return), spawn $ XMonad.terminal conf)
-- Spawn dmenu
, ((modMask, xK_p), spawn "dmenu_run")
-- Close focused window
, ((modMask .|. shiftMask, xK_c), kill)
-- Rotate through the available layout algorithms
, ((modMask, xK_space ), sendMessage NextLayout)
-- Reset the layouts on the current workspace to default
, ((modMask .|. shiftMask, xK_space), setLayout $ XMonad.layoutHook conf)
-- Resize viewed windows to the correct size
, ((modMask, xK_n), refresh)
-- Move focus to the next window
, ((modMask, xK_Tab), windows W.focusDown)
-- Move focus to the next window
, ((modMask, xK_j), windows W.focusDown)
-- Move focus to the previous window
, ((modMask, xK_k), windows W.focusUp)
-- Move focus to the master window
, ((modMask, xK_m), windows W.focusMaster)
-- Swap the focused window and the master window
, ((modMask, xK_Return), windows W.swapMaster)
-- Swap the focused window with the next window
, ((modMask .|. shiftMask, xK_j), windows W.swapDown)
-- Swap the focused window with the previous window
, ((modMask .|. shiftMask, xK_k), windows W.swapUp)
-- Shrink the master area
, ((modMask, xK_h), sendMessage Shrink)
-- Expand the master area
, ((modMask, xK_l), sendMessage Expand)
-- Push window back into tiling
, ((modMask, xK_t), withFocused $ windows . W.sink)
-- Increment the number of windows in the master area
, ((modMask, xK_comma), sendMessage (IncMasterN 1))
-- Deincrement the number of windows in the master area
, ((modMask, xK_period), sendMessage (IncMasterN (-1)))
-- toggle the status bar gap
, ((modMask, xK_b), sendMessage ToggleStruts)
-- Restart xmonad
, ((modMask, xK_q), broadcastMessage ReleaseResources >> restart "xmonad" True)
]
myAdditionalKeysP =
[
("M-<F2>", spawn "thunar")
, ("M-<F3>", spawn "firefox")
, ("M-<F4>", spawn "code")
, ("M-<F5>", spawn "thunderbird")
, ("M-<Escape>", spawn "xfce4-appfinder")
, ("M4-<Print>", spawn "xfce4-screenshooter")
, ("M4-<KP_Add>", spawn "amixer -D pulse sset Master 5%+")
, ("M4-<KP_Subtract>", spawn "amixer -D pulse sset Master 5%-")
, ("M-C-p", spawn "passmenu")
, ("M-C-c", spawn "clipmenu")
, ("M-C-m", spawn "mailwatch_restart")
, ("M-C-x", spawn "xfce4-panel -r")
, ("M-C-<Left>", spawn "playerctl previous")
, ("M-C-<Right>", spawn "playerctl next")
, ("M-C-<Space>", spawn "playerctl play-pause")
]
clickable' :: WorkspaceId -> String
clickable' w = xmobarAction ("xmonadctl view\\\"" ++ w ++ "\\\"") "1" w
compareNumbers = comparing (read :: String -> Int)
pp h s = marshallPP s def
{ ppOutput = hPutStrLn h
, ppCurrent = xmobarColor "blue" "" . wrap "[" "]"
, ppHiddenNoWindows = xmobarColor "grey" "" . clickable'
, ppVisible = wrap "(" ")"
, ppUrgent = xmobarColor "red" "yellow"
, ppOrder = \(ws:_:_:_) -> [pad ws]
, ppHidden = clickable'
, ppSort = mkWsSort $ return compareNumbers
}
main = do
xmprocs <- mapM (\i -> spawnPipe $ "xmobar ~/.config/xmobar/xmobarrc-" ++ show i ++ " -x" ++ show i) [0..1]
xmonad $ docks def
{
workspaces = withScreens 2 (map show [1..12])
, keys = myKeys
, borderWidth = 2
, focusedBorderColor = "#226fa5"
, normalBorderColor = "#191919"
, handleEventHook = serverModeEventHookCmd
<+> serverModeEventHook
<+> serverModeEventHookF "XMONAD_PRINT" (io . putStrLn)
, layoutHook = avoidStruts myLayout
, logHook = mapM_ dynamicLogWithPP $ zipWith pp xmprocs [0..1]
, startupHook = setWMName "LG3D"
, manageHook = manageDocks
} `additionalKeysP` myAdditionalKeysP
How can I properly use clickablePP with my setup, or whatever is needed to make the workspaces clickable?
This guy has this in his xmonad.hs
myClickableWorkspaces :: [String]
myClickableWorkspaces = clickable . (map xmobarEscape)
-- $ [" 1 ", " 2 ", " 3 ", " 4 ", " 5 ", " 6 ", " 7 ", " 8 ", " 9 "]
$ [" dev ", " www ", " sys ", " doc ", " vbox ", " chat ", " mus ", " vid ", " gfx "]
where
clickable l = [ "<action=xdotool key super+" ++ show (n) ++ ">" ++ ws ++ "</action>" |
(i,ws) <- zip [1..9] l,
let n = i ]
and in his xmobarrc
, commands = [
...
-- The workspaces are 'clickable' in my configs.
, Run UnsafeStdinReader
]
, template = " <action=`xdotool key control+alt+g`>...
and it looks that works, at least for him. You'll need 'xdotool' to make this work, in arch you can find it in the community repo, or clone it from here.
Something like this should work, I think:
clickable' :: ScreenId -> VirtualWorkspace -> String
clickable' s w = xmobarAction ("xmonadctl view\\\"" ++ marshall s w ++ "\\\"") "1" w
pp h s = marshallPP s def
{ ppHiddenNoWindows = xmobarColor "grey" "" . clickable' s
, -- and the other stuff
}
I haven't tested it, though...

drawing the game board in Haskell - problem

I'm trying to build the board for the Peg Solitaire game but I'm stuck. I hope you can help me. The following code runs and generates a square of 3 on 3 circles. How could I make 3 more such squares but put them in other positions? I use the Gloss library
module Main(main) where
import Graphics.Gloss
import Graphics.Gloss.Data.ViewPort
import Graphics.Gloss.Interface.Pure.Game
import Data.List
width, height, offset :: Int
width = 400
height = 400
offset = 100
window :: Display
window = InWindow "Peg Solitaire" (width, height) (offset, offset)
background :: Color
background = white
drawing :: Picture
drawing = Pictures [ (translate (x * 40) (y * 40) $ circleSolid 12)| x<-[-1..1], y<-[2..4] ]
main = display window background drawing
I am not familiar with your graphic library, but apparently you can use a list comprehension and pass it to the Pictures constructor.
So it is just a matter of writing the appropriate list comprehension expression.
The expression you give can be rewritten as:
drawing1 = let circles1 = [ (translate (x1 * 40) (y1 * 40) $ circleSolid 12) |
x1 <- [-1..1], y1 <- [2..4] ] in Pictures circles1
If you would like to arrange your 3+1=4 circle groups into a regular grid, you can introduce extra loop levels, say with variables x0 and y0, like this:
drawing2 = let circles2 = [ (translate (x0*200 + x1*40) (y0*200 + y1*40)
$ circleSolid 12) |
x0 <- [0,1] , y0 <- [0,1],
x1 <- [-1..1], y1 <- [2..4] ]
in Pictures circles2
If you prefer to arrange the circle groups in arbitrary fashion, you can introduce one extra loop variable, say cg which is to run over the main coordinates of the circle groups:
circleGroups = [ (0,0), (0,200), (200,0), (200,200) ] -- list of (x,y) pairs
drawing3 = let circles3 = [
translate ((fst cg)*200 + x1*40) ((snd cg)*200 + y1*40)
$ circleSolid 12 |
cg <- circleGroups,
x1 <- [-1..1], y1 <- [2..4] ]
in Pictures circles3
Note: Please please limit your source code to about 80 characters per line, so we don't have to use the horizontal slider. This is really a massive hindrance to code readability. Thanks.

Fix arrowShaft (Diagrams Library)

I've made a data storage symbol using B.difference and B.union. The one with the red dot in the middle.
dataStorage :: Diagram B
dataStorage = (strokePath $ B.difference Winding combined block1) # translateX (-0.3)
where block1 = (circle 0.5) # scaleX 0.5 # translateX (-1)
block2 = rect 2 1
block3 = (circle 0.5) # translateX (1)
combined = B.union Winding $ block2 <> block3
I've been trying for hours now but can't make an arrow straight between Previous Estimate written inside that symbol and Signal Decomposition (SSA). The goal is to draw the arrow starting at the center right outside of the symbol. Any help is welcome. Thank you very much.
EDIT 1: Added wanted result.
Here's the complete code.
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
module FlowChartTR where
import System.Process
--import Graphics.SVGFonts
import Diagrams.Backend.SVG.CmdLine
import Diagrams.Prelude
import Diagrams.TwoD.Arrow
import qualified Diagrams.TwoD.Path.Boolean as B
oneLineText txt = text txt
twoLineText txt1 txt2 = center $ (text txt1) === strutY 0.2 === (text txt2)
threeLineText txt1 txt2 txt3 = center $
(text txt1) === strutY 0.2 === (text txt2) === strutY 0.2 === (text txt3)
terminal writeText w h r = (writeText <> roundedRect w h r) # lwL 0.02 # fontSize (local 0.2)
--terminalInput = (text "Input Data" <> roundedRect 1 0.3 0.3) # lwL 0.02 # fontSize (local 0.2)
--process txt w h = (text txt <> rect w h) # lwL 0.02 # fontSize (local 0.2)
process writeText w h = (writeText <> rect w h) # lwL 0.02 # fontSize (local 0.2)
dataStorage :: Diagram B
dataStorage = (strokePath $ B.difference Winding combined block1) # translateX (-0.3)
where block1 = (circle 0.5) # scaleX 0.5 # translateX (-1)
block2 = rect 2 1
block3 = (circle 0.5) # translateX (1)
combined = B.union Winding $ block2 <> block3
--decision :: Diagram B
--decision = (text "BPM" <> rect 0.4 0.3) # lwL 0.02 # fontSize (local 0.2)
input = (terminal (oneLineText "Input Data") 1.2 0.3 0.3) # named "terminalInput"
bandpass = (process (twoLineText "Bandpass" "Filtering") 1.5 0.5) # named "bandpass"
ssa = (process (threeLineText "Signal" "Decomposition" "(SSA)") 1.5 1) # translateY (-0.3) # named "ssa" # showOrigin
td = (process (twoLineText "Temporal" "Difference") 1 0.5) # named "td"
focuss = (process (threeLineText "Sparse Signal" "Reconstruction" "(FOCUSS)") 1.5 0.8) # named "focuss"
outputBPM = (terminal (oneLineText "Output BPM") 1.2 0.3 0.3) # named "terminalOutput"
spt = (process (threeLineText "Spectral Peak" "Tracking" "Select & Verif") 1.5 0.8) # named "spt"
prior = (oneLineText "Previous Estimate" <> dataStorage) # fontSize (local 0.2) # named "prior" #showOrigin # translateY 1
arrowStyle = (with & arrowHead .~ dart & headLength .~ large & tailLength .~ veryLarge)
ushaft = trailFromVertices (map p2 [(0, 0), (0.5, 0), (0.5, 1), (1, 1)])
arrowStyleU = (with & arrowHead .~ dart & headLength .~ large & tailLength .~ veryLarge & arrowShaft .~ ushaft)
decision :: Diagram B
decision = square 5 # rotate (45 ## deg) # scaleY 0.5
placeBlocks :: Diagram B
placeBlocks = atPoints [ P (V2 0 0), -- input
P (V2 4 0), -- bandpass
P (V2 8 0), -- ssa
P (V2 8 (-2)), -- td
P (V2 8 (-4)), -- focuss
P (V2 4 (-4)), -- spt
P (V2 0 (-4)), -- outputBPM
P (V2 4 (-2)) -- prior
] [input, bandpass,ssa, td, focuss, spt, outputBPM, prior]
flowChart :: Diagram B
flowChart = placeBlocks # connectOutside' arrowStyle "terminalInput" "bandpass"
# connectOutside' arrowStyle "bandpass" "ssa"
# connectOutside' arrowStyle "ssa" "td"
# connectOutside' arrowStyle "td" "focuss"
# connectOutside' arrowStyle "focuss" "spt"
# connectOutside' arrowStyle "spt" "terminalOutput"
# connectOutside' arrowStyle "prior" "spt"
# connectOutside' arrowStyleU "prior" "ssa"
# pad 1.1
flowChartTR :: IO ()
flowChartTR = mainWith flowChart
I got it. After I scaled down the symbol It becomes easier to adjust the connection.
Here's the changes.
...
[input, bandpass,ssa, td, focuss, spt, outputBPM, (prior # scale 0.7)]
...
# connectPerim' arrowStyleU "prior" "ssa" (0 ## deg) (205 ## deg)
...
NOTE:
- Adding arrowTail .~ lineTail is critical.

How can I set a random seed using the jags() function?

Each time I run my JAGS model using the jags() function, I get very different values of fitted parameters. However, I want other people to reproduce my results.
I tried to add set.seed(123), but it didn't help. This link describes how to achieve my goal using the run.jags() function. I wonder how I can do similar things using jags(). Thank you!
Below is my model in R:
##------------- read data -------------##
m <- 6
l <- 3
node <- read.csv("answer.csv", header = F)
n <- nrow(node)
# values of nodes
## IG
IG <- c(c(0.0, 1.0, 0.0), c(0.0, 0.0, 1.0), c(1.0, 0.0, 0.0), c(1.0, 0.0, 0.0), c(0.0, 1.0, 0.0), c(0.0, 0.0, 1.0))
IG <- matrix(IG, nrow=6, ncol=3, byrow=T)
V_IG <- array(0, dim=c(n, m, l))
for (i in 1:n){
for (j in 1:m){
for (k in 1:l)
{
V_IG[i,j,k] <- IG[j,k] # alternatively, V[i,j,k] <- PTS[j,k]
}
}
}
## PTS
PTS <- c(c(1.0, 0.5, 0.0), c(1.0, 0.0, 0.5), c(1.0, 1.0, 0.0), c(1.0, 0.0, 1.0), c(0.0, 0.5, 1.0), c(0.0, 1.0, 0.5))
PTS <- matrix(PTS, nrow=m, ncol=3, byrow=T)
V_PTS <- array(0, dim=c(n, m, l))
for (i in 1:n){
for (j in 1:m){
for (k in 1:l)
{
V_PTS[i,j,k] <- PTS[j,k]
}
}
}
##------------- fit model -------------##
set.seed(123)
data <- list("n", "m", "V_IG", "V_PTS", "node")
myinits <- list(list(tau = rep(1,n), theta = rep(0.5,n)))
parameters <- c("tau", "theta")
samples <- jags(data, inits=myinits, parameters,
model.file ="model.txt", n.chains=1, n.iter=10000,
n.burnin=1, n.thin=1, DIC=T)
And my model file model.txt:
model{
# data: which node (1, 2, 3) was chosen by each child in each puzzle
for(i in 1:n) # for each child
{
for (j in 1:m) # for each problem
{
# node chosen
node[i,j] ~ dcat(mu[i,j,1:3])
mu[i,j,1:3] <- exp_v[i,j,1:3] / sum(exp_v[i,j,1:3])
for (k in 1:3) {
exp_v[i,j,k] <- exp((V_IG[i,j,k]*theta[i] + V_PTS[i,j,k]*(1-theta[i]))/tau[i])
}
}
}
# priors on tau and theta
for (i in 1:n)
{
tau[i] ~ dgamma(0.001,0.001)
theta[i] ~ dbeta(1,1)
}
}
I know this is an older question, but for anyone using the jagsUI package, the jags() function has an argument for setting the seed, 'seed = ####'. So for example, a JAGS call could be;
np.sim1 <- jags(data = data1, parameters.to.save = params1, model.file = "mod1_all.txt",
n.chains = nc, n.iter = ni, n.burnin = nb, n.thin = nt, seed = 4879)
summary(np.sim1)
Here is a toy example for linear regression. First the model:
model{
a0 ~ dnorm(0, 0.0001)
a1 ~ dnorm(0, 0.0001)
tau ~ dgamma(0.001,0.001)
for (i in 1:100) {
y[i] ~ dnorm(mu[i], tau)
mu[i] <- a0 + a1 * x[i]
}
}
Now we generate some data and you the set.seed function to generate identical results from multiple calls to the jags function.
# make the data and prepare what we need to fit the model
x <- rnorm(100)
y <- 1 + 1.2 * x + rnorm(100)
data <- list("x", "y")
parameters <- c("a0", "a1", "tau")
inits = list(list(a0 = 1, a1=0.5, tau = 1))
# First fit
set.seed(121)
samples <- jags(data, inits,
parameters,model.file = "./sov/lin_reg.R",
n.chains = 1, n.iter = 5000, n.burnin = 1, n.thin = 1)
# second fit
set.seed(121) # with set.seed at same value
samples2 <- jags(data, inits,
parameters,model.file = "./sov/lin_reg.R",
n.chains = 1, n.iter = 5000, n.burnin = 1, n.thin = 1)
If we pull out the draws for one of the parameters from samples and samples2 we can see that they have generated the same values.
a0_1 <- samples$BUGSoutput$sims.list$a0
a0_2 <- samples2$BUGSoutput$sims.list$a0
head(cbind(a0_1, a0_2))
[,1] [,2]
[1,] 1.0392019 1.0392019
[2,] 0.9155636 0.9155636
[3,] 0.9497509 0.9497509
[4,] 1.0706620 1.0706620
[5,] 0.9901852 0.9901852
[6,] 0.9307072 0.9307072

Resources