Updated mapserver-mode for editing UMN Mapserver files in Emacs

tl;dr Updated mapserver-mode for Emacs to help editing UMN Mapserver map-files is available at https://github.com/AxxL/mapserver-emacs-mode

I’ve updated the mapserver-mode provided by Hal Mueller. His last change on the code dates 2 years ago, the file was created 10 years ago. Using the mode with the current Mapserver version 6.4.1 was possible but a lot of keywords were not highlighted.

The code seems to follow the Mode Tutorial by Scott Andrew Borton. I left it that way and made some adjustments to the regular expressions. The way to do this is explained in the Mode Tutorial. You have to use the regexp-opt function.

My working files are at the tmp folder.

“keywords_level1_objects.txt” lists the Keywords that are Objects in Mapservers sense. All that are ended with the END keyword.

“keywords_level2_variables.txt” lists the variables that you can use in objects.

“keywords_level3-constants.txt” lists values that you can set to variables.

“regexp-opt-umnmapserver.el” is the file where I copy & paste the lists and start the regexp-opt function. After this I copy & paste the result (from the *Messages* buffer) in my mapserver-mode.el. It is a raw handmade workflow but it’s OK.

“todo_web_object_metadata.txt” lists the metadata for OWS services. They are not highlighted yet.

“issues.org” contains my notes for Todos and issues.

Using Emacs org-mode for a tipping game

I have to give credits for this solution to my colleague Andreas, who wrote the Lisp Code and the Org-Mode table formula. Thanks Andreas.

tl;dr Using Emacs org-mode for a tipping game. Tips are written into an org-mode table. With Lisp and table-formulas it’s easy to count and sum the points, each player got.

Step 1: Rules of the game.

  • Tendency: 2 Points (also when Draw),
  • Goal Difference: 3 Points,
  • Result: 4 Points (also when Draw).

Step 2: Creating the table.

|  1 | DATUM              | G | PARTIE                   | ERG | AXEL | Pnt | ANDR | Pnt |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  2 | WELTMEISTERTIPP    |   |                          | ??? |  Bra |     |  Por |     |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  3 | DO 12.6. 22.00 Uhr | A | Brasilien - Kroatien     | 3-1 |  2-0 |     |  2-1 |     |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  4 | FR 13.6. 18.00 Uhr | A | Mexiko - Kamerun         | 1-0 |  1-0 |     |  2-1 |     |
|  5 | FR 13.6. 21.00 Uhr | B | Spanien - Niederlande    | 1-5 |  1-1 |     |  2-1 |     |
|  6 | FR 13.6. 24.00 Uhr | B | Chile - Australien       | 3-1 |  0-0 |     |  2-1 |     |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  7 | SA 14.6. 18.00 Uhr | C | Kolumbien - Griechenland | 3-0 |  2-0 |     |  2-1 |     |
|  8 | SA 14.6. 21.00 Uhr | D | Uruguay - Costa Rica     | 1-3 |  1-0 |     |  2-1 |     |
|  9 | SA 14.6. 24.00 Uhr | D | England - Italien        | 1-2 |  0-1 |     |  1-2 |     |
| 10 | SA 14.6. 27!!! Uhr | C | Elfenbeinküste - Japan   | 2-1 |  0-2 |     |  2-1 |     |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
| 11 | SUMME              |   |                          |     |      |     |      |     |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  1 | 2                  | 3 | 4                        |   5 |    6 |   7 |    8 |   9 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|

This is a simple table in org-mode containing the columns for the game information (date and time, group, game itself), the column for the result and then the tips by me and Andreas. After each column containing the tips, I’ve included a column for the points, each from us gets for his tip (named “Pnt”). These columns are filled when I run the function below.

I’ve added a column containing the row-numbers at the left and a row containing the column-numbers at the bottom. We need this information for the org-table formula later (org-table-current-column and org-table-current-dline)

Step 3: The calculation logic

This lisp-code gets the actual result of the game (act) and the bet value (erg) to compare them according to the rules above. It calculates either 4, 3, 2 or 0.


: ;; Tipprunde
: (defun wm-calc-pts (act erg)
: (if (or (not (string-match "-" act))
: (not (string-match "-" erg)))
: 0
: (let ((a (string-to-number (car (split-string act "-"))))
: (b (string-to-number (cadr (split-string act "-"))))
: (x (string-to-number (car (split-string erg "-"))))
: (y (string-to-number (cadr (split-string erg "-")))))
: (cond
: ((and (= a x) (= b y)) 4)
: ((= (- b a) (- y x)) 3)
: ((or
: (and (> a b) (> x y))
: (and (< a b) (< x y))
: (and (= a b) (= x y))) 2)
: (t 0)))))

Step 4: The Table Function

The table function is placed below the table. It does two things:

– Use the lisp function wm-calc-pts to calculate the points for each bet.
– Sum the points and write them to a cell.

: #+TBLFM: $7='(wm-calc-pts $5 $6)::@11$7=vsum(@4..@-1)

The two calulcations are divided by the “::” mark.

$7='(wm-calc-pts $5 $6) uses column 7 (Pnt for player Axel) and fills it with the results from the wm-calc-pts function. The input parameters for this function is filled with the values from column $5 (the actual result, act) and column $7 (the bet from player Axel, erg).

@11$7=vsum(@4..@-1) fills the cell at row 11 and column 7 (for player Axel) with the sum from the column, beginning at line 4 and ending at line 10 (11 minus 1).

This is done for each player, so we added an additional stack of formulars for player Andr: $9='(wm-calc-pts $5 $8)::@11$9=vsum(@2..@-1)

The whole formular looks like this:

#+TBLFM: $7='(wm-calc-pts $5 $6)::@11$7=vsum(@2..@-1)::$9='(wm-calc-pts $5 $8)::@11$9=vsum(@2..@-1)

Step 5: Executing the function

Executing the function is easy. Place your cursor inside the line and hit C-c C-c. The values will be calculated and the cells are filled with the calculated values.

|  1 | DATUM              | G | PARTIE                   | ERG | AXEL | Pnt | ANDR | Pnt |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  2 | WELTMEISTERTIPP    |   |                          | ??? |  Bra |   0 |  Por |   0 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  3 | DO 12.6. 22.00 Uhr | A | Brasilien - Kroatien     | 3-1 |  2-0 |   3 |  2-1 |   2 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  4 | FR 13.6. 18.00 Uhr | A | Mexiko - Kamerun         | 1-0 |  1-0 |   4 |  2-1 |   3 |
|  5 | FR 13.6. 21.00 Uhr | B | Spanien - Niederlande    | 1-5 |  1-1 |   0 |  2-1 |   0 |
|  6 | FR 13.6. 24.00 Uhr | B | Chile - Australien       | 3-1 |  0-0 |   0 |  2-1 |   2 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  7 | SA 14.6. 18.00 Uhr | C | Kolumbien - Griechenland | 3-0 |  2-0 |   2 |  2-1 |   2 |
|  8 | SA 14.6. 21.00 Uhr | D | Uruguay - Costa Rica     | 1-3 |  1-0 |   0 |  2-1 |   0 |
|  9 | SA 14.6. 24.00 Uhr | D | England - Italien        | 1-2 |  0-1 |   3 |  1-2 |   4 |
| 10 | SA 14.6. 27!!! Uhr | C | Elfenbeinküste - Japan   | 2-1 |  0-2 |   0 |  2-1 |   4 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
| 11 | SUMME              |   |                          |     |      |  12 |      |  17 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  1 | 2                  | 3 | 4                        |   5 |    6 |   0 |    8 |   0 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
#+TBLFM: $7='(wm-calc-pts $5 $6)::@11$7=vsum(@2..@-1)::$9='(wm-calc-pts $5 $8)::@11$9=vsum(@2..@-1)

Emacs snippet: lookup / search wikipedia, google or any other side

This small snippet looks up a the current word or region in Wikipedia. If the cursor is before a word, it grabs the word. If you have marked a region, it grabs that. The function opens the search site in Wikipedia to lookup your searchterm.

I’ve used the sample from from Xah Lees page (http://ergoemacs.org/emacs/elisp_idioms.html).


;; ,----
;; | Wikipedia Lookup
;; `----
;; Looks up current word in Wikipedia in a browser. If a region is
;; active (a phrase), lookup hat phrase.
;; http://ergoemacs.org/emacs/elisp_idioms.html
(defun wikipedia-lookup ()
(interactive)
(let (myWord myUrl)
(setq myWord
(if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'symbol)))
(setq myUrl
(concat "http://de.wikipedia.org/wiki/Special:Search?search=" myWord))
(browse-url myUrl)))

You can do that for some other sites. Simply set the my Url to a different location:

I’ll give you the example for the emacs wiki. You have to put a ” ” space after the URL parameter “site:emacswiki.org ”


(defun emacswiki-lookup ()
(interactive)
(let (myWord myUrl)
(setq myWord
(if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'symbol)))
(setq myUrl
(concat "http://www.google.de/search?q=site:emacswiki.org " myWord))
(browse-url myUrl)))

If you define more lookups an interactive function would be nice which asks for the specific lookup:

  • [e]macswiki
  • [g]oogle
  • [l]eo
  • [w]ikipedia

Don’t know how to do that. 😉

Emacs navigating {parantheses}

Writing a lot of JavaScript I want to navigate to the end of a method, object or similar. Normally I want to go from the beginning parenthesis “{” to the ending one “}”. The font-lock-mode highlights the opposite parenthesis or the region (if it’s outside the visible screen).

Navigation goes quick with C-M-n or C-M-p.

See the Emacs wiki: http://www.emacswiki.org/emacs/NavigatingParentheses

C-h k C-M-n:

C-M-n runs the command forward-list, which is an interactive compiled Lisp
function in `lisp.el’.

It is bound to C-M-n.

(forward-list &optional ARG)

Move forward across one balanced group of parentheses.
With ARG, do it that many times.
Negative arg -N means move backward across N groups of parentheses.
This command assumes point is not in a string or comment.

C-h k C-M-p:

C-M-p runs the command backward-list, which is an interactive compiled Lisp
function in `lisp.el’.

It is bound to C-M-p.

(backward-list &optional ARG)

Move backward across one balanced group of parentheses.
With ARG, do it that many times.
Negative arg -N means move forward across N groups of parentheses.
This command assumes point is not in a string or comment.

Emacs Tutorial: XEmacs Minibuffer (13.02.2002)

Auf meiner alten Homepage hatte ich im Februar 2002 begonnen, ein Tutorial zum XEmacs zu schreiben. Ich nannte das Ding "XEmacs Minibuffer" und habe damals einiges an Zeit darin investiert. Irgendwie erinnert mich der Text an das Tutorial. Warum ich damals XEmacs genutzt hatte, weiß ich nicht mehr. Entweder war es einfacher an Builds für Windows zu kommen oder die ganze Integration in das Betriebssystem war besser. Oder ich fand ihn einfach cooler.

Irgendwann habe ich zuletzt die alten Sicherungen der Seite gefunden und etwas melancholisch auf das alles geschaut. Am Ende hatte ich die Seite tatsächlich wie eine Linux Shell gestyled, diese Version finde ich aber nur noch im Web-Archiv. Ich habe auch lange auf den XEmacs Text geschaut und mich gefragt, was ich damit machen soll.

Ich habe mich für ein Weiterleben des Textes hier im Blog entschieden. Und möchte mit einem umgewandelten lateinischen Zitat enden: Wem nützt "Cui bono"?


Continue reading

Emacs pretty print JSON (if you have Python)

This short lisp example shows how to define a function that pretty prints your JSON selection in Emacs, based on a standard Python tool. …

My first regards go to the Irreal Blog and the author of Beautifying JSON In Emacs.

My second regards go to Trey Jackson who provided the example for the usage of "shell-command-on-region" (see the Stackoverflow post).

The key element is the "shell-command-on-region" function which calls a Python tool named "json.tool". This small tool beautifies your JSON. The code around is for replacing the selection with the output from the shell command.

USAGE: Mark your JSON text and call M-x pretty-print-json. The selection should be replaced with the beautified JSON.

GET IT: Put the code below to your emacs.el and adjust the path to python.exe. Irreal says Python > 2.6 is required.

WHY? The command provided by Irreal "C-u M-|" is impossible to type on German keyboard layouts. C-u Esc-AltGr-| is too long. Also Python.exe is not in my PATH and I have to type it down by hand (I could have adjusted my path as well) and remember the Python tool name.

;; pretty prints the selection on a json document
;; uses python.
;; adjust the python path and executable.
;; see http://stackoverflow.com/questions/1548605/emacs-lisp-shell-command-on-region
(defun pretty-print-json(&optional b e)
  (interactive "r")
  (shell-command-on-region b e "c:/Python27/python -m json.tool" (current-buffer) t)
)