-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgimp-mode.el
More file actions
3554 lines (3217 loc) · 121 KB
/
gimp-mode.el
File metadata and controls
3554 lines (3217 loc) · 121 KB
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
;;; gimp-mode.el --- Interaction/editing mode for the GIMP
;; Copyright (C) 2008-2012 Niels Giesen
;; Author: Niels Giesen <niels.giesen@gmail.com>
;; Keywords: processes, languages, multimedia, tools
;; 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Interaction/editing mode for the GIMP
;; see README.markdown for full description and usage.
;; TOC:
;;;; Requirements
;;;; Structure
;;;; Faces
;;;; Globals
;;;; Customization
;;;; General purpose functions and macros
;;;; Keybindings
;;;; Set up the modes
;;;; Versioning
;;;; Evaluation
;;;; Help
;;;; Stuff pertaining to running 'n' quitting
;;;; Utility functions
;;;; Caches: saving, deleting, restoring
;;;; Tracing
;;;; Modes
;;;; Internal information retrieval
;;;; Completion
;;;; Shortcuts
;;;; Doc echoing
;;;; Source look-up
;;;; Snippets
;;;; Misc interactive commands
;; Client mode global vars
;;; Code:
(provide 'gimp-mode)
;;;; Requirements
(require 'cmuscheme)
(require 'outline)
(require 'cl)
(require 'eldoc)
(require 'thingatpt)
(eval-when-compile (load "gimp-init.el"))
(eval-when (compile load)
(require 'ring)
(require 'snippet)
(require 'scheme-complete)
;; (require 'fud)
)
;;;; Structure
(defgroup gimp nil "Customization group for GIMP (script-fu) programming."
:group 'shell
:group 'scheme
:group 'multimedia
:group 'languages)
(defgroup gimp-faces
nil
"Customization group for GIMP Mode faces"
:group 'gimp)
;;;; Faces
;; text properties
(defun gimp-highlight (str)
(propertize str 'mouse-face 'highlight))
(defmacro gimp-set-face (str face)
`(propertize ,str
'font-lock-face
(read (format "gimp-%S-face" ',face))))
(defmacro gimp-button (text field hint face)
"Make a button showing TEXT, with field FIELD.
HINT is the help-echo, and face the gimp-FACE-face."
`(propertize
,text
'mouse-face 'highlight
'field ',field
'help-echo
,hint
'font-lock-face (read (format "gimp-%S-face" ',face))))
(defface gimp-shy-face
'((((class color)(background dark))
:foreground "saddlebrown")
(((class color)(background light))
:foreground "#ecf"))
"Face for uninteresting stuff."
:group 'gimp-faces)
(defface gimp-less-shy-face
'((((class color)(background dark))
:foreground "darkkhaki")
(((class color)(background light))
:foreground "darkslateblue"))
"Face for uninteresting stuff."
:group 'gimp-faces)
(defface gimp-menu-face
'((t (:foreground "#7f8c29"
:box '(:line-width 2 :color grey15)
:height 1.2
:bold t
)))
"Face for menu items."
:group 'gimp-faces)
(defface gimp-red-face
'((t (:foreground "#f00"
:bold t
)))
"Face for menu items."
:group 'gimp-faces)
(defface gimp-terminal-menu-face
'((default
(:inherit gimp-menu-face))
(((class color)(background dark))
:foreground "#cc0")
(((class color)(background light))
:foreground "#200"))
"Face for terminal menu item."
:group 'gimp-faces)
(defface gimp-link-face
'((t
(:underline t
:foreground "#d37511")))
"Face for links."
:group 'gimp-faces)
(defface gimp-visited-procedure-face
'((t
(:foreground "#8c7829")))
"Face for procedures that have been visited with GIMP Help"
:group 'gimp-faces)
(defface gimp-level1-face
'((default
:family "helv"
:height 1.2
:weight bold)
(((class color)(background dark))
:foreground "gold3")
(((class color)(background light))
:foreground "gold"))
"Face for level2 items (== arguments)"
:group 'gimp-faces)
(defface gimp-level2-face
'((default
:weight bold)
(((class color)(background dark))
:foreground "gold")
(((class color)(background light))
:foreground "#200"))
"Face for level2 items (== arguments)"
:group 'gimp-faces)
(defface gimp-function-name-face
'((((class color) (min-colors 88) (background light))
(:foreground "Blue1"))
(((class color) (min-colors 88) (background dark))
(:foreground "LightSkyBlue"))
(((class color) (min-colors 16) (background light))
(:foreground "Blue"))
(((class color) (min-colors 16) (background dark))
(:foreground "LightSkyBlue"))
(((class color) (min-colors 8))
(:foreground "blue" :weight bold))
(t (:inverse-video t :weight bold)))
"Gimp Mode face used to highlight function names."
:group 'gimp-faces)
(defface gimp-variable-name-face
'((((class grayscale) (background light))
(:foreground "Gray90" :weight bold :slant italic))
(((class grayscale) (background dark))
(:foreground "DimGray" :weight bold :slant italic))
(((class color) (min-colors 88) (background light))
(:foreground "DarkGoldenrod"))
(((class color) (min-colors 88) (background dark))
(:foreground "LightGoldenrod"))
(((class color) (min-colors 16) (background light))
(:foreground "DarkGoldenrod"))
(((class color) (min-colors 16) (background dark))
(:foreground "LightGoldenrod"))
(((class color) (min-colors 8))
(:foreground "yellow" :weight light))
(t (:weight bold :slant italic)))
"Gimp Mode face used to highlight variable names."
:group 'gimp-faces)
(font-lock-add-keywords
'gimp-mode
'(("[A-Z-]\\{3,\\}" . font-lock-constant-face)) )
;;;; Globals
(defconst gimp-email-regexp
"\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]\\{2,4\\}\\b")
(defconst gimp-url-regexp
"\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\\|gopher\\|nntp\\|news\\|\
telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-z0-9_.]+:[0-9]*\\)?\
\[-a-z0-9_=!?#$@~%&*+\\/:;.,[:word:]]+[-a-z0-9_=#$@~%&*+\\/[:word:]]\\)")
(defconst gimp-interactive t
"Provide interaction with inferior gimp process?
Leave this a non-nil-val; this might turn into a defcustom one time or
another. Now best left at the non-nil value.")
(defvar gimp-current-procedure nil
"Value of queried procedure in GIMP Help buffer")
(make-variable-buffer-local 'comint-input-filter-functions)
(make-variable-buffer-local 'gimp-current-procedure)
(defvar gimp-mode-line-format
'("["
(:eval
(reduce (lambda (memo this)
(or (and (and memo this)(concat memo "," this)) this memo))
(list
(if gimp-complete-p
(if gimp-complete-fuzzy-p
"fuzzy"
"strict"))
(when (get 'gimp-trace 'trace-wanted) "tracing")
(and gimp-cl-host (concat gimp-cl-host ":"
(number-to-string gimp-cl-port))))))
"]")
"State of various Gimp Mode options.")
(defvar gimp-cl-proc nil
"Gimp Client Process Object")
(defvar gimp-cl-buffer-name "*Gimp-Client*"
"Name of Gimp client buffer.")
(defvar gimp-output ""
"Contains output from inferior gimp process.")
;; (Bases of following caches) generated on GIMP startup (by
;; emacs-interaction.scm)
(defvar gimp-dump nil
"Dump of GIMPs pdb as a hash table.
Access this only through `gimp-get-dump-hash', since it requires
that GIMP run and generate the symbol information for completion
and documentation first.")
(defvar gimp-pdb-cache nil
"Cache containing all symbols in GIMPs Procedural Database.
Access this only through `gimp-get-pdb-cache', for the same
reasons as given by the documentation for `gimp-dump'.")
(defvar gimp-registrations nil)
(defvar gimp-fonts-cache nil
"Cache of available fonts")
(defvar gimp-brushes-cache nil
"Cache of available brushes")
(defvar gimp-patterns-cache nil
"Cache of available patterns")
(defvar gimp-gradients-cache nil
"Cache of available gradients")
(defvar gimp-palettes-cache nil
"Cache of available palettes")
(defvar gimp-oblist-cache nil
"Cache containing ALL symbols in TinyScheme, whether bound or not.
The last might be subject to change.")
(defvar gimp-menu nil)
(defvar gimp-shortcuts nil
"Commands that can be completed from inferior GIMP buffer.
Define such command with `gimp-defcommand' to be automatically included.")
(defvar gimp-help-ring ())
(setf (get 'gimp-help-ring 'gimp-help-positions)
(make-vector 100 1))
(put 'gimp-help-ring 'index 0)
;;;; Customization
(defgroup gimp-directories nil
"Directories where the GIMP finds its sources"
:group 'gimp)
(defcustom gimp-src-dir (expand-file-name "~/Src/gimp-2.6/")
"Source directory for the GIMP.
On Debian(-derivatives), get the source for your distribution with apt-get
source gimp. You will most probably have to change this value."
:group 'gimp-directories
:type 'string)
(defcustom gimp-data-dir "/usr/share/gimp/2.0/"
"Fall-back data dir of the GIMP.
Setting this variable is only necessary for non-interactive use.
Value returned by evaluating the variable `gimp-data-dir' in the GIMP
script-fu console."
:group 'gimp-directories
:type 'string)
(defun gimp-src-dir ()
(if (or
(null gimp-src-dir)
(not (file-exists-p gimp-src-dir)))
(error "%s does not exist. Check variable `gimp-src-dir'"
(or gimp-src-dir "gimp-src-dir"))
gimp-src-dir))
(defcustom gimp-docs-alist
'(("script-fu introduction" .
"http://www.ve3syb.ca/wiki/doku.php?id=software:sf:start")
("script-fu tutorial" .
"http://docs.gimp.org/en/gimp-using-script-fu-tutorial.html")
("yahoo script-fu group" . "http://tech.groups.yahoo.com/group/script-fu/")
("mailing lists" . "http://www.gimp.org/mail_lists.html")
("developer.gimp.org" . "http://developer.gimp.org/")
("registry.gimp.org" . "http://registry.gimp.org/")
("local help" . "file:///usr/share/gimp/2.0/help/en/index.html")
("gimp talk (needs graphical browser)" . "http://www.gimptalk.com/forum/")
("gimp-mode page" . "https://github.com/pft/gimpmode")
("grokking the gimp" .
"http://www.linuxtopia.org/online_books/graphics_tools\
/gimp_advanced_guide/index.html")
("sicp (Structure and Interpretation of Computer Programs)" .
"http://mitpress.mit.edu/sicp/full-text/book/book.html"))
"Alist of gimp documentation URLs."
:group 'gimp
:type '(alist :key-type string :value-type string))
(defcustom gimp-program (if (eq window-system 'w32)
"gimp-2.6.exe" ;Probably... does not
;really matter anyway
"gimp")
"Name of Gimp executable.
If Gimp produces error messages, you may want to define and a
shell script redirecting stderr, something like:
#!/bin/sh
GIMP=`which gimp-2.7`
$GIMP $@ 2>/tmp/gimp-err.log
should do.
This is because Emacs cannot distinguish between stderr and
stdout for subprocesses.
See Info node `(elisp)Output from Processes'."
:group 'gimp
:type '(string))
(defcustom gimp-command-line-args
"-c --batch-interpreter=plug-in-script-fu-eval -b -"
"Arguments to give to the GIMP.
The list below is taken from GIMP version 2.6:
GNU Image Manipulation Program
Application Options:
-v, --version Show version information and exit
--license Show license information and exit
--verbose Be more verbose
-n, --new-instance Start a new GIMP instance
-a, --as-new Open images as new
-i, --no-interface Run without a user interface
-d, --no-data Do not load brushes, gradients, patterns, ...
-f, --no-fonts Do not load any fonts
-s, --no-splash Do not show a startup window
--no-shm Do not use shared memory between GIMP and plugins
--no-cpu-accel Do not use special CPU acceleration functions
--session=<name> Use an alternate sessionrc file
-g, --gimprc=<filename> Use an alternate user gimprc file
--system-gimprc=<filename> Use an alternate system gimprc file
-b, --batch=<command> Batch command to run (can be used multiple times)
--batch-interpreter=<proc> The procedure to process batch commands with
-c, --console-messages Send messages to console instead of using a dialog
--pdb-compat-mode=<mode> PDB compatibility mode (off|on|warn)
--stack-trace-mode=<mode> Debug in case of a crash (never|query|always)
--debug-handlers Enable non-fatal debugging signal handlers
--g-fatal-warnings Make all warnings fatal
--dump-gimprc Output a gimprc file with default settings
--display=DISPLAY X display to use"
:group 'gimp
:type 'string)
(defcustom gimp-inhibit-start-up-message nil
"Inhibit start-up message in Inferior Gimp Mode."
:group 'gimp
:type 'boolean)
(defcustom gimp-just-one-space nil
"When issuing `gimp-space', act as `just-one-space'?"
:group 'gimp
:type 'boolean)
(defcustom gimp-try-and-get-closure-code-p t
"When this variable has a non-nil value, try to get closure
code for echoing. Otherwise, don't. When running in client mode,
this can really make a difference in responsiveness."
:group 'gimp
:type 'boolean)
(defcustom gimp-insert-quotes-for-strings-p t
"Insert quotes when the current argument is a string?
With its value set to nil, gimp-mode will still offer contextual
completion (when possible) when point is in a string."
:group 'gimp
:type 'boolean)
(defcustom gimp-prompt ">"
"Prompt TinyScheme uses.
The prompt used to be >, but Gimp git commit
9541fe03872d15d049c95809fa50b97ce5791129, of Fri Aug 7 2009
changed this to ts>. Presumably to comply with changes in
TinyScheme cvs. What this roughly means is that Gimp 2.7 and
beyond need ts>, not >.
Run-gimp will automagically change this session-wide to ts> if it
deems required, but not back again.
And this is *not* done when connecting to a Gimp server via
`gimp-cl-connect'."
:group 'gimp
:type 'string)
;;;; General purpose functions and macros
(defmacro gimp-defcommand (name arglist docstring &rest body)
"Define NAME as a function.
Put its name without the gimp-prefix in variable `gimp-shortcuts'.
The definition is (lambda ARGLIST DOCSTRING BODY...).
If no `interactive' form is used in BODY, an error is signalled."
(declare (indent defun)
(debug defun))
(when (not (stringp docstring))
(error "Error defining %S: docstring seems to be missing" name))
`(progn
(eval-when (load eval) (add-to-list 'gimp-shortcuts
,(replace-regexp-in-string
"^gimp-" "" (symbol-name name))))
(defun ,name (,@arglist)
,docstring
,@body)
(when (not (commandp ',name))
(error "Error: function %S defined, but not interactive" ',name))))
(defmacro gimp-hash-to-list (hash-table)
(let ((nl (gensym)))
`(let (,nl)
(maphash (lambda (k v)
(push (list k v) ,nl)) ,hash-table)
,nl)))
(defmacro gimp-list-to-hash (list)
(let ((ht (gensym)))
`(let ((,ht (make-hash-table)))
(mapc (lambda (item)
(puthash (car item) (cadr item) ,ht)) ,list)
,ht)))
(defmacro gimp-without-string (&rest body)
`(save-excursion
(if (gimp-in-string-p)
(gimp-up-string))
,@body))
(defmacro gimp-without-string-or-comment (&rest body)
`(save-excursion
(if (gimp-in-string-p)
(gimp-up-string))
(if (gimp-in-comment-p)
(gimp-up-comment))
,@body))
(defun gimp-string-match (re str &optional num)
(when (string-match re str)
(if num (match-string num str)
(let (result)
(dotimes (v (/ (length (match-data)) 2) (reverse result))
(push (match-string v str) result))))))
(defun gimp-test-recursively (fun list max)
"Test FUN on subsequent items in LIST, for MAX recursions."
(cond
((< (decf max) 0))
((endp (cdr list)) t)
((apply fun
(car list)
(list (cadr list)))
(gimp-test-recursively fun (cdr list) max))
(t nil)))
;; Emacs 22 compatibility for rings
(eval-when (compile load)
(when (not (fboundp 'ring-member))
(defun ring-member (ring item)
"Return index of ITEM if on RING, else nil.
Comparison is done via `equal'. The index is 0-based."
(catch 'found
(dotimes (ind (ring-length ring) nil)
(when (equal item (ring-ref ring ind))
(throw 'found ind)))))))
(eval-when (compile load)
(when (not (fboundp 'ring-next))
(defun ring-next (ring item)
"Return the next item in the RING, after ITEM.
Raise error if ITEM is not in the RING."
(let ((curr-index (ring-member ring item)))
(unless curr-index (error "Item is not in the ring: `%s'" item))
(ring-ref ring (ring-plus1 curr-index (ring-length ring)))))))
;; Adapted from elmo-util.el
(defun gimp-uniq-list! (lst)
"Destructively uniquify elements of LST. Quite fast."
(let ((tmp lst))
(while tmp
(setq tmp
(setcdr tmp
(and (cdr tmp)
(delete
(car tmp)
(cdr tmp)))))))
lst)
(defun gimp-regular-files-in-dir (dir)
"List all regular files in DIR, "
(loop for f in (directory-files dir t)
when (file-regular-p f)
collect f))
(defun gimp-de-underscore (list-or-item)
"Return LIST-OR-ITEM with all underscores recursively eradicated."
(cond ((listp list-or-item)
(loop for i in list-or-item
when (not (eq '_ i))
collect (gimp-de-underscore i)))
(t list-or-item)))
(defun gimp-buffer-window (buffer)
(get-window-with-predicate
(lambda (w)
(eq (window-buffer w)
(get-buffer buffer)))))
;;;; Keybindings
;; menu "mixin"
(defvar gimp-menu-map
(let ((m (make-sparse-keymap)))
(define-key m [menu-bar gimp]
(cons "Gimp" (make-sparse-keymap "gimp-mode")))
(define-key m [menu-bar gimp gimp-documentation]
(cons "Related documentation" 'gimp-documentation))
(define-key m [menu-bar gimp gimp-report-bug]
(cons "Report a bug in gimp-mode" 'gimp-report-bug))
(define-key m [menu-bar gimp gimp-open-image]
(cons "Open image" 'gimp-open-image))
(define-key m [menu-bar gimp gimp-list-snippets]
(cons "List snippets" 'gimp-list-snippets))
(define-key m [menu-bar gimp gimp-toggle-completion]
(cons "Toggle completion" 'gimp-toggle-completion))
(define-key m [menu-bar gimp gimp-toggle-fuzzy-completion]
(cons "Toggle fuzzy completion" 'gimp-toggle-fuzzy-completion))
(define-key m [menu-bar gimp gimp-code-search]
(cons "Search for source code" 'gimp-code-search))
(define-key m [menu-bar gimp gimp-apropos]
(cons "Apropos GIMP procedure" 'gimp-apropos))
(define-key m [menu-bar gimp gimp-menu]
(cons "Find procedure through GIMP menu structure" 'gimp-menu))
(define-key m [menu-bar gimp gimp-describe-procedure]
(cons "Describe procedure" 'gimp-describe-procedure))
(define-key m [menu-bar gimp gimp-help]
(cons "Gimp Help" 'gimp-help))
(define-key m [menu-bar gimp gimp-run]
(cons "Run the GIMP" 'run-gimp))
m))
(defvar inferior-gimp-mode-map
(let ((m (copy-keymap inferior-scheme-mode-map)))
(define-key m "\t" 'gimp-indent-and-complete)
(define-key m " " 'gimp-space)
(define-key m "\C-c," 'gimp-describe-this-arg)
(define-key m "\C-c." 'gimp-echo)
(define-key m "\C-cf" 'gimp-describe-procedure)
(define-key m "\C-ca" 'gimp-apropos)
(define-key m "\C-ch" 'gimp-help)
(define-key m "\C-cd" 'gimp-documentation)
(define-key m "\C-cs" 'gimp-code-search)
(define-key m "\C-cm" 'gimp-menu)
(define-key m "\C-m" 'gimp-send-input)
(define-key m "\M-\\" 'comint-dynamic-complete-filename)
(define-key m "\C-cr" 'gimp-toggle-fuzzy-completion)
(define-key m "\C-cc" 'gimp-toggle-completion)
(define-key m [mouse-1] 'gimp-insert-input)
(define-key m [mouse-2] 'gimp-insert-input)
(with-no-warnings
(if (featurep 'fud)
(define-key m [(down-mouse-3)]
'fud-mouse-set-breakpoint)))
m))
(defvar gimp-mode-map
(let ((m (copy-keymap scheme-mode-map)))
(define-key m "\t" 'gimp-indent-and-complete)
(define-key m " " 'gimp-space)
(define-key m "\C-x\C-e" 'gimp-send-last-sexp)
(define-key m "\C-c," 'gimp-describe-this-arg)
(define-key m "\C-c." 'gimp-echo)
(define-key m "\C-ch" 'gimp-help)
(define-key m "\C-cf" 'gimp-describe-procedure)
(define-key m "\C-ca" 'gimp-apropos)
(define-key m "\C-cd" 'gimp-documentation)
(define-key m "\C-cs" 'gimp-code-search)
(define-key m "\C-cm" 'gimp-menu)
(define-key m "\C-ci" 'run-gimp)
(define-key m "\C-cr" 'gimp-toggle-fuzzy-completion)
(define-key m "\C-cc" 'gimp-toggle-completion)
(define-key m "\M-\\" 'comint-dynamic-complete-filename)
(with-no-warnings
(if (featurep 'fud)
(define-key m [(down-mouse-3)]
'fud-mouse-set-breakpoint)))
m))
(defvar gimp-help-mode-map
(let ((m (copy-keymap outline-mode-map)))
(define-key m "\C-m" 'gimp-help-enter)
(define-key m "\t" 'gimp-next-field)
(define-key m [(backtab)]
(lambda (n)
"Hop fields backwards."
(interactive "p")
(gimp-next-field (- n))))
(define-key m " " 'gimp-space)
(define-key m "," 'gimp-echo-at-point)
(define-key m "f" 'gimp-help-forward)
(define-key m "b" 'gimp-help-back)
(define-key m "F" 'gimp-describe-procedure)
(define-key m "a" 'gimp-apropos)
(define-key m "q" 'bury-buffer)
(define-key m "p" 'previous-line)
(define-key m "n" 'next-line)
(define-key m "i" 'run-gimp)
(define-key m "d" 'gimp-documentation)
(define-key m "s" 'gimp-code-search)
(define-key m "m" 'gimp-menu)
(define-key m "i" 'gimp-insert-sexp-at-repl)
(define-key m "t" 'outline-toggle-children)
(define-key m "S" 'gimp-selector)
(define-key m "\C-cf" 'gimp-describe-procedure)
(define-key m "\C-ca" 'gimp-apropos)
(define-key m "\C-ch" 'gimp-help)
(define-key m "\C-cd" 'gimp-documentation)
(define-key m "\C-cs" 'gimp-code-search)
(define-key m "\C-cm" 'gimp-menu)
m))
(setq inferior-gimp-mode-map
(append gimp-menu-map (cdr inferior-gimp-mode-map)))
(setq gimp-mode-map
(append gimp-menu-map (cdr gimp-mode-map)))
(setq gimp-help-mode-map
(append gimp-menu-map (cdr gimp-help-mode-map)))
(define-key gimp-help-mode-map
[(down-mouse-1)]
(lambda ()
"Put point where mouse is, and do what ENTER does at that spot."
(interactive)
(let ((event (read-event)))
(mouse-set-point event)
(gimp-help-enter))))
(defun gimp-help-enter (&optional event)
"Dispatch enter key or mouse click in GIMP Help buffer.
Optional argument EVENT is a mouse event."
(interactive)
(cond ((or (eq (field-at-pos (point)) 'submenu)
(eq (field-at-pos (1+ (point))) 'submenu))
(gimp-menu
(gimp-submenu-at-point (point))))
((or (eq (field-at-pos (point)) 'procedure)
(eq (field-at-pos (1+ (point))) 'procedure))
(let ((char (read-char "Insert at REPL or search source code [is]: ")))
(case char
(?i (gimp-insert-sexp-at-repl))
(?s (gimp-code-search (symbol-name (gimp-procedure-at-point)))))))
((gimp-procedure-at-point)
(gimp-describe-procedure (gimp-procedure-at-point t)))
((or (eq (field-at-pos (point)) 'email)
(eq (field-at-pos (1+ (point))) 'email))
(mail nil (field-string-no-properties (1+ (point)))
(symbol-name gimp-current-procedure)))
((or (eq (field-at-pos (point)) 'url)
(eq (field-at-pos (1+ (point))) 'url))
(browse-url (field-string-no-properties (1+ (point)))))
((or (eq (field-at-pos (point)) 'function)
(eq (field-at-pos (1+ (point))) 'function))
(let ((str (field-string-no-properties (1+ (point)))))
(gimp-describe-procedure (substring str 1 (1- (length str))))))
((or (eq (field-at-pos (point)) 'forward)
(eq (field-at-pos (1+ (point))) 'forward))
(gimp-help-forward))
((or (eq (field-at-pos (point)) 'back)
(eq (field-at-pos (1+ (point))) 'back))
(gimp-help-back))
(t (error "Nothing to do at point"))))
(defun gimp-space (n)
"Dispatch space to DWIM actions:
- in help (apropos) mode: echo documentation and move to next line
- in other modes:
- when in a string: insert N spaces
- otherwise: echo documentation
Note that - though this is not handled by this command - if space
is entered after a snippet abbreviation (see
`gimp-list-snippets'), a snippet is inserted in a gimp-mode
buffer."
(interactive "p")
(cond ((eq major-mode 'gimp-help-mode)
(if (gimp-describe-procedure-at-point)
(unless (= (point-at-eol) (point-max))
(call-interactively 'next-line))
(scroll-up n)))
(t
(if gimp-just-one-space
(just-one-space n)
(self-insert-command n))
(gimp-echo))))
;; Notify delete-selection-mode that gimp-space should act like what a space
;; event would do, that is, if/when delete-selection-mode is activated by the
;; user:
(put 'gimp-space 'delete-selection t)
;;;; Set up the modes
(define-derived-mode gimp-mode scheme-mode "GIMP mode"
"Mode for editing script-fu and interacting with an inferior gimp process."
(use-local-map gimp-mode-map)
(abbrev-mode 1)
(add-to-list 'mode-line-process gimp-mode-line-format t)
(setq indent-line-function 'lisp-indent-line)
(if (null gimp-oblist-cache)
(gimp-restore-caches))
(setf ;turn off CASE-FOLD in
;font-lock search
(caddr font-lock-defaults) nil))
(define-derived-mode inferior-gimp-mode inferior-scheme-mode
"Inferior GIMP"
"Mode for interaction with inferior gimp process."
(use-local-map inferior-gimp-mode-map)
(setq comint-input-filter-functions
'(gimp-add-define-to-oblist))
(add-to-list 'mode-line-process gimp-mode-line-format t))
;;;; Versioning
(defvar gimp-version "1.54"
"Gimp Mode version")
(gimp-defcommand gimp-gimp-mode-version ()
"Version of this mode."
(interactive)
(if (interactive-p)
(prog1 nil
(message "GIMP mode version: %s" gimp-version))
gimp-version))
(defalias 'gimp-mode-version 'gimp-gimp-mode-version)
(gimp-defcommand gimp-gimp-version ()
"Version of the GIMP."
(interactive)
(destructuring-bind (version major minor rev)
(gimp-string-match
"\\([[:digit:]]\\)\.\\([[:digit:]]\\)\.\\([[:digit:]]\\)"
(gimp-eval "(car (gimp-version))"))
(when (interactive-p)
(message "GIMP version: %s" version))
(list version major minor rev)))
(defun gimp-check-version-compatibility ()
(let ((m (string-match "\\([0-9]\.[0-9]\\)\\(\.exe\\)?$" gimp-program)))
(when (and m
(not
(string-match (concat (match-string 1 gimp-program) "$")
gimp-dir)))
(message "Version of `gimp-program' and `gimp-dir' \
do not match, this is probably an error, please correct"))))
;;;; Evaluation
(defmacro in-gimp (body)
"Evaluate fu sexps without having to quote them. Syntactic sugar.
Argument BODY is a SCHEME sexp. Caveats:
1. Inclusion of literal Scheme vectors is impossible. This is due to read
syntax of Emacs lisp and the use of [] in scheme as if they were
parentheses. Be sure to use portable (vector elem1 elem2...) if you want to
make a vector in SCHEME with `in-gimp'.
2. As body must be a single sexp, use (begin ...) in your scheme code.
3. Possibly others. Use it lightly."
`(gimp-eval
(format "%S" (backquote ,body))))
;; Core inferior interaction functions
(defun gimp-proc ()
(or (scheme-get-process)
gimp-cl-proc))
(defun gimp-filter (proc string)
"Filter for inferior gimp-process."
(setq gimp-output
(replace-regexp-in-string "Eval" "" (concat gimp-output string))))
(defun gimp-set-comint-filter ()
(set-process-filter
(gimp-proc)
'gimp-comint-filter))
(defun gimp-comint-filter (proc string)
(gimp-filter proc string)
(with-no-warnings
(when (featurep 'fud)
(fud-find-reference string)
(fud-echo-value string)
(setq string (fud-prettify-code string))))
(comint-output-filter
proc
(let ((string
(replace-regexp-in-string
(concat "\n" gimp-prompt " \nEval: (tracing 0)\nEval: tracing\nEval: 0\nApply to: (0)1")
"" string)))
(condition-case err
(setq string
(replace-regexp-in-string "^\\(?:Apply to\\|Eval\\|Gives\\): .*\n"
(lambda (m)
(gimp-set-face
m
(case
(aref m 0)
(?E shy)
(?A less-shy)
(?G visited-procedure))))
string))
(error nil))
(replace-regexp-in-string "[]()[]"
(lambda (m)
(gimp-set-face m red)) string))))
(defun scheme-send-string (string &optional newline)
"Send STRING to the scheme process.
When optional argument NEWLINE is non-nil, append a newline char."
(gimp-add-define-to-oblist string)
(comint-send-string
(gimp-proc)
(concat string (if newline "\n" ""))))
(defun gimp-send-last-sexp (&optional insert)
"Send the previous sexp to the inferior Scheme process."
(interactive "P")
(if (featurep 'fud)
(with-no-warnings (fud-update-breakpoints)))
(let ((result
(gimp-eval-to-string
(buffer-substring-no-properties
(save-excursion (backward-sexp) (point)) (point)))))
(message "%s" result)
(when (and insert (not (string-equal result "")))
(insert (substring result 0 -1)))))
(defun gimp-eval (string)
"Eval STRING, and return it read, somewhat, though not fully, elispified.
Best is to craft STRING so that script-fu returns something universal to the
Lisp world."
(let ((output (gimp-eval-to-string string)))
(if (string-match "^#" output) ;unreadable by the lisp reader
(if (string= (concat "#f\n" gimp-prompt " ") gimp-output) ;gimp returned #f
nil
(read (substring output 1))) ;so strip
(read output))))
(defun gimp-eval-to-string (string &optional discard timeout)
"Eval STRING in the GIMP.
With optional argument DISCARD, do not wait van return value.
Throw an error if TIMEOUT seconds have passed. This can be used
to avoid infinite looping. "
;; Apparently `with-local-quit' did not cut it, therefore the timeout
;; option'. Without TIMEOUT, just keep on trying until `gimp-output'
;; matches \"^> $\""; actually, replace > with value of `gimp-prompt'
(cond
((gimp-interactive-p)
(setq gimp-output "")
(set-process-filter (gimp-proc) 'gimp-filter)
(scheme-send-string string t)
(unless discard
(let ((time (current-time)))
(with-local-quit
(while
(not (string-match (concat "^" gimp-prompt " $") gimp-output)) ;prompt has not
;yet returned
(when (and timeout (> (cadr (time-since time)) timeout))
(error "%S Timed out" this-command))
(sit-for .01)) ;keep polling
(substring gimp-output 0 -2)))))
;; Bail early with an error message if the user needs to start the GIMP process:
((not (gimp-proc))
(gimp-error-must-run-gimp))
((eq (process-status gimp-cl-proc)
'open)
(gimp-cl-eval-to-string string discard))
(t "nil"))) ;strip prompt
(defun gimp-insert-input (event)
"Insert input field at point after prompt.
Argument EVENT is a mouse-event."
(interactive "e")
(mouse-set-point event)
(let ((pos (posn-point (event-end event)))
input)
(with-selected-window (posn-window (event-end event))
(when (eq (field-at-pos pos) 'input)
(setq input (field-string-no-properties pos))))
(comint-insert-input event)
(when (member input gimp-shortcuts)
(save-excursion
(comint-bol-or-process-mark)
(insert ?,)))))
(defun gimp-add-define-to-oblist (str)
"Put vars, functions and macros defined by STR in the oblist."
(set-text-properties 0 (length str) nil str)
(let*
((var-or-fun
(gimp-string-match
"[[:space:]]*(define\\(-macro\\)?[[:space:]]+(?\\([[:word:]-?!><]+\\)"
str 2)))
(if (and var-or-fun (not (member var-or-fun (gimp-get-oblist-cache))))
(push var-or-fun gimp-oblist-cache))))
(defun dotted-to-list (dl)
"'Undot' DL.
Turn DL (of the form (\"a\" \"b\" . \"c\")) into a list of the form (\"a\"
\"b\" \". c\"))."
(cond ((atom (cdr dl))
(cons (car dl)
(if (endp (cdr dl))
nil
(list (format ". %s" (cdr dl))))))
(t (cons (car dl)
(dotted-to-list (cdr dl))))))
(defun gimp-insert-and-send-string (string)
"In *GIMP* buffer, set the current string to STRING."
(save-window-excursion
(switch-to-buffer (process-buffer (gimp-proc)))
(unless (null comint-accum-marker)
(goto-char (point-max))
;; First delete any old unsent string at the end
(delete-region
(or (marker-position comint-accum-marker)
(process-mark (get-buffer-process (current-buffer))))
(point))
;; Insert the string