I need a function for calculate age. I've just started to study CLIPS. I can calculate years but the answer is wrong.
(defrule calc-age
(person (date-of-birth ?dob))
=>
(bind ?age (- 2017 ?dob))
(printout t ?age))
CLIPS>
(deffunction age (?cy ; current year
?cm ; current month
?cd ; current day
?by ; birth year
?bm ; birth month
?bd) ; birth day
(bind ?age (- ?cy ?by))
(if (or (< ?cm ?bm)
(and (= ?cm ?bm) (< ?cd ?bd)))
then
(bind ?age (- ?age 1)))
?age)
CLIPS> (age 2017 4 6 2017 3 2)
0
CLIPS> (age 2017 4 6 2016 8 3)
0
CLIPS> (age 2017 4 6 2016 4 3)
1
CLIPS> (age 2017 4 6 2016 3 3)
1
CLIPS> (age 2017 4 6 2015 3 3)
2
CLIPS>
Related
For simplicity, this is a toy version of the actual problem: given a set of integers, find the longest sequence of consecutive numbers from that set.
I looked at CLIPS and other expert systems, and they seem ill-suited to express this kind of problem. Specifically, I don't see a list like data structure, which seems to be necessary to implement a solution. I'm looking for an example of implementation using logic programming.
One way:
CLIPS (6.4 2/9/21)
CLIPS>
(deffacts start
(set 1 9 2 10 4 3 11 13 5 14))
CLIPS>
(defrule combine-1
?f <- (set $?b ?n $?e)
=>
(retract ?f)
(assert (combine ?n))
(assert (set ?b ?e)))
CLIPS>
(defrule combine-2
?f1 <- (combine $?b ?j1)
?f2 <- (combine ?j2&=(+ ?j1 1) $?e)
=>
(retract ?f1 ?f2)
(assert (combine ?b ?j1 ?j2 ?e)))
CLIPS>
(defrule longest
(declare (salience -10))
(combine $?c)
(not (combine $?o&:(> (length$ ?o) (length$ ?c))))
=>
(println "Longest is " ?c))
CLIPS> (reset)
CLIPS> (run)
Longest is (1 2 3 4 5)
CLIPS> (facts)
f-21 (set)
f-22 (combine 13 14)
f-26 (combine 1 2 3 4 5)
f-28 (combine 9 10 11)
For a total of 4 facts.
CLIPS>
Another way:
CLIPS> (clear)
CLIPS>
(deffacts start
(set 1 9 2 10 4 3 11 13 5 14))
CLIPS>
(defrule sort
?f <- (set $?s)
(test (neq ?s (sort > ?s)))
=>
(retract ?f)
(assert (set (sort > ?s))))
CLIPS>
(deffunction consecutive ($?s)
(loop-for-count (?i (- (length$ ?s) 1))
(if (<> (+ (nth$ ?i ?s) 1) (nth$ (+ ?i 1) ?s))
then (return FALSE)))
(return TRUE))
CLIPS>
(defrule longest
(set $? $?s&:(consecutive $?s) $?)
(not (set $? $?s2&~$?s&:(consecutive $?s2)&:(> (length$ ?s2) (length$ ?s)) $?))
=>
(println "Longest is " ?s))
CLIPS> (reset)
CLIPS> (run)
Longest is (1 2 3 4 5)
CLIPS> (facts)
f-2 (set 1 2 3 4 5 9 10 11 13 14)
For a total of 1 fact.
CLIPS>
I'm doing an algorithm that randomizes a TSP (array of citys) based on 1 TSP.
(do ((i 0 (+ i 1)))
((= i n-population))
(setf (aref population i) (shuffle TSP 100))
)
And as far as I know im filling up i positions of the array population with (shuffle TSP 100) that is beeing called each iteration, but the algorithm is setting all array positions and not just i position.
[Note. An earlier version of this answer contained a mistake which would badly alter the statistics of the shuffling: please check below for the corrected version and a note as to what the problem was.]
Given your code, slightly elaborated to turn it into a function:
(defun fill-array-with-something (population n-population TSP)
(do ((i 0 (+ i 1)))
((= i n-population))
(setf (aref population i) (shuffle TSP 100))))
Then each element of population from 0 to (1- n-population) will be set to the result of (shuffle TSP 100). There are then two possibilities:
(shuffle TSP 100) returns a fresh object from each call;
(shuffle TSP 100) returns the same object – probably TSP – from each call.
In the first case, each element of the array will have a distinct value. In the second case, all elements below n-population will have the same value.
Without knowing what your shuffle function does, here is an example of one which will give the latter behaviour:
(defun shuffle (vec n)
;; shuffle pairs of elts of VEC, N times.
(loop with max = (length vec)
repeat n
do (rotatef (aref vec (random max))
(aref vec (random max)))
finally (return vec)))
And we can test this:
> (let ((pop (make-array 10))
(tsp (vector 0 1 2 3 4 5 6 7 8 9 )))
(fill-array-with-something pop (length pop) tsp)
pop)
#(#(2 8 7 1 3 9 5 4 0 6) #(2 8 7 1 3 9 5 4 0 6) #(2 8 7 1 3 9 5 4 0 6)
#(2 8 7 1 3 9 5 4 0 6) #(2 8 7 1 3 9 5 4 0 6) #(2 8 7 1 3 9 5 4 0 6)
#(2 8 7 1 3 9 5 4 0 6) #(2 8 7 1 3 9 5 4 0 6) #(2 8 7 1 3 9 5 4 0 6)
#(2 8 7 1 3 9 5 4 0 6))
As you can see all the elements are mysteriously the same thing, which is because my shuffle simply returned its first argument, having modified it in place.
You can check this by either explicitly checking the result of shuffle, or by, for instance, using *print-circle* to see the sharing. The latter approach is pretty neat:
> (let ((*print-circle* t)
(pop (make-array 10))
(tsp (vector 0 1 2 3 4 5 6 7 8 9 )))
(fill-array-with-something pop (length pop) tsp)
(print pop)
(values))
#(#1=#(4 6 7 0 1 2 5 9 3 8) #1# #1# #1# #1# #1# #1# #1# #1# #1#)
And now it's immediately apparent what the problem is.
The solution is to make sure either that shuffle returns a fresh object, or to copy its result. With my shuffle this can be done like this:
(defun fill-array-with-something (population n-population tsp)
(do ((i 0 (+ i 1)))
((= i n-population))
(setf (aref population i) (shuffle (copy-seq TSP) 100))))
Note that a previous version of this answer had (copy-seq (shuffle TSP 100)): with my version of shuffle this is a serious mistake, as it means that the elements in population are related to each other but get increasingly shuffled as you go along. With (shuffle (copy-seq TSP) 100) each element gets the same amount of shuffling, independently.
And now
> (let ((*print-circle* t)
(pop (make-array 10))
(tsp (vector 0 1 2 3 4 5 6 7 8 9 )))
(fill-array-with-something pop (length pop) tsp)
(print pop)
(values))
#(#(8 3 4 1 6 9 2 5 0 7) #(8 6 5 1 3 0 4 2 9 7) #(5 0 4 7 1 6 9 3 2 8)
#(3 0 7 6 2 9 4 5 1 8) #(8 2 5 1 7 3 9 0 4 6) #(0 5 6 3 8 7 2 1 4 9)
#(4 1 3 7 8 0 5 2 9 6) #(6 9 1 5 0 7 4 2 3 8) #(2 7 5 8 0 9 6 3 4 1)
#(5 4 8 9 6 7 2 0 1 3))
I suspect that the problem is in OP function SHUFFLE which has not yet been shared; my suspicion is that SHUFFLE is shuffling the *TSP* array itself in place instead of creating a shuffled copy of that array. The POPULATION values are then all referencing the same shuffled *TSP* array.
To solve this problem, SHUFFLE should return a shuffled array instead of shuffling the array in place. Here is a function that performs a Fisher-Yates shuffle on a vector:
(defun shuffle-vector (vect)
"Takes a vector argument VECT and returns a shuffled vector."
(let ((result (make-array (length vect) :fill-pointer 0)))
(labels ((shuffle (v)
(if (zerop (length v))
result
(let* ((i (random (length v)))
(x (elt v i)))
(vector-push x result)
(shuffle (concatenate 'vector
(subseq v 0 i)
(subseq v (1+ i))))))))
(shuffle vect))))
Testing in the REPL:
CL-USER> (defvar *TSP* #("Village" "Town" "City" "Metropolis" "Megalopolis"))
*TSP*
CL-USER> (defvar *n-population* 5)
*N-POPULATION*
CL-USER> (defvar *population* (make-array *n-population*))
*POPULATION*
CL-USER> (dotimes (i *n-population*)
(setf (aref *population* i) (shuffle-vector *TSP*)))
NIL
CL-USER> *population*
#(#("Megalopolis" "City" "Metropolis" "Town" "Village")
#("Megalopolis" "Metropolis" "Town" "City" "Village")
#("City" "Megalopolis" "Town" "Village" "Metropolis")
#("City" "Megalopolis" "Village" "Metropolis" "Town")
#("Megalopolis" "Town" "Metropolis" "City" "Village"))
GOAL: The goal of this thread is to create a mathematical formula to replace the long-hand solution by #lawlist in the function lawlist-target-year-function (below).
NOTE: The solution to this thread is somewhat similar, but will nevertheless be different, than the algorithm written by #AShelly in a related thread: https://stackoverflow.com/a/21709710/2112489
STORY PROBLEM
There now exists a 12-month calendar in Emacs that scrolls forwards and backwards one month (or more) at a time. A helper function called lawlist-target-year-function is used by sevaral holiday functions to place an overlay on each holiday.
A full working draft of the 12-month scrolling calendar (including the long-hand solution) may be found here -- [the Github source code has been revised to include the concise algorithm solution by #legoscia]:
https://github.com/lawlist/calendar-yearly-scroll-by-month/blob/master/lawlist-cal.el
LEGEND:
displayed-month (numbers 1 through 12) is the month that appears in the upper left-hand corner of the buffer, and this changes as the 12-month calendar is scrolled forwards or backwards.
The target-month (numbers 1 through 12) is the month that contains the holiday that will be marked with an overlay. There are three (3) possible x axis coordinates (i.e., column 1, column 2, or column 3). There are four (4) possible y axis coordinates (i.e., row 1, row 2, row 3, or row 4). [Citation to x / y coordinates: http://www.mathsisfun.com/data/cartesian-coordinates.html ]
The displayed-year is the year that appears in the upper left-hand corner of the buffer, and this changes as the 12-month calendar is scrolled forwards or backwards.
The target year is the year of the target-month.
EXAMPLE:
When displayed-month is January (i.e., 1), then the year is the same for all target months.
When displayed-month is February (i.e., 2):
(if (memq target-month `(2 3 4 5 6 7 8 9 10 11 12))
displayed-year
(+ displayed-year 1))
When displayed-month is March (i.e., 3):
(if (memq target-month `(3 4 5 6 7 8 9 10 11 12))
displayed-year
(+ displayed-year 1))
When displayed-month is April (i.e., 4):
(if (memq target-month `(4 5 6 7 8 9 10 11 12))
displayed-year
(+ displayed-year 1))
When displayed-month is May (i.e., 5)
(if (memq target-month `(5 6 7 8 9 10 11 12))
displayed-year
(+ displayed-year 1))
When displayed-month is June (i.e., 6):
(if (memq target-month `(6 7 8 9 10 11 12))
displayed-year
(+ displayed-year 1))
When displayed-month is July (i.e., 7):
(if (memq target-month `(7 8 9 10 11 12))
displayed-year
(+ displayed-year 1))
When displayed-month is August (i.e, 8):
(if (memq target-month `(8 9 10 11 12))
displayed-year
(+ displayed-year 1))
When displayed-month is September (i.e., 9):
(if (memq target-month `(9 10 11 12))
displayed-year
(+ displayed-year 1))
When displayed-month is October (i.e., 10):
(if (memq target-month `(10 11 12))
displayed-year
(+ displayed-year 1))
When displayed-month is November (i.e., 11):
(if (memq target-month `(11 12))
displayed-year
(+ displayed-year 1))
When displayed-month is December (i.e., 12):
(if (memq target-month `(12))
displayed-year
(+ displayed-year 1))
The 12-month calendar looks like the following as the layout scrolls forward one month at a time:
;; 1 2 3
;; 4 5 6
;; 7 8 9
;; 10 11 12
;; 2 3 4
;; 5 6 7
;; 8 9 10
;; 11 12 1
;; 3 4 5
;; 6 7 8
;; 9 10 11
;; 12 1 2
;; 4 5 6
;; 7 8 9
;; 10 11 12
;; 1 2 3
;; 5 6 7
;; 8 9 10
;; 11 12 1
;; 2 3 4
;; 6 7 8
;; 9 10 11
;; 12 1 2
;; 3 4 5
;; 7 8 9
;; 10 11 12
;; 1 2 3
;; 4 5 6
;; 8 9 10
;; 11 12 1
;; 2 3 4
;; 5 6 7
;; 9 10 11
;; 12 1 2
;; 3 4 5
;; 6 7 8
;; 10 11 12
;; 1 2 3
;; 4 5 6
;; 7 8 9
;; 11 12 1
;; 2 3 4
;; 5 6 7
;; 8 9 10
;; 12 1 2
;; 3 4 5
;; 6 7 8
;; 9 10 11
The long-hand solution by #lawlist is as follows:
(defun lawlist-target-year-function (target-month)
(cond
;; 1 2 3
;; 4 5 6
;; 7 8 9
;; 10 11 12
((eq displayed-month 1)
displayed-year)
;; 2 3 4
;; 5 6 7
;; 8 9 10
;; 11 12 1
((eq displayed-month 2)
(if (memq target-month `(2 3 4 5 6 7 8 9 10 11 12))
displayed-year
(+ displayed-year 1)))
;; 3 4 5
;; 6 7 8
;; 9 10 11
;; 12 1 2
((eq displayed-month 3)
(if (memq target-month `(3 4 5 6 7 8 9 10 11 12))
displayed-year
(+ displayed-year 1)))
;; 4 5 6
;; 7 8 9
;; 10 11 12
;; 1 2 3
((eq displayed-month 4)
(if (memq target-month `(4 5 6 7 8 9 10 11 12))
displayed-year
(+ displayed-year 1)))
;; 5 6 7
;; 8 9 10
;; 11 12 1
;; 2 3 4
((eq displayed-month 5)
(if (memq target-month `(5 6 7 8 9 10 11 12))
displayed-year
(+ displayed-year 1)))
;; 6 7 8
;; 9 10 11
;; 12 1 2
;; 3 4 5
((eq displayed-month 6)
(if (memq target-month `(6 7 8 9 10 11 12))
displayed-year
(+ displayed-year 1)))
;; 7 8 9
;; 10 11 12
;; 1 2 3
;; 4 5 6
((eq displayed-month 7)
(if (memq target-month `(7 8 9 10 11 12))
displayed-year
(+ displayed-year 1)))
;; 8 9 10
;; 11 12 1
;; 2 3 4
;; 5 6 7
((eq displayed-month 8)
(if (memq target-month `(8 9 10 11 12))
displayed-year
(+ displayed-year 1)))
;; 9 10 11
;; 12 1 2
;; 3 4 5
;; 6 7 8
((eq displayed-month 9)
(if (memq target-month `(9 10 11 12))
displayed-year
(+ displayed-year 1)))
;; 10 11 12
;; 1 2 3
;; 4 5 6
;; 7 8 9
((eq displayed-month 10)
(if (memq target-month `(10 11 12))
displayed-year
(+ displayed-year 1)))
;; 11 12 1
;; 2 3 4
;; 5 6 7
;; 8 9 10
((eq displayed-month 11)
(if (memq target-month `(11 12))
displayed-year
(+ displayed-year 1)))
;; 12 1 2
;; 3 4 5
;; 6 7 8
;; 9 10 11
((eq displayed-month 12)
(if (memq target-month `(12))
displayed-year
(+ displayed-year 1))) ))
Would this work?
(defun lawlist-target-year-function (target-month)
(if (>= target-month displayed-month)
displayed-year
(1+ displayed-year)))
I use emacs diary.
As I append both future plans and daily review in the diary file,
the entries in the file results in not following chronological order.
When I review the diary file at some occasion, I would like to have these entries sorted.
Is there any commands or lisp that I can use to modify the diary file so that the entries get sorted in the chronological order?
A long time ago, I wrote myself a sort function:
(defun diary-sort-diary-keyfun nil
"Key function to order diary entries.
Entries sort in the groups: (days, anniversaries, cyclics, blocks, dates), with any unrecognised
forms before the groups.
Within each group, entries are in ascending date order.
You can prefix entries with `#' to comment them out without affecting sort order.
Prefixing with `&' also does not affect sort order."
(let ((number "\\s-+\\([0-9]+\\)")
(months '("Dec" "Nov" "Oct" "Sep" "Aug" "Jul" "Jun" "May" "Apr" "Mar" "Feb" "Jan"))
(days '("Saturday" "Friday" "Thursday" "Wednesday" "Tuesday" "Monday" "Sunday")))
(skip-chars-forward "#&")
(cond
((looking-at (concat "%%(diary-block" number number number number number number ")\\(.*\\)"))
(format "50%04d%02d%02d%04d%02d%02d%s"
(string-to-number (match-string 3))
(string-to-number (match-string 2)) (string-to-number (match-string 1))
(string-to-number (match-string 6))
(string-to-number (match-string 5)) (string-to-number (match-string 4))
(match-string 7)))
((looking-at (concat "%%(diary-cyclic" number number number number ")\\(.*\\)"))
(format "40%04d%02d%02d%05d%s"
(string-to-number (match-string 4))
(string-to-number (match-string 3)) (string-to-number (match-string 2))
(string-to-number (match-string 1))
(match-string 5)))
((looking-at (concat "%%(diary-anniversary" number number number ")\\(.*\\)"))
(format "30%04d%02d%02d%s"
(string-to-number (match-string 3))
(string-to-number (match-string 2)) (string-to-number (match-string 1))
(match-string 4)))
((looking-at "%%(\\(.*\\)") ; after all othe "%%()" rules
(format "20(%s" (match-string 1)))
((looking-at (concat "\\(" (mapconcat 'identity days "\\|") "\\)"
"\\( *[0-2 ][0-9]:[0-9][0-9]\\)?\\(.*\\)"))
(format "10%d%6s%s"
(length (member (match-string 1) days))
(or (match-string 2) "")
(match-string 3)))
((looking-at (concat "\\([0-9]+\\)\\s-+" "\\(" (mapconcat 'identity months "\\|") "\\)"
number "\\s-+\\([0-2 ][0-9]:[0-9][0-9]\\)?\\(.*\\)"))
(format "60%04d%02d%02d%6s%s"
(string-to-number (match-string 3))
(length (member (match-string 2) months))
(string-to-number (match-string 1))
(or (match-string 4) "")
(match-string 5)))
((looking-at (concat "\\(" (mapconcat 'identity months "\\|") "\\)"
number "," number "\\( *[0-2 ][0-9]:[0-9][0-9]\\)?\\(.*\\)"))
(format "60%04d%02d%02d%6s%s"
(string-to-number (match-string 3))
(length (member (match-string 1) months))
(string-to-number (match-string 2))
(or (match-string 4) "")
(match-string 5)))
((looking-at "[ \t\r]*$") ; blank line
(concat "99" (match-string 0)))
((looking-at ".*") ; last rule
(concat "00" (match-string 0))))))
(defun diary-sort-diary-file nil
"Sort the diary entries.
See `diary-sort-diary-keyfun' for the collation sequence."
(interactive "*")
;; sort-order:
;; randoms, days, anniversaries, cyclics, blocks, dates
(goto-char (point-min))
(let* ((locals-start (and (re-search-forward "\\(\n.*\\)Local variables:\\(.*\n\\)" nil t)
(match-beginning 0)))
(locals-end (and locals-start
(search-forward (concat (match-string 1) "End:" (match-string 2)) nil t)
(match-end 0)))
(locals (and locals-start locals-end
(buffer-substring-no-properties locals-start locals-end))))
(when locals
(delete-region locals-start locals-end))
(and (> (point-max) 1)
(/= (char-after (1- (point-max))) ?\n)
(goto-char (point-max))
(insert ?\n))
(goto-char (point-min))
(sort-subr nil 'forward-line 'end-of-line 'diary-sort-diary-keyfun)
(goto-char (point-max))
(insert "\n")
(delete-blank-lines)
(when locals
(insert locals))))
It's likely that I've assumed European date order, but if you prefer a different order, it shouldn't be hard to adapt it accordingly.
The way it works is that the keyfun returns a string that begins with two digits for the entry type (00 for unknowns, 10 for day-of-week entries, up to 60 for non-repeating dates), followed by a big-endian representation of the entry, like ISO 8601.
The interactive function, diary-sort-diary-file, then uses this keyfun. It saves any Local variables section, reinstating it at the end of the file (this is nice when you've inserted entries from calendar, as they get appended). If you have any LocalWords lines (for Ispell), then you could use similar code to keep that intact, or you could adapt the keyfun to place them last.
Sample results (somewhat censored):
&Sep 12 Xxxxxxx
&Monday 21:00 Xxxxxxx
#&Thursday Xxxxxxx
%%(diary-float t 0 2) Xxxxxxx
%%(diary-float t 6 1) Xxxxxxx
&%%(diary-phases-of-moon)
&%%(diary-anniversary 26 6 1952) Xxxxxxx
%%(diary-anniversary 1 6 1972) Xxxxxxx
&%%(diary-anniversary 15 3 1975) Xxxxxxx
&%%(diary-anniversary 7 2 1976) Xxxxxxx
%%(diary-anniversary 4 5 1977) International Star Wars day! :-)
&%%(diary-anniversary 13 8 1978) Xxxxxxx
&%%(diary-anniversary 26 8 1980) Xxxxxxx
&%%(diary-anniversary 16 10 1980) Xxxxxxx
&%%(diary-anniversary 15 3 2010) Xxxxxxx
&%%(diary-cyclic 1000 1 6 1972) Xxxxxxx
&%%(diary-cyclic 1000 13 8 1978) Xxxxxxx
&%%(diary-cyclic 1000 26 8 1980) Xxxxxxx
%%(diary-cyclic 1000 9 9 2013) Xxxxxxx
%%(diary-block 22 3 2013 24 3 2013) Xxxxxxx
%%(diary-block 6 4 2013 7 4 2013) Xxxxxxx
&%%(diary-block 20 4 2013 21 4 2013) Xxxxxxx
%%(diary-block 27 4 2013 28 4 2013) Xxxxxxx
%%(diary-block 18 5 2013 19 5 2013) Xxxxxxx
%%(diary-block 1 6 2013 2 6 2013) Xxxxxxx
%%(diary-block 1 6 2013 2 6 2013) Xxxxxxx
%%(diary-block 15 6 2013 16 6 2013) Xxxxxxx
%%(diary-block 22 6 2013 23 6 2013) Xxxxxxx
%%(diary-block 22 6 2013 30 6 2013) Xxxxxxx
%%(diary-block 6 7 2013 7 7 2013) Xxxxxxx
%%(diary-block 20 7 2013 24 7 2013) Xxxxxxx
%%(diary-block 9 8 2013 11 8 2013) Xxxxxxx
%%(diary-block 23 4 2016 24 4 2016) Xxxxxxx
%%(diary-block 13 8 2016 21 8 2016) Xxxxxxx
%%(diary-block 26 8 2016 28 8 2016) Xxxxxxx
22 Jun 2009 11:30 Xxxxxxx
30 Jun 2009 13:00 Xxxxxxx
&22 Jul 2009 Xxxxxxx
25 Jul 2009 Xxxxxxx
&14 Aug 2009 17:30 Xxxxxxx
&17 Aug 2009 Xxxxxxx
13 Mar 2010 Xxxxxxx
23 Mar 2010 10:50 Xxxxxxx
&17 Jan 2013 14:00 Xxxxxxx
1 Feb 2013 Xxxxxxx
&8 Feb 2013 16:00 Xxxxxxx
12 Feb 2013 18:30 Xxxxxxx
19 Feb 2013 18:00 Xxxxxxx
&12 Mar 2013 10:00 Xxxxxxx
16 Mar 2013 Xxxxxxx
&19 Mar 2013 13:50 Xxxxxxx
20 Mar 2016 Xxxxxxx
2 Apr 2016 Xxxxxxx
18 Jun 2016 Xxxxxxx
17 Jul 2016 Xxxxxxx
12 Nov 2016 Xxxxxxx
28 Mar 2017 Xxxxxxx
If you like to keep your diary file organised, you might also like these:
(define-generic-mode 'diary-generic-mode
'(?#)
nil ;; keywords
'(("^&.*$" (0 font-lock-comment-face))
("^&?\\(%%(diary-[a-z]+[^)]*)\\)" (1 font-lock-type-face t))
("^&?[ \t\n]*\\(\\([0-9*]+/[0-9*]+\\(/[0-9*]+\\)?\\|\\(Jan\\(uary\\)?\\|Feb\\(ruary\\)?\\|Mar\\(ch\\)?\\|Apr\\(il\\)?\\|May\\|June?\\|July?\\|Aug\\(ust\\)?\\|Sep\\(tember\\)?\\|Oct\\(ober\\)?\\|Nov\\(ember\\)?\\|Dec\\(ember\\)?\\|\\*\\)\\.?\\s-*[0-3]?[0-9]\\|\\(\\(Mon\\|Tues?\\|Wed\\(nes\\)?\\|Thu\\(rs\\)?\\|Fri\\|Sat\\(ur\\)?\\|Sun\\)\\(day\\)?\\)\\(\\s-[0-9]+\\)?\\)\\(,\\s-+\\(19\\|20\\)[0-9][0-9]\\)?\\s.?\\)" (1 font-lock-type-face t))
("^&?\\s-*\\([0-3]?[0-9]\\s-+\\(Jan\\(uary\\)?\\|Feb\\(ruary\\)?\\|Mar\\(ch\\)?\\|Apr\\(il\\)?\\|May\\|June?\\|July?\\|Aug\\(ust\\)?\\|Sep\\(tember\\)?\\|Oct\\(ober\\)?\\|Nov\\(ember\\)?\\|Dec\\(ember\\)?\\)\\s-+[0-9]+\\)" (1 font-lock-type-face t))
("[0-2]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?\\>\\s.?" (0 font-lock-keyword-face t))) ;; font-lock
'("/diary") ;; auto-mode
'(diary-mode-setup) ;; function
"Mode for diary file.")
(defun diary-mode-setup nil
(set (make-local-variable 'align-rules-list)
'((date (regexp . "^&?\\([1-3]?[0-9]\\)\\(\\s-+\\)\\(+Jan\\|Feb\\|Ma[ry]\\|Apr\\|Ju[nl]\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\)\\(\\s-+\\)[12][90-4][0-9][0-9]")
(group 2 4)
(justify . t))
(block (regexp . "^&?%%(diary-block\\(\\s-+[0-9]+\\)\\(\\s-+[0-9]+\\)\\(\\s-+[0-9]+\\)\\(\\s-+[0-9]+\\)\\(\\s-+[0-9]+\\)\\(\\s-+[0-9]+\\))")
(group 1 2 3 4 5 6)
(justify . t))
(cyclic (regexp . "^&?%%(diary-cyclic\\(\\s-+[0-9]+\\)\\(\\s-+[0-9]+\\)\\(\\s-+[0-9]+\\)\\(\\s-+[0-9]+\\))")
(group 1 2 3 4)
(justify . t))))
(set (make-local-variable 'require-final-newline) t)
(set (make-local-variable 'version-control) 'never))
In icalendar.el, function icalendar--datetime-to-iso-date, change format from "%d%s%d%s%d" to "%04d%s%02d%s%02d".
Add this to your initialization:
(setq
diary-date-forms diary-iso-date-forms
calendar-date-style 'iso
)
Edit old entries in your diary-file to the ISO standard, formatted as above, i.e YYYY/MM/DD. (The ISO standard is really YYYY-MM-DD).
Now further add this to your initialization:
(defun sort--diary (diary-filename)
(with-current-buffer
(set-buffer (find-file-noselect (expand-file-name diary-filename)))
(goto-char (point-min))
(while (search-forward "\C-j " nil t)
(replace-match "^j "))
(sort-lines nil (point-min) (point-max))
(goto-char (point-min))
(while (search-forward "^j" nil t)
(replace-match "\C-j"))
(save-buffer)))
(defvar sort--diary-filename (expand-file-name diary-file)
"History for sort--diary diary-filename")
(defun sort-diary (diary-filename)
"Sort diary file. Requires dates to use ISO standard"
(interactive (list (read-from-minibuffer
"diary file name: "
(car sort--diary-filename)
nil nil 'sort--diary-filename)))
(sort--diary diary-filename))
Now you can sort your diary by calling sort-diary.
GOAL: The goal of this thread is to create two (2) mathematical formulas to replace the long-hand solution by #lawlist in the function lawlist-calendar-cursor-to-visible-date (below).
STORY PROBLEM
There now exists a 12-month calendar in Emacs that scrolls forwards and backwards one month (or more) at a time. The function lawlist-calendar-cursor-to-visible-date is used to mark dates with overlays for designated events (e.g., birthdays, holidays, appointments, etc.); or, to simply move the cursor to a particular date. #lawlist has devised a solution by long-hand, which does not entirely use mathematical equations to calculate the cursor position for each of the 365 days that are displayed. It may be possible to create two (2) concise algorithms to replace the long-hand solution.
A working draft of the 12-month scrolling calendar (without the long-hand solution) may be found here:
https://stackoverflow.com/a/21409154/2112489
LEGEND:
displayed-month (numbers 1 through 12) is the month that appears in the upper left-hand corner of the buffer, and this changes as the 12-month calendar is scrolled forwards or backwards.
The target month (numbers 1 through 12) is the month that we need to locate with assistance from the two mathematical formulas -- its location varies depending upon the date being marked (e.g., birthday, holiday, appointment), and depending upon the displayed-month in the upper left-hand corner of the buffer. The target month can be in any one of 12 possible positions. There are three (3) possible x axis coordinates (i.e., 6, 31, or 56). There are four (4) possible y axis coordinates (i.e., 0, 9, 18 or 27). [Citation to x / y coordinates: http://www.mathsisfun.com/data/cartesian-coordinates.html ]
A row is defined as 3 months horizontally.
A column is defined as 4 months vertically.
The first forumula must equal 0, 9, 18 or 27 depending upon whether the point is on row 1, 2, 3 or 4 -- i.e., from top to bottom.
The second forumula must equal 6, 31, or 56 depending upon whether the point is on column 1, 2 or 3 -- i.e., from left to right.
EXAMPLE:
If displayed-month is January (i.e., 1) and the target month is August (i.e., 8), then row equals 18 and column equals 31.
If displayed-month is February (i.e., 2) and the target month is August (i.e., 8), then row equals 18 and column equals 6.
If displayed-month is March (i.e., 3) and the target month is August (i.e., 8), then row equals 9 and column equals 56.
If displayed-month is April (i.e., 4) and target month is August (i.e., 8), then row equals 9 and column equals 31.
If displayed-month is May (i.e., 5) and the target month is August (i.e., 8), then row equals 9 and column equals 6.
The 12-month calendar looks like the following as the layout scrolls forward one month at a time:
;; 1 2 3
;; 4 5 6
;; 7 8 9
;; 10 11 12
;; 2 3 4
;; 5 6 7
;; 8 9 10
;; 11 12 1
;; 3 4 5
;; 6 7 8
;; 9 10 11
;; 12 1 2
;; 4 5 6
;; 7 8 9
;; 10 11 12
;; 1 2 3
;; 5 6 7
;; 8 9 10
;; 11 12 1
;; 2 3 4
;; 6 7 8
;; 9 10 11
;; 12 1 2
;; 3 4 5
;; 7 8 9
;; 10 11 12
;; 1 2 3
;; 4 5 6
;; 8 9 10
;; 11 12 1
;; 2 3 4
;; 5 6 7
;; 9 10 11
;; 12 1 2
;; 3 4 5
;; 6 7 8
;; 10 11 12
;; 1 2 3
;; 4 5 6
;; 7 8 9
;; 11 12 1
;; 2 3 4
;; 5 6 7
;; 8 9 10
;; 12 1 2
;; 3 4 5
;; 6 7 8
;; 9 10 11
The long-hand solution by #lawlist is as follows:
(defun lawlist-calendar-cursor-to-visible-date (date)
"Move the cursor to DATE that is on the screen."
(let* (
(month (calendar-extract-month date))
(day (calendar-extract-day date))
(year (calendar-extract-year date))
(first-of-month-weekday (calendar-day-of-week (list month 1 year))))
(goto-line
(+ 3
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(cond
;; 1 2 3
;; 4 5 6
;; 7 8 9
;; 10 11 12
((and
(eq displayed-month 1)
(memq month `(1 2 3)))
0)
((and
(eq displayed-month 1)
(memq month `(4 5 6)))
9)
((and
(eq displayed-month 1)
(memq month `(7 8 9)))
18)
((and
(eq displayed-month 1)
(memq month `(10 11 12)))
27)
;; 2 3 4
;; 5 6 7
;; 8 9 10
;; 11 12 1
((and
(eq displayed-month 2)
(memq month `(2 3 4)))
0)
((and
(eq displayed-month 2)
(memq month `(5 6 7)))
9)
((and
(eq displayed-month 2)
(memq month `(8 9 10)))
18)
((and
(eq displayed-month 2)
(memq month `(11 12 1)))
27)
;; 3 4 5
;; 6 7 8
;; 9 10 11
;; 12 1 2
((and
(eq displayed-month 3)
(memq month `(3 4 5)))
0)
((and
(eq displayed-month 3)
(memq month `(6 7 8)))
9)
((and
(eq displayed-month 3)
(memq month `(9 10 11)))
18)
((and
(eq displayed-month 3)
(memq month `(12 1 2)))
27)
;; 4 5 6
;; 7 8 9
;; 10 11 12
;; 1 2 3
((and
(eq displayed-month 4)
(memq month `(4 5 6)))
0)
((and
(eq displayed-month 4)
(memq month `(7 8 9)))
9)
((and
(eq displayed-month 4)
(memq month `(10 11 12)))
18)
((and
(eq displayed-month 4)
(memq month `(1 2 3)))
27)
;; 5 6 7
;; 8 9 10
;; 11 12 1
;; 2 3 4
((and
(eq displayed-month 5)
(memq month `(5 6 7)))
0)
((and
(eq displayed-month 5)
(memq month `(8 9 10)))
9)
((and
(eq displayed-month 5)
(memq month `(11 12 1)))
18)
((and
(eq displayed-month 5)
(memq month `(2 3 4)))
27)
;; 6 7 8
;; 9 10 11
;; 12 1 2
;; 3 4 5
((and
(eq displayed-month 6)
(memq month `(6 7 8)))
0)
((and
(eq displayed-month 6)
(memq month `(9 10 11)))
9)
((and
(eq displayed-month 6)
(memq month `(12 1 2)))
18)
((and
(eq displayed-month 6)
(memq month `(3 4 5)))
27)
;; 7 8 9
;; 10 11 12
;; 1 2 3
;; 4 5 6
((and
(eq displayed-month 7)
(memq month `(7 8 9)))
0)
((and
(eq displayed-month 7)
(memq month `(10 11 12)))
9)
((and
(eq displayed-month 7)
(memq month `(1 2 3)))
18)
((and
(eq displayed-month 7)
(memq month `(4 5 6)))
27)
;; 8 9 10
;; 11 12 1
;; 2 3 4
;; 5 6 7
((and
(eq displayed-month 8)
(memq month `(8 9 10)))
0)
((and
(eq displayed-month 8)
(memq month `(11 12 1)))
9)
((and
(eq displayed-month 8)
(memq month `(2 3 4)))
18)
((and
(eq displayed-month 8)
(memq month `(5 6 7)))
27)
;; 9 10 11
;; 12 1 2
;; 3 4 5
;; 6 7 8
((and
(eq displayed-month 9)
(memq month `(9 10 11)))
0)
((and
(eq displayed-month 9)
(memq month `(12 1 2)))
9)
((and
(eq displayed-month 9)
(memq month `(3 4 5)))
18)
((and
(eq displayed-month 9)
(memq month `(6 7 8)))
27)
;; 10 11 12
;; 1 2 3
;; 4 5 6
;; 7 8 9
((and
(eq displayed-month 10)
(memq month `(10 11 12)))
0)
((and
(eq displayed-month 10)
(memq month `(1 2 3)))
9)
((and
(eq displayed-month 10)
(memq month `(4 5 6)))
18)
((and
(eq displayed-month 10)
(memq month `(7 8 9)))
27)
;; 11 12 1
;; 2 3 4
;; 5 6 7
;; 8 9 10
((and
(eq displayed-month 11)
(memq month `(11 12 1)))
0)
((and
(eq displayed-month 11)
(memq month `(2 3 4)))
9)
((and
(eq displayed-month 11)
(memq month `(5 6 7)))
18)
((and
(eq displayed-month 11)
(memq month `(8 9 10)))
27)
;; 12 1 2
;; 3 4 5
;; 6 7 8
;; 9 10 11
((and
(eq displayed-month 12)
(memq month `(12 1 2)))
0)
((and
(eq displayed-month 12)
(memq month `(3 4 5)))
9)
((and
(eq displayed-month 12)
(memq month `(6 7 8)))
18)
((and
(eq displayed-month 12)
(memq month `(9 10 11)))
27) )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(/ (+ day -1
(mod
(- (calendar-day-of-week (list month 1 year)) calendar-week-start-day)
7))
7)))
(move-to-column
(+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(cond
;; 1 2 3
;; 4 5 6
;; 7 8 9
;; 10 11 12
((and
(eq displayed-month 1)
(memq month `(1 4 7 10)))
6)
((and
(eq displayed-month 1)
(memq month `(2 5 8 11)))
31)
((and
(eq displayed-month 1)
(memq month `(3 6 9 12)))
56)
;; 2 3 4
;; 5 6 7
;; 8 9 10
;; 11 12 1
((and
(eq displayed-month 2)
(memq month `(2 5 8 11)))
6)
((and
(eq displayed-month 2)
(memq month `(3 6 9 12)))
31)
((and
(eq displayed-month 2)
(memq month `(4 7 10 1)))
56)
;; 3 4 5
;; 6 7 8
;; 9 10 11
;; 12 1 2
((and
(eq displayed-month 3)
(memq month `(3 6 9 12)))
6)
((and
(eq displayed-month 3)
(memq month `(4 7 10 1)))
31)
((and
(eq displayed-month 3)
(memq month `(5 8 11 2)))
56)
;; 4 5 6
;; 7 8 9
;; 10 11 12
;; 1 2 3
((and
(eq displayed-month 4)
(memq month `(4 7 10 1)))
6)
((and
(eq displayed-month 4)
(memq month `(5 8 11 2)))
31)
((and
(eq displayed-month 4)
(memq month `(6 9 12 3)))
56)
;; 5 6 7
;; 8 9 10
;; 11 12 1
;; 2 3 4
((and
(eq displayed-month 5)
(memq month `(5 8 11 2)))
6)
((and
(eq displayed-month 5)
(memq month `(6 9 12 3)))
31)
((and
(eq displayed-month 5)
(memq month `(7 10 1 4)))
56)
;; 6 7 8
;; 9 10 11
;; 12 1 2
;; 3 4 5
((and
(eq displayed-month 6)
(memq month `(6 9 12 3)))
6)
((and
(eq displayed-month 6)
(memq month `(7 10 1 4)))
31)
((and
(eq displayed-month 6)
(memq month `(8 11 2 5)))
56)
;; 7 8 9
;; 10 11 12
;; 1 2 3
;; 4 5 6
((and
(eq displayed-month 7)
(memq month `(7 10 1 4)))
6)
((and
(eq displayed-month 7)
(memq month `(8 11 2 5)))
31)
((and
(eq displayed-month 7)
(memq month `(9 12 3 6)))
56)
;; 8 9 10
;; 11 12 1
;; 2 3 4
;; 5 6 7
((and
(eq displayed-month 8)
(memq month `(8 11 2 5)))
6)
((and
(eq displayed-month 8)
(memq month `(9 12 3 6)))
31)
((and
(eq displayed-month 8)
(memq month `(10 1 4 7)))
56)
;; 9 10 11
;; 12 1 2
;; 3 4 5
;; 6 7 8
((and
(eq displayed-month 9)
(memq month `(9 12 3 6)))
6)
((and
(eq displayed-month 9)
(memq month `(10 1 4 7)))
31)
((and
(eq displayed-month 9)
(memq month `(11 2 5 8)))
56)
;; 10 11 12
;; 1 2 3
;; 4 5 6
;; 7 8 9
((and
(eq displayed-month 10)
(memq month `(10 1 4 7)))
6)
((and
(eq displayed-month 10)
(memq month `(11 2 5 8)))
31)
((and
(eq displayed-month 10)
(memq month `(12 3 6 9)))
56)
;; 11 12 1
;; 2 3 4
;; 5 6 7
;; 8 9 10
((and
(eq displayed-month 11)
(memq month `(11 2 5 8)))
6)
((and
(eq displayed-month 11)
(memq month `(12 3 6 9)))
31)
((and
(eq displayed-month 11)
(memq month `(1 4 7 10)))
56)
;; 12 1 2
;; 3 4 5
;; 6 7 8
;; 9 10 11
((and
(eq displayed-month 12)
(memq month `(12 3 6 9)))
6)
((and
(eq displayed-month 12)
(memq month `(1 4 7 10)))
31)
((and
(eq displayed-month 12)
(memq month `(2 5 8 11)))
56) )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(* 3 (mod
(- (calendar-day-of-week date) calendar-week-start-day)
7))))))
I must be missing something because it looks like the formulas are as simple as (pseudocode):
first = 9 * ( rows - 1 )
second = 6 + 25 * ( cols - 1 )
based on your edit, you can calculate the rows and cols to move with:
if target > display
difference = target - display
else
difference = 12 + target - display
rows = difference / 3
cols = difference % 3
rowmove = 9 * rows
colmove = 6 + 25 * cols
And then use the formula above.
My attempt at elisp:
(let difference (if (>= target-month display-month)
(- target-month display-month)
(- (+ target-month 12) display-month)))
(let rows (/ difference 3))
(let cols (% difference 3))
(let rowmove (* 9 rows))
(let colmove (+ 6 (* 25 cols)))