1 ;;; Sepia -- Simple Emacs-Perl InterAction: ugly, yet effective.
2 ;; (a.k.a. Septik -- Sean's Emacs-Perl Total Integration Kludge.)
4 ;; Author: Sean O'Rourke <seano@cpan.org>
5 ;; Keywords: Perl, languages
7 ;; Copyright (C) 2004-2009 Sean O'Rourke. All rights reserved, some
8 ;; wrongs reversed. This code is distributed under the same terms
13 ;; Sepia is a set of tools for Perl development in Emacs. Its goal is
14 ;; to extend CPerl mode with two contributions: fast code navigation
15 ;; and interactive development. It is inspired by Emacs' current
16 ;; support for a number of other languages, including Lisp, Python,
17 ;; Ruby, and Emacs Lisp.
19 ;; See sepia.texi, which comes with the distribution.
26 ;; try optional modules, but don't bitch if we fail:
27 (ignore-errors (require 'sepia-w3m
))
28 (ignore-errors (require 'sepia-tree
))
29 (ignore-errors (require 'sepia-ido
))
30 (ignore-errors (require 'sepia-snippet
))
31 ;; extensions that should always load (autoload later?)
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 ;;; Comint communication
37 (defvar sepia-perl5lib nil
38 "* List of extra PERL5LIB directories for `sepia-repl'.")
40 (defvar sepia-program-name
"perl"
41 "* Perl program name.")
43 (defvar sepia-view-pod-function
44 (if (featurep 'w3m
) 'sepia-w3m-view-pod
'sepia-perldoc-buffer
)
45 "* Function to view current buffer's documentation.
47 Useful values include `sepia-w3m-view-pod' and `sepia-perldoc-buffer'.")
49 (defvar sepia-module-list-function
50 (if (featurep 'w3m
) 'w3m-find-file
'browse-url-of-file
)
51 "* Function to view a list of installed modules.
53 Useful values include `w3m-find-file' and `browse-url-of-buffer'.")
55 (defvar sepia-complete-methods t
56 "* Non-nil if Sepia should try to complete methods for \"$x->\".
58 NOTE: this feature can be problematic, since it evaluates the
59 object in order to find its type. Currently completion is only
60 attempted for objects that are simple scalars.")
62 (defvar sepia-indent-expand-abbrev t
63 "* If non-NIL, `sepia-indent-or-complete' tries `expand-abbrev'.")
65 (defvar sepia-use-completion t
66 "* Use completion based on Xref database.
68 Turning this off may speed up some operations, if you don't mind
71 (defvar sepia-eval-defun-include-decls t
72 "* Generate and use a declaration list for `sepia-eval-defun'.
73 Without this, code often will not parse; with it, evaluation may
74 be a bit less responsive. Note that since this only includes
75 subs from the evaluation package, it may not always work.")
77 (defvar sepia-prefix-key
"\M-."
78 "* Prefix for functions in `sepia-keymap'.")
80 ;;; User options end here.
82 (defvar sepia-process nil
83 "The perl process with which we're interacting.")
84 (defvar sepia-output nil
85 "Current perl output for a response to `sepia-eval-raw', appended
86 to by `perl-collect-output'.")
87 (defvar sepia-passive-output
""
88 "Current perl output for miscellaneous user interaction, used to
89 look for \";;;###\" lisp evaluation markers.")
91 (defvar sepia-perl-builtins nil
92 "List of Perl builtins for completion.")
94 (defun sepia-collect-output (string)
95 "Collect perl output for `sepia-eval-raw' into sepia-output."
96 (setq sepia-output
(concat sepia-output string
))
99 (defun sepia-eval-raw (str)
100 "Evaluate perl code STR, returning a pair (RESULT-STRING . OUTPUT)."
101 (sepia-ensure-process)
104 (let ((sepia-output "")
106 (with-current-buffer (process-buffer sepia-process
)
107 (setq ocpof comint-preoutput-filter-functions
108 comint-preoutput-filter-functions
109 '(sepia-collect-output)))
110 (setq str
(concat "local $Sepia::STOPDIE=0;"
111 "local $Sepia::STOPWARN=0;"
112 "{ package " (sepia-buffer-package) ";"
114 (comint-send-string sepia-process
115 (concat (format "<<%d\n" (length str
)) str
))
116 (while (not (and sepia-output
117 (string-match "> $" sepia-output
)))
118 (accept-process-output sepia-process
))
119 (if (string-match "^;;;[0-9]+\n" sepia-output
)
121 (let* ((x (read-from-string sepia-output
122 (+ (match-beginning 0) 3)))
125 (prog1 (substring sepia-output
(1+ pos
) (+ len pos
1))
126 (setq start
(+ pos len
1))))
127 (and (string-match ";;;[0-9]+\n" sepia-output start
)
128 (let* ((x (read-from-string
130 (+ (match-beginning 0) 3)))
133 (substring sepia-output
(1+ pos
) (+ len pos
1)))))
134 (cons sepia-output nil
)))
135 (with-current-buffer (process-buffer sepia-process
)
136 (setq comint-preoutput-filter-functions ocpof
)))))
138 (defun sepia-eval (str &optional context detailed
)
139 "Evaluate STR in CONTEXT (void by default), and return its result
140 as a Lisp object. If DETAILED is specified, return a
141 pair (RESULT . OUTPUT)."
142 (let* ((tmp (sepia-eval-raw
145 (concat "Sepia::tolisp([" str
"])"))
147 (concat "Sepia::tolisp(scalar(" str
"))"))
148 (t (concat str
";1")))))
151 (setq res
(if context
152 (if (string= res
"") "" (car (read-from-string res
)))
158 (defun sepia-call (fn context
&rest args
)
159 "Call perl function FN in CONTEXT with arguments ARGS, returning
160 its result as a Lisp value."
161 (sepia-eval (concat fn
"(" (mapconcat #'sepia-lisp-to-perl args
", ") ")")
164 (defun sepia-watch-for-eval (string)
165 "Monitor inferior Perl output looking for Lisp evaluation
166 requests. The format for these requests is
167 \"\\n;;;###LENGTH\\nDATA\". Only one such request can come from
168 each inferior Perl prompt."
169 (setq sepia-passive-output
(concat sepia-passive-output string
))
171 ((string-match "^;;;###[0-9]+" sepia-passive-output
)
172 (if (string-match "^;;;###\\([0-9]+\\)\n\\(?:.\\|\n\\)*\n\\(.*> \\)"
173 sepia-passive-output
)
174 (let* ((len (car (read-from-string
175 (match-string 1 sepia-passive-output
))))
176 (pos (1+ (match-end 1)))
177 (res (ignore-errors (eval (car (read-from-string
178 sepia-passive-output pos
181 (substring sepia-passive-output pos
(+ pos len
)) res
)
182 (goto-char (point-max))
183 (insert (substring sepia-passive-output
(+ 1 pos len
)))
184 (set-marker (process-mark (get-buffer-process (current-buffer)))
186 (setq sepia-passive-output
""))
188 (t (setq sepia-passive-output
"") string
)))
191 (defvar sepia-metapoint-map
192 (let ((map (make-sparse-keymap)))
193 (when (featurep 'ido
)
194 (define-key map
"j" 'sepia-jump-to-symbol
))
195 (dolist (kv '(("c" . sepia-callers
)
196 ("C" . sepia-callees
)
197 ("a" . sepia-apropos
)
198 ("A" . sepia-var-apropos
)
199 ("v" . sepia-var-uses
)
200 ("V" . sepia-var-defs
)
201 ;; ("V" . sepia-var-assigns)
202 ("\M-." . sepia-dwim
)
203 ;; ("\M-." . sepia-location)
204 ("l" . sepia-location
)
206 ("r" . sepia-rebuild
)
207 ("m" . sepia-module-find
)
210 ("d" . sepia-perldoc-this
)
211 ("u" . sepia-describe-object
)))
212 (define-key map
(car kv
) (cdr kv
)))
214 "Keymap for Sepia functions. This is just an example of how you
215 might want to bind your keys, which works best when bound to
218 (defvar sepia-shared-map
219 (let ((map (make-sparse-keymap)))
220 (define-key map sepia-prefix-key sepia-metapoint-map
)
221 (define-key map
"\M-," 'sepia-next
)
222 (define-key map
"\C-\M-x" 'sepia-eval-defun
)
223 (define-key map
"\C-c\C-l" 'sepia-load-file
)
224 (define-key map
"\C-c\C-p" 'sepia-view-pod
) ;was cperl-pod-spell
225 (define-key map
"\C-c\C-d" 'cperl-perldoc
)
226 (define-key map
"\C-c\C-r" 'sepia-repl
)
227 (define-key map
"\C-c\C-s" 'sepia-scratch
)
228 (define-key map
"\C-c\C-e" 'sepia-eval-expression
)
229 (define-key map
"\C-c!" 'sepia-set-cwd
)
230 (define-key map
(kbd "TAB") 'sepia-indent-or-complete
)
232 "Sepia bindings common to all modes.")
235 (defun sepia-perldoc-this (name)
236 "View perldoc for module at point."
237 (interactive (list (sepia-interactive-arg 'module
)))
238 (let ((wc (current-window-configuration))
239 (old-pd (symbol-function 'w3m-about-perldoc
))
240 (old-pdb (symbol-function 'w3m-about-perldoc-buffer
)))
241 (condition-case stuff
242 (flet ((w3m-about-perldoc (&rest args
)
243 (let ((res (apply old-pd args
)))
244 (or res
(error "lose: %s" args
))))
245 (w3m-about-perldoc-buffer (&rest args
)
246 (let ((res (apply old-pdb args
)))
247 (or res
(error "lose: %s" args
)))))
248 (funcall (if (featurep 'w3m
) 'w3m-perldoc
'cperl-perldoc
) name
))
249 (error (set-window-configuration wc
)))))
251 (defun sepia-view-pod ()
252 "View POD for the current buffer."
254 (funcall sepia-view-pod-function
))
256 (defun sepia-module-list ()
257 "List installed modules with links to their documentation.
259 This lists not just top-level packages appearing in packlist
260 files, but all documented modules on the system, organized by
263 (let ((file "/tmp/modlist.html"))
264 ;; (unless (file-exists-p file)
265 (sepia-eval-raw (format "Sepia::html_module_list(\"%s\")" file
))
266 (funcall sepia-module-list-function file
)))
268 (defun sepia-package-list ()
269 "List installed packages with links to their documentation.
271 This lists only top-level packages appearing in packlist files.
272 For modules within packages, see `sepia-module-list'."
274 (let ((file "/tmp/packlist.html"))
275 ;; (unless (file-exists-p file)
276 (sepia-eval-raw (format "Sepia::html_package_list(\"%s\")" file
))
277 (funcall sepia-module-list-function file
)))
279 (defun sepia-perldoc-buffer ()
280 "View current buffer's POD using pod2html and `browse-url'.
282 Interactive users should call `sepia-view-pod'."
283 (let ((buffer (get-buffer-create "*sepia-pod*"))
284 (errs (get-buffer-create "*sepia-pod-errors*"))
285 (inhibit-read-only t
))
286 (with-current-buffer buffer
(erase-buffer))
287 (save-window-excursion
288 (shell-command-on-region (point-min) (point-max) "pod2html"
290 (with-current-buffer buffer
(browse-url-of-buffer))))
292 (defun sepia-perl-name (sym &optional mod
)
293 "Convert a Perl name to a Lisp name."
294 (setq sym
(substitute ?_ ?-
(if (symbolp sym
) (symbol-name sym
) sym
)))
296 (concat mod
"::" sym
)
299 (defun sepia-live-p ()
300 (and (processp sepia-process
)
301 (eq (process-status sepia-process
) 'run
)))
303 (defun sepia-ensure-process (&optional remote-host
)
304 (unless (sepia-live-p)
305 (with-current-buffer (get-buffer-create "*sepia-repl*")
307 (set (make-local-variable 'sepia-passive-output
) ""))
309 (comint-exec "*sepia-repl*" "attachtty" "attachtty" nil
311 (let ((stuff (split-string sepia-program-name nil t
)))
312 (comint-exec (get-buffer-create "*sepia-repl*")
313 "perl" (car stuff
) nil
316 (mapcar (lambda (x) (concat "-I" x
)) sepia-perl5lib
)
317 '("-MSepia" "-MSepia::Xref"
318 "-e" "Sepia::repl")))))
319 (setq sepia-process
(get-buffer-process "*sepia-repl*"))
320 (accept-process-output sepia-process
1)
321 ;; Steal a bit from gud-common-init:
323 (setq gud-last-last-frame nil
)
324 (set-process-filter sepia-process
'gud-filter
)
325 (set-process-sentinel sepia-process
'gud-sentinel
)))
328 (defun sepia-repl (&optional remote-host
)
329 "Start the Sepia REPL."
330 (interactive (list (and current-prefix-arg
331 (read-string "Host: "))))
332 (sepia-init) ;; set up keymaps, etc.
333 (sepia-ensure-process remote-host
)
334 (pop-to-buffer (get-buffer "*sepia-repl*")))
336 (defun sepia-cont-or-restart ()
338 (if (get-buffer-process (current-buffer))
339 (gud-cont current-prefix-arg
)
342 (defvar sepia-repl-mode-map
343 (let ((map (copy-keymap sepia-shared-map
)))
344 (set-keymap-parent map gud-mode-map
)
345 (define-key map
(kbd "<tab>") 'comint-dynamic-complete
)
346 (define-key map
"\C-a" 'comint-bol
)
347 (define-key map
"\C-c\C-r" 'sepia-cont-or-restart
)
350 "Keymap for Sepia interactive mode.")
352 (define-derived-mode sepia-repl-mode gud-mode
"Sepia REPL"
353 "Major mode for the Sepia REPL.
355 \\{sepia-repl-mode-map}"
356 ;; (set (make-local-variable 'comint-use-prompt-regexp) t)
357 (modify-syntax-entry ?
: "_")
358 (modify-syntax-entry ?
> ".")
359 (set (make-local-variable 'comint-prompt-regexp
) "^[^>\n]*> *")
360 (set (make-local-variable 'gud-target-name
) "sepia")
361 (set (make-local-variable 'gud-marker-filter
) 'sepia-gud-marker-filter
)
362 (set (make-local-variable 'gud-minor-mode
) 'sepia
)
363 (sepia-install-eldoc)
365 (setq gud-comint-buffer
(current-buffer))
366 (setq gud-last-last-frame nil
)
367 (setq gud-sepia-acc nil
)
369 (gud-def gud-break
",break %f:%l" "\C-b" "Set breakpoint at current line.")
370 (gud-def gud-step
",step %p" "\C-s" "Step one line.")
371 (gud-def gud-next
",next %p" "\C-n" "Step one line, skipping calls.")
372 (gud-def gud-cont
",continue" "\C-r" "Continue.")
373 (gud-def gud-print
"%e" "\C-p" "Evaluate something.")
374 (gud-def gud-remove
",delete %l %f" "\C-d" "Delete current breakpoint.")
375 ;; Sadly, this hoses our keybindings.
376 (compilation-shell-minor-mode 1)
377 (set (make-local-variable 'comint-dynamic-complete-functions
)
378 '(sepia-complete-symbol comint-dynamic-complete-filename
))
379 (set (make-local-variable 'comint-preoutput-filter-functions
)
380 '(sepia-watch-for-eval))
381 (run-hooks 'sepia-repl-mode-hook
)
384 (defvar gud-sepia-acc nil
385 "Accumulator for `sepia-gud-marker-filter'.")
387 (defun sepia-gud-marker-filter (str)
390 (concat gud-sepia-acc str
)
392 (while (string-match "_<\\([^:>]+\\):\\([0-9]+\\)>\\(.*\\)" gud-sepia-acc
)
393 (setq gud-last-last-frame gud-last-frame
395 (match-string 1 gud-sepia-acc
)
396 (string-to-number (match-string 2 gud-sepia-acc
)))
397 gud-sepia-acc
(match-string 3 gud-sepia-acc
)))
399 (if (string-match "\\(_<.*\\)" gud-sepia-acc
)
400 (match-string 1 gud-sepia-acc
)
404 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
407 (defun define-xref-function (package name doc
)
408 "Define a lisp mirror for a low-level Sepia function."
409 (let ((lisp-name (intern (format "xref-%s" name
)))
410 (pl-name (sepia-perl-name name package
)))
411 (fmakunbound lisp-name
)
412 (eval `(defun ,lisp-name
(&rest args
)
414 (apply #'sepia-call
,pl-name
'list-context args
)))))
416 (defun define-modinfo-function (name &optional doc context
)
417 "Define a lisp mirror for a function from Module::Info."
418 (let ((name (intern (format "sepia-module-%s" name
)))
419 (pl-func (sepia-perl-name name
))
420 (full-doc (concat (or doc
"") "
422 This function uses Module::Info, so it does not require that the
423 module in question be loaded.")))
424 (when (fboundp name
) (fmakunbound name
))
425 (eval `(defun ,name
(mod)
427 (interactive (list (sepia-interactive-arg 'module
)))
429 (sepia-call "Sepia::module_info" ',(or context
'scalar-context
)
433 (defun sepia-thing-at-point (what)
434 "Like `thing-at-point', but hacked to avoid REPL prompt."
435 (let ((th (thing-at-point what
)))
436 (and th
(not (string-match "[ >]$" th
)) th
)))
438 (defvar sepia-sub-re
"^ *sub\\s +\\(.+\\_>\\)")
440 (defvar sepia-history nil
)
442 (defun sepia-interactive-arg (&optional sepia-arg-type
)
443 "Default argument for most Sepia functions. TYPE is a symbol --
444 either 'file to look for a file, or anything else to use the
446 (let* ((default (case sepia-arg-type
447 (file (or (thing-at-point 'file
) (buffer-file-name)))
448 (t (sepia-thing-at-point 'symbol
))))
449 (text (capitalize (symbol-name sepia-arg-type
)))
451 (lambda (str &rest blah
)
452 (let ((completions (xref-completions
455 (variable "VARIABLE")
459 (when (eq sepia-arg-type
'module
)
461 (remove-if (lambda (x) (string-match "::$" x
)) completions
)))
464 (format "%s [%s]: " text default
)
465 (format "%s: " text
)))
466 (ret (if sepia-use-completion
467 (completing-read prompt
'blah-choices nil nil nil
'sepia-history
469 (read-string prompt nil
'sepia-history default
))))
470 (push ret sepia-history
)
473 (defun sepia-interactive-module ()
474 "Guess which module we should look things up in. Prompting for a
475 module all the time is a PITA, but I don't think this (choosing
476 the current file's module) is a good alternative, either. Best
477 would be to choose the module based on what we know about the
479 (let ((xs (xref-file-modules (buffer-file-name))))
480 (if (= (length xs
) 1)
484 (defun sepia-maybe-echo (result &optional print-message
)
486 (message "%s" result
))
489 (defun sepia-find-module-file (mod)
490 (or (sepia-module-file mod
)
491 (car (xref-guess-module-file mod
))))
493 (defun sepia-module-find (mod)
494 "Find the file defining module MOD."
495 (interactive (list (sepia-interactive-arg 'module
)))
496 (let ((fn (sepia-find-module-file mod
)))
499 (message "Module %s in %s." mod fn
)
500 (pop-to-buffer (find-file-noselect (expand-file-name fn
))))
501 (message "Can't find module %s." mod
))))
503 (defmacro ifa
(test then
&rest else
)
505 (if it
,then
,@else
)))
507 (defvar sepia-found-refiner
)
509 (defun sepia-show-locations (locs)
511 (pop-to-buffer (get-buffer-create "*sepia-places*"))
512 (let ((inhibit-read-only t
))
514 (dolist (loc (sort (remove nil locs
) ; XXX where's nil from?
516 (or (string< (car a
) (car b
))
517 (and (string= (car a
) (car b
))
518 (< (second a
) (second b
)))))))
519 (destructuring-bind (file line name
&rest blah
) loc
520 (let ((str (ifa (find-buffer-visiting file
)
521 (with-current-buffer it
522 (ifa sepia-found-refiner
523 (funcall it line name
)
525 (message "line for %s was %d, now %d" name line
526 (line-number-at-pos))
527 (setq line
(line-number-at-pos))
529 (buffer-substring (sepia-bol-from (point))
530 (sepia-eol-from (point)))))
531 (if (> (length tmpstr
) 60)
532 (concat "\n " tmpstr
)
535 (insert (format "%s:%d:%s\n" (abbreviate-file-name file
) line str
)))))
537 (goto-char (point-min)))))
539 (defmacro define-sepia-query
(name doc
&optional gen test prompt
)
540 "Define a sepia querying function."
541 `(defun ,name
(ident &optional module file line display-p
)
544 With prefix arg, list occurences in a `grep-mode' buffer.
545 Without, place the occurrences on `sepia-found', so that
546 calling `sepia-next' will cycle through them.
548 Depending on the query, MODULE, FILE, and LINE may be used to
549 narrow the results, as long as doing so leaves some matches.
550 When called interactively, they are taken from the current
553 (interactive (list (sepia-interactive-arg ,(or prompt
''function
))
554 (sepia-interactive-module)
556 (line-number-at-pos (point))
561 `(let ((tmp (,gen ident module file line
)))
562 (or (mapcan #',test tmp
) tmp
))
563 `(,gen ident module file line
))))
564 ;; Always clear out the last found ring, because it's confusing
566 (sepia-set-found nil
,(or prompt
''function
))
568 (sepia-show-locations ret
)
569 (sepia-set-found ret
,(or prompt
''function
))
572 (define-sepia-query sepia-defs
573 "Find all definitions of sub."
577 (define-sepia-query sepia-callers
578 "Find callers of FUNC."
582 (define-sepia-query sepia-callees
583 "Find a sub's callees."
587 (define-sepia-query sepia-var-defs
588 "Find a var's definitions."
590 (lambda (x) (setf (third x
) ident
) (list x
))
593 (define-sepia-query sepia-var-uses
596 (lambda (x) (setf (third x
) ident
) (list x
))
599 (define-sepia-query sepia-var-assigns
600 "Find/list assignments to a variable."
602 (lambda (x) (setf (third x
) ident
) (list x
))
605 (defalias 'sepia-package-defs
'sepia-module-describe
)
607 (define-sepia-query sepia-apropos
608 "Find/list subroutines matching regexp."
609 (lambda (name &rest blah
) (xref-apropos name
1))
613 (define-sepia-query sepia-var-apropos
614 "Find/list variables matching regexp."
619 (defun sepia-location (name &optional jump-to
)
620 "Find the definition of NAME.
622 When called interactively (or with JUMP-TO true), go directly
624 (interactive (list (sepia-interactive-arg 'function
) t
))
625 (let* ((fl (or (car (xref-location name
))
626 (car (remove-if #'null
627 (apply #'xref-location
(xref-apropos name
)))))))
628 (when (and (car fl
) (string-match "^(eval " (car fl
)))
629 (message "Can't find definition of %s in %s." name
(car fl
))
633 (sepia-set-found (list fl
) 'function
)
635 (message "No definition for %s." name
))
639 (defun sepia-dwim (&optional display-p
)
640 "Try to do the right thing with identifier at point.
641 * Find all definitions, if thing-at-point is a function
642 * Find all uses, if thing-at-point is a variable
643 * Find documentation, if thing-at-point is a module
647 (multiple-value-bind (type obj
) (sepia-ident-at-point)
648 (sepia-set-found nil type
)
649 (let* ((module-doc-p nil
)
652 ((member type
'(?% ?$ ?
@)) (xref-var-defs obj
))
654 (let (case-fold-search)
655 (string-match "^[^A-Z]" obj
)))
656 (list (sepia-location obj
)))
657 ((sepia-looks-like-module obj
)
658 (setq module-doc-p t
)
659 `((,(sepia-perldoc-this obj
) 1 nil nil
)))
660 (t (setq module-doc-p t
)
661 (call-interactively 'sepia-defs
)))))
664 (sepia-show-locations ret
)
665 (sepia-set-found ret type
)
668 (defun sepia-rebuild ()
669 "Rebuild the Xref database."
673 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
674 ;;; Perl motion commands.
676 ;;; XXX -- these are a hack to prevent infinite recursion calling
677 ;;; e.g. beginning-of-defun from beginning-of-defun-function.
678 ;;; `beginning-of-defun' should handle this.
679 (defmacro sepia-safe-bodf
(&optional n
)
680 `(let ((beginning-of-defun-function
681 (if (and (boundp 'beginning-of-defun-function
)
682 (eq beginning-of-defun-function
'sepia-beginning-of-defun
))
684 beginning-of-defun-function
)))
685 (beginning-of-defun ,n
)))
687 (defmacro sepia-safe-eodf
(&optional n
)
688 `(let ((end-of-defun-function
689 (if (and (boundp 'end-of-defun-function
)
690 (eq end-of-defun-function
'sepia-end-of-defun
))
692 end-of-defun-function
)))
695 (defun sepia-beginning-of-defun (&optional n
)
696 "Move to beginning of current function.
698 The prefix argument is the same as for `beginning-of-defun'."
703 (sepia-end-of-defun (- n
))
705 (re-search-backward sepia-sub-re nil nil n
)))
707 (defun sepia-inside-defun ()
708 "True if point is inside a sub."
712 (re-search-backward sepia-sub-re
)
713 (when (< (point) cur
)
720 (defun sepia-end-of-defun (&optional n
)
721 "Move to end of current function.
723 The prefix argument is the same as for `end-of-defun'."
727 (sepia-beginning-of-defun (- n
))
729 ;; If we're outside a defun, skip to the next
731 (unless (sepia-inside-defun)
732 (re-search-forward sepia-sub-re
)
735 (re-search-backward sepia-sub-re
)
741 (defun sepia-rename-lexical (old new
&optional prompt
)
742 "Replace lexical variable OLD with NEW in the current function.
744 With prefix argument, query for each replacement. It is an error
745 to call this outside a function."
747 (let ((old (sepia-thing-at-point 'symbol
)))
748 (list (read-string "Old name: " old nil old
)
749 (read-string "New name: ")
750 current-prefix-arg
)))
751 (message "(%s %s)" old new
)
752 (unless (sepia-inside-defun)
753 (error "Can't rename %s outside a defun." old
))
754 (setq old
(concat "\\([$%@]\\)\\_<" (regexp-quote old
) "\\_>")
757 (let ((bod (sepia-beginning-of-defun))
758 (eod (sepia-end-of-defun)))
760 (query-replace-regexp old new nil bod eod
)
761 (replace-regexp old new nil bod eod
))))
763 (defun sepia-defun-around-point (&optional where
)
764 "Return the text of function around point."
766 (setq where
(point)))
769 (and (sepia-beginning-of-defun)
770 (match-string-no-properties 1))))
772 (defun sepia-lexicals-at-point (&optional where
)
773 "Find lexicals in scope at point."
776 (setq where
(point)))
777 (let ((subname (sepia-defun-around-point where
))
778 (mod (sepia-buffer-package)))
779 (xref-lexicals (sepia-perl-name subname mod
))))
782 (defun sepia-load-file (file &optional rebuild-p collect-warnings
)
783 "Reload a file (interactively, the current buffer's file).
785 With REBUILD-P (or a prefix argument when called interactively),
786 also rebuild the xref database."
787 (interactive (list (expand-file-name (buffer-file-name))
789 (format "*%s errors*" (buffer-file-name))))
791 (when collect-warnings
792 (let (kill-buffer-query-functions)
794 (kill-buffer collect-warnings
))))
795 (let* ((tmp (sepia-eval (format "do '%s' || ($@ && do { local $Sepia::Debug::STOPDIE; die $@ })" file
)
799 (message "sepia: %s returned %s" (abbreviate-file-name file
)
800 (if (equal res
"") "undef" res
))
801 (when (and collect-warnings
803 (with-current-buffer (get-buffer-create collect-warnings
)
804 (let ((inhibit-read-only t
))
805 (delete-region (point-min) (point-max))
807 (sepia-display-errors (point-min) (point-max))
808 (pop-to-buffer (current-buffer))))))
814 (defun sepia-set-found (list &optional type
)
816 (remove-if (lambda (x)
818 (and (not (car x
)) (string= (fourth x
) "main"))))
820 (setq sepia-found
(cons -
1 list
))
821 (setq sepia-found-refiner
(sepia-refiner type
)))
823 (defun sepia-refiner (type)
827 (let ((sub-re (concat "^\\s *sub\\s +.*" ident
"\\_>")))
828 ;; Test this because sometimes we get lucky and get the line
829 ;; just right, in which case beginning-of-defun goes to the
835 (looking-at sub-re
)))
836 (progn (goto-char (point-min))
837 (re-search-forward sub-re nil t
)))
838 (beginning-of-line))))
839 ;; Old version -- this may actually work better if
840 ;; beginning-of-defun goes flaky on us.
841 ;; (or (re-search-backward sub-re
842 ;; (sepia-bol-from (point) -20) t)
843 ;; (re-search-forward sub-re
844 ;; (sepia-bol-from (point) 10) t))
845 ;; (beginning-of-line)
848 (let ((var-re (concat "\\_<" ident
"\\_>")))
850 (line (goto-line line
)
851 (or (re-search-backward var-re
(sepia-bol-from (point) -
5) t
)
852 (re-search-forward var-re
(sepia-bol-from (point) 5) t
)))
853 (t (goto-char (point-min))
854 (re-search-forward var-re nil t
))))))
855 (t (lambda (line ident
) (and line
(goto-line line
))))))
857 (defun sepia-next (&optional arg
)
858 "Go to the next thing (e.g. def, use) found by sepia."
860 (or arg
(setq arg
1))
861 (if (cdr sepia-found
)
862 (let ((i (car sepia-found
))
863 (list (cdr sepia-found
))
864 (len (length (cdr sepia-found
)))
865 (next (+ (car sepia-found
) arg
))
867 (if (and (= len
1) (>= i
0))
868 (message "No more definitions.")
869 ;; if stepwise found next or previous item, it can cycle
870 ;; around the `sepia-found'. When at first or last item, get
880 (setq prompt
"Last one! ")
882 (setq prompt
"First one! "))))
883 ;; if we skip several item, when arrive the first or last
884 ;; item, we will stop at the one. But if we already at last
885 ;; item, then keep going
888 (setq i
(mod next len
))
890 prompt
"First one!"))
893 (setq i
(mod next len
))
895 prompt
"Last one!")))))
896 (setcar sepia-found i
)
897 (setq next
(nth i list
))
898 (let ((file (car next
))
903 (setq file
(and mod
(sepia-find-module-file mod
)))
906 (error "No file for %s." (car next
))))
907 (message "%s at %s:%s. %s" short file line prompt
)
908 (when (file-exists-p file
)
909 (find-file (or file
(sepia-find-module-file mod
)))
910 (when sepia-found-refiner
911 (funcall sepia-found-refiner line short
))
914 (message "No more definitions.")))
916 (defun sepia-previous (&optional arg
)
918 (or arg
(setq arg
1))
919 (sepia-next (- arg
)))
921 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
924 (defun sepia-ident-before-point ()
925 "Find the Perl identifier at or preceding point."
927 (skip-syntax-backward " ")
929 (sepia-ident-at-point)))
931 (defun sepia-simple-method-before-point ()
932 "Find the \"simple\" method call before point.
934 Looks for a simple method called on a variable before point and
935 returns the list (OBJECT METHOD). For example, \"$x->blah\"
936 returns '(\"$x\" \"blah\"). Only simple methods are recognized,
937 because completing anything evaluates it, so completing complex
938 expressions would lead to disaster."
939 (when sepia-complete-methods
941 (bound (max (- (point) 100) (point-min)))
944 ;; XXX - can't do this because COMINT's syntax table is weird.
945 ;; (skip-syntax-backward "_w")
946 (skip-chars-backward "a-zA-Z0-9_")
947 (when (looking-back "->\\s *" bound
)
948 (setq arrow
(search-backward "->" bound
))
949 (skip-chars-backward "a-zA-Z0-9_:")
952 ((char-equal (char-before (point)) ?$
)
953 (setq beg
(1- (point))))
955 ((multiple-value-bind (type obj
) (sepia-ident-at-point)
957 (sepia-looks-like-module obj
)))
960 (list (buffer-substring-no-properties beg arrow
)
961 (buffer-substring-no-properties (+ 2 arrow
) end
)
962 (buffer-substring-no-properties beg end
))))))))
964 (defun sepia-ident-at-point ()
965 "Find the Perl identifier at point."
967 (let ((orig (point)))
968 (when (looking-at "[%$@*&]")
971 (when (re-search-backward "[^A-Za-z_0-9:]" nil
'mu
)
974 (sigil (if (= beg
(point-min))
976 (char-before (point))))
978 (when (re-search-forward "[^A-Za-z_0-9:]" nil
'mu
)
982 ;; try special variables
983 (if (and (member (char-before orig
) '(?$ ?
@ ?%
))
984 (member (car (syntax-after orig
)) '(1 4 5 7 9)))
985 (list (char-before orig
)
986 (buffer-substring-no-properties orig
(1+ orig
)))
989 (list (when (member sigil
'(?$ ?
@ ?% ?
* ?
&)) sigil
)
990 (buffer-substring-no-properties beg end
)))))))
992 (defun sepia-function-at-point ()
993 "Find the Perl function called at point."
998 (sepia-beginning-of-defun)
1001 (sepia-end-of-defun)
1002 (when (and (>= pt bof
) (< pt
(point)))
1003 (sepia-beginning-of-defun)
1004 (when (and (= (point) bof
) (looking-at "\\s *sub\\s +"))
1005 (forward-char (length (match-string 0)))
1006 (concat (or (sepia-buffer-package) "")
1008 (cadr (sepia-ident-at-point)))))))
1011 (defun sepia-repl-complete ()
1012 "Try to complete the word at point in the REPL.
1013 Just like `sepia-complete-symbol', except that it also completes
1018 (defvar sepia-shortcuts
1019 '("break" "cd" "debug" "define" "delete" "eval" "format" "help" "lsbreak"
1020 "methods" "package" "pwd" "quit" "reload" "shell" "size" "strict" "undef"
1022 "List of currently-defined REPL shortcuts.
1024 XXX: this needs to be updated whenever you add one on the Perl side.")
1026 (defun sepia-complete-symbol ()
1027 "Try to complete the word at point.
1028 The word may be either a global or lexical variable if it has a
1029 sigil, a module, or a function. The function currently ignores
1030 module qualifiers, which may be annoying in larger programs.
1032 The function is intended to be bound to \\M-TAB, like
1033 `lisp-complete-symbol'."
1035 (let ((win (get-buffer-window "*Completions*" 0))
1040 (if (and (eq last-command this-command
)
1041 win
(window-live-p win
) (window-buffer win
)
1042 (buffer-name (window-buffer win
)))
1044 ;; If this command was repeated, and
1045 ;; there's a fresh completion window with a live buffer,
1046 ;; and this command is repeated, scroll that window.
1047 (with-current-buffer (window-buffer win
)
1048 (if (pos-visible-in-window-p (point-max) win
)
1049 (set-window-start win
(point-min))
1050 (save-selected-window
1054 ;; Otherwise actually do completion:
1055 ;; 0 - try a shortcut
1056 (when (eq major-mode
'sepia-repl-mode
)
1059 (when (looking-at ",\\([a-z]+\\)$")
1060 (let ((str (match-string 1)))
1061 (setq len
(length str
)
1062 completions
(all-completions str sepia-shortcuts
))))))
1063 ;; 1 - Look for a method call:
1065 (setq meth
(sepia-simple-method-before-point))
1067 (setq len
(length (caddr meth
))
1068 completions
(xref-method-completions
1069 (cons 'expr
(format "'%s'" (car meth
)))
1072 type
(format "%s->" (car meth
)))))
1073 ;; 1.x - look for a module
1076 (and (looking-back " *\\(?:use\\|require\\|package\\|no\\)\\s +[^ ]*" (sepia-bol-from (point)))
1077 (xref-apropos-module
1078 (multiple-value-bind (typ name
)
1079 (sepia-ident-before-point)
1080 (setq len
(length name
))
1084 (multiple-value-bind (typ name
) (sepia-ident-before-point)
1086 ;; 2 - look for a regular function/variable/whatever
1088 len
(+ (if type
1 0) (length name
))
1089 completions
(xref-completions
1098 (and (eq major-mode
'sepia-mode
)
1099 (sepia-function-at-point)))))
1100 ;; 3 - try a Perl built-in
1101 (when (and (not completions
)
1102 (or (not type
) (eq type ?
&)))
1103 (when (string-match ".*::([^:]+)$" name
)
1104 (setq name
(match-string 1 name
)))
1105 (setq completions
(all-completions name sepia-perl-builtins
)))
1106 (case (length completions
)
1107 (0 (message "No completions.") nil
)
1108 (1 ;; XXX - skip sigil to match s-i-before-point
1109 (delete-region (- (point) len
) (point))
1110 (insert (or type
"") (car completions
))
1111 ;; Hide stale completions buffer (stolen from lisp.el).
1112 (if win
(with-selected-window win
(bury-buffer))) t
)
1114 (new (try-completion "" completions
)))
1115 (if (<= (length new
) (length old
))
1116 (with-output-to-temp-buffer "*Completions*"
1117 (display-completion-list completions
))
1118 (let ((win (get-buffer-window "*Completions*" 0)))
1119 (if win
(with-selected-window win
(bury-buffer))))
1120 (delete-region (- (point) len
) (point))
1121 (insert (or type
"") new
))))))
1124 (defun sepia-indent-or-complete ()
1125 "Indent the current line or complete the symbol around point.
1127 Specifically, try completion when indentation doesn't move point.
1128 This function is intended to be bound to TAB."
1130 (let ((pos (point)))
1131 (let (beginning-of-defun-function
1132 end-of-defun-function
)
1133 (cperl-indent-command))
1134 (when (and (= pos
(point))
1136 (or (eq last-command
'sepia-indent-or-complete
)
1137 (looking-at "\\_>")))
1138 (when (or (not sepia-indent-expand-abbrev
)
1139 (and (not (expand-abbrev))
1140 ;; XXX this shouldn't be necessary, but
1141 ;; expand-abbrev returns NIL for e.g. the "else"
1144 (sepia-complete-symbol)))))
1146 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1149 (defvar sepia-mode-map
1150 (let ((map (copy-keymap sepia-shared-map
)))
1151 (set-keymap-parent map cperl-mode-map
)
1152 (define-key map
"\C-c\C-h" nil
)
1154 "Keymap for Sepia mode.")
1157 (define-derived-mode sepia-mode cperl-mode
"Sepia"
1158 "Major mode for Perl editing, derived from cperl mode.
1161 (sepia-install-eldoc)
1163 (set (make-local-variable 'beginning-of-defun-function
)
1164 'sepia-beginning-of-defun
)
1165 (set (make-local-variable 'end-of-defun-function
)
1166 'sepia-end-of-defun
)
1167 (setq indent-line-function
'sepia-indent-line
))
1169 (defun sepia-init ()
1170 "Perform the initialization necessary to start Sepia."
1172 ;; Create glue wrappers for Module::Info funcs.
1173 (unless (fboundp 'xref-completions
)
1174 (dolist (x '((name "Find module name.\n\nDoes not require loading.")
1175 (version "Find module version.\n\nDoes not require loading.")
1176 (inc-dir "Find directory in which this module was found.\n\nDoes not require loading.")
1177 (file "Absolute path of file defining this module.\n\nDoes not require loading.")
1178 (is-core "Guess whether or not a module is part of the core distribution.
1179 Does not require loading.")
1180 (modules-used "List modules used by this module.\n\nRequires loading." list-context
)
1181 (packages-inside "List sub-packages in this module.\n\nRequires loading." list-context
)
1182 (superclasses "List module's superclasses.\n\nRequires loading." list-context
)))
1183 (apply #'define-modinfo-function x
))
1184 ;; Create low-level wrappers for Sepia
1185 (dolist (x '((completions "Find completions in the symbol table.")
1186 (method-completions "Complete on an object's methods.")
1187 (location "Find an identifier's location.")
1188 (mod-subs "Find all subs defined in a package.")
1189 (mod-decls "Generate declarations for subs in a package.")
1190 (mod-file "Find the file defining a package.")
1191 (apropos "Find subnames matching RE.")
1192 (lexicals "Find lexicals for a sub.")
1193 (apropos-module "Find installed modules matching RE.")
1195 (apply #'define-xref-function
"Sepia" x
))
1197 (dolist (x '((rebuild "Build Xref database for current Perl process.")
1198 (redefined "Rebuild Xref information for a given sub.")
1200 (callers "Find all callers of a function.")
1201 (callees "Find all functions called by a function.")
1203 (var-apropos "Find varnames matching RE.")
1204 (mod-apropos "Find modules matching RE.")
1205 (file-apropos "Find files matching RE.")
1207 (var-defs "Find all definitions of a variable.")
1208 (var-assigns "Find all assignments to a variable.")
1209 (var-uses "Find all uses of a variable.")
1211 (mod-redefined "Rebuild Xref information for a given package.")
1212 (guess-module-file "Guess file corresponding to module.")
1213 (file-modules "List the modules defined in a file.")))
1214 (apply #'define-xref-function
"Sepia::Xref" x
))
1215 ;; Initialize built hash
1216 (sepia-init-perl-builtins)))
1218 (defvar sepia-scratchpad-mode-map
1219 (let ((map (make-sparse-keymap)))
1220 (set-keymap-parent map sepia-mode-map
)
1221 (define-key map
"\C-j" 'sepia-scratch-send-line
)
1225 (define-derived-mode sepia-scratchpad-mode sepia-mode
"Sepia-Scratch"
1226 "Major mode for the Perl scratchpad, derived from Sepia mode."
1230 (defun sepia-scratch ()
1231 "Switch to the sepia scratchpad."
1234 (or (get-buffer "*sepia-scratch*")
1235 (with-current-buffer (get-buffer-create "*sepia-scratch*")
1236 (sepia-scratchpad-mode)
1237 (current-buffer)))))
1239 (defun sepia-scratch-send-line (&optional scalarp
)
1240 "Send the current line to perl, and display the result."
1246 (concat "$Sepia::REPL{eval}->(q#"
1247 (buffer-substring (sepia-bol-from (point))
1248 (sepia-eol-from (point))) "#)"))))))
1250 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1253 (defun sepia-indent-line (&rest args
)
1254 "Unbind `beginning-of-defun-function' to not confuse `cperl-indent-line'."
1255 (let (beginning-of-defun-function)
1256 (apply #'cperl-indent-line args
)))
1258 (defun sepia-string-count-matches (reg str
)
1261 (while (setq pos
(string-match reg str
(1+ pos
)))
1265 (defun sepia-perlize-region-internal (pre post beg end replace-p
)
1266 "Pass buffer text from BEG to END through a Perl command."
1267 (let* ((exp (concat pre
"<<'SEPIA_END_REGION';\n"
1268 (buffer-substring-no-properties beg end
)
1269 (if (= (char-before end
) ?
\n) "" "\n")
1270 "SEPIA_END_REGION\n" post
))
1271 (new-str (car (sepia-eval-raw exp
))))
1273 (progn (delete-region beg end
)
1276 (if (> (sepia-string-count-matches "\n" new-str
) 2)
1277 (with-current-buffer (get-buffer-create "*sepia-filter*")
1278 (let ((inhibit-read-only t
))
1281 (goto-char (point-min))
1282 (pop-to-buffer (current-buffer))))
1283 (message "%s" new-str
)))))
1285 (defun sepia-eol-from (pt &optional n
)
1291 (defun sepia-bol-from (pt &optional n
)
1294 (beginning-of-line n
)
1297 (defun sepia-perl-pe-region (expr beg end
&optional replace-p
)
1298 "Do the equivalent of perl -pe on region
1300 \(i.e. evaluate an expression on each line of region). With
1301 prefix arg, replace the region with the result."
1302 (interactive "MExpression: \nr\nP")
1303 (sepia-perlize-region-internal
1304 "do { my $ret=''; local $_; local $/ = \"\\n\"; my $region = "
1305 (concat "; for (split /(?<=\\n)/, $region, -1) { " expr
1306 "} continue { $ret.=$_}; $ret}")
1307 (sepia-bol-from beg
) (sepia-eol-from end
) replace-p
))
1309 (defun sepia-perl-ne-region (expr beg end
&optional replace-p
)
1310 "Do the moral equivalent of perl -ne on region
1312 \(i.e. evaluate an expression on each line of region). With
1313 prefix arg, replace the region with the result."
1314 (interactive "MExpression: \nr\nP")
1315 (sepia-perlize-region-internal
1316 "do { my $ret='';my $region = "
1317 (concat "; for (split /(?<=\\n)/, $region, -1) { $ret .= do { " expr
1319 (sepia-bol-from beg
) (sepia-eol-from end
) replace-p
))
1321 (defun sepia-perlize-region (expr beg end
&optional replace-p
)
1322 "Evaluate a Perl expression on the region as a whole.
1324 With prefix arg, replace the region with the result."
1325 (interactive "MExpression: \nr\nP")
1326 (sepia-perlize-region-internal
1327 "do { local $_ = " (concat "; do { " expr
";}; $_ }") beg end replace-p
))
1329 (defun sepia-core-version (module &optional message
)
1330 "Report the first version of Perl shipping with MODULE."
1331 (interactive (list (sepia-interactive-arg 'module
) t
))
1334 (format "eval { Sepia::core_version('%s') }" module
)
1337 (format "%s was first released in %s." module version
)
1338 (format "%s is not in core." module
))))
1339 (when message
(message "%s" res
))
1342 (defun sepia-guess-package (sub &optional file
)
1343 "Guess which package SUB is defined in."
1344 (let ((defs (xref-location (xref-apropos sub
))))
1345 (or (and (= (length defs
) 1)
1346 (or (not file
) (equal (caar defs
) file
))
1347 (fourth (car defs
)))
1349 (fourth (find-if (lambda (x) (equal (car x
) file
)) defs
)))
1350 ;; (car (xref-file-modules file))
1351 (sepia-buffer-package))))
1354 (defun sepia-apropos-module (name)
1355 "List installed modules matching a regexp."
1356 (interactive "MList modules matching regexp: ")
1357 (let ((res (xref-apropos-module name
)))
1359 (with-output-to-temp-buffer "*Modules*"
1360 (display-completion-list res
))
1361 (message "No modules matching %s." name
))))
1364 (defun sepia-eval-defun ()
1365 "Re-evaluate the current function and rebuild its Xrefs."
1367 (let (pt end beg sub res
1373 end
(progn (end-of-defun) (point))
1374 beg
(progn (beginning-of-defun) (point)))
1376 (when (looking-at "^sub\\s +\\(.+\\_>\\)")
1377 (setq sub
(match-string 1))
1378 (let ((body (buffer-substring-no-properties beg end
)))
1380 (setq sepia-eval-package
(sepia-guess-package sub
(buffer-file-name))
1381 sepia-eval-file
(buffer-file-name)
1382 sepia-eval-line
(line-number-at-pos beg
)
1385 (if sepia-eval-defun-include-decls
1387 (apply #'concat
(xref-mod-decls sepia-eval-package
))
1392 (when (string-match " line \\([0-9]+\\), near \"\\([^\"]*\\)\""
1395 (beginning-of-line (string-to-number (match-string 1 (cdr res
))))
1396 (search-forward (match-string 2 (cdr res
))
1397 (sepia-eol-from (point)) t
))
1398 (message "Error: %s" (cdr res
)))
1399 (xref-redefined sub sepia-eval-package
)
1400 (message "Defined %s" sub
))))
1403 (defun sepia-eval-expression (expr &optional list-p message-p
)
1404 "Evaluate EXPR in scalar context."
1405 (interactive (list (read-string "Expression: ") current-prefix-arg t
))
1406 (let ((res (sepia-eval expr
(if list-p
'list-context
'scalar-context
))))
1407 (when message-p
(message "%s" res
))
1410 (defun sepia-extract-def (file line obj
)
1411 (with-current-buffer (find-file-noselect (expand-file-name file
))
1413 (funcall (sepia-refiner 'function
) line obj
)
1415 (when (looking-at (concat "^\\s *sub\\_>.*\\_<" obj
"\\_>"))
1416 (buffer-substring (point)
1417 (progn (end-of-defun) (point)))))))
1419 (defun sepia-eval-no-run (string)
1420 (let ((res (sepia-eval-raw
1421 (concat "eval q#{ BEGIN { use B; B::minus_c(); $^C=1; } do { "
1423 " };BEGIN { die \"ok\\n\" }#, $@"))))
1424 (if (string-match "^ok\n" (car res
))
1428 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1431 (defvar sepia-eval-file nil
1432 "File in which `sepia-eval' evaluates perl expressions.")
1433 (defvar sepia-eval-line nil
1434 "Line at which `sepia-eval' evaluates perl expressions.")
1436 (defun sepia-set-cwd (dir)
1437 "Set the inferior Perl process's working directory to DIR.
1439 When called interactively, the current buffer's
1440 `default-directory' is used."
1441 (interactive (list (expand-file-name default-directory
)))
1442 (sepia-call "Cwd::chdir" 'list-context dir
))
1444 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1447 (defvar sepia-doc-map
(make-hash-table :test
#'equal
))
1448 (defvar sepia-var-doc-map
(make-hash-table :test
#'equal
))
1449 (defvar sepia-module-doc-map
(make-hash-table :test
#'equal
))
1451 (defun sepia-doc-scan-buffer ()
1453 (goto-char (point-min))
1455 while
(re-search-forward
1456 "^=\\(item\\|head[2-9]\\)\\s +\\([%$&@A-Za-z_].*\\)" nil t
)
1459 (let ((short (match-string 2)) longdoc
)
1461 (let ((case-fold-search nil
))
1462 (replace-regexp-in-string
1464 (replace-regexp-in-string
1466 (replace-regexp-in-string
1467 "[A-DF-Z]<\\([^<>]+\\)>" "\\1" short
)))))
1468 (while (string-match "^\\s *[A-Z]<\\(.*\\)>\\s *$" short
)
1469 (setq short
(match-string 1 short
)))
1471 (let ((beg (progn (forward-line 2) (point)))
1472 (end (1- (re-search-forward "^=" nil t
))))
1475 (if (re-search-forward "^\\(.+\\)$" end t
)
1477 (substring-no-properties
1479 0 (position ?.
(match-string 1))))
1482 ;; e.g. "$x -- this is x"
1483 ((string-match "^[%$@]\\([A-Za-z0-9_:]+\\)\\s *--\\s *\\(.*\\)"
1485 (list 'variable
(match-string-no-properties 1 short
)
1486 (or (and (equal short
(match-string 1 short
)) longdoc
)
1488 ;; e.g. "C<foo(BLAH)>" or "$x = $y->foo()"
1489 ((string-match "\\([A-Za-z0-9_:]+\\)\\s *\\(\\$\\|(\\)" short
)
1490 (list 'function
(match-string-no-properties 1 short
)
1491 (or (and (equal short
(match-string 1 short
)) longdoc
)
1493 ;; e.g. "C<$result = foo $args...>"
1494 ((string-match "=\\s *\\([A-Za-z0-9_:]+\\)" short
)
1495 (list 'function
(match-string-no-properties 1 short
)
1496 (or (and (equal short
(match-string 1 short
)) longdoc
)
1498 ;; e.g. "$x this is x" (note: this has to come last)
1499 ((string-match "^[%$@]\\([^( ]+\\)" short
)
1500 (list 'variable
(match-string-no-properties 1 short
) longdoc
)))))
1503 (defun sepia-buffer-package ()
1505 (or (and (re-search-backward "^\\s *package\\s +\\([^ ;]+\\)\\s *;" nil t
)
1506 (match-string-no-properties 1))
1509 (defun sepia-doc-update ()
1510 "Update documentation for a file.
1512 This documentation, taken from \"=item\" entries in the POD, is
1513 used for eldoc feedback."
1515 (let ((pack (ifa (sepia-buffer-package) (concat it
"::") "")))
1516 (dolist (x (sepia-doc-scan-buffer))
1517 (let ((map (ecase (car x
)
1518 (function sepia-doc-map
)
1519 (variable sepia-var-doc-map
))))
1520 (puthash (second x
) (third x
) map
)
1521 (puthash (concat pack
(second x
)) (third x
) map
)))))
1523 (defun sepia-looks-like-module (obj)
1524 (let (case-fold-search)
1526 (eval-when-compile (regexp-opt '("strict" "vars" "warnings" "lib")))
1529 (string-match "^\\([A-Z][A-Za-z0-9]*::\\)*[A-Z]+[A-Za-z0-9]+\\sw*$" obj
)
1530 (xref-apropos-module obj
)))))
1532 (defun sepia-describe-object (thing)
1533 "Display documentation for `thing', like ``describe-function'' for elisp."
1535 (let ((id (sepia-ident-at-point)))
1536 (when (string= (cadr id
) "")
1537 (setq id
(sepia-ident-before-point)))
1543 (setq thing
(format "%c%s" (car thing
) (cadr thing
)))
1544 (with-current-buffer (get-buffer-create "*sepia-help*")
1545 (let ((inhibit-read-only t
))
1547 (shell-command (concat "perldoc -v " (shell-quote-argument thing
))
1550 (goto-char (point-min)))
1551 (unless (looking-at "No documentation for")
1552 (pop-to-buffer "*sepia-help*" t
))))
1553 ((gethash thing sepia-perl-builtins
)
1554 (with-current-buffer (get-buffer-create "*sepia-help*")
1555 (let ((inhibit-read-only t
))
1557 (shell-command (concat "perldoc -f " thing
) (current-buffer))
1559 (goto-char (point-min))))
1560 (pop-to-buffer "*sepia-help*" t
))))
1562 (defun sepia-symbol-info (&optional obj type
)
1563 "Eldoc function for `sepia-mode'.
1565 Looks in `sepia-doc-map' and `sepia-var-doc-map', then tries
1566 calling `cperl-describe-perl-symbol'."
1568 (multiple-value-bind (ty ob
) (sepia-ident-at-point)
1569 (setq obj
(if (consp ob
) (car ob
) ob
)
1572 (or (gethash obj
(ecase (or type ?
&)
1574 ((?$ ?
@ ?%
) sepia-var-doc-map
)
1575 (nil sepia-module-doc-map
)
1576 (?
* sepia-module-doc-map
)
1577 (t (error "sepia-symbol-info: %s" type
))))
1578 ;; Loathe cperl a bit.
1579 (flet ((message (&rest blah
) (apply #'format blah
)))
1580 (let* (case-fold-search
1581 (cperl-message-on-help-error nil
)
1582 (hlp (car (save-excursion (cperl-describe-perl-symbol obj
)))))
1585 ;; cperl's docstrings are too long.
1586 (setq hlp
(replace-regexp-in-string "\\s \\{2,\\}" " " hlp
))
1587 (if (> (length hlp
) 75)
1588 (concat (substring hlp
0 72) "...")
1590 ;; Try to see if it's a module
1592 (let ((bol (save-excursion (beginning-of-line)
1594 (looking-back " *\\(?:use\\|require\\|package\\|no\\)\\s +[^ ]*" bol
))
1595 (sepia-looks-like-module obj
))
1596 (sepia-core-version obj
)
1600 (defun sepia-install-eldoc ()
1601 "Install Sepia hooks for eldoc support."
1604 (set-variable 'eldoc-documentation-function
'sepia-symbol-info t
)
1605 (if cperl-lazy-installed
(cperl-lazy-unstall))
1607 (set-variable 'eldoc-idle-delay
1.0 t
))
1609 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1612 (defun sepia-extract-next-warning (pos &optional end
)
1614 (while (re-search-forward "^\\(.+\\) at \\(.+?\\) line \\([0-9]+\\)"
1616 (unless (string= "(eval " (substring (match-string 2) 0 6))
1617 (throw 'foo
(list (match-string 2)
1618 (string-to-number (match-string 3))
1619 (match-string 1)))))))
1621 (defun sepia-goto-error-at (pos)
1622 "Visit the source of the error on line at point."
1624 (ifa (sepia-extract-next-warning (sepia-bol-from pos
) (sepia-eol-from pos
))
1625 (destructuring-bind (file line msg
) it
1629 (error "No error to find.")))
1631 (defun sepia-display-errors (beg end
)
1632 "Display source causing errors in current buffer from BEG to END."
1636 (loop for w
= (sepia-extract-next-warning (sepia-bol-from (point)) end
)
1638 do
(destructuring-bind (file line msg
) w
1639 (push (format "%s:%d:%s\n" (abbreviate-file-name file
) line msg
)
1642 (goto-char (point-min))
1643 (mapcar #'insert
(nreverse msgs
))
1644 (goto-char (point-min))
1647 (defun sepia-lisp-to-perl (thing)
1648 "Convert elisp data structure to Perl."
1650 ((null thing
) "undef")
1652 (let ((pname (substitute ?_ ?-
(symbol-name thing
)))
1653 (type (string-to-char (symbol-name thing
))))
1654 (if (member type
'(?% ?$ ?
@ ?
*))
1656 (concat "\\*" pname
))))
1657 ((stringp thing
) (format "%S" (substring-no-properties thing
0)))
1658 ((integerp thing
) (format "%d" thing
))
1659 ((numberp thing
) (format "%g" thing
))
1661 ((and (consp thing
) (eq (car thing
) 'expr
))
1662 (cdr thing
)) ; XXX -- need quoting??
1663 ((and (consp thing
) (not (consp (cdr thing
))))
1664 (concat (sepia-lisp-to-perl (car thing
)) " => "
1665 (sepia-lisp-to-perl (cdr thing
))))
1667 ((or (not (consp (car thing
)))
1668 (listp (cdar thing
)))
1669 (concat "[" (mapconcat #'sepia-lisp-to-perl thing
", ") "]"))
1672 (concat "{" (mapconcat #'sepia-lisp-to-perl thing
", ") "}"))))
1674 (defun sepia-init-perl-builtins ()
1675 (setq sepia-perl-builtins
(make-hash-table :test
#'equal
))
1877 (puthash s t sepia-perl-builtins
)))
1880 ;;; sepia.el ends here