Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

10746 lines (9701 sloc) 438.613 kB
;;; bongo.el --- flexible and usable media player for Emacs
;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Daniel Brockman
;; Copyright (C) 2006, 2007 Daniel Jensen
;; Copyright (C) 2005 Lars Öhrman
;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004, 2005
;; Free Software Foundation, Inc.
;; Author: Daniel Brockman <daniel@brockman.se>
;; Created: September 3, 2005
;; URL: http://github.com/dbrock/bongo
;; This file is part of Bongo.
;; Bongo is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;; Bongo is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty
;; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;; See the GNU General Public License for more details.
;; You should have received a copy of the GNU General Public
;; License along with Bongo (see the file `COPYING'); if not,
;; write to the Free Software Foundation, 51 Franklin Street,
;; Fifth Floor, Boston, MA 02110-1301, USA.
;;; To do:
;; Shuffle operations. It would be nice to have both a
;; random shuffle operation and an interleaving
;; enqueue operation.
;; Sorting operations.
;; Implement `bongo-file-name-roots' so that forcer can try
;; out Bongo. :-)
;; Customizing `bongo-header-line-mode' should have
;; immediate effect on existing Bongo playlist buffers.
;; Implement scrolling for the header line.
;; Better error messages when players fail.
;; Generalize intra-playlist queue.
;; Implement intra-track region repeat.
;; Provide a way to specify that intermediate headers
;; (cf. `bongo-insert-intermediate-headers') should be
;; inserted even for singleton albums.
;; Implement support for crossfading.
;; Optimize `bongo-line-get-property' as much as possible,
;; as it is called lots of times all over the place.
;;; Code:
(eval-when-compile
(require 'cl)
(require 'rx))
(eval-and-compile
(if (<= emacs-major-version 21)
(require 'bongo-emacs21)
;; These are macros.
(defalias 'bongo-define-obsolete-function-alias
'define-obsolete-function-alias)
(defalias 'bongo-define-obsolete-variable-alias
'define-obsolete-variable-alias)
(defalias 'bongo-define-global-minor-mode
'define-global-minor-mode)
(put 'bongo-define-obsolete-function-alias
'lisp-indent-function 'defun)
(put 'bongo-define-obsolete-variable-alias
'lisp-indent-function 'defun)
(put 'bongo-define-global-minor-mode
'lisp-indent-function 'defun)
;; These are functions.
(defalias 'bongo-face-foreground
'face-foreground)
(defalias 'bongo-face-background
'face-background)
(defalias 'bongo-read-directory-name
'read-directory-name)
(defalias 'bongo-run-mode-hooks
'run-mode-hooks)
(defalias 'bongo-read-number
'read-number)
(defalias 'bongo-image-type-from-file-name
'image-type-from-file-name)
(defalias 'bongo-process-get
'process-get)
(defalias 'bongo-process-put
'process-put)
(defalias 'bongo-custom-set-minor-mode
'custom-set-minor-mode)
(defalias 'bongo-customize-mark-as-set
'customize-mark-as-set)
(defalias 'bongo-custom-reevaluate-setting
'custom-reevaluate-setting)))
;; We try to load this library so that we can later decide
;; whether to enable Bongo Last.fm mode by default.
(require 'lastfm-submit nil 'no-error)
;; This is not autoloaded on Emacs 21.
(require 'time-date)
(defgroup bongo nil
"Buffer-oriented media player."
:prefix "bongo-"
:group 'multimedia
:group 'applications)
;;;; Macro definitions
(defmacro with-bongo-buffer (&rest body)
"Execute the forms in BODY in some Bongo buffer.
The value returned is the value of the last form in BODY.
If the current buffer is not a Bongo buffer, switch to
the buffer returned by the function `bongo-buffer'."
(declare (indent 0) (debug t))
`(with-current-buffer
(if (bongo-buffer-p)
(current-buffer)
(bongo-buffer))
,@body))
(defmacro with-temp-bongo-playlist-buffer (&rest body)
"Execute the forms in BODY in a temporary Bongo playlist buffer.
The value returned is the value of the last form in BODY."
(declare (indent 0) (debug t))
`(with-temp-buffer
(bongo-playlist-mode)
,@body))
(defmacro with-temp-bongo-library-buffer (&rest body)
"Execute the forms in BODY in a temporary Bongo library buffer.
The value returned is the value of the last form in BODY."
(declare (indent 0) (debug t))
`(with-temp-buffer
(bongo-library-mode)
,@body))
(defmacro with-bongo-library-buffer (&rest body)
"Execute the forms in BODY in some Bongo library buffer.
The value returned is the value of the last form in BODY.
If the current buffer is not a library buffer, switch to
the buffer returned by the function `bongo-library-buffer'."
(declare (indent 0) (debug t))
`(with-current-buffer
(if (bongo-library-buffer-p)
(current-buffer)
(bongo-library-buffer))
,@body))
(defmacro with-bongo-playlist-buffer (&rest body)
"Execute the forms in BODY in some Bongo playlist buffer.
The value returned is the value of the last form in BODY.
If the current buffer is not a playlist buffer, switch to
the buffer returned by the function `bongo-playlist-buffer'."
(declare (indent 0) (debug t))
`(with-current-buffer
(if (bongo-playlist-buffer-p)
(current-buffer)
(bongo-playlist-buffer))
,@body))
(defmacro with-point-at-bongo-track (point &rest body)
"Execute BODY with point at the Bongo track line at POINT.
If there is no track at POINT, use the next track line.
If there is no next track line, signal an error."
(declare (indent 1) (debug t))
`(save-excursion
(bongo-goto-point ,point)
(when line-move-ignore-invisible
(bongo-skip-invisible))
(let ((line-move-ignore-invisible nil))
(when (not (bongo-track-line-p))
(bongo-goto-point (or (bongo-point-at-next-track-line)
(error "No track at point"))))
,@body)))
(defvar bongo-player-start-imminent nil
"If non-nil, Bongo is just about to start playing some track.
For example, this will be non-nil while switching tracks.
See `with-imminent-bongo-player-start'.")
(defvar bongo-deferred-status-indicator-updates nil
"List of deferred Bongo status indicator updates.
Entries are of the form (FUNCTION . ARGUMENTS).
When `bongo-player-start-imminent' is non-nil, all status
indicator updates should be deferred to prevent flicker.")
(defmacro with-imminent-bongo-player-start (&rest body)
"Execute BODY with `bongo-player-start-imminent' bound to t.
Afterwards, unless `bongo-player-start-imminent' was already bound
to a non-nil value, perform all deferred status indicator updates.
See `bongo-deferred-status-indicator-updates'."
(declare (indent 0) (debug t))
`(progn
(let ((bongo-player-start-imminent t))
,@body)
(unless bongo-player-start-imminent
(dolist (entry (nreverse bongo-deferred-status-indicator-updates))
(apply (car entry) (cdr entry)))
(setq bongo-deferred-status-indicator-updates nil))))
(defmacro bongo-ignore-movement-errors (&rest body)
"Execute BODY; if a Bongo movement error occurs, return nil.
Otherwise, return the value of the last form in BODY."
(declare (indent 0) (debug t))
`(condition-case nil
(progn ,@body)
(bongo-movement-error nil)))
(defmacro bongo-until (test &rest body)
"If TEST yields nil, evaluate BODY... and repeat.
The order of execution is thus TEST, BODY..., TEST, BODY..., TEST,
and so on, until TEST returns non-nil.
Return the final value of TEST.
\(fn TEST BODY...)"
(declare (indent 1) (debug t))
(let ((result (gensym)))
`(let (,result)
(while (unless (setq ,result ,test)
(prog1 t
,@body)))
,result)))
;;;; Commonly-used variables
(defvar bongo-backends '()
"List of symbols naming available Bongo player backends.
The backend data for each entry is stored in the `bongo-backend'
property of the backend name symbol.")
(defvar bongo-backend-matchers '()
"List of Bongo player backend matchers.
See `bongo-custom-backend-matchers' for more information.")
(defvar bongo-player nil
"The currently active player for this buffer, or nil.
This variable is only used in Bongo mode buffers.")
(make-variable-buffer-local 'bongo-player)
(defvar bongo-seek-buffer nil
"The current interactive Bongo Seek buffer, or nil.")
;;;; Customization variables
(defcustom bongo-enabled-backends nil
"Dummy declaration."
:group 'bongo)
(defcustom bongo-custom-backend-matchers nil
"Dummy declaration."
:group 'bongo)
(defun bongo-evaluate-backend-defcustoms ()
"Define `bongo-enabled-backends' and `bongo-custom-backend-matchers'.
This should be done whenever `bongo-backends' changes, so that
the `defcustom' options can be updated."
(custom-declare-variable 'bongo-enabled-backends
`',(apply 'nconc
(mapcar (lambda (backend-name)
(when (executable-find
(bongo-backend-program-name
(bongo-backend backend-name)))
(list backend-name)))
bongo-backends))
"List of names of enabled Bongo player backends.
See `bongo-backends' for a list of available backends."
:type `(list (set :inline t :format "%v"
,@(mapcar (lambda (backend-name)
`(const :tag ,(bongo-backend-pretty-name
backend-name)
,backend-name))
bongo-backends)))
:set (lambda (name value)
(set-default name value)
(when (fboundp 'bongo-buffer-p)
(dolist (buffer (if custom-local-buffer
(list (current-buffer))
(buffer-list)))
(when (bongo-buffer-p buffer)
(with-current-buffer buffer
(bongo-update-enabled-backends-list))))))
:group 'bongo)
(bongo-custom-reevaluate-setting 'bongo-enabled-backends)
(custom-declare-variable 'bongo-custom-backend-matchers nil
"List of custom Bongo player backend matchers.
Entries are rules of the form (BACKEND-NAME . MATCHER).
BACKEND-NAME is either `ignore' (which tells Bongo to ignore
the matched files), or a symbol naming the backend to use.
MATCHER specifies which files the rule applies to;
it is given to `bongo-file-name-matches-p'.
This option overrides `bongo-enabled-backends' in that disabled
backends will still be used if these rules say so. In addition,
it always takes precedence over `bongo-backend-matchers'.
For example, let's say that you want to use VLC instead of
mpg123 to play MP3 files, use speexdec to play \".speex\" files
in addition to \".spx\" files, and ignore WAV files altogether.
Then you could use the following setting:
(setq bongo-custom-backend-matchers
'((vlc local-file \"mp3\")
(speexdec local-file \"speex\")
(ignore local-file \"wav\")))"
:type
`(repeat
(cons :format "%v"
(choice :tag "Backend"
(const :tag "Ignore matching files" ignore)
,@(mapcar (lambda (backend-name)
(list 'const
:tag (bongo-backend-pretty-name
(bongo-backend backend-name))
backend-name))
bongo-backends)
(symbol :tag "Other backend"))
(cons :format "%v"
(repeat :tag "File types"
:value (local-file)
(choice :tag "Type"
(const :tag "Local file" local-file)
(string :tag "\
URI (specify scheme followed by a colon)")))
(choice :tag "Matcher"
(repeat :tag "File extensions" string)
(regexp :tag "File name (regexp)")
(function :tag "File name (predicate)")
(const :tag "All files" t)))))
:group 'bongo))
(bongo-evaluate-backend-defcustoms)
(bongo-define-obsolete-variable-alias
'bongo-preferred-backends
'bongo-custom-backend-matchers nil
"This is an obsolete name for `bongo-custom-backend-matchers'.
Please read the documentation for that variable, as the new usage
differs slightly from the old.")
(defcustom bongo-default-directory nil
"Default directory for Bongo buffers, or nil.
If nil, use the value of `default-directory' when the buffer is created."
:type '(choice (const :tag "None in particular" nil)
directory)
:group 'bongo
:group 'bongo-file-names)
(defcustom bongo-prefer-library-buffers t
"If non-nil, prefer library buffers over playlist buffers.
This affects what kind of buffer is created by `\\[bongo]' when there
are no existing Bongo buffers.
Regardless of this setting, you can switch to a specific type of Bongo
buffer using the commands `\\[bongo-library]' and `\\[bongo-playlist]'.
To create a new library or playlist, create a new buffer and then switch to
the right mode using `\\[bongo-library-mode]' or `\\[bongo-playlist-mode]'.
If you set this variable to nil, you can happily use Bongo without ever
seeing a library buffer (unless you create one yourself, of course)."
:type 'boolean
:group 'bongo)
(make-obsolete-variable 'bongo-avoid-interrupting-playback
"use playback command prefix arguments instead"
"2006-12-03")
(defcustom bongo-display-playlist-after-enqueue t
"Whether to display the playlist after enqueuing a track."
:type 'boolean
:group 'bongo)
(defcustom bongo-mark-played-tracks nil
"Whether to mark all tracks that have been played.
Tracks marked as played are not selected for random playback.
Enabling Bongo Sprinkle mode sets this variable locally;
see `bongo-sprinkle-mode' for more about that.
Played tracks are displayed in the face `bongo-played-track'.
To delete all played tracks, use `\\[bongo-flush-playlist]'.
To clear the mark for all tracks, use `\\[bongo-reset-playlist]'."
:type 'boolean
:group 'bongo)
(defcustom bongo-confirm-flush-playlist t
"If non-nil, ask for confirmation before flushing the playlist.
This affects only the command `\\[bongo-flush-playlist]'.
In particular, `\\[bongo-erase-buffer]' and `\\[bongo-delete-played-tracks]'
never ask for confirmation, regardless of the value of this variable."
:type 'boolean
:group 'bongo)
(defcustom bongo-default-playlist-buffer-name "*Bongo Playlist*"
"The name of the default Bongo playlist buffer."
:type 'string
:group 'bongo)
(defcustom bongo-default-library-buffer-name "*Bongo Library*"
"The name of the default Bongo library buffer."
:type 'string
:group 'bongo)
(defcustom bongo-next-action 'bongo-play-next-or-stop
"The function to call after the current track finishes playing."
:type '(choice
(function-item :tag "Stop playback"
bongo-stop)
(function-item :tag "Play the next track"
bongo-play-next-or-stop)
(function-item :tag "Play the same track again"
bongo-replay-current)
(function-item :tag "Play the previous track"
bongo-play-previous-or-stop)
(function-item :tag "Play a random track"
bongo-play-random-or-stop))
:group 'bongo)
(make-variable-buffer-local 'bongo-next-action)
(defvar bongo-stored-next-action nil
"The old value of `bongo-next-action'.
This variable is used by `bongo-play-queued'.")
(defgroup bongo-file-names nil
"File names and file name parsing in Bongo.
If your files do not have nice names but do have nice tags, then you
can use the `tree-from-tags.rb' tool (shipped with Bongo) to create a
hierarchy of nicely-named links to your files."
:group 'bongo)
(defcustom bongo-file-name-field-separator " - "
"String used to split track file names into fields.
For example, if your tracks are named like this,
Frank Morton - 2004 - Frank Morton - 01 - Pojken på Tallbacksvägen.ogg
and your file name field separator is \" - \" (which is the default),
then the fields are \"Frank Morton\", \"2004\", \"Frank Morton\", \"01\",
and \"Pojken på Tallbacksvägen\".
When the the fields of a track's file name have been extracted,
they are used to build an infoset.
This is used by `bongo-default-infoset-from-file-name'."
:type 'string
:group 'bongo-file-names)
(defcustom bongo-file-name-album-year-regexp
"\\`\\([0-9]\\{4\\}\\|'?[0-9]\\{2\\}\\)\\'"
"Regexp matching album years.
This is used by `bongo-default-infoset-from-file-name'."
:type 'regexp
:group 'bongo-file-names)
(defcustom bongo-file-name-track-index-regexp "\\`[0-9]+\\'"
"Regexp matching track indices.
This is used by `bongo-default-infoset-from-file-name'."
:type 'regexp
:group 'bongo-file-names)
(defcustom bongo-album-cover-file-names
'("cover.jpg" "cover.jpeg" "cover.png"
"front.jpg" "front.jpeg" "front.png"
"album.jpg" "album.jpeg" "album.png")
"File names of images that should be considered album covers.
See also `bongo-insert-album-covers'."
:type '(repeat string)
:group 'bongo-file-names)
(defcustom bongo-update-references-to-renamed-files 'ask
"Whether to search all Bongo buffers after renaming a file.
If nil, never search through any buffers after renaming a file.
If `ask', prompt the user every time.
If any other value, always perform the search.
You can rename a file from Bongo using `bongo-rename-line'."
:type '(choice (const :tag "Never" nil)
(const :tag "Ask" ask)
(other :tag "Always" t))
:group 'bongo
:group 'bongo-file-names)
(defun bongo-format-string (format)
"Short for (apply 'concat (mapcar 'eval FORMAT))."
(apply 'concat (mapcar 'eval format)))
(defgroup bongo-display nil
"Display of Bongo playlist and library buffers."
:group 'bongo)
(defcustom bongo-field-separator
(if (and (fboundp 'char-displayable-p)
(char-displayable-p ?—))
" —— " " -- ")
"String used to separate field values in track descriptions.
This is used by the function `bongo-default-format-field'."
:type '(choice (const :tag " —— (Unicode dashes)" " —— ")
(const :tag " -- (ASCII dashes)" " -- ")
string)
:group 'bongo-display)
(defcustom bongo-insert-album-covers (and window-system t)
"Whether to put album cover images into Bongo buffers.
See also `bongo-album-cover-file-names'."
:type 'boolean
:link '(custom-group-link bongo-file-names)
:group 'bongo-display)
(defcustom bongo-join-inserted-tracks t
"Whether to automatically join newly-inserted tracks.
This is done by repeatedly running `bongo-join'."
:type 'boolean
:group 'bongo-display)
(defcustom bongo-insert-intermediate-headers nil
"Whether to automatically insert intermediate headers.
This is best explained by an example. Say you have the
following section,
[Frank Morton —— Frank Morton (2004)]
01. Pojken på Tallbacksvägen
02. Kanske det blir så att jag måste gå
and you insert the following section immediately afterwards.
[Frank Morton —— Jag såg en film om en gammal man (2005)]
01. Det är så mysigt att vara två
02. Labyrinten
If this variable is nil, the result will be as follows:
[Frank Morton —— Frank Morton (2004)]
01. Pojken på Tallbacksvägen
02. Kanske det blir så att jag måste gå
[Frank Morton —— Jag såg en film om en gammal man (2005)]
01. Det är så mysigt att vara två
02. Labyrinten
On the other hand, if it is non-nil, the result will be as follows:
[Frank Morton]
[Frank Morton (2004)]
01. Pojken på Tallbacksvägen
02. Kanske det blir så att jag måste gå
[Jag såg en film om en gammal man (2005)]
01. Det är så mysigt att vara två
02. Labyrinten
Notice that an intermediate header ``[Frank Morton]'' was inserted."
:type 'boolean
:group 'bongo-display)
(defcustom bongo-album-function
'bongo-default-album
"Function for displaying albums in Bongo.
Value is a funcion, evaluating to a string or nil.
When the function is evaluated,
- `bongo-title' is bound to the formatted album title;
- `bongo-year' is bound to the formatted album year or nil;
- `bongo-album' is bound to the contents of the `album' field;
- `bongo-infoset' is bound to the whole infoset;
- `bongo-target' is short for `bongo-infoset-formatting-target';
- `bongo-line' is short for `bongo-infoset-formattnig-target-line'.
This variable is used by the function `bongo-default-format-field'."
:type 'function
:options '(bongo-default-album)
:group 'bongo-display)
(defun bongo-default-album ()
(concat
bongo-title
(when bongo-year
(concat " (" bongo-year ")"))))
(defcustom bongo-display-track-lengths t
"Whether to display track lengths in Bongo playlist buffers.
See also `bongo-track-length-column'."
:type 'boolean
:group 'bongo-display)
(defcustom bongo-track-length-column 60
"Column at which to align track lengths in Bongo playlist buffers.
See also `bongo-display-track-lengths'."
:type 'integer
:group 'bongo-display)
(defcustom bongo-track-function
'bongo-default-track
"Function for displaying tracks in Bongo.
Value is a function, evaluating to a string or nil.
When the function is evaluated,
- `bongo-title' is bound to the formatted track title;
- `bongo-index' is bound to the formatted track index or nil;
- `bongo-length' is bound to the formatted track length or nil;
- `bongo-track' is bound to the contents of the `track' field;
- `bongo-infoset' is bound to the whole infoset;
- `bongo-target' is short for `bongo-infoset-formatting-target';
- `bongo-line' is short for `bongo-infoset-formatting-target-line'.
This variable is used by the function `bongo-default-format-field'."
:type 'function
:options '()
:group 'bongo-display)
(defun bongo-default-track ()
(concat
(when bongo-index
(concat bongo-index ". "))
bongo-title
(when (and bongo-display-track-lengths bongo-length
(bongo-playlist-buffer-p bongo-target))
(concat (let ((other-fields-width
(with-temp-buffer
(insert (bongo-format-infoset
`((track (length . nil)
,@bongo-track)
,@bongo-infoset)))
(current-column)))
(indentation-width
(* (length bongo-indentation-string)
(bongo-line-indentation bongo-line))))
(make-string (max 0 (- bongo-track-length-column
indentation-width
other-fields-width)) 32))
" " bongo-length))))
(defcustom bongo-track-length-function
'bongo-default-track-length
"Function for displaying track lengths in Bongo.
Value is a Function, evaluating to a string or nil.
When the function is evaluated,
- `bongo-length' is bound to the length of the track in seconds;
- `bongo-track' is bound to the contents of the `track' field;
- `bongo-infoset' is bound to the whole infoset;
- `bongo-target' is short for `bongo-infoset-formatting-target';
- `bongo-line' is short for `bongo-infoset-formattnig-target-line'."
:type 'function
:options '(bongo-default-track-length)
:group 'bongo-display)
(defun bongo-default-track-length ()
(concat "[" (bongo-format-seconds bongo-length) "]"))
(defcustom bongo-action-format
'("Action: " bongo-action-description)
"Template for displaying action tracks in Bongo.
Value is a list of expressions, each evaluating to a string or nil.
The values of the expressions are concatenated.
When the expressions are evaluated,
- `bongo-action-description' is bound to the action description;
- `bongo-action-expression' is bound to the action expression;
- `bongo-target' is short for `bongo-infoset-formatting-target';
- `bongo-line' is short for `bongo-infoset-formatting-target-line'."
:type '(repeat sexp)
:group 'bongo-display)
(defcustom bongo-stream-format
'((or bongo-uri-title bongo-stream-name bongo-uri)
(when bongo-stream-genre
(concat " (" bongo-stream-genre ")"))
(when bongo-stream-part-title
(concat ": " bongo-stream-part-title)))
"Template for displaying stream tracks in Bongo.
Value is a list of expressions, each evaluating to a string or nil.
The values of the expressions are concatenated.
When the expressions are evaluated,
- `bongo-uri' is bound to the URI of the stream;
- `bongo-uri-title' is bound to the URI title or nil;
- `bongo-stream-name' is bound to the stream name or nil;
- `bongo-stream-genre' is bound to the stream genre or nil;
- `bongo-stream-part-title' is bound to the stream part title;
- `bongo-target' is short for `bongo-infoset-formatting-target';
- `bongo-line' is short for `bongo-infoset-formatting-target-line'."
:type '(repeat sexp)
:group 'bongo-display)
(defcustom bongo-track-line-format
'((bongo-format-infoset bongo-internal-infoset))
"Template for displaying track lines in Bongo.
Value is a list of expressions, each evaluating to a string or nil.
The values of the expressions are concatenated.
When the expressions are evaluated,
- `bongo-internal-infoset' is bound to the internal infoset;
- `bongo-infoset' is bound to the whole infoset;
- `bongo-target' is short for `bongo-infoset-formatting-target';
- `bongo-line' is short for `bongo-infoset-formatting-target-line'."
:type '(repeat sexp)
:group 'bongo-display)
(defcustom bongo-display-header-icons t
"Whether to display icons for header lines in Bongo buffers."
:type 'boolean
:group 'bongo-display)
(defcustom bongo-expanded-header-icon "expanded-header-icon.png"
"File name of icon to use for header lines of expanded sections.
If nil, do not use any icon."
:type '(choice file (const :tag "None" nil))
:group 'bongo-display)
(defcustom bongo-collapsed-header-icon "collapsed-header-icon.png"
"File name of icon to use for header lines of collapsed sections.
If nil, do not use any icon."
:type '(choice file (const :tag "None" nil))
:group 'bongo-display)
(defcustom bongo-expanded-header-format "[%s]"
"Template for displaying header lines for expanded sections.
%s means the header line content.
This variable is only used when not displaying header icons."
:type 'string
:group 'bongo-display)
(defcustom bongo-collapsed-header-format "[%s ...]"
"Template for displaying header lines for collapsed sections.
%s means the header line content.
This variable is only used when not displaying header icons."
:type 'string
:group 'bongo-display)
(defcustom bongo-section-header-line-format
'((if (and bongo-display-header-icons (display-images-p))
(bongo-format-infoset bongo-internal-infoset)
(format (if bongo-collapsed
bongo-collapsed-header-format
bongo-expanded-header-format)
(bongo-format-infoset bongo-internal-infoset))))
"Template for displaying header lines in Bongo.
Value is a list of expressions, each evaluating to a string or nil.
The values of the expressions are concatenated.
When the expressions are evaluated,
- `bongo-internal-infoset' is bound to the internal infoset;
- `bongo-infoset' is bound to the whole infoset;
- `bongo-collapsed' is non-nil if the section is collapsed;
- `bongo-target' is short for `bongo-infoset-formatting-target';
- `bongo-line' is short for `bongo-infoset-formatting-target-line'.
The values of the expressions are concatenated."
:type '(repeat sexp)
:group 'bongo-display)
(defcustom bongo-indentation-string " "
"String prefixed to lines once for each level of indentation."
:type 'string
:group 'bongo-display)
(defgroup bongo-track-icons nil
"Display of track icons in Bongo buffers."
:group 'bongo-display)
(defcustom bongo-display-track-icons t
"Whether to display icons for track lines in Bongo buffers."
:type 'boolean
:group 'bongo-track-icons
:group 'bongo-display)
(defcustom bongo-unknown-local-file-track-icon
"unknown-local-file-track-icon.png"
"File name of icon to use for unknown local file tracks."
:type '(choice file (const :tag "None" nil))
:group 'bongo-track-icons)
(defcustom bongo-local-audio-file-track-icon
"local-audio-file-track-icon.png"
"File name of icon to use for local audio file tracks.
If nil, use the same icon as for unknown local file tracks."
:type '(choice file (const :tag "\
Same as for unknown local file tracks" nil))
:group 'bongo-track-icons)
(defcustom bongo-local-video-file-track-icon
"local-video-file-track-icon.png"
"File name of icon to use for video file tracks.
If nil, use the same icon as for unknown file tracks."
:type '(choice file (const :tag "\
Same as for unknown local file tracks" nil))
:group 'bongo-track-icons)
(defcustom bongo-audio-cd-track-icon "audio-cd-track-icon.png"
"File name of icon to use for audio CD tracks.
If nil, use the same icon as for local audio file tracks."
:type '(choice file (const :tag "\
Same as for local audio file tracks" nil))
:group 'bongo-track-icons)
(defcustom bongo-uri-track-icon "uri-track-icon.png"
"File name of icon to use for URI tracks.
If nil, do not use any icon at all."
:type '(choice file (const :tag "None" nil))
:group 'bongo-track-icons)
(defcustom bongo-action-track-icon "action-track-icon.png"
"File name of icon to use for action tracks.
If nil, do not use any icon at all."
:type '(choice file (const :tag "None" nil))
:group 'bongo-track-icons)
(defcustom bongo-currently-playing-track-icon nil
"File name of icon to use for currently playing tracks.
If nil, use the same icon as for other tracks."
:type '(choice file (const :tag "Same as for other tracks" nil))
:group 'bongo-track-icons)
(defcustom bongo-played-track-icon nil
"File name of icon to use for played track lines.
If nil, use the same icon as for unplayed tracks."
:type '(choice file (const :tag "Same as for other tracks" nil))
:group 'bongo-track-icons)
(require 'find-func)
(defvar bongo-etc-directory
(expand-file-name "etc" (file-name-directory (find-library-name "bongo"))))
(defun bongo-find-image (file-name &optional face)
(let ((image-load-path (cons bongo-etc-directory
(and (boundp 'image-load-path)
image-load-path))))
(find-image
(list (list :ascent 'center
:file file-name
:type (bongo-image-type-from-file-name file-name)
:foreground (bongo-face-foreground
(or face 'default) nil 'default)
:background (bongo-face-background
(or face 'default) nil 'default))))))
(defun bongo-make-image-string (image)
"Return a string with IMAGE in its `display' property."
(propertize " " 'display image))
(defun bongo-make-image-placeholder-string (image)
"Return a blank string taking up as much space as IMAGE would."
(let ((size (image-size image t)))
(propertize " " 'display `(space :width (,(car size))
:height (,(cdr size))))))
(defun bongo-line-icon-string ()
"Return the string to use as an icon for the current line."
(let ((file-name
(cond ((and (bongo-track-line-p) bongo-display-track-icons)
(cond ((and (bongo-currently-playing-track-line-p)
bongo-currently-playing-track-icon)
bongo-currently-playing-track-icon)
((and (bongo-played-track-line-p)
bongo-played-track-icon)
bongo-played-track-icon)
((bongo-audio-cd-track-line-p)
bongo-audio-cd-track-icon)
((bongo-uri-track-line-p)
bongo-uri-track-icon)
((bongo-action-track-line-p)
bongo-action-track-icon)
((bongo-local-audio-file-track-line-p)
(or bongo-local-audio-file-track-icon
bongo-unknown-local-file-track-icon))
((bongo-local-video-file-track-line-p)
(or bongo-local-video-file-track-icon
bongo-unknown-local-file-track-icon))
((bongo-local-file-track-line-p)
bongo-unknown-local-file-track-icon)))
((and (bongo-header-line-p) bongo-display-header-icons)
(if (bongo-collapsed-header-line-p)
bongo-collapsed-header-icon
bongo-expanded-header-icon)))))
(when file-name
(bongo-make-image-string (bongo-find-image file-name)))))
(defgroup bongo-header-line nil
"Display of header lines in Bongo playlist buffers."
:group 'bongo
:group 'bongo-display)
(defcustom bongo-header-line-mode t
"Whether to display header lines in Bongo playlist buffers."
:type 'boolean
:initialize 'custom-initialize-default
:set 'bongo-custom-set-minor-mode
:group 'bongo-header-line)
(defcustom bongo-header-line-playing-string "Playing:"
"String to display in the header line when a track is playing."
:type 'string
:group 'bongo-header-line)
(defcustom bongo-header-line-paused-string "Paused: "
"String to display in the header line when a track is paused."
:type 'string
:group 'bongo-header-line)
(defun bongo-header-line-playback-status ()
"Return the string to use for header line playback status."
(when (bongo-playing-p)
(if (bongo-paused-p)
bongo-header-line-paused-string
bongo-header-line-playing-string)))
(defcustom bongo-header-line-function
'bongo-default-header-line-function
"Function for Bongo playlist header lines."
:type 'function
:options '(bongo-default-header-line-function)
:group 'bongo-header-line)
(defun bongo-default-header-line-function ()
"Return String of Playback status and Track description"
(concat (bongo-header-line-playback-status) " "
(bongo-formatted-infoset)))
(defvar bongo-header-line-string nil
"Bongo header line string.
Value is derived from `bongo-header-line-function'.
The name of this variable should go in `header-line-format'.")
(make-variable-buffer-local 'bongo-header-line-string)
(put 'bongo-header-line-string 'risky-local-variable t)
(defun bongo-update-header-line-string (&rest dummy)
"Update `bongo-header-line-string' using `bongo-header-line-format'.
If Bongo is not playing anything, set the header line string to nil.
Accept DUMMY arguments to ease hook usage."
(when (bongo-buffer-p)
(let ((new-header-line-format header-line-format)
(new-bongo-header-line-string bongo-header-line-string))
(when (null new-header-line-format)
(setq new-header-line-format '("")))
(if bongo-header-line-mode
(add-to-list 'new-header-line-format
'bongo-header-line-string t)
(setq new-header-line-format
(remq 'bongo-header-line-string new-header-line-format)))
(setq new-bongo-header-line-string
(when (bongo-playing-p)
(funcall bongo-header-line-function)))
(when (or (equal new-header-line-format '(""))
(and (equal new-header-line-format
'("" bongo-header-line-string))
(null new-bongo-header-line-string)))
(setq new-header-line-format nil))
(if (not bongo-player-start-imminent)
(setq header-line-format new-header-line-format
bongo-header-line-string new-bongo-header-line-string)
(add-to-list 'bongo-deferred-status-indicator-updates
(list 'set 'header-line-format
new-header-line-format))
(add-to-list 'bongo-deferred-status-indicator-updates
(list 'set 'bongo-header-line-string
new-bongo-header-line-string))))))
(defun bongo-header-line-mode (argument &optional called-interactively-p)
"Toggle display of Bongo mode line indicator on or off.
With ARGUMENT equal to `toggle', or interactively
with no prefix argument, toggle the mode.
With zero or negative ARGUMENT, turn the mode off.
With any other ARGUMENT, turn the mode on.
When called interactively, CALLED-INTERACTIVELY-P is non-nil."
;; Use `toggle' rather than (if mode 0 1) so that using
;; `repeat-command' still does the toggling correctly.
(interactive (list (or current-prefix-arg 'toggle)
'called-interactively-p))
(setq bongo-header-line-mode
(if (eq argument 'toggle)
(not bongo-header-line-mode)
(> (prefix-numeric-value argument) 0)))
(when called-interactively-p
(bongo-customize-mark-as-set 'bongo-header-line-mode))
(when (interactive-p)
(message "Bongo header line mode %s."
(if bongo-header-line-mode
"enabled" "disabled")))
bongo-header-line-mode)
(defgroup bongo-mode-line nil
"Display of Bongo mode line indicators."
:group 'bongo
:group 'bongo-display)
(defcustom bongo-display-playback-mode-indicator t
"Display playback mode indicators in playlist buffer mode lines.
These indicate which playback mode is in effect.
However, nothing is shown for normal in-order playback.
To change playback mode, try (for example) `\\[bongo-random-playback-mode]'."
:type 'boolean
:group 'bongo-mode-line
:group 'bongo-display)
(defcustom bongo-mode-line-indicator-mode t
"Display a Bongo playback status indicator in the global mode line.
See `bongo-mode-line-indicator-format'."
:type 'boolean
:initialize 'custom-initialize-default
:set 'bongo-custom-set-minor-mode
:group 'bongo-mode-line)
(defvar bongo-player-times-last-updated 0
"Time in seconds when player output was last updated because of
player times changed")
(defun bongo-hyphen-padded-mode-line-p ()
"Return non-nil if the mode line is padded with hyphens.
That is, if `mode-line-format' ends with a string ending with \"%-\"."
(and (listp mode-line-format)
(let ((last (car (last mode-line-format))))
(and (stringp last)
(string-match "%-$" last)))))
(defun bongo-mode-line-pad-string ()
"Return the string to use for padding in the mode line.
This is either \"-\" or \" \", depending on the return value of
the function `bongo-hyphen-padded-mode-line-p'."
(if (bongo-hyphen-padded-mode-line-p) "-" " "))
(defcustom bongo-mode-line-indicator-function
'bongo-default-mode-line-indicator-function
"Function for the Bongo mode line indicator."
:type 'function
:options '(bongo-default-mode-line-indicator-function)
:group 'bongo-mode-line)
(defun bongo-default-mode-line-indicator-function ()
(concat (bongo-mode-line-pad-string)
(when (bongo-hyphen-padded-mode-line-p) "[")
(bongo-mode-line-volume-button)
(when (require 'volume nil t) " ")
(bongo-mode-line-backward/previous-button)
(bongo-mode-line-pause/resume-button)
(bongo-mode-line-start/stop-button)
(bongo-mode-line-forward/next-button)
(when (bongo-playing-p) " ")
(when (bongo-playing-p)
(cond ((and (bongo-elapsed-time) (bongo-total-time))
(format "%d%%" (/ (* 100.0 (bongo-elapsed-time))
(bongo-total-time))))
((bongo-elapsed-time)
(bongo-format-seconds (bongo-elapsed-time)))))
(when (bongo-hyphen-padded-mode-line-p) "]")
(bongo-mode-line-pad-string)
(when (bongo-hyphen-padded-mode-line-p)
(bongo-mode-line-pad-string))))
(defcustom bongo-mode-line-indicator-parent 'global-mode-string
"List variable in which to put the Bongo mode line indicator.
Value is the name of a variable whose value is a list.
If nil, `bongo-mode-line-indicator-string' is not put anywhere."
:type '(choice (const :tag "None" nil) variable)
:group 'bongo-mode-line)
(defcustom bongo-mode-line-icon-color
(bongo-face-foreground 'mode-line nil 'default)
"Color of Bongo mode line icons."
:type 'string
:group 'bongo-mode-line)
(defcustom bongo-mode-line-playing-string "Playing"
"Fallback string for the Bongo [Pause] button icon."
:type 'string
:group 'bongo-mode-line)
(defcustom bongo-mode-line-paused-string "Paused"
"Fallback string for the Bongo [Resume] button icon."
:type 'string
:group 'bongo-mode-line)
(defvar bongo-mode-line-pause-icon-18
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *pause_18[] = {
/* width height number of colors number of characters per pixel */
\" 18 18 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"..................\",
\"..................\",
\"..................\",
\"...####....####...\",
\"...####....####...\",
\"...####....####...\",
\"...####....####...\",
\"...####....####...\",
\"...####....####...\",
\"...####....####...\",
\"...####....####...\",
\"...####....####...\",
\"...####....####...\",
\"...####....####...\",
\"...####....####...\",
\"..................\",
\"..................\",
\"..................\"
};"))
"Bongo [Pause] button icon (18 pixels tall).")
(defvar bongo-mode-line-pause-icon-11
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *pause_11[] = {
/* width height number of colors number of characters per pixel */
\" 10 11 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"..........\",
\"..........\",
\"..##..##..\",
\"..##..##..\",
\"..##..##..\",
\"..##..##..\",
\"..##..##..\",
\"..##..##..\",
\"..##..##..\",
\"..........\",
\"..........\"};"))
"Bongo [Pause] button icon (11 pixels tall).")
(defvar bongo-mode-line-resume-icon-18
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *resume_18[] = {
/* width height number of colors number of characters per pixel */
\" 18 18 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"..................\",
\"..................\",
\"......##..........\",
\"......###.........\",
\"......####........\",
\"......#####.......\",
\"......######......\",
\"......#######.....\",
\"......########....\",
\"......########....\",
\"......#######.....\",
\"......######......\",
\"......#####.......\",
\"......####........\",
\"......###.........\",
\"......##..........\",
\"..................\",
\"..................\"
};"))
"Bongo [Resume] button icon (18 pixels tall).")
(defvar bongo-mode-line-resume-icon-11
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *resume_11[] = {
/* width height number of colors number of characters per pixel */
\" 10 11 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"..........\",
\"...#......\",
\"...##.....\",
\"...###....\",
\"...####...\",
\"...#####..\",
\"...####...\",
\"...###....\",
\"...##.....\",
\"...#......\",
\"..........\"
};"))
"Bongo [Resume] button icon (11 pixels tall).")
(defvar bongo-mode-line-stop-icon-18
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *stop_18[] = {
/* width height number of colors number of characters per pixel */
\" 18 18 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"..................\",
\"..................\",
\"..................\",
\"...############...\",
\"...############...\",
\"...############...\",
\"...############...\",
\"...############...\",
\"...############...\",
\"...############...\",
\"...############...\",
\"...############...\",
\"...############...\",
\"...############...\",
\"...############...\",
\"..................\",
\"..................\",
\"..................\"
};"))
"Bongo [Stop] button icon (18 pixels tall).")
(defvar bongo-mode-line-stop-icon-11
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *stop_11[] = {
/* width height number of colors number of characters per pixel */
\" 10 11 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"..........\",
\"..........\",
\"..######..\",
\"..######..\",
\"..######..\",
\"..######..\",
\"..######..\",
\"..######..\",
\"..######..\",
\"..........\",
\"..........\"};"))
"Bongo [Stop] button icon (11 pixels tall).")
(defvar bongo-mode-line-previous-icon-18
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *previous_18[] = {
/* width height number of colors number of characters per pixel */
\" 20 18 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"....................\",
\"....................\",
\"....................\",
\"....................\",
\"....###.......##....\",
\"....###......###....\",
\"....###.....####....\",
\"....###....#####....\",
\"....###...######....\",
\"....###...######....\",
\"....###....#####....\",
\"....###.....####....\",
\"....###......###....\",
\"....###.......##....\",
\"....................\",
\"....................\",
\"....................\",
\"....................\"
};"))
"Bongo [Previous] button icon (18 pixels tall)")
(defvar bongo-mode-line-previous-icon-11
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *previous_11[] = {
/* width height number of colors number of characters per pixel */
\" 11 11 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"...........\",
\"...........\",
\"..##....#..\",
\"..##...##..\",
\"..##..###..\",
\"..##.####..\",
\"..##..###..\",
\"..##...##..\",
\"..##....#..\",
\"...........\",
\"...........\"
};"))
"Bongo [Previous] button icon (11 pixels tall).")
(defvar bongo-mode-line-next-icon-18
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *next_18[] = {
/* width height number of colors number of characters per pixel */
\" 20 18 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"....................\",
\"....................\",
\"....................\",
\"....................\",
\"....##.......###....\",
\"....###......###....\",
\"....####.....###....\",
\"....#####....###....\",
\"....######...###....\",
\"....######...###....\",
\"....#####....###....\",
\"....####.....###....\",
\"....###......###....\",
\"....##.......###....\",
\"....................\",
\"....................\",
\"....................\",
\"....................\"
};"))
"Bongo [Next] button icon (18 pixels tall)")
(defvar bongo-mode-line-next-icon-11
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *next_11[] = {
/* width height number of colors number of characters per pixel */
\" 11 11 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"...........\",
\"...........\",
\"..#....##..\",
\"..##...##..\",
\"..###..##..\",
\"..####.##..\",
\"..###..##..\",
\"..##...##..\",
\"..#....##..\",
\"...........\",
\"...........\"
};"))
"Bongo [Next] button icon (11 pixels tall).")
(defvar bongo-mode-line-backward-icon-18
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *backward_18[] = {
/* width height number of colors number of characters per pixel */
\" 20 18 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"....................\",
\"....................\",
\"....................\",
\"....................\",
\"....................\",
\"......##......##....\",
\".....###.....###....\",
\"....####....####....\",
\"...#####...#####....\",
\"...#####...#####....\",
\"....####....####....\",
\".....###.....###....\",
\"......##......##....\",
\"....................\",
\"....................\",
\"....................\",
\"....................\",
\"....................\"
};"))
"Bongo [Rewind] button icon (18 pixels tall).")
(defvar bongo-mode-line-backward-icon-11
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *backward_11[] = {
/* width height number of colors number of characters per pixel */
\" 11 11 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"...........\",
\"...........\",
\"...........\",
\"....#...#..\",
\"...##..##..\",
\"..###.###..\",
\"...##..##..\",
\"....#...#..\",
\"...........\",
\"...........\",
\"...........\"
};"))
"Bongo [Rewind] button icon (11 pixels tall).")
(defvar bongo-mode-line-forward-icon-18
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *forward_18[] = {
/* width height number of colors number of characters per pixel */
\" 20 18 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"....................\",
\"....................\",
\"....................\",
\"....................\",
\"....................\",
\"....##......##......\",
\"....###.....###.....\",
\"....####....####....\",
\"....#####...#####...\",
\"....#####...#####...\",
\"....####....####....\",
\"....###.....###.....\",
\"....##......##......\",
\"....................\",
\"....................\",
\"....................\",
\"....................\",
\"....................\"
};"))
"Bongo [Fast-forward] button icon (18 pixels tall).")
(defvar bongo-mode-line-forward-icon-11
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *forward_11[] = {
/* width height number of colors number of characters per pixel */
\" 11 11 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"...........\",
\"...........\",
\"...........\",
\"..#...#....\",
\"..##..##...\",
\"..###.###..\",
\"..##..##...\",
\"..#...#....\",
\"...........\",
\"...........\",
\"...........\"
};"))
"Bongo [Fast-forward] button icon (11 pixels tall).")
(defvar bongo-mode-line-volume-icon-18
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *volume_18[] = {
/* width height number of colors number of characters per pixel */
\" 20 18 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"....................\",
\"....................\",
\"........#...........\",
\".......##....##.....\",
\"......###.....##....\",
\".....####...#..##...\",
\"..#######...##.##...\",
\"..#######.#..##.##..\",
\"..#######..#.##.##..\",
\"..#######..#.##.##..\",
\"..#######.#..##.##..\",
\"..#######...##.##...\",
\".....####...#..##...\",
\"......###.....##....\",
\".......##....##.....\",
\"........#...........\",
\"....................\",
\"....................\"
};"))
"Bongo [Volume] button icon (18 pixels tall).")
(defvar bongo-mode-line-volume-icon-11
'`(image :type xpm :ascent center :data ,(concat "/* XPM */
static char *volume_11[] = {
/* width height number of colors number of characters per pixel */
\" 11 11 2 1\",
/* colors */
\"# c " bongo-mode-line-icon-color "\",
\". c None\",
/* pixels */
\"...........\",
\"...........\",
\".....#.....\",
\"....##.#...\",
\"..####..#..\",
\"..#####.#..\",
\"..####..#..\",
\"....##.#...\",
\".....#.....\",
\"...........\",
\"...........\"
};"))
"Bongo [Volume] button icon (11 pixels tall).")
(defvar bongo-mode-line-start-map
(let ((map (make-sparse-keymap)))
(prog1 map
(define-key map [mode-line mouse-1]
(lambda (e)
(interactive "e")
(bongo-start)))
(define-key map [mode-line mouse-3]
(lambda (e)
(interactive "e")
(if (bongo-buffer-p)
(bongo-switch-buffers)
(bongo)))))))
(defvar bongo-mode-line-pause/resume-map
(let ((map (make-sparse-keymap)))
(prog1 map
(define-key map [mode-line mouse-1]
(lambda (e)
(interactive "e")
(bongo-pause/resume)))
(define-key map [mode-line mouse-3]
(lambda (e)
(interactive "e")
(if (bongo-buffer-p)
(bongo-switch-buffers)
(bongo)))))))
(defvar bongo-mode-line-stop-map
(let ((map (make-sparse-keymap)))
(prog1 map
(define-key map [mode-line mouse-1]
(lambda (e)
(interactive "e")
(bongo-stop)))
(define-key map [mode-line mouse-3]
(lambda (e)
(interactive "e")
(if (bongo-buffer-p)
(bongo-switch-buffers)
(bongo)))))))
(defvar bongo-mode-line-previous-map
(let ((map (make-sparse-keymap)))
(prog1 map
(define-key map [mode-line mouse-1]
(lambda (e)
(interactive "e")
(bongo-play-previous)))
(define-key map [mode-line mouse-3]
(lambda (e)
(interactive "e")
(bongo-play-previous))))))
(defvar bongo-mode-line-next-map
(let ((map (make-sparse-keymap)))
(prog1 map
(define-key map [mode-line mouse-1]
(lambda (e)
(interactive "e")
(bongo-play-next)))
(define-key map [mode-line mouse-3]
(lambda (e)
(interactive "e")
(bongo-play-next))))))
(defvar bongo-mode-line-backward-map
(let ((map (make-sparse-keymap)))
(prog1 map
(define-key map [mode-line mouse-1]
(lambda (e)
(interactive "e")
(bongo-seek-backward-10)))
(define-key map [mode-line mouse-3]
(lambda (e)
(interactive "e")
(if (bongo-buffer-p)
(bongo-switch-buffers)
(bongo)))))))
(defvar bongo-mode-line-forward-map
(let ((map (make-sparse-keymap)))
(prog1 map
(define-key map [mode-line mouse-1]
(lambda (e)
(interactive "e")
(bongo-seek-forward-10)))
(define-key map [mode-line mouse-3]
(lambda (e)
(interactive "e")
(if (bongo-buffer-p)
(bongo-switch-buffers)
(bongo)))))))
(defvar bongo-mode-line-backward/previous-map
(let ((map (make-sparse-keymap)))
(prog1 map
(define-key map [mode-line mouse-1]
(lambda (e)
(interactive "e")
(bongo-seek-backward-10)))
(define-key map [mode-line mouse-3]
(lambda (e)
(interactive "e")
(bongo-previous))))))
(defvar bongo-mode-line-forward/next-map
(let ((map (make-sparse-keymap)))
(prog1 map
(define-key map [mode-line mouse-1]
(lambda (e)
(interactive "e")
(bongo-seek-forward-10)))
(define-key map [mode-line mouse-3]
(lambda (e)
(interactive "e")
(bongo-next))))))
(defvar bongo-mode-line-volume-map
(let ((map (make-sparse-keymap)))
(prog1 map
(define-key map [mode-line mouse-1]
(lambda (e)
(interactive "e")
(if volume-buffer
(volume-quit)
(volume))))
(define-key map [mode-line mouse-4]
(lambda (e)
(interactive "e")
(volume-raise)))
(define-key map [mode-line mouse-5]
(lambda (e)
(interactive "e")
(volume-lower))))))
(defun bongo-face-height (face-name)
"Return the height of the font used for FACE-NAME, or nil.
If running without a window system, signal an error."
(catch 'return
(aref (or (font-info (or (face-font face-name)
(throw 'return nil)))
(throw 'return nil)) 3)))
(defun bongo-face-width (face-name)
"Return the width of the font used for FACE-NAME, or nil.
If running without a window system, signal an error."
(catch 'return
(aref (or (font-info (or (face-font face-name)
(throw 'return nil)))
(throw 'return nil)) 2)))
(defun bongo-mode-line-icon-size ()
"Return the size to use for mode line icons."
(if (>= (or (bongo-face-height 'mode-line) 0) 18) 18 11))
(defun bongo-mode-line-start-button ()
"Return the string to use as [Start] button in the mode line."
(when (and window-system (not (bongo-playing-p)))
(let ((icon-size (bongo-mode-line-icon-size)))
(concat
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))
(propertize " "
'display (cond ((= icon-size 18)
(eval bongo-mode-line-resume-icon-18))
((= icon-size 11)
(eval bongo-mode-line-resume-icon-11)))
'help-echo
(let ((position (bongo-point-at-current-track-line)))
;; We can't put the <mouse-3> text at
;; the bottom because it'll truncate.
(concat "mouse-3: display Bongo buffers\nmouse-1: "
(if position
(concat "play "
(bongo-format-infoset
(bongo-line-infoset position)))
"start playback")))
'local-map bongo-mode-line-start-map
'mouse-face 'highlight)
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))))))
(defun bongo-mode-line-stop-button ()
"Return the string to use as [Stop] button in the mode line."
(when (and window-system (bongo-playing-p))
(let ((icon-size (bongo-mode-line-icon-size)))
(concat
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))
(propertize " "
'display (cond ((= icon-size 18)
(eval bongo-mode-line-stop-icon-18))
((= icon-size 11)
(eval bongo-mode-line-stop-icon-11)))
'help-echo (concat "mouse-1: stop playback")
'local-map bongo-mode-line-stop-map
'mouse-face 'highlight)
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))))))
(defun bongo-mode-line-start/stop-button ()
"Return the string to use as [Start] or [Stop] button."
(or (bongo-mode-line-start-button)
(bongo-mode-line-stop-button)))
(defun bongo-mode-line-pause/resume-button ()
"Return the string to use as [Pause] or [Resume] button."
(when (and (bongo-playing-p) (bongo-pausing-supported-p))
(if window-system
(let ((icon-size (bongo-mode-line-icon-size)))
(concat
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))
(propertize
" "
'display (if (bongo-paused-p)
(cond ((= icon-size 18)
(eval bongo-mode-line-resume-icon-18))
((= icon-size 11)
(eval bongo-mode-line-resume-icon-11)))
(cond ((= icon-size 18)
(eval bongo-mode-line-pause-icon-18))
((= icon-size 11)
(eval bongo-mode-line-pause-icon-11))))
'help-echo (concat (if (bongo-paused-p)
"mouse-1: resume "
"mouse-1: pause ")
(bongo-format-infoset
(bongo-player-infoset bongo-player)))
'local-map bongo-mode-line-pause/resume-map
'mouse-face 'highlight)
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))))
(if (bongo-paused-p)
bongo-mode-line-paused-string
bongo-mode-line-playing-string))))
(defun bongo-mode-line-previous-button ()
"Return the string to use as [Previous] button in the mode line."
(when (and window-system (bongo-point-at-current-track-line))
(let ((icon-size (bongo-mode-line-icon-size)))
(concat
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))
(propertize " "
'display (cond ((= icon-size 18)
(eval bongo-mode-line-previous-icon-18))
((= icon-size 11)
(eval bongo-mode-line-previous-icon-11)))
'help-echo
(let ((position (bongo-point-at-previous-track-line
(bongo-point-at-current-track-line))))
(if position
(concat "mouse-1: play "
(bongo-format-infoset
(bongo-line-infoset position)))
"No previous track"))
'local-map bongo-mode-line-previous-map
'mouse-face 'highlight)
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))))))
(defun bongo-mode-line-next-button ()
"Return the string to use as [Next] button in the mode line."
(when (and window-system (bongo-point-at-current-track-line))
(let ((icon-size (bongo-mode-line-icon-size)))
(concat
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))
(propertize " "
'display (cond ((= icon-size 18)
(eval bongo-mode-line-next-icon-18))
((= icon-size 11)
(eval bongo-mode-line-next-icon-11)))
'help-echo
(let ((position (bongo-point-at-next-track-line
(bongo-point-at-current-track-line))))
(if position
(concat "mouse-1: play "
(bongo-format-infoset
(bongo-line-infoset position)))
"No next track"))
'local-map bongo-mode-line-next-map
'mouse-face 'highlight)
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))))))
(defun bongo-mode-line-backward-button ()
"Return the string to use as [Backward] button in the mode line."
(when (and window-system (bongo-playing-p) (bongo-seeking-supported-p))
(let ((icon-size (bongo-mode-line-icon-size)))
(concat
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))
(propertize " "
'display (cond ((= icon-size 18)
(eval bongo-mode-line-backward-icon-18))
((= icon-size 11)
(eval bongo-mode-line-backward-icon-11)))
'help-echo "mouse-1: rewind 10 seconds"
'local-map bongo-mode-line-backward-map
'mouse-face 'highlight)
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))))))
(defun bongo-mode-line-forward-button ()
"Return the string to use as [Forward] button in the mode line."
(when (and window-system (bongo-playing-p) (bongo-seeking-supported-p))
(let ((icon-size (bongo-mode-line-icon-size)))
(concat
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))
(propertize " "
'display (cond ((= icon-size 18)
(eval bongo-mode-line-forward-icon-18))
((= icon-size 11)
(eval bongo-mode-line-forward-icon-11)))
'help-echo "mouse-1: fast-forward 10 seconds"
'local-map bongo-mode-line-forward-map
'mouse-face 'highlight)
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))))))
(defun bongo-mode-line-backward/previous-button ()
"Return the string to use as [Rewind/Previous] button."
(when (and window-system
;; This condition could be made optional.
(bongo-playing-p))
(let ((icon-size (bongo-mode-line-icon-size)))
(concat
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))
(propertize " "
'display
(if (bongo-playing-p)
(cond ((= icon-size 18)
(eval bongo-mode-line-backward-icon-18))
((= icon-size 11)
(eval bongo-mode-line-backward-icon-11)))
(cond ((= icon-size 18)
(eval bongo-mode-line-previous-icon-18))
((= icon-size 11)
(eval bongo-mode-line-previous-icon-11))))
'help-echo
(let ((position
(and (bongo-point-at-current-track-line)
(bongo-point-at-previous-track-line
(bongo-point-at-current-track-line)))))
(concat (when (and (bongo-playing-p)
(bongo-seeking-supported-p))
"mouse-1: rewind 10 seconds")
(if position
(concat (if (bongo-playing-p)
"\nmouse-3: "
"mouse-1: ")
"play "
(bongo-format-infoset
(bongo-line-infoset position)))
(unless (bongo-playing-p)
"No previous track"))))
'local-map (if (bongo-playing-p)
bongo-mode-line-backward/previous-map
bongo-mode-line-backward-map)
'mouse-face 'highlight)
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))))))
(defun bongo-mode-line-forward/next-button ()
"Return the string to use as [Fast-forward/Next] button."
(when (and window-system
;; This condition could be made optional.
(bongo-playing-p))
(let ((icon-size (bongo-mode-line-icon-size)))
(concat
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))
(propertize " "
'display
(if (bongo-playing-p)
(cond ((= icon-size 18)
(eval bongo-mode-line-forward-icon-18))
((= icon-size 11)
(eval bongo-mode-line-forward-icon-11)))
(cond ((= icon-size 18)
(eval bongo-mode-line-next-icon-18))
((= icon-size 11)
(eval bongo-mode-line-next-icon-11))))
'help-echo
(let ((position
(and (bongo-point-at-current-track-line)
(bongo-point-at-next-track-line
(bongo-point-at-current-track-line)))))
(concat (when (and (bongo-playing-p)
(bongo-seeking-supported-p))
"mouse-1: fast-forward 10 seconds")
(if position
(concat (if (bongo-playing-p)
"\nmouse-3: "
"mouse-1: ")
"play "
(bongo-format-infoset
(bongo-line-infoset position)))
(unless (bongo-playing-p)
"No next track"))))
'local-map (if (bongo-playing-p)
bongo-mode-line-forward/next-map
bongo-mode-line-next-map)
'mouse-face 'highlight)
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))))))
(defun bongo-mode-line-volume-button ()
"Return the string to use as [Volume] button in the mode line."
(when (and window-system (require 'volume nil t))
(let ((icon-size (bongo-mode-line-icon-size)))
(concat
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))
(propertize " "
'display (cond ((= icon-size 18)
(eval bongo-mode-line-volume-icon-18))
((= icon-size 11)
(eval bongo-mode-line-volume-icon-11)))
'help-echo (concat "mouse-1: open volume control\n"
"mouse-4: raise volume\n"
"mouse-5: lower volume")
'local-map bongo-mode-line-volume-map
'mouse-face 'highlight)
(when (>= emacs-major-version 22)
(propertize " " 'display '(space :width (1))))))))
(defvar bongo-mode-line-indicator-string nil
"Bongo mode line indicator string.
Value is derived from `bongo-mode-line-indicator-function'.
The name of this variable should go in, e.g., `global-mode-string'.")
(put 'bongo-mode-line-indicator-string 'risky-local-variable t)
(defun bongo-update-mode-line-indicator-string (&rest dummy)
"Update `bongo-mode-line-indicator-string'.
Evalutate elements of `bongo-mode-line-indicator-format' and store
the resulting string in `bongo-mode-line-indicator-string'.
Accept DUMMY arguments to ease hook usage."
(when (bongo-buffer-p)
(let ((new-value (funcall bongo-mode-line-indicator-function)))
(if (not bongo-player-start-imminent)
(setq bongo-mode-line-indicator-string new-value)
(add-to-list 'bongo-deferred-status-indicator-updates
(list 'set 'bongo-mode-line-indicator-string new-value))))))
(defun bongo-mode-line-indicator-mode
(argument &optional called-interactively-p)
"Toggle display of Bongo mode line indicator on or off.
With ARGUMENT equal to `toggle', or interactively
with no prefix argument, toggle the mode.
With zero or negative ARGUMENT, turn the mode off.
With any other ARGUMENT, turn the mode on.
When called interactively, CALLED-INTERACTIVELY-P is non-nil."
;; Use `toggle' rather than (if mode 0 1) so that using
;; `repeat-command' still does the toggling correctly.
(interactive (list (or current-prefix-arg 'toggle)
'called-interactively-p))
(setq bongo-mode-line-indicator-mode
(if (eq argument 'toggle)
(not bongo-mode-line-indicator-mode)
(> (prefix-numeric-value argument) 0)))
(when called-interactively-p
(bongo-customize-mark-as-set 'bongo-mode-line-indicator-mode))
(when bongo-mode-line-indicator-parent
(if (not bongo-mode-line-indicator-mode)
(set bongo-mode-line-indicator-parent
(remq 'bongo-mode-line-indicator-string
(symbol-value bongo-mode-line-indicator-parent)))
(when (null (symbol-value bongo-mode-line-indicator-parent))
(set bongo-mode-line-indicator-parent '("")))
(add-to-list bongo-mode-line-indicator-parent
'bongo-mode-line-indicator-string 'append)))
(when (interactive-p)
(message "Bongo mode line indicator mode %s."
(if bongo-mode-line-indicator-mode
"enabled" "disabled")))
bongo-mode-line-indicator-mode)
(defgroup bongo-infosets nil
"Structured track information in Bongo."
:group 'bongo)
(defcustom bongo-potential-external-fields '(artist album)
"The fields that may be used to join tracks into sections.
Currently, this list needs to be completely ordered, starting with
the most general field and ending with the most specific field."
:type '(repeat symbol)
:group 'bongo-infosets)
(bongo-define-obsolete-variable-alias 'bongo-fields
'bongo-potential-external-fields "2006-12-07")
(defcustom bongo-infoset-from-file-name-function
'bongo-default-infoset-from-file-name
"Function used to convert file names into infosets.
This function should be chosen so that the following identity holds:
(equal (file-name-sans-extension
(file-name-nondirectory FILE-NAME))
(bongo-file-name-from-infoset
(bongo-infoset-from-file-name FILE-NAME)))
Good functions are `bongo-default-infoset-from-file-name'
and `bongo-simple-infoset-from-file-name'.
See also `bongo-file-name-from-infoset-function'."
:type 'function
:options '(bongo-default-infoset-from-file-name
bongo-simple-infoset-from-file-name)
:group 'bongo-file-names
:group 'bongo-infosets)
(defcustom bongo-file-name-from-infoset-function
'bongo-default-file-name-from-infoset
"Function used to represent an infoset as a file name.
This function should be chosen so that the following identity holds:
(equal (file-name-sans-extension
(file-name-nondirectory FILE-NAME))
(bongo-file-name-from-infoset
(bongo-infoset-from-file-name FILE-NAME)))
If the infoset cannot be represented as a file name, the
function should signal an error. To satisfy the above
identity, this must not be the case for any infoset that
`bongo-infoset-from-file-name' can generate.
The default function cannot represent infosets that contain
general but not specific data. For example, it cannot
represent ((artist (name \"Foo\"))), because a file name
containing only \"Foo\" would be interpreted as containing
only a track title.
See also `bongo-infoset-from-file-name-function'."
:type 'function
:group 'bongo-file-names
:group 'bongo-infosets)
(defcustom bongo-file-name-part-from-field-function
'bongo-default-file-name-part-from-field
"Function used to represent an info field as part of a file name.
This is used by `bongo-default-file-name-from-infoset'."
:type 'function
:group 'bongo-file-names
:group 'bongo-infosets)
(defcustom bongo-infoset-formatting-function
'bongo-default-format-infoset
"Function used to represent an infoset as a user-friendly string."
:type 'function
:group 'bongo-display
:group 'bongo-infosets)
(defcustom bongo-field-formatting-function
'bongo-default-format-field
"Function used to represent an info field as a user-friendly string.
This is used by `bongo-default-format-infoset'."
:type 'function
:group 'bongo-display
:group 'bongo-infosets)
(defgroup bongo-faces nil
"Faces used by Bongo."
:group 'bongo)
(defface bongo-comment
'((t (:inherit font-lock-comment-face)))
"Face used for comments in Bongo buffers."
:group 'bongo-faces)
(defface bongo-warning
'((t (:inherit font-lock-warning-face)))
"Face used for warnings in Bongo buffers."
:group 'bongo-faces)
(defface bongo-artist
'((t (:inherit font-lock-keyword-face)))
"Face used for Bongo artist names."
:group 'bongo-faces)
(defface bongo-album '((t nil))
"Face used for Bongo albums (year, title, and punctuation)."
:group 'bongo-faces)
(defface bongo-album-title
'((t (:inherit (font-lock-type-face bongo-album))))
"Face used for Bongo album titles."
:group 'bongo-faces)
(defface bongo-album-year
'((t (:inherit bongo-album)))
"Face used for Bongo album years."
:group 'bongo-faces)
(defface bongo-track '((t nil))
"Face used for Bongo tracks (index, title, length, and punctuation)."
:group 'bongo-faces)
(defface bongo-track-title
'((t (:inherit (font-lock-function-name-face bongo-track))))
"Face used for Bongo track titles."
:group 'bongo-faces)
(defface bongo-track-index
'((t (:inherit bongo-track)))
"Face used for Bongo track indices."
:group 'bongo-faces)
(defface bongo-track-length
'((t (:inherit bongo-track)))
"Face used for Bongo track lengths."
:group 'bongo-faces)
(defface bongo-played-track
'((t (:strike-through "#808080" :inherit bongo-track)))
"Face used for already played Bongo tracks."
:group 'bongo-faces)
(defface bongo-elapsed-track-part
'((t (:strike-through "#808080")))
"Face used for the elapsed part of the currently playing Bongo track."
:group 'bongo-faces)
(defface bongo-currently-playing-track
'((t (:weight bold :inherit bongo-track)))
"Face used for the currently playing Bongo track."
:group 'bongo-faces)
(defface bongo-action-track '((t nil))
"Face used for Bongo action tracks."
:group 'bongo-faces)
(defface bongo-action-track-description
'((t (:weight bold :inherit (font-lock-builtin-face
bongo-action-track))))
"Face used for Bongo action track descriptions."
:group 'bongo-faces)
(defface bongo-action-track-expression
'((t (:inherit bongo-action-track)))
"Face used for Bongo action track expressions."
:group 'bongo-faces)
;;;; Infoset- and field-related functions
(defun bongo-format-infoset (infoset)
"Represent INFOSET as a user-friendly string.
This function just calls `bongo-infoset-formatting-function'."
(funcall bongo-infoset-formatting-function infoset))
(defvar bongo-infoset-formatting-target 'unspecified
"Target of the current infoset formatting.
When an infoset is formatted for display in a Bongo buffer,
this variable is dynamically bound to the target buffer.
When there is no particular target, the value is `unspecified'.
This value can be used by the formatting function.")
(defvar bongo-infoset-formatting-target-line nil
"Target line of the current infoset formatting, or nil.
When an infoset is formatted for display on a line in a Bongo buffer,
this variable is dynamically bound to the buffer position of the line.
The buffer itself appears in `bongo-infoset-formatting-target'.
When the formatting target is not a Bongo buffer line, the value is nil.
This value can be used by the formatting function.")
(defun bongo-default-format-infoset (infoset)
"Format INFOSET by calling `bongo-format-field' on each field.
Only the first instance of each field with a given name is considered.
Dynamically bind `bongo-infoset' to INFOSET while formatting.
Return the concatenation of the obtained formatted field values
separated by `bongo-field-separator'."
(let* ((bongo-infoset infoset)
(bongo-target bongo-infoset-formatting-target)
(bongo-line bongo-infoset-formatting-target-line)
(processed-fields nil)
(formatted-fields
(apply 'nconc
(mapcar (lambda (field)
(unless (or (memq (car field) processed-fields)
(null (cdr field))
(eq (cdr field) 'unknown)
(eq (cdr field) 'unbound))
(push (car field) processed-fields)
(list (bongo-format-field field))))
infoset))))
(mapconcat 'identity formatted-fields bongo-field-separator)))
(defun bongo-file-name-from-infoset (infoset)
"Represent INFOSET as a file name, if possible.
If INFOSET cannot be represented as a file name, signal an error.
This function just calls `bongo-file-name-from-infoset-function'.
See the documentation for that variable for more information."
(funcall bongo-file-name-from-infoset-function infoset))
(defun bongo-default-file-name-from-infoset (infoset)
"Represent INFOSET as a file name, if possible.
This function calls `bongo-file-name-part-from-field' on
each field and separates the obtained field values using
`bongo-file-name-field-separator'."
;; Signal an error if the infoset cannot be represented.
(mapconcat 'bongo-file-name-part-from-field infoset
bongo-file-name-field-separator))
(defun bongo-join-fields (values)
(mapconcat 'identity values bongo-field-separator))
(defun bongo-join-file-name-fields (values)
(mapconcat 'identity values bongo-file-name-field-separator))
(defun bongo-format-field (field)
(funcall bongo-field-formatting-function field))
(defun bongo-default-format-field (field)
(let ((type (car field))
(data (cdr field)))
(case type
((artist)
(propertize (bongo-alist-get data 'name) 'face 'bongo-artist))
((album)
;; These variables are used in `bongo-album-format',
;; so their names are significant.
(let* ((bongo-title
(propertize (bongo-alist-get data 'title)
'face 'bongo-album-title))
(bongo-year
(when (bongo-alist-get data 'year)
(propertize (bongo-alist-get data 'year)
'face 'bongo-album-year)))
(bongo-artist data))
(funcall bongo-album-function)))
((track)
;; These variables are used in `bongo-track-format',
;; so their names are significant.
(let* ((bongo-title
(propertize (bongo-alist-get data 'title)
'face 'bongo-track-title))
(bongo-index
(when (bongo-alist-get data 'index)
(propertize (bongo-alist-get data 'index)
'face 'bongo-track-index)))
(bongo-track data)
(bongo-length
(bongo-alist-get data 'length))
(length-string
(when bongo-length
(funcall bongo-track-length-function)))
(bongo-length
(when length-string
(propertize length-string 'face 'bongo-track-length))))
(funcall bongo-track-function)))
((stream)
;; These variables are used in `bongo-stream-format',
;; so their names are significant.
(let ((bongo-uri
(propertize (bongo-alist-get data 'uri)
'face 'bongo-album-title))
(bongo-uri-title
(when (bongo-alist-get data 'uri-title)
(propertize (bongo-alist-get data 'uri-title)
'face 'bongo-album-title)))
(bongo-stream-name
(when (bongo-alist-get data 'name)
(propertize (bongo-alist-get data 'name)
'face 'bongo-album-title)))
(bongo-stream-genre
(when (bongo-alist-get data 'genre)
(bongo-alist-get data 'genre)))
(bongo-stream-part-title
(when (bongo-alist-get data 'part-title)
(propertize (bongo-alist-get data 'part-title)
'face 'bongo-track-title))))
(bongo-format-string bongo-stream-format)))
((action)
;; These variables are used in `bongo-action-format',
;; so their names are significant.
(let ((bongo-action-expression data)
(bongo-action-description
(let ((description-specifier
(when (listp data)
(get (car data) 'bongo-action-description))))
(cond ((null description-specifier)
(bongo-facify (with-temp-buffer
(emacs-lisp-mode)
(insert (prin1-to-string data))
(let ((font-lock-verbose nil))
(font-lock-fontify-buffer))
(buffer-string))
'bongo-action-track-expression))
((stringp description-specifier)
(bongo-facify-copy description-specifier
'bongo-action-track-description))
((functionp description-specifier)
(bongo-facify-copy (apply description-specifier data)
'bongo-action-track-description))
(t (error (concat "Invalid action description "
"specifier: `%S'")
description-specifier))))))
(bongo-format-string bongo-action-format))))))
(defun bongo-file-name-part-from-field (field)
"Represent FIELD as part of a file name.
This is used by `bongo-default-file-name-from-infoset'."
(funcall bongo-file-name-part-from-field-function field))
(defun bongo-default-file-name-part-from-field (field)
(let ((type (car field))
(data (cdr field)))
(case type
((artist) data)
((album)
(let ((title (bongo-alist-get data 'title))
(year (bongo-alist-get data 'year)))
(if (null year) title
(bongo-join-file-name-fields (list year title)))))
((track)
(let ((title (bongo-alist-get data 'title))
(index (bongo-alist-get data 'index)))
(if (null index) title
(bongo-join-file-name-fields (list index title))))))))
(defun bongo-infoset-from-file-name (file-name)
(funcall bongo-infoset-from-file-name-function file-name))
(defun bongo-uri-scheme (file-name)
"Return the URI scheme of FILE-NAME, or nil if it has none.
To avoid treating Microsoft Windows and DOS drive letters as URI schemes,
we require that URI schemes be at least two characters long."
(when (string-match (eval-when-compile
(rx (and string-start
(submatch
(any "a-zA-Z")
(one-or-more
(or (any "a-zA-Z0-9$_@.&!*\"'(),")
(and "%" (repeat 2 hex-digit)))))
":")))
file-name)
(match-string 1 file-name)))
(defun bongo-uri-p (file-name)
"Return non-nil if FILE-NAME is a URI.
As a special case, return nil if FILE-NAME is nil."
(and file-name (not (null (bongo-uri-scheme file-name)))))
(defun bongo-unescape-uri (uri)
"Replace all occurences of `%HH' in URI by the character HH."
(with-temp-buffer
(insert uri)
(goto-char (point-min))
(while (re-search-forward
(eval-when-compile
(rx (and "%" (submatch (repeat 2 hex-digit)))))
nil 'no-error)
(replace-match (char-to-string
(string-to-number (match-string 1) 16))))
(buffer-string)))
(defun bongo-default-infoset-from-file-name (file-name)
(let ((track-length-part
(when (and (boundp 'bongo-track-length) bongo-track-length)
`((length . ,bongo-track-length)))))
(if (bongo-uri-p file-name)
`((artist . unknown)
(album . unknown)
(track (title . ,(bongo-unescape-uri file-name))
,@track-length-part))
(let* ((base-name (file-name-sans-extension
(file-name-nondirectory file-name)))
(values (split-string base-name bongo-file-name-field-separator)))
(when (> (length values) 5)
(let ((fifth-and-rest (nthcdr 4 values)))
(setcar fifth-and-rest (bongo-join-fields fifth-and-rest))
(setcdr fifth-and-rest nil)))
(cond ((= 5 (length values))
(if (string-match bongo-file-name-track-index-regexp
(nth 3 values))
`((artist (name . ,(nth 0 values)))
(album (year . ,(nth 1 values))
(title . ,(nth 2 values)))
(track (index . ,(nth 3 values))
(title . ,(nth 4 values))
,@track-length-part))
`((artist (name . ,(nth 0 values)))
(album (year . ,(nth 1 values))
(title . ,(nth 2 values)))
(track (title . ,(bongo-join-fields
(nthcdr 3 values)))
,@track-length-part))))
((and (= 4 (length values))
(string-match bongo-file-name-track-index-regexp
(nth 2 values)))
`((artist (name . ,(nth 0 values)))
(album (title . ,(nth 1 values)))
(track (index . ,(nth 2 values))
(title . ,(nth 3 values))
,@track-length-part)))
((and (= 4 (length values))
(string-match bongo-file-name-album-year-regexp
(nth 1 values)))
`((artist (name . ,(nth 0 values)))
(album (year . ,(nth 1 values))
(title . ,(nth 2 values)))
(track (title . ,(nth 3 values))
,@track-length-part)))
((= 4 (length values))
`((artist (name . ,(nth 0 values)))
(album (title . ,(nth 1 values)))
(track (title . ,(bongo-join-fields
(nthcdr 2 values)))
,@track-length-part)))
((= 3 (length values))
`((artist (name . ,(nth 0 values)))
(album (title . ,(nth 1 values)))
(track (title . ,(nth 2 values))
,@track-length-part)))
((= 2 (length values))
`((artist (name . ,(nth 0 values)))
(album . unknown)
(track (title . ,(nth 1 values))
,@track-length-part)))
((= 1 (length values))
`((artist . unknown)
(album . unknown)
(track (title . ,(nth 0 values))
,@track-length-part))))))))
(defun bongo-simple-infoset-from-file-name (file-name)
`((track (title . ,(file-name-sans-extension
(file-name-nondirectory
(if (bongo-uri-p file-name)
(bongo-unescape-uri file-name)
file-name)))))))
(defun bongo-double-alist-get (alist-1 key-1 key-2)
(let ((alist-2 (bongo-alist-get alist-1 key-1)))
(when (listp alist-2)
(bongo-alist-get alist-2 key-2))))
(defun bongo-infoset-artist-name (infoset)
(bongo-double-alist-get infoset 'artist 'name))
(defun bongo-infoset-album-year (infoset)
(bongo-double-alist-get infoset 'album 'year))
(defun bongo-infoset-album-title (infoset)
(bongo-double-alist-get infoset 'album 'title))
(defun bongo-infoset-track-index (infoset)
(bongo-double-alist-get infoset 'track 'index))
(defun bongo-infoset-track-title (infoset)
(bongo-double-alist-get infoset 'track 'title))
;;;; Basic point-manipulation routines
(defun bongo-goto-point (point)
"Set point to POINT, if POINT is non-nil.
POINT may be a number, a marker or nil."
(when point (goto-char point)))
(defun bongo-before-invisible-text-p (&optional point)
"Return non-nil if the character after POINT is invisible.
Return nil if POINT is at the last buffer position.
See `buffer-invisibility-spec'."
(save-excursion
(bongo-goto-point point)
(and (not (eobp))
(let ((property (get-char-property (point) 'invisible)))
(if (eq buffer-invisibility-spec t)
property
(or (memq property buffer-invisibility-spec)
(assq property buffer-invisibility-spec)))))))
(defun bongo-after-invisible-text-p (&optional point)
"Return non-nil if the character before POINT is invisible.
Return nil if POINT is at the first buffer position.
See `buffer-invisibility-spec'."
(save-excursion
(bongo-goto-point point)
(and (not (bobp))
(bongo-before-invisible-text-p (1+ (point))))))
(defun bongo-skip-invisible ()
"Move point to the next visible character.
If point is already on a visible character, do nothing."
(while (bongo-before-invisible-text-p)
(goto-char (next-single-char-property-change (point) 'invisible))))
(defun bongo-point-at-bol (&optional point)
"Return the first character position of the line at POINT.
If `line-move-ignore-invisible' is non-nil, ignore invisible text.
For lines that start with invisible text, return the position of
the first visible character on the line."
(save-excursion
(bongo-goto-point point)
(if (not line-move-ignore-invisible)
(point-at-bol)
(while (progn (skip-chars-backward "^\n")
(bongo-after-invisible-text-p))
(goto-char (previous-single-char-property-change
(point) 'invisible)))
(bongo-skip-invisible)
(point))))
(defun bongo-point-at-eol (&optional point)
"Return the last character position of the line at POINT.
If `line-move-ignore-invisible' is non-nil, ignore invisible text.
Always return the position of the newline character, even for lines
that contain invisible text immediately before the newline."
(save-excursion
(bongo-goto-point point)
(if (not line-move-ignore-invisible)
(point-at-eol)
(while (progn (skip-chars-forward "^\n")
(bongo-before-invisible-text-p))
(goto-char (next-single-char-property-change
(point) 'invisible)))
(point))))
(defun bongo-beginning-of-line (&optional point)
"Move to the beginning of the line at POINT.
See `bongo-point-at-bol'."
(goto-char (bongo-point-at-bol point)))
(defun bongo-end-of-line (&optional point)
"Move to the end of the line at POINT.
See `bongo-point-at-eol'."
(goto-char (bongo-point-at-eol point)))
(defun bongo-first-line-p (&optional point)
"Return non-nil if POINT is on the first line."
(= (bongo-point-at-bol point) (point-min)))
(defun bongo-last-line-p (&optional point)
"Return non-nil if POINT is on the last line.
An empty line at the end of the buffer doesn't count."
(>= (1+ (bongo-point-at-eol point)) (point-max)))
(defun bongo-first-object-line-p (&optional point)
"Return non-nil if POINT is on the first object line.
See also `bongo-point-at-previous-object-line'."
(null (bongo-point-at-previous-object-line point)))
(defun bongo-last-object-line-p (&optional point)
"Return non-nil if POINT is on the last object line.
See also `bongo-point-at-next-object-line'."
(null (bongo-point-at-next-object-line point)))
(defalias 'bongo-point-before-line
'bongo-point-at-bol)
(defun bongo-point-after-line (&optional point)
"Return the first character position after the line at POINT.
In the normal case, for lines that end with newlines, the point
after a line is the same as the point before the next line."
(let ((eol (bongo-point-at-eol point)))
(if (= eol (point-max)) eol (1+ eol))))
(defun bongo-point-at-bol-forward (&optional point)
"Return the position of the first line beginning after or at POINT.
If POINT is at the beginning of a line, just return POINT.
Otherwise, return the first character position after the line at POINT."
(or point (setq point (point)))
(if (= point (bongo-point-at-bol point))
point
(bongo-point-after-line point)))
(bongo-define-obsolete-function-alias
'bongo-point-snapped-forwards
'bongo-point-at-bol-forward)
(defun bongo-point-before-previous-line (&optional point)
"Return the first position of the line before the one at POINT.
If the line at POINT is the first line, return nil."
(unless (bongo-first-line-p point)
(bongo-point-at-bol (1- (bongo-point-at-bol point)))))
(defun bongo-point-before-next-line (&optional point)
"Return the first position of the line after the one at POINT.
If the line at POINT is the last line, return nil."
(unless (bongo-last-line-p point)
(1+ (bongo-point-at-eol point))))
(defalias 'bongo-point-at-previous-line
'bongo-point-before-previous-line)
(defalias 'bongo-point-at-next-line
'bongo-point-before-next-line)
(defun bongo-point-before-previous-line-satisfying (predicate &optional point)
"Return the position of the previous line satisfying PREDICATE.
If POINT is non-nil, the search starts before the line at POINT.
If POINT is nil, it starts before the current line.
If no matching line is found, return nil."
(save-excursion
(bongo-goto-point point)
(when (not (bongo-first-line-p))
(let (match)
(while (and (not (bobp)) (not match))
(let ((goal-column 0))
(previous-line 1))
(when (funcall predicate)
(setq match t)))
(when match
(point))))))
(defalias 'bongo-point-at-previous-line-satisfying
'bongo-point-before-previous-line-satisfying)
(defun bongo-point-before-next-line-satisfying (predicate &optional point)
"Return the position of the next line satisfying PREDICATE.
If POINT is non-nil, the search starts after the line at POINT.
If POINT is nil, it starts after the current line.
If no matching line is found, return nil."
(save-excursion
(bongo-goto-point point)
(when (not (bongo-last-line-p))
(let (match)
(while (and (not (eobp)) (not match))
(let ((goal-column 0))
(next-line 1))
(when (funcall predicate)
(setq match t)))
(when match
(point))))))
(defalias 'bongo-point-at-next-line-satisfying
'bongo-point-before-next-line-satisfying)
(defun bongo-point-after-next-line-satisfying (predicate &optional point)
"Return the position after the next line satisfying PREDICATE.
This function works like `bongo-point-before-next-line-satisfying'.
Start searching at POINT, or at point if POINT is nil."
(let ((before-next (bongo-point-before-next-line-satisfying
predicate point)))
(when before-next
(bongo-point-at-eol before-next))))
(defun bongo-point-at-first-line-satisfying (predicate &optional point)
"Return the position of the first line satisfying PREDICATE.
If POINT is non-nil, the search starts at the line at POINT.
If POINT is nil, it starts at the first line in the buffer.
If no matching line is found, return nil."
(when (null point)
(goto-char (point-min)))
(if (funcall predicate)
(point)
(bongo-point-at-next-line-satisfying predicate)))
(defvar bongo-random-number-generator-seeded nil
"Non-nil if Bongo has seeded the random number generator.")
(defun bongo-point-at-random-line-satisfying (predicate)
"Return the position of a random line satisfying PREDICATE.
If there are no lines that satisfy PREDICATE, loop forever."
(unless bongo-random-number-generator-seeded
(random t)
(setq bongo-random-number-generator-seeded t))
(save-excursion
(while (progn (goto-line (+ (point-min)
(random (count-lines (point-max)
(point-min)))))
(not (funcall predicate))))
(point)))
(defun bongo-point-at-random-track-line ()
"Return the position of a random track line.
If there are no track lines, loop forever."
(bongo-point-at-random-line-satisfying 'bongo-track-line-p))
(defun bongo-point-before-previous-object-line (&optional point)
"Return the character position of the previous object line.
If POINT is non-nil, start before that line; otherwise,
start before the current line.
If no object line is found before the starting line, return nil."
(bongo-point-before-previous-line-satisfying 'bongo-object-line-p point))
(defalias 'bongo-point-at-previous-object-line
'bongo-point-before-previous-object-line)
(defun bongo-point-before-next-object-line (&optional point)
"Return the character position of the next object line.
If POINT is non-nil, start after that line; otherwise,
start after the current line.
If no object line is found after the starting line, return nil."
(bongo-point-before-next-line-satisfying 'bongo-object-line-p point))
(defalias 'bongo-point-at-next-object-line
'bongo-point-before-next-object-line)
(defun bongo-point-after-next-object-line (&optional point)
"Return the character position after the next object line.
This function works like `bongo-point-before-next-object-line'.
Start searching at POINT, or at point if POINT is nil."
(bongo-point-after-next-line-satisfying 'bongo-object-line-p point))
(put 'bongo-no-previous-object
'error-conditions
'(error bongo-movement-error bongo-no-previous-object))
(put 'bongo-no-previous-object
'error-message
"No previous section or track")
(defun bongo-previous-object-line (&optional no-error)
"Move point to the previous object line, if possible.
If NO-ERROR is non-nil, return non-nil if and only if point was moved.
If NO-ERROR is not given or nil, and there is no previous object line,
signal `bongo-no-previous-object'."
(interactive "p")
(let ((position (bongo-point-at-previous-object-line)))
(if position
(prog1 'point-moved
(goto-char position))
(unless no-error
(signal 'bongo-no-previous-object nil)))))
(bongo-define-obsolete-function-alias
'bongo-backward-object-line
'bongo-previous-object-line)
(put 'bongo-no-next-object
'error-conditions
'(error bongo-movement-error bongo-no-next-object))
(put 'bongo-no-next-object
'error-message
"No next section or track")
(defun bongo-next-object-line (&optional no-error)
"Move point to the next object line, if possible.
If NO-ERROR is non-nil, return non-nil if and only if point was moved.
If NO-ERROR is not given or nil, and there is no next object line,
signal `bongo-no-next-object'."
(interactive "p")
(let ((position (bongo-point-at-next-object-line)))
(if position
(prog1 'point-moved
(goto-char position))
(unless no-error
(signal 'bongo-no-next-object nil)))))
(bongo-define-obsolete-function-alias
'bongo-forward-object-line
'bongo-next-object-line)
(defun bongo-snap-to-object-line (&optional no-error)
"Move point to the next object line unless it is already on one.
If point was already on an object line, return `point-not-moved'.
If point was moved to the next object line, return `point-moved'.
If there is no next object line, signal `bongo-no-next-object'.
If NO-ERROR is non-nil, return nil instead of signalling an error."
(interactive)
(if (bongo-object-line-p)
'point-not-moved
(bongo-next-object-line no-error)))
(bongo-define-obsolete-function-alias
'bongo-maybe-forward-object-line
'bongo-snap-to-object-line)
(put 'bongo-no-previous-header-line
'error-conditions
'(error bongo-movement-error bongo-no-previous-header-line))
(put 'bongo-no-previous-header-line
'error-message
"No previous header line")
(defun bongo-previous-header-line (&optional n)
"Move N header lines backward.
With negative N, move forward instead."
(interactive "p")
(if (< n 0)
(bongo-next-header-line (- n))
(dotimes (dummy n)
(let ((position (bongo-point-at-previous-line-satisfying
'bongo-header-line-p)))
(if position
(goto-char position)
(signal 'bongo-no-previous-header-line nil))))))
(bongo-define-obsolete-function-alias
'bongo-backward-header-line
'bongo-previous-header-line)
(put 'bongo-no-next-header-line
'error-conditions
'(error bongo-movement-error bongo-no-next-header-line))
(put 'bongo-no-next-header-line
'error-message
"No next header line")
(defun bongo-next-header-line (&optional n)
"Move N header lines forward.
With negative N, move backward instead."
(interactive "p")
(if (< n 0)
(bongo-previous-header-line (- n))
(dotimes (dummy n)
(let ((position (bongo-point-at-next-line-satisfying
'bongo-header-line-p)))
(if position
(goto-char position)
(signal 'bongo-no-next-header-line nil))))))
(bongo-define-obsolete-function-alias
'bongo-forward-header-line
'bongo-next-header-line)
(defun bongo-backward-expression (&optional n)
"Move backward across one section, track, or stretch of text.
With prefix argument N, do it that many times.
With negative argument -N, move forward instead."
(interactive "p")
(or n (setq n 1))
(if (< n 0)
(bongo-forward-expression (- n))
(catch 'done
(dotimes (dummy n)
(if (= (point) (point-min))
(throw 'done nil)
(goto-char (or (if (bongo-object-line-p)
(or (bongo-point-before-previous-object)
(bongo-point-at-previous-object-line))
(bongo-point-after-line
(bongo-point-at-previous-object-line)))
(point-min))))))))
(bongo-define-obsolete-function-alias
'bongo-backward-section
'bongo-backward-expression)
(defun bongo-forward-expression (&optional n)
"Move forward across one section, track, or stretch of text.
With prefix argument N, do it that many times.
With negative argument -N, move backward instead.
This function is a suitable value for `forward-sexp-function'."
(interactive "p")
(or n (setq n 1))
(if (< n 0)
(bongo-backward-expression (- n))
(catch 'done
(dotimes (dummy n)
(if (= (point) (point-max))
(throw 'done nil)
(goto-char (or (if (bongo-object-line-p)
(bongo-point-after-object)
(bongo-point-before-next-object-line))
(point-max))))))))
(bongo-define-obsolete-function-alias
'bongo-forward-section
'bongo-forward-expression)
(defun bongo-previous-object (&optional no-error n)
"Move to the previous object (either section or track).
With prefix argument N, do it that many times.
With negative prefix argument -N, move forward instead.
If there is no previous object, signal `bongo-no-previous-object'.
If NO-ERROR is non-nil, move to the beginning of the buffer instead.
Return non-nil only if the move was successful."
(interactive "p")
(or n (setq n 1))
(if (< n 0)
(bongo-next-object no-error (- n))
(let ((result t))
(dotimes (dummy n result)
(goto-char (or (bongo-point-at-previous-object)
(if no-error
(prog1 (point-min)
(setq result nil))
(signal 'bongo-no-previous-object nil))))))))
(defun bongo-next-object (&optional no-error n)
"Move to the next object (either section or track).
With prefix argument N, do it that many times.
With negative prefix argument -N, move backward instead.
If there is no next object, signal `bongo-no-next-object'.
If NO-ERROR is non-nil, move to end of the buffer instead.
Return non-nil only if the move was successful."
(interactive (list nil (prefix-numeric-value current-prefix-arg)))
(or n (setq n 1))
(if (< n 0)
(bongo-previous-object no-error (- n))
(let ((result t))
(dotimes (dummy n result)
(goto-char (or (bongo-point-at-next-object)
(if no-error
(prog1 (point-max)
(setq result nil))
(signal 'bongo-no-next-object nil))))))))
(defun bongo-point-before-next-track-line (&optional point)
"Return the character position of the next track line.
If POINT is non-nil, start after that line; otherwise,
start after the current line.
If no track line is found after the starting line, return nil."
(bongo-point-before-next-line-satisfying 'bongo-track-line-p point))
(defalias 'bongo-point-at-next-track-line
'bongo-point-before-next-track-line)
(defun bongo-point-before-previous-track-line (&optional point)
"Return the character position of the previous track line.
If POINT is non-nil, start before that line; otherwise,
start before the current line.
If no track line is found before the starting line, return nil."
(bongo-point-before-previous-line-satisfying 'bongo-track-line-p point))
(defalias 'bongo-point-at-previous-track-line
'bongo-point-before-previous-track-line)
(defun bongo-point-after-object (&optional point)
"Return the character position after the object at POINT.
By object is meant either section or track.
If there are no sections or tracks at POINT, return nil."
(save-excursion
(bongo-goto-point point)
(when line-move-ignore-invisible
(bongo-skip-invisible))
(when (bongo-snap-to-object-line 'no-error)
(let ((indentation (bongo-line-indentation)))
(let ((after-last nil))
(bongo-ignore-movement-errors
(while (progn
(setq after-last (bongo-point-after-line))
(bongo-next-object-line)
(> (bongo-line-indentation) indentation))))
after-last)))))
(bongo-define-obsolete-function-alias
'bongo-point-after-section
'bongo-point-after-object)
(defun bongo-point-at-next-object (&optional point)
"Return the character position of the object after the one at POINT.
By object is meant either section or track.
If there are no sections or tracks after the one at POINT, return nil."
(save-excursion
(bongo-goto-point point)
(when line-move-ignore-invisible
(bongo-skip-invisible))
(if (bongo-object-line-p)
(let ((indentation (bongo-line-indentation)))
(goto-char (bongo-point-after-object))
(when (and (bongo-snap-to-object-line 'no-error)
(= (bongo-line-indentation) indentation))
(point)))
(when (bongo-snap-to-object-line 'no-error)
(point)))))
(defun bongo-point-before-previous-object (&optional point)
"Return the character position of the object previous to POINT.
By object is meant either section or track.
If there are no sections or tracks before POINT, return nil."
(save-excursion
(bongo-goto-point point)
(when line-move-ignore-invisible
(bongo-skip-invisible))
(let ((indentation (if (bongo-snap-to-object-line 'no-error)
(bongo-line-indentation)
0)))
(bongo-ignore-movement-errors
(while (progn
(bongo-previous-object-line)
(> (bongo-line-indentation) indentation)))
(when (= (bongo-line-indentation) indentation)
(bongo-point-before-line))))))
(bongo-define-obsolete-function-alias
'bongo-point-before-previous-section
'bongo-point-before-previous-object)
(defalias 'bongo-point-at-previous-object
'bongo-point-before-previous-object)
(defun bongo-point-at-first-track-line ()
"Return the character position of the first track line, or nil."
(save-excursion
(goto-char (point-min))
(if (bongo-track-line-p)
(bongo-point-at-bol)
(bongo-point-at-next-track-line))))
(defun bongo-point-at-last-track-line ()
"Return the character position of the last track line, or nil."
(save-excursion
(goto-char (point-max))
(if (bongo-track-line-p)
(bongo-point-at-bol)
(bongo-point-at-previous-track-line))))
(defun bongo-uri-track-infoset (&optional point)
"Return the infoset for the URI track at POINT.
You should use `bongo-line-infoset' most of the time."
(let ((file-name
(bongo-line-file-name point))
(uri-title
(bongo-line-get-property 'bongo-uri-title point))
(stream-name
(bongo-line-get-property 'bongo-stream-name point))
(stream-genre
(bongo-line-get-property 'bongo-stream-genre point))
(stream-part-title
(let ((player (bongo-line-get-property 'bongo-player point)))
(and player (bongo-player-running-p player)
(bongo-player-get player 'stream-part-title)))))
(cond ((or stream-name stream-genre stream-part-title)
`((artist . unknown)
(album . unknown)
(stream (uri . ,file-name)
(uri-title . ,uri-title)
(name . ,stream-name)
(genre . ,stream-genre)
(part-title . ,stream-part-title))))
(uri-title
(let ((track-length
(bongo-line-get-property 'bongo-track-length point)))
`((artist . unknown)
(album . unknown)
(track (title . ,uri-title)
,@(when track-length
`((length . ,track-length)))))))
(t
(bongo-file-track-infoset point)))))
(defun bongo-file-track-infoset (&optional point)
"Return the infoset for the file track at POINT.
You should use `bongo-line-infoset' most of the time."
(let ((bongo-track-length
(bongo-line-get-property 'bongo-track-length point)))
(bongo-infoset-from-file-name (bongo-line-file-name point))))
(defun bongo-action-track-infoset (&optional point)
"Return the infoset for the action track at POINT.
You should use `bongo-line-infoset' most of the time."
`((action . ,(bongo-line-action point))))
(defun bongo-track-infoset (&optional point)
"Return the infoset for the track at POINT.
You should use `bongo-line-infoset' most of the time."
(unless (bongo-track-line-p point)
(error "Point is not on a track line"))
(or (bongo-line-get-property 'bongo-infoset point)
(cond ((bongo-uri-track-line-p point)
(bongo-uri-track-infoset point))
((bongo-file-track-line-p point)
(bongo-file-track-infoset point))
((bongo-action-track-line-p point)
(bongo-action-track-infoset point)))))
(defun bongo-header-infoset (&optional point)
"Return the infoset for the header at POINT.
You should use `bongo-line-infoset' most of the time."
(save-excursion
(bongo-goto-point point)
(unless (bongo-header-line-p)
(error "Point is not on a header line"))
(let* ((line-move-ignore-invisible nil)
(fields (bongo-line-fields))
(indentation (bongo-line-indentation)))
(while (and (bongo-next-object-line 'no-error)
(> (bongo-line-indentation) indentation)
(or (not (bongo-track-line-p))
(bongo-action-track-line-p))))
(if (and (> (bongo-line-indentation) indentation)
(bongo-track-line-p))
(bongo-filter-alist fields (bongo-track-infoset))
(mapcar (lambda (field)
(cons field 'unbound))
fields)))))
(defun bongo-line-infoset (&optional point)
"Return the infoset for the line at POINT.
For header lines, derive the infoset from the `bongo-fields' text
property and the infoset of the nearest following track line.
For track lines, first check the `bongo-infoset' text property: If the
value is non-nil, return it; otherwise, construct the infoset as follows.
For action track lines, construct the infoset as ((action . ACTION)),
where ACTION is the value of the `bongo-action' text property.
For file track lines, construct the infoset by passing the file name to
the value of `bongo-file-name-parsing-function'."
(cond ((bongo-track-line-p point)
(bongo-track-infoset point))
((bongo-header-line-p point)
(bongo-header-infoset point))))
(defun bongo-line-internal-infoset (&optional point)
"Return the internal infoset for the line at POINT.
The internal infoset contains values of the internal fields only."
(bongo-filter-alist (bongo-line-internal-fields point)
(bongo-line-infoset point)))
(defun bongo-line-field-value (field &optional point)
"Return the value of FIELD for the line at POINT."
(cdr (assoc field (bongo-line-infoset point))))
(defun bongo-line-field-values (fields &optional point)
"Return the values of FIELDS for the line at POINT."
(mapcar 'cdr (bongo-filter-alist fields (bongo-line-infoset point))))
(defun bongo-line-fields (&optional point)
"Return the names of the fields defined for the line at POINT."
(if (bongo-header-line-p point)
(bongo-line-get-property 'bongo-fields point)
(mapcar 'car (bongo-line-infoset point))))
(defun bongo-line-external-fields (&optional point)
"Return the names of the fields external to the line at POINT."
(bongo-line-get-property 'bongo-external-fields point))
(defun bongo-line-set-external-fields (fields &optional point)
"Set FIELDS to be external to the line at POINT.
FIELDS should be a list of field names."
(save-excursion
(bongo-goto-point point)
(bongo-line-set-property 'bongo-external-fields fields)
(if (bongo-empty-header-line-p)
(bongo-delete-line)
(bongo-redisplay-line))))
(defun bongo-line-internal-fields (&optional point)
"Return the names of the fields internal to the line at POINT."
(bongo-set-difference (bongo-line-fields point)
(bongo-line-external-fields point)))
(defun bongo-line-indentation (&optional point)
"Return the number of external fields of the line at POINT."
(length (bongo-line-external-fields point)))
(defun bongo-line-indented-p (&optional point)
(> (bongo-line-indentation point) 0))
(defun bongo-line-external-fields-proposal (&optional point)
"Return the external fields proposal of the line at POINT.
This proposal is a list of field names that subsequent lines can
externalize if their field values match those of this line.
For track lines, this is always the same as the external field names.
For header lines, the internal field names are also added."
(cond ((bongo-track-line-p point)
(bongo-line-external-fields point))
((bongo-header-line-p point)
(append (bongo-line-external-fields point)
(bongo-line-internal-fields point)))))
(defun bongo-line-indentation-proposal (&optional point)
"Return the number of external fields proposed by the line at POINT.
See `bongo-line-external-fields-proposal'."
(cond ((bongo-track-line-p point)
(bongo-line-indentation point))
((bongo-header-line-p point)
(+ (length (bongo-line-external-fields point))
(length (bongo-line-internal-fields point))))))
(defun bongo-line-proposed-external-fields (&optional point)
"Return the external fields proposed to the line at POINT.
This is nil for the first line, and equal to the external field names
proposal of the previous object line for all other lines."
(catch 'return
(bongo-line-external-fields-proposal
(or (bongo-point-at-previous-object-line point)
(throw 'return nil)))))
(defun bongo-line-proposed-indentation (&optional point)
"Return the number of external fields proposed to the line at POINT.
See `bongo-line-proposed-external-fields'."
(catch 'return
(bongo-line-indentation-proposal
(or (bongo-point-at-previous-object-line point)
(throw 'return 0)))))
;;; (defun bongo-line-relatively-outdented-p ()
;;; (< (bongo-line-indentation) (bongo-line-proposed-indentation)))
(defun bongo-line-file-name (&optional point)
"Return the `bongo-file-name' text property of the line at POINT."
(bongo-line-get-property 'bongo-file-name point))
(defun bongo-line-action (&optional point)
"Return the `bongo-action' text property of the line at POINT."
(bongo-line-get-property 'bongo-action point))
(defun bongo-file-track-line-p (&optional point)
"Return non-nil if the line at POINT is a file track line."
(bongo-line-file-name point))
(defun bongo-uri-track-line-p (&optional point)
"Return non-nil if the line at POINT is a URI track line."
(and (bongo-file-track-line-p point)
(bongo-uri-p (bongo-line-file-name point))))
(defun bongo-audio-cd-uri-p (file-name)
"Return non-nil if FILE-NAME is an audio CD (CDDA) URI."
(equal (bongo-uri-scheme file-name) "cdda"))
(defun bongo-audio-cd-track-line-p (&optional point)
"Return non-nil if the line at POINT is an audio CD track line."
(and (bongo-file-track-line-p point)
(bongo-audio-cd-uri-p (bongo-line-file-name point))))
(defun bongo-local-file-track-line-p (&optional point)
"Return non-nil if the line at POINT is a local file track line."
(and (bongo-file-track-line-p point)
(not (bongo-uri-track-line-p point))))
(defcustom bongo-audio-file-name-extensions
`("ogg" "flac" "spx" "mka" "mp3" "wav" "wma"
"mid" "midi" "mod" "rcp" "r36" "g18" "g36"
"669" "amf" "dsm" "far" "gdm" "imf"
"it" "med" "mod" "mtm" "okt" "s3m"
"stm" "stx" "ult" "uni" "apun" "xm")
"List of file name extensions of audio files."
:type '(repeat string)
:group 'bongo-file-names)
(defun bongo-audio-file-name-p (file-name)
"Return non-nil if FILE-NAME has an audio file name extension."
(member (file-name-extension file-name)
bongo-audio-file-name-extensions))
(defun bongo-local-audio-file-track-line-p (&optional point)
"Return non-nil if the line at POINT is an audio file track line."
(and (bongo-local-file-track-line-p point)
(bongo-audio-file-name-p (bongo-line-file-name point))))
(defcustom bongo-video-file-name-extensions
`("ogm" "avi" "mpg" "mpeg" "mp4" "vob" "mkv" "flv"
"mov" "asf" "wmv" "rm" "rmvb" "qt" "ts")
"List of file name extensions of video files."
:type '(repeat string)
:group 'bongo-file-names)
(defun bongo-video-file-name-p (file-name)
"Return non-nil if FILE-NAME has a video file name extension."
(member (file-name-extension file-name)
bongo-video-file-name-extensions))
(defun bongo-local-video-file-track-line-p (&optional point)
"Return non-nil if the line at POINT is an video file track line."
(and (bongo-local-file-track-line-p point)
(bongo-video-file-name-p (bongo-line-file-name point))))
(defun bongo-action-track-line-p (&optional point)
"Return non-nil if the line at POINT is an action track line."
(bongo-line-action point))
(defun bongo-track-line-p (&optional point)
"Return non-nil if the line at POINT is a track line."
(case major-mode
((bongo-playlist-mode bongo-library-mode)
(or (bongo-file-track-line-p point)
(bongo-action-track-line-p point)))
(dired-mode
(save-excursion
(bongo-goto-point point)
(ignore-errors
(bongo-backend-for-file (dired-get-file-for-visit)))))))
(defun bongo-currently-playing-track-line-p (&optional point)
"Return non-nil if the line at POINT is currently playing."
(and (bongo-current-track-line-p point)
(bongo-playing-p)))
(defun bongo-played-track-line-p (&optional point)
"Return non-nil if the line at POINT is a played track line."
(and (bongo-track-line-p point)
(bongo-line-get-property 'bongo-played point)))
(defun bongo-unplayed-track-line-p (&optional point)
"Return non-nil if the line at POINT is an unplayed track line."
(and (bongo-track-line-p point)
(null (bongo-line-get-property 'bongo-played point))))
(defun bongo-mark-line-as-played (&optional point)
"Mark the track line at POINT as played."
(bongo-line-set-property 'bongo-played t point)
(bongo-redisplay-line point)
(when bongo-sprinkle-mode
(bongo-sprinkle-until-saturated)))
(defun bongo-mark-current-track-line-as-played ()
"Mark the current track line as played.
If there is no current track line, do nothing."
(catch 'abort
(let ((line-move-ignore-invisible nil))
(bongo-mark-line-as-played
(or (bongo-point-at-current-track-line)
(throw 'abort nil))))))
(defun bongo-track-lines-exist-p ()
"Return non-nil if the buffer contains any track lines.
This function does not care about the visibility of the lines."
(let (track-lines-exist)
(let ((line-move-ignore-invisible nil))
(save-excursion
(goto-char (point-min))
(while (and (not (eobp)) (not track-lines-exist))
(when (bongo-track-line-p)
(setq track-lines-exist t))
(forward-line))))
track-lines-exist))
(defun bongo-header-line-p (&optional point)
"Return non-nil if the line at POINT is a header line."
(bongo-line-get-property 'bongo-header point))
(defun bongo-object-line-p (&optional point)
"Return non-nil if the line at POINT is an object line.
Object lines are either track lines or header lines."
(or (bongo-track-line-p point) (bongo-header-line-p point)))
(defun bongo-empty-header-line-p (&optional point)
"Return non-nil if the line at POINT is an empty header line.
Empty header lines have no internal fields and are not supposed ever
to exist for long enough to be visible to the user."
(and (bongo-header-line-p point)
(null (bongo-line-internal-fields point))))
(defun bongo-collapsed-header-line-p (&optional point)
"Return non-nil if the line at POINT is a collapsed header line.
Collapsed header lines are header lines whose sections are collapsed."
(and (bongo-header-line-p point)
(bongo-line-get-property 'bongo-collapsed point)))
(defun bongo-empty-section-p (&optional point)
"Return non-nil if the line at POINT is an empty section.
That is, the header line of a section that has no content."
(and (bongo-header-line-p point)
(catch 'return
(let ((line-move-ignore-invisible nil))
(not (> (bongo-line-indentation
(or (bongo-point-at-next-object-line point)
(throw 'return t)))
(bongo-line-indentation point)))))))
;;;; General convenience routines
(defsubst bongo-xor (a b)
"Return non-nil if exactly one of A and B is nil."
(if a (not b) b))
(defun bongo-shortest (a b)
"Return the shorter of the lists A and B."
(if (<= (length a) (length b)) a b))
(defun bongo-longest (a b)
"Return the longer of the lists A and B."
(if (>= (length a) (length b)) a b))
(defun bongo-equally-long-p (a b)
"Return non-nil if the lists A and B have equal length."
(= (length a) (length b)))
(defun bongo-set-union (&rest sets)
"Return the set-theoretic union of the items in SETS.
Comparisons are done with `eq'. Order is *not* preserved."
(let (result)
(dolist (set sets result)
(dolist (entry set)
(unless (memq entry result)
(push entry result))))))
(defun bongo-set-intersection (a b)
"Return the items in A that are also in B.
Comparisons are done with `eq'. Order is preserved."
(let (result)
(dolist (entry a (nreverse result))
(when (memq entry b)
(push entry result)))))
(defun bongo-set-exclusive-or (a b)
"Return the items that appear in either A or B but not both.
Comparisons are done with `eq'. Order is *not* preserved."
(let (result)
(dolist (set (list a b) result)
(dolist (entry set)
(when (bongo-xor (memq entry a) (memq entry b))
(push entry result))))))
(defun bongo-set-difference (a b)
"Return the items in A that are not also in B.
Comparisons are done with `eq'. Order is preserved."
(let (result)
(dolist (entry a (nreverse result))
(unless (memq entry b)
(push entry result)))))
(defun bongo-set-equal-p (a b)
"Return non-nil if A and B have equal elements.
Comparisons are done with `eq'. Element order is not significant."
(null (bongo-set-exclusive-or a b)))
(defun bongo-subset-p (a b)
"Return non-nil if all elements in B are also in A.
Comparisons are done with `eq'. Element order is not significant."
(bongo-set-equal-p (bongo-set-union a b) a))
(defun bongo-alist-get (alist key)
"Return the cdr of the element in ALIST whose car equals KEY.
If no such element exists, return nil."
(cdr (assoc key alist)))
(defun bongo-alist-put (alist key value)
"Set the cdr of the element in ALIST whose car equals KEY to VALUE.
If no such element exists, add a new element to the start of ALIST.
This function destructively modifies ALIST and returns the new head.
If ALIST is a symbol, operate on the vaule of that symbol instead."
(if (and (symbolp alist) (not (null alist)))
(set alist (bongo-alist-put (symbol-value alist) key value))
(let ((entry (assoc key alist)))
(if entry (prog1 alist (setcdr entry value))
(cons (cons key value) alist)))))
;; This function is used by `define-bongo-backend'.
(eval-and-compile
(defun bongo-plist-get-all (plist property)
"Return a list of all values in PLIST corresponding to PROPERTY."
(let ((result nil))
(while plist
(when (eq (car plist) property)
(push (cadr plist) result))
(setq plist (cddr plist)))
(nreverse result))))
(defun bongo-filter-alist (keys alist)
"Return a new list of each pair in ALIST whose car is in KEYS.
Key comparisons are done with `eq'. Order is preserved."
(let (result)
(dolist (entry alist (nreverse result))
(when (memq (car entry) keys)
(push entry result)))))
(defun bongo-filter-plist (keys plist)
"Return a new list of each property in PLIST whose name is in KEYS.
Key comparisons are done with `eq'. Order is *not* preserved."
(let ((result nil))
(while plist
(when (memq (car plist) keys)
(setq result (cons (car plist) (cons (cadr plist) result))))
(setq plist (cddr plist)))
result))
(defun bongo-region-active-p ()
"Return non-nil if the region is active."
(and transient-mark-mode mark-active))
;;;; Line-oriented convenience routines
(defun bongo-ensure-final-newline ()
"Make sure the last line in the current buffer ends with a newline.
Do nothing if the current buffer is empty."
(or (= (point-min) (point-max))
(= (char-before (point-max)) ?\n)
(save-excursion
(goto-char (point-max))
(insert "\n"))))
(defun bongo-delete-line (&optional point)
"Delete the line at POINT."
(let ((inhibit-read-only t))
(delete-region (bongo-point-before-line point)
(bongo-point-after-line point))))
(defun bongo-line-string (&optional point)
"Return the contents of the line at POINT.
The contents includes the final newline, if any."
(buffer-substring (bongo-point-before-line point)
(bongo-point-after-line point)))
(defun bongo-extract-line (&optional point)
"Delete the line at POINT and return its content.
The content includes the final newline, if any."
(prog1 (bongo-line-string point)
(bongo-delete-line point)))
(defun bongo-clear-line (&optional point)
"Remove all contents of the line at POINT."
(let ((inhibit-read-only t))
(bongo-ensure-final-newline)
(save-excursion
(bongo-goto-point point)
;; Avoid deleting the newline, because that would
;; cause the markers on this line to become mixed up
;; with those on the next line.
(delete-region (point-at-bol) (point-at-eol))
;; Remove all text properties from the newline.
(set-text-properties (point) (1+ (point)) nil))))
(defun bongo-region-line-count (beg end)
"Return the number of lines between BEG and END.
If BEG and END are the same, return 0.
If they are distinct but on the same line, return 1."
(save-excursion
(goto-char beg)
(let ((result 0))
(while (< (point) end)
(setq result (1+ result))
(forward-line))
result)))
(defun bongo-count-lines-satisfying (predicate &optional beg end)
"Return the total number of lines that satisfy PREDICATE.
If BEG and END are non-nil, only count lines in that region."
(or beg (setq beg (point-min)))
(or end (setq end (point-max)))
(let ((result 0))
(save-excursion
(goto-char beg)
(while (< (point) end)
(when (funcall predicate)
(setq result (+ result 1)))
(forward-line 1)))
result))
;;;; Text properties
(defun bongo-line-get-property (name &optional point)
"Return the value of the text property NAME on the line at POINT.
Actually only look at the terminating newline."
(get-text-property (bongo-point-at-eol point) name))
(defvar bongo-line-semantic-properties
;; When changing this, consider also changing
;; `bongo-line-serializable-properties'.
(list 'bongo-file-name 'bongo-action 'bongo-backend
'bongo-infoset 'bongo-uri-title 'bongo-track-length
'bongo-stream-name 'bongo-stream-genre
'bongo-fields 'bongo-external-fields
'bongo-header 'bongo-collapsed
'bongo-marked 'bongo-reference-counted-marker
'bongo-player 'bongo-played)
"List of semantic text properties used in Bongo buffers.
When redisplaying lines, semantic text properties are preserved,
whereas all other text properties (e.g., `face') are discarded.")
(defun bongo-line-get-semantic-properties (&optional point)
"Return the list of semantic text properties on the line at POINT.
Actually only look at the terminating newline.
The value of `bongo-line-semantic-properties' determines which
text properties are considered \"semantic\" by this function."
(bongo-filter-plist bongo-line-semantic-properties
(text-properties-at (bongo-point-at-eol point))))
(defun bongo-line-set-property (name value &optional point)
"Set the text property NAME to VALUE on the line at POINT.
The text property will only be set for the terminating newline."
(let ((inhibit-read-only t)
(position (bongo-point-at-eol point)))
(bongo-ensure-final-newline)
(put-text-property position (1+ position) name value)))
(put 'bongo-line-set-property 'lisp-indent-function 1)
(defun bongo-line-set-properties (properties &optional point)
"Set the text properties PROPERTIES on the line at POINT.
The text properties will only be set for the terminating newline."
(let ((inhibit-read-only t)
(position (bongo-point-at-eol point)))
(bongo-ensure-final-newline)
(add-text-properties position (1+ position) properties)))
(defun bongo-line-remove-property (name &optional point)
"Remove the text property NAME from the line at POINT.
The text properties will only be removed from the terminating newline."
(let ((inhibit-read-only t)
(position (bongo-point-at-eol point)))
(bongo-ensure-final-newline)
(remove-text-properties position (1+ position) (list name nil))))
(defun bongo-keep-text-properties (beg end keys)
"Keep only some properties in the text between BEG and END.
Remove any property that is not in KEYS."
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(while (not (eobp))
(let* ((properties (text-properties-at (point)))
(kept-properties (bongo-filter-plist keys properties))
(next (or (next-property-change (point)) (point-max))))
(set-text-properties (point) next kept-properties)
(goto-char next))))))
;;;; Sectioning
(defvar bongo-bind-field-values nil
"If non-nil, `unbound' field values are joinable.
This variable should only be bound environmentally.
See `bongo-joinable-field-values-p'.")
(defun bongo-joinable-field-values-p (a b)
"Return non-nil if A and B are joinable field values.
Field values are joinable if they are `equal', except that
- `unknown' is not joinable with anything (including itself),
- `unbound' is joinable with anything non-`unknown' if and
only if `bongo-bind-field-values' is non-nil, and
- nil is joinable with everything."
(or (null a) (null b)
(and bongo-bind-field-values
(or (and (eq a 'unbound)
(not (eq b 'unknown)))
(and (eq b 'unbound)
(not (eq a 'unknown)))))
(and (not (eq a 'unknown))
(equal a b))))
(defun bongo-region-joinable-on-field-p (beg end field)
"Return non-nil if all lines in the region are joinable on FIELD.
FIELD is a symbol naming a field (for example, `artist' or `title').
Two lines are joinable on FIELD if their values for FIELD are joinable.
If there is less than two object lines between BEG and END, return nil.
Also return nil if the value for FIELD is nil throughout the region.
See `bongo-joinable-field-values-p' about what it means for two field
values to be joinable."
(save-excursion
(let ((result 'maybe))
(goto-char beg)
(bongo-ignore-movement-errors
(bongo-snap-to-object-line)
(let ((last (bongo-line-field-value field)))
(while (progn (bongo-next-object-line)
(and (< (point) end) result))
(let ((current (bongo-line-field-value field)))
(if (bongo-joinable-field-values-p last current)
(when (or last current)
(setq result t))
(setq result nil))))))
(eq result t))))
(bongo-define-obsolete-function-alias
'bongo-field-common-in-region-p
'bongo-region-joinable-on-field-p "2006-12-07")
;; XXX: This will not work properly unless the list of
;; potential external fields is completely ordered.
(defun bongo-potential-external-fields-in-region (beg end)
"Return the names of the fields that could join the region.
That is, the fields on which the region between BEG and END is joinable.
See `bongo-region-joinable-on-field-p' about what it means for a region
to be joinable on a field."
(let ((fields (reverse bongo-potential-external-fields))
(joining-fields nil))
(while fields
(if (bongo-region-joinable-on-field-p beg end (car fields))
(when (null joining-fields)
(setq joining-fields fields))
(setq joining-fields nil))
(setq fields (cdr fields)))
joining-fields))
(bongo-define-obsolete-function-alias
'bongo-common-fields-in-region
'bongo-potential-external-fields-in-region "2006-12-07")
(defun bongo-potential-external-fields-at-point (&optional point)
"Return the names of the fields that could join tracks around POINT.
That is, find the largest subset of `bongo-potential-external-fields'
such that some region around POINT is joinable on all those fields.
See `bongo-potential-external-fields-in-region'."
(let ((interesting-line-p
(lambda ()
(catch 'return
(dolist (field (bongo-line-fields) nil)
(when (and (bongo-line-field-value field)
(memq field bongo-potential-external-fields))
(throw 'return t)))))))
(save-excursion
(bongo-goto-point point)
(let ((before-previous
(bongo-point-before-previous-line-satisfying
interesting-line-p))
(after-next
(bongo-point-after-next-line-satisfying
interesting-line-p)))
(if (funcall interesting-line-p)
(bongo-longest
(when before-previous
(bongo-potential-external-fields-in-region
before-previous (bongo-point-after-line)))
(when after-next
(bongo-potential-external-fields-in-region
(bongo-point-before-line) after-next)))
(when (and before-previous after-next)
(bongo-potential-external-fields-in-region
before-previous after-next)))))))
;;; XXX: This will not work properly unless the list of
;;; potential external fields is completely ordered.
;;;
;;; This function appears to be unused, and since its
;;; implementation is not general, I commented it out.
;;;
;;; (defun bongo-fields-external-in-region-p (beg end fields)
;;; "Return non-nil if FIELDS are external between BEG and END.
;;; FIELDS is a list of symbols naming fields (for example, `artist').
;;; More precisely, return nil if and only if there is a field in FIELDS
;;; that is not external for some line in the region."
;;; (save-excursion
;;; (let ((result t))
;;; (goto-char beg)
;;; (while (and (< (point) end) result)
;;; (when (< (bongo-line-indentation) (length fields))
;;; (setq result nil))
;;; (forward-line))
;;; result)))
(defun bongo-line-potential-external-fields (&optional point)
"Return the field names of the line at POINT that could be external.
That is, return the largest subset of `bongo-potential-external-fields'
that could join the line at POINT with the previous object line.
If the line at POINT is the first line, return nil."
(catch 'return
(bongo-potential-external-fields-in-region
(or (bongo-point-before-previous-object-line point)
(throw 'return nil))
(bongo-point-after-line point))))
(defun bongo-line-externalizable-fields (&optional point)
"Return the externalizable fields of the line at POINT.
That is, return the names of all internal fields of the line at POINT
that could be made external without controversy.
This function respects `bongo-insert-intermediate-headers',
in order to implement the correct semantics."
(if bongo-insert-intermediate-headers
(bongo-set-difference (bongo-set-intersection
(bongo-line-proposed-external-fields point)
(bongo-line-potential-external-fields point))
(bongo-line-external-fields point))
;; We are looking for an already existing header line, above the
;; current line, such that the proposed external fields below the
;; existing header line is a subset of the potential external
;; fields of the current line. If such a header line exists, then
;; the externalizable fields of the current line is equal to the
;; proposed external fields of the existing header line.
(let ((potential (bongo-line-potential-external-fields point)))
(bongo-ignore-movement-errors
(save-excursion
;; We begin the search on the previous line.
(bongo-previous-object-line)
;; If this is a header line, it might be the one we are
;; looking for.
(or (and (bongo-header-line-p)
(let ((proposal (bongo-line-external-fields-proposal)))
(and (bongo-subset-p potential proposal) proposal)))
;; If not, continue the search by backing up to the parent
;; header line while there still is one.
(let (fields)
(while (and (null fields) (bongo-line-indented-p))
(bongo-backward-up-section)
(let ((proposal (bongo-line-external-fields-proposal)))
(when (bongo-subset-p potential proposal)
(setq fields proposal))))
fields)))))))
(defun bongo-down-section (&optional n)
"Move to the first object line in the section at point.
With N, repeat that many times.
If there are not enough sections at point, signal an error."
(interactive "p")
(or n (setq n 1))
(while (> n 0)
(bongo-snap-to-object-line)
(if (bongo-header-line-p)
(let ((indentation (bongo-line-indentation)))
(unless (and (bongo-next-object-line 'no-error)
(> (bongo-line-indentation) indentation))
(error "Empty section")))
(error "No section here"))
(setq n (- n 1))))
(defun bongo-backward-up-section (&optional n)
"Move to the header line of this section.
With N, repeat that many times."
(interactive "p")
(or n (setq n 1))
(while (> n 0)
(let ((indentation (bongo-line-indentation)))
(when (zerop indentation)
(error "Already at the top level"))
(while (progn (bongo-previous-object-line)
(>= (bongo-line-indentation) indentation))))
(setq n (- n 1))))
(defun bongo-maybe-insert-intermediate-header ()
"Make sure that the current line has a suitable header.
If the first outer header is too specific, split it in two."
(when (bongo-line-indented-p)
(let ((current (bongo-line-external-fields)))
(save-excursion
(bongo-backward-up-section)
(let ((proposal (bongo-line-external-fields-proposal)))
(unless (bongo-set-equal-p current proposal)
;; The new header will not display properly
;; until the header below is indented correctly,
;; so call `bongo-externalize-fields' on the old
;; header before calling `bongo-redisplay-line'
;; on the new header.
(let ((header-line-position (point)))
(bongo-insert-header current)
(let ((bongo-bind-field-values t))
(bongo-externalize-fields))
(bongo-redisplay-line header-line-position))))))))
(defun bongo-externalize-fields ()
"Externalize as many fields of the current line as possible.
This function may create a new section header, but only by splitting an
existing header into two (see `bongo-maybe-insert-intermediate-header')."
(unless (zerop (bongo-line-proposed-indentation))
(let ((fields (bongo-line-externalizable-fields)))
(when (> (length fields) (bongo-line-indentation))
(bongo-line-set-external-fields fields)
(bongo-maybe-insert-intermediate-header)))))
;;;; The prefix/region/marking convention
(defun bongo-universal-prefix/region/marking-object-command (&optional n)
"In Bongo, force the prefix/region/marking convention for a command.
That is, read a key sequence and execute the command bound to that sequence
some number of times, each time with point at a different track or section.
If given a prefix argument N, execute the command once for each of the
next N tracks or sections.
Otherwise, if the region is active, execute the command once for each
track or section in the region.
Otherwise, if there are any marked tracks, execute the command once for
each marked track.
Otherwise, just execute the command on the track or section at point.
In all cases (and this is an important detail), execute the command
without any prefix argument, active region or marking.
See `bongo-universal-prefix/region/marking-track-command' for a similar
utility that operates only on tracks (not sections)."
(interactive "P")
(let ((command (key-binding (read-key-sequence nil t))))
(when (eq command 'keyboard-quit)
(keyboard-quit))
(save-excursion
(cond (n
(setq n (prefix-numeric-value n))
(let ((mark-active nil)
(marking bongo-marking))
(when marking
;; XXX: This discards the killed marking
;; as an unfortunate side-effect.
(bongo-kill-marking))
(if (< n 0)
(dotimes (dummy (- n))
(bongo-previous-object)
(save-excursion
(command-execute command)))
(dotimes (dummy n)
(bongo-snap-to-object-line)
(save-excursion
(command-execute command))
(goto-char (bongo-point-after-object))))
(when marking
(bongo-yank-marking))))
((bongo-region-active-p)
(deactivate-mark)
(let ((marking bongo-marking)
(end (move-marker (make-marker) (region-end))))
(when marking
;; XXX: This discards the killed marking
;; as an unfortunate side-effect.
(bongo-kill-marking))
(goto-char (region-beginning))
(while (and (bongo-snap-to-object-line 'no-error)
(< (point) end))
(command-execute command)
(goto-char (bongo-point-after-object)))
(move-marker end nil)
(when marking
(bongo-yank-marking))))
(bongo-marking
(let ((mark-active nil)
(marking bongo-marking))
(bongo-kill-marking)
(dolist (marker (reverse marking))
(catch 'abort
(goto-char (or (marker-position (car marker))
(throw 'abort nil)))
(command-execute command)))))
(t
(let ((mark-active nil)
(marking bongo-marking))
(when marking
;; XXX: This discards the killed marking
;; as an unfortunate side-effect.
(bongo-kill-marking))
(bongo-snap-to-object-line)
(command-execute command)
(when marking
(bongo-yank-marking))))))))
(defun bongo-universal-prefix/region/marking-track-command (&optional n)
"In Bongo, force the prefix/region/marking convention for a command.
That is, read a key sequence and execute the command bound to that sequence
some number of times, each time with point at a different track.
If given a prefix argument N, execute the command once for each of the
next N tracks.
Otherwise, if the region is active, execute the command once for each
track in the region.
Otherwise, if there are any marked tracks, execute the command once for
each marked track.
Otherwise, just execute the command on the track at point.
In all cases (and this is an important detail), execute the command
without any prefix argument, active region or marking.
Unlike `bongo-universal-prefix/region/marking-object-command',
ignore all section structure (i.e., operate only on tracks)."
(interactive "P")
(let ((command (key-binding (read-key-sequence nil t))))
(when (eq command 'keyboard-quit)
(keyboard-quit))
(save-excursion
(cond (n
(setq n (prefix-numeric-value n))
(let ((mark-active nil)
(marking bongo-marking))
(when marking
;; XXX: This discards the killed marking
;; as an unfortunate side-effect.
(bongo-kill-marking))
(if (< n 0)
(dotimes (dummy (- n))
(goto-char (or (bongo-point-at-previous-track-line)
(signal 'bongo-no-previous-object nil)))
(save-excursion
(command-execute command)))
(dotimes (dummy n)
(unless (bongo-track-line-p)
(goto-char (or (bongo-point-at-next-track-line)
(signal 'bongo-no-next-object nil))))
(save-excursion
(command-execute command))
(forward-line 1)))
(when marking
(bongo-yank-marking))))
((bongo-region-active-p)
(deactivate-mark)
(let ((marking bongo-marking)
(end (move-marker (make-marker) (region-end))))
(when marking
;; XXX: This discards the killed marking
;; as an unfortunate side-effect.
(bongo-kill-marking))
(goto-char (region-beginning))
(while (and (or (bongo-track-line-p)
(goto-char (or (bongo-point-at-next-track-line)
(point-max))))
(< (point) end))
(command-execute command)
(forward-line 1))
(move-marker end nil)
(when marking
(bongo-yank-marking))))
(bongo-marking
(let ((mark-active nil)
(marking bongo-marking))
(bongo-kill-marking)
(dolist (marker (reverse marking))
(catch 'abort
(goto-char (or (marker-position (car marker))
(throw 'abort nil)))
(command-execute command)))))
(t
(let ((mark-active nil)
(marking bongo-marking))
(when marking
;; XXX: This discards the killed marking
;; as an unfortunate side-effect.
(bongo-kill-marking))
(unless (bongo-track-line-p)
(goto-char (or (bongo-point-at-next-track-line)
(signal 'bongo-no-next-object nil))))