Showing posts with label pastebin. Show all posts
Showing posts with label pastebin. Show all posts

Friday, 5 September 2014

Index project with ctags and cscope with a single button press in emacs.


I should probably comment on the madness happening below, but I'm too tired at the moment, and, more importantly, I don't think anyone needs it anyway.

The short and messy description is that you press f9 and both ctags and cscope databases are built for your project. Pretty convenient.

To make it work with the project of your choice, you need to describe your project in the known-projects list (it's already filled with some examples).

The first part is a list of directories that designate the root of the project (so that you can run the command from any internal dir).

The second part is a blacklist that contains names and paths that should not be indexed, e.g. you would want to blacklist the huge test collection in GCC which will inflate your database tremendously and add an incredible amount of duplicated names, or the equally humongous Linux driver collection (unless you're a driver developer, duh).

1-2 auxiliary subroutines may have been copied from various places on the Internet. The code may hang your editor if you have cyclic links. Also, I can't program in elisp, although that must be strikingly obvious without me saying that.

;; Define the core dir trappings and blacklisted directories here
(setq known-projects '((("gcc" "config" "libgcc" "libiberty") ("/testsuite/" "/test/" "/build/"))
               (("mono" "mcs" "eglib" "docs" "tools") ())
               (("DROD" "DRODLib" "DRODUtil") ())
               (("arch" "drivers" "include" "kernel") ("/drivers/")) ; Linux
 

               (("README" "INSTALL") ("/.pc/")) ; Generic software project #1 (e.g. emacs)
               (("README") ("/.pc/")) ; Generic software project #2.
               ((".") ())          ; Oh well, let's at least index the current dir
               ))              ; if everything else fails.

(defun index-project () (interactive)
  (defun dump-vars-to-file (varlist filename)
    "simplistic dumping of variables in VARLIST to a file FILENAME"
    (save-excursion
      (let ((buf (find-file-noselect filename)))
    (set-buffer buf)
    (erase-buffer)
    (loop for var in varlist do
          (princ (concat var "\n") buf))
    (save-buffer)
    (kill-buffer))))
 
  (defun lookup-project (projects)
    (let ((lookup-failed t) (blacklist nil) (remaining-trappings nil) (lookup-path nil) (project nil))
      (while (and (consp projects) lookup-failed)
    (setq lookup-path buffer-file-name)
    (setq project (pop projects))
    ; (message "Trying project %s" project)
    (while (and (> (string-width lookup-path) 1) lookup-failed)
      ;; For some reason (file-exists-p "main.c/.") = t!
      (when lookup-failed
        (setq lookup-path (replace-regexp-in-string "/[^/]*$" "" lookup-path)))
      ; (message "Trying path %s" lookup-path)
      (setq lookup-failed nil)
      (setq remaining-trappings (nth 0 project))
      (setq blacklist (nth 1 project))
      (while (and (consp remaining-trappings) (not lookup-failed))
        (if (not (file-exists-p (concat lookup-path "/" (pop remaining-trappings))))
        (setq lookup-failed t)))))
      (when lookup-failed (setq lookup-path nil) (setq blacklist nil))
      (list lookup-path blacklist)))

  (let ((project-info (lookup-project known-projects))
    (project-root nil) (project-blacklist nil))

    (setq project-root (nth 0 project-info))
    (setq project-blacklist (nth 1 project-info))

    (message "Project found at %s. Scanning files... (excliding %s)" project-root project-blacklist)

    (when project-root
      (defun is-path-blacklisted (path blacklist)
    (while (and blacklist (not (string-match (car blacklist) path))) (pop blacklist))
    (if blacklist t nil))

      (defun build-cscope-database (root blacklist)
    (setq dirs (list root)) ;; "/usr/include/")) ; I found it to add too much clutter, better add some individual files instead.
    (setq files '())
    (while dirs
      (setq dir (pop dirs))
      (setq ls (directory-files dir))
      (while ls
        (setq file (pop ls))
        (setq file-path (concat dir "/" file))
        (unless (is-path-blacklisted file-path blacklist)
          (if (file-accessible-directory-p file-path)
          (unless (or (string= file ".") (string= file "..")) (add-to-list 'dirs file-path))
        (if (and (file-readable-p file-path) (string-match "\\.\\(c\\|cpp\\|h\\|hpp\\|vala\\|def\\)$" file-path))
            (add-to-list 'files file-path))))))
    ;; Append some commonly used header files. Uncomment the "/usr/include" line above
    ;; and remove/comment this one if you want to see them all their multitude instead
    ;; of this selectivity.
    (dolist (header '("stdio.h" "stdlib.h" "stdarg.h" "ctype.h" "string.h" "math.h" "dlfcn.h"
              "sys/types.h" "sys/time.h" "unistd.h" "assert.h" "limits.h" "poll.h"
              "libgen.h" "signal.h" "fcntl.h"
              "bits/stdio2.h" "bits/string2.h"
              "X11/Xos.h" "X11/Xlib.h" "X11/Xfuncproto.h" "X11/Xatom.h" "X11/Xproto.h"
              "X11/extensions/Xrandr.h" "X11/extensions/shape.h" "X11/cursorfont.h"
              "X11/extensions/XInput2.h" "X11/X.h"
              "boost/bind.hpp" "boost/foreach.hpp"
              "sys/wait.h" "sys/types.h" "sys/stat.h"
              "glib-2.0/glib/gspawn.h" "glib-2.0/glib/gslice.h" "glib-2.0/glib/gmacros.h"
              "glib-2.0/gobject/gobject.h" "glib-2.0/gobject/gtype.h"
              ))
      (add-to-list 'files (concat "/usr/include/" header)))
    files)

      (dump-vars-to-file (build-cscope-database project-root project-blacklist)
             (concat project-root "/" cscope-index-file))

      (message "Processing...")
      (cscope-set-initial-directory project-root)
      (with-temp-buffer (shell-command (concat "cd " project-root "; cscope -b; ctags -eL " cscope-index-file) t)) ; with-temp-buffer is needed to hide output
      (setq tags-file-name (concat project-root "/TAGS"))
      (message "Done processing")))
)

(global-set-key (kbd "<f9>") 'index-project)

Saturday, 4 January 2014

Running applications so that they wouldn't close when you close the console

Nohup.

But since it litters nohup.outs everywhere let's make a custom shortcut script for launching it:

#!/bin/sh
nohup "$@" >/dev/null 2>&1 &

Just save it as nh into a bin dir and you can use it:

$ nh wine 2hu.exe
$ nh vlc reincarnation.mp3
$ exit

And applications keep on running! Why did I not bother learning about this useful command earlier?

Wednesday, 30 October 2013

asfdsdafwrhfgdgg

cscope-indexer -r -i ~/(path)/cscope.files -f ~/(path)/cscope.out /usr/include/

Tuesday, 30 July 2013

im-switch -s ibus

When in trouble with IME detecting shortcuts (even if it "sees" the window).

Friday, 19 July 2013

Mirroring a website using wget

Every time I have to restore from my memory and man the same line of making a local copy of a website (or a part of it). But enough of that! I'll just post the line here and will never forget as long as this blog is not taken down (which may happen at some point, but who knows when and why?)

wget -N -np -r -l0 -k -p <URL>

wget -N -r -l0 -k -p -D website.edu,img.website.edu http://website.edu/stuffdir/ -I stuffdir  -H

Also, this fine man explains how to fix the tilde character converted to a hex value: http://www.kozubik.com/docs/wget_hack.txt (tldr: urlchr_table:url.c). Weird that the source Ubuntu version has it fixed while the binary package doesn't.

This post was brought to you by: a teacup of red wine.

Thursday, 14 March 2013

Probabilities in the game of Quasar.

def getPrize(num):
    if num == 15:
        return 5
    elif num == 16:
        return 10
    elif num == 17:
        return 20
    elif num == 18:
        return 25
    elif num == 19:
        return 30
    elif num == 20:
        return 40
    else:
        return 0
   
def getRoll(before):
    if before > 20:
        return ((), 0)

    best_roll = ()
    best_avg = getPrize(before)

    for roll in (range(1,9), range(4,8)):
        avg = 0.
        for i in roll:
            avg += getRoll(before + i)[1]
        avg /= float(len(roll))
        if avg > best_avg:
            best_avg = avg
            best_roll = roll
    return (best_roll, best_avg)

for before in range(20):
    roll=getRoll(before)
    print("%d: %s(%f)" % (before, str(roll[0]), roll[1]))

Which leaves us with the following table of the best bets:

current sum: best choice option (average money won)
0: 4..7(24.459561)
1: 4..7(24.461427)
2: 4..7(23.844185)
3: 1..8(23.573145)
4: 1..8(23.723004)
5: 1..8(24.281559)
6: 4..7(24.718933)
7: 4..7(25.114746)
8: 4..7(23.730469)
9: 1..8(21.812592)
10: 1..8(22.166748)
11: 1..8(23.037109)
12: 1..8(24.921875)
13: 4..7(28.750000)
14: 4..7(23.750000)
15: 4..7(17.500000)
16: 1..8(14.375000)
17+: stop

Too bad the wages are too low to make it a viable source of income.

Monday, 8 October 2012

TMNT as I will always remember them.

We're not miserable bugs,
We're Super Ninja Turtles!
Wearing shells like T-shirts,
Young talents, yeah, yeah!

Our fighting team is always united! (April: United!)
We'll defeat mean and negligent folks! (April: Defeat!)
We're not scared of attacks!
We'll beat all meanies in a row!

We're not miserable bugs,
We're Super Ninja Turtles!

Teacher gave us lessons of his Skill! (April: Yeah, yeah!)
Now we know it as good as 2x2! (April: 2x2!)
Be always cold-blooded,
Protect your friend's chest well! (April: HOORAAAY!!! :D )

We're not miserable bugs,
We're Super Ninja Turtles!
Wearing shells like T-shirts,
Young talents, yeah, yeah!

Lionsgate are a bunch of cunts.

Sunday, 2 September 2012

Finding kanji I haven't studied yet in my main deck



Present in Heisig:

鼠汰闇鼈雛韮

sqlite3 ~/.anki/decks/Japanese.anki 'select * from facts'|sed -E 's/(.)/\1\n/g'|sort|uniq > /tmp/used_kanji && for kanji in `sqlite3 -header ~/.anki/decks/Heisigs\ Remember\ the\ Kanji\ \(RTK\)\ 13.anki 'select value from (select * from cards left join facts on facts.id=cards.factid where type=-1) as t left join fields on t.factid=fields.factid where fieldmodelid=(select id from fieldmodels where name="Kanji")'`; do grep -F $kanji /tmp/used_kanji|tr -d '\n' ; done ; echo; rm /tmp/used_kanji

Any:

汰薤辣鐶闇雛韭韮鼈鼠

sqlite3 -header ~/.anki/decks/Heisigs\ Remember\ the\ Kanji\ \(RTK\)\ 13.anki 'select value from (select * from cards left join facts on facts.id=cards.factid where type!=-1) as t left join fields on t.factid=fields.factid where fieldmodelid=(select id from fieldmodels where name="Kanji")' > /tmp/unlocked_kanji; for kanji in $(sqlite3 ~/.anki/decks/Japanese.anki 'select * from facts'|sed -E 's/(.)/\1\n/g'|sort|uniq|grep -v '*'); do if ! grep -qF "$kanji" /tmp/unlocked_kanji; then echo $kanji; fi ; done |tr -d '\n'; echo; rm /tmp/unlocked_kanji

Obsoletee! Obsoletee! With Anki 2.0!