This file is indexed.

/usr/share/emacs/site-lisp/picolisp/picolisp-wiki-mode.el is in picolisp 3.1.5.2-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 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
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 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
 262
 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
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 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
 610
 611
 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
 661
 662
 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
 740
 741
 742
 743
 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
 822
 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
 871
 872
 873
 874
 875
 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
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 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
1048
1049
1050
1051
1052
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
;;; picolisp-wiki-mode.el --- Emacs Major mode for PicoLisp-Wiki formatted text files

;; Copyright (C) 2012-13 Thorsten Jolitz <tjolitz@gmail.com>

;; Author: Thorsten Jolitz <tjolitz@gmail.com>
;; Maintainer: Thorsten Jolitz <tjolitz@gmail.com>
;; Created: September 01, 2012
;; Version: 1.0
;; Keywords: PicoLisp, wiki
;; URL: http://picolisp.com/5000/!wiki?home

;; This file is not part of GNU Emacs.

;; This program 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.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; picolisp-wiki-mode is a major mode for editing text files for
;; PicoLisp-Wiki in GNU Emacs. picolisp-wiki-mode is free software,
;; licensed under the GNU GPL.

;;; Dependencies:

;; picolisp-wiki-mode requires easymenu, a standard package since GNU Emacs
;; 19 and XEmacs 19, which provides a uniform interface for creating
;; menus in GNU Emacs and XEmacs.

;;; Installation:

;; Make sure to place `picolisp-wiki-mode.el` somewhere in the
;; load-path and add the following lines to your `.emacs` file to
;; associate picolisp-wiki-mode with `.text` files:

    (autoload 'picolisp-wiki-mode "picolisp-wiki-mode"
       "Major mode for editing Picolisp-Wiki files" t)
    (setq auto-mode-alist
       ;; (cons '("\\.text" . picolisp-wiki-mode) auto-mode-alist))
       (cons '("\\.plw" . picolisp-wiki-mode) auto-mode-alist))

;; There is no consensus on an official file extension so change `.text` to
;; `.plw`, `.lw`, `.lwik`, or whatever you call your picolisp-wiki files.

;;; Customization:

;; Although no configuration is *necessary* there are a few things
;; that can be customized.  The `M-x customize-mode` command
;; provides an interface to all of the possible customizations.

;; Usage:

;; Keybindings for inserting are grouped by prefixes based on their
;; function. For example, commands inserting links and lists begin
;; with `C-c C-l`, those inserting floating content with `C-c C-f`,
;; all other inserting commands with `C-c C-c`. The commands in each
;; group are described below. You can obtain a list of all keybindings
;; by pressing `C-c C-h`.

;;     ;; Element insertion
;;     "\C-c\C-l n" Insert Internal Link
;;     "\C-c\C-l x" Insert External Link
;;     "\C-c\C-l u" Insert Unordered List
;;     "\C-c\C-l o" Insert Ordered List
;;     "\C-c\C-l i" Insert List Item
;;     "\C-c\C-f l" Insert Left-Floating-Content
;;     "\C-c\C-f n" Insert Non-Floating Content
;;     "\C-c\C-f r" Insert Right-Floating-Content
;;     "\C-c\C-c k" Insert Line Breaks
;;     "\C-c\C-c 1" Insert Header 1
;;     "\C-c\C-c 2" Insert Header 2
;;     "\C-c\C-c 3" Insert Header 3
;;     "\C-c\C-c 4" Insert Header 4
;;     "\C-c\C-c 5" Insert Header 5
;;     "\C-c\C-c 6" Insert Header 6
;;     "\C-c\C-c b" Insert Bold
;;     "\C-c\C-c i" Insert Italic
;;     "\C-c\C-c u" Insert Underlined
;;     "\C-c\C-c p" Insert Pre Block
;;     "\C-c\C-c c" Insert Comment
;;     "\C-c\C-c -" Insert Horizontal Rule (hr)
;;     ;; Visibility cycling
;;     "<tab>" Picolisp Wiki Cycle
;;     "<S-iso-lefttab>" Picolisp Wiki Shifttab
;;     ;; Header navigation
;;     "C-M-n" Outline Next Visible Heading
;;     "C-M-p" Outline Previous Visible Heading
;;     "C-M-f" Outline Forward Same Level
;;     "C-M-b" Outline Backward Same Level
;;     "C-M-u" Outline Up Heading

;; Many of the commands described above behave differently depending on
;; whether Transient Mark mode is enabled or not.  When it makes sense,
;; if Transient Mark mode is on and a region is active, the command
;; applies to the text in the region (e.g., `C-c C-c b` makes the region
;; bold).  For users who prefer to work outside of Transient Mark mode,
;; in Emacs 22 it can be enabled temporarily by pressing `C-SPC C-SPC`.
;;
;; picolisp-wiki-mode supports outline-minor-mode as well as
;; org-mode-style visibility cycling for PicoLisp-Wikli-style headers.
;; There are two types of visibility cycling: Pressing `S-TAB` cycles
;; globally between the table of contents view (headers only), outline
;; view (top-level headers only), and the full document view. Pressing
;; `TAB` while the point is at a header will cycle through levels of
;; visibility for the subtree: completely folded, visible children,
;; and fully visible. 

;;   * Outline Navigation:
;;
;;     Navigation between headings is possible using `outline-mode'.
;;     Use `C-M-n` and `C-M-p` to move between the next and previous
;;     visible headings.  Similarly, `C-M-f` and `C-M-b` move to the
;;     next and previous visible headings at the same level as the one
;;     at the point.  Finally, `C-M-u` will move up to a lower-level
;;     (more inclusive) visible heading.
;;
;; FIXME: different headers levels are not yet recognized by the outine
;; commands.


;;; Acknowledgments:

;; picolisp-wiki-mode is based on markdown.el (available from ELPA).
;; It has benefited greatly from the efforts of the following people:
;;
;;   * Thorsten Jolitz <tjolitz [AT] gmail [DOT] com>
;;   * Doug Lewan <dougl [@] shubertticketing [DOT] com>
;;; Bugs:

;; picolisp-wiki-mode is developed and tested primarily using GNU
;; Emacs 24, compatibility with earlier Emacsen is no priority. For
;; bugs and todo's, see the HISTORY.org file in the github-repo
;; (https://github.com/tj64/picolisp-wiki-mode).
;;
;; If you find any bugs in picolisp-wiki-mode, please construct a test case
;; or a patch and email me at <tjolitz@gmail.com>.

;;; History:

;; picolisp-wiki-mode was written and is maintained by Thorsten
;; Joltiz. The first version (0.9) was released on Sept 01, 2012. For
;; further information see the HISTORY.org file in the github-repo
;; (https://github.com/tj64/picolisp-wiki-mode).


;;; Code:

(require 'easymenu)
(require 'outline)
(require 'cl)

;;; Constants =================================================================

(defconst picolisp-wiki-mode-version "1.0"
  "Picolisp-Wiki mode version number.")

;;; Customizable variables ====================================================

(defvar picolisp-wiki-mode-hook nil
  "Hook runs when Picolisp-Wiki mode is loaded.")

(defgroup picolisp-wiki nil
  "Major mode for editing text files in Picolisp-Wiki format."
  :prefix "picolisp-wiki-"
  :group 'wp
  :link '(url-link "http://picolisp.com/5000/!wiki?homef"))


(defcustom picolisp-wiki-hr-string "------------------------------------"
  "String to use for horizonal rules."
  :group 'picolisp-wiki
  :type 'string)

(defcustom picolisp-wiki-uri-types
  '("acap" "cid" "data" "dav" "fax" "file" "ftp" "gopher" "http" "https"
    "imap" "ldap" "mailto" "mid" "modem" "news" "nfs" "nntp" "pop" "prospero"
    "rtsp" "service" "sip" "tel" "telnet" "tip" "urn" "vemmi" "wais")
  "Link types for syntax highlighting of URIs."
  :group 'picolisp-wiki
  :type 'list)


(defcustom picolisp-wiki-link-space-sub-char
  "_"
  "Character to use instead of spaces when mapping wiki links to filenames."
  :group 'picolisp-wiki
  :type 'string)


;;; Font lock =================================================================

(require 'font-lock)

(defvar picolisp-wiki-starting-brace-face
  'picolisp-wiki-starting-brace-face
  "Face name to use for starting braces.")

(defvar picolisp-wiki-closing-brace-face
  'picolisp-wiki-closing-brace-face
  "Face name to use for closing braces.")

(defvar picolisp-wiki-line-break-face 'picolisp-wiki-line-break-face
  "Face name to use for line breaks.")

(defvar picolisp-wiki-italic-face 'picolisp-wiki-italic-face
  "Face name to use for italic text.")

(defvar picolisp-wiki-bold-face 'picolisp-wiki-bold-face
  "Face name to use for bold text.")

(defvar picolisp-wiki-underlined-face 'picolisp-wiki-underlined-face
  "Face name to use for underlined text.")

(defvar picolisp-wiki-header-face 'picolisp-wiki-header-face
  "Face name to use as a base for headers.")

(defvar picolisp-wiki-header-face-1 'picolisp-wiki-header-face-1
  "Face name to use for level-1 headers.")

(defvar picolisp-wiki-header-face-2 'picolisp-wiki-header-face-2
  "Face name to use for level-2 headers.")

(defvar picolisp-wiki-header-face-3 'picolisp-wiki-header-face-3
  "Face name to use for level-3 headers.")

(defvar picolisp-wiki-header-face-4 'picolisp-wiki-header-face-4
  "Face name to use for level-4 headers.")

(defvar picolisp-wiki-header-face-5 'picolisp-wiki-header-face-5
  "Face name to use for level-5 headers.")

(defvar picolisp-wiki-header-face-6 'picolisp-wiki-header-face-6
  "Face name to use for level-6 headers.")

(defvar picolisp-wiki-list-item-face 'picolisp-wiki-list-item-face
  "Face name to use for list markers.")

(defvar picolisp-wiki-left-floating-content-face
  'picolisp-wiki-left-floating-content-face
  "Face name to use for left floating content.")

(defvar picolisp-wiki-non-floating-content-face
  'picolisp-wiki-non-floating-content-face
  "Face name to use for non floating content.")

(defvar picolisp-wiki-right-floating-content-face
  'picolisp-wiki-right-floating-content-face
  "Face name to use for right floating content.")

(defvar picolisp-wiki-pre-face 'picolisp-wiki-pre-face
  "Face name to use for preformatted text.")

(defvar picolisp-wiki-link-label-face 'picolisp-wiki-link-label-face
  "Face name to use for link labels.")

(defvar picolisp-wiki-url-face 'picolisp-wiki-url-face
  "Face name to use for URLs.")

(defvar picolisp-wiki-link-title-face 'picolisp-wiki-link-title-face
  "Face name to use for reference link titles.")

(defvar picolisp-wiki-comment-face 'picolisp-wiki-comment-face
  "Face name to use for HTML comments.")



;; FACE definitions

(defgroup picolisp-wiki-faces nil
  "Faces used in Picolisp-Wiki Mode"
  :group 'picolisp-wiki
  :group 'faces)

(defface picolisp-wiki-hr-face
  '((t (:inherit font-lock-comment-delimiter-face)))
  "Face for starting braces."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-starting-brace-face
  '((t (:inherit font-lock-comment-delimiter-face)))
  "Face for starting braces."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-closing-brace-face
  '((t (:inherit font-lock-comment-delimiter-face)))
  "Face for closing braces."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-italic-face
  '((t (:inherit font-lock-negation-char-face :slant italic)))
  "Face for italic text."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-bold-face
  '((t (:inherit font-lock-negation-char-face :weight bold)))
  "Face for bold text."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-underlined-face
  '((t (:inherit font-lock-negation-char-face :underline t)))
  "Face for underlined text."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-line-break-face
  '((t (:inherit font-lock-warning-face)))
  "Face for underlined text."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-header-face
  '((t (:inherit font-lock-function-name-face :weight bold)))
  "Base face for headers."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-header-face-1
  '((t (:inherit picolisp-wiki-header-face)))
  "Face for level-1 headers."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-header-face-2
  '((t (:inherit picolisp-wiki-header-face)))
  "Face for level-2 headers."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-header-face-3
  '((t (:inherit picolisp-wiki-header-face)))
  "Face for level-3 headers."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-header-face-4
  '((t (:inherit picolisp-wiki-header-face)))
  "Face for level-4 headers."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-header-face-5
  '((t (:inherit picolisp-wiki-header-face)))
  "Face for level-5 headers."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-header-face-6
  '((t (:inherit picolisp-wiki-header-face)))
  "Face for level-6 headers."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-list-item-face
  '((t (:inherit font-lock-string-face)))
  "Face for list item markers."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-pre-face
  '((t (:inherit font-lock-constant-face)))
  ;; '((t (:inherit font-lock-string-face)))
  "Face for preformatted text."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-internal-link-face
  '((t (:inherit font-lock-keyword-face)))
  "Face for internal links."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-external-link-face
  '((t (:inherit font-lock-keyword-face)))
  "Face for external links."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-url-face
  '((t (:inherit font-lock-string-face)))
  "Face for URLs."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-link-label-face
  '((t (:inherit font-lock-keyword-face)))
  "Face for reference link titles."
  :group 'picolisp-wiki-faces)

(defface picolisp-wiki-comment-face
  '((t (:inherit font-lock-comment-face)))
  "Face for HTML comments."
  :group 'picolisp-wiki-faces)


;; REGEXP
;; FIXME consider linebreaks in pattern

;; [start] regexp by Doug Lewan (Newsgroups: gmane.emacs.help)

(defconst picolisp-wiki-regex-plain-text 
  (concat "\\([[:space:]]*[^}]+\\)[[:space:]]*" ; Matches "123$%^ Чебурашка &*(0-="
	  )
  "Regular expression defining what 'plain text' is.")

(defconst  picolisp-wiki-regex-bold-text
  (concat "\\(!{\\)"
	  picolisp-wiki-regex-plain-text
	  "\\(}\\)")
  "Regular expression defining what 'bold text' is.")

(defconst picolisp-wiki-regex-text
  (concat "\\("
	  picolisp-wiki-regex-plain-text
	  "\\|"
	  picolisp-wiki-regex-bold-text
	  "\\)")
  "Regular expression defining what 'text'.
Text is a mix of plain text and bold text.")

(defconst picolisp-wiki-regex-list-item-text
  (concat "\\(-{\\)"
	  picolisp-wiki-regex-text "+"
	  "\\(}\\)")
  "Regular expression defining what a 'list item' is.")

;; [end] regexp by Doug Lewan (Newsgroups: gmane.emacs.help)

;; [start] testcode for regexp by Doug Lewan

;; ;; 
;; ;; Sunny day test code
;; ;; 
;; (defconst test-plain-text (list "foo"
;; 				"foo bar "
;; 				"	foo bar baz bat"
;; 				" --- 123$%^ Чебурашка &*(0-= --- "))
;; (defconst test-bold-text (mapcar (lambda (text)
;; 				   (concat "!{" text "}"))
;; 				 test-plain-text))
;; (defconst test-list-item-text (mapcar (lambda (list-text)
;; 				   (concat "-{" list-text "}"))
;; 				 (append test-plain-text test-bold-text)))

;; (mapc (lambda (test-spec)
;; 	(let ((re (car test-spec))
;; 	      (test-data (cdr test-spec)))
;; 	  (mapc (lambda (item)
;; 		  (if (string-match re item)
;; 		      (message "PASS -- [[%s]] matches [[%s]]" re item)
;; 		    (message "FAIL -- [[%s]] DIDN'T match [[%s]]" re item))
;; 		  (sit-for 1))
;; 		test-data)))
;;       (list (cons picolisp-wiki-regex-plain-text test-plain-text)
;; 	    (cons picolisp-wiki-regex-bold-text test-bold-text)
;; 	    (cons picolisp-wiki-regex-list-item-text test-list-item-text)))

;; [end] testcode testcode for regexp by Doug Lewan

(defconst picolisp-wiki-regex-internal-link
  "\\(={\\)\\([^ ]+\\)\\( \\)\\(.*\\)\\(}\\)"
  "Regular expression for an internal link.")

(defconst picolisp-wiki-regex-external-link
   "\\(\\^{\\)\\([^ ]+\\)\\( \\)\\(.*\\)\\(}\\)"
  "Regular expression for an external link.")

(defconst picolisp-wiki-regex-comment
  "\\(#{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for an external link.")

(defconst picolisp-wiki-regex-header-1
  "\\(1{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for level 1 headers.")

(defconst picolisp-wiki-regex-header-2
  "\\(2{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for level 2 headers.")

(defconst picolisp-wiki-regex-header-3
  "\\(3{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for level 3 headers.")

(defconst picolisp-wiki-regex-header-4
  "\\(4{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for level 4 headers.")

(defconst picolisp-wiki-regex-header-5
  "\\(5{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for level 5 headers.")

(defconst picolisp-wiki-regex-header-6
  "\\(6{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for level 6 headers.")

(defconst picolisp-wiki-regex-hr
  "\\(--\\)+\\(---\\)*$"
  "Regular expression for matching Picolisp-Wiki horizontal rules.")

(defconst picolisp-wiki-regex-left-floating-content
  "\\(<{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for matching left-floating-content.")

(defconst picolisp-wiki-regex-non-floating-content
  "\\(@{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for matching non-floating-content.")

(defconst picolisp-wiki-regex-right-floating-content
  "\\(>{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for matching right-floating-content.")

(defconst picolisp-wiki-regex-pre-block
   "\\(:{\\)\\([ 	
]*[^}]+\\)\\(}\\)"

;;  "\\(:{\\)\\([ \t\n]*[^}]+\\)\\(}\\)"

;;   "\\(:{\\)\\([ 	
;; ][^}]+\\)\\(}\\)"
  "Regular expression for matching preformatted text sections.")

;; (defconst picolisp-wiki-regex-unordered-list
;;   "\\(^[\\t ]*\\*{\\)\\([ 	]*[ 
;; ]+\\)\\(-{.*}[ 	
;; ]+\\)\\{1,\\}\\(}\\)"
;;   "Regular expression for matching unordered list markers.")

;; (defconst picolisp-wiki-regex-ordered-list
;;   "\\(^[\\t ]*\\+{\\)\\([ 	]*[ 
;; ]+\\)\\(-{.*}[ 	
;; ]+\\)\\{1,\\}\\(}\\)"
;;   "Regular expression for matching ordered list markers.")

;; (defconst picolisp-wiki-regex-unordered-list-start
;;   "\\(^\\*{\\)\\([ \t\n]*$\\)"
;;   "Regular expression for matching the start of an unordered list.")

;; (defconst picolisp-wiki-regex-ordered-list-start
;;   "\\(^\\+{\\)\\([ \t\n]*$\\)"
;;   "Regular expression for matching the start of an ordered list.")

(defconst picolisp-wiki-regex-starting-brace
  (concat "\\(\\*\\|\\+\\|-\\|&\\|/\\|_\\|\\^\\|"
          "<\\|>\\|@\\|!\\|=\\|:\\|#\\|1\\|2\\|"
          "3\\|4\\|5\\|6\\)\\({\\)")
  "Regular expression for matching a starting brace.")

(defconst picolisp-wiki-regex-closing-brace
  "\\([^\\]\\)\\(}\\)"
  "Regular expression for matching a closing brace.")

(defconst picolisp-wiki-regex-list-item
  "\\(-{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for matching a list item.")

(defconst picolisp-wiki-regex-bold
  "\\(!{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for matching bold text.")

(defconst picolisp-wiki-regex-italic
  "\\(/{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for matching italic text.")

(defconst picolisp-wiki-regex-underlined
   "\\(_{\\)\\([ 	
]*[^}]+\\)\\(}\\)"
  "Regular expression for matching underlined text.")

(defconst picolisp-wiki-regex-line-break
  "\\(&{\\)\\([-]?[0-9]\\)\\(}\\)"
  "Regular expression for matching line breaks.")

(defconst picolisp-wiki-regex-wiki-link
  "\\[\\[\\([^]|]+\\)\\(|\\([^]]+\\)\\)?\\]\\]"
  "Regular expression for matching wiki links.
This matches typical bracketed [[WikiLinks]] as well as 'aliased'
wiki links of the form [[PageName|link text]].  In this regular
expression, #1 matches the page name and #3 matches the link
text.")

(defconst picolisp-wiki-regex-uri
  (concat
   "\\(" (mapconcat 'identity picolisp-wiki-uri-types "\\|")
   "\\):[^]\t\n\r<>,;() ]+")
  "Regular expression for matching inline URIs.")

(defconst picolisp-wiki-regex-angle-uri
  (concat
   "\\(<\\)\\("
   (mapconcat 'identity picolisp-wiki-uri-types "\\|")
   "\\):[^]\t\n\r<>,;()]+\\(>\\)")
  "Regular expression for matching inline URIs in angle brackets.")

(defconst picolisp-wiki-regex-email
  "<\\(\\sw\\|\\s_\\|\\s.\\)+@\\(\\sw\\|\\s_\\|\\s.\\)+>"
  "Regular expression for matching inline email addresses.")



;; Keywords

(defvar picolisp-wiki-mode-font-lock-keywords-basic
  (list
   (cons picolisp-wiki-regex-header-1 '(2 picolisp-wiki-header-face-1))
   (cons picolisp-wiki-regex-header-2 '(2 picolisp-wiki-header-face-2))
   (cons picolisp-wiki-regex-header-3 '(2 picolisp-wiki-header-face-3))
   (cons picolisp-wiki-regex-header-4 '(2 picolisp-wiki-header-face-4))
   (cons picolisp-wiki-regex-header-5 '(2 picolisp-wiki-header-face-5))
   (cons picolisp-wiki-regex-header-6 '(2 picolisp-wiki-header-face-6))
   (cons picolisp-wiki-regex-starting-brace 'picolisp-wiki-starting-brace-face)
   (cons picolisp-wiki-regex-closing-brace '(2 picolisp-wiki-closing-brace-face))
   (cons picolisp-wiki-regex-pre-block '(2 picolisp-wiki-pre-face))
   (cons picolisp-wiki-regex-hr '(2 picolisp-wiki-hr-face))
   (cons picolisp-wiki-regex-line-break '(2 picolisp-wiki-line-break-face))
   (cons picolisp-wiki-regex-comment '(2 picolisp-wiki-comment-face))
   (cons picolisp-wiki-regex-angle-uri '(2 picolisp-wiki-url-face))
   (cons picolisp-wiki-regex-uri '(2 picolisp-wiki-url-face))
   (cons picolisp-wiki-regex-email '(2 picolisp-wiki-url-face))
   ;; (cons picolisp-wiki-regex-left-floating-content
   ;;       '(2 picolisp-wiki-left-floating-content-fact))
   ;; (cons picolisp-wiki-regex-non-floating-content
   ;;       '(2 picolisp-wiki-non-floating-content-fact))
   ;; (cons picolisp-wiki-regex-right-floating-content
   ;;       '(2 picolisp-wiki-right-floating-content-fact))
   (cons picolisp-wiki-regex-email '(2 picolisp-wiki-url-face))
   ;; changed from picolisp-wiki-regex-list-item
   ;; (cons picolisp-wiki-regex-list-item '(2 picolisp-wiki-list-item-face))
   ;; (cons picolisp-wiki-regex-list-item-text 'picolisp-wiki-list-item-face)
   (cons picolisp-wiki-regex-internal-link
         '((2 picolisp-wiki-url-face t)
           (4 picolisp-wiki-internal-link-face t)))
   (cons picolisp-wiki-regex-external-link
         '((2 picolisp-wiki-url-face t)
           (4 picolisp-wiki-external-link-face t)))
   (cons picolisp-wiki-regex-bold '(2 picolisp-wiki-bold-face))
   ;; (cons picolisp-wiki-regex-bold-text 'picolisp-wiki-bold-face)
   (cons picolisp-wiki-regex-italic '(2 picolisp-wiki-italic-face))
   (cons picolisp-wiki-regex-underlined '(2 picolisp-wiki-underlined-face))
    )
   "Syntax highlighting for Picolisp-Wiki files.")

(defvar picolisp-wiki-mode-font-lock-keywords
  (append picolisp-wiki-mode-font-lock-keywords-basic)
  "Default highlighting expressions for Picolisp-Wiki mode.")



;;; Syntax Table ==============================================================

(defvar picolisp-wiki-mode-syntax-table
  (let ((picolisp-wiki-mode-syntax-table (make-syntax-table)))
    (modify-syntax-entry ?\" "w" picolisp-wiki-mode-syntax-table)
    picolisp-wiki-mode-syntax-table)
  "Syntax table for `picolisp-wiki-mode'.")



;;; Element Insertion =========================================================

(defun picolisp-wiki-wrap-or-insert (s1 s2 &optional beg-newline-p)
  "Insert the strings S1 and S2.
If Transient Mark mode is on and a region is active, wrap the strings S1
and S2 around the region."
  (if (and transient-mark-mode mark-active)
      (let ((a (region-beginning)) (b (region-end)))
        (goto-char a)
        (insert s1)
        (goto-char (+ b (length s1)))
        (insert s2))
    (if (not beg-newline-p)
        (insert s1 s2)
      (end-of-line)
      (newline 2)
      (insert s1 s2))))

(defun picolisp-wiki-insert-hr ()
  "Insert a horizonal rule using `picolisp-wiki-hr-string'."
  (interactive)
  ;; Leading blank line
  (when (and (>= (point) (+ (point-min) 2))
             (not (looking-back "\n\n" 2)))
    (insert "\n"))
  ;; Insert custom HR string
  (insert (concat picolisp-wiki-hr-string "\n"))
  ;; Following blank line
  (backward-char)
  (unless (looking-at "\n\n")
          (insert "\n")))

(defun picolisp-wiki-insert-bold ()
  "Insert markup for a bold word or phrase.
If Transient Mark mode is on and a region is active, it is made bold."
  (interactive)
  (picolisp-wiki-wrap-or-insert "!{" "}")
  (backward-char 1))

(defun picolisp-wiki-insert-italic ()
  "Insert markup for an italic word or phrase.
If Transient Mark mode is on and a region is active, it is made italic."
  (interactive)
  (picolisp-wiki-wrap-or-insert "/{" "}")
  (backward-char 1))

(defun picolisp-wiki-insert-underlined ()
  "Insert markup for an underlined word or phrase.
If Transient Mark mode is on and a region is active, it is underlined."
  (interactive)
  (picolisp-wiki-wrap-or-insert "_{" "}")
  (backward-char 1))


(defun picolisp-wiki-insert-pre-block ()
  "Insert markup for a pre-formatted block.
If Transient Mark mode is on and a region is active, it is marked
as inline code."
  (interactive)
  (picolisp-wiki-wrap-or-insert ":{" "}")
  (backward-char 1))

(defun picolisp-wiki-insert-comment ()
  "Insert markup for an comment.
If Transient Mark mode is on and a region is active, it is marked
as inline code."
  (interactive)
  (picolisp-wiki-wrap-or-insert "#{" "}")
  (backward-char 1))


(defun picolisp-wiki-insert-internal-link ()
  "Insert an internal link.
If Transient Mark mode is on and a region is active, it is used
as the link text."
  (interactive)
  (picolisp-wiki-wrap-or-insert "={" "}")
  (backward-char 1))

(defun picolisp-wiki-insert-external-link ()
  "Insert an external link.
If Transient Mark mode is on and a region is active, it is used
as the link text."
  (interactive)
  (picolisp-wiki-wrap-or-insert "^{" "}")
  (backward-char 1))


(defun picolisp-wiki-insert-left-floating-content ()
  "Insert an inline image tag of the form <{content}.
If Transient Mark mode is on and a region is active, it is used
as the alt text of the image."
  (interactive)
  (picolisp-wiki-wrap-or-insert "<{" "}")
  (backward-char 1))

(defun picolisp-wiki-insert-non-floating-content ()
  "Insert an inline image tag of the form @{content}.
If Transient Mark mode is on and a region is active, it is used
as the alt text of the image."
  (interactive)
  (picolisp-wiki-wrap-or-insert "@{" "}")
  (backward-char 1))

(defun picolisp-wiki-insert-right-floating-content ()
  "Insert an inline image tag of the form >{content}.
If Transient Mark mode is on and a region is active, it is used
as the alt text of the image."
  (interactive)
  (picolisp-wiki-wrap-or-insert ">{" "}")
  (backward-char 1))


(defun picolisp-wiki-insert-line-breaks (n)
  "Insert line-breaks. With no prefix argument, insert 1
line-break. With prefix N, insert N line-breaks. With prefix N,
insert N line-breaks. With negative prefix -N, insert N
line-breaks and clear float style."
  (interactive "p")
  (unless n                             ; Test to see if n is defined
    (setq n 1))                         ; Default to level 1 header
  (insert
   (format "&{%d}" n )))


(defun picolisp-wiki-insert-unordered-list ()
  "Insert an unordered list.
If Transient Mark mode is on and a region is active, it is wrapped in an unordered list (the region should only contain list-items)."
  (interactive)
  (end-of-line)
  (newline)
  (insert "*{")
  (newline)
  (insert "   -{}")
  (newline)
  (insert "}")
  (newline)
  (search-backward "-{" nil t 1)
  (forward-char 2))


(defun picolisp-wiki-insert-ordered-list ()
  "Insert an ordered list.
If Transient Mark mode is on and a region is active, it is
wrapped in an ordered list (the region should only contain
list-items)."
  (interactive)
  (end-of-line)
  (newline)
  (insert "+{")
  (newline)
  (insert "   -{}")
  (newline)
  (insert "}")
  (newline)
  (search-backward "-{" nil t 1)
  (forward-char 2))


;; FIXME consider escaped braces '\{'and '\}' inside list items
(defun picolisp-wiki--inside-list-item-p (&optional second-trial-p)
  "Return t if inside list-item, nil otherwise.
This function takes care of the (common) case when there is one
nested markup inside the list item, e.g. a link or a bold text,
and point is inside the nested markup braces."
  (save-excursion
    (let ((pt (point)))
      (search-backward "{" nil t 1)
      (backward-char)
      (if (not (looking-at "-{"))
          (if (and               
               (not second-trial-p)
               (looking-at
                (concat "\\(\\*\\|\\+\\|&\\|/\\|_\\|\\^\\|"
                        "<\\|>\\|@\\|!\\|=\\|:\\|#\\)\\({\\)")))
              (picolisp-wiki--inside-list-item-p 'SECOND-TRIAL-P)
            nil)
        (and
         (if second-trial-p
             (search-forward-regexp "}[^}]*}" nil t 1)
           (search-forward "}" nil t 1))
         (setq item-end (point))
         (> item-end pt))))))
        
(defun picolisp-wiki-insert-list-item ()
  "Insert a list-item.
If Transient Mark mode is on and a region is active, it becomes the text of a list item."
  (interactive)
  (if (not (picolisp-wiki--inside-list-item-p))   
      (progn  
        (picolisp-wiki-wrap-or-insert "-{" "}")
        (backward-char 1))
    (end-of-line)
    (newline)
    (insert "   -{}")
    (backward-char 1)
    ))


(defun picolisp-wiki-insert-header-1 ()
  "Insert a first level picolisp-wiki-style header.
If Transient Mark mode is on and a region is active, it is used
as the header text."
  (interactive)
  (picolisp-wiki-insert-header 1))

(defun picolisp-wiki-insert-header-2 ()
  "Insert a second level picolisp-wiki-style header.
If Transient Mark mode is on and a region is active, it is used
as the header text."
  (interactive)
  (picolisp-wiki-insert-header 2))

(defun picolisp-wiki-insert-header-3 ()
  "Insert a third level picolisp-wiki-style header.
If Transient Mark mode is on and a region is active, it is used
as the header text."
  (interactive)
  (picolisp-wiki-insert-header 3))

(defun picolisp-wiki-insert-header-4 ()
  "Insert a fourth level picolisp-wiki-style header.
If Transient Mark mode is on and a region is active, it is used
as the header text."
  (interactive)
  (picolisp-wiki-insert-header 4))

(defun picolisp-wiki-insert-header-5 ()
  "Insert a fifth level picolisp-wiki-style header.
If Transient Mark mode is on and a region is active, it is used
as the header text."
  (interactive)
  (picolisp-wiki-insert-header 5))

(defun picolisp-wiki-insert-header-6 ()
  "Insert a sixth level picolisp-wiki-style header.
If Transient Mark mode is on and a region is active, it is used
as the header text."
  (interactive)
  (picolisp-wiki-insert-header 6))

(defun picolisp-wiki-insert-header (n)
  "Insert an picolisp-wiki-style header.
With no prefix argument, insert a level-1 header.  With prefix N,
insert a level-N header.  If Transient Mark mode is on and the
region is active, it is used as the header text."
  (interactive "p")
  (unless n                             ; Test to see if n is defined
    (setq n 1))                         ; Default to level 1 header
  (picolisp-wiki-wrap-or-insert
   (concat (number-to-string n) "{") "}" 'BEG-NEWLINE-P)
  (backward-char 1))



;;; Keymap ====================================================================

(defvar picolisp-wiki-mode-map
  (let ((map (make-keymap)))
    ;; Element insertion
    (define-key map "\C-c\C-ln" 'picolisp-wiki-insert-internal-link)
    (define-key map "\C-c\C-lx" 'picolisp-wiki-insert-external-link)
    (define-key map "\C-c\C-lu" 'picolisp-wiki-insert-unordered-list)
    (define-key map "\C-c\C-lo" 'picolisp-wiki-insert-ordered-list)
    (define-key map "\C-c\C-li" 'picolisp-wiki-insert-list-item)
    (define-key map "\C-c\C-fl" 'picolisp-wiki-insert-left-floating-content)
    (define-key map "\C-c\C-fn" 'picolisp-wiki-insert-non-floating-content)
    (define-key map "\C-c\C-fr" 'picolisp-wiki-insert-right-floating-content)
    (define-key map "\C-c\C-ck" 'picolisp-wiki-insert-line-breaks)
    (define-key map "\C-c\C-c1" 'picolisp-wiki-insert-header-1)
    (define-key map "\C-c\C-c2" 'picolisp-wiki-insert-header-2)
    (define-key map "\C-c\C-c3" 'picolisp-wiki-insert-header-3)
    (define-key map "\C-c\C-c4" 'picolisp-wiki-insert-header-4)
    (define-key map "\C-c\C-c5" 'picolisp-wiki-insert-header-5)
    (define-key map "\C-c\C-c6" 'picolisp-wiki-insert-header-6)
    (define-key map "\C-c\C-cb" 'picolisp-wiki-insert-bold)
    (define-key map "\C-c\C-ci" 'picolisp-wiki-insert-italic)
    (define-key map "\C-c\C-cu" 'picolisp-wiki-insert-underlined)
    (define-key map "\C-c\C-cp" 'picolisp-wiki-insert-pre-block)
    (define-key map "\C-c\C-cc" 'picolisp-wiki-insert-comment)
    (define-key map "\C-c\C-c-" 'picolisp-wiki-insert-hr)
    ;; Visibility cycling
    (define-key map (kbd "<tab>") 'picolisp-wiki-cycle)
    (define-key map (kbd "<S-iso-lefttab>") 'picolisp-wiki-shifttab)
    ;; Header navigation
    (define-key map (kbd "C-M-n") 'outline-next-visible-heading)
    (define-key map (kbd "C-M-p") 'outline-previous-visible-heading)
    (define-key map (kbd "C-M-f") 'outline-forward-same-level)
    (define-key map (kbd "C-M-b") 'outline-backward-same-level)
    (define-key map (kbd "C-M-u") 'outline-up-heading)
    ;; Picolisp-Wiki functions
    ;; (define-key map "\C-c\C-cm" 'picolisp-wiki)
    ;; (define-key map "\C-c\C-cp" 'picolisp-wiki-preview)
    ;; (define-key map "\C-c\C-ce" 'picolisp-wiki-export)
    ;; (define-key map "\C-c\C-cv" 'picolisp-wiki-export-and-view)
    map)
  "Keymap for Picolisp-Wiki major mode.")





;;; Menu ==================================================================

(easy-menu-define picolisp-wiki-mode-menu picolisp-wiki-mode-map
  "Menu for Picolisp-Wiki mode"
  '("Picolisp-Wiki"
    ("Show/Hide"
     ["Cycle visibility" picolisp-wiki-cycle (outline-on-heading-p)]
     ["Cycle global visibility" picolisp-wiki-shifttab])
    "---"
    ("Headers"
     ["First level" picolisp-wiki-insert-header-1]
     ["Second level" picolisp-wiki-insert-header-2]
     ["Third level" picolisp-wiki-insert-header-3]
     ["Fourth level" picolisp-wiki-insert-header-4]
     ["Fifth level" picolisp-wiki-insert-header-5]
     ["Sixth level" picolisp-wiki-insert-header-6])
    "---"
    ["Bold" picolisp-wiki-insert-bold]
    ["Italic" picolisp-wiki-insert-italic]
    ["Underlined" picolisp-wiki-insert-underlined]
    ["Preformatted" picolisp-wiki-insert-pre-block]
    ["Comment" picolisp-wiki-insert-comment]
    ["Insert horizontal rule" picolisp-wiki-insert-hr]
    "---"
    ["Insert internal link" picolisp-wiki-insert-internal-link]
    ["Insert external link" picolisp-wiki-insert-external-link]
    "---"
    ["Insert left-floating content" picolisp-wiki-insert-left-floating-content]
    ["Insert non-floating content" picolisp-wiki-insert-non-floating-content]
    ["Insert right-floating content" picolisp-wiki-insert-right-floating-content]
    "---"
    ["Insert unordered list" picolisp-wiki-insert-unordered-list]
    ["Insert ordered list" picolisp-wiki-insert-ordered-list]
    "---"
    ["Version" picolisp-wiki-show-version]
    ))



;;; Outline ===================================================================

;; The following visibility cycling code was taken from org-mode
;; by Carsten Dominik and adapted for picolisp-wiki-mode.

(defvar picolisp-wiki-cycle-global-status 1)
(defvar picolisp-wiki-cycle-subtree-status nil)

;; Based on org-end-of-subtree from org.el
(defun picolisp-wiki-end-of-subtree (&optional invisible-OK)
  "Move to the end of the current subtree.
Only visible heading lines are considered, unless INVISIBLE-OK is
non-nil."
  (outline-back-to-heading invisible-OK)
  (let ((first t)
        (level (funcall outline-level)))
    (while (and (not (eobp))
                (or first (> (funcall outline-level) level)))
      (setq first nil)
      (outline-next-heading))
    (if (memq (preceding-char) '(?\n ?\^M))
        (progn
          ;; Go to end of line before heading
          (forward-char -1)
          (if (memq (preceding-char) '(?\n ?\^M))
              ;; leave blank line before heading
              (forward-char -1)))))
  (point))

;; Based on org-cycle from org.el.
(defun picolisp-wiki-cycle (&optional arg)
  "Visibility cycling for Picolisp-Wiki mode.
If ARG is t, perform global visibility cycling. If the point is
at an picolisp-wiki-style header, cycle visibility of the
corresponding subtree. Otherwise, insert a tab using
`indent-relative'."
  (interactive "P")
  (cond
     ((eq arg t) ;; Global cycling
      (cond
       ((and (eq last-command this-command)
             (eq picolisp-wiki-cycle-global-status 2))
        ;; Move from overview to contents
        (hide-sublevels 1)
        (message "CONTENTS")
        (setq picolisp-wiki-cycle-global-status 3))

       ((and (eq last-command this-command)
             (eq picolisp-wiki-cycle-global-status 3))
        ;; Move from contents to all
        (show-all)
        (message "SHOW ALL")
        (setq picolisp-wiki-cycle-global-status 1))

       (t
        ;; Defaults to overview
        (hide-body)
        (message "OVERVIEW")
        (setq picolisp-wiki-cycle-global-status 2))))

     ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
      ;; At a heading: rotate between three different views
      (outline-back-to-heading)
      (let ((goal-column 0) eoh eol eos)
        ;; Determine boundaries
        (save-excursion
          (outline-back-to-heading)
          (save-excursion
            (beginning-of-line 2)
            (while (and (not (eobp)) ;; this is like `next-line'
                        (get-char-property (1- (point)) 'invisible))
              (beginning-of-line 2)) (setq eol (point)))
          (outline-end-of-heading)   (setq eoh (point))
          (picolisp-wiki-end-of-subtree t)
          (skip-chars-forward " \t\n")
          (beginning-of-line 1) ; in case this is an item
          (setq eos (1- (point))))
        ;; Find out what to do next and set `this-command'
      (cond
         ((= eos eoh)
          ;; Nothing is hidden behind this heading
          (message "EMPTY ENTRY")
          (setq picolisp-wiki-cycle-subtree-status nil))
         ((>= eol eos)
          ;; Entire subtree is hidden in one line: open it
          (show-entry)
          (show-children)
          (message "CHILDREN")
          (setq picolisp-wiki-cycle-subtree-status 'children))
         ((and (eq last-command this-command)
               (eq picolisp-wiki-cycle-subtree-status 'children))
          ;; We just showed the children, now show everything.
          (show-subtree)
          (message "SUBTREE")
          (setq picolisp-wiki-cycle-subtree-status 'subtree))
         (t
          ;; Default action: hide the subtree.
          (hide-subtree)
          (message "FOLDED")
          (setq picolisp-wiki-cycle-subtree-status 'folded)))))

     (t
      (indent-for-tab-command))))

;; Based on org-shifttab from org.el.
(defun picolisp-wiki-shifttab ()
  "Global visibility cycling.
Calls `picolisp-wiki-cycle' with argument t."
  (interactive)
  (picolisp-wiki-cycle t))


;;; Miscellaneous =============================================================

(defun picolisp-wiki-line-number-at-pos (&optional pos)
  "Return (narrowed) buffer line number at position POS.
If POS is nil, use current buffer location.
This is an exact copy of `line-number-at-pos' for use in emacs21."
  (let ((opoint (or pos (point))) start)
    (save-excursion
      (goto-char (point-min))
      (setq start (point))
      (goto-char opoint)
      (forward-line 0)
      (1+ (count-lines start (point))))))

(defun picolisp-wiki-nobreak-p ()
  "Return nil if it is acceptable to break the current line at the point."
  ;; inside in square brackets (e.g., link anchor text)
  (looking-back "\\[[^]]*"))



;;; Mode definition  ==========================================================

(defun picolisp-wiki-show-version ()
  "Show the version number in the minibuffer."
  (interactive)
  (message "picolisp-wiki-mode, version %s" picolisp-wiki-mode-version))

;;;###autoload
(define-derived-mode picolisp-wiki-mode text-mode "PicoLisp-Wiki"
  "Major mode for editing PicoLisp-Wiki files."
  ;; Natural Picolisp-Wiki tab width
  (setq tab-width 4)
  ;; Comments
  (make-local-variable 'comment-start)
  (setq comment-start "#{")
  (make-local-variable 'comment-end)
  (setq comment-end "}")
  ;; (make-local-variable 'comment-start-skip)
  ;; (setq comment-start-skip "#{ \t}*")
  (make-local-variable 'comment-column)
  (setq comment-column 0)
  ;; Font lock.
  (set (make-local-variable 'font-lock-defaults)
       '(picolisp-wiki-mode-font-lock-keywords))
  (set (make-local-variable 'font-lock-multiline) t)
  ;; Make filling work with lists (unordered, ordered, and definition)
  ;; (set (make-local-variable 'paragraph-start)
  ;;      "\f\\|[ \t]*$\\|^[ \t]*[*+-] \\|^[ \t*][0-9]+\\.\\|^[ \t]*: ")
  ;; Outline mode
  (make-local-variable 'eval)
  (setq eval (outline-minor-mode))
  (make-local-variable 'outline-regexp)
  ;; (setq outline-regexp "^[ \t]*[0-9]{")
  (setq outline-regexp
        "^[ 	]*\\(1{\\|2{.\\|3{..\\|4{...\\|5{....\\|6{.....\\)")
  ;; Cause use of ellipses for invisible text.
  (add-to-invisibility-spec '(outline . t)))


(provide 'picolisp-wiki-mode)

;;; picolisp-wiki-mode.el ends here