code:
#+TITLE:Org Cheatsheat
#+AUTHOR: Joshua Branson
# I'm not liking the latexpreview #+STARTUP: latexpreview
#+LANGUAGE: en
#+HTML_HEAD <link rel="stylesheet" type="text/css" href="stylesheet.css" />
# The next lines says that I can make 10 levels of headlines, and org will treat those headlines as how to structure the book into
# chapters, then sections, then subsections, then sub-sub-sections, etc.
* Movement commands
  - ~C-c C-n~ Next heading
  - ~C-c C-p~ Previous heading
  - ~C-c C-u~ backward to a higher level heading
  - ~C-c C-j~  org-goto...it is super nice!!
* "TODO" notes
  Org mode has some VERY handy ways of keeping track of todo items.

** The Default TODO/DONE state
   When point is on a heading, pressing "C-c C-t" when insert a TODO at the begining of the line, indicatating that it is an item that
   needs to be completed.  Pressing "C-c C-t" changes the TODO "DONE".  You can change the default values as well.

** You can also break tasks into neat little subtasks.  And keep track of how much of the task is done!

*** TODO Take over the world [20%]
    :LOGBOOK:
    - State "TODO"       from              [2015-10-31 Sat 20:20]
    :END:

**** TODO Get some converts [1/2]
     :LOGBOOK:
     - State "TODO"       from              [2015-10-31 Sat 20:20]
     :END:

***** TODO Talk to people [66%]
      :LOGBOOK:
      - State "TODO"       from              [2015-10-31 Sat 20:20]
      :END:

****** DONE Kyle
       CLOSED: [2015-10-31 Sat 20:22]
       :LOGBOOK:
       - State "DONE"       from "TODO"       [2015-10-31 Sat 20:22]
       - State "TODO"       from              [2015-10-31 Sat 20:20]
       :END:

****** TODO Jen
       :LOGBOOK:
       - State "TODO"       from              [2015-10-31 Sat 20:20]
       :END:

****** DONE Juice
       CLOSED: [2015-10-31 Sat 20:21]
       :LOGBOOK:
       - State "DONE"       from "TODO"       [2015-10-31 Sat 20:21]
       - State "TODO"       from              [2015-10-31 Sat 20:21]
       :END:

***** DONE Write some web blogs
      CLOSED: [2015-10-31 Sat 20:22]
      :LOGBOOK:
      - State "DONE"       from              [2015-10-31 Sat 20:22]
      :END:
**** TODO Get some $$$  [33%]
     :LOGBOOK:
     - State "TODO"       from              [2015-10-31 Sat 20:20]
     :END:
***** DONE Rob a bank
      CLOSED: [2015-10-31 Sat 20:21]
      :LOGBOOK:
      - State "DONE"       from "TODO"       [2015-10-31 Sat 20:21]
      - State "TODO"       from              [2015-10-31 Sat 20:21]
      :END:
***** TODO start a business
      :LOGBOOK:
      - State "TODO"       from              [2015-10-31 Sat 20:21]
      :END:
***** TODO sell my stuff
      :LOGBOOK:
      - State "TODO"       from              [2015-10-31 Sat 20:22]
      :END:
**** DONE Overthrow the government
     CLOSED: [2015-10-31 Sat 20:22]
     :LOGBOOK:
     - State "DONE"       from "TODO"       [2015-10-31 Sat 20:22]
     - State "TODO"       from              [2015-10-31 Sat 20:20]
     :END:

**** TODO help the common man
     :LOGBOOK:
     - State "TODO"       from              [2015-10-31 Sat 20:20]
     :END:

**** TODO kill all the remaining zombies [1/2]
     :LOGBOOK:
     - State "TODO"       from              [2015-10-31 Sat 20:20]
     :END:

***** DONE kill the zombies in Africa
      CLOSED: [2015-10-31 Sat 20:23]
      :LOGBOOK:
      - State "DONE"       from              [2015-10-31 Sat 20:23]
      :END:

***** STARTED kill the zombies in NY
      :LOGBOOK:
      - State "STARTED"    from              [2015-10-31 Sat 20:23]
      :END:

** You can also create little checkboxs too!

*** TODO shopping [2/3]
    :LOGBOOK:
    - State "TODO"       from              [2015-10-31 Sat 20:24]
    :END:

    - [ ]  pickles

    - [X] onions

    - [X] lettuce
* Getting familiar with org-agenda
** commands
   - ~v d~  or ~d~  shows’ today’s agenda
   - ~v w~  or ~w~ shows this week’s agenda
   - ~v m~  view this month’s agenda
   - ~v t~  shows the fortnight
   - ~.~  goto today’s agenda
   - ~j~  goto a specified date
   - ~J~ goto the currently clocked in task
   - ~v c~  shows you overlapping clock entries
   - ~r~ redraw the agenda buffer
   - ~C-k~  kill the current agenda item in the agenda buffer and in the file
   - ~C-c C-w~ refile the entry
   - ~T~ show the tags of an entry
   - ~:~ set the tags for the current entry
   - ~,~  set the priority of the item
   - ~P~  show the priority of the item
   - ~+~ Increase its priority
   - ~-~ minus its priority
   - ~z~ add a note to the entry
   - ~>~ change the timestamp with that entry
   - ~I~ clock in on an entry
   -  ~O~ clockout on an entry
   - ~X~ cancel the current running clock
   - ~m~ mark the entry at point
   - ~u~ undo the marked point
   -  ~U~ unmark all marked entries
   - ~%~ mark via regexp
** views
   - (org-agenda-log-mode)  enteries that were marked done while logging was on, are shone in hte agenda, as are
     entries that have been clocked today.
   - (org-agenda-clockreport-mode) the agenda view will show a table with all your currently clocked time on tasks.
   - (org-agenda-entry-text-mode)
* export commands
  In an org buffer type C-c C-e and org-mode will open a buffer that will prompt you for how you would like to export your document.
** Export Settings
   [[info:org#Export%20settings][info:org#Export settings]]
   Export options can be set globally with variables, but its much easier to specify your export options in your org buffer

   For example
   =#+OPTIONS: toc:nil DATE:<2015-11-20 Fri>=  will not export your table of contents and specifies an export date
   =+TOC:nil=  does nothing

   - = Trees with the tag =:noexport:= means they won't be exported
* Org babel
    :PROPERTIES:
    :engine: mysql
    :dbhost: localhost
    :dbuser: root
    :dbpassword: <your password here>
    :database: usbc
    :END:
  Org babel allows you to insert code blocks into a document.  Those code block's results, can be /weaved/ to html, latex, info,
  etc OR that code can be /tangled/ to be later run by a computer!  It super cool because inside one emacs buffer, you can be writing
  a program that uses 30 different languages, and each block's results can be used as input to any other block!
  - An sql example

#+BEGIN_SRC org
  :PROPERTIES:
  :engine: mysql
  :dbhost: localhost
  :dbuser: root
  :dbpassword: <your password here>
  :database: usbc
  :END:
#+END_SRC

** sql
    #+name: my_query
    # results output and exports both let the exported file show both the code and the results in the exported file
    #+BEGIN_SRC sql :results output :exports both
      SELECT first, last FROM users WHERE last = 'Branson' limit 5
    #+END_SRC

    #+RESULTS: my_query
    | first  | last    |
    |--------+---------|
    | Joshua | Branson |
    | Joshua | Branson |
    | Joshua | Branson |
    | Joshua | Branson |

    That "my_query" block can be passed as input to the following code block
    #+BEGIN_SRC emacs-lisp :var x=my_query
      (print x)

    #+END_SRC

    #+RESULTS:
    | Joshua | Branson |
    | Joshua | Branson |
    | Joshua | Branson |
    | Joshua | Branson |

    One can also tell emacs NOT to export the results of a code execution via
    #+BEGIN_SRC emacs-lisp :exports none
      (print "hello")
    #+END_SRC

    #+RESULTS:
    : hello

    This just mean that when you export the document to html, pdf, .odt, etc., then you will not see that code block's source code or results.

  - A gnu plot example

    #+BEGIN_SRC gnuplot
    set title "My First Plot";  plot 'data';  print "all done!"
    #+END_SRC
** gnu calc
   #+BEGIN_SRC calc
     fsolve (x*2 + 4 = 5, x)
   #+END_SRC

   #+RESULTS:
   : x = 0.5

   #+BEGIN_SRC calc
   fsolve([x + y = a, x - y = b],[x,y])
   #+END_SRC

   #+RESULTS:
   : [x = a + (b - a) / 2, y = (a - b) / 2]

  #+BEGIN_SRC calc
  integ (x**2,x)
  #+END_SRC

  #+RESULTS:
  : x^3 / 3

** gnuplot
   #+BEGIN_SRC gnuplot
   plot x**2 with lines
   #+END_SRC

   #+RESULTS:

** running code remotely
    :PROPERTIES:
    :dir: /ssh:user@host.com:/home/user
    :END:
    #+BEGIN_SRC sh :dir /<user>@<host>:/path/to/file
      ls
    #+END_SRC

** C code
More documentation can be found here:  https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-C.html#org966170f

I could try to get this documentation included into the Emacs manual.

#+BEGIN_SRC C :includes '("<stdio.h>" "<math.h>") :flags -lm :defines MAX_NUMBER 128

  /* This function takes an int and prints its binary representation */
  int return_b_number (int i)
  {
    /* find the highest power of 2 <= i */
    short int n = 0;
    for (; pow(2 , n) <= i; ++n)
      {

      }
    printf("%d\n", n);

  }

  int main ()
  {
    return_b_number (33);
    return 0;
  }
#+END_SRC

#+RESULTS:
: 6

   #+BEGIN_SRC C :exports both :includes <math.h> :flags -lm :var x=1.0 y=4.0 z=10.0
     double pi = 4*atan(1);
     double r, theta, phi;
     r = sqrt(x*x+y*y+z*z);
     theta = acos(z/r) * 180.0/pi;
     phi = atan2(y,x) * 180.0/pi;
     printf("%f %f %f", r, theta, phi);
   #+END_SRC

   #+RESULTS:
   : 10.816654 22.406871 75.963757

   To include multiple C header files
   #+BEGIN_SRC C :includes <math.h> <time.h>
   int b;
   #+END_SRC

   #+RESULTS:

   Finding delimiters in a string
    #+BEGIN_SRC C  :includes <string.h>
    const char * string = "<title>Hello World</title>
Hello World
";

    const char delimiters[] = "<>";
    char *token, *cp;

    cp = strdup (string);                /* Make writable copy.  */
    token = strtok (cp, delimiters);      /* token => "words" */
    printf("%s\n", token);

    token = strtok (NULL, delimiters);      /* token => "words" */
    printf("%s\n", token);

    token = strtok (NULL, delimiters);      /* token => "words" */
    printf("%s\n", token);
    #+END_SRC

    #+RESULTS:
    | html  |
    | body  |
    | title |

** org mode
   Sometimes you may need to generate org-mode source code inside your document for exporting. So to do that you do:

   #+BEGIN_SRC org
    #+BEGIN_SRC python
     print "foo"
    ,#+END_SRC
   #+END_SRC
** simple calc example
   :PROPERTIES:
   :DATE:     2011-01-25
   :END:
#+begin_src calc
2*3
#+end_src

#+name:
: 6
** lisp
   :PROPERTIES:
   :DATE:     2010-08-12
   :END:

#+begin_src lisp :var n=5
  (mapcar (lambda (el) (* el el)) (append '(1 7 3 4) (list n)))
#+end_src

#+name:
| 1 | 49 | 9 | 16 | 25 |

#+name: short-list
| 1 |
| 2 |
| 3 |

#+begin_src lisp :var lst=short-list :session t
  (+ 1 (length lst))
#+end_src

#+name:
: 4

** bash
  #+begin_src sh
    echo "This file takes up `du -h org-babel.org |sed 's/\([0-9k]*\)[ ]*org-babel.org/\1/'`"
  #+end_src

  #+RESULTS:
  : This file takes up

  #+begin_src sh
    pacman -Q firefox
  #+end_src

  #+RESULTS:
  : firefox 41.0.1-1

  This program lists duplicate files.  It runs md5sum on the files to find the duplicates
  https://www.maketecheasier.com/remove-duplicate-files-linux/
  #+BEGIN_SRC sh :dir ~/manuals/
  find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
  #+END_SRC

  #+RESULTS:
  | 2a44284ee46b6572872e5c704889eba4 | ./emacs.org                                               |
  | 2a44284ee46b6572872e5c704889eba4 | ./emacs.org.backup                                        |
  |                                  |                                                           |
  | 7ac95b56101e570db018311e775b7952 | ./ltxpng/org_8efa66ec78ea83e97f9815c08a43ee880750b6ee.png |
  | 7ac95b56101e570db018311e775b7952 | ./ltxpng/org_e7d761bed1cade664f3496421252f6aebdf30781.png |

  In this example both the lisp code and the results will be exported:

  #+BEGIN_SRC emacs-lisp :exports both
  (print "hello")
  #+END_SRC

  #+RESULTS:
  : hello

 #+BEGIN_SRC emacs-lisp

 #+END_SRC

** [[http://kitchingroup.cheme.cmu.edu/blog/2016/02/09/Calling-remote-code-blocks-in-org-mode/?utm_source=feedburner&utm_medium=twitter&utm_campaign=Feed:+TheKitchinResearchGroup+(The+Kitchin+Research+Group)#hallenbeck-2015-compar-co2][calling remote code blocks]]
So, we make a named code block in the si.org file called "figure-1". Then we call it like this:

#+BEGIN_SRC org
  ,#+call: si.org:figure-1() :wrap html
#+END_SRC

** http
[[https://github.com/zweifisch/ob-http][ob-http]] mode
#+BEGIN_SRC http :pretty
GET https://api.github.com/repos/zweifisch/ob-http/languages
Accept: application/vnd.github.moondragon+json
#+END_SRC

#+RESULTS:
: {
:   "Shell": 139,
:   "Emacs Lisp": 13293
: }

** header arguments [[info:org#Specific%20header%20arguments][info:org#Specific header arguments]]
*** :var
:var [<variable name>=<[ literal_value | #+table_name ]>]  [<variable name>=<[ literal_value | #+table_name ]>] ...

**** :var lets you assign a variable at the top level header argument.
#+BEGIN_SRC calc :var x=5 y=6 z=50
x + y + z
#+END_SRC

#+RESULTS:
: 61

#+NAME:GAS
#+BEGIN_SRC calc
1.89
#+END_SRC

#+RESULTS: GAS
: 1.89

#+RESULTS:
: 1.89

***** you can assign a variable to the result of an org src code block
5 gallons of gas costs
#+BEGIN_SRC calc :var gas=GAS
5 * gas
#+END_SRC

#+RESULTS:
: 9.45

*** :dir
#+BEGIN_SRC sh :dir ~/manuals :results output
ls | grep -m 5 -e ".*org" -
#+END_SRC

#+RESULTS:
: accrisoft.org
: apache.org
: arch.org
: auto-complete.org
: bash.org

*** :output-dir
*** :exports
:exports [both | results | code | none]

Export specifies what needs to be saved when you export the document
** passing specific org table columns
#+NAME: output
| groceries |  50 |
| rent      | 800 |
| debt      | 500 |
| electric  | 100 |
|-----------+-----|

#+BEGIN_SRC sh :results output :var name=output[,0]
  for output in $name; do
      echo $name
  done
#+END_SRC

#+RESULTS:
: groceries rent debt electric
: groceries rent debt electric
: groceries rent debt electric
: groceries rent debt electric
** using multiple languages

#+NAME: mynewformula
#+BEGIN_SRC calc
2+a*x**y
#+END_SRC

#+RESULTS: mynewformula
: a x^y + 2

#+CALL: mynewformula(x=10,y=2,a=2)

#+RESULTS:
: 202

* Org spreadsheets/tables

|-------------------------+-----------+-----------+-------|
| Money I earn in a month | Expenses  | Expenses$ | Total |
|-------------------------+-----------+-----------+-------|
|                    2000 | groceries |       500 |       |
|                     100 | gas       |       200 |       |
|-------------------------+-----------+-----------+-------|
|                         |           |       700 |       |
|-------------------------+-----------+-----------+-------|
#+TBLFM: $3=@2$3+@3$3

One refers to a specific table in org, @ROW$COLUMN.  Sometimes its hard to see the column and row values.  To show these, anywhere in the table type =C-c }=. Typing =C-c }= again, will turn hide the row and column values.  You can add columns fairly easily.  For example, let's sum my expenses for the month: at the bottom of my expenses row, type in ~=@2$3 + @3$3~

But let's say that you have lots of expenses.  How can I add a range of values? in the total section under expenses type:
~=vsum(@2$3..@5$3)~
|-------------------------+---------------+-----------+-------|
| Money I earn in a month | Expenses      | Expenses$ | Total |
|-------------------------+---------------+-----------+-------|
|                    2000 | groceries     |       500 |       |
|                     100 | gas           |       200 |       |
|                     800 | rent          |       450 |       |
|                      20 | entertainment |        20 |       |
|-------------------------+---------------+-----------+-------|
|                         |               |      1170 |       |
|-------------------------+---------------+-----------+-------|
#+TBLFM: $3=vsum(@2$3..@5$3)

** Commands
   - =C-c *= recalculates that table row
   - =C-}= toggles showing the row and column numbers
   - =C-{= toggles formula debugging
   - =C-c |= convert the active region to a table
   - =TAB= moves you to the next column
   - =S-TAB= takes you back to the last column
   - =org-table-delete-column= deletes the column point is in
   - =C-c -= insert a horizontal line below point
   - =C-c SPC= blank the field at point
   - =M-left= move column to the left
   - =M-right= move column to the right
   - =C-c '= edit the field in a seperate window=
   - =C-c SPC= blank the field at point
   - =C-c <RET>= insert a horizontal row below point
   - =C-c ^= sort the current table, by the column that point is in alphabetically or numerically SUPER COOL.
* Org Columns
  When in an org file, you can change the file to column view.  You can define a custom column format for the whole file with:
  #+BEGIN_SRC org
    ,#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO
  #+END_SRC

  You can define a column view for a specific subtree with
  #+BEGIN_SRC org
    ,** Top node for columns view
       :PROPERTIES:
       :COLUMNS: %25ITEM %TAGS %PRIORITY %TODO
       :END:
  #+END_SRC
* org lists

#+BEGIN_SRC org
  ,* hello there you cool people
  ,* hello  C-RET will make a new item in this list
  ,* here is that new item!
#+END_SRC

When you have a list like the one above, to add to it just type C-RET, but when you have a list like the one below, you need to type M-RET.

#+BEGIN_SRC org
  1) tahsetnhaentuh
  2) snthaoentuhesntuh
  3)
  4)
  5)
  6)

  This paragraph would mess up your list.  If you try to start a new list here, org will assume that it should start at "1)".  To fix it, you have to include the =[@<number>] text  [@<number>]=.

  7) [@7] sntahenusth [@7]
  8)
#+END_SRC
* latex equations

  \[
  \hat{F}(\nu) = \frac{G(\nu)}{H(\nu)}.
  \]

  There are other ways that I'm supposed to be able to make latex fragments
  If $a^2=b$ and \( b=2 \), then the solution must be
     either $$ a=+\sqrt{2} $$ or \[ a=-\sqrt{2} \].

     \begin{equation}
     x=\sqrt{b}
     \end{equation}

  org-preview latex fragment with "C-c C-x C-l"

\begin{equation} R=-µ\fraq{du_{x}}{dy} \end{equation}

* Creating Indexes
  You can specify entries that will be used for generating an index during
  publishing.  This is done by lines starting with `#+INDEX'.  An entry
  the contains an exclamation mark will create a sub item.  See *note
  Generating an index:: for more information.

     * Curriculum Vitae
     #+INDEX: CV
     #+INDEX: Application!CV

* Agenda
** Agenda commands
   - =A= redraw the agenda view and prompt for how you want it
   - =d= show the day's view
   - =w= show the week's view
   - =f= go forward in time
   - =b= go backward in time
   - =j= jump to a specific date
   - =.= jump to today
   - =D= toggle inclusion of diary entries
   - =vl= toggle logbook mode.
   - =vR= will toggle clockreport mode.  You will see a table showing you the total amount of time
   - =vc= show overlaping clock entries, clocking gaps, or other clocking problems. Then press l to return to the normal
     agenda view
   - =G= toggle displaying of the time grid in the view
   - =g/r= redraw the agenda buffer
   - =/= filter by tag
   - =\= narrow the agenda buffer further by another tag
   - ~=~ filter the agenda view by regexp
   - =|= remove all filters on the agenda view
   - =a= archive the item at point
   - =T= show the tags associated with the current item
   - =:= set the tags for the current item
   - =,= set the priority of the item
   - =z= add a note to this entry
   - =C-c C-s= attach an item to this item
   - =I= clock in
   - =m= mark the agenda item
   - =u= unmark the item
   - =U= unmark all items
   - =%= mark all entries via a regexp
   - =B= do a bulk action on the marked items in the agenda

   When inside the calendar/agenda
   - =i= insert a new entry into the diary
   - =M= show the date phases of the moon
   - =S= show sunrise and sunset times
   - =H= show upcoming Holidays
   - =
* inserting links
** a bbdb record link into the file
    [[bbdb:Nathaniel Simone]]
    How cool is that?

To do this "C-c I" bbdb RET <name of person>
** an email address
[[mailto:bransoj@hotmail.com][my email address]]

To do this "C-c I" mailto RET <email address>
** gnus links!!!???
* Putting images in the document
Create some markup like this:

#+BEGIN_SRC org
  ,#+CAPTION: Which key displays the current major mode's keybindings
  ,#+NAME:   fig:which-key
  [[file:./images/speedtest.net.png]]
#+END_SRC

Then "C-c C-x C-v" will display the inline image.

You could also customize the variable: ‘org-startup-with-inline-images’.

Alternatively you could specify an org variable at the top of your document.

#+CAPTION: Which key displays the current major mode's keybindings
#+NAME:   fig:which-key
[[~/.emacs.d/images/which-key.png]]
* Easy Templates
  Org mode supports insertion of empty structural elements (like #+BEGIN_SRC and #+END_SRC pairs) with just a few key strokes. This is achieved through a native template expansion mechanism. Note that Emacs has several other template mechanisms which could be used in a similar way, for example yasnippet.

To insert a structural element, type a ‘<’, followed by a template selector and <TAB>. Completion takes effect only when the above keystrokes are typed on a line by itself.

The following template selectors are currently supported.

- s 	#+BEGIN_SRC ... \\ #+END_SRC
- e 	#+BEGIN_EXAMPLE ... \\ #+END_EXAMPLE
- q 	#+BEGIN_QUOTE ... \\ #+END_QUOTE
- v 	#+BEGIN_VERSE ... \\ #+END_VERSE
- c 	#+BEGIN_CENTER ... \\ #+END_CENTER
- l 	#+BEGIN_LaTeX ...  \\ #+END_LaTeX
- L 	#+LaTeX:
- h 	#+BEGIN_HTML ...  \\ #+END_HTML
- H 	#+HTML:
- a 	#+BEGIN_ASCII ... \\ #+END_ASCII
- A 	#+ASCII:
- i 	#+INDEX: line
- I 	#+INCLUDE: line

For example, on an empty line, typing "<e" and then pressing TAB, will expand into a complete EXAMPLE template.

You can install additional templates by customizing the variable org-structure-template-alist. See the docstring of the variable for additional details.
* Categories
  I can specify Categories to show up nicely in the agenda buffer
  Right under a tree item, I can say

  #+BEGIN_SRC org
    ,* Good Jokes
      :PROPERTIES:
      :CATEGORY: Projects
      :END:
    ,** One time this farmer
  #+END_SRC
* Schedulting

Scheduling involves creating timestamps.  An active timestamp is enclosed by "<" and ">", and it will cause the heading it's in, to show up in the agenda buffer.  An inactive timestamp is enclosed by "[" "]" and it will not show up in the agenda buffer.
  You can schedule to begin working on a TODO item by typing C-c C-s.  You can schedule a deadline for a TODO item by typing C-c C-d.

  The result is this

  #+BEGIN_SRC org
    ,* TODO Walk the dog
      SCHEDULED: <2015-11-23 Mon>
  #+END_SRC

  You can also make items repeatable like so.  This tasks repeats everyday and lets you know if you miss it.

  #+BEGIN_SRC org
    ,* TODO Walk the dog
      SCHEDULED: <2015-11-23 Mon +1d>
  #+END_SRC

  However the above is not satisfactory.  Org-mode provides two nice extensions to this.

  The below reminds you to take out your trash once a week.  BUT if you forget to do it, it'll still make you take it out on Monday.
  The + sign is a precise mathematical operation. It precisely remembers the day to do this task.

  #+BEGIN_SRC org
    ,* TODO Take out the trash
      SCHEDULED: <2015-11-23 Mon ++1w>
  #+END_SRC

  The below reminds you to brush your teeth.  BUT if you forget to do it, it'll still tell you do it the next 3rd day.
  The . is a regexp symbol that can is a variable.  In the same way, the day that you do this task is variable.

  #+BEGIN_SRC org
    ,* TODO Work out
      SCHEDULED: <2015-11-23 Mon .+3d>
  #+END_SRC

  You can also say that you want to do something at least every third day but at most every fourth day like this

  #+BEGIN_SRC org
    ,* TODO workout
       SCHEDULED: <2015-11-26 Thu .+3d/4d>
  #+END_SRC

  # I got this text from: [[info:org#Repeated%20tasks][info:org#Repeated tasks]]
  To mark a task with a repeater as `DONE', use `C-- 1 C-c C-t' (i.e.,
`org-todo' with a numeric prefix argument of -1.)
** Advanced Scheduling

Org scheduling will only get you so far.  Org timestamps cannot plan things like "The 2nd Thursday of the Month", but the Emacs Diary Sexp Entries can!

For example,  suppose your local bowling team goes bowling on the 3rd Tuesday of every month.  Here is how you can schedule that task:

#+BEGIN_SRC org
  ,*  Go Bowling with the Gents
   <%%(diary-float t 2 3)>
#+END_SRC

If you would like to know more, the documentation for the function "diary-float", is quite sufficient.
* inactive timestamps

Inactive timpstamps can be inserted into the document via: "C-c !".  But they can't be because "C-c !" is flyspell's keybinding.
Maybe I should make a keybinding for inserting an inactive timestamp into the document.
* parsing an org file

#+BEGIN_SRC emacs-lisp
  (setq my/ast (with-temp-buffer
                 (insert-file-contents "~/file.txt")
                 (org-mode)
                 (org-element-parse-buffer)))
#+END_SRC

The elisp function is org-element-parse-buffer &optional GRANULARITY VISIBLE-ONLY  ...

granularity can be any of the following:

|-----------------+-----------------------------------|
| headline        | only parse headlines...           |
| greater-element | only parse the top level elements |
| elements  parse | all but objcets and plain text    |
| object          | parse all stuff                   |
|-----------------+-----------------------------------|

visible-only means don't parse contens of hidden elements...


code:
#+AUTHOR:Joshua Branson
#+TITLE: OS Arch
#+LATEX_HEADER: \usepackage{lmodern}
#+LATEX_HEADER: \usepackage[QX]{fontenc}
#+OPTIONS: H:10 toc:nil

* OS Arch
This is my cheatsheet from [[https://en.wikibooks.org/wiki/Operating_System_Design][this]] wikibook.

An operating system is the fundamental bits of software that make your computer function.  Your computer is a number crunching machine.  Specifically, your processor performs calculations all day long.  Your processor has to do various things for different applications.  All of the applications on your computer fight for processor time.  It is the job of your OS to ensure that all applications efficiently perform their function.

OS do some of this with threads and processes.  Let's explain what a process is first.  When every application starts, your OS starts that application inside a process.  The OS then schedules that process CPU time.  That process has a list of open files, a certain amount of memory, and dates with which to work with the CPU.  However, there are times when two different processes need to communicate to other processes.  This is called IPC or inter-process communication.  Each process has its own amount of memory reserved, its own list of open files, and its own dates to talk to the CPU.  A process is like a like a boat with one occupant.  The occupant, by virtue of being the only occupant, is the captain.  He dictates where the ship goes, who eats the food, etc.

In order to effective communicate well between processes, an operating system uses locks, mutexes, semaphores, etc.

IPC can be done via

1) socket programming TCP/UDP
2) Pipes
3) shared memory

One the other hand a thread is a small clone of a process.  A thread inherits the amount of information that the process has.  The thread can look at the files its parent process has, can look at the variables its parent process has set.  A thread takes less time to spawn than a process, so you can think of it as a light-weight process.

[[./images/OS-structure.png]]

** 4 main kernel types
*** monolithic
A monolithic kernel runs everything in kernel space, except for user applications.
*** microkernel
A micro-kernel runs as few things as possible in kernel space.  A true micro kernel-will only have the virtual file system, IPC, and scheduling inside the kernel.  Everything else is outside the kernel.

So device drivers and file servers run as user space processes.

Theoretically, a micro kernel-is more stable than a monolithic kernel.  Consider that in a micro-kernel the internet runs as a process in user-space. If this user-space process crashes, the whole system does not crash.  In fact the internet connection could be restarted and processes that were using it would be connect to the new instance.

There is a problem with this model though.  State is not preserved when the internet server (users pace process) crashes and restarts.  A file that is connected to over say ssh, may not be aware that its connection was lost.

*** hybrid
A hybrid tries to mix both a micro-kernel and a monolithic kernel.
*** exokernel
An exokernel is a kernel where each application connects to a sub-operating system.  It is a recent development.
** More Processes
*** Interrupts
An interrupt is a signal sent to the CPU that something has happened that demands its immediatelyte attention.  This can happen from the keyboard (when a key is pressed), or by a number of other things.
*** Content switch
The CPU is told that some process needs its attention.  The CPU then saves information on its current process.  Does some computation on the other process, and when it has more time, it will get back to processing the save process.  The CPU has to save the processes stack (or variables that the program has defined).  Most of this information is saved in thee stack itself.

Context switches are expensive.  So the OS tries to avoid doing them.
** Scheduling
A processor can only do one thing at one time.  It cannot perform computations on two different processes at once.  Therefore it has to schedule when processes can access the CPU.  There are several different strategies
*** First come, first served
This is the simplest scheduler and it is a poor choice.  It does not allow for preemption.  If a process takes a long amount of time, then it has to complete the entire process, before doing anything else.

This means that a small process must wait for the VERY LONG process to finish.
*** Shortest Process Next
This schedules that the next process is the shortest process.  This is an OK way to schedule a process, but it effectively stops long processes from accessing the CPU, this is called /live lock/.

It also creates the /halting problem/.  If we do not know how long it will take a process to execute, we can never really know if a process will ever finish.  Run a program and wait 5 minutes.
*** Shortest Remaining Time
This allows short processes to skip ahead of long processes as they appear.

It can still have problems with the /halting problem/ or /live lock/.
*** Round Robin
Round Robin scheduling schedules a certain amount of time a process has with the CPU set over periodic and separate points in time.  When a process uses enough CPU time for one session, it is put back at the end of the queue.
*** Preemption
Preemption allows a process to interrupt another process, because it has a higher priority.
*** Priority Scheduling
Each process has a priority.  Higher priority processes get access to the CPU sooner than lower ones.  In the Linux kernel priority numbers are nice numbers.
** Concurrency
Concurrency can be melted into 4 main issues:
- Concurrent processes or threads often need to access shared date and shared resources
- The OS must control access to shared data, otherwise data can become inconsistent, corrupted, deleted, etc.
- Maintaining data means that one needs to orderly schedule cooperating processes
- various programs may cause race conditions
*** deadlock

A deadlock occurs when two processes are waiting for the other to give up a resource.  Neither gives up the resource, and both processes wait indefinitely.

This is like two people trying to draw a straight line.  One reaches for the pencil, the other reaches for the ruler.  Neither can draw a straight line, so they wait.

When two processes are fighting for software based resources (memory) this is a soft (software) lock.

When two processes are fighting for hardware this is a hard (hardware) lock.
*** livelock
Two processes commit livelock when both processes effectively achieve nothing, but continue doing things.

A good example is when I need to buy a new social security card because someone stole money from my account..  To generate a new social security card, costs $10, but all of my money is in the bank.  So I go to the bank to get money out of it.  But they ask me to prove who I am via a social security card, which I don't have.
** Inter-process communication
*** Locks
A lock is a method of restricting multiple threads from using, modifying, or changing a resource at the same time.  For example if two people try to take a drink out of the same cup at the same time, neither will be very successful.  Locks are meant to ensure that those kinds of things do not happen.

Most locking mechanisms are guide locks.  Threads work together in order to properly use resources.  Most locks are not /minatory/ locks.
*** Signals
Signals are software communication to a process.  They Are messages that tell a process about things that it needs to do.  Signals can tell a process when a Martian region of memory is accessed.  Or signals can be used to tell a process to terminate, etc.
*** Semaphore
A semaphore is a variable that signifies when a process can access a resource.  Suppose 100 people want to ride a roller coaster, but the ride only seats 20 at a time.  When the ride is empty the semaphore value is 20.  When 20 people get on the ride, the semaphore value becomes 0.  This kind of semaphore is called a /counting semaphore/.

Interestingly-ly, A semaphore that can only be 1 or 0 is called a /binary semaphore/ and these are usually used to make /mutexes/.

Hardware does some atomic operations to ensure this stuff works.

One disadvantage of semaphores is that processes that want to use a resource, but must wait, usually just call the CPU over and over again to see if they can use the resource.  This is called /busy waiting/ or /spinlock/.
*** Monitors
A monitor is a resource that can be used safely by all threads without having to worry about corruption.  High level languages may have support for them, but C just has threads.
*** Shared Memory
Processes must use memory. In this case it means they all play in the same sandbox, but they all get their own sections to play with.

I'm really not sure why they call it shared memory.  I suppose when one process dies, another process may access that dead processes memory.  They may share in time, but rarely will two processes share the same regions of memory at the same time.  That would be foolish, because no one throws gas on a campfire, if they are smart.
*** thread

A thread is a sub-process with fewer rights, running inside a process.
** Memory Management
Memory management is what an operating system does to ensure that the computer does not run out of RAM.

*** Physical memory
Physical memory is the amount of RAM available to your operating system.  When someone says a physical address, they are referring physical memory.

If an operating system only uses physical memory and fails to use virtual memory, this is called /segmented memory/.  BUT no operating system does this, because the benefits far exceed the downsides.

*** Virtual memory
Virtual memory is fake memory that maps onto physical memory (RAM).  Suppose a system has 1 GB of physical memory, but the
same system might have 2GB of virtual memory (swap comes in handy too).  This can be useful for a variety of ways, but one way is that one can maneuver and disrupt virtual memory in ways that one cannot do with physical memory.

When someone says /logical address/, they are referring to physical address.

It can be thought of a LVM or logical volume management.  Logical volume management lets you create, resize, and manipulate partitions on your hard-drive with ease.  LVM lets one maintain many partitions, and constantly move things around without breaking the underlying actual partitions.  Virtual memory does something similar.

Suppose an Firefox needs 50MB of memory.  The application may not want to manage 20 continuous MG then another 20 elsewhere and the last 10MB somewhere else, like so.

|---------+------+---------+-------+---------|
| firefox | grep | firefox | emacs | firefox |
| 20MB    | 2MB  | 20MB    | 7MB   | 10MB    |
|---------+------+---------+-------+---------|

So instead of Firefox managing separate and discreet regions of memory, the physical memory is mapped onto virtual memory, so the above table becomes:

|---------+------+-------|
| firefox | grep | emacs |
| 50MB    | 2MB  | 7MB   |
|---------+------+-------|

An Firefox is unaware that it is making the operating systems job so much harder.

Virtual memory is also great for inter-process communication, because numerous process can share the same logical address space.  Sharing logical address space makes COW (copy on write) possible.

*** Page memory
Page memory is essentially using translating virtual memory to physical memory with an MMU (or memory management unit).

*** The heap

repo_name: hwpplayers/cheatsheets

license: mit


code:
#+AUTHOR: Joshua Branson

* Pacman CheatSheat

 Pacman is Arch GNU/Linux's [fn:1] official package manager.  It is written in C and it's pretty rad.

** Common Commands
- =pacman -Syu=
  upgrade all outdated packages
- =pacman -S <package name> [<package name> [<package name> [ ... ]]]= a package
-

** Common Problems

*** Failed to commit transaction package: /path/to/file exists in the filesystem

# this came from https://wiki.archlinux.org/index.php/Pacman#pacman_crashes_during_an_upgrade
#+BEGIN_SRC org
  Why this is happening: pacman has detected a file conflict, and by design, will not overwrite files for you. This is a design feature, not a flaw.

  The problem is usually trivial to solve. A safe way is to first check if another package owns the file (pacman -Qo /path/to/file). If the file is owned by another package, file a bug report. If the file is not owned by another package, rename the file which 'exists in filesystem' and re-issue the update command. If all goes well, the file may then be removed.

  If you had installed a program manually without using pacman or a frontend, for example through make install, you have to remove it and all its files and reinstall properly using pacman. See also Pacman tips#Identify files not owned by any package.

  Every installed package provides a /var/lib/pacman/local/$package-$version/files file that contains metadata about this package. If this file gets corrupted, is empty or goes missing, it results in file exists in filesystem errors when trying to update the package. Such an error usually concerns only one package. Instead of manually renaming and later removing all the files that belong to the package in question, you may exceptionally run pacman -S --force $package to force pacman to overwrite these files.

#+END_SRC

*** error: bogofilter: signature from "Luke T. Shumaker <lukeshu@sbcglobal.net>" is marginal trust
:: File /var/cache/pacman/pkg/bogofilter-1.2.4-3.parabola2-x86_64.pkg.tar.xz is corrupted (invalid or corrupted package (PGP signature)).

https://bbs.archlinux.org/viewtopic.php?id=195139

You can download the key via:  pacman-key -r 0xFC1B547C8D8172C8

* Footnotes

[fn:1] The Official name is Arch Linux, but I prefer to call it Arch GNU/Linux, to respect the hard word that GNU put into creating the GNU/Linux operating system.  For more information, you can check out: [[https://www.gnu.org/gnu/why-gnu-linux.html%20][What's in a Name?]]  and [[https://www.gnu.org/gnu/gnu-linux-faq.html][GNU/Linux FAQ]].


code:
#+TITLE:PHP cheatsheat
#+AUTHOR:Joshua Branson
#+OPTIONS: H:10 toc:nil

* check if a string is numeric

#+BEGIN_SRC
$number = "550;"
$number2 = "-550";
$number3 = "+420.342";
if (is_numeric ($number) && is_numeric ($number2) && is_numeric ($number3)) {
echo "they are all numeric strings!";
}
#+END_SRC

* check if variables have value
http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index

$value = !empty($_POST['value']) ? $_POST['value'] : '';
* Database connections
PHP is smart enough to let you connect to a database in a procedural way or in an OO way.

You can mix and match the ways you access the database.

Here is the OO style.
#+BEGIN_EXAMPLE
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}
?>
#+END_EXAMPLE

Here is the procedural style

#+BEGIN_EXAMPLE
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Create table doesn't return a resultset */
if (mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}

/* Select queries return a resultset */
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", mysqli_num_rows($result));

#+END_EXAMPLE

Now here is how one might mix them via using the procedural style in setting up the connection,
but querying via the OO style.

#+BEGIN_EXAMPLE
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* Create table doesn't return a resultset */
if ($link->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}
#+END_EXAMPLE
* htmlspecialspecialchars ()

Everytime you echo user input, you /must/ use this function on the string.  This function quotes any html characters.  So if a user filled out their name as:  <script src="some-javascript.css"></script>,  when you echo it on the script, the script won't run.


code:
* What is pixi.js?
  pixi.js is a javascript library that uses WebGL, or canvas on devices that don’t support WebGL, to render things to the browser.
* A Display Object
  is a pixi class that should not be used by itself. Instead it is extended into other classes like Graphics.
*


code:
#+AUTHOR:Joshua Branson
#+TITLE: Post Script
#+LATEX_HEADER: \usepackage{lmodern}
#+LATEX_HEADER: \usepackage[QX]{fontenc}
#+OPTIONS: H:10 toc:nil

* Post Script
Post script is a Turing complete programming language that is used to print documents or generate pdf documents.

ghostview is a way to view postscript files.


code:
#+TITLE:Emacs Cheatsheat
#+AUTHOR: Joshua Branson

* Sed cheatsheat
sed SCRIPT file
sed [options] SCRIPT file
** sed flags
*** -n do not print out each line.  This means sed only prints things when it sees the p command.
*** -r use extended regexps
** Some sed example
*** weird example
#+BEGIN_SRC sh :dir ~/ :results output
sed -n '/enabled/  s/^.*- // p ' disabled-systemd-files.org | awk '{print $1}' -
#+END_SRC

#+RESULTS:
#+begin_example
avahi-daemon.service
avahi-dnsconfd.service
console-getty.service
console-shell.service
gssproxy.service
nscd.service
serial-getty@.service
systemd-bootchart.service
systemd-journal-upload.service
systemd-networkd-wait-online.service
systemd-networkd.service
systemd-nspawn@.service
systemd-resolved.service
udisks2.service
avahi-daemon.socket
sshd.socket
systemd-journal-gatewayd.socket
systemd-journal-remote.socket
systemd-networkd.socket
uuidd.socket
ctrl-alt-del.target
exit.target
halt.target
kexec.target
machines.target
poweroff.target
reboot.target
rescue.target
fstrim.timer
pacman_keyring.timer
#+end_example
*** make sed print out lines matching a regexp
#+BEGIN_SRC sh :results output
journalctl -b | grep -m 30 ".*" | sed -nr '/system/ p'
#+END_SRC

#+RESULTS:
: Mar 30 07:49:51 parabola systemd-journald[168]: Runtime journal (/run/log/journal/) is 8.0M, max 184.7M, 176.7M free.
: Mar 30 07:49:52 parabola systemd-journald[168]: System journal (/var/log/journal/) is 62.5M, max 50.0M, 0B free.
: Mar 30 07:49:52 parabola systemd-journald[168]: Time spent on flushing to /var is 1.103ms for 2 entries.
*** make sed print out a numbered line
#+BEGIN_SRC sh :results output
journalctl -b | grep -m 30 ".*" | sed -nr '1 p'
#+END_SRC

#+RESULTS:
: -- Logs begin at Tue 2016-03-29 15:07:04 EDT, end at Wed 2016-03-30 08:01:28 EDT. --
*** make sed print out lines 1-10 and matching regexps
#+BEGIN_SRC sh :results output
journalctl -b | grep -m 30 ".*" | sed -nr '1,10 p
/cgroup/ p
/linux/ p'
#+END_SRC

#+RESULTS:
#+begin_example
-- Logs begin at Tue 2016-03-29 15:07:04 EDT, end at Wed 2016-03-30 08:16:08 EDT. --
Mar 30 07:49:51 parabola systemd-journald[168]: Runtime journal (/run/log/journal/) is 8.0M, max 184.7M, 176.7M free.
Mar 30 07:49:52 parabola systemd-journald[168]: System journal (/var/log/journal/) is 62.5M, max 50.0M, 0B free.
Mar 30 07:49:52 parabola systemd-journald[168]: Time spent on flushing to /var is 1.103ms for 2 entries.
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpuset
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpuset
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpu
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpu
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpuacct
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpuacct
Mar 30 07:49:52 parabola kernel: Linux version 4.4.6-gnu-1 (builduser@emulatorman-pn) (gcc version 5.3.0 (GCC) ) #1 SMP PREEMPT Tue Mar 22 19:10:53 UYT 2016
Mar 30 07:49:52 parabola kernel: Command line: BOOT_IMAGE=/vmlinuz-linux-libre root=UUID=159e1ea0-111c-4d3c-9ec2-c1bcd77e96ca rw quiet
Mar 30 07:49:52 parabola kernel: Command line: BOOT_IMAGE=/vmlinuz-linux-libre root=UUID=159e1ea0-111c-4d3c-9ec2-c1bcd77e96ca rw quiet
Mar 30 07:49:52 parabola kernel: x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers'
#+end_example
*** make sed select the odd lines
#+BEGIN_SRC sh :results output
journalctl -b | grep -m 30 ".*" | sed -nr '1~2 p'
#+END_SRC

#+RESULTS:
#+begin_example
-- Logs begin at Tue 2016-03-29 15:07:04 EDT, end at Wed 2016-03-30 08:03:18 EDT. --
Mar 30 07:49:52 parabola systemd-journald[168]: System journal (/var/log/journal/) is 62.5M, max 50.0M, 0B free.
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpuset
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpuacct
Mar 30 07:49:52 parabola kernel: Command line: BOOT_IMAGE=/vmlinuz-linux-libre root=UUID=159e1ea0-111c-4d3c-9ec2-c1bcd77e96ca rw quiet
Mar 30 07:49:52 parabola kernel: x86/fpu: Supporting XSAVE feature 0x02: 'SSE registers'
Mar 30 07:49:52 parabola kernel: x86/fpu: Using 'lazy' FPU context switches.
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000af000000-0x00000000beffffff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000bf719000-0x00000000bf938fff] ACPI NVS
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000bfef9000-0x00000000bfefefff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000d3200000-0x00000000d3200fff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000fec00000-0x00000000fec00fff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000ffc00000-0x00000000ffffffff] reserved
#+end_example
*** make sed select the even lines
#+BEGIN_SRC sh :results output
journalctl -b | grep -m 30 ".*" | sed -nr '2~2 p'
#+END_SRC

#+RESULTS:
#+begin_example
Mar 30 07:49:51 parabola systemd-journald[168]: Runtime journal (/run/log/journal/) is 8.0M, max 184.7M, 176.7M free.
Mar 30 07:49:52 parabola systemd-journald[168]: Time spent on flushing to /var is 1.103ms for 2 entries.
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpu
Mar 30 07:49:52 parabola kernel: Linux version 4.4.6-gnu-1 (builduser@emulatorman-pn) (gcc version 5.3.0 (GCC) ) #1 SMP PREEMPT Tue Mar 22 19:10:53 UYT 2016
Mar 30 07:49:52 parabola kernel: x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers'
Mar 30 07:49:52 parabola kernel: x86/fpu: Enabled xstate features 0x3, context size is 576 bytes, using 'standard' format.
Mar 30 07:49:52 parabola kernel: e820: BIOS-provided physical RAM map:
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x0000000000100000-0x00000000aeffffff] usable
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000bf000000-0x00000000bf718fff] usable
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000bf939000-0x00000000bfef8fff] ACPI data
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000bfeff000-0x00000000bfefffff] ACPI data
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000f0000000-0x00000000f3ffffff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000fee00000-0x00000000fee00fff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x0000000100000000-0x000000013fffffff] usable
#+end_example
*** delete the pattern space
#+BEGIN_SRC sh :results output
journalctl -b | grep -m 30 ".*"
#+END_SRC

#+RESULTS:
#+begin_example
-- Logs begin at Tue 2016-03-29 15:07:04 EDT, end at Wed 2016-03-30 08:16:42 EDT. --
Mar 30 07:49:51 parabola systemd-journald[168]: Runtime journal (/run/log/journal/) is 8.0M, max 184.7M, 176.7M free.
Mar 30 07:49:52 parabola systemd-journald[168]: System journal (/var/log/journal/) is 62.5M, max 50.0M, 0B free.
Mar 30 07:49:52 parabola systemd-journald[168]: Time spent on flushing to /var is 1.103ms for 2 entries.
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpuset
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpu
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpuacct
Mar 30 07:49:52 parabola kernel: Linux version 4.4.6-gnu-1 (builduser@emulatorman-pn) (gcc version 5.3.0 (GCC) ) #1 SMP PREEMPT Tue Mar 22 19:10:53 UYT 2016
Mar 30 07:49:52 parabola kernel: Command line: BOOT_IMAGE=/vmlinuz-linux-libre root=UUID=159e1ea0-111c-4d3c-9ec2-c1bcd77e96ca rw quiet
Mar 30 07:49:52 parabola kernel: x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers'
Mar 30 07:49:52 parabola kernel: x86/fpu: Supporting XSAVE feature 0x02: 'SSE registers'
Mar 30 07:49:52 parabola kernel: x86/fpu: Enabled xstate features 0x3, context size is 576 bytes, using 'standard' format.
Mar 30 07:49:52 parabola kernel: x86/fpu: Using 'lazy' FPU context switches.
Mar 30 07:49:52 parabola kernel: e820: BIOS-provided physical RAM map:
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x0000000000100000-0x00000000aeffffff] usable
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000af000000-0x00000000beffffff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000bf000000-0x00000000bf718fff] usable
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000bf719000-0x00000000bf938fff] ACPI NVS
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000bf939000-0x00000000bfef8fff] ACPI data
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000bfef9000-0x00000000bfefefff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000bfeff000-0x00000000bfefffff] ACPI data
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000d3200000-0x00000000d3200fff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000f0000000-0x00000000f3ffffff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000fec00000-0x00000000fec00fff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000fee00000-0x00000000fee00fff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x00000000ffc00000-0x00000000ffffffff] reserved
Mar 30 07:49:52 parabola kernel: BIOS-e820: [mem 0x0000000100000000-0x000000013fffffff] usable
#+end_example
*** do many s commands. NOT WORKING.
How can I perform more than 1 command on one address?  A sed address is can be a REGEXP or a line number, etc.

#+BEGIN_SRC sh :results output
journalctl -b | grep -m 30 ".*" | sed -nr '1,10 {p}
/cgroup/ {p}
/linux/ {p}
/BIOS/ {s/BIOS/HELLO/p}'
#+END_SRC

#+RESULTS:
#+begin_example
-- Logs begin at Tue 2016-03-29 15:07:04 EDT, end at Wed 2016-03-30 08:22:45 EDT. --
Mar 30 07:49:51 parabola systemd-journald[168]: Runtime journal (/run/log/journal/) is 8.0M, max 184.7M, 176.7M free.
Mar 30 07:49:52 parabola systemd-journald[168]: System journal (/var/log/journal/) is 62.5M, max 50.0M, 0B free.
Mar 30 07:49:52 parabola systemd-journald[168]: Time spent on flushing to /var is 1.103ms for 2 entries.
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpuset
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpuset
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpu
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpu
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpuacct
Mar 30 07:49:52 parabola kernel: Initializing cgroup subsys cpuacct
Mar 30 07:49:52 parabola kernel: Linux version 4.4.6-gnu-1 (builduser@emulatorman-pn) (gcc version 5.3.0 (GCC) ) #1 SMP PREEMPT Tue Mar 22 19:10:53 UYT 2016
Mar 30 07:49:52 parabola kernel: Command line: BOOT_IMAGE=/vmlinuz-linux-libre root=UUID=159e1ea0-111c-4d3c-9ec2-c1bcd77e96ca rw quiet
Mar 30 07:49:52 parabola kernel: Command line: BOOT_IMAGE=/vmlinuz-linux-libre root=UUID=159e1ea0-111c-4d3c-9ec2-c1bcd77e96ca rw quiet
Mar 30 07:49:52 parabola kernel: x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers'
Mar 30 07:49:52 parabola kernel: e820: HELLO-provided physical RAM map:
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x0000000000100000-0x00000000aeffffff] usable
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x00000000af000000-0x00000000beffffff] reserved
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x00000000bf000000-0x00000000bf718fff] usable
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x00000000bf719000-0x00000000bf938fff] ACPI NVS
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x00000000bf939000-0x00000000bfef8fff] ACPI data
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x00000000bfef9000-0x00000000bfefefff] reserved
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x00000000bfeff000-0x00000000bfefffff] ACPI data
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x00000000d3200000-0x00000000d3200fff] reserved
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x00000000f0000000-0x00000000f3ffffff] reserved
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x00000000fec00000-0x00000000fec00fff] reserved
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x00000000fee00000-0x00000000fee00fff] reserved
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x00000000ffc00000-0x00000000ffffffff] reserved
Mar 30 07:49:52 parabola kernel: HELLO-e820: [mem 0x0000000100000000-0x000000013fffffff] usable
#+end_example
*** make sed delete lines in a file matching a pattern

#+BEGIN_SRC sh :results output :exports both
sed -i '/pattern to match/d' ./infile
#+END_SRC

** How sed works
sed goes through the input line by line, if a line matches a particular condition, then some action is performed.
** selecting lines with sed
*** NUMBER
only select this line number
*** FIRST~STEP
select line number at number FIRST, then every following STEP-th number.

ie: 1~2  selects all odd numbered lines
ie: 2~2  selects all even numbered lines
ie  5~5  selects all factors of 5
*** /REGEXP/ match every line that matches that regexp
** sed commands
*** =#= comment
*** =q= exit sed NOW
*** =d= delete pattern space, start next cycle
*** =p= print out the pattern space
*** =s= the substitute command
s/REGEXP/REPLACEMENT/FLAGS

The most common flag used is "p", which is print.
*** ={ COMMANDS }= do more than one command on one match


code:
#+AUTHOR:Joshua Branson
#+TITLE: Shepherd
#+LATEX_HEADER: \usepackage{lmodern}
#+LATEX_HEADER: \usepackage[QX]{fontenc}
#+OPTIONS: H:10 toc:nil

* Shepherd
** Things I could fix
*** complete this bit of the shepherd manual

[[info:shepherd#Service%20Examples][info:shepherd#Service Examples]]


code:
#+AUTHOR:Joshua Branson
#+TITLE: Solr
#+LATEX_HEADER: \usepackage{lmodern}
#+LATEX_HEADER: \usepackage[QX]{fontenc}
#+OPTIONS: H:10 toc:nil

* Solr

Solr is an open source website search engine.  It's an alternative to Google custom search.

http://lucene.apache.org/


code:
#+TITLE:SQL Cheatsheat
# DO NOT add this file to git!  It has private underage names in it.  I need to use my own fake database to demonstrate
# these queries

# The next lines says that I can make 10 levels of headlines, and org will treat those headlines as how to structure the book into

# chapters, then sections, then subsections, then sub-sub-sections, etc.
#+OPTIONS: H:10

# This next line says that the table of contents should mention the chapters and the chapter sections.
#+OPTIONS: toc:2
* SQL Cheatsheat
     :PROPERTIES:
     :engine:   mysql
     :dbhost:   localhost
     :dbuser:   root
     :dbpassword: GraceLikeRain
     :database: usbc
     :END:
** Glossary
*** Primary Key
allegedly this is required, but that's not true.
*** Foreign Key
A foreign key is a column in one table that references another column.  So two tables might have the same ID column,
but one of those columns contains a foreign key.
*** Relationship
A relationship of 2 tables is where two columns (typically have the same name), from different tables, have the same values.  Both of these columns contain primary keys, and one of those primary keys contains a foreign key to the other table.
*** Database design
**** 1st normal form
**** 2nd normal form
**** 3rd normal form
** connecting to a database via the terminal
   ~$ mysql -h host -u <user> -p~

   ~Enter password: ********~

   ~MariaDB> USE <table_name>~

   ~MariaDB table_name>~

   Now you can make calls to the database like this

   ~MariaDB table_name>SELECT * FROM people~
** Select Syntax
   Note this is not the complete SELECT syntax; instead, it is a simplication. See the SQL documentation here for the complete
   syntax: http://dev.mysql.com/doc/refman/5.6/en/select.html
   SELECT [ DISTINCT ]

   select_expr [, select_expr ...]

   [FROM table_references

   [WHERE where_condition]

   [GROUP BY {col_name | expr | position}

      [ASC | DESC]]

   [ORDER BY {col_name | expr | position}

      [ASC | DESC]]

   [HAVING having_condition ]

   [LIMIT {[offset,] row_count | row_count OFFSET offset}]]

   * select_expr indicates a column you wish to retrieve.

   * table_references indicates the table of tables from which to retrieve rows
** LIMIT is particularly useful for SELECTs which use ORDER BY, DISTINCT and GROUP BY,
because their calculations don't have to involve all the rows. This can speed the query up!
** 4.22.9 IN and NOT IN

#+BEGIN_SRC sql
  SELECT ID, first, last from users WHERE ID IN (250, 251, 252, 254, 255)
#+END_SRC

#+RESULTS:
|  ID | first  | last     |
|-----+--------+----------|
| 250 | John   | Niegos   |
| 251 | Frank  | Poole    |
| 252 | Jim    | Goodlink |
| 254 | Randy  | Johnson  |
| 255 | Darren | Haehnel  |

#+BEGIN_SRC sql
  SELECT ID, first, last from users WHERE ID NOT IN (250, 251, 252, 254, 255) limit 5
#+END_SRC

#+RESULTS:
| ID | first  | last       |
|----+--------+------------|
|  1 | Steve  | Kunkel     |
|  2 | Jim    | Carrey     |
| 62 | Rhonda | Smith      |
| 63 | Greg   | Keller     |
| 64 | Ski    | Baranowski |

** ORDER BY and WHERE
ORDER BY and the WHERE clauses work together.  WHERE excludes certain rows from the result and ORDER BY orders the data by the way specified.

The following sql statement only selects the males and puts them in alphabetical order.  You cannot use the aggregate functions like SUM (), AVG (), or MAX () in the where clause.
#+BEGIN_SRC sql
SELECT male, first, last from bowlers WHERE male = '1' ORDER BY bowlers.last limit 5
#+END_SRC

#+RESULTS:
| male | first   | last     |
|------+---------+----------|
|    1 | Ethan   | Achten   |
|    1 | Jordan  | Adkins   |
|    1 | Manuel  | Agreda   |
|    1 | Spencer | Anderson |
|    1 | John    | Andrews  |

** GROUP BY and HAVING
 Lets you order the query is special ways.  The GROUP BY clause splits up the data into specific groups, and then the having clause lets you perform aggregate functions on each group.  This can let you perform some basic statistics on each group.

 Having filters rows after they have been grouped.

 #+BEGIN_SRC sql
 SELECT male, first, last from bowlers WHERE male = '1' GROUP BY last limit 5
 #+END_SRC

 #+RESULTS:
 | male | first   | last     |
 |------+---------+----------|
 |    1 | Ethan   | Achten   |
 |    1 | Jordan  | Adkins   |
 |    1 | Manuel  | Agreda   |
 |    1 | Spencer | Anderson |
 |    1 | John    | Andrews  |

 #+BEGIN_SRC sql
 SELECT first, last from bowlers group BY first limit 5
 #+END_SRC

 #+RESULTS:
 | first   | last      |
 |---------+-----------|
 | Aaron   | Skibinski |
 | Abbey   | Pitcock   |
 | Abby    | Ludlow    |
 | Abigail | Velchek   |
 | Adam    | Armstrong |
** 4.22.10 EXISTS and ALL

(Compatible: Mysql 4+)
SELECT ownerfirstname, ownerlastname
FROM owner
WHERE EXISTS (SELECT * FROM antiques WHERE item = 'chair')
SELECT buyerid, item
FROM antiques
WHERE price = ALL (SELECT price FROM antiques)

** UNION == UNION DISTINCT
   UNION ALL shows you duplicate values
   UNION DISTINCT shows you only the distinct values

** JOINS
JOINS lets you return data from multiple tables.  When you use joins you should try to specify columns by their
full name.  This prevents ambiguity, improves performance, and ensures no ambiguity is introduced if someone creates a new column name. ie:

#+BEGIN_SRC sql
SELECT bowlers.first, bowlers.last, bowlers.ID, LEFT(activationEmail.activationString, 5) as sig
from bowlers
LEFT JOIN activationEmail on bowlers.ID = activationEmail.ID
limit 5
#+END_SRC

#+RESULTS:
| first   | last      |  ID |   sig |
|---------+-----------+-----+-------|
| Ryan    | Freese    | 113 | 11356 |
| Thomas  | Sosinski  | 120 | 12056 |
| Scott   | Lohman    | 121 | 12156 |
| Michael | Koliboski | 127 | 12756 |
| Joshua  | Spotnock  | 134 |  NULL |

Listing out columns by their full name makes sure that you do not confuse say bowlers.ID with activationEmail.ID.  For example the following query won't work, because the ID clause could is ambiguous.

#+BEGIN_SRC sql
SELECT bowlers.first, bowlers.last, bowlers.ID, LEFT(activationEmail.activationString, 5) as sig
from bowlers
LEFT JOIN activationEmail on ID = ID
limit 5
#+END_SRC

#+RESULTS:

*** creating table aliases
#+BEGIN_SRC sql
SELECT u.first, u.last, schoolID, c.region from users as u
LEFT JOIN conference as c on u.schoolID = c.groupID
WHERE region is NOT NULL
limit 15
#+END_SRC

#+RESULTS:
| first    | last       | schoolID | region              |
|----------+------------+----------+---------------------|
| Jim      | Carrey     |        1 | Testing Conference  |
| Rhonda   | Smith      |       46 | Blue Chip North     |
| Greg     | Keller     |       47 | Blue Chip South     |
| Ski      | Baranowski |       50 | Greater Lake County |
| KC       | Spillers   |       49 | Ft. Wayne           |
| Nancy    | Batlilner  |       50 | Greater Lake County |
| Ed       | Tackett    |       51 | Hoosier             |
| Tom      | Staten     |       53 | Indianapolis South  |
| Karen    | Elrod      |       54 | Indianapolis West   |
| Larry    | Zaideman   |       55 | Lakeshore           |
| Christel | Hughey     |       56 | Michiana Central    |
| Chris    | Harrison   |       57 | Michiana West       |
| DeWayne  | Allen      |       58 | Mid-Central Indiana |
| Jane     | Shafer     |       59 | Mid-Indiana         |
| Chris    | Toyias     |       60 | Northeast           |

*** cartesian joins you join every row of one table to every row of another table
*** left Joins
    Left joins allow one to combine the results from two different tables in one query.  It will only display show matching records.  In the following case, it will display the bowler, their coach, and their school.

    How would I expand this query to get the team's assistant, the manager of that school, etc. w/o using sub-queries.

#+BEGIN_SRC sql
SELECT bowlers.first, bowlers.last, bowlers.ID, bowlers.schoolID as schoolID, schools.name as school,
users.first as coachFirst, users.last as coachLast
from bowlers
LEFT JOIN schools on bowlers.schoolID = schools.schoolID
LEFT JOIN users on bowlers.schoolID = users.schoolID
WHERE users.level = 3
ORDER by bowlers.last
limit 5
#+END_SRC

#+RESULTS:
| first   | last   |  ID | schoolID | school     | coachFirst | coachLast |
|---------+--------+-----+----------+------------+------------+-----------|
| Ethan   | Achten | 662 |      164 | Chesterton | Alexis     | Carter    |
| Ethan   | Achten | 662 |      164 | Chesterton | Derek      | Carter    |
| Ethan   | Achten | 662 |      164 | Chesterton | Henry      | Huyser    |
| Hallee  | Acree  | 985 |      799 | McCutcheon | Jessica    | Hamilton  |
| Jeanica | Adams  | 703 |      129 | Jimtown    | Kevin      | Reed      |

**** why are there NULLs in activationEmail.activationString.  Because left JOIN shows NULL when there is no result
:LOGBOOK:
- State "TODO"       from              [2016-03-15 Tue 08:38]
:END:

#+BEGIN_SRC sql
SELECT bowlers.first, bowlers.last, bowlers.ID, LEFT(activationEmail.activationString, 5) as sig
from bowlers
LEFT JOIN activationEmail on bowlers.ID = activationEmail.ID
WHERE activationString IS NULL
limit 50
#+END_SRC

#+RESULTS:
| first    | last       |  ID | sig  |
|----------+------------+-----+------|
| Joshua   | Spotnock   | 134 | NULL |
| Jordan   | Shepherd   | 161 | NULL |
| Steven   | Haehnel    | 164 | NULL |
| Jeffery  | Mann       | 165 | NULL |
| Scott    | Lohman     | 181 | NULL |
| Brandon  | Haffner    | 223 | NULL |
| jarret   | whitehead  | 226 | NULL |
| Javier   | Onate      | 317 | NULL |
| Jada     | webster    | 323 | NULL |
| Daniel   | Collins    | 326 | NULL |
| Colton   | Schwartz   | 374 | NULL |
| James    | Branson    | 375 | NULL |
| Dillon   | Hasenour   | 376 | NULL |
| Matthew  | Nonte      | 378 | NULL |
| jacob    | brown      | 379 | NULL |
| James    | Kelley     | 380 | NULL |
| Matt     | Musgrave   | 381 | NULL |
| Brett    | Cobban     | 393 | NULL |
| Lindsey  | Lawson     | 398 | NULL |
| Jeffrey  | Severson   | 399 | NULL |
| Nicholas | Graveel    | 400 | NULL |
| Alysha   | Ridgeway   | 401 | NULL |
| Meagan   | Sumption   | 402 | NULL |
| Charles  | Colip      | 409 | NULL |
| Rebecca  | Lohse      | 411 | NULL |
| Heather  | Waters     | 412 | NULL |
| Lauren   | Smith      | 416 | NULL |
| Nicholas | Lacy       | 419 | NULL |
| Alisa    | Lopez      | 422 | NULL |
| Tanner   | Charles    | 445 | NULL |
| Benjamin | Mathewson  | 446 | NULL |
| Ashlee   | Goelz      | 448 | NULL |
| Kaelie   | Loftus     | 450 | NULL |
| Brianna  | Minnich    | 451 | NULL |
| Sarah    | Glant      | 452 | NULL |
| Bryttney | Philpot    | 453 | NULL |
| Alexis   | Shuler     | 454 | NULL |
| Leah     | Kilbride   | 455 | NULL |
| Kyara    | Palmer     | 456 | NULL |
| Ashlee   | Goelz      | 457 | NULL |
| Tyler    | Rodgers    | 462 | NULL |
| Skyler   | Rodgers    | 463 | NULL |
| Alex     | Johnson    | 469 | NULL |
| Jessica  | Davis      | 473 | NULL |
| jimmie   | hubart     | 476 | NULL |
| Montez   | Thompson   | 478 | NULL |
| Kharese  | Battle     | 481 | NULL |
| Austin   | Cartwright | 484 | NULL |
| Garrett  | Cartwright | 486 | NULL |
| James    | Harrison   | 494 | NULL |

*** Types of Joins
Joins are ways of combining portions (or all) of two tables based on some commonality.  For example, the following query gets a user with the last name 'Baggins', and the school that that coach belongs to.

#+BEGIN_SRC sql
SELECT first, last, schools.name
from users
LEFT JOIN schools on users.schoolID = schools.schoolID
WHERE last = 'Baggins'
#+END_SRC

#+RESULTS:
| first | last    | name                                                        |
|-------+---------+-------------------------------------------------------------|
| Frodo | Baggins | This school is for testing purposes please do not delete it |

If you are comparing to based base on rows that are equal, then instead of spelling out the comparison on the JOIN line with ~users.schoolID = schools.schoolID~, you could instead using the USING syntax. Here is the same query using the USING syntax:

#+BEGIN_SRC sql
SELECT first, last, schools.name
from users
LEFT JOIN schools USING (schoolID)
WHERE last = 'Baggins'
#+END_SRC

#+RESULTS:
| first | last    | name                                                        |
|-------+---------+-------------------------------------------------------------|
| Frodo | Baggins | This school is for testing purposes please do not delete it |

Here is the same join using the WHERE syntax.  The WHERE syntax is a little easier to read, but the JOIN syntax is usually better to use on queries that join multiple tables, because then you can specify use natural joins, cross joins, and self joins in the same query!

#+BEGIN_SRC sql
SELECT first, last, schools.name
from users, schools
WHERE last = 'Baggins' and schools.schoolID = users.schoolID
#+END_SRC

#+RESULTS:
| first | last    | name                                                        |
|-------+---------+-------------------------------------------------------------|
| Frodo | Baggins | This school is for testing purposes please do not delete it |

A classic join uses two tables.  The first (or left) and second (or right) table respectively.  In the above example the first table was 'users' and the second was 'schools'.
**** Cross Join
this displays the rows from the left table where every row from the right is added to the left table.  There is no condition that must be met to add columns from the right to the left table.  All rows from both tables are added together.  This is a time consuming query, and it is rarely done.

#+BEGIN_SRC sql
SELECT users.first, users.last, bowlers.first, bowlers.last
FROM users
CROSS JOIN bowlers
limit 5
#+END_SRC

#+RESULTS:
| first  | last       | first  | last     |
|--------+------------+--------+----------|
| Steve  | Kunkel     | Joshua | Spotnock |
| Jim    | Carrey     | Joshua | Spotnock |
| Rhonda | Smith      | Joshua | Spotnock |
| Greg   | Keller     | Joshua | Spotnock |
| Ski    | Baranowski | Joshua | Spotnock |

**** NATURAL JOIN
Joins two tables where the columns that have the same name in both tables also have the same value.  In this case there is only one column with the same name "ID" in both tables. ie:

#+BEGIN_SRC sql
SELECT first, last, LEFT (a.activationString, 2) as string
FROM bowlers
NATURAL JOIN activationEmail as a limit 5
#+END_SRC

#+RESULTS:
| first   | last      | string |
|---------+-----------+--------|
| Jacob   | Deweese   |     10 |
| Ryan    | Freese    |     11 |
| Thomas  | Sosinski  |     12 |
| Scott   | Lohman    |     12 |
| Michael | Koliboski |     12 |

**** INNER JOIN
These are the most common type of joins.  If common columns have the same value, it returns both results.

#+BEGIN_SRC sql
SELECT first, last, LEFT (a.activationString, 2) as string
FROM users
INNER JOIN activationEmail as a ON users.ID = a.ID
limit 5
#+END_SRC

#+RESULTS:
| first   | last      | string |
|---------+-----------+--------|
| Russell | Klosinski |     10 |
| Randy   | Lohman    |     11 |
| Thomas  | Sosinski  |     11 |
| Jerry   | Keppen    |     12 |
| Corky   | Koch      |     12 |

**** LEFT OUTER JOIN
This shows /all/ rows from the left table, even if the right table does not have matching values.  When this happens the right table's results are null in all columns.
**** RIGHT OUTER JOIN
This shows /all/ rows from the right table, even if the left table's values do not match.  When the left table values do not match the right, then the left columns will be null.
**** FULL OUTER JOIN
Returns /all/ rows from the left and right table, regardless if they match.  If both tables do not match, then the resulting rows will be null.
**** SELF JOIN
A join of a table to itself.  This is useful is you have a table that contains coaches and managers.  To find a coach's manager you could do:

#+BEGIN_SRC sql
  SELECT c.first as cFirst, c.last as cLast, c.schoolID as cSchoolID,
  @groupID := s.groupID, LEFT (s.name, 4) as name,
  m.first as mFirst, m.last as mLast, m.schoolID as mSchoolID
  FROM users as c
  LEFT JOIN schools as s on c.schoolID = s.schoolID
  INNER JOIN users as m
  WHERE c.last = 'Baggins'
  AND c.schoolID = m.schoolID
  # AND m.schoolID = @groupID
  limit 5
#+END_SRC

#+RESULTS:
| cFirst | cLast   | cSchoolID | @groupID := s.groupID | name | mFirst | mLast   | mSchoolID |
|--------+---------+-----------+-----------------------+------+--------+---------+-----------|
| Frodo  | Baggins |       714 |                     1 | This | Joshua | Branson |       714 |
| Frodo  | Baggins |       714 |                     1 | This | Frodo  | Baggins |       714 |
| Frodo  | Baggins |       714 |                     1 | This | Harry  | Potter  |       714 |
| Frodo  | Baggins |       714 |                     1 | This | Sam    | Gamgee  |       714 |

** subqueries
   https://dev.mysql.com/doc/refman/5.0/en/subqueries.html
   Subqueries appear in the where or having clause, as a result in the select statement, in the FROM clause
   a subquery can only product a single column of data as a result. Order by is not allowed, doesn't make sense

   to find girls with the same last name as the boys
   SELECT * from girls WHERE last = (SELEct last from boys)

   how find a coach a little better
   #+BEGIN_SRC sql
   select ID, first, last from bowlers WHERE level IN ('3', '6') limit 5
   #+END_SRC

   #+RESULTS:
   |  ID | first   | last     |
   |-----+---------+----------|
   | 121 | Scott   | Lohman   |
   | 413 | Heather | Waters   |
   | 414 | Heather | Waters   |
   | 555 | Jessica | Zaideman |
   | 563 | Michael | Ramos    |

   verses
   SELEct ID from bowlers WHERE level = '3' and level = '6'

   How to find all non coaches
   #+BEGIN_SRC sql
   SELEct ID, first, last from bowlers WHERE level NOT IN ('3', '6') limit 5
   #+END_SRC

   #+RESULTS:
   |  ID | first   | last      |
   |-----+---------+-----------|
   | 113 | Ryan    | Freese    |
   | 120 | Thomas  | Sosinski  |
   | 127 | Michael | Koliboski |
   | 134 | Joshua  | Spotnock  |
   | 161 | Jordan  | Shepherd  |

   Counting all of the assistants, and coaches and embedding a subquery
     #+BEGIN_SRC sql
       SELECT COUNT(IF(level='3',1,NULL)) 'coaches', COUNT(IF(level='4',1,NULL)) 'assistants',
       (SELECT name from schools WHERE schoolID = 714) as schoolName
       from users
     #+END_SRC

     #+RESULTS:
     | coaches | assistants | schoolName                                                  |
     |---------+------------+-------------------------------------------------------------|
     |     145 |         81 | This school is for testing purposes please do not delete it |

**
** BETWEEN
#+BEGIN_SRC sql
SELECT ID from users WHERE ID BETWEEN '5' and '200' limit 5
#+END_SRC

#+RESULTS:
| ID |
|----|
| 62 |
| 63 |
| 64 |
| 65 |
| 66 |

** Aggregate Functions
- MIN (expr)
- MAX (expr)
- SUM (expr)
- AVG (expr)
- COUNT (expr) counts non-null values
- COUNT (*)    counts all values including null

  expr can be anything like "ID * 5 / groupID".

*** what you can and cannot do w/ aggregate functions (AF)
**** An aggregate function cannot appear in the where clause
#+BEGIN_SRC sql
SELECT ID from users WHERE schoolID = MAX(schoolID)
#+END_SRC

#+RESULTS:
**** You allegedly can't mix aggregate and nonaggregate statements in a select statement

BUT I just did with mariadb

#+BEGIN_SRC sql
SELECT ID, MAX(schoolID), first, last from users
#+END_SRC

#+RESULTS:
| ID | MAX(schoolID) | first | last   |
|----+---------------+-------+--------|
|  1 |          1170 | Steve | Kunkel |
**** However you can mix aggregate functions w/ nonaggregate if you use group

This sql statement is nonsensical, but it demonstrates what you can do.

#+BEGIN_SRC sql
SELECT SUM(ID + schoolID), last from users group BY last limit 5
#+END_SRC

#+RESULTS:
| SUM(ID + schoolID) | last      |
|--------------------+-----------|
|                941 | Achten    |
|               1121 | Adams     |
|               1540 | ALEXANDER |
|                132 | Allen     |
|                853 | alley     |
**** You can't use an aggregate function inside another aggregate function

#+BEGIN_SRC sql
SELECT SUM(MAX(ID) + MIN(schoolID)) from users limit 5
#+END_SRC

#+RESULTS:
**** You alledibly can't use use aggregate functions in subqueries, but I did

via mariadb

#+BEGIN_SRC sql
SELECT first, last, LEFT (parentImages.svgData, 5) as svg from bowlers
LEFT JOIN parentImages on bowlers.ID = parentImages.ID
WHERE parentImages.svgData = (SELECT MAX(svgData) from parentImages)
limit 5
#+END_SRC

#+RESULTS:
| first | last   | svg   |
|-------+--------+-------|
| Bria  | Schley | <? |

** Control flow functions
*** IF (val1, val2, val3)
   if val1 is true (val1 != 0 and val1 IS NOT NULL), then it returns val2. Otherwise it returns val3.
*** CASE statement.  Display a table value based on another table value
The case statement is a control flow statement.  It allows you to do change the value of a column in each record
based on some value.  Here's a decent example of a nice case statement, that displays a creates a column "user" that
returns a string value of what the user is, 'admin' or 'manager', or anything else.

# Why is the distinct keyword not working?
#+BEGIN_SRC sql
  SELECT DISTINCT level,
  ID, email, first, last, schoolID,
  CASE level
  WHEN 1 THEN 'admin'
  WHEN 2 THEN 'manager'
  WHEN 3 THEN 'coach'
  WHEN 4 THEN 'assistant'
  WHEN 5 THEN 'inactive manager'
  WHEN 6 THEN 'inactive coach a roster coach should never be here'
  WHEN 7 THEN 'inactive assistant an assistant should never be here.'
  WHEN 8 THEN 'manager that has not clicked on the email'
  WHEN 9 THEN 'coach that has not clicked on the email'
  WHEN 10 THEN 'assistant that has not clicked on the email'
  WHEN 11 THEN 'an archived admin'
  WHEN 12 THEN 'an archived manager'
  WHEN 13 THEN 'an archived coach'
  WHEN 14 THEN 'an archived assistant'
  END as user
  FROM users
  limit 10

#+END_SRC

#+RESULTS:
| level | ID | email                         | first  | last       | schoolID | user    |
|-------+----+-------------------------------+--------+------------+----------+---------|
|     1 |  1 | highschoolbowling@hotmail.com | Steve  | Kunkel     |        0 | admin   |
|     2 |  2 | bransoj@hotmail.com           | Jim    | Carrey     |        1 | manager |
|     2 | 62 | bradam2000@aol.com            | Rhonda | Smith      |       46 | manager |
|     2 | 63 | gjkeller@netsurfusa.net       | Greg   | Keller     |       47 | manager |
|     2 | 64 | rebecb3@bluemarble.net        | Ski    | Baranowski |       50 | manager |
|     2 | 65 | kcspillers@yahoo.com          | KC     | Spillers   |       49 | manager |
|     2 | 66 | nancybatlinerdcc@gmail.com    | Nancy  | Batlilner  |       50 | manager |
|     2 | 67 | edtackett360@gmail.com        | Ed     | Tackett    |       51 | manager |
|     2 | 69 | tomlinstat@aol.com            | Tom    | Staten     |       53 | manager |
|     2 | 70 | karen.elrod@sbcglobal.net     | Karen  | Elrod      |       54 | manager |

#+BEGIN_SRC sql
select distinct level from users order by level limit 10
#+END_SRC

#+RESULTS:
| level |
|-------|
|     1 |
|     2 |
|     3 |
|     4 |
|     6 |
|     9 |
|    10 |
|    13 |
|    14 |

** concat strings

#+BEGIN_SRC sql
SELECT CONCAT(first, ' ', last) as name from users limit 5
#+END_SRC

#+RESULTS:
| name           |
|----------------|
| Steve Kunkel   |
| Jim Carrey     |
| Rhonda Smith   |
| Greg Keller    |
| Ski Baranowski |

** LIKE
   like has 2 special charters
   "%" represents any sequence of chars
   "_" means any 1 char

   #+BEGIN_SRC sql
   SELEcT last from bowlers WHERE last LIKE 'SM_TH' limit 5
   #+END_SRC

   #+RESULTS:
   | last  |
   |-------|
   | Smith |
   | SMITH |
   | Smith |
   | Smith |
   | Smith |

   LIKE also works with numbers
   #+BEGIN_SRC sql
   SELEct ID from bowlers WHERE ID LIKE '_2%' limit 5
   #+END_SRC

   #+RESULTS:
   |  ID |
   |-----|
   | 120 |
   | 121 |
   | 127 |
   | 223 |
   | 226 |

   it will select 123434 or 223434 or 4222323

** SOUNDS LIKE can check if two words sound similiar! wow!
   SELEcT first from girls WHERE first SOUNDS LIKE 'karlie'
** regexp
   SELECT 'string' REGEXP pattern
** GREATEST (value1, value2, value3)
** LEAST (value1, value2, value3)
   returns the smallest value
** LOAD DATA INFILE '/path/to/file' INTO TABLE usbcData
** user defined variables! And Subqueries!
   You can write one query and save the results in a sql variable!  Then you can refer to that variable in the union clause!

   SELECT ID, first, last, schools.schoolID, level, @groupID := schools.groupID as groupID
   FROM users LEFT JOIN schools on users.schoolID=schools.schoolID
   WHERE (level = '3' OR level = '6' OR level = '9') AND (ID = '3')
   UNION ALL SELECT ID, first, last, schoolID, level, schoolID as groupID from users
   WHERE level = '2' AND schoolID = @groupID

   You can get even crazier by defining sql variables that can be used as subqueries

    #+BEGIN_SRC sql
    SELECT users.first, users.last, users.ID, @schoolID := users.schoolID as schoolID, images.ID
    as sig, schools.schoolID,  @groupID := (SELECT groupID FROM schools WHERE schoolID = @schoolID) as groupID,
    (SELECT region FROM conference WHERE groupID = @groupID) as region
    FROM users
    LEFT JOIN schools    ON users.schoolID=schools.schoolID
    LEFT JOIN images     ON users.ID=images.ID
    WHERE (level = '6' OR level = '3') limit 10
    #+END_SRC

    #+RESULTS:
    | first     | last      |  ID | schoolID | sig | schoolID | groupID | region              |
    |-----------+-----------+-----+----------+-----+----------+---------+---------------------|
    | Stephanie | Cooley    |  92 |      714 |  92 |      714 |       1 | Testing Conference  |
    | Janee     | Babbitt   |  93 |       84 |  93 |       84 |      50 | Greater Lake County |
    | Linda     | Olszewski |  95 |       79 |  95 |       79 |      50 | Greater Lake County |
    | Edward    | Olszewski |  96 |       79 |  96 |       79 |      50 | Greater Lake County |
    | Christine | Munsie    |  97 |       89 |  97 |       89 |      50 | Greater Lake County |
    | Patrick   | Cobban    |  98 |       89 |  98 |       89 |      50 | Greater Lake County |
    | Derrick   | Bohn      |  99 |      109 |  99 |      109 |      50 | Greater Lake County |
    | Russell   | Klosinski | 107 |      184 | 107 |      184 |      55 | Michiana Central    |
    | Tami      | Nedza     | 112 |      119 | 112 |      119 |      55 | Michiana Central    |
    | Richard   | Hughey    | 114 |      114 | 114 |      114 |      56 | Michiana West       |

the query up!
** CURDATE()

#+BEGIN_SRC sql
SELECT Year(CURDATE())
#+END_SRC

#+RESULTS:
| Year(CURDATE()) |
|-----------------|
|            2016 |

#+BEGIN_SRC sql
SELECT CURDATE()
#+END_SRC

#+RESULTS:
|  CURDATE() |
|------------|
| 2016-03-10 |

** COUNT
:PROPERTIES:
     :engine:   mysql
     :dbhost:   localhost
     :dbuser:   root
     :dbpassword: GraceLikeRain
     :database: usbc
     :END:
*** Select how many users there are in this database:

#+BEGIN_SRC sql
SELECT COUNT(*) from users
#+END_SRC

#+RESULTS:
| COUNT(*) |
|----------|
|      342 |

*** Select how many coaches and assistants there are.  A coach has level 3 and an assistant has level 4.  VERY COOL!

#+BEGIN_SRC sql
SELECT COUNT(IF(level='3',1,NULL)) 'coach', COUNT(IF(level='4',1,NULL)) 'assistant' from users
#+END_SRC

#+RESULTS:
| coach | assistant |
|-------+-----------|
|   128 |        64 |

*** Throw in a subquery as a column named "schoolName".  How cool is that?
#+BEGIN_SRC sql
  SELECT COUNT(IF(level='3',1,NULL)) 'coaches', COUNT(IF(level='4',1,NULL)) 'assistants',
  (SELECT name from schools WHERE schoolID = 714) as schoolName
  from users
#+END_SRC

#+RESULTS:
| coaches | assistants | schoolName                                                  |
|---------+------------+-------------------------------------------------------------|
|     145 |         81 | This school is for testing purposes please do not delete it |

*** See how many unique first and last names there are

#+BEGIN_SRC sql
  SELECT COUNT(DISTINCT first) as uniqueFirst, COUNT(DISTINCT last) as uniqueLast
  from bowlers
#+END_SRC

#+RESULTS:
| uniqueFirst | uniqueLast |
|-------------+------------|
|         275 |        352 |

** LEFT only show the first n chars of a value

LEFT (<column name>, <number>)

#+BEGIN_SRC sql
  SELECT LEFT (activationString,10), ID, initials, date FROM activationEmail limit 3
#+END_SRC

#+RESULTS:
| LEFT (activationString,10) |   ID | initials | date       |
|----------------------------+------+----------+------------|
| 1000568d9f                 | 1000 |          | 05/16/2000 |
| 1001568f2e                 | 1001 |          | 09/19/1999 |
| 10085696df                 | 1008 |          | 01/08/2016 |
** UPDATE

UPDATE <table> SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE condition]
[ORDER BY ...]
[LIMIT number]

 The UPDATE command alters data in an existing sql database.

 Notice that bowler ID 375 is James Branson.  Let's change that to "Josh Branson".
 #+BEGIN_SRC sql
 SELECT first, last, ID from bowlers WHERE ID = '375'
 #+END_SRC

 #+RESULTS:
 | first | last    |  ID |
 |-------+---------+-----|
 | James | Branson | 375 |

 When you do an update clause, *always* include a where clause.  If a where clause is missing then every column will be updated from your set clause.  In this case, if I did NOT include the WHERE clause, every bowler would have the first name 'Josh'!  It's also good practice to add a limit clause at the end of an update clause.  If you only expect the update command to affect one row, then add the =limit 1=, just to be guarantee that the query will /only/ affect one row.
 #+BEGIN_SRC sql
 UPDATE bowlers SET first='Josh' WHERE ID = '375' limit 1
 #+END_SRC

 #+RESULTS:

 #+BEGIN_SRC sql
 SELECT first, last, ID from bowlers WHERE ID = '375'
 #+END_SRC

 #+RESULTS:
 | first | last    |  ID |
 |-------+---------+-----|
 | Josh  | Branson | 375 |

* fixing pcn
     :PROPERTIES:
     :engine:   mysql
     :dbhost:   localhost
     :dbuser:   root
     :dbpassword: GraceLikeRain
     :database: usbc
     :END:

#+BEGIN_SRC sql
SELECT first from bowlers  WHERE first = 'Joshua' limit 5
#+END_SRC

#+RESULTS:
| first  |
|--------|
| Joshua |
| Joshua |
| Joshua |
| Joshua |
| Joshua |

     #+BEGIN_SRC sql
     UPDATE bowlers SET first = 'Joshua' WHERE first = 'Joshua' limit 5
     #+END_SRC

     #+RESULTS:


code:
#+AUTHOR:Joshua Branson
#+TITLE: Sway
#+LATEX_HEADER: \usepackage{lmodern}
#+LATEX_HEADER: \usepackage[QX]{fontenc}
#+OPTIONS: H:10 toc:nil

https://www.youtube.com/watch?v=j1I63wGcvU4

* Sway

may 5 sway and man sway are helpful.

After sway is running, sway [COMMAND] sends commands to sway.

man 7 xkeyboard-config   has lots of options that I can set for sway.  There's are the options that one sets with setxkbmap.

Model Apple laptop apple_laptop, Macbook/Macbook Pro

Options  ctrl:swapcaps

** man 5 sway

set variable value   cool for newer commands

clipboard content  store some content in the clipboard!

layout mode  sets the layout

layout auto   cycles between layouts

* commands I need to fix

M+s  to stack windows

M+w is tab mode

M+C+direction moves the window
* I can try this is debian
https://www.phoronix.com/scan.php?page=news_item&px=Debian-Experimental-Sway
* I should set the default layout to stacking.

I can set this manually via Mod4+s


code:
#+TITLE:Systemd Cheatsheat
#+AUTHOR: Joshua Branson

* Commands
Systemd will not execute a command as a non-root user, unless, you create a seperate instance of systemd by
following this guide: https://wiki.archlinux.org/index.php/Systemd/User
* journalctl
The systemd journal store logs produced by systemd.  It stores those logs in a binary format, so these logs can only be viewed by journalctl.  Here are some of the commands

- journalctl -b
  show all the logs from this boot

- journalctl --since "20 min ago"
  show all the logs since 20 minutes ago

- journalctl --since "2016-02-18 18:17:16"

- journalctl --since "2016-02-18"

- journalctl _PID=1
  show all logs with a specific process

- journalctl /usr/bin/program
  show all logs from a specific executable


code:
#+TITLE: Joshua Branson
#+AUTHOR:ufw.org
#+LATEX_HEADER: \usepackage{lmodern}
#+LATEX_HEADER: \usepackage[QX]{fontenc}

Ufw is a uncomplicated firewall.