I have an agent's list made up by elements from its neighbours' lists. I would need to pick an item from it based on a parameter: the higher the parameter h is, the more probable is to pick that item.
To pick the item I tried with: print one-of list, where list contains all the items collected from a neighbour (set list lput item list).
However, I do not know how to add the condition about the parameter.
I tried with max (list item), but this does not consider the value of the parameter h (it can take values from 1 to 10).
Could you please suggest me how to consider it?
Many thanks
The rnd extension does exactly what you need. Put extensions [rnd] at the top of your code. Then use rnd:weighted-one-of-list to do the selection. Here is a complete program that demonstrates. Put it in an empty NetLogo model and then run the testme procedure several times.
extensions [rnd]
to testme
let mylist [1 3 5]
repeat 10
[ type rnd:weighted-one-of-list mylist [ [ii] -> ii ]
type " "
]
end
How do I refer to a color name that is defined by a color theme in another part of emacs?
In base16-solarized-dark-theme.elfile the following colors are defined:
(deftheme base16-solarized-dark)
(let ((base00 "#002b36")
(base01 "#073642")
(base02 "#586e75")
(base03 "#657b83")
(base04 "#839496")
(base05 "#93a1a1")
(base06 "#eee8d5")
(base07 "#fdf6e3")
(base08 "#dc322f")
(base09 "#cb4b16")
(base0A "#b58900")
(base0B "#859900")
(base0C "#2aa198")
(base0D "#268bd2")
(base0E "#6c71c4")
(base0F "#d33682"))
Now, I'm trying to define the face colors for the auto-complete package in my .emacs like this:
(set-face-foreground 'ac-candidate-face "base02")
(set-face-background 'ac-candidate-face "base0A")
But it doesn't work. Emacs doesn't give any error, but the colors don't get defined like that.
I can't tell for sure without seeing the rest of the code, but in this particular case, the chances are that you can't refer to them by name.
let bindings are only in scope while the body of the let form is being evaluated.
Whatever is going on in the body of that form presumably makes use of those named values, but the symbols don't continue to hold those values afterwards.
FYI if they did you would refer to them as, e.g., base0A, rather than "base0A" -- the former being a symbol which evaluates to the value assigned to it, whereas the latter is a string value (which could be interned to get at the variable of that name, but typically wouldn't be if there wasn't a good reason for it).
I want to add item to toolbar in Emacs. I heard Emacs can add even menu item to toolbar.
Thanks.
Hi i found a simple solution to add a toolbar button
"spell" is the image in /usr/share/emacs/23.4/etc/images/
(defun omar-hotel ()
"another nonce menu function"
(interactive)
(message "hotel, motel, holiday inn"))
(tool-bar-add-item "spell" 'omar-hotel
'omar-hotel
:help "Run fonction omar-hotel")
Here's an example from LaTeX mode:
;;; Installation of the tool bar
;;;###autoload
(defun LaTeX-install-toolbar ()
"Install toolbar buttons for LaTeX mode."
(interactive)
(require 'toolbar-x)
(add-to-list 'toolbarx-image-path
(expand-file-name "images" TeX-data-directory))
(add-hook 'TeX-PDF-mode-hook 'toolbarx-refresh nil t)
(toolbarx-install-toolbar TeX-bar-LaTeX-buttons
(let ((append-list))
(dolist (elt TeX-bar-LaTeX-all-button-alists)
(setq append-list (append append-list
(eval elt))))
append-list)))
Found using M-x apropos <RET> toolbar <RET> and clicking through to the source EL file…
The documentation for toolbarx-install-toolbar is pretty lengthy:
Install toolbar buttons given in BUTTONS.
Button properties are optionally given in MEANING-ALIST. If
GLOBAL-FLAG is non-nil, toolbar is installed globally (on every
buffer that does not have a toolbar set locally). BUTTONS is a
list of format
(ELEM ... ELEM . PROPS),
where each ELEM is either
- a list in the same format od BUTTONS, which is going to be
refered as a *group*; groups are used to distribute properties
recursively to its elements; there are groups with special
format for special purpose: *dropdown groups* and also *eval
groups*.
- a symbol, which could be associated in MEANING-ALIST with a
list of button properties (symbol + properties = a *button*)
or associated to a special kind of group (an *alias group*).
- a vector, which elements are on the previous formats (but not
another vector); this is useful to specify different
ingredients to the toolbar depending if editor is Emacs or
XEmacs; the first element will be used in Emacs; the second
element is going to be used in XEmacs.
Meaning alist
=============
MEANING-ALIST is a list where each element is in one of the
formats (SYMB . BUTTON-PROPS-LIST) or (SYMB . ALIAS-GROUP).
BUTTON-PROPS-LIST is a list in one of the formats
(IMAGE COMMAND PROP VAL PROP VAL ... PROP VAL) or
(PROP VAL PROP VAL ... PROP VAL).
The IMAGE is going to be used as the `:image' property of the
button (see button properties bellow), and COMMAND shall be used
as the `:command' property of the button. Each PROP is one of
the button properties, and VAL is its respective value.
ALIAS-GROUP is a list which first element is the symbol `:alias'
and the cdr shall be processed as a group.
However, a symbol is not required to have an association in
MEANING-ALIST, which is only a way to specify properties to a
button. One can use groups to specify properties. Nil is a good
MEANING-ALIST.
Buttons
=======
A toolbar button in `toolbarx' is the set with a symbol and
properties used to display the button, like a image and a command
to call when the button is pressed (which are the minimal
elements that a button should have.) The supported properties
for buttons and their `basic types' (see note on how values of
properties are obtained!) are:
:image -- in Emacs, either a string or image descriptor (see
info for a definition), or a variable bound to a image
descriptor (like those defined with `defimage') or a list of 4
strings or image descriptors; in XEmacs, either a string or a
glyph, or a symbol bount to a glyph, or a list of at least 1
and at most 6 strings or glyphs or nil (not the first element
though); defines the image file displayed by the button. If
it is a string, the image file found with that name (always
using the function `toolbarx-find-image' to make the
\`internal\' image descriptor) is used as button image. For
the other formats, the button image is handled in the same way
as it is treated by the editors; see info nodes bellow for a
description of the capabilities of each editor
Emacs: info file \"elisp\", node \"Tool Bar\" (see `:image'
property);
PS: a *vector* of four strings is used in the Emacs
Lisp documentation as the `more ellaborated' image
property format, but here we reserve vectors to
provide editor-dependent values; this motivates our
choice for a list instead of vector (however,
internally the list becomes a vector when displaying
the button).
XEmacs: info file \"lispref\", node \"Toolbar Descriptor
Format\" (see GLYPH-LIST) or the documentation of
the variable `default-toolbar'; check the inheritage
in case of a ommited glyph or nil instead of glyph.
:command -- a form; if the form happens to be a command, it will
be called with `call-interactively'.
:append-command -- a form added to the end of the value of
`:command'.
:prepend-command -- a form added at the beginning of the value
of `:command'.
:help -- either a string or nil; defined the help string of the
button;
:enable -- a form, evaluated constantly by both editors to
determine if a button is active (enabled) or not.
:visible -- in Emacs, a form that is evaluated constantly to
determine if a button is visible; in XEmacs, this property is
ignored.
:button -- in Emacs, a cons cell (TYPE . SELECTED) where the
TYPE should be `:toggle' or `:radio' and the cdr should be a
form. SELECTED is evaluated to determine when the button is
selected. This property is ignored in XEmacs.
:insert -- a form that is evaluated every time that the toolbar
is refresh (a call of `toolbarx-refresh') to determine if the
button is inserted or just ignored (until next refresh).
:toolbar -- in XEmacs, either one of the symbols `default',
`top', `bottom', `left', `right', or a cons cell
(POS . POS-AVOID-DEFAULT) where POS and POS-AVOID-DEFAULT
should be one of the symbols `top', `bottom', `left', `right';
if a symbol, the button will be inserted in one of these
toolbars; if a cons cell, button will be inserted in toolbar
POS unless the position of the default toolbar is POS (then,
the default toolbar would override the position-specific
toolbar), and in this case, button will be inserted in toolbar
POS-AVOID-DEFAULT; in Emacs, this property is meaningless, and
therefore ignored. Hint of use of this property: in a
program, use or everything with `default' and the cons format
to avoid the default toolbar, or use only the position
specific buttons (symbols that are not `default'), because of
the `overriding' system in XEmacs, when a position-specific
toolbar overrides the default toolbar; for instance, if you
put a button in the default toolbar and another in the top
toolbar (and the default toolbar is in the top), then *only*
the ones in the top toolbar will be visible!
How to specify a button
=======================
One can specify a button by its symbol or by a group to specify
properties. For example,
BUTTON =
( foo
(bar :image [\"bar-Emacs\" \"bar-XEmacs\"]
:command bar-function :help \"Bar help string\")
:insert foo-bar )
MEANING-ALIST = ( (foo :image \"foo\" :command foo-function) )
specifiy two buttons `foo' and `bar', each one with its necessary
:image and :command properties, and both use the :insert property
specified ate the end of BUTTONS (because groups distribute
properties to all its elements). `foo' and `bar' will be
inserted only if `foo-bar' evaluation yields non-nil. `bar' used
a different :image property depending if editor is Emacs or
XEmacs.
Note on how values of properties are obtained
=============================================
For each property PROP, its value should be either:
i) a vector of 2 elements; then each element should be of the
basic type of PROP.
ii) an element on the basic type of PROP.
iii) a function (that does not need arguments); it is evaluated
and the return should be ot type i) or ii) above
iv) a symbol bound to a element of type i) or ii).
The type is cheched in the order i), ii) iii) and iv). This
evaluations are done every time that the oolbar is refresh.
Ps.: in order to specify a vector as value of a property (like
the :image in Emacs), it is necessary to provide the vector as
element of another vector.
Special groups
==============
Eval groups
-----------
If the first element of a group is the symbol `:eval-group', each
element is evaluated (with `eval'), put inside a list and
processed like a group. Eval groups are useful to store
definition of buttons in a variable.
Dropdown groups
---------------
The idea is to specify a set of buttons that appear when a
determined menu item of a dropdown menu is active. The dropdown
menu appears when a button (by default with a triangle pointing
down) is clicked. This button is called `dropdown button'. The
dropdown button appears on the left of the currently visible
buttons of the dropdown group.
A dropdown group is a list which first element is the symbol
`:dropdown-group' and in one of the following formats
(:dropdown-group SYMBOL-1 ... SYMBOL-n PROP-1 VAL-1 ... PROP-k VAL-k)
or
(:dropdown-group
STRING-1 ITEM-11 ... ITEM-1n
STRING-2 ITEM-21 ... ITEM-2m
. . .
STRING-n ITEM-n1 ... ITEM-np
PROP-1 VAL-1 ... PROP-j VAL-j)
where
SYMBOL-* is a symbol that defines a button in MEANING-ALIST;
STRING-* is a string that will appear in the dropdown menu;
ITEM-* is any format that define buttons or groups.
\(a dropdown group of first format is internally converted to the
second by making strings from the symbols and each symbol is the
item)
The same rules for obtaining property values, described above,
apply here. Properties are also distributed by groups. The
supported properties and their basic type are:
:type -- one of the symbols `radio' (default) or `toggle'; if
type is radio, only one of the itens may be active, and if
type is toggle, any item number of itens can be active.
:variable -- a symbol; it is the variable that govern the
dropdown button; every time the value should be an integer
starting from 1 (if type is radio) or a list of integers (if
type is toggle). The Nth set of buttons is :insert'ed.
:default -- determines the default value when the menu is
installed; it is ignored if a value was saved with custom; it
defaults to 1 if type is radio or nil if type is toggle. If
value is a integer and type is `toggle', value used is a list
with that integer.
:save -- one of the symbols nil (default), `offer' or
`always'; determined if it is possible for the user to save
the which menu itens are active, for a next session. If value
is `offer', a item (offering to save) is added to the
popup menu. If the value is `always', every time that a item
is selected, the variable is saved. If value is nil, variable
shall not be saved. If value is non-nil then `:variable' is
mandatory.
:title -- a string or nil; if a string, the popup menu will show
is as menu title; if nil, no title is shown.
:dropdown-help -- a string or nil; the help string of the
dropdown button.
:dropdown-image -- in Emacs, either a string or a vector of 4
strings; in XEmacs, either a string or a glyph or a list of at
least 1 and at most 6 strings or glyphs; defines the image
file displayed by the dropdown button; by default, it is the
string \"dropdown\".
:dropdown-append-command,
:dropdownprepend-command -- a form; append or prepend forms to
the command that shows the dropdown menu, allowing extra code
to run before or after the menu appears (remember that every
menu item clicked refresh the toolbar.)
:dropdown-enable -- a form; evaluated constantly by both editors
to determine if the dropdown button is active (enabled) or
not.
:dropdown-visible -- a form; in Emacs, it is evaluated
constantly to determine if the dropdown button is visible; in
XEmacs, this property is ignored.
:dropdown-toolbar -- in XEmacs, one of the symbols `default',
`opposite', `top', `bottom', `left' or `right'; ignored in
Emacs; in XEmacs, the toolbar where the dropdown button will
appear.
Also, if the symbol `dropdown' is associted in MEANING-ALIST
with some properties, these properties override (or add) with
higher precedence.
Special buttons
===============
If the symbol of a button is `:new-line', it is inserted
a (faked) return, and the next button will be displayed a next
line of buttons. The only property supported for this button is
`:insert'. This feature is available only in Emacs. In XEmacs,
this button is ignored.
I am following the elisp introduction. Very first chapters. Here is two exemples I've copied/pasted from the html book. I've evaluated both forms, and here I copie/paste the returned value and the output as a side effect from the *Messages* buffer (I don't know how to copy the mini-buffer content).
First form
(let ((zebra 'stripes)
(tiger 'fierce))
(message "One kind of animal has %s and another is %s."
zebra tiger))
Output from *Messages*
One kind of animal has stripes and another is fierce.
#("One kind of animal has stripes and another is fierce." 23 30 (fontified t))
Second form
(let ((birch 3)
pine
fir
(oak 'some))
(message "Here are %d variables with %s, %s and %s value."
birch pine fir oak))
Output from *Messages* is:
Here are 3 variables with nil, nil and some value.
"Here are 3 variables with nil, nil and some value."
Why is the first form returning a lambda value please?
What make the first form so special that the returned value won't be a character string?
The message return type is merely a string. In the *Messages* buffer you see both the return value of message (displayed in quotes in the echo area by the evaluating command), and the unquoted string displayed in the echo area by message itself. The first result was not a lambda, but a string with text properties.
In Emacs, the printable representation of string objects is normally their contents in double quotes, as in your second example. However, strings with text properties attached are printed in a more complex manner, as #("...string contents..." start end (property value...) ...). This extended syntax is for the Lisp reader to be able to recreate the properties when the string is read back from its textual representation. You can test this by evaluating M-: (insert #("foo" 0 3 (face (:foreground "yellow")))) in a fresh buffer - the text will come up yellow because the string itself contained instructions to paint it yellow. (To see this, you need to be in a fresh buffer obtained with, say, C-x b randomname RET — it won't work in syntax-highlighted buffers such as *scratch*, because they colorize the text themselves.)
So, the first string you printed came with properties attached, possibly as an artifact of copying and pasting, which is why it was printed as #("..." ...). The second string had no properties, and could be printed as a simple string.
The Emacs Lisp manual contains much more info on text properties.
I really want to use org-mode.
But, I want to use org-mode to understand structured documents that have already been written using different heading syntax,
e.g. using twiki's ---+
---+ H1
Top level
---++ H2
Nested
---+ H1 #2
Second top level
Or mediawiki like
= H1 =
Top level
== H2 ==
Nested
= H1 #2 =
Second top level
I'd like to have all of the goodness of org-mode folding, etc., just using these different heading styles.
Actually, worse that that:
I would like, say, the twiki or mediawaiki headings to take priority over org mode asterisk headings. But I would like to have both in use.
= H1 =
Top level
* this is a list
** nested
* list
** nested
== H2 ==
Nested
= H1 #2 =
Second top level
--+ What I have tried so far
I have been able to use outline mode to handle twiki,
for example via
---+ Emacs stuff
# try (defvar twiki-outline-regexp "---+\\++ \\|\\(\\(?: \\)+\\)[0-9*] ")
Local Variables: ***
outline-regexp: "^---\\++" ***
org-outline-regexp: "^---\\++" ***
End: ***
However, org-outline-regexp doesn't do hwat I would hope.
emacs' outline-mode's out-level function looks almost exactly like what I want.
(defvar outline-level 'outline-level
"*Function of no args to compute a header's nesting level in an outline.
It can assume point is at the beginning of a header line and that the match
data reflects the `outline-regexp'.")
i.e. instead of regexps, a generic function.
But I have not managed to make it work with org-mode. It looks like org-mode does not really use this, or, rather, has other stuff.
;; In Org buffers, the value of `outline-regexp' is that of
;; `org-outline-regexp'. The only function still directly relying on
;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
;; job when `orgstruct-mode' is active.
(defvar org-outline-regexp "\\*+ "
"Regexp to match Org headlines.")
(defconst org-outline-regexp-bol "^\\*+ "
"Regexp to match Org headlines.
This is similar to `org-outline-regexp' but additionally makes
sure that we are at the beginning of the line.")
(defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
"Matches an headline, putting stars and text into groups.
Stars are put in group 1 and the trimmed body in group 2.")
Failing this, well, the main thing that I want from org-mode is links, Asking another question here
How can I "linkify" a non-org-mode buffer in emacs
My frustration was simply that org-mode has different rules for what constitutes a new outline section than outline-mode does. It requires a space after the asterisks, so it doesn't work on my extensive collection of notes, which forgo those spaces.
I resolved that by removing the trailing space in the not-well-documented org-outline-regexp variable, which is used to initialize the buffer-local variable outline-regexp, and that seems to be working for me. E.g. (set-variable 'org-outline-regexp "\\*+")
As to your actual question, my guess is that other regexp's and code would have to change to handle radically different stuff like twiki or mediawiki headings.