Previously, MacVim added custom VimScript to query what the user
selected text is before calling `showdefinition` on it. Since Vim has
since implemented the `getregion()` API that provides that info
natively, use that API instead of the custom implementation.
Also, the previous implementation was broken when dealing with wide
characters where it would select more characters than necessary. Using
the native `getregion()` is much more robust and handle those
situations.
Fix broken printing in macOS 14. It was broken because the OS stopped
supporting Postscript and removed the `pstopdf` tool. Fix the printexpr
to detect when `pstopdf` doesn't exist and try to use `ps2pdf` instead.
This is a third-party tool and it's not guaranteed to exist. If it
doesn't exist, give an error prompt for the user to suggest installing
Ghostscript first. Settled on this solution as printing is a relatively
niche feature and it's not worth spending too much effort fixing this.
Related:
- #1390 / #1347: macOS 13 Ventura broke printing by removing Preview
support for PostScript. The fix was to use `pstopdf`, which eventually
got removed in macOS 14.
MacVim's `:hardcopy` implementation just uses Preview to show the
generated PostScript file and lets the user decide what to do with it.
macOS 13 Ventura removed PostScript support from Preview, so we now have
to manually convert it to PDF first using `pstopdf` (which is thankfully
bundled with macOS).
While we are at it, update the script so that we actually delete the
file after sending it to Preview. Previously MacVim never did that and
therefore leaks the file in a temp folder until Vim closes, which isn't
ideal for privacy. Now, just set a 10 sec timer to delete the file after
it's opened (we just need enough time to allow Preview to open and load
the file, which doesn't take much time. The 10 sec timer is to account
for slow computers).
Fix#1347
Add a new Vim script function `showdefinition()` that allows Vim script
to call back to macOS's data lookup feature and show the definition /
URL preview / etc for any text, at a designated row/col position. If the
row/col are not provided this function will just show it at the cursor.
Also, add a new autoload/macvim.vim for utility functions to call
showdefinition() for selected texts and the word under cursor. Make a
new right-click popup menu "Look Up" call that when there are selected
texts in visual mode to make the lookup functionality easier to access
for users without a trackpad (since Ctrl-Cmd-D is a little obscure and
unwieldy to use). For the utility functions, it was a little hard to
determine how to get the text under visual selection without yanking (we
don't want to pollute the register here), and just implemented a
function to take care of all the edge cases including visual/block/line
modes and selection=exclusive. It could be useful in other situations.
As a side refactor, change the message handler in MacVim from if/else to
switch case. In optimized builds, they both essentially optimize to the
same thing, but for debug builds, the if/else statements have to step
through one by one, and switch case just makes more sense for a giant
message ID lookup like this.
Part of Epic #1311