In JESS, is there an equivalent command to Java's String.contains("x")? - jess

As the title says. I am writing an expert system in JESS and I need to read an user input to see if it contains "Windows", "Mac", or "Linux".

Try this:
(bind ?sentence "The quick brown fox")
(bind ?word "quick")
(if (str-index ?word ?sentence)
then
(printout t "contained" crlf)
else
(printout t "not contained" crlf))
The regexp function lets you use Java's java.util.regexp.Pattern patterns for more sophisticated matching.

Related

Doubts with some Vim commands and macros (The missing semester of your CS Education, parse XML to JSON with Vim macros)

I'm trying this exercise from The missing semester of your CS Education, Lecture 3: Editors (Vim):
(Advanced) Convert XML to JSON (example file) using Vim macros. Try to do this on your own, but you can look at the macros section above if you get stuck.
They give this solution:
Gdd, ggdd delete first and last lines
Macro to format a single element (register e)
Go to line with <name>
qe^r"f>s": "<ESC>f<C"<ESC>q
Macro to format a person (register p)
Go to line with <person>
qpS{<ESC>j#eA,<ESC>j#ejS},<ESC>q
Macro to format a person and go to the next person (register q)
Go to line with <person>
qq#pjq
Execute macro until end of file
999#q
Manually remove last , and add [ and ] delimiters
I don't understand commands like:
<C
^r"f>s":
S{<ESC>j#eA,<ESC>j#ejS}
Among others, could someone do a breakdown of the solution in order to understand the commands?
<C
This one is meaningless because you split the command at the wrong place. The correct place is between f< and C":
f< means "move the cursor to the next < on the line", see :help f.
C" means "cut the rest of the line and insert a "", see :help C.
^r"f>s":
^ means "move the cursor to the first printable character of the line", see :help ^.
r" means "replace the current character with a "", see :help r.
f> means "move the cursor to the next > on the fine", see :help f.
s": is actually s": ", meaning "replace the current character with ": ", see :help s.
S{<ESC>j#eA,<ESC>j#ejS}
S{ means "replace the whole line with a " at the correct indentation", see :help S.
<ESC> is the Escape key, see :help key-notation.
j means "go down one line", see :help j.
#e means "play back macro stored in register e", see :help #.
A, means "append a , at the end of the line", see :help A.
<ESC>, j, #e, j, and S} have already been explained.
All of that is pretty easy to piece out after a while (I rarely need to use Vim itself when answering Vim questions) but it sure is a lot to take in without the proper foundations.
That cryptic example is really a case of giving the reader a quasi-magical fish instead of teaching them how to fish. I am not sure what the people behind those "get rich quick" Vim articles and courses think they are achieving but questions like this one should make them pause a little.
The subject matter is much too complex to be covered in one lecture or in a short webpage so I would suggest you learn Vim properly instead of consuming context-free content like this.
If you haven't already, do $ vimtutor as many times as needed to get the basics right.
As instructed at the end of vimtutor, level up to the user manual :help user-manual. It's a hands-on tutorial that will guide you progressively through every feature, from basic to advanced. Go at your own pace and, most importantly, experiment along the way.
Keep an eye on anti-patterns and inefficient actions, find improvements, practice. Rinse. Repeat.

ELISP interactive function with both prefix argument and user input as optional arguments

In ELISP, the documentation for interactive codes mentions:
P -- Prefix arg in raw form. Does not do I/O.
...
s -- Arbitrary text, read in the minibuffer and returned as a string ... Prompt.
I presumed that I could write a function with an optional prefix argument, as in:
(defun some-function (&optional prefix)
(interactive "P")
...
)
or a function with user input, as in:
(defun some-function (user-argument)
(interactive "sProvide an argument: ")
...
)
but not both. Then I found the Org-mode function org-match-sparse-tree, which I can call with C-u C-c \, where the prefix argument restricts the match to open org-mode headings and I am still prompted for a match. The source code is below and I cannot find how the variable match is assigned:
(defun org-match-sparse-tree (&optional todo-only match)
"..."
(interactive "P")
(org-agenda-prepare-buffers (list (current-buffer)))
(let ((org--matcher-tags-todo-only todo-only))
(org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))
org--matcher-tags-todo-only)))
How does this function take both prefix argument and user input?
How does this function [interactively] take both prefix argument and user input?
It doesn't -- the match argument is not obtained, and is therefore nil. What you're seeing is the effect of the subsequent call to (org-make-tags-matcher match) with that nil value as the argument:
(defun org-make-tags-matcher (match)
"..."
(unless match
;; Get a new match request, with completion against the global
;; tags table and the local tags in current buffer.
(let ((org-last-tags-completion-table
(org-tag-add-to-alist
(org-get-buffer-tags)
(org-global-tags-completion-table))))
(setq match
(completing-read
"Match: "
'org-tags-completion-function nil nil nil 'org-tags-history))))
...)
Functions can take multiple interactive arguments, though.
See C-hf interactive
To pass several arguments to the command, concatenate the individual strings, separating them by newline characters.
The very first example in that help demonstrates this:
(defun foo (arg buf) "Doc string" (interactive "P\nbbuffer: ") .... )
This is elaborated upon at (elisp)Using Interactive -- up one level in the documentation you'd linked to:
It may be a string; its contents are a sequence of elements
separated by newlines, one for each argument(1). Each element
consists of a code character (*note Interactive Codes::) optionally
followed by a prompt (which some code characters use and some
ignore). Here is an example:
(interactive "P\nbFrobnicate buffer: ")
The code letter ‘P’ sets the command’s first argument to the raw
command prefix (*note Prefix Command Arguments::). ‘bFrobnicate
buffer: ’ prompts the user with ‘Frobnicate buffer: ’ to enter the
name of an existing buffer, which becomes the second and final
argument.
You should read that documentation fully, though -- there are more sophisticated things you can do, including writing arbitrary elisp to produce the interactive arguments (which may or may not involve prompting the user).

Replace non-ASCII characters with SGML entity codes with Emacs

I have a HTML file with a few non-ASCII characters, say encoded in UTF-8 or UTF-16. To save the file in ASCII, I would like to replace them with their (SGML/HTML/XML) entity codes. So for example, every ë should become ë and every ◊ should become ◊. How do I do that?
I use Emacs as an editor. I'm sure it has a function to do the replace, but I cannot find it. What am I missing? Or how do I implement it myself?
I searched high and low but it seems Emacs (or at least version 24.3.1) doesn't have such a function. Nor can I find it somewhere.
Based on a similar (but different) function I did find, I implemented it myself:
(require 'cl)
(defun html-nonascii-to-entities (string)
"Replace any non-ascii characters with HTML (actually SGML) entity codes."
(mapconcat
#'(lambda (char)
(case char
(t (if (and (<= 8 char)
(<= char 126))
(char-to-string char)
(format "&#%02d;" char)))))
string
""))
(defun html-nonascii-to-entities-region (region-begin region-end)
"Replace any non-ascii characters with HTML (actually SGML) entity codes."
(interactive "r")
(save-excursion
(let ((escaped (html-nonascii-to-entities (buffer-substring region-begin region-end))))
(delete-region region-begin region-end)
(goto-char region-begin)
(insert escaped))))
I'm no Elisp guru at all, but this works!
I also found find-next-unsafe-char to be of value.
Edit: an interactive version!
(defun query-replace-nonascii-with-entities ()
"Replace any non-ascii characters with HTML (actually SGML) entity codes."
(interactive)
(perform-replace "[^[:ascii:]]"
`((lambda (data count)
(format "&#%02d;" ; Hex: "&#x%x;"
(string-to-char (match-string 0)))))
t t nil))
There is a character class which includes exactly the ASCII character set. You can use a regexp that matches its complement to find occurrences of non-ASCII characters, and then replace them with their codes using elisp:
M-x replace-regexp RET
[^[:ascii:]] RET
\,(concat "&#" (number-to-string (string-to-char \&)) ";") RET
So when, for example, á is matched: \& is "á", string-to-char converts it to ?á (= the number 225), and number-to-string converts that to "225". Then, concat concatenates "&#", "225" and ";" to get "á", which replaces the original match.
Surround these commands with C-x ( and C-x ), and apply C-x C-k n and M-x insert-kbd-macro as usual to make a function out of them.
To see the elisp equivalent of calling this function interactively, run the command and then press C-x M-: (Repeat complex command).
A simpler version, which doesn't take into account the active region, could be:
(while (re-search-forward "[^[:ascii:]]" nil t)
(replace-match (concat "&#"
(number-to-string (string-to-char (match-string 0)))
";")))
(This uses the recommended way to do search + replace programmatically.)
I think you are looking for iso-iso2sgml

How to yank in a search in an emacs lisp function

(defun search-for-what-is-just-killed ()
(interactive)
(search-forward latestkillringvariable? nil t)
)
How to use "yank" in an emacs lisp function?
You can access directly the kill-ring list to access the latest kill:
(substring-no-properties (car kill-ring))
The substring-no-properties bit is important since text is kept in the kill ring with additional properties (like fontification specific to a particular mode and you'll probably want to strip those).

Clojure : missing namespace errors when running "use" at the REPL

I have a .clj file that starts like this :
(ns clojure_crawl.core)
(require '[clj-http.client :as client])
(use 'clojure.contrib.json)
Followed by several function definitions :
(defn f1 [] "" (+ 1 1))
(defn f2 [] "" (+ 2 2))
etc...
However, when I run the command "(use 'myfile.core :reload)"
Some of my functions , although visible at the REPL, cannot run do to "missing namespace" errors.
How do I add the dependencies so that the REPL can run any of the functions defined in my file ?
If your code is in "clojure_crawl/core.clj", its namespace should be clojure-crawl.core (note the hyphen). See http://clojure.org/libs
As Joost already said, you have to be careful with hyphens and underscores: wherever you use a hyphen in your namespace names, replace it with an underscore in the corresponding file/directory names (and vice versa).
Also, the use of the require and use functions in clj source files is discouraged. Instead, declare the libraries you need directly in the ns macro:
(ns clojure-crawl.core
(:require [clj-http.client :as client])
(:use clojure.contrib.json))
This also takes the burden of properly quoting the required namespaces from you.