Klimi's new dotfiles with stow.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

872 строки
39 KiB

4 лет назад
  1. (require 'slime)
  2. (require 'bridge)
  3. (require 'cl-lib)
  4. (eval-when-compile
  5. (require 'cl))
  6. (define-slime-contrib slime-presentations
  7. "Imitate LispM presentations."
  8. (:authors "Alan Ruttenberg <alanr-l@mumble.net>"
  9. "Matthias Koeppe <mkoeppe@mail.math.uni-magdeburg.de>")
  10. (:license "GPL")
  11. (:slime-dependencies slime-repl)
  12. (:swank-dependencies swank-presentations)
  13. (:on-load
  14. (add-hook 'slime-repl-mode-hook
  15. (lambda ()
  16. ;; Respect the syntax text properties of presentation.
  17. (set (make-local-variable 'parse-sexp-lookup-properties) t)
  18. (add-hook 'after-change-functions
  19. 'slime-after-change-function 'append t)))
  20. (add-hook 'slime-event-hooks 'slime-dispatch-presentation-event)
  21. (setq slime-write-string-function 'slime-presentation-write)
  22. (add-hook 'slime-connected-hook 'slime-presentations-on-connected)
  23. (add-hook 'slime-repl-return-hooks 'slime-presentation-on-return-pressed)
  24. (add-hook 'slime-repl-current-input-hooks 'slime-presentation-current-input)
  25. (add-hook 'slime-open-stream-hooks 'slime-presentation-on-stream-open)
  26. (add-hook 'slime-repl-clear-buffer-hook 'slime-clear-presentations)
  27. (add-hook 'slime-edit-definition-hooks 'slime-edit-presentation)
  28. (setq sldb-insert-frame-variable-value-function
  29. 'slime-presentation-sldb-insert-frame-variable-value)
  30. (slime-presentation-init-keymaps)
  31. (slime-presentation-add-easy-menu)))
  32. ;; To get presentations in the inspector as well, add this to your
  33. ;; init file.
  34. ;;
  35. ;; (eval-after-load 'slime-presentations
  36. ;; '(setq slime-inspector-insert-ispec-function
  37. ;; 'slime-presentation-inspector-insert-ispec))
  38. ;;
  39. (defface slime-repl-output-mouseover-face
  40. '((t (:box (:line-width 1 :color "black" :style released-button)
  41. :inherit slime-repl-inputed-output-face)))
  42. "Face for Lisp output in the SLIME REPL, when the mouse hovers over it"
  43. :group 'slime-repl)
  44. (defface slime-repl-inputed-output-face
  45. '((((class color) (background light)) (:foreground "Red"))
  46. (((class color) (background dark)) (:foreground "light salmon"))
  47. (t (:slant italic)))
  48. "Face for the result of an evaluation in the SLIME REPL."
  49. :group 'slime-repl)
  50. ;; FIXME: This conditional is not right - just used because the code
  51. ;; here does not work in XEmacs.
  52. (when (boundp 'text-property-default-nonsticky)
  53. (pushnew '(slime-repl-presentation . t) text-property-default-nonsticky
  54. :test 'equal)
  55. (pushnew '(slime-repl-result-face . t) text-property-default-nonsticky
  56. :test 'equal))
  57. (make-variable-buffer-local
  58. (defvar slime-presentation-start-to-point (make-hash-table)))
  59. (defun slime-mark-presentation-start (id &optional target)
  60. "Mark the beginning of a presentation with the given ID.
  61. TARGET can be nil (regular process output) or :repl-result."
  62. (setf (gethash id slime-presentation-start-to-point)
  63. ;; We use markers because text can also be inserted before this presentation.
  64. ;; (Output arrives while we are writing presentations within REPL results.)
  65. (copy-marker (slime-repl-output-target-marker target) nil)))
  66. (defun slime-mark-presentation-start-handler (process string)
  67. (if (and string (string-match "<\\([-0-9]+\\)" string))
  68. (let* ((match (substring string (match-beginning 1) (match-end 1)))
  69. (id (car (read-from-string match))))
  70. (slime-mark-presentation-start id))))
  71. (defun slime-mark-presentation-end (id &optional target)
  72. "Mark the end of a presentation with the given ID.
  73. TARGET can be nil (regular process output) or :repl-result."
  74. (let ((start (gethash id slime-presentation-start-to-point)))
  75. (remhash id slime-presentation-start-to-point)
  76. (when start
  77. (let* ((marker (slime-repl-output-target-marker target))
  78. (buffer (and marker (marker-buffer marker))))
  79. (with-current-buffer buffer
  80. (let ((end (marker-position marker)))
  81. (slime-add-presentation-properties start end
  82. id nil)))))))
  83. (defun slime-mark-presentation-end-handler (process string)
  84. (if (and string (string-match ">\\([-0-9]+\\)" string))
  85. (let* ((match (substring string (match-beginning 1) (match-end 1)))
  86. (id (car (read-from-string match))))
  87. (slime-mark-presentation-end id))))
  88. (cl-defstruct slime-presentation text id)
  89. (defvar slime-presentation-syntax-table
  90. (let ((table (copy-syntax-table lisp-mode-syntax-table)))
  91. ;; We give < and > parenthesis syntax, so that #< ... > is treated
  92. ;; as a balanced expression. This allows to use C-M-k, C-M-SPC,
  93. ;; etc. to deal with a whole presentation. (For Lisp mode, this
  94. ;; is not desirable, since we do not wish to get a mismatched
  95. ;; paren highlighted everytime we type < or >.)
  96. (modify-syntax-entry ?< "(>" table)
  97. (modify-syntax-entry ?> ")<" table)
  98. table)
  99. "Syntax table for presentations.")
  100. (defun slime-add-presentation-properties (start end id result-p)
  101. "Make the text between START and END a presentation with ID.
  102. RESULT-P decides whether a face for a return value or output text is used."
  103. (let* ((text (buffer-substring-no-properties start end))
  104. (presentation (make-slime-presentation :text text :id id)))
  105. (let ((inhibit-modification-hooks t))
  106. (add-text-properties start end
  107. `(modification-hooks (slime-after-change-function)
  108. insert-in-front-hooks (slime-after-change-function)
  109. insert-behind-hooks (slime-after-change-function)
  110. syntax-table ,slime-presentation-syntax-table
  111. rear-nonsticky t))
  112. ;; Use the presentation as the key of a text property
  113. (case (- end start)
  114. (0)
  115. (1
  116. (add-text-properties start end
  117. `(slime-repl-presentation ,presentation
  118. ,presentation :start-and-end)))
  119. (t
  120. (add-text-properties start (1+ start)
  121. `(slime-repl-presentation ,presentation
  122. ,presentation :start))
  123. (when (> (- end start) 2)
  124. (add-text-properties (1+ start) (1- end)
  125. `(,presentation :interior)))
  126. (add-text-properties (1- end) end
  127. `(slime-repl-presentation ,presentation
  128. ,presentation :end))))
  129. ;; Also put an overlay for the face and the mouse-face. This enables
  130. ;; highlighting of nested presentations. However, overlays get lost
  131. ;; when we copy a presentation; their removal is also not undoable.
  132. ;; In these cases the mouse-face text properties need to take over ---
  133. ;; but they do not give nested highlighting.
  134. (slime-ensure-presentation-overlay start end presentation))))
  135. (defvar slime-presentation-map (make-sparse-keymap))
  136. (defun slime-ensure-presentation-overlay (start end presentation)
  137. (unless (cl-find presentation (overlays-at start)
  138. :key (lambda (overlay)
  139. (overlay-get overlay 'slime-repl-presentation)))
  140. (let ((overlay (make-overlay start end (current-buffer) t nil)))
  141. (overlay-put overlay 'slime-repl-presentation presentation)
  142. (overlay-put overlay 'mouse-face 'slime-repl-output-mouseover-face)
  143. (overlay-put overlay 'help-echo
  144. (if (eq major-mode 'slime-repl-mode)
  145. "mouse-2: copy to input; mouse-3: menu"
  146. "mouse-2: inspect; mouse-3: menu"))
  147. (overlay-put overlay 'face 'slime-repl-inputed-output-face)
  148. (overlay-put overlay 'keymap slime-presentation-map))))
  149. (defun slime-remove-presentation-properties (from to presentation)
  150. (let ((inhibit-read-only t))
  151. (remove-text-properties from to
  152. `(,presentation t syntax-table t rear-nonsticky t))
  153. (when (eq (get-text-property from 'slime-repl-presentation) presentation)
  154. (remove-text-properties from (1+ from) `(slime-repl-presentation t)))
  155. (when (eq (get-text-property (1- to) 'slime-repl-presentation) presentation)
  156. (remove-text-properties (1- to) to `(slime-repl-presentation t)))
  157. (dolist (overlay (overlays-at from))
  158. (when (eq (overlay-get overlay 'slime-repl-presentation) presentation)
  159. (delete-overlay overlay)))))
  160. (defun slime-insert-presentation (string output-id &optional rectangle)
  161. "Insert STRING in current buffer and mark it as a presentation
  162. corresponding to OUTPUT-ID. If RECTANGLE is true, indent multi-line
  163. strings to line up below the current point."
  164. (cl-labels ((insert-it ()
  165. (if rectangle
  166. (slime-insert-indented string)
  167. (insert string))))
  168. (let ((start (point)))
  169. (insert-it)
  170. (slime-add-presentation-properties start (point) output-id t))))
  171. (defun slime-presentation-whole-p (presentation start end &optional object)
  172. (let ((object (or object (current-buffer))))
  173. (string= (etypecase object
  174. (buffer (with-current-buffer object
  175. (buffer-substring-no-properties start end)))
  176. (string (substring-no-properties object start end)))
  177. (slime-presentation-text presentation))))
  178. (defun slime-presentations-around-point (point &optional object)
  179. (let ((object (or object (current-buffer))))
  180. (loop for (key value . rest) on (text-properties-at point object) by 'cddr
  181. when (slime-presentation-p key)
  182. collect key)))
  183. (defun slime-presentation-start-p (tag)
  184. (memq tag '(:start :start-and-end)))
  185. (defun slime-presentation-stop-p (tag)
  186. (memq tag '(:end :start-and-end)))
  187. (cl-defun slime-presentation-start (point presentation
  188. &optional (object (current-buffer)))
  189. "Find start of `presentation' at `point' in `object'.
  190. Return buffer index and whether a start-tag was found."
  191. (let* ((this-presentation (get-text-property point presentation object)))
  192. (while (not (slime-presentation-start-p this-presentation))
  193. (let ((change-point (previous-single-property-change
  194. point presentation object (point-min))))
  195. (unless change-point
  196. (return-from slime-presentation-start
  197. (values (etypecase object
  198. (buffer (with-current-buffer object 1))
  199. (string 0))
  200. nil)))
  201. (setq this-presentation (get-text-property change-point
  202. presentation object))
  203. (unless this-presentation
  204. (return-from slime-presentation-start
  205. (values point nil)))
  206. (setq point change-point)))
  207. (values point t)))
  208. (cl-defun slime-presentation-end (point presentation
  209. &optional (object (current-buffer)))
  210. "Find end of presentation at `point' in `object'. Return buffer
  211. index (after last character of the presentation) and whether an
  212. end-tag was found."
  213. (let* ((this-presentation (get-text-property point presentation object)))
  214. (while (not (slime-presentation-stop-p this-presentation))
  215. (let ((change-point (next-single-property-change
  216. point presentation object)))
  217. (unless change-point
  218. (return-from slime-presentation-end
  219. (values (etypecase object
  220. (buffer (with-current-buffer object (point-max)))
  221. (string (length object)))
  222. nil)))
  223. (setq point change-point)
  224. (setq this-presentation (get-text-property point
  225. presentation object))))
  226. (if this-presentation
  227. (let ((after-end (next-single-property-change point
  228. presentation object)))
  229. (if (not after-end)
  230. (values (etypecase object
  231. (buffer (with-current-buffer object (point-max)))
  232. (string (length object)))
  233. t)
  234. (values after-end t)))
  235. (values point nil))))
  236. (cl-defun slime-presentation-bounds (point presentation
  237. &optional (object (current-buffer)))
  238. "Return start index and end index of `presentation' around `point'
  239. in `object', and whether the presentation is complete."
  240. (multiple-value-bind (start good-start)
  241. (slime-presentation-start point presentation object)
  242. (multiple-value-bind (end good-end)
  243. (slime-presentation-end point presentation object)
  244. (values start end
  245. (and good-start good-end
  246. (slime-presentation-whole-p presentation
  247. start end object))))))
  248. (defun slime-presentation-around-point (point &optional object)
  249. "Return presentation, start index, end index, and whether the
  250. presentation is complete."
  251. (let ((object (or object (current-buffer)))
  252. (innermost-presentation nil)
  253. (innermost-start 0)
  254. (innermost-end most-positive-fixnum))
  255. (dolist (presentation (slime-presentations-around-point point object))
  256. (multiple-value-bind (start end whole-p)
  257. (slime-presentation-bounds point presentation object)
  258. (when whole-p
  259. (when (< (- end start) (- innermost-end innermost-start))
  260. (setq innermost-start start
  261. innermost-end end
  262. innermost-presentation presentation)))))
  263. (values innermost-presentation
  264. innermost-start innermost-end)))
  265. (defun slime-presentation-around-or-before-point (point &optional object)
  266. (let ((object (or object (current-buffer))))
  267. (multiple-value-bind (presentation start end whole-p)
  268. (slime-presentation-around-point point object)
  269. (if (or presentation (= point (point-min)))
  270. (values presentation start end whole-p)
  271. (slime-presentation-around-point (1- point) object)))))
  272. (defun slime-presentation-around-or-before-point-or-error (point)
  273. (multiple-value-bind (presentation start end whole-p)
  274. (slime-presentation-around-or-before-point point)
  275. (unless presentation
  276. (error "No presentation at point"))
  277. (values presentation start end whole-p)))
  278. (cl-defun slime-for-each-presentation-in-region (from to function
  279. &optional (object (current-buffer)))
  280. "Call `function' with arguments `presentation', `start', `end',
  281. `whole-p' for every presentation in the region `from'--`to' in the
  282. string or buffer `object'."
  283. (cl-labels ((handle-presentation (presentation point)
  284. (multiple-value-bind (start end whole-p)
  285. (slime-presentation-bounds point presentation object)
  286. (funcall function presentation start end whole-p))))
  287. ;; Handle presentations active at `from'.
  288. (dolist (presentation (slime-presentations-around-point from object))
  289. (handle-presentation presentation from))
  290. ;; Use the `slime-repl-presentation' property to search for new presentations.
  291. (let ((point from))
  292. (while (< point to)
  293. (setq point (next-single-property-change point 'slime-repl-presentation
  294. object to))
  295. (let* ((presentation (get-text-property point 'slime-repl-presentation object))
  296. (status (get-text-property point presentation object)))
  297. (when (slime-presentation-start-p status)
  298. (handle-presentation presentation point)))))))
  299. ;; XEmacs compatibility hack, from message by Stephen J. Turnbull on
  300. ;; xemacs-beta@xemacs.org of 18 Mar 2002
  301. (unless (boundp 'undo-in-progress)
  302. (defvar undo-in-progress nil
  303. "Placeholder defvar for XEmacs compatibility from SLIME.")
  304. (defadvice undo-more (around slime activate)
  305. (let ((undo-in-progress t)) ad-do-it)))
  306. (defun slime-after-change-function (start end &rest ignore)
  307. "Check all presentations within and adjacent to the change.
  308. When a presentation has been altered, change it to plain text."
  309. (let ((inhibit-modification-hooks t))
  310. (let ((real-start (max 1 (1- start)))
  311. (real-end (min (1+ (buffer-size)) (1+ end)))
  312. (any-change nil))
  313. ;; positions around the change
  314. (slime-for-each-presentation-in-region
  315. real-start real-end
  316. (lambda (presentation from to whole-p)
  317. (cond
  318. (whole-p
  319. (slime-ensure-presentation-overlay from to presentation))
  320. ((not undo-in-progress)
  321. (slime-remove-presentation-properties from to
  322. presentation)
  323. (setq any-change t)))))
  324. (when any-change
  325. (undo-boundary)))))
  326. (defun slime-presentation-around-click (event)
  327. "Return the presentation around the position of the mouse-click EVENT.
  328. If there is no presentation, signal an error.
  329. Also return the start position, end position, and buffer of the presentation."
  330. (when (and (featurep 'xemacs) (not (button-press-event-p event)))
  331. (error "Command must be bound to a button-press-event"))
  332. (let ((point (if (featurep 'xemacs) (event-point event) (posn-point (event-end event))))
  333. (window (if (featurep 'xemacs) (event-window event) (caadr event))))
  334. (with-current-buffer (window-buffer window)
  335. (multiple-value-bind (presentation start end)
  336. (slime-presentation-around-point point)
  337. (unless presentation
  338. (error "No presentation at click"))
  339. (values presentation start end (current-buffer))))))
  340. (defun slime-check-presentation (from to buffer presentation)
  341. (unless (slime-eval `(cl:nth-value 1 (swank:lookup-presented-object
  342. ',(slime-presentation-id presentation))))
  343. (with-current-buffer buffer
  344. (slime-remove-presentation-properties from to presentation))))
  345. (defun slime-copy-or-inspect-presentation-at-mouse (event)
  346. (interactive "e") ; no "@" -- we don't want to select the clicked-at window
  347. (multiple-value-bind (presentation start end buffer)
  348. (slime-presentation-around-click event)
  349. (slime-check-presentation start end buffer presentation)
  350. (if (with-current-buffer buffer
  351. (eq major-mode 'slime-repl-mode))
  352. (slime-copy-presentation-at-mouse-to-repl event)
  353. (slime-inspect-presentation-at-mouse event))))
  354. (defun slime-inspect-presentation (presentation start end buffer)
  355. (let ((reset-p
  356. (with-current-buffer buffer
  357. (not (eq major-mode 'slime-inspector-mode)))))
  358. (slime-eval-async `(swank:inspect-presentation ',(slime-presentation-id presentation) ,reset-p)
  359. 'slime-open-inspector)))
  360. (defun slime-inspect-presentation-at-mouse (event)
  361. (interactive "e")
  362. (multiple-value-bind (presentation start end buffer)
  363. (slime-presentation-around-click event)
  364. (slime-inspect-presentation presentation start end buffer)))
  365. (defun slime-inspect-presentation-at-point (point)
  366. (interactive "d")
  367. (multiple-value-bind (presentation start end)
  368. (slime-presentation-around-or-before-point-or-error point)
  369. (slime-inspect-presentation presentation start end (current-buffer))))
  370. (defun slime-M-.-presentation (presentation start end buffer &optional where)
  371. (let* ((id (slime-presentation-id presentation))
  372. (presentation-string (format "Presentation %s" id))
  373. (location (slime-eval `(swank:find-definition-for-thing
  374. (swank:lookup-presented-object
  375. ',(slime-presentation-id presentation))))))
  376. (unless (eq (car location) :error)
  377. (slime-edit-definition-cont
  378. (and location (list (make-slime-xref :dspec `(,presentation-string)
  379. :location location)))
  380. presentation-string
  381. where))))
  382. (defun slime-M-.-presentation-at-mouse (event)
  383. (interactive "e")
  384. (multiple-value-bind (presentation start end buffer)
  385. (slime-presentation-around-click event)
  386. (slime-M-.-presentation presentation start end buffer)))
  387. (defun slime-M-.-presentation-at-point (point)
  388. (interactive "d")
  389. (multiple-value-bind (presentation start end)
  390. (slime-presentation-around-or-before-point-or-error point)
  391. (slime-M-.-presentation presentation start end (current-buffer))))
  392. (defun slime-edit-presentation (name &optional where)
  393. (if (or current-prefix-arg (not (equal (slime-symbol-at-point) name)))
  394. nil ; NAME came from user explicitly, so decline.
  395. (multiple-value-bind (presentation start end whole-p)
  396. (slime-presentation-around-or-before-point (point))
  397. (when presentation
  398. (slime-M-.-presentation presentation start end (current-buffer) where)))))
  399. (defun slime-copy-presentation-to-repl (presentation start end buffer)
  400. (let ((text (with-current-buffer buffer
  401. ;; we use the buffer-substring rather than the
  402. ;; presentation text to capture any overlays
  403. (buffer-substring start end)))
  404. (id (slime-presentation-id presentation)))
  405. (unless (integerp id)
  406. (setq id (slime-eval `(swank:lookup-and-save-presented-object-or-lose ',id))))
  407. (unless (eql major-mode 'slime-repl-mode)
  408. (slime-switch-to-output-buffer))
  409. (cl-flet ((do-insertion ()
  410. (unless (looking-back "\\s-" (- (point) 1))
  411. (insert " "))
  412. (slime-insert-presentation text id)
  413. (unless (or (eolp) (looking-at "\\s-"))
  414. (insert " "))))
  415. (if (>= (point) slime-repl-prompt-start-mark)
  416. (do-insertion)
  417. (save-excursion
  418. (goto-char (point-max))
  419. (do-insertion))))))
  420. (defun slime-copy-presentation-at-mouse-to-repl (event)
  421. (interactive "e")
  422. (multiple-value-bind (presentation start end buffer)
  423. (slime-presentation-around-click event)
  424. (slime-copy-presentation-to-repl presentation start end buffer)))
  425. (defun slime-copy-presentation-at-point-to-repl (point)
  426. (interactive "d")
  427. (multiple-value-bind (presentation start end)
  428. (slime-presentation-around-or-before-point-or-error point)
  429. (slime-copy-presentation-to-repl presentation start end (current-buffer))))
  430. (defun slime-copy-presentation-at-mouse-to-point (event)
  431. (interactive "e")
  432. (multiple-value-bind (presentation start end buffer)
  433. (slime-presentation-around-click event)
  434. (let ((presentation-text
  435. (with-current-buffer buffer
  436. (buffer-substring start end))))
  437. (when (not (string-match "\\s-"
  438. (buffer-substring (1- (point)) (point))))
  439. (insert " "))
  440. (insert presentation-text)
  441. (slime-after-change-function (point) (point))
  442. (when (and (not (eolp)) (not (looking-at "\\s-")))
  443. (insert " ")))))
  444. (defun slime-copy-presentation-to-kill-ring (presentation start end buffer)
  445. (let ((presentation-text
  446. (with-current-buffer buffer
  447. (buffer-substring start end))))
  448. (kill-new presentation-text)
  449. (message "Saved presentation \"%s\" to kill ring" presentation-text)))
  450. (defun slime-copy-presentation-at-mouse-to-kill-ring (event)
  451. (interactive "e")
  452. (multiple-value-bind (presentation start end buffer)
  453. (slime-presentation-around-click event)
  454. (slime-copy-presentation-to-kill-ring presentation start end buffer)))
  455. (defun slime-copy-presentation-at-point-to-kill-ring (point)
  456. (interactive "d")
  457. (multiple-value-bind (presentation start end)
  458. (slime-presentation-around-or-before-point-or-error point)
  459. (slime-copy-presentation-to-kill-ring presentation start end (current-buffer))))
  460. (defun slime-describe-presentation (presentation)
  461. (slime-eval-describe
  462. `(swank::describe-to-string
  463. (swank:lookup-presented-object ',(slime-presentation-id presentation)))))
  464. (defun slime-describe-presentation-at-mouse (event)
  465. (interactive "@e")
  466. (multiple-value-bind (presentation) (slime-presentation-around-click event)
  467. (slime-describe-presentation presentation)))
  468. (defun slime-describe-presentation-at-point (point)
  469. (interactive "d")
  470. (multiple-value-bind (presentation)
  471. (slime-presentation-around-or-before-point-or-error point)
  472. (slime-describe-presentation presentation)))
  473. (defun slime-pretty-print-presentation (presentation)
  474. (slime-eval-describe
  475. `(swank::swank-pprint
  476. (cl:list
  477. (swank:lookup-presented-object ',(slime-presentation-id presentation))))))
  478. (defun slime-pretty-print-presentation-at-mouse (event)
  479. (interactive "@e")
  480. (multiple-value-bind (presentation) (slime-presentation-around-click event)
  481. (slime-pretty-print-presentation presentation)))
  482. (defun slime-pretty-print-presentation-at-point (point)
  483. (interactive "d")
  484. (multiple-value-bind (presentation)
  485. (slime-presentation-around-or-before-point-or-error point)
  486. (slime-pretty-print-presentation presentation)))
  487. (defun slime-mark-presentation (point)
  488. (interactive "d")
  489. (multiple-value-bind (presentation start end)
  490. (slime-presentation-around-or-before-point-or-error point)
  491. (goto-char start)
  492. (push-mark end nil t)))
  493. (defun slime-previous-presentation (&optional arg)
  494. "Move point to the beginning of the first presentation before point.
  495. With ARG, do this that many times.
  496. A negative argument means move forward instead."
  497. (interactive "p")
  498. (unless arg (setq arg 1))
  499. (slime-next-presentation (- arg)))
  500. (defun slime-next-presentation (&optional arg)
  501. "Move point to the beginning of the next presentation after point.
  502. With ARG, do this that many times.
  503. A negative argument means move backward instead."
  504. (interactive "p")
  505. (unless arg (setq arg 1))
  506. (cond
  507. ((plusp arg)
  508. (dotimes (i arg)
  509. ;; First skip outside the current surrounding presentation (if any)
  510. (multiple-value-bind (presentation start end)
  511. (slime-presentation-around-point (point))
  512. (when presentation
  513. (goto-char end)))
  514. (let ((p (next-single-property-change (point) 'slime-repl-presentation)))
  515. (unless p
  516. (error "No next presentation"))
  517. (multiple-value-bind (presentation start end)
  518. (slime-presentation-around-or-before-point-or-error p)
  519. (goto-char start)))))
  520. ((minusp arg)
  521. (dotimes (i (- arg))
  522. ;; First skip outside the current surrounding presentation (if any)
  523. (multiple-value-bind (presentation start end)
  524. (slime-presentation-around-point (point))
  525. (when presentation
  526. (goto-char start)))
  527. (let ((p (previous-single-property-change (point) 'slime-repl-presentation)))
  528. (unless p
  529. (error "No previous presentation"))
  530. (multiple-value-bind (presentation start end)
  531. (slime-presentation-around-or-before-point-or-error p)
  532. (goto-char start)))))))
  533. (define-key slime-presentation-map [mouse-2] 'slime-copy-or-inspect-presentation-at-mouse)
  534. (define-key slime-presentation-map [mouse-3] 'slime-presentation-menu)
  535. (when (featurep 'xemacs)
  536. (define-key slime-presentation-map [button2] 'slime-copy-or-inspect-presentation-at-mouse)
  537. (define-key slime-presentation-map [button3] 'slime-presentation-menu))
  538. ;; protocol for handling up a menu.
  539. ;; 1. Send lisp message asking for menu choices for this object.
  540. ;; Get back list of strings.
  541. ;; 2. Let used choose
  542. ;; 3. Call back to execute menu choice, passing nth and string of choice
  543. (defun slime-menu-choices-for-presentation (presentation buffer from to choice-to-lambda)
  544. "Return a menu for `presentation' at `from'--`to' in `buffer', suitable for `x-popup-menu'."
  545. (let* ((what (slime-presentation-id presentation))
  546. (choices (with-current-buffer buffer
  547. (slime-eval
  548. `(swank::menu-choices-for-presentation-id ',what)))))
  549. (cl-labels ((savel (f) ;; IMPORTANT - xemacs can't handle lambdas in x-popup-menu. So give them a name
  550. (let ((sym (cl-gensym)))
  551. (setf (gethash sym choice-to-lambda) f)
  552. sym)))
  553. (etypecase choices
  554. (list
  555. `(,(format "Presentation %s" (truncate-string-to-width
  556. (slime-presentation-text presentation)
  557. 30 nil nil t))
  558. (""
  559. ("Find Definition" . ,(savel 'slime-M-.-presentation-at-mouse))
  560. ("Inspect" . ,(savel 'slime-inspect-presentation-at-mouse))
  561. ("Describe" . ,(savel 'slime-describe-presentation-at-mouse))
  562. ("Pretty-print" . ,(savel 'slime-pretty-print-presentation-at-mouse))
  563. ("Copy to REPL" . ,(savel 'slime-copy-presentation-at-mouse-to-repl))
  564. ("Copy to kill ring" . ,(savel 'slime-copy-presentation-at-mouse-to-kill-ring))
  565. ,@(unless buffer-read-only
  566. `(("Copy to point" . ,(savel 'slime-copy-presentation-at-mouse-to-point))))
  567. ,@(let ((nchoice 0))
  568. (mapcar
  569. (lambda (choice)
  570. (incf nchoice)
  571. (cons choice
  572. (savel `(lambda ()
  573. (interactive)
  574. (slime-eval
  575. '(swank::execute-menu-choice-for-presentation-id
  576. ',what ,nchoice ,(nth (1- nchoice) choices)))))))
  577. choices)))))
  578. (symbol ; not-present
  579. (with-current-buffer buffer
  580. (slime-remove-presentation-properties from to presentation))
  581. (sit-for 0) ; allow redisplay
  582. `("Object no longer recorded"
  583. ("sorry" . ,(if (featurep 'xemacs) nil '(nil)))))))))
  584. (defun slime-presentation-menu (event)
  585. (interactive "e")
  586. (let* ((point (if (featurep 'xemacs) (event-point event)
  587. (posn-point (event-end event))))
  588. (window (if (featurep 'xemacs) (event-window event) (caadr event)))
  589. (buffer (window-buffer window))
  590. (choice-to-lambda (make-hash-table)))
  591. (multiple-value-bind (presentation from to)
  592. (with-current-buffer buffer
  593. (slime-presentation-around-point point))
  594. (unless presentation
  595. (error "No presentation at event position"))
  596. (let ((menu (slime-menu-choices-for-presentation
  597. presentation buffer from to choice-to-lambda)))
  598. (let ((choice (x-popup-menu event menu)))
  599. (when choice
  600. (call-interactively (gethash choice choice-to-lambda))))))))
  601. (defun slime-presentation-expression (presentation)
  602. "Return a string that contains a CL s-expression accessing
  603. the presented object."
  604. (let ((id (slime-presentation-id presentation)))
  605. (etypecase id
  606. (number
  607. ;; Make sure it works even if *read-base* is not 10.
  608. (format "(swank:lookup-presented-object-or-lose %d.)" id))
  609. (list
  610. ;; for frame variables and inspector parts
  611. (format "(swank:lookup-presented-object-or-lose '%s)" id)))))
  612. (defun slime-buffer-substring-with-reified-output (start end)
  613. (let ((str-props (buffer-substring start end))
  614. (str-no-props (buffer-substring-no-properties start end)))
  615. (slime-reify-old-output str-props str-no-props)))
  616. (defun slime-reify-old-output (str-props str-no-props)
  617. (let ((pos (slime-property-position 'slime-repl-presentation str-props)))
  618. (if (null pos)
  619. str-no-props
  620. (multiple-value-bind (presentation start-pos end-pos whole-p)
  621. (slime-presentation-around-point pos str-props)
  622. (if (not presentation)
  623. str-no-props
  624. (concat (substring str-no-props 0 pos)
  625. ;; Eval in the reader so that we play nice with quote.
  626. ;; -luke (19/May/2005)
  627. "#." (slime-presentation-expression presentation)
  628. (slime-reify-old-output (substring str-props end-pos)
  629. (substring str-no-props end-pos))))))))
  630. (defun slime-repl-grab-old-output (replace)
  631. "Resend the old REPL output at point.
  632. If replace it non-nil the current input is replaced with the old
  633. output; otherwise the new input is appended."
  634. (multiple-value-bind (presentation beg end)
  635. (slime-presentation-around-or-before-point (point))
  636. (slime-check-presentation beg end (current-buffer) presentation)
  637. (let ((old-output (buffer-substring beg end))) ;;keep properties
  638. ;; Append the old input or replace the current input
  639. (cond (replace (goto-char slime-repl-input-start-mark))
  640. (t (goto-char (point-max))
  641. (unless (eq (char-before) ?\ )
  642. (insert " "))))
  643. (delete-region (point) (point-max))
  644. (let ((inhibit-read-only t))
  645. (insert old-output)))))
  646. ;;; Presentation-related key bindings, non-context menu
  647. (defvar slime-presentation-command-map nil
  648. "Keymap for presentation-related commands. Bound to a prefix key.")
  649. (defvar slime-presentation-bindings
  650. '((?i slime-inspect-presentation-at-point)
  651. (?d slime-describe-presentation-at-point)
  652. (?w slime-copy-presentation-at-point-to-kill-ring)
  653. (?r slime-copy-presentation-at-point-to-repl)
  654. (?p slime-previous-presentation)
  655. (?n slime-next-presentation)
  656. (?\ slime-mark-presentation)))
  657. (defun slime-presentation-init-keymaps ()
  658. (slime-init-keymap 'slime-presentation-command-map nil t
  659. slime-presentation-bindings)
  660. (define-key slime-presentation-command-map "\M-o" 'slime-clear-presentations)
  661. ;; C-c C-v is the prefix for the presentation-command map.
  662. (define-key slime-prefix-map "\C-v" slime-presentation-command-map))
  663. (defun slime-presentation-around-or-before-point-p ()
  664. (multiple-value-bind (presentation beg end)
  665. (slime-presentation-around-or-before-point (point))
  666. presentation))
  667. (defvar slime-presentation-easy-menu
  668. (let ((P '(slime-presentation-around-or-before-point-p)))
  669. `("Presentations"
  670. [ "Find Definition" slime-M-.-presentation-at-point ,P ]
  671. [ "Inspect" slime-inspect-presentation-at-point ,P ]
  672. [ "Describe" slime-describe-presentation-at-point ,P ]
  673. [ "Pretty-print" slime-pretty-print-presentation-at-point ,P ]
  674. [ "Copy to REPL" slime-copy-presentation-at-point-to-repl ,P ]
  675. [ "Copy to kill ring" slime-copy-presentation-at-point-to-kill-ring ,P ]
  676. [ "Mark" slime-mark-presentation ,P ]
  677. "--"
  678. [ "Previous presentation" slime-previous-presentation ]
  679. [ "Next presentation" slime-next-presentation ]
  680. "--"
  681. [ "Clear all presentations" slime-clear-presentations ])))
  682. (defun slime-presentation-add-easy-menu ()
  683. (easy-menu-define menubar-slime-presentation slime-mode-map "Presentations" slime-presentation-easy-menu)
  684. (easy-menu-define menubar-slime-presentation slime-repl-mode-map "Presentations" slime-presentation-easy-menu)
  685. (easy-menu-define menubar-slime-presentation sldb-mode-map "Presentations" slime-presentation-easy-menu)
  686. (easy-menu-define menubar-slime-presentation slime-inspector-mode-map "Presentations" slime-presentation-easy-menu)
  687. (easy-menu-add slime-presentation-easy-menu 'slime-mode-map)
  688. (easy-menu-add slime-presentation-easy-menu 'slime-repl-mode-map)
  689. (easy-menu-add slime-presentation-easy-menu 'sldb-mode-map)
  690. (easy-menu-add slime-presentation-easy-menu 'slime-inspector-mode-map))
  691. ;;; hook functions (hard to isolate stuff)
  692. (defun slime-dispatch-presentation-event (event)
  693. (slime-dcase event
  694. ((:presentation-start id &optional target)
  695. (slime-mark-presentation-start id target)
  696. t)
  697. ((:presentation-end id &optional target)
  698. (slime-mark-presentation-end id target)
  699. t)
  700. (t nil)))
  701. (defun slime-presentation-write-result (string)
  702. (with-current-buffer (slime-output-buffer)
  703. (let ((marker (slime-repl-output-target-marker :repl-result))
  704. (saved-point (point-marker)))
  705. (goto-char marker)
  706. (slime-propertize-region `(face slime-repl-result-face
  707. rear-nonsticky (face))
  708. (insert string))
  709. ;; Move the input-start marker after the REPL result.
  710. (set-marker marker (point))
  711. (set-marker slime-output-end (point))
  712. ;; Restore point before insertion but only it if was farther
  713. ;; than `marker'. Omitting this breaks REPL test
  714. ;; `repl-type-ahead'.
  715. (when (> saved-point (point))
  716. (goto-char saved-point)))
  717. (slime-repl-show-maximum-output)))
  718. (defun slime-presentation-write (string &optional target)
  719. (case target
  720. ((nil) ; Regular process output
  721. (slime-repl-emit string))
  722. (:repl-result
  723. (slime-presentation-write-result string))
  724. (t (slime-repl-emit-to-target string target))))
  725. (defun slime-presentation-current-input (&optional until-point-p)
  726. "Return the current input as string.
  727. The input is the region from after the last prompt to the end of
  728. buffer. Presentations of old results are expanded into code."
  729. (slime-buffer-substring-with-reified-output (slime-repl-history-yank-start)
  730. (if until-point-p
  731. (point)
  732. (point-max))))
  733. (defun slime-presentation-on-return-pressed (end-of-input)
  734. (when (and (car (slime-presentation-around-or-before-point (point)))
  735. (< (point) slime-repl-input-start-mark))
  736. (slime-repl-grab-old-output end-of-input)
  737. (slime-repl-recenter-if-needed)
  738. t))
  739. (defun slime-presentation-bridge-insert (process output)
  740. (slime-output-filter process (or output "")))
  741. (defun slime-presentation-on-stream-open (stream)
  742. (install-bridge)
  743. (setq bridge-insert-function #'slime-presentation-bridge-insert)
  744. (setq bridge-destination-insert nil)
  745. (setq bridge-source-insert nil)
  746. (setq bridge-handlers
  747. (list* '("<" . slime-mark-presentation-start-handler)
  748. '(">" . slime-mark-presentation-end-handler)
  749. bridge-handlers)))
  750. (defun slime-clear-presentations ()
  751. "Forget all objects associated to SLIME presentations.
  752. This allows the garbage collector to remove these objects
  753. even on Common Lisp implementations without weak hash tables."
  754. (interactive)
  755. (slime-eval-async `(swank:clear-repl-results))
  756. (unless (eql major-mode 'slime-repl-mode)
  757. (slime-switch-to-output-buffer))
  758. (slime-for-each-presentation-in-region 1 (1+ (buffer-size))
  759. (lambda (presentation from to whole-p)
  760. (slime-remove-presentation-properties from to
  761. presentation))))
  762. (defun slime-presentation-inspector-insert-ispec (ispec)
  763. (if (stringp ispec)
  764. (insert ispec)
  765. (slime-dcase ispec
  766. ((:value string id)
  767. (slime-propertize-region
  768. (list 'slime-part-number id
  769. 'mouse-face 'highlight
  770. 'face 'slime-inspector-value-face)
  771. (slime-insert-presentation string `(:inspected-part ,id) t)))
  772. ((:label string)
  773. (insert (slime-inspector-fontify label string)))
  774. ((:action string id)
  775. (slime-insert-propertized (list 'slime-action-number id
  776. 'mouse-face 'highlight
  777. 'face 'slime-inspector-action-face)
  778. string)))))
  779. (defun slime-presentation-sldb-insert-frame-variable-value (value frame index)
  780. (slime-insert-presentation
  781. (sldb-in-face local-value value)
  782. `(:frame-var ,slime-current-thread ,(car frame) ,index) t))
  783. (defun slime-presentations-on-connected ()
  784. (slime-eval-async `(swank:init-presentations)))
  785. (provide 'slime-presentations)