org-element: Change algorithm for `org-element-at-point'

* contrib/lisp/org-element.el (org-element-at-point): Change
  algorithm.
(org-element-guess-type): Removed function.
(org-element--element-block-types): Removed variable.
(org-element-forward, org-element-backward, org-element-up): Rewrite
functions.
* testing/contrib/lisp/test-org-element.el: Add tests.
org-element: Refactor code
org-agenda: Fix handling of indirect buffer and window

* org-agenda.el (org-agenda-tree-to-indirect-buffer): Fix handling of
indirect buffer and window.

Check if the window of last indirect buffer is still live and properly
handle the case of `org-last-indirect-buffer' being nil.

Changes suggested by Dave Abrahams in <m2boow1q9e.fsf@boostpro.com>.
Merge branch 'maint'
org-export: Do not ignore automatically elements with empty output

* contrib/lisp/org-export.el (org-export-data): org-export: Do not
  ignore automatically elements with empty output.
org-element: Function mapped through org-element-map accepts only one argument

* contrib/lisp/org-element.el (org-element-map): Remove use of an
  alternate communication channel.  Function is now called with only
  one argument.
* contrib/lisp/org-export.el (org-export-collect-tree-properties):
(org-export-collect-headline-numbering, org-export--selected-trees,
org-export-collect-footnote-definitions,
org-export-footnote-first-reference-p,
org-export-get-footnote-number, org-export-resolve-fuzzy-link,
org-export-resolve-id-link, org-export-resolve-ref-link,
org-export-resolve-coderef, org-export-get-ordinal,
org-export-get-loc, org-export-collect-headlines): Apply changes.
* EXPERIMENTAL/org-e-ascii.el (org-e-ascii--unique-links): Apply
  changes.
* EXPERIMENTAL/org-e-publish.el (org-e-publish-collect-index): Apply
  changes.
org-export: Allow user to explicitely ignore parts of parse tree

* contrib/lisp/org-export.el (org-export-collect-tree-properties):
  Do not overwrite any user's ignore list.
* testing/contrib/lisp/test-org-export.el: Add test.

A good way to populate `:ignore-list' is through the use of
`org-export-filter-parse-tree-functions', with the help of
`org-element-map' and `org-export-ignore-element'.  As an example, the
following code will skip every headline containing the word "note"
in its title during a LaTeX export:

(defun user-skip-note-headlines (data backend info)
  ;; For now LaTeX back-end is called `e-latex'.
  (when (eq backend 'test)
    ;; Traverse the parse tree, adding to ignore list any headline
    ;; matching criteria.
    (org-element-map
     data 'headline
     (lambda (headline)
       (when (string-match "\\<note\\>"
                           (org-element-property :raw-value headline))
         (org-export-ignore-element headline info)))
     info))
  ;; Return original DATA.
  data)

Then install it in parse-tree filters:

(add-to-list 'user-skip-note-headlines org-export-filter-parse-tree-functions)

Back-end delevopers will install it via `org-BACKEND-filters-alist'
where BACKEND stands for the name of the back-end considered.  Se
`org-export-filters-alist' for more information.
org-e-latex: Allow nested footnotes

* EXPERIMENTAL/org-e-latex.el (org-e-latex-footnote-reference): Allow
  nested footnotes.
* contrib/lisp/org-element.el (org-element-string-restrictions): Allow
  footnote references within a footnote reference.
* testing/contrib/lisp/test-org-export.el: Add a test for nested
  footnotes.
enhancements to org-babel-goto-named-src-block

> attached is a patch that enhances org-babel-goto-named-src-block (bound
> to C-c C-v g by default).  Included are two enhancements:
>
>  1. the point is pushed to the org-mark-ring, such that returning with
>     C-c & becomes possible
>  2. the target src block is guessed from
>      a) noweb-reference
>      b) #+call:
>      c) #+results:
>      d) symbol-at-point
>     if one of these is found (in that order)

* lisp/ob.el (org-babel-goto-named-src-block): Pushing the point to
  the org-mark-ring and guessing at the code block name to jump to.
add support for MonetDB to SQL code blocks

* lisp/ob-sql.el(org-babel-execute:sql): Add support for MonetDB to
  SQL code blocks.

  Hi,

  the attached patch adds support for evaluating SQL blocks on MonetDB.
  The MonetDB client normally requires the password to be inputted on the
  console. To get around this, you have to use a dotfile ~/.monetdb with
  authentification data. Note that this file is ignored if you specify a
  user on the cmdline. See
  http://www.monetdb.org/Documentation/mclient-man-page for details.

  A usage example follows.

  Setup MonetDB test database and authentification data

  #+BEGIN_SRC sh :results output verbatim
  MONETDB_DIR=$HOME/unix/var/monetdb/demodb
  monetdbd create $MONETDB_DIR
  monetdbd start $MONETDB_DIR
  monetdb create demodb
  monetdb release demodb
  cat > ~/.monetdb <<EOF
  user=monetdb
  password=monetdb
  EOF
  #+END_SRC

  #+RESULTS:
  : created database in maintenance mode: demodb
  : taken database out of maintenance mode: demodb

  Data is returned without column names (the default return format
  cannot be parsed by Babel).

  #+BEGIN_SRC sql :engine monetdb :cmdline demodb
  CREATE TABLE foo ( bar INTEGER );
  SELECT 'Table count', count(*) FROM foo;
  #+END_SRC

  #+RESULTS:
  | Table count | 0 |

  The parameter "-i" is required on the command line in order to use
  special client commands. Also note that a newline is required at the
  end if the last line contains a special client command.

  #+BEGIN_SRC sql :engine monetdb :cmdline demodb -i :results output
  verbatim
  \d
  \?

  #+END_SRC

  #+RESULTS:
  #+begin_example
  TABLE  sys.foo
  \?      - show this message
  \<file  - read input from file
  \>file  - save response in file, or stdout if no file is given
  \|cmd   - pipe result to process, or stop when no command is given
  \h      - show the readline history
  \D table- dumps the table, or the complete database if none given.
  \d[Stvsfn]+ [obj] - list database objects, or describe if obj given
  \A      - enable auto commit
  \a      - disable auto commit
  \e      - echo the query in sql formatting mode
  \f      - format using a built-in renderer {csv,tab,raw,sql,xml}
  \w#     - set maximal page width (-1=unlimited, 0=terminal width,
  >0=limit to num)
  \r#     - set maximum rows per page (-1=raw)
  \L file - save client/server interaction
  \X      - trace mclient code
  \q      - terminate session
  #+end_example

  Have fun!
org-odt.el: Fix a regression in handling TOC

* lisp/org-odt.el (org-odt-insert-toc): Remove this stray,
dysfunctional routine.  This possibly has crept in because of the
broken merges between "maint" and "origin" branches.
(org-odt-begin-table): Don't emit an empty paragraph when a table is
neither labelled or captioned.
(org-odt-init-outfile): Remove reference to an unused variable.
Merge remote-tracking branch 'origin/maint'
org-export: Allow nested footnotes (part 2)

* EXPERIMENTAL/org-e-latex.el (org-e-latex-footnote-reference):
  Correctly handle numbering with nested footnotes.
* contrib/lisp/org-element.el (org-element-map): Apply function to
  element or object before applying it to its secondary string, if
  any.  Otherwise, linearity is broken.
* contrib/lisp/org-export.el (org-export-footnote-first-reference-p,
  org-export-get-footnote-number): Take care of recursive footnotes.
(org-export-get-genealogy): Correctly get genealogy of an item within
a secondary string.
* testing/contrib/lisp/test-org-export.el: Add tests.
org-export: fix bug with nested footnotes in invisible definitions

* contrib/lisp/org-export.el (org-export-collect-footnote-definitions):
  Also collect footnotes in footnote definitions
(org-export-get-footnote-number): Remove unused check.
* testing/contrib/lisp/test-org-export.el: Add test.
org-element: Fix small bug in org-element-map

* contrib/lisp/org-element.el (org-element-map): Don't forget to apply
  function on paragraph even when no object should be mapped.  Just
  don't recurse in it.
org-element: Refactor code
Don't add spurious preceding newline if caption is not above a longtable

* lisp/org-latex.el (org-export-latex-tables): Don't add spurious
  preceding newline if caption is not above a longtable.
Merge branch 'origin-maint'
org-bbdb: Wrap defvar org-bbdb-old in eval-after-load

* org-bbdb.el (org-bbdb-old): Wrap `defvar' so the variable gets
  defined after bbdb was loaded.

Thanks to Nick Dokos for reporting this glitch in
<8273.1330161610@alphaville>.
Merge branch 'maint'