cal-tex.el 64.2 KB
Newer Older
Edward M. Reingold's avatar
Edward M. Reingold committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
;;; cal-tex.el --- calendar functions for printing calendars with LaTeX.

;; Copyright (C) 1995 Free Software Foundation, Inc.

;; Author: Steve Fisk <fisk@bowdoin.edu>
;;      Edward M. Reingold <reingold@cs.uiuc.edu>
;; Keywords: calendar
;; Human-Keywords: Calendar, LaTeX

;; This file is part of GNU Emacs.

;; GNU Emacs 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, or (at your option)
;; any later version.

;; GNU Emacs 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
Erik Naggum's avatar
Erik Naggum committed
23 24 25
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Edward M. Reingold's avatar
Edward M. Reingold committed
26 27 28

;;; Commentary:

Edward M. Reingold's avatar
Edward M. Reingold committed
29 30
;; This collection of functions implements the creation of LaTeX calendars
;; based on the user's holiday choices and diary file.
Edward M. Reingold's avatar
Edward M. Reingold committed
31

Erik Naggum's avatar
Erik Naggum committed
32 33 34 35 36 37 38 39
;; TO DO
;;
;;     (*)  Add holidays and diary entries to daily calendar.
;;
;;     (*)  Add diary entries to weekly calendar functions.
;;
;;     (*)  Make calendar styles for A4 paper.
;;
40
;;     (*)  Make monthly styles Filofax paper.
Edward M. Reingold's avatar
Edward M. Reingold committed
41 42 43 44 45

;;; Code:

(require 'calendar)

46
(autoload 'list-diary-entries "diary-lib" nil t)
Edward M. Reingold's avatar
Edward M. Reingold committed
47 48 49 50 51 52 53
(autoload 'calendar-holiday-list "holidays" nil t)
(autoload 'calendar-iso-from-absolute "cal-iso" nil t)

;;;
;;; Customizable variables 
;;;

Richard M. Stallman's avatar
Richard M. Stallman committed
54
(defcustom cal-tex-which-days '(0 1 2 3 4 5 6)
Edward M. Reingold's avatar
Edward M. Reingold committed
55 56 57 58 59 60
  "*The days of the week that are displayed on the portrait monthly calendar.
Sunday is 0, Monday is 1, and so on.  The default is to print from Sunday to
Saturday.  For example, use

                    (setq cal-tex-which-days '(1 3 5))

Richard M. Stallman's avatar
Richard M. Stallman committed
61 62 63
to only print Monday, Wednesday, Friday."
  :type '(repeat integer)
  :group 'calendar-tex)
Edward M. Reingold's avatar
Edward M. Reingold committed
64

Richard M. Stallman's avatar
Richard M. Stallman committed
65
(defcustom cal-tex-holidays t
Edward M. Reingold's avatar
Edward M. Reingold committed
66
  "*If t (default), then the holidays are also printed.
Richard M. Stallman's avatar
Richard M. Stallman committed
67 68 69
If finding the holidays is too slow, set this to nil."
  :type 'boolean
  :group 'calendar-tex)
Edward M. Reingold's avatar
Edward M. Reingold committed
70

Richard M. Stallman's avatar
Richard M. Stallman committed
71 72 73 74
(defcustom cal-tex-diary nil
  "*If t, the diary entries are printed in the calendar."
  :type 'boolean
  :group 'calendar-tex)
Edward M. Reingold's avatar
Edward M. Reingold committed
75

76 77 78 79 80
(defcustom cal-tex-rules nil
  "*If t, pages will be ruled in some styles."
  :type 'boolean
  :group 'calendar-tex)

Richard M. Stallman's avatar
Richard M. Stallman committed
81
(defcustom cal-tex-daily-string
Edward M. Reingold's avatar
Edward M. Reingold committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  '(let* ((year (extract-calendar-year date))
          (day  (calendar-day-number date))
          (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
     (format "%d/%d" day  days-remaining))
  "*An expression in the variable `date' whose value is placed on date.
The string resulting from evaluating this expression is placed at the bottom
center of `date' on the monthly calendar, next to the date in the weekly
calendars, and in the top center of daily calendars.

Default is ordinal day number of the year and the number of days remaining.
As an example of what you do, setting this to

    '(progn
       (require 'cal-hebrew)
       (calendar-hebrew-date-string date))

Richard M. Stallman's avatar
Richard M. Stallman committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
will put the Hebrew date at the bottom of each day."
  :type 'sexp
  :group 'calendar-tex)

(defcustom cal-tex-buffer "calendar.tex"
  "*The name for the tex-ed calendar."
  :type 'string
  :group 'calendar-tex)

(defcustom cal-tex-24 nil
  "*If t, use a 24 hour clock in the daily calendar."
  :type 'boolean
  :group 'calendar-tex)

(defcustom cal-tex-daily-start 8
  "*The first hour of the daily calendar page."
  :type 'integer
  :group 'calendar-tex)

(defcustom cal-tex-daily-end 20
  "*The last hour of the daily calendar page."
  :type 'integer
  :group 'calendar-tex)
Edward M. Reingold's avatar
Edward M. Reingold committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

;;;
;;; Definitions for LaTeX code
;;;

(defvar  cal-tex-day-prefix "\\caldate{%s}{%s}"
  "The initial LaTeX code for a day.  
The holidays, diary entries, bottom string, and the text follow.")
 
(defvar cal-tex-day-name-format "\\myday{%s}%%"
  "The format for LaTeX code for a day name.  The names are taken from
calendar-day-name-array.")

(defvar cal-tex-cal-one-month
"\\def\\calmonth#1#2%
{\\begin{center}%
\\Huge\\bf\\uppercase{#1} #2 \\\\[1cm]%
\\end{center}}% 
\\vspace*{-1.5cm}%
%
"
  "LaTeX code for the month header")

(defvar cal-tex-cal-multi-month
"\\def\\calmonth#1#2#3#4%
{\\begin{center}%
\\Huge\\bf #1 #2---#3 #4\\\\[1cm]%
\\end{center}}% 
\\vspace*{-1.5cm}%
%
"
  "LaTeX code for the month header")

(defvar cal-tex-myday
"\\renewcommand{\\myday}[1]%
{\\makebox[\\cellwidth]{\\hfill\\large\\bf#1\\hfill}}
%
"
  "LaTeX code for a day heading")

(defvar cal-tex-caldate
"\\fboxsep=0pt
\\long\\def\\caldate#1#2#3#4#5#6{%
    \\fbox{\\hbox to\\cellwidth{%
     \\vbox to\\cellheight{%
       \\hbox to\\cellwidth{%
          {\\hspace*{1mm}\\Large \\bf \\strut #2}\\hspace{.05\\cellwidth}%
          \\raisebox{\\holidaymult\\cellheight}%
                   {\\parbox[t]{.75\\cellwidth}{\\tiny \\raggedright #4}}}
       \\hbox to\\cellwidth{%
           \\hspace*{1mm}\\parbox{.95\\cellwidth}{\\tiny \\raggedright #3}}
       \\hspace*{1mm}%
       \\hbox to\\cellwidth{#6}%
       \\vfill%
       \\hbox to\\cellwidth{\\hfill \\tiny #5 \\hfill}%
       \\vskip 1.4pt}%
     \\hskip -0.4pt}}}
"
  "LaTeX code to insert one box with date info in calendar.
This definition is the heart of the calendar!")

(defun cal-tex-list-holidays (d1 d2)
  "Generate a list of all holidays from absolute date D1 to D2."
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  (let* ((start (calendar-gregorian-from-absolute d1))
         (displayed-month (extract-calendar-month start))
         (displayed-year (extract-calendar-year start))
         (end (calendar-gregorian-from-absolute d2))
         (end-month (extract-calendar-month end))
         (end-year (extract-calendar-year end))
         (number-of-intervals
          (1+ (/ (calendar-interval displayed-month displayed-year
                                    end-month end-year)
                 3)))
         (holidays nil)
         (in-range))
    (increment-calendar-month displayed-month displayed-year 1)
    (calendar-for-loop i from 1 to number-of-intervals do
      (setq holidays (append holidays (calendar-holiday-list)))
      (increment-calendar-month displayed-month displayed-year 3))
    (while holidays
      (and (car (car holidays))
           (let ((a (calendar-absolute-from-gregorian (car (car holidays)))))
             (and (<= d1 a) (<= a d2)))
           (setq in-range (append (list (car holidays)) in-range)))
      (setq holidays (cdr holidays)))
    in-range))
Edward M. Reingold's avatar
Edward M. Reingold committed
207 208 209

(defun cal-tex-list-diary-entries (d1 d2)
  "Generate a list of all diary-entries from absolute date D1 to D2."
210
  (let ((diary-list-include-blanks nil)
211
        (diary-display-hook 'ignore))
Edward M. Reingold's avatar
Edward M. Reingold committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    (list-diary-entries
     (calendar-gregorian-from-absolute d1)
     (1+ (- d2 d1)))))

(defun cal-tex-preamble (&optional args)
  "Insert the LaTeX preamble.
Preamble Includes initial definitions for various LaTeX commands.
Optional ARGS are included."
  (set-buffer (get-buffer-create cal-tex-buffer))
  (erase-buffer)
  (insert "\\documentstyle")
  (if args
      (insert "[" args "]"))
  (insert "{article}\n"
          "\\hbadness 20000
227
\\hfuzz=1000pt
Edward M. Reingold's avatar
Edward M. Reingold committed
228
\\vbadness 20000
229
\\lineskip 0pt
Edward M. Reingold's avatar
Edward M. Reingold committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
\\marginparwidth 0pt
\\oddsidemargin  -2cm
\\evensidemargin -2cm
\\marginparsep   0pt
\\topmargin      0pt
\\textwidth      7.5in
\\textheight     9.5in
\\newlength{\\cellwidth}
\\newlength{\\cellheight}
\\newlength{\\boxwidth}
\\newlength{\\boxheight}
\\newlength{\\cellsize}
\\newcommand{\\myday}[1]{}
\\newcommand{\\caldate}[6]{}
\\newcommand{\\nocaldate}[6]{}
\\newcommand{\\calsmall}[6]{}
%
"))

;;;
;;;  Yearly calendars
;;;

(defun cal-tex-cursor-year (&optional arg)
  "Make a buffer with LaTeX commands for the year cursor is on.
Optional prefix argument specifies number of years."
  (interactive "P")
  (cal-tex-year (extract-calendar-year (calendar-cursor-to-date t))
                (if arg arg 1)))

(defun cal-tex-cursor-year-landscape (&optional arg)
  "Make a buffer with LaTeX commands for the year cursor is on.
262
Optional prefix argument specifies number of years."
Edward M. Reingold's avatar
Edward M. Reingold committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
  (interactive "P")
  (cal-tex-year (extract-calendar-year (calendar-cursor-to-date t))
                (if arg arg 1)
                t))

(defun cal-tex-year (year n &optional landscape)
  "Make a one page yearly calendar of YEAR; do this for N years.
There are four rows of three months each, unless optional LANDSCAPE is t,
in which case the calendar isprinted in landscape mode with three rows of
four months each."
  (cal-tex-insert-preamble 1 landscape "12pt")
  (if landscape
      (cal-tex-vspace "-.6cm")
    (cal-tex-vspace "-3.1cm"))
  (calendar-for-loop j from 1 to n do
     (insert "\\vfill%\n")
     (cal-tex-b-center)
     (cal-tex-Huge (number-to-string year))
     (cal-tex-e-center)
     (cal-tex-vspace "1cm")
     (cal-tex-b-center)
     (cal-tex-b-parbox "l" (if landscape "5.9in" "4.3in"))
     (insert "\n")
     (cal-tex-noindent)
     (cal-tex-nl)
     (calendar-for-loop i from 1 to 12 do
        (insert (cal-tex-mini-calendar i year "month" "1.1in" "1in"))
        (insert "\\month")
        (cal-tex-hspace "0.5in")
        (if (zerop (mod i (if landscape 4 3)))
            (cal-tex-nl "0.5in")))
     (cal-tex-e-parbox)
     (cal-tex-e-center)
     (insert "\\vfill%\n")
     (setq year (1+ year))
     (if (/= j n)
         (cal-tex-newpage)
       (cal-tex-end-document))
     (run-hooks 'cal-tex-year-hook))
  (run-hooks 'cal-tex-hook))

(defun cal-tex-cursor-filofax-year (&optional arg)
  "Make a Filofax one page yearly calendar of year indicated by cursor.
Optional parameter specifies number of years."
  (interactive "P")
  (let* ((n (if arg arg 1))
         (year (extract-calendar-year (calendar-cursor-to-date t))))
    (cal-tex-preamble "twoside")
    (cal-tex-cmd "\\textwidth 3.25in")
    (cal-tex-cmd "\\textheight 6.5in")
313 314
    (cal-tex-cmd "\\oddsidemargin 1.675in")
    (cal-tex-cmd "\\evensidemargin 1.675in")
Edward M. Reingold's avatar
Edward M. Reingold committed
315 316
    (cal-tex-cmd "\\topmargin 0pt")
    (cal-tex-cmd "\\headheight -0.875in")
317
    (cal-tex-cmd "\\fboxsep 0.5mm")
Edward M. Reingold's avatar
Edward M. Reingold committed
318 319
    (cal-tex-cmd "\\pagestyle{empty}")
    (cal-tex-b-document)
320
    (cal-tex-cmd "\\vspace*{0.25in}")
Edward M. Reingold's avatar
Edward M. Reingold committed
321 322 323 324 325 326 327 328 329 330
    (calendar-for-loop j from 1 to n do
       (insert (format "\\hfil {\\Large \\bf %s} \\hfil\\\\\n" year))
       (cal-tex-b-center)
       (cal-tex-b-parbox "l" "\\textwidth")
       (insert "\n")
       (cal-tex-noindent)
       (cal-tex-nl)
       (calendar-for-loop i from 1 to 12 do
          (insert (cal-tex-mini-calendar i year
                                         (calendar-month-name i)
331
                                         "1in" ".9in" "tiny" "0.6mm")))
Edward M. Reingold's avatar
Edward M. Reingold committed
332 333 334 335 336 337 338 339 340
       (insert
"\\noindent\\fbox{\\January}\\fbox{\\February}\\fbox{\\March}\\\\
\\noindent\\fbox{\\April}\\fbox{\\May}\\fbox{\\June}\\\\
\\noindent\\fbox{\\July}\\fbox{\\August}\\fbox{\\September}\\\\
\\noindent\\fbox{\\October}\\fbox{\\November}\\fbox{\\December}
")
       (cal-tex-e-parbox)
       (cal-tex-e-center)
       (setq year (1+ year))
341 342 343 344
       (if (= j n)
           (cal-tex-end-document)
         (cal-tex-newpage)
         (cal-tex-cmd "\\vspace*{0.25in}"))
Edward M. Reingold's avatar
Edward M. Reingold committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
       (run-hooks 'cal-tex-year-hook))
    (run-hooks 'cal-tex-hook)))

;;;
;;;  Monthly calendars
;;;

(defun cal-tex-cursor-month-landscape (&optional arg)
  "Make a buffer with LaTeX commands for the month cursor is on.
Optional prefix argument specifies number of months to be produced.
The output is in landscape format, one month to a page."
  (interactive "P")
  (let* ((n (if arg arg 1))
         (date (calendar-cursor-to-date t))
         (month (extract-calendar-month date))
         (year (extract-calendar-year date))
         (end-month month)
         (end-year year)
         (cal-tex-which-days '(0 1 2 3 4 5 6)))
    (increment-calendar-month end-month end-year (1- n))
    (let ((diary-list (if cal-tex-diary
                          (cal-tex-list-diary-entries
                           (calendar-absolute-from-gregorian
                            (list month 1 year))
                           (calendar-absolute-from-gregorian
                            (list end-month
                                  (calendar-last-day-of-month
                                   end-month end-year)
                                  end-year)))))
          (holidays (if cal-tex-holidays
                        (cal-tex-list-holidays
                         (calendar-absolute-from-gregorian
                          (list month 1 year))
                         (calendar-absolute-from-gregorian
                          (list end-month
                                (calendar-last-day-of-month end-month end-year)
                                end-year)))))
          (other-month)
          (other-year)
          (small-months-at-start))
      (cal-tex-insert-preamble (cal-tex-number-weeks month year 1) t "12pt")
      (cal-tex-cmd cal-tex-cal-one-month)
      (calendar-for-loop i from 1 to n do
         (setq other-month month)
         (setq other-year year)
         (increment-calendar-month other-month other-year -1)
         (insert (cal-tex-mini-calendar other-month other-year "lastmonth"
                                        "\\cellwidth" "\\cellheight"))
         (increment-calendar-month other-month other-year 2)
         (insert (cal-tex-mini-calendar other-month other-year "nextmonth"
                                        "\\cellwidth" "\\cellheight"))
         (cal-tex-insert-month-header 1 month year month year)
         (cal-tex-insert-day-names)
         (cal-tex-nl ".2cm")
         (setq small-months-at-start
               (< 1 (mod (- (calendar-day-of-week (list month 1 year))
                            calendar-week-start-day)
                         7)))
         (if small-months-at-start
             (insert "\\lastmonth\\nextmonth\\hspace*{-2\\cellwidth}"))
         (cal-tex-insert-blank-days month year cal-tex-day-prefix)
         (cal-tex-insert-days month year diary-list holidays
                              cal-tex-day-prefix)
         (cal-tex-insert-blank-days-at-end month year cal-tex-day-prefix)
         (if (and (not small-months-at-start)
                  (< 1 (mod (- (1- calendar-week-start-day)
                               (calendar-day-of-week
                                (list month
                                      (calendar-last-day-of-month month year)
                                      year)))
                                 7)))
             (insert "\\vspace*{-\\cellwidth}\\hspace*{-2\\cellwidth}"
                     "\\lastmonth\\nextmonth"))
         (if (/= i n)
             (progn
               (run-hooks 'cal-tex-month-hook)
               (cal-tex-newpage)
               (increment-calendar-month month year 1)
               (cal-tex-vspace "-2cm")
               (cal-tex-insert-preamble
                (cal-tex-number-weeks month year 1) t "12pt" t))))
      (cal-tex-end-document)
      (run-hooks 'cal-tex-hook))))

(defun cal-tex-cursor-month (arg)
  "Make a buffer with LaTeX commands for the month cursor is on.
Optional prefix argument specifies number of months to be produced.
Calendar is condensed onto one page."
  (interactive "P")
  (let* ((date (calendar-cursor-to-date t))
         (month (extract-calendar-month date))
         (year (extract-calendar-year date))
         (end-month month)
         (end-year year)
         (n (if arg arg 1)))
    (increment-calendar-month end-month end-year (1- n))
    (let ((diary-list (if cal-tex-diary
                          (cal-tex-list-diary-entries
                           (calendar-absolute-from-gregorian
                            (list month 1 year))
                           (calendar-absolute-from-gregorian
                            (list end-month
                                  (calendar-last-day-of-month
                                   end-month end-year)
                                  end-year)))))
          (holidays (if cal-tex-holidays
                        (cal-tex-list-holidays
                         (calendar-absolute-from-gregorian
                          (list month 1 year))
                         (calendar-absolute-from-gregorian
                          (list end-month
                                (calendar-last-day-of-month end-month end-year)
                                end-year)))))
          (other-month)
          (other-year))
      (cal-tex-insert-preamble (cal-tex-number-weeks month year n) nil"12pt")
      (if (> n 1)
          (cal-tex-cmd cal-tex-cal-multi-month)
        (cal-tex-cmd cal-tex-cal-one-month))
      (cal-tex-insert-month-header n month year end-month end-year)
      (cal-tex-insert-day-names)
      (cal-tex-nl ".2cm")
      (cal-tex-insert-blank-days month year cal-tex-day-prefix)
      (calendar-for-loop i from 1 to n do
         (setq other-month month)
         (setq other-year year)
         (cal-tex-insert-days month year diary-list holidays
                              cal-tex-day-prefix)
         (increment-calendar-month month year 1))
      (cal-tex-insert-blank-days-at-end end-month end-year cal-tex-day-prefix)
      (cal-tex-end-document)))
  (run-hooks 'cal-tex-hook))

(defun cal-tex-insert-days (month year diary-list holidays day-format) 
  "Insert LaTeX commands for a range of days in monthly calendars.
LaTeX commands are inserted for the days of the MONTH in YEAR.
Diary entries on DIARY-LIST are included. Holidays on HOLIDAYS are included.
Each day is formatted using format DAY-FORMAT."
  (let* ((blank-days;; at start of month
          (mod
           (- (calendar-day-of-week (list month 1 year))
              calendar-week-start-day)
           7))
         (date)
         (last (calendar-last-day-of-month month year)))
    (calendar-for-loop i from 1 to last do
       (setq date (list month i year))
       (if (memq (calendar-day-of-week date) cal-tex-which-days)
           (progn 
             (insert (format day-format (calendar-month-name month) i))
             (cal-tex-arg (cal-tex-latexify-list diary-list date))
             (cal-tex-arg (cal-tex-latexify-list holidays date))
             (cal-tex-arg (eval cal-tex-daily-string))
             (cal-tex-arg)
             (cal-tex-comment)))
       (if (and (zerop (mod (+ i blank-days) 7))
                (/= i last))
           (progn
             (cal-tex-hfill)
             (cal-tex-nl))))))

(defun cal-tex-insert-day-names ()
  "Insert the names of the days at top of a monthly calendar."
  (calendar-for-loop i from 0 to 6 do
     (if (memq i cal-tex-which-days)
         (insert (format cal-tex-day-name-format 
                         (aref calendar-day-name-array 
                               (mod (+ calendar-week-start-day i) 7)))))
     (cal-tex-comment)))

(defun cal-tex-insert-month-header (n month year end-month end-year)
  "Create a title for a calendar.
A title is inserted for a calendar with N months starting with 
MONTH YEAR and ending with END-MONTH END-YEAR."
  (let ( (month-name (calendar-month-name  month))
         (end-month-name (calendar-month-name  end-month)))
    (if (= 1 n)
        (insert (format "\\calmonth{%s}{%s}\n\\vspace*{-0.5cm}"
                month-name year) )       
        (insert (format "\\calmonth{%s}{%s}{%s}{%s}\n\\vspace*{-0.5cm}"
                month-name year end-month-name end-year))))
  (cal-tex-comment))

(defun cal-tex-insert-blank-days (month year day-format)
  "Insert code for initial days not in calendar.
Insert LaTeX code for the blank days at the beginning of the MONTH in
YEAR.  The entry is formatted using DAY-FORMAT.  If the entire week is
blank, no days are inserted."
  (if (cal-tex-first-blank-p month year)
      (let* ((blank-days;; at start of month
              (mod
               (- (calendar-day-of-week (list month 1 year))
                  calendar-week-start-day)
               7)))
        (calendar-for-loop i from 0 to (1- blank-days) do 
            (if (memq i cal-tex-which-days)
                (insert (format day-format " " " ") "{}{}{}{}%\n"))))))

(defun cal-tex-insert-blank-days-at-end (month year day-format)
  "Insert code for final days not in calendar.
Insert LaTeX code for the blank days at the end of the MONTH in YEAR.
The entry is formatted using DAY-FORMAT."
  (if (cal-tex-last-blank-p month year)
      (let* ((last-day (calendar-last-day-of-month month year))
             (blank-days;; at end of month
              (mod
               (- (calendar-day-of-week (list month last-day year))
                  calendar-week-start-day)
               7)))
        (calendar-for-loop i from (1+ blank-days) to 6 do 
           (if (memq i cal-tex-which-days)
               (insert (format day-format "" "") "{}{}{}{}%\n"))))))

(defun cal-tex-first-blank-p (month year)
  "Determine if any days of the first week will be printed.
Return t if there will there be any days of the first week printed
in the calendar starting in MONTH YEAR."
  (let ((any-days nil)
        (the-saturday))                 ;the day of week of 1st Saturday
    (calendar-for-loop i from 1 to 7 do
       (if (= 6 (calendar-day-of-week (list month i year)))
           (setq the-saturday i)))
    (calendar-for-loop i from 1 to the-saturday do
       (if (memq (calendar-day-of-week (list month i year))
                 cal-tex-which-days)
           (setq any-days t)))
    any-days))

(defun cal-tex-last-blank-p (month year)
  "Determine if any days of the last week will be printed.
Return t if there will there be any days of the last week printed
in the calendar starting in MONTH YEAR."
  (let ((any-days nil)
        (last-day (calendar-last-day-of-month month year))
        (the-sunday))                   ;the day of week of last Sunday
    (calendar-for-loop i from (- last-day 6) to last-day do
       (if (= 0 (calendar-day-of-week (list month i year)))
           (setq the-sunday i)))
    (calendar-for-loop i from the-sunday to last-day do
       (if (memq (calendar-day-of-week (list month i year))
                 cal-tex-which-days)
           (setq any-days t)))
    any-days))

(defun cal-tex-number-weeks (month year n)
  "Determine the number of weeks in a range of dates.
Compute the number of  weeks in the calendar starting with MONTH and YEAR, 
and lasting N months, including only the days in WHICH-DAYS. As it stands, 
this is only an upper bound."
  (let ((d (list month 1 year)))
    (increment-calendar-month month year (1- n))
    (/ (- (calendar-dayname-on-or-before
           calendar-week-start-day
           (+ 7 (calendar-absolute-from-gregorian
                   (list month (calendar-last-day-of-month month year) year))))
          (calendar-dayname-on-or-before
           calendar-week-start-day
           (calendar-absolute-from-gregorian d)))
       7)))

;;;
;;; Weekly calendars
;;;

(defun cal-tex-cursor-week (&optional arg)
610 611
  "Make a buffer with LaTeX commands for a two-page one-week calendar.
It applies to the week that point is in.
Edward M. Reingold's avatar
Edward M. Reingold committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
Optional prefix argument specifies number of weeks.
Holidays are included if `cal-tex-holidays' is t."
  (interactive "P")
  (let* ((n (if arg arg 1))
         (date (calendar-gregorian-from-absolute
                (calendar-dayname-on-or-before
                 calendar-week-start-day
                 (calendar-absolute-from-gregorian
                  (calendar-cursor-to-date t)))))
         (month (extract-calendar-month date))
         (year (extract-calendar-year date))
         (holidays (if cal-tex-holidays
                       (cal-tex-list-holidays
                        (calendar-absolute-from-gregorian date)
                        (+ (* 7 n)
                           (calendar-absolute-from-gregorian date))))))
    (cal-tex-preamble "11pt")
    (cal-tex-cmd "\\textwidth   6.5in")
    (cal-tex-cmd "\\textheight 10.5in")
    (cal-tex-cmd "\\oddsidemargin 0in")
    (cal-tex-cmd "\\evensidemargin 0in")
    (insert cal-tex-LaTeX-hourbox)
    (cal-tex-b-document)
    (cal-tex-cmd "\\pagestyle{empty}")
    (calendar-for-loop i from 1 to n do
       (cal-tex-vspace "-1.5in")
       (cal-tex-b-center)
       (cal-tex-Huge-bf (format "\\uppercase{%s}"
                                (calendar-month-name month)))
       (cal-tex-hspace "2em")
       (cal-tex-Huge-bf (number-to-string year))
       (cal-tex-nl ".5cm")
       (cal-tex-e-center)
       (cal-tex-hspace "-.2in")
       (cal-tex-b-parbox "l" "7in")
       (calendar-for-loop j from 1 to 7 do 
          (cal-tex-week-hours date holidays "3.1")
          (setq date (cal-tex-incr-date date)))
       (cal-tex-e-parbox)
       (setq month (extract-calendar-month date))
       (setq year (extract-calendar-year date))
       (if (/= i n)
           (progn
             (run-hooks 'cal-tex-week-hook)
             (cal-tex-newpage))))
    (cal-tex-end-document)
    (run-hooks 'cal-tex-hook)))

(defun cal-tex-cursor-week2 (&optional arg)
661 662
  "Make a buffer with LaTeX commands for a two-page one-week calendar.
It applies to the week that point is in.
Edward M. Reingold's avatar
Edward M. Reingold committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
Optional prefix argument specifies number of weeks.
Holidays are included if `cal-tex-holidays' is t."
  (interactive "P")
  (let* ((n (if arg arg 1))
         (date (calendar-gregorian-from-absolute
                (calendar-dayname-on-or-before
                 calendar-week-start-day
                 (calendar-absolute-from-gregorian
                  (calendar-cursor-to-date t)))))
         (month (extract-calendar-month date))
         (year (extract-calendar-year date))
         (d date)
         (holidays (if cal-tex-holidays
                       (cal-tex-list-holidays
                        (calendar-absolute-from-gregorian date)
                        (+ (* 7 n)
                           (calendar-absolute-from-gregorian date))))))
    (cal-tex-preamble "12pt")
    (cal-tex-cmd "\\textwidth   6.5in")
    (cal-tex-cmd "\\textheight 10.5in")
    (cal-tex-cmd "\\oddsidemargin 0in")
    (cal-tex-cmd "\\evensidemargin 0in")
    (insert cal-tex-LaTeX-hourbox)
    (cal-tex-b-document)
    (cal-tex-cmd "\\pagestyle{empty}")
    (calendar-for-loop i from 1 to n do
       (cal-tex-vspace "-1.5in")
       (cal-tex-b-center)
       (cal-tex-Huge-bf (format "\\uppercase{%s}"
                                (calendar-month-name month)))
       (cal-tex-hspace "2em")
       (cal-tex-Huge-bf (number-to-string year))
       (cal-tex-nl ".5cm")
       (cal-tex-e-center)
       (cal-tex-hspace "-.2in")
       (cal-tex-b-parbox "l" "\\textwidth")
       (calendar-for-loop j from 1 to 3 do 
          (cal-tex-week-hours date holidays "5")
          (setq date (cal-tex-incr-date date)))
       (cal-tex-e-parbox)
       (cal-tex-nl)
       (insert (cal-tex-mini-calendar
                (extract-calendar-month (cal-tex-previous-month date))
                (extract-calendar-year (cal-tex-previous-month date))
                "lastmonth" "1.1in" "1in"))
       (insert (cal-tex-mini-calendar
                (extract-calendar-month date)
                (extract-calendar-year date)
                "thismonth" "1.1in" "1in"))
       (insert (cal-tex-mini-calendar
                (extract-calendar-month (cal-tex-next-month date))
                (extract-calendar-year (cal-tex-next-month date))
                "nextmonth" "1.1in" "1in"))
       (insert "\\hbox to \\textwidth{")
       (cal-tex-hfill)
       (insert "\\lastmonth")
       (cal-tex-hfill)
       (insert "\\thismonth")
       (cal-tex-hfill)
       (insert "\\nextmonth")
       (cal-tex-hfill)
       (insert "}")
       (cal-tex-nl)
       (cal-tex-b-parbox "l" "\\textwidth")
       (calendar-for-loop j from 4 to 7 do 
                          (cal-tex-week-hours date holidays "5")
                          (setq date (cal-tex-incr-date date)))
       (cal-tex-e-parbox)
       (setq month (extract-calendar-month date))
       (setq year (extract-calendar-year date))
       (if (/= i n)
           (progn
             (run-hooks 'cal-tex-week-hook)
             (cal-tex-newpage))))
    (cal-tex-end-document)
    (run-hooks 'cal-tex-hook)))

740 741
(defun cal-tex-cursor-week-iso (&optional arg)
  "Make a buffer with LaTeX commands for a one page ISO-style weekly calendar.
Edward M. Reingold's avatar
Edward M. Reingold committed
742
Optional prefix argument specifies number of weeks.
743
Diary entries are included if `cal-tex-diary' is t.
Edward M. Reingold's avatar
Edward M. Reingold committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
Holidays are included if `cal-tex-holidays' is t."
  (interactive "P")
  (let* ((n (if arg arg 1))
         (date (calendar-gregorian-from-absolute
                (calendar-dayname-on-or-before
                 1
                 (calendar-absolute-from-gregorian
                  (calendar-cursor-to-date t)))))
         (month (extract-calendar-month date))
         (year (extract-calendar-year date))
         (day (extract-calendar-day date))
         (holidays (if cal-tex-holidays
                       (cal-tex-list-holidays
                        (calendar-absolute-from-gregorian date)
                        (+ (* 7 n)
                           (calendar-absolute-from-gregorian date)))))
         (diary-list (if cal-tex-diary
                         (cal-tex-list-diary-entries
                          (calendar-absolute-from-gregorian
                           (list month 1 year))
                        (+ (* 7 n)
                           (calendar-absolute-from-gregorian date))))))
    (cal-tex-preamble "11pt")
    (cal-tex-cmd "\\textwidth 6.5in")
    (cal-tex-cmd "\\textheight 10.5in")
    (cal-tex-cmd "\\oddsidemargin 0in")
    (cal-tex-cmd "\\evensidemargin 0in")
    (cal-tex-b-document)
    (cal-tex-cmd "\\pagestyle{empty}")
    (calendar-for-loop i from 1 to n do
       (cal-tex-vspace "-1.5in")
       (cal-tex-b-center)
       (cal-tex-Huge-bf
        (let* ((d (calendar-iso-from-absolute
                   (calendar-absolute-from-gregorian date))))
          (format "Week %d of %d"
                  (extract-calendar-month d)
                  (extract-calendar-year d))))
       (cal-tex-nl ".5cm")
       (cal-tex-e-center)
       (cal-tex-b-parbox "l" "\\textwidth")
       (calendar-for-loop j from 1 to 7 do 
          (cal-tex-b-parbox "t" "\\textwidth")
          (cal-tex-b-parbox "t" "\\textwidth")
          (cal-tex-rule "0pt" "\\textwidth" ".2mm")
          (cal-tex-nl)
          (cal-tex-b-parbox "t" "\\textwidth")
          (cal-tex-large-bf (calendar-day-name date))
          (insert ", ")
          (cal-tex-large-bf (calendar-month-name month))
          (insert " ")
          (cal-tex-large-bf (number-to-string day))
          (if (not (string= "" (cal-tex-latexify-list holidays date)))
              (progn
                (insert ": ")
                (cal-tex-large-bf (cal-tex-latexify-list holidays date "; "))))
          (cal-tex-hfill)
          (insert " " (eval cal-tex-daily-string))
          (cal-tex-e-parbox)
          (cal-tex-nl)
          (cal-tex-noindent)
          (cal-tex-b-parbox "t" "\\textwidth")
          (if (not (string= "" (cal-tex-latexify-list diary-list date)))
              (progn
                (insert "\\vbox to 0pt{")
                (cal-tex-large-bf
                 (cal-tex-latexify-list diary-list date))
                (insert "}")))
          (cal-tex-e-parbox)
          (cal-tex-nl)
          (setq date (cal-tex-incr-date date))
          (setq month (extract-calendar-month date))
          (setq day (extract-calendar-day date))
          (cal-tex-e-parbox)
          (cal-tex-e-parbox "2cm")
          (cal-tex-nl)
          (setq month (extract-calendar-month date))
          (setq year (extract-calendar-year date)))
822
       (cal-tex-e-parbox)
Edward M. Reingold's avatar
Edward M. Reingold committed
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
       (if (/= i n)
           (progn
             (run-hooks 'cal-tex-week-hook)
             (cal-tex-newpage))))
    (cal-tex-end-document)
    (run-hooks 'cal-tex-hook)))

(defvar cal-tex-LaTeX-hourbox 
  "\\newcommand{\\hourbox}[2]%
{\\makebox[2em]{\\rule{0cm}{#2ex}#1}\\rule{3in}{.15mm}}\n"
  "One hour and a line on the right.")

(defun cal-tex-week-hours (date holidays height)
  "Insert hourly entries for DATE with HOLIDAYS, with line height HEIGHT." 
  (let ((month (extract-calendar-month date))
        (day   (extract-calendar-day date))
        (year  (extract-calendar-year date))
        (afternoon))
  (cal-tex-comment "begin cal-tex-week-hours")
  (cal-tex-cmd  "\\ \\\\[-.2cm]")
  (cal-tex-cmd "\\noindent")
  (cal-tex-b-parbox "l" "6.8in")
  (cal-tex-large-bf (calendar-day-name date))
  (insert ", ")
  (cal-tex-large-bf (calendar-month-name month))
  (insert " ")
  (cal-tex-large-bf (number-to-string day))
  (if (not (string= "" (cal-tex-latexify-list holidays date)))
      (progn
        (insert ": ")
        (cal-tex-large-bf (cal-tex-latexify-list holidays date "; "))))
  (cal-tex-hfill)
  (insert " " (eval cal-tex-daily-string))
  (cal-tex-e-parbox)
  (cal-tex-nl "-.3cm")
  (cal-tex-rule "0pt" "6.8in" ".2mm")
  (cal-tex-nl "-.1cm")
  (calendar-for-loop i from 8 to 12 do
     (if cal-tex-24
         (setq afternoon (+ i 5))
       (setq afternoon (- i 7)))
     (cal-tex-cmd "\\hourbox"  (number-to-string i))
     (cal-tex-arg height)
     (cal-tex-hspace ".4cm")
     (cal-tex-cmd "\\hourbox"  (number-to-string afternoon))
     (cal-tex-arg height)
     (cal-tex-nl))))

871 872 873 874 875
(defun cal-tex-cursor-week-monday (&optional arg)
  "Make a buffer with LaTeX commands for a two-page one-week calendar.
It applies to the week that point is in, and starts on Monday.
Optional prefix argument specifies number of weeks.
Holidays are included if `cal-tex-holidays' is t."
Edward M. Reingold's avatar
Edward M. Reingold committed
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
  (interactive "P")
  (let* ((n (if arg arg 1))
         (date (calendar-gregorian-from-absolute
                (calendar-dayname-on-or-before
                 0
                 (calendar-absolute-from-gregorian
                  (calendar-cursor-to-date t))))))
    (cal-tex-preamble "11pt")
    (cal-tex-cmd "\\textwidth   6.5in")
    (cal-tex-cmd "\\textheight 10.5in")
    (cal-tex-cmd "\\oddsidemargin 0in")
    (cal-tex-cmd "\\evensidemargin 0in")
    (cal-tex-b-document)
    (calendar-for-loop i from 1 to n do
       (cal-tex-vspace "-1cm")
       (insert "\\noindent ")
       (cal-tex-weekly4-box (cal-tex-incr-date date) nil)
       (cal-tex-weekly4-box (cal-tex-incr-date date 4) nil)
       (cal-tex-nl ".2cm")
       (cal-tex-weekly4-box (cal-tex-incr-date date 2) nil)
       (cal-tex-weekly4-box (cal-tex-incr-date date 5) nil)
       (cal-tex-nl ".2cm")
       (cal-tex-weekly4-box (cal-tex-incr-date date 3) nil)
       (cal-tex-weekly4-box (cal-tex-incr-date date 6) t)
       (if (/= i n)
           (progn
             (run-hooks 'cal-tex-week-hook)
Edward M. Reingold's avatar
Edward M. Reingold committed
903
	     (setq date (cal-tex-incr-date date 7))
Edward M. Reingold's avatar
Edward M. Reingold committed
904 905 906 907 908
             (cal-tex-newpage))))
    (cal-tex-end-document)
    (run-hooks 'cal-tex-hook)))

(defun cal-tex-weekly4-box (date weekend)
Edward M. Reingold's avatar
Edward M. Reingold committed
909
  "Make one box for DATE, different if WEEKEND."
Edward M. Reingold's avatar
Edward M. Reingold committed
910 911 912 913 914 915 916 917 918 919 920 921 922
  (let* (
	(day (extract-calendar-day date))
	(month (extract-calendar-month date))
	(year (extract-calendar-year date))
	(dayname (calendar-day-name date))
	(date1 (cal-tex-incr-date date))
	(day1 (extract-calendar-day date1))
	(month1 (extract-calendar-month date1))
	(year1 (extract-calendar-year date1))
	(dayname1 (calendar-day-name date1))
	)
    (cal-tex-b-framebox "8cm" "l")
    (cal-tex-b-parbox "b" "7.5cm")
Edward M. Reingold's avatar
Edward M. Reingold committed
923
    (insert (format "{\\Large\\bf %s,} %s/%s/%s\\\\\n" dayname month day year))
Edward M. Reingold's avatar
Edward M. Reingold committed
924 925 926 927 928 929 930
    (cal-tex-rule "0pt" "7.5cm" ".5mm")
    (cal-tex-nl)
    (if (not weekend)
	(progn
	  (calendar-for-loop i from 8 to 12 do
	     (insert (format "{\\large\\sf %d}\\\\\n" i)))
	  (calendar-for-loop i from 1 to 5 do
931 932
	     (insert (format "{\\large\\sf %d}\\\\\n" 
			     (if cal-tex-24 (+ i 12) i))))))
Edward M. Reingold's avatar
Edward M. Reingold committed
933 934 935 936 937 938 939 940 941 942 943 944 945 946
    (cal-tex-nl ".5cm")
    (if weekend
	(progn
	  (cal-tex-vspace "1cm")
	  (insert "\\ \\vfill")
	  (insert (format "{\\Large\\bf %s,} %s/%s/%s\\\\\n" 
			  dayname1 month1 day1 year1))
	  (cal-tex-rule "0pt" "7.5cm" ".5mm")
	  (cal-tex-nl "1.5cm")
	  (cal-tex-vspace "1cm")))
     (cal-tex-e-parbox)
     (cal-tex-e-framebox)
     (cal-tex-hspace "1cm")))

947
(defun cal-tex-cursor-filofax-2week (&optional arg)
Edward M. Reingold's avatar
Edward M. Reingold committed
948 949
  "Two-weeks-at-a-glance Filofax style calendar for week indicated by cursor.
Optional prefix argument specifies number of weeks.
950
Diary entries are included if `cal-tex-diary' is t.
Edward M. Reingold's avatar
Edward M. Reingold committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
Holidays are included if `cal-tex-holidays' is t."
  (interactive "P")
  (let* ((n (if arg arg 1))
         (date (calendar-gregorian-from-absolute
                (calendar-dayname-on-or-before
                 calendar-week-start-day
                 (calendar-absolute-from-gregorian
                  (calendar-cursor-to-date t)))))
         (month (extract-calendar-month date))
         (year (extract-calendar-year date))
         (day (extract-calendar-day date))
         (holidays (if cal-tex-holidays
                       (cal-tex-list-holidays
                        (calendar-absolute-from-gregorian date)
                        (+ (* 7 n)
                           (calendar-absolute-from-gregorian date)))))
         (diary-list (if cal-tex-diary
                         (cal-tex-list-diary-entries
                          (calendar-absolute-from-gregorian
                           (list month 1 year))
                        (+ (* 7 n)
                           (calendar-absolute-from-gregorian date))))))
    (cal-tex-preamble "twoside")
    (cal-tex-cmd "\\textwidth 3.25in")
    (cal-tex-cmd "\\textheight 6.5in")
    (cal-tex-cmd "\\oddsidemargin 1.75in")
    (cal-tex-cmd "\\evensidemargin 1.5in")
    (cal-tex-cmd "\\topmargin 0pt")
    (cal-tex-cmd "\\headheight -0.875in")
    (cal-tex-cmd "\\headsep 0.125in")
    (cal-tex-cmd "\\footskip .125in")
    (insert "\\def\\righthead#1{\\hfill {\\normalsize \\bf #1}\\\\[-6pt]}
\\long\\def\\rightday#1#2#3#4#5{%
   \\rule{\\textwidth}{0.3pt}\\\\%
   \\hbox to \\textwidth{%
     \\vbox to 0.7in{%
          \\vspace*{2pt}%
          \\hbox to \\textwidth{\\small #5 \\hfill #1 {\\normalsize \\bf #2}}%
          \\hbox to \\textwidth{\\vbox {\\raggedleft \\footnotesize \\em #4}}%
          \\hbox to \\textwidth{\\vbox to 0pt {\\noindent \\footnotesize #3}}}}\\\\}
\\def\\lefthead#1{\\noindent {\\normalsize \\bf #1}\\hfill\\\\[-6pt]}
\\long\\def\\leftday#1#2#3#4#5{%
   \\rule{\\textwidth}{0.3pt}\\\\%
   \\hbox to \\textwidth{%
     \\vbox to 0.7in{%
          \\vspace*{2pt}%
          \\hbox to \\textwidth{\\noindent {\\normalsize \\bf #2} \\small #1 \\hfill #5}%
          \\hbox to \\textwidth{\\vbox {\\noindent \\footnotesize \\em #4}}%
          \\hbox to \\textwidth{\\vbox to 0pt {\\noindent \\footnotesize #3}}}}\\\\}
")
    (cal-tex-b-document)
    (cal-tex-cmd "\\pagestyle{empty}")
    (calendar-for-loop i from 1 to n do
       (if (= (mod i 2) 1)
           (insert "\\righthead")
         (insert "\\lefthead"))
       (cal-tex-arg
        (let ((d (cal-tex-incr-date date 6)))
          (if (= (extract-calendar-month date)
                 (extract-calendar-month d))
              (format "%s %s"
                      (calendar-month-name
                       (extract-calendar-month date))
                      (extract-calendar-year date))
            (if (=  (extract-calendar-year date)
                    (extract-calendar-year d))
                (format "%s---%s %s"
                        (calendar-month-name
                         (extract-calendar-month date))
                        (calendar-month-name
                         (extract-calendar-month d))
                        (extract-calendar-year date))
              (format "%s %s---%s %s"
                      (calendar-month-name
                       (extract-calendar-month date))
                      (extract-calendar-year date)
                      (calendar-month-name (extract-calendar-month d))
                      (extract-calendar-year d))))))
       (insert "%\n")
       (calendar-for-loop j from 1 to 7 do 
          (if (= (mod i 2) 1)
              (insert "\\rightday")
            (insert "\\leftday"))
          (cal-tex-arg (calendar-day-name date))
          (cal-tex-arg (int-to-string (extract-calendar-day date)))
          (cal-tex-arg (cal-tex-latexify-list diary-list date))
          (cal-tex-arg (cal-tex-latexify-list holidays date))
          (cal-tex-arg (eval cal-tex-daily-string))
          (insert "%\n")
          (setq date (cal-tex-incr-date date)))
       (if (/= i n)
           (progn
             (run-hooks 'cal-tex-week-hook)
             (cal-tex-newpage))))
    (cal-tex-end-document)
    (run-hooks 'cal-tex-hook)))

1048
(defun cal-tex-cursor-filofax-week (&optional arg)
Edward M. Reingold's avatar
Edward M. Reingold committed
1049 1050 1051
  "One-week-at-a-glance Filofax style calendar for week indicated by cursor.
Optional prefix argument specifies number of weeks.
Weeks start on Monday. 
1052
Diary entries are included if `cal-tex-diary' is t.
Edward M. Reingold's avatar
Edward M. Reingold committed
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
Holidays are included if `cal-tex-holidays' is t."
  (interactive "P")
  (let* ((n (if arg arg 1))
         (date (calendar-gregorian-from-absolute
                (calendar-dayname-on-or-before
                 1
                 (calendar-absolute-from-gregorian
                  (calendar-cursor-to-date t)))))
         (month (extract-calendar-month date))
         (year (extract-calendar-year date))
         (day (extract-calendar-day date))
         (holidays (if cal-tex-holidays
                       (cal-tex-list-holidays
                        (calendar-absolute-from-gregorian date)
                        (+ (* 7 n)
                           (calendar-absolute-from-gregorian date)))))
         (diary-list (if cal-tex-diary
                         (cal-tex-list-diary-entries
                          (calendar-absolute-from-gregorian
                           (list month 1 year))
                        (+ (* 7 n)
                           (calendar-absolute-from-gregorian date))))))
    (cal-tex-preamble "twoside")
    (cal-tex-cmd "\\textwidth 3.25in")
    (cal-tex-cmd "\\textheight 6.5in")
    (cal-tex-cmd "\\oddsidemargin 1.75in")
    (cal-tex-cmd "\\evensidemargin 1.5in")
    (cal-tex-cmd "\\topmargin 0pt")
    (cal-tex-cmd "\\headheight -0.875in")
    (cal-tex-cmd "\\headsep 0.125in")
    (cal-tex-cmd "\\footskip .125in")
    (insert "\\def\\righthead#1{\\hfill {\\normalsize \\bf #1}\\\\[-6pt]}
\\long\\def\\rightday#1#2#3#4#5{%
   \\rule{\\textwidth}{0.3pt}\\\\%
   \\hbox to \\textwidth{%
     \\vbox to 1.85in{%
          \\vspace*{2pt}%
          \\hbox to \\textwidth{\\small #5 \\hfill #1 {\\normalsize \\bf #2}}%
          \\hbox to \\textwidth{\\vbox {\\raggedleft \\footnotesize \\em #4}}%
          \\hbox to \\textwidth{\\vbox to 0pt {\\noindent \\footnotesize #3}}}}\\\\}
\\long\\def\\weekend#1#2#3#4#5{%
   \\rule{\\textwidth}{0.3pt}\\\\%
   \\hbox to \\textwidth{%
     \\vbox to .8in{%
          \\vspace*{2pt}%
          \\hbox to \\textwidth{\\small #5 \\hfill #1 {\\normalsize \\bf #2}}%
          \\hbox to \\textwidth{\\vbox {\\raggedleft \\footnotesize \\em #4}}%
          \\hbox to \\textwidth{\\vbox to 0pt {\\noindent \\footnotesize #3}}}}\\\\}
\\def\\lefthead#1{\\noindent {\\normalsize \\bf #1}\\hfill\\\\[-6pt]}
\\long\\def\\leftday#1#2#3#4#5{%
   \\rule{\\textwidth}{0.3pt}\\\\%
   \\hbox to \\textwidth{%
     \\vbox to 1.85in{%
          \\vspace*{2pt}%
          \\hbox to \\textwidth{\\noindent {\\normalsize \\bf #2} \\small #1 \\hfill #5}%
          \\hbox to \\textwidth{\\vbox {\\noindent \\footnotesize \\em #4}}%
          \\hbox to \\textwidth{\\vbox to 0pt {\\noindent \\footnotesize #3}}}}\\\\}
")
    (cal-tex-b-document)
    (cal-tex-cmd "\\pagestyle{empty}\\ ")
    (cal-tex-newpage)
    (calendar-for-loop i from 1 to n do
       (insert "\\lefthead")
       (cal-tex-arg
        (let ((d (cal-tex-incr-date date 2)))
          (if (= (extract-calendar-month date)
                 (extract-calendar-month d))
              (format "%s %s"
                      (calendar-month-name
                       (extract-calendar-month date))
                      (extract-calendar-year date))
            (if (=  (extract-calendar-year date)
                    (extract-calendar-year d))
                (format "%s---%s %s"
                        (calendar-month-name
                         (extract-calendar-month date))
                        (calendar-month-name
                         (extract-calendar-month d))
                        (extract-calendar-year date))
              (format "%s %s---%s %s"
                      (calendar-month-name
                       (extract-calendar-month date))
                      (extract-calendar-year date)
                      (calendar-month-name (extract-calendar-month d))
                      (extract-calendar-year d))))))
       (insert "%\n")
       (calendar-for-loop j from 1 to 3 do 
          (insert "\\leftday")
          (cal-tex-arg (calendar-day-name date))
          (cal-tex-arg (int-to-string (extract-calendar-day date)))
          (cal-tex-arg (cal-tex-latexify-list diary-list date))
          (cal-tex-arg (cal-tex-latexify-list holidays date))
          (cal-tex-arg (eval cal-tex-daily-string))
          (insert "%\n")
          (setq date (cal-tex-incr-date date)))
       (insert "\\noindent\\rule{\\textwidth}{0.3pt}\\\\%\n")
       (cal-tex-newpage)
       (insert "\\righthead")
       (cal-tex-arg
        (let ((d (cal-tex-incr-date date 3)))
          (if (= (extract-calendar-month date)
                 (extract-calendar-month d))
              (format "%s %s"
                      (calendar-month-name
                       (extract-calendar-month date))
                      (extract-calendar-year date))
            (if (=  (extract-calendar-year date)
                    (extract-calendar-year d))
                (format "%s---%s %s"
                        (calendar-month-name
                         (extract-calendar-month date))
                        (calendar-month-name
                         (extract-calendar-month d))
                        (extract-calendar-year date))
              (format "%s %s---%s %s"
                      (calendar-month-name
                       (extract-calendar-month date))
                      (extract-calendar-year date)
                      (calendar-month-name (extract-calendar-month d))
                      (extract-calendar-year d))))))
       (insert "%\n")
       (calendar-for-loop j from 1 to 2 do 
          (insert "\\rightday")
          (cal-tex-arg (calendar-day-name date))
          (cal-tex-arg (int-to-string (extract-calendar-day date)))
          (cal-tex-arg (cal-tex-latexify-list diary-list date))
          (cal-tex-arg (cal-tex-latexify-list holidays date))
          (cal-tex-arg (eval cal-tex-daily-string))
          (insert "%\n")
          (setq date (cal-tex-incr-date date)))
       (calendar-for-loop j from 1 to 2 do 
          (insert "\\weekend")
          (cal-tex-arg (calendar-day-name date))
          (cal-tex-arg (int-to-string (extract-calendar-day date)))
          (cal-tex-arg (cal-tex-latexify-list diary-list date))
          (cal-tex-arg (cal-tex-latexify-list holidays date))
          (cal-tex-arg (eval cal-tex-daily-string))
          (insert "%\n")
          (setq date (cal-tex-incr-date date)))
       (if (/= i n)
           (progn
             (run-hooks 'cal-tex-week-hook)
             (cal-tex-newpage))))
    (cal-tex-end-document)
    (run-hooks 'cal-tex-hook)))
1198 1199 1200 1201 1202

(defun cal-tex-cursor-filofax-daily (&optional arg)
  "Day-per-page Filofax style calendar for week indicated by cursor.
Optional prefix argument specifies number of weeks.  Weeks start on Monday. 
Diary entries are included if `cal-tex-diary' is t.
1203 1204
Holidays are included if `cal-tex-holidays' is t.
Pages are ruled if `cal-tex-rules' is t."
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
  (interactive "P")
  (let* ((n (if arg arg 1))
         (date (calendar-gregorian-from-absolute
                (calendar-dayname-on-or-before
                 1
                 (calendar-absolute-from-gregorian
                  (calendar-cursor-to-date t)))))
         (month (extract-calendar-month date))
         (year (extract-calendar-year date))
         (day (extract-calendar-day date))
         (holidays (if cal-tex-holidays
                       (cal-tex-list-holidays
                        (calendar-absolute-from-gregorian date)
                        (+ (* 7 n)
                           (calendar-absolute-from-gregorian date)))))
         (diary-list (if cal-tex-diary
                         (cal-tex-list-diary-entries
                          (calendar-absolute-from-gregorian
                           (list month 1 year))
1224 1225
			  (+ (* 7 n)
			     (calendar-absolute-from-gregorian date))))))
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
    (cal-tex-preamble "twoside")
    (cal-tex-cmd "\\textwidth 3.25in")
    (cal-tex-cmd "\\textheight 6.5in")
    (cal-tex-cmd "\\oddsidemargin 1.75in")
    (cal-tex-cmd "\\evensidemargin 1.5in")
    (cal-tex-cmd "\\topmargin 0pt")
    (cal-tex-cmd "\\headheight -0.875in")
    (cal-tex-cmd "\\headsep 0.125in")
    (cal-tex-cmd "\\footskip .125in")
    (insert "\\def\\righthead#1{\\hfill {\\normalsize \\bf #1}\\\\[-6pt]}
\\long\\def\\rightday#1#2#3{%
   \\rule{\\textwidth}{0.3pt}\\\\%
   \\hbox to \\textwidth{%
1239
     \\vbox {%
1240 1241 1242
          \\vspace*{2pt}%
          \\hbox to \\textwidth{\\hfill \\small #3 \\hfill}%
          \\hbox to \\textwidth{\\vbox {\\raggedleft \\em #2}}%
1243
          \\hbox to \\textwidth{\\vbox {\\noindent \\footnotesize #1}}}}}
1244 1245 1246
\\long\\def\\weekend#1#2#3{%
   \\rule{\\textwidth}{0.3pt}\\\\%
   \\hbox to \\textwidth{%
1247
     \\vbox {%
1248 1249 1250
          \\vspace*{2pt}%
          \\hbox to \\textwidth{\\hfill \\small #3 \\hfill}%
          \\hbox to \\textwidth{\\vbox {\\noindent \\em #2}}%
1251
          \\hbox to \\textwidth{\\vbox {\\noindent \\footnotesize #1}}}}}
1252 1253 1254 1255
\\def\\lefthead#1{\\noindent {\\normalsize \\bf #1}\\hfill\\\\[-6pt]}
\\long\\def\\leftday#1#2#3{%
   \\rule{\\textwidth}{0.3pt}\\\\%
   \\hbox to \\textwidth{%
1256
     \\vbox {%
1257 1258 1259
          \\vspace*{2pt}%
          \\hbox to \\textwidth{\\hfill \\small #3 \\hfill}%
          \\hbox to \\textwidth{\\vbox {\\noindent \\em #2}}%
1260 1261 1262 1263 1264
          \\hbox to \\textwidth{\\vbox {\\noindent \\footnotesize #1}}}}}
\\newbox\\LineBox
\\setbox\\LineBox=\\hbox to\\textwidth{%
\\vrule height.2in width0pt\\leaders\\hrule\\hfill}
\\def\\linesfill{\\par\\leaders\\copy\\LineBox\\vfill}
1265 1266 1267 1268 1269
")
    (cal-tex-b-document)
    (cal-tex-cmd "\\pagestyle{empty}")
    (calendar-for-loop i from 1 to n do
       (calendar-for-loop j from 1 to 5 do 
1270 1271 1272 1273 1274
	  (let ((odd (/= 0 (% j 2))))
	    (insert (if odd "\\righthead" "\\lefthead"))
	    (cal-tex-arg (calendar-date-string date))
	    (insert "%\n")
	    (insert (if odd "\\rightday"  "\\leftday")))
1275 1276 1277 1278 1279 1280 1281 1282 1283
	  (cal-tex-arg (cal-tex-latexify-list diary-list date))
	  (cal-tex-arg (cal-tex-latexify-list holidays date "\\\\" t))
	  (cal-tex-arg (eval cal-tex-daily-string))
	  (insert "%\n")
	  (if cal-tex-rules
	      (insert "\\linesfill\n")
	    (insert "\\vfill\\noindent\\rule{\\textwidth}{0.3pt}\\\\%\n"))
	  (cal-tex-newpage)
	  (setq date (cal-tex-incr-date date)))
1284 1285
       (insert "%\n")
       (calendar-for-loop j from 1 to 2 do 
1286
	  (insert "\\lefthead")
1287 1288 1289
          (cal-tex-arg (calendar-date-string date))
          (insert "\\weekend")
          (cal-tex-arg (cal-tex-latexify-list diary-list date))
1290
          (cal-tex-arg (cal-tex-latexify-list holidays date "\\\\" t))
1291 1292
          (cal-tex-arg (eval cal-tex-daily-string))
          (insert "%\n")
1293 1294 1295
          (if cal-tex-rules
              (insert "\\linesfill\n")
            (insert "\\vfill"))
1296
          (setq date (cal-tex-incr-date date)))
1297 1298
       (if (not cal-tex-rules)
	   (insert "\\noindent\\rule{\\textwidth}{0.3pt}\\\\%\n"))
1299 1300 1301 1302 1303 1304 1305 1306
       (if (/= i n)
           (progn
             (run-hooks 'cal-tex-week-hook)
             (cal-tex-newpage))))
    (cal-tex-end-document)
    (run-hooks 'cal-tex-hook)))


Edward M. Reingold's avatar
Edward M. Reingold committed
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
;;;
;;;  Daily calendars
;;;

(defun cal-tex-cursor-day (&optional arg)
  "Make a buffer with LaTeX commands for the day cursor is on.
Optional prefix argument specifies number of days."
  (interactive "P")
  (let ((n (if arg arg 1))
        (date (calendar-absolute-from-gregorian (calendar-cursor-to-date t))))
    (cal-tex-preamble "12pt")
    (cal-tex-cmd "\\textwidth       6.5in")
    (cal-tex-cmd "\\textheight 10.5in")
    (cal-tex-b-document)
    (cal-tex-cmd "\\pagestyle{empty}")
    (calendar-for-loop i from 1 to n do
       (cal-tex-vspace "-1.7in")
       (cal-tex-daily-page (calendar-gregorian-from-absolute date))
       (setq date (1+ date))
       (if (/= i n)
          (progn
            (cal-tex-newpage)
            (run-hooks 'cal-tex-daily-hook))))
    (cal-tex-end-document)
    (run-hooks 'cal-tex-hook)))

(defun cal-tex-daily-page (date)
  "Make a calendar page for Gregorian DATE on 8.5 by 11 paper."
  (let* ((hour)
         (month-name (calendar-month-name (extract-calendar-month date))))
    (cal-tex-banner "cal-tex-daily-page")
    (cal-tex-b-makebox "4cm" "l")
    (cal-tex-b-parbox "b" "3.8cm")
    (cal-tex-rule "0mm" "0mm" "2cm")
    (cal-tex-Huge (number-to-string (extract-calendar-day date)))
    (cal-tex-nl ".5cm")
    (cal-tex-bf month-name )
    (cal-tex-e-parbox)
    (cal-tex-hspace "1cm")
    (cal-tex-scriptsize (eval cal-tex-daily-string))
    (cal-tex-hspace "3.5cm")
    (cal-tex-e-makebox)
    (cal-tex-hfill)
    (cal-tex-b-makebox "4cm" "r")
    (cal-tex-bf (calendar-day-name date))
    (cal-tex-e-makebox)
    (cal-tex-nl)
    (cal-tex-hspace ".4cm")
    (cal-tex-rule "0mm" "16.1cm" "1mm")
    (cal-tex-nl ".1cm")
    (calendar-for-loop i from cal-tex-daily-start to cal-tex-daily-end do
       (cal-tex-cmd "\\noindent")
       (setq hour (if cal-tex-24
                      i
                    (mod i 12)))
       (if (= 0 hour) (setq hour 12))
       (cal-tex-b-makebox "1cm" "c")
       (cal-tex-arg (number-to-string hour))
       (cal-tex-e-makebox)
       (cal-tex-rule "0mm" "15.5cm" ".2mm")
       (cal-tex-nl ".2cm")
       (cal-tex-b-makebox "1cm" "c")
       (cal-tex-arg "$\\diamond$" )
       (cal-tex-e-makebox)
       (cal-tex-rule "0mm" "15.5cm" ".2mm")
       (cal-tex-nl ".2cm"))
    (cal-tex-hfill)
    (insert (cal-tex-mini-calendar
             (extract-calendar-month (cal-tex-previous-month date))
             (extract-calendar-year (cal-tex-previous-month date))
             "lastmonth" "1.1in" "1in"))
    (insert (cal-tex-mini-calendar
             (extract-calendar-month date)
             (extract-calendar-year date)
             "thismonth" "1.1in" "1in"))
    (insert (cal-tex-mini-calendar
             (extract-calendar-month (cal-tex-next-month date))
             (extract-calendar-year (cal-tex-next-month date))
             "nextmonth" "1.1in" "1in"))
    (insert "\\hbox to \\textwidth{")
    (cal-tex-hfill)
    (insert "\\lastmonth")
    (cal-tex-hfill)
    (insert "\\thismonth")
    (cal-tex-hfill)
    (insert "\\nextmonth")
    (cal-tex-hfill)
    (insert "}")
    (cal-tex-banner "end of cal-tex-daily-page")))
    
;;;
;;;  Mini calendars
;;;

1401
(defun cal-tex-mini-calendar (month year name width height &optional ptsize colsep)
Edward M. Reingold's avatar
Edward M. Reingold committed
1402
  "Produce mini-calendar for MONTH, YEAR in macro NAME with WIDTH and HEIGHT.
1403 1404
Optional PTSIZE gives the point ptsize; scriptsize is the default.  Optional
COLSEP gives the column separation; 1mm is the default."
Edward M. Reingold's avatar
Edward M. Reingold committed
1405 1406 1407 1408 1409
  (let* ((blank-days;; at start of month
          (mod
           (- (calendar-day-of-week (list month 1 year))
              calendar-week-start-day)
           7))
1410 1411
          (last (calendar-last-day-of-month month year))
         (colsep (if colsep colsep "1mm"))
Edward M. Reingold's avatar
Edward M. Reingold committed
1412 1413 1414 1415
         (str (concat "\\def\\" name "{\\hbox to" width "{%\n"
                      "\\vbox to" height "{%\n"
                      "\\vfil  \\hbox to" width "{%\n"
                      "\\hfil\\"
1416
                      (if ptsize ptsize "scriptsize")
Edward M. Reingold's avatar
Edward M. Reingold committed
1417
                      "\\begin{tabular}"
1418 1419 1420 1421
                      "{@{\\hspace{0mm}}r@{\\hspace{" colsep
                      "}}r@{\\hspace{" colsep "}}r@{\\hspace{" colsep
                      "}}r@{\\hspace{" colsep "}}r@{\\hspace{" colsep
                      "}}r@{\\hspace{" colsep "}}r@{\\hspace{0mm}}}%\n"
Edward M. Reingold's avatar
Edward M. Reingold committed
1422 1423 1424 1425
                      "\\multicolumn{7}{c}{"
                      (calendar-month-name month)
                      " "
                      (int-to-string year)
1426
                      "}\\\\[1mm]\n")))
Edward M. Reingold's avatar
Edward M. Reingold committed
1427 1428 1429 1430 1431 1432 1433
    (calendar-for-loop i from 0 to 6 do
      (setq str (concat str
                        (substring (aref calendar-day-name-array 
                                         (mod (+ calendar-week-start-day i) 7))
                                   0 2)
                        (if (/= i 6)
                            " & "
1434
                          "\\\\[0.7mm]\n"))))
Edward M. Reingold's avatar
Edward M. Reingold committed
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
    (calendar-for-loop i from 1 to blank-days do
      (setq str (concat str " & ")))
    (calendar-for-loop i from 1 to last do
      (setq str (concat str (int-to-string i)))
      (setq str (concat str (if (zerop (mod (+ i blank-days) 7))
                                (if (/= i last) "\\\\[0.5mm]\n" "")
                              " & "))))
    (setq str (concat str "\n\\end{tabular}\\hfil}\\vfil}}}%\n"))
   str))

;;;
;;;  Various calendar functions
;;;

(defun cal-tex-incr-date (date &optional n)
  "The date of the day following DATE.
If optional N is given, the date of N days after DATE."
  (calendar-gregorian-from-absolute
   (+ (if n n 1) (calendar-absolute-from-gregorian date))))

1455
(defun cal-tex-latexify-list (date-list date &optional separator final-separator)
Edward M. Reingold's avatar
Edward M. Reingold committed
1456
  "Return string with concatenated, LaTeXified entries in DATE_LIST for DATE.
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
Use double backslash as a separator unless optional SEPARATOR is given.
If resulting string is not empty, put separator at end if optional
FINAL-SEPARATOR is t."
  (let* ((sep (if separator separator "\\\\"))
         (result
          (mapconcat '(lambda (x) (cal-tex-LaTeXify-string  x))
                     (let ((result)
                           (p date-list))
                       (while p
                         (and (car (car p))
                              (calendar-date-equal date (car (car p)))
                              (setq result (cons (car (cdr (car p))) result)))
                         (setq p (cdr p)))
                       (reverse result))
                     sep)))
    (if (and final-separator (not (string-equal result "")))
          (concat result sep)
        result)))
Edward M. Reingold's avatar
Edward M. Reingold committed
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735

(defun cal-tex-previous-month (date)
  "Return the date of the first day in the month previous to DATE."
  (let* ((month (extract-calendar-month date))
         (year (extract-calendar-year date)))
    (increment-calendar-month month year -1)
    (list month 1 year)))

(defun cal-tex-next-month (date)
  "Return the date of the first day in the  month following DATE."
  (let* ((month (extract-calendar-month date))
         (year (extract-calendar-year date)))
    (increment-calendar-month month year 1)
    (list month 1 year)))

;;;
;;;  LaTeX Code
;;;

(defun cal-tex-end-document ()
  "Finish the LaTeX document.
Insert the trailer to LaTeX document, pop to LaTeX buffer, add 
informative header, and run HOOK."
  (cal-tex-e-document)
  (latex-mode)
  (pop-to-buffer cal-tex-buffer)
  (goto-char (point-min))    
  (cal-tex-comment "       This buffer was produced by cal-tex.el.")
  (cal-tex-comment "       To print a calendar, type")
  (cal-tex-comment "          M-x tex-buffer RET")
  (cal-tex-comment "          M-x tex-print  RET")
  (goto-char (point-min)))

(defun cal-tex-insert-preamble (weeks landscape size &optional append)
  "Initialize the output buffer.
Select the output buffer, and insert the preamble for a calendar of 
WEEKS weeks.  Insert code for landscape mode if LANDSCAPE is true. 
Use pointsize SIZE.  Optional argument APPEND, if t, means add to end of
without erasing current contents."
  (let ((width "18cm")
        (height "24cm"))
    (if landscape
        (progn
          (setq width "24cm")
          (setq height "18cm")))
    (if (not append)
        (progn
          (cal-tex-preamble size)
          (if (not landscape) 
            (progn
              (cal-tex-cmd "\\oddsidemargin -1.75cm")
              (cal-tex-cmd "\\def\\holidaymult{.06}"))
            (cal-tex-cmd "\\special{landscape}")
            (cal-tex-cmd "\\textwidth  9.5in")
            (cal-tex-cmd "\\textheight 7in")
            (cal-tex-comment)
            (cal-tex-cmd "\\def\\holidaymult{.08}"))
          (cal-tex-cmd  cal-tex-caldate)
          (cal-tex-cmd  cal-tex-myday)
          (cal-tex-b-document)
          (cal-tex-cmd "\\pagestyle{empty}")))
    (cal-tex-cmd "\\setlength{\\cellwidth}" width)
    (insert (format "\\setlength{\\cellwidth}{%f\\cellwidth}\n"
                    (/ 1.1 (length cal-tex-which-days))))
    (cal-tex-cmd "\\setlength{\\cellheight}" height)
    (insert (format "\\setlength{\\cellheight}{%f\\cellheight}\n"
                    (/ 1.0 weeks)))              
    (cal-tex-cmd "\\ \\par")
    (cal-tex-vspace "-3cm")))

(defvar cal-tex-LaTeX-subst-list
  '(("\"". "``")
    ("\"". "''");; Quote changes meaning when list is reversed.
    ("@" . "\\verb|@|") 
    ("&" . "\\&") 
    ("%" . "\\%") 
    ("$" . "\\$")
    ("#" . "\\#")
    ("_" . "\\_")
    ("{" . "\\{")
    ("}" . "\\}")
    ("<" . "$<$")
    (">" . "$>$")
    ("\n" . "\\ \\\\"))  ;\\ needed for e.g \begin{center}\n AA\end{center}
  "List of symbols and their replacements.")

(defun cal-tex-LaTeXify-string (string)
  "Protect special characters in STRING from LaTeX."
  (if (not string)
      ""
    (let ((head "")
          (tail string)
          (list cal-tex-LaTeX-subst-list))
      (while (not (string-equal tail ""))
        (let* ((ch (substring tail 0 1))
               (pair (assoc ch list)))
          (if (and pair (string-equal ch "\""))
              (setq list (reverse list)));; Quote changes meaning each time.
          (setq tail (substring tail 1))
          (setq head (concat head (if pair (cdr pair) ch)))))
      head)))

(defun cal-tex-hfill () "Insert hfill." (insert "\\hfill"))

(defun cal-tex-newpage () "Insert newpage." (insert "\\newpage%\n"))

(defun cal-tex-noindent () "Insert noindent." (insert "\\noindent"))

(defun cal-tex-vspace (space)
  "Insert vspace command to move SPACE vertically."
  (insert "\\vspace*{" space "}")
  (cal-tex-comment))

(defun cal-tex-hspace (space)
  "Insert hspace command to move SPACE horizontally."
  (insert "\\hspace*{" space "}")
  (cal-tex-comment))

(defun cal-tex-comment (&optional comment)
  "Insert % at end of line, include COMMENT if present, and move
   to next line."
  (insert "% ")
  (if comment
      (insert comment))
  (insert "\n"))

(defun cal-tex-banner (comment)
  "Insert the COMMENT separated by blank lines."
  (cal-tex-comment)
  (cal-tex-comment)
  (cal-tex-comment (concat "\t\t\t" comment))
  (cal-tex-comment))


(defun cal-tex-nl (&optional skip comment)
  "End a line with \\.  If SKIP, then add that much spacing.
   Add COMMENT if present"
  (insert "\\\\")       
  (if skip 
      (insert "[" skip "]"))
  (cal-tex-comment comment))
    
(defun cal-tex-arg (&optional text)
  "Insert optional TEXT surrounded by braces."
  (insert "{")
  (if text (insert text))
  (insert "}"))

(defun cal-tex-cmd (cmd &optional arg)
  "Insert LaTeX CMD, with optional ARG, and end with %"
  (insert cmd)
  (cal-tex-arg arg)
  (cal-tex-comment))

;;;
;;;   Environments
;;;

(defun cal-tex-b-document ()
  "Insert beginning of document."
  (cal-tex-cmd "\\begin{document}"))

(defun cal-tex-e-document ()
  "Insert end of document."
  (cal-tex-cmd "\\end{document}"))

(defun cal-tex-b-center ()
  "Insert beginning of centered block."
  (cal-tex-cmd "\\begin{center}"))

(defun cal-tex-e-center ()
  "Insert end of centered block."
  (cal-tex-comment)
  (cal-tex-cmd "\\end{center}"))


;;;
;;;  Boxes
;;;


(defun cal-tex-b-parbox (position width)
  "Insert parbox with parameters POSITION and WIDTH."
  (insert "\\parbox[" position "]{" width "}{")
  (cal-tex-comment))

(defun cal-tex-e-parbox (&optional height)
  "Insert end of parbox. Force it to be a given HEIGHT."
  (cal-tex-comment)
  (if height
      (cal-tex-rule "0mm" "0mm" height))
  (insert "}")
  (cal-tex-comment "end parbox"))

(defun cal-tex-b-framebox (