Haskell Persistent note recording program

Here’s a version of the previous notes program that uses Persistent. The imports are:

  • text
  • cmdargs
  • friendly-time
  • time
  • persistent
  • persistent-sqlite
  • persistent-template
  • transformers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}

module Main where

import System.Console.CmdArgs
import Control.Monad (when, void)
import Database.Persist
import Database.Persist.TH
import Database.Persist.Sqlite
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Reader
import Data.Time
import Data.List (intercalate)
import Data.Time.Format.Human

share
    [mkPersist sqlSettings, mkMigrate "migrateAll"]
    [persistLowerCase|
Note
    message String
    tags String Maybe
    createdAt UTCTime default=CURRENT_TIME
    deriving Show
|]

data Options = Options
    { inputMode :: Bool
    , message :: String
    , delete :: Integer
    , humanTime :: Bool
    , tags :: Maybe String
    , update :: Integer
    , showTag :: Maybe String
    } deriving (Data, Typeable)

options :: Options
options =
    Options
    { inputMode = False &= typ "Input mode" &= help "Toggle input mode"
    , message = def &= typ "message" &= opt ("" :: String) &= args
    , Main.delete = def &= typ "[number]" &= help "Delete a note."
    , humanTime =
        False &= typ "Relative time" &= help "Show time relative to now in human readable format."
    , tags = def &= typ "tags" &= help "Tag a note."
    , Main.update = def &= typ "[number]" &= help "Modify a note."
    , showTag = def &= typ "TAG" &= help "Show entries with given tag."
    } &=
    summary "n notes" &=
    program "n"

showNote :: Bool -> Entity Note -> IO ()
showNote humanizeTime note = do
    let n = entityVal note
    let t' = noteCreatedAt n
    let key = show $ fromSqlKey $ entityKey note
    time <-
        if humanizeTime
            then humanReadableTime t'
            else return $ show t'
    t <-
        case noteTags n of
            Nothing -> return ""
            Just xs -> return $ "#" ++ xs
    let o = intercalate " | " [key, time, show (noteMessage n), t]
    print o


asSqlBackendReader :: ReaderT SqlBackend m a -> ReaderT SqlBackend m a
asSqlBackendReader = id

main :: IO ()
main =
    runSqlite "testn.db" $
    asSqlBackendReader $
    do opts <- liftIO $ cmdArgs options
       runMigration migrateAll
       time <- liftIO getCurrentTime
       when (message opts /= "") $ void $ insert $ Note (message opts) (tags opts) time
       when
           (inputMode opts)
           (do content <- liftIO getContents
               void $ insert $ Note content (tags opts) time
               return ())
       when
           (Main.delete opts /= 0)
           (do let key = toSqlKey $ fromIntegral (Main.delete opts) :: Key Note
               Database.Persist.Sqlite.delete key)
       notes <-
           case showTag opts of
               Nothing -> selectList [] [Asc NoteCreatedAt]
               Just x -> selectList [NoteTags ==. Just x] [Asc NoteCreatedAt]
       if humanTime opts
           then liftIO $ mapM_ (showNote True) notes
           else liftIO $ mapM_ (showNote False) notes

Improving basic haskell programs

Using the symbols >>= (bind) <$> (fmap) and <*> we can improve our Haskell programs. For example:

1
2
3
4
5
6
7
8
9
extractDomain :: Text -> Text
extractDomain url =
    case parseURI (unpack url) of
        Nothing -> ""
        Just uri ->
            case uriAuthority uri of
                Nothing -> ""
                Just auth -> pack $ uriRegName auth

1
2
3
4
5
extractDomainB :: Text -> Text
extractDomainB url = fromMaybe "" $ parseURI (unpack url)
                     >>= uriAuthority
                     >>= pure . pack . uriRegName

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- | Retrieve the members of a set
sout :: Connection -> ByteString -> IO [ByteString] -- (Either Reply [ByteString])
sout conn k = do
  members <-
    liftIO $
    runRedis conn $ do
      res <- (smembers k)
      mem <-
        case res of
          Left _ -> return [] -- Hmm
          Right m -> return m
      return mem
  return members
1
2
3
4
-- | Retrieve the members of a set
sout :: Connection -> ByteString -> IO [ByteString]
sout conn k = 
    runRedis conn $ either (return []) id <$> smembers k

Markdown and syntax highlighting

Jekyll and escape sequences can be strange. With Github Flavored Markdown, it is possible to set syntax highlighting in fenced code blocks like so:

1
2
3
```haskell
putStrln "Hello World!"
```

But what about code blocks that are made up using four initial spaces on each line? Like so: <pre> print "bar" </pre>. The only option is to add {% highlight haskell %} and {% endhighlight %}` after:

    print "bar

Blocks of code are either fenced by lines with three back-ticks ```, or are indented with four spaces. I recommend only using the fenced code blocks – they’re easier and only they support syntax highlighting. – Markdown Cheatsheet

You can check out the source code of this blog post also.

Sidenote: Make sure to keep your post in UTF-8 format or Jekyll might spaz out.

References

Note recording Haskell program

A small Haskell program that manages short notes in a SQLite database. Uses sqlite-simple and CmdArgs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveDataTypeable #-}

module Main where

import System.Environment (getArgs)
import Database.SQLite.Simple
import System.Console.CmdArgs
import Data.List hiding (delete)
import Data.Time.Format.Human
import Data.Time
import Data.Time.Format
import Control.Monad (when)

data Options = Options
    { inputMode :: Bool
    , message :: String
    , delete :: Integer
    , humanTime :: Bool
    , tags :: Maybe String
    , update :: Integer
    , showTag :: Maybe String
    } deriving (Data, Typeable)

options :: Options
options =
    Options
    { inputMode = False &= typ "Input mode" &= help "Toggle input mode"
    , message = def &= typ "message" &= opt ("" :: String) &= args
    , delete = def &= typ "[number]" &= help "Delete a note."
    , humanTime =
        False &= typ "Relative time" &= help "Show time relative to now in human readable format."
    , tags = def &= typ "tags" &= help "Tag a note."
    , update = def &= typ "[number]" &= help "Modify a note."
    , showTag = def &= typ "TAG" &= help "Show entries with given tag."
    } &=
    summary "n notes" &=
    program "n"

data TestField = TestField
    { id_ :: Int
    , time :: String
    , note :: String
    , tags' :: Maybe String
    } deriving (Show)

instance FromRow TestField where
    fromRow = TestField <$> field <*> field <*> field <*> field

instance ToRow TestField where
    toRow (TestField id_ date_added str str') = toRow (id_, date_added, str, str')

dbPut :: Connection -> String -> Maybe String -> IO ()
dbPut conn val mTags =
    case mTags of
        Nothing ->
            execute
                conn
                "INSERT INTO test (date_added, str) \
        \VALUES (datetime('now'), ?)"
                (Only (val :: String))
        tags ->
            execute
                conn
                "INSERT INTO test (date_added, str, tags) \
        \VALUES (datetime('now'), ?, ?)"
                (val :: String, tags)

deleteRecord :: Connection -> Integer -> IO ()
deleteRecord conn x = execute conn "DELETE FROM test WHERE ID = (?)" (Only x)

showNote :: Bool -> TestField -> IO ()
showNote humanizeTime x = do
    time' <- if humanizeTime then
        do let t = parseTimeOrError False defaultTimeLocale "%F %T" (time x) :: UTCTime
           humanReadableTime t
    else
           return (time x)
    t <-
        case tags' x of
            Nothing -> return ""
            Just xs -> return $ "#" ++ xs

    let msg = note x ++ " " ++ t
    let o = intercalate " | " [show (id_ x), time', msg]
    print o

createTable conn =
    execute_
        conn
        "CREATE TABLE IF NOT EXISTS \
        \test (id INTEGER PRIMARY KEY, \
        \date_added DATETIME DEFAULT CURRENT_TIMESTAMP, \
        \str TEXT, \
        \tags TEXT)"

main :: IO ()
main = do
    opts <- cmdArgs options
    conn <- open "test.db"
    createTable conn
    when (delete opts /= 0) (deleteRecord conn (delete opts))
    when (message opts /= "") (dbPut conn (message opts) (tags opts))
    when
        (inputMode opts)
        (do content <- getContents
            dbPut conn content (tags opts))
    r <-
        case showTag opts of
            Nothing -> query_ conn "SELECT * from test" :: IO [TestField]
            Just x -> query conn "SELECT * from test WHERE tags LIKE ?" (Only x) :: IO [TestField]
    if humanTime opts
        then mapM_ (showNote True) r
        else mapM_ (showNote False) r
    close conn

From org to markdown

Configure Emacs to enable orgmode markdown exports for Jekyll use.

Code blocks

1
putStrln "Hello World!"

Use the org-export-dispatch (C-c e) and export to a temporary buffer.

Add highlighting tags, eg {% highlight python %} … {% endhighlight %} for non-default languages.

    print("Hello world!")

Add the YAML front-matter to make Jekyll display the post.

    ---

    title: "From org to markdown

    tags: "meta"

    ---

Add your configured excerpt_separator e.g: <!--more--> to the post.

VSCode Insiders 2017 Review - Coming from Emacs

How I set up VSCode

  • I hit C-S-p, selected color theme and chose Solarized Dark
  • C-,, opened settings where I set the font Hasklig, and enabled font ligatures.
  • Installed extensions relevant for the programming modes I use.

Things I immediately miss from Emacs:

  • dired
  • the minibuffer, e.g. open a file from minibuffer.

Customization

  • Coming from Emacs, I have the notion that mostly anything that can be thought up can ultimately be customized (via Emacs-Lisp)1,

Things I sort of miss from Emacs

  • org-mode - Always having your note taking tool available (e.g. org-capture).

Things I like about VSCode

  • Built in Zen mode. I like keeping my screen clear from everything but the text sometimes.
  • Section folding built in.
  • The settings configuration mode. It’s quite minimal and smart. Focus in on creating a distributable text file for settings.
  • Markdown preview built in.

Things I strongly dislike about VSCode

  • The hard blue color of the status bar can not be themed as of writing, I read this is about to soon be changed though.

Working in Haskell

  • Working in the programming language Haskell with some extensions, depending on Intero seems to work fine. Terminal integration is nice and auto completion in GHCi does work.

Performance

  • In comparison to Atom editor (or LightTable), VSCode launches much faster and seems to perform a lot better. I am surprised that a program based on Electron can perform this well.

What you’re giving up when moving from Emacs to VSCode

  • Using the mouse. Emacs doesn’t need one. VSCode will sometimes need mouse interaction. This can impact workflow.

What’s to be gained from using VScode over Emacs

  • Fonts may look better.
  • Performance may be more reliable.

Conclusion,

  • I’m seriously impressed by how mature of an editor VSCode is. I really think they should replace the default “open file” panes with something better (compare Emacs minibuffer, ido and such). I had tried Atom and managed to customize it to a functioning state, but really slow startup times and performance reliability made that editor feel bloated and unacceptably slow and I went back to GNU Emacs.
  • The performance characteristics of GNU Emacs running on Windows are however quite poor in my experience and I even looked at minimal retro editors such as micro-emacs to always have a fast reliable text editor which always start up in under a second. MicroEmacs achieve that but the downsides are many - apart from all the possible functionality one gives up, making text look decent can be very hard, near impossible. Perhaps running a microEmacs in a good terminal emulator is a smart choice, but if you’re on Windows for whatever reason, there really isn’t a good terminal emulator available.

Update, April 1st, 2017:

  • For quickly opening files, there are extensions such as:

Update, April 2nd, 2017:

  1. Of course, the limitation of emacs-lisp are there. Some things in Emacs are after all decided in compiled C code. 

Heroku staging to production

  1. Fork your app

    heroku fork --from myapp --to staging-myapp

  2. Create a heroku pipeline for your app heroku pipelines:create -a myapp
  3. Add the staging app to that pipeline heroku pipelines:add -a myapp-staging myapp
  4. Change the heroku remote to be that of your staging app heroku git:remote -a fedry-staging
  5. From now on we'll be pushing code to staging so your workflow (may be) is: git push heroku master

    .. verify app behavior (heroku open)

    heroku pipelines:promote to send staging -> production without the need for recompile.

More here, and here.

You will now be much less likely to put your "production" app in a broken state.

Note that from now on, your myapp-staging is the target for most of your heroku commands, so if you want to view your production logs - for example - you'll want heroku logs -a myapp.

(f)map with arguments

To map over (\x y -> do print x; putStrLn y):

map (\x -> foo x "my string argument")
map (flip foo "my string argument")

-- < jle`> if you can re-write foo to take the string arugment first, it's even simpler
map (foo "my string argument") [1..10]

-- < MarcelineVQ> if that's inconvenient you can also section it:  
map (`foo`"my string argument") [1..10]



23:21 < jmnoz> so it's a good idea to keep possible mapping in mind when setting the argument order (designing the function) then?
23:23 < jle`> jmnoz: actually, yes :)
23:23 < jle`> a lot of people choose argument orders based on what they expect to partially apply

23:23 < jmnoz> hmm also do you use map/fmap interchangeably?
23:23 < jle`> i usually use map for lists, it tends to be slightly more readable
23:23 < jle`> and it can help with error messages sometimes

How to install a recent package in haskell stack

  • create a new project: stack new foo-latest
  • edit foo-latest/stack.yaml, add any needed packages to extra-deps
  • do stack install somepackage-1.20.0 with the package version specified.

This can be useful when LTS (long term support) is lagging behind.

Note: stack might suggest editing the global project stack.yaml, but this might not be the best thing to do.

Emacs: Code folding

Call vimish-fold-toggle when fold-dwim-org/toggle is called.

;; Toggle vimish-fold with Tab key.
(defadvice fold-dwim-org/toggle (after my-fold-dwim-org/toggle (&optional key lst-point) activate)
  "Call vimish-fold-toggle after fold-dwim-org/toggle"
  (vimish-fold-toggle)
  )

(Source)

See Also:

  • fold-dwim-org
  • vimish-fold

Change Caps Lock to Control key on Windows 10

Nothing has changed. Install/Run/Execute/Apply/Random Verb ChangeCapsToControl.reg from here: http://www.howtogeek.com/howto/windows-vista/disable-caps-lock-key-in-windows-vista/

The .reg-file looks like so:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1d,00,3a,00,00,00,00,00

How to deshake (stabilize) video in postprocessing using Virtualdub for Windows

The Deshaker plugin by Gunnar Thalin for Virtualdub on Windows runs over two passes and aims to stabilize shaky video. Here's a small writeup on how to use it.

  • Get virtualdub from virtualdub.org (64 bit worked fine for me)
  • Get ffdshow from ffdshow-tryouts
  • Get the deshaker plugin from http://www.guthspot.se/video/deshaker.htm
  • Extract virtualdub somewhere where you can easily find it,
  • Extract the deshaker.vdf plugin in virtualdubs plugins subdirectory
  • Open virtualdub
  • Go to the menu Audio/Interleaving (Ctrl-I)
  • Enable audio/video interleaving and in Audio Block placement, set "Interleave audio every" to 1000 ms. Click OK.
  • Go to the menu Video/Filters (Ctrl+F) click the button "Add" on the right hand side.
  • Select "Deshaker" and click OK (My version was v3.1).
  • In the "Filter: Deshaker" Options, Make sure "Pass 1" is selected in "Select Processing pass",
    • Note: There are interesting options here that controls quality.
    • Click "OK" - and Click "OK in the "Filters" Dialog to close it.
  • Now there are two play buttons in the bottom left controls of Virtualdub, Click the second one that has a zero below it - This will start the first pass of filter rendering.
  • After this pass has finished running, Rewind the Video to the beginning using the bottom control button (|<).

Now it's time to configure and run the second pass of the deshaker plugin run.

  • Go to the menu Video/Filters (Ctrl+F)
  • Uncheck the deshaker Pass 1 filter in the checkbox.
  • On the right hand side, Click Add and select "Deshaker" and click OK, This time in "Select processing pass", Make sure to click the right button which reads "Pass 2", and click "OK"
  • Make sure that only the Pass 2 filter is ticked in the "Filters" dialog.
    • Note that there are interesting options here to configure borders and such
    • Press "OK.
  • Run the pass two rendering using the Play-Zero in the bottom controls.

You can now use the menu "File/Save as AVI" (shortcut key F7) to save the deshaked file.

This article has pretty much been a transcription of "JayPlays" tutorial video on using Virtualdub Deshaker so thanks to them!

No more Firefox bookmarklets: A workaround

There is a bug[1] preventing bookmarklets from working correctly in Firefox. To work around this bug, it is possible to use extensions, which can execute all (including potentially unsafe) browser code. One option is to use the "Custom buttons" extension[2] and create custom buttons which execute your Javascript code.

My example case: In my .keysnailrc I have this function:

ext.add("bookmark-with-org",
        function (aEvent, aArg) {
          var od = window.content.document;
          window.loadURI('org-protocol://capture://f/'+encodeURIComponent(od.location.href)+'/'+encodeURIComponent(od.title)+'/'+encodeURIComponent(window.content.getSelection()));
}, M({en: "Save bookmark in Emacs org-mode using org-protocol"}));

Running the function sends a bookmark to Emacs org-mode capture.

To call the function from a custom button, I use this code:

window.KeySnail.modules.ext.exec("bookmark-with-org",null);

References

[1] 866522 – bookmarklets affected by CSP. [ bib | http ]
[2] Custom buttons :: Add-ons for firefox. [ bib | http ]

Use Emacs org-protocol capture with Firefox input

Org-protocol can be used to send data from a browser to Emacs. For example, you can use a bookmarklet to send the current URL, title and selection from the browser into Emacs org-mode capture.

On a Windows system I found that this works fine with Emacs UTF-8 settings when using Chrome and IE, but the data sent from Firefox was not UTF-8 (or utf-16-le) encoded. To handle this I used a special capture template for Firefox:

(setq org-capture-templates
 '(
   ("f" "Capture bookmark from firefox" entry (file+datetree "~/org/notes.org") 
    "* %?\n:PROPERTIES:\n:CODING: cp1252\n:END:\n%i\n  %a" :empty-lines 1)
   )
 )

A function which runs after content is captured checks for the "CODING" property, if it is there it recodes the buffer and removes the property:

;; If the org property value of CODING is not nil, mark the (capture) buffer it and use recode-region to recode it to UTF-8.

(defalias 'jmn/recode-buffer-from-cp1252-to-utf-8
  #'(lambda nil "Recodes a buffer from cp1252 to utf-8 encoding"
      (message "recoding buffer!")
      (interactive)
      (mark-whole-buffer)
      (recode-region (region-beginning) (region-end) 'cp1252 'utf-8)))

(add-hook 'org-capture-prepare-finalize-hook
          '(lambda nil
             (if(cdr(assoc "CODING"(org-entry-properties nil 'standard)))
                 (progn
                   (org-delete-property "CODING")
                   (jmn/recode-buffer-from-cp1252-to-utf-8)))))

Now text can be selected in the browser and sent to emacs org-mode capture using this bookmarklet code:

javascript:location.href='org-protocol://capture://f/'+encodeURIComponent(location.href)+'/'+encodeURIComponent(document.title)+'/'+encodeURIComponent(window.getSelection())

When the capture is finalized, the data will be recoded to utf-8.

Margin notes feedback on published web content

Some sites allow for readers of a document to make annotations in the margin. qz.com and medium.com are examples of this. I would like to easily allow for readers to easily contribute to my documents. It is possible to enable "comments" on otherwise static web content using javascript and services like disqus.com but as far as I'm aware there is not a drop in solution like that for enabling commentary (or notes) on each paragraph in a document. So now I wonder, how can I make this happen. Please respond below or e-mail me if you know how to do this.

Update May 6th 2015: Thanks to http://carnivalapp.io/ coming into existence, this problem is now solved.

How to start new projects on Bitbucket

This is how to add a git project which you have created locally to Bitbucket.

curl -k -X POST --user my_username "https://api.bitbucket.org/1.0/repositories" -d "name=project_name" -d is_private='true'
git remote add origin [email protected]:my_username/project_name.git
git push -u origin --all
git push -u origin --tags

(source)

How to make a public post.

  1. Write the note as normal with org capture.
  2. Save it and open it again (C-c o)
  3. Insert the :blog: tag on the heading and the :on: property 3a. I should have a function that does this automatically. 3b. M-x blogthis.
(defun blogthis ()
"add the necessary tags and properties to publish an org subtree"
(interactive)
    (org-back-to-heading t)
    (org-set-tags-to "blog")
(org-set-property "on" (format-time-string "%Y-%m-%d %H:%M" (current-time)))
(org-jekyll-export-current-entry)
)
  1. Use M-x org-jekyll-export-current-entry to create the HTML file.
  2. Add/Commit and push the new HTML file to Github.

Current flow to share notes online

  1. Write a note, in org-mode capture
  2. Insert the YAML frontmatter required for Jekyll weblogs (I use a custom function for this).
  3. Use the org exporter and export the subtree has HTML body-only to a buffer
  4. Save the buffer to [my-blog-directory]/_posts/YYYY-MM-DD-post-title.html
  5. Commit and push

Note: This is the function I use to insert the YAML front matter.

(defun insert-YAML-front-matter (string)
  "Insert \label{ARG} \index{\nameref{ARG}} at point"
  (interactive "sString for \\title: ")
  (insert "#+BEGIN_HTML
---
layout: post
title: " string " 
tags: 
---

#+END_HTML"))

Also note: For convenience I set the org export defaults to subtree-only.

(setq org-export-initial-scope 'subtree)

There does not seem to be a default setting available for body-only.

Sources:

Sharing a file between git repositories

I have a file in a git repository (my config. files) which I want to add to another repository (my website).

One alternative would be to do things manually. Just copy the file, commit and push.

How can I have this happen automatically?

The easiest thing would be for my repository to automatically update the other repository when the file changes.

Sources:

Expand from dictionary with hippie-expand in Emacs

It is possible to connect M-x hippie-expand with M-x ispell-complete-word. A simple way of doing it could be to add the later function to the hippie-expand-try-functions-list

Either by using the M-x customize-variable hippie-expand-try-functions-list

or by adding the following line to your .emacs configuration:

(add-to-list 'hippie-expand-try-functions-list 'ispell-complete-word t)

Now calling hippie-expand will, as a last resort, use ispell completion to complete words.

Glorious.

Using Git with Dropbox

Having had a bunch of conflicts to some git-files (indexes et c) in my git repositories in a Dropbox folder I looked for information on doing it in a more safe/sane way. Found this on stackoverflow:

I think that Git on Dropbox is great. I use it all of the time. I have multiple computers (two at home and one at work) that I use Dropbox as a central bare repository. Since I don't want to host it on a public service, and I don't have access to a server that I can always ssh to, Dropbox takes care of this by syncing (very quickly) in the background.

Setup is something like this:

~/project $ git init
~/project $ git add .
~/project $ git commit -m "first commit"
~/project $ cd ~/Dropbox/git

~/Dropbox/git $ git init --bare project.git
~/Dropbox/git $ cd ~/project

~/project $ git remote add origin ~/Dropbox/git/project.git
~/project $ git push -u origin master

From there, you can just clone ~/Dropbox/git/project.git that you have associated with your Dropbox account (or have shared this directory with people), you can do all the normal Git operations and they will be synchronised to all your other machines automatically.

I wrote a blog post, On Version Control, […] on my reasoning and how I set up my environment, it's based on my Ruby on Rails development experience, but it can be applied to anything, really.

Then again, as mentioned in the other answers on stack overflow, there is risk of causing corruption using Dropbox so it might not be a great idea (in fact, it might potentially lead to horrific experiences and lost data), so bear that in mind.

How to hide annoying web elements

I use a Chrome/Chromium extension which makes viewing videos on Youtube a better experience. It is called "Magic actions for youtube". It is a nice extension, except it tends to be a bit noisy. I.e. when it updates it opens a page, which is annoying. The extension also adds some spammy looking "share" buttons when using the "cinema mode" which it enables (it centers the video and hides other page elements.) Anyway, to get rid of this annoying little graphic, install "userstyles" from userstyles.org and create a new style (by left clicking the userstyles button, select "find more styles for this site" and so on.)

Add this CSS:

# ma-cin-share {
  visibility: hidden !important;
}

select "applies to" "URLs on the domain" and enter "youtube.com", hit save. And enable the userstyle by checking the box on the left (under the name you gave it.)

You won't have to see the annoying element ever again. Obviously this trick can be used for any annoying HTML element which has got an identifying class name or id.

Forking a Jekyll Bootstrap theme

(Here are the Jekyll Bootstrap docs on themes.)

First, visit https://github.com/jekyllbootstrap and choose the theme you wish to use and fork it using the "fork" button (on the top right). There is even a theme explorer where you can preview the available themes.

Then clone the repository to a directory of your choosing:

git clone https://github.com/USERNAME/theme-the-forked-theme.git

Set the remote repository:

git remote set-url origin [email protected]:USERNAME/theme-the-forked-theme.git

Make any changes you may wish to make and commit them:

git commit -am "My changes."

– push the changes to Github:

git push origin master

Then go to your jekyllbootstrap repo and do:

rake theme:install git="https://github.com/USER/theme-the-forked-theme.git"

Done.

I'm not as of this moment sure of how to elegantly update the theme in the Jekyll Bootrap repository once you have commited changes to the theme onto Github. For now I'm going with removing the theme files and then doing the theme:install command above again. Like so:

rm -rf _theme_packages/the-forked-theme assets/themes/the-forked-theme _includes/themes/the-forked-theme
rake theme:install git="https://github.com/USER/theme-the-forked-theme.git"

How to disable Update pop up windows in Ubuntu 13.04 (Raring Ringtail)

  1. Run dconf-editor.
  2. Go to com->ubuntu->update-notifier
  3. Check the box next to no-show-notifications.

There are ways of keeping your system up to date that do not include intrusive pop up windows.

Comparing time in Python

This is an example of testing if a timestamp in Python is more than five minutes old:

import datettime
five_minutes_ago = datetime.datetime.now() - datetime.timedelta(minutes=5)

if some_timestamp > five_minutes_ago: 
        print "It's older than five minutes"
else:
        print "it's more recent than five minutes"

Related python documentation

Unable to login to Ubuntu

As I started my machine today I found myself unable to login to Ubuntu using the normal graphical login screen (lightdm). I tried looking through the logs in /var/log/lightdm for clues but did not seem to find anything.

Looking in ~/.xsession-errors however, I found

/usr/sbin/lightdm-session: 28: /home/jmn/.profile: Syntax error: end of file unexpected (expecting "fi")

Turns out I had accidentally broken the syntax of the .profile and this caused login to break silently.

Simply fixing the .profile (or moving it temporarily) will solve the issue.

In case you didn't know, you can open a console in Ubuntu by pressing Ctrl+Alt+F1 and then logging in. To move the .profile you can do

mv ~/.profile ~/.profile-old

Here's a relevant thread on Ask Ubuntu.

Marshmallows on top

(via ma.tt) Watched a TED talk by Tom Wujec, who is an Autodesk fellow, about the "Marshmallow challenge" (http://marshmallowchallenge.com). Wujec talks about what you can learn from watching teams with different backgrounds collaborate on a design/engineering challenge. Prototype/refine (iterate) is generally a successful method. Introducing high stakes lowered results for non-informed (unprepared) teams but led to improved results for "informed" (prepared) teams who understood the benefit of iterative prototyping.

How to bring up all blog entries in org-mode

To get a list of all my blog posts, in Emacs, I hit

C-c a m 

and enter "blog"

Alternatively, I open the file containing blog posts and use

C-c / m [blog]

to create a sparse tree (org-sparse-tree) showing only the blog posts. Note though that this only works on a well formed tree. (For some reason my .org file didn't contain a level 1 heading and this caused org-sparse-tree to fail).

Editing system files with Emacs

There does not seem to be a built-in way of editing files which are owned by other users in Emacs.

The two ways that most people seem to be doing it are: via "tramp" or using "sudoedit".

Here is one example:

alias E="SUDO_EDITOR=\"emacsclient -c -a emacs\" sudoedit"

With tramp (see discussion here):

(require 'tramp)

It is possible to edit files like this:

C-c C-f /sudo::/path/to/file

There is also an Emacs package called "sudo-save" available.

I find it surprising that something like sudo-save hasn't been made available in distributions of Emacs. It might of course have to do with security reasons. I have yet to find an elegant solution. Ideally I would prefer if, when opening files owned to another user, Emacs would ask me if I would want to edit using sudo or in read-only mode.

Current blog practice

Currently my workflow to create a new blog post is this. In emacs, I type C-c c which brings up org-capture where I've set up various templates for different types of notes and tasks to capture.

I select j "for journal"

It opens a window and asks me for a headline, followed by tags. If I am sure that I want it to be a blog post I add the tag "blog".

In the template I've set up empty properties for "excerpt" and "tags" which I can fill out if I want the blog post to have an excerpt or tags.

I then write my note (I can at any time hit C-c C-e b to preview it in a browser).

C-c C-c to save the note (the capture buffer).

If I want to revisit the note I hit C-c o which is the key that I have bound to org-capture-goto-last-stored.

This file contains many headings and if I want to preview the exported HTML of the current heading I use the command C-c C-e 1 b.

When I am happy with the entry I use M-x org-jekyll-export-current-entry to create the html file in my git repository.

I can then either preview the post locally using "jekyll –server" and visiting http://localhost:4000, or add the file to the git repository and commit it.

I usually go to the git repository and do a git status:

cd ~/git/jmn.github.io && git status

it shows the new file in the directory "\posts" I then use

git add .

to add all new files. Do the commit:

git commit -am "New post on subject subject"

and push it to the server

git push origin master

(Which I've aliased to "pub")

Remember to set your BROWSER

I had some trouble with Emacs opening html files in the wrong browser recently. Thanks to this post on stackoverflow made by Pierre-Antoine I realized why that other browser kept opening html files. It's because somewhere deep down in the many lines of e-lisp a script called "sensible-browser" is being called. Among other things, it checks the user environment variable BROWSER to see what is it set to.

So to fix this I opened .profile and added this line:

export BROWSER=/usr/bin/chromium-browser

Overriding a single key of evil-mode

From section 3.1 of the evil-mode manual (PDF)

(evil-define-key 'normal foo-mode-map "w" 'bar)

For example I did this:

(evil-define-key 'normal org-mode-map (kbd "TAB") 'org-cycle)

To make tab work properly to expand entries in org files in console mode (emacsclient -nw).

Configure GNU screen to update window names with zsh

It is possible to dynamically update the window names of GNU screen when running commands.

Put this line in .screenrc:

caption always "%{= kw}%-w%{= BW}%n %t%{-}%+w %-= %c"

Put this in .zshrc:

function title {
    if [[ $TERM == "screen"* ]]; then
        print -nR $'\033k'$1$'\033\\'
        print -nR $'\033]0;'$2$'\a'
    fi
}

function precmd {
    title "zsh" "$PWD"
}   

function preexec {
    emulate -L zsh
    local -a cmd; cmd=(${(z)1})
    title "$cmd[1]:t" "$cmd[2,-1]"
}

Found here.

Improved org-jekyll

I've been tweaking org-jekyll for my purposes. Headings in my org-mode journal (usually) contain a timestamp in brackets and org-jekyll wasn't set up to handle that (which resulted in bad file names and post titles). I used a regexp to solve this. (more on Emacs and regular expressions here):

(title (replace-regexp-in-string "-\\[.*?\\]" ""
                                             (replace-regexp-in-string
                                              "[ \t]" "-" heading)))

I also set it up so that the org properties "excerpt" and "tags" are now carried over to YAML, like so:

(defun org-jekyll-export-entry (project)
  (let* ((props (org-entry-properties nil 'standard))
         (time (cdr (or (assoc "on" props)
                        (assoc "ON" props))))
         (excerpt (cdr (or (assoc "excerpt" props)
                        (assoc "EXCERPT" props))))
         (excerpt (cdr (or (assoc "tags" props)
                        (assoc "TAGS" props))))
         (lang (cdr (or (assoc "lang" props)
                        (assoc "LANG" props))))
         (category (if org-jekyll-category
                       (cdr (assoc org-jekyll-category props))
                     nil))
...

I must say it was pretty easy to get a nice set up going using Jekyll Bootstrap and and org-jekyll, and I'm already quite happy with the result. Posts are now looking okay but it's still not 100% convenient to add a new post. I am thinking of modifying org-jekyll so that the "on" property is not required if there exists a timestamp in the title. I am also thinking of making it possible to have the tags from emacs transfer over to YAML and Jekyll.

Another small annoyance is that posting does not seem to work directly from the org capture buffer. This might be understandably hard to and the pay off not that great.

Yet Another potential annoyance is that Jekyll seems to be (notoriously?) poor at detecting file changes and therefore local server restarts are often required to review posts before they are pushed to the Github servers.

Another thing that needs fixing is linking to static assets with relative filepaths.)

Or rather, how to configure org-publish so that "./static/coffee.jpg" in org becomes "/static/coffee.jpg" in HTML.

But that is a headache better left for other days. For now I'm having a coffee.

coffee.jpg

Fullscreen flash on secondary monitor

Tried watching some flash video in fullscreen on a secondary monitor on X.org but found that flash kept leaving fullscreen mode as soon as I tried to do something on the primary monitor. I found out how to solve it from this page: Ubunturoot - Fullscreen Flash Video With Dual Monitors Workarounds.

There are several possible solutions mentioned, one involves editing the flashplayer binary file with a hex editor. I used Maximize Flash for Chrome to solve it for now.

http://www.webupd8.org/2012/10/ubuntu-multi-monitor-tweaks-full-screen.html

Update: Okay, I've tested the hack of libflashplayer.so which prevents the fullscreen flash from exiting and it works for me in Chromium.

It seems I had two versions of the file installed: /opt/google/chromium/plugins/libflashplayer.so /usr/lib/flashplugin-installer/libflashplayer.so

It turns out chromium was using the second one.

To find out which one chromium is using you can move all of the files, start chromium and see if flash works or not.

I still have a problem with the fullscreen mode not actually covering the full screen.

There are supposedly hacks that exist to solve this.

Another tweak, which I am unsure of how (or even if) it works. edit /etc/adobe/mss.cfg

OverrideGPUValidation=true

So far: Editing the libflashplayer.so did solve the problem with fullscreen mode exiting as soon as I would try to do something on the other screen.

Still having problems with the full screen mode taking the wrong size (resolution) and/or proportions.

As shown in chromium about://plugins I am using Shockwave Flash 11.2 r202

Strangely, using the display settings to "move" the external monitor to the left (from the right) of my laptop monitor makes things better. I am now wondering if there is a way to configure which display is the "primary". Perhaps using xrandr.

`xrandr -q` lists devices

I have LVDS1 (laptop display) and VGA1 (external monitor)

supposedly using `xrandr –output VGA1 –primary` sets the external display to be the primary one.

Appearantly flash looks for point +0+0 and calculates the fullscreen size from that monitor. If you have an extended display to the right, you are in trouble. This explains how one person solved it: http://ubuntuforums.org/showthread.php?t=887675&p=12095552#post12095552

Someone suggested trying google's "pepper" flash player which comes with chrome. So I installed chrome from: http://www.chromium.org/getting-involved/dev-channel

Here's how to use it with Chromium: http://www.webupd8.org/2012/09/how-to-make-chromium-use-flash-player.html

Update: I tried installing chrome but unfortunately "pepper flash" was running too slow on my system. On top of that, it also has the same "feature" of the adobe flash player which means that it will exit fullscreen mode as soon as there is activity on the other monitor. And unfortunately I don't know how to get rid of this behavior.

So back to the old adobe flash player I go.

Currently I can get a "fullscreen" window on the screen to my right with the size of my left screen. This because flash seems to read the resolution from the screen that contains coordinate +0,+0. So if I "virtually" rearrange the screens, putting my external screen to the left, I can run true fullscreen.

Setting up org export to Jekyll

I use org-mode to handle notes and I would like to easily publish certain notes from my .org files. Ideally I would like to have two ways of publishing these notes. One from the org capture-buffer. Before I file the capture. I'd do M-x org-publish-to-jekyll. But I would also like to handle edits. I am not yet sure of what would be a good way to do this. There doesn't seem to be a function "org-export-tree-as-html". I am thinking that I could use org-tree-to-indirect-buffer (C-c C-x b) and then export from there.

I asked on IRC and got this message:

15:05 < BerntH> jmnoz: export only the subtree
15:06 < jmnoz> BerntH: okay, thanks. but how do I do that?
15:07 < jmnoz> via an indirect buffer?

15:14 < YoungFrog> jmnoz: you can add restrictions when exporting. read the help buffer shown from C-c C-e carefully
15:14 < YoungFrog> (something like C-s to restrict to subtree)

In the end I found that using "M-x org-tree-to-indirect-buffer" (C-c C-x b) - doing the edits - and then doing the export (M-x org-export-as-html) does what I want.

Juan Reyero's set-up is the one I prefer: http://juanreyero.com/open/org-jekyll/

Gorg Negre's article on Blogging with Emacs org-mode and Jekyll http://www.gorgnegre.com/linux/using-emacs-orgmode-to-blog-with-jekyll.html

Error, when trying to set up Juan Reyero's org-jekyll code: "symbol's function definition is void: org-publish-get-project-from-filename"

Progress with Emacs org-mode Jekyll export

Like many other nerds, I do most of my writing in Emacs org-mode. I am currently adapting Juan Reyero's code to easily publish some of my notes onto this very blog directly from Emacs. I am having some small issues but I seem to have gotten it to work to some degree now!

To publish a post I simply make sure that it has the label (tag) "blog", then run M-x org-jekyll-export-current-entry. The org property ON must also contain a timestamp at the moment, but I might change that because most of my log entries contain a timestamp in the title anyway.

I also need to fix it so that tags get properly translate from org to jekyll..

Update: I modified org-jekyll (some regexp in the org-jekyll-export-entry function) to remove the main heading. It is not needed because Jekyll generates the heading of the page via YAML in my configuration (which is currently the default from jekyll bootstrap).

Next things to fix are:

  1. Removing the timestamp from the title
  2. Making tags transfer properly from org to jekyll.

New export code in Org-mode 8.0

Appearantly there are significant changes coming in the next version of org-mode with regards to the export engine.

Hello World

Hi. I intend to use this space to publish notes about technology. Configuration, solutions and stuff like that. To do this, I am using Jekyll.


When I want to create a new post I simply use the command rake post title="subject" from my jmn.github.io repository.

That creates a markdown-file (syntax) (YYYY-mm-dd-subject.md) in the _posts directory which I then edit.

I use jekyll --server to preview the page locally (at http://localhost:4000) before publishing it using the following git commands:

1
2
3
git add .
git commit -m "Added a new note on subject subject"
git push origin master`

Here’s the Jekyll Quick start Guide and here’s a Step by step guide to setting up a blog like this.

If you are using Emacs org-mode, you might find this guide to integrating Jekyll and org-mode to be of use.


I hope that someone else might find something useful here, but if not, it does not matter much, at least I might myself come back and have a look when memory fails and save myself some time.

Escape key behavior in GNU Screen

Part of the reason I decided to start publishing things here, is that I sometimes run into what seems to be a small annoying tech problem, look for a solution, which sometimes can take time: and solve it only to years later have the very same problem pop up again.

One such problem has been coming up when I’ve been using Vim and GNU Emacs under GNU screen.

One solution, for vim was to add the following lines to .vimrc:

if $TERM == "screen" set term=xterm endif

But the easiest way of solving the problem seems to be to add this line to .screenrc:

maptimeout 2