Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix :hardcopy not working in macOS 13 Ventura #1390

Merged
merged 1 commit into from
Mar 18, 2023

Conversation

ychin
Copy link
Member

@ychin ychin commented Mar 15, 2023

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

@ychin ychin added this to the Release 177 milestone Mar 15, 2023
@ychin ychin force-pushed the fix-macos-13-ventura-printing branch from 62b7eca to 842cd7e Compare March 17, 2023 23:05

# Delete the file after it's opened in Preview for privacy. We don't have an
# easy way to detect that Preview has opened the file already (unless we use
# `open -W` which will stall Vim), so we just use a generous 10 secs timer.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically, it can be done using client-server architecture. A server can be either GUI main process itself or a shell script which will fork process properly.

Second proposition can be used from terminal and require less coding. I see the first one as a more clean though

What do you think on this idea?

Copy link
Member Author

@ychin ychin Mar 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue is actually that -W only terminates when Preview.app closes. A normal user may have multiple documents opened in Preview, which never closes, meaning that -W has to wait forever to finish. You can use some AppleScript to figure out when the document has opened/closed but it's somewhat convoluted and also requires asking permissions from the user.

Otherwise we could have simply used call system(open -W Preview.app; rm ... &) to open it in the background.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just remove that comment about -W.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, my idea was to call an external routine(script or via client-server architectrure) which will fork and wait and remove files as they not needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, keep the comment, it's useful

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I can add something back. The key point here though is that -W does not do what we want, so the original comment was wrong. It only works if the user literally only has one file open and immediately close Preview, which is often not the case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks

@ychin ychin force-pushed the fix-macos-13-ventura-printing branch 2 times, most recently from b5e4bde to 377323c Compare March 18, 2023 02:33
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 macvim-dev#1347
@ychin ychin force-pushed the fix-macos-13-ventura-printing branch from 377323c to e3a5374 Compare March 18, 2023 10:00
@ychin ychin merged commit 43b4967 into macvim-dev:master Mar 18, 2023
@ychin ychin deleted the fix-macos-13-ventura-printing branch March 18, 2023 11:36
@MichaelHendry
Copy link

Thanks, guys.

I had worked around the printing issue on Ventura by adding

se printexpr=system('ps2pdf\ '.v:fname_in.'\ '.v:fname_in.'.pdf\ &&\ open\ '.v:fname_in.'.pdf')\ +\ v:shell_error

to my .vimrc file. I'll update my copy of MacVim when prompted, and test after commenting out the "se printexpr..." line.

Michael

@ychin
Copy link
Member Author

ychin commented Mar 18, 2023

There should be a pre-release version (176.1) coming out soon. If you want that version you can go to Advanced settings to enable the pre-release update channel. Otherwise though if it's been working for you (ps2pdf is more powerful than the OS bundled pstopdf anyway) and you prefer just having something stable you could just wait for a proper release (177) later.

@MichaelHendry
Copy link

Thanks, I think I'll just sit tight until 177 is released, I'm having a few problems with unstable software (not MacVim) at the moment...

tono pushed a commit to tono/macvim that referenced this pull request Mar 21, 2023
…inting

Fix :hardcopy not working in macOS 13 Ventura
@jubilatious1
Copy link

jubilatious1 commented Apr 6, 2023

I'm on Vim 9.0.1276 (MacVim r176), and :hardcopy all alone goes nowhere (MacOS Ventura 13.3).

That means I need to wait for version-177 ? Thanks.

EDIT1: My current workaround for printing (not viewing) is to grab the file path, go to iTerm2, open vim path/to/file, then use :hardcopy from there.

EDIT2: Just tried @MichaelHendry 's workaround and it indeed does work to create/open a PDF in MacOS Preview:

:hardcopy > 0.ps | !ps2pdf 0.ps && open -a preview 0.pdf

@ychin
Copy link
Member Author

ychin commented Apr 7, 2023

@jubilatious1 you need r177 (which is not released yet) or you can get the pre-release version r176.1. Otherwise if the workaround works for you that works too.

ychin added a commit that referenced this pull request Jul 10, 2023
Updated to Vim 9.0.1677

Announcements
====================

Website
--------------------

The official website for MacVim is now https://macvim.org. Previously it just
forwarded to https://macvim-dev.github.io/macvim/. You can also read the MacVim
documentation at https://macvim.org/docs/gui_mac.txt.html. #1385

Features
====================

Updater / What's New page
--------------------

There is now a "What's New" page that will automatically be shown whenever
MacVim detected that it has been updated to a new version (can be disabled in
Settings). The page will also include all the release notes if you have
updated across multiple versions. This feature is useful for users who turned
on "Automatically install updates" or installs MacVim through other methods
like Homebrew but would still like to see the release notes when a new version
comes out. You can also access it through the Help menu. #1414

MacVim should now report its version in a much more consistent manner in the
"About MacVim" page and when the updater reports there is a new version. It
should look something like "r176 (Vim 9.0.1276)" where "r176" is the MacVim
release number and the 9.0.1276 is the bundled Vim version. #1293 #1393

Sparkle (updater for MacVim) is now updated to 2.4.2. #1416

New Vim features
--------------------

- New bundled colorschemes: wildcharm/retrobox/sorbet/zaibatsu (vim/vim#12163)
- File encryption now has a new `cryptmethod`: `xchacha20v2`, which is designed
  to be more forward compatible with future Vim versions than `xchacha20`.
  (v9.0.1481)
- `switchbuf` works for more commands. (v9.0.1546)
- Statusline now supports multiple alignment "%=" items. (v9.0.1300)
- New UTF-16 utility functions (`strutf16len` and `utf16idx`) (v9.0.1485)
- Misc 'smoothscroll' bugs fixes

General
====================

* Removed non-Unicode localization files, which helps cut down on app size. #1397
* Miscellaneous documentation fixes. #1415 #1375 #1386 #1363 (by @dkav)
* The disk image for MacVim (MacVim.dmg) is now in APFS and uses better
  compression for better efficiency. #1409

Fixes
====================

* Printing a file in macOS 13 Ventura (using `:hardcopy` or File→Print) should
  work again. #1390
* Fixed a broken symlink to XPCServices in the Sparkle framework. #1367
* Fixed MacVim to not throw (safe) Objective C exceptions when quitting. #1371
* Fixed welcome message not being aligned properly in Simplified Chinese and
  show the Vim 9 prompt. #1381
* Removed some unnecessary test files in the runtime folders which were
  included erroneously. #1418

Scripting
====================

- Scripting languages versions:
    - Python is now built against 3.11, up from 3.10.

Compatibility
====================

Requires macOS 10.9 or above. (10.9 - 10.12 requires downloading a
separate legacy build)

Script interfaces have compatibility with these versions:

- Lua 5.4
- Perl 5.30
- Python2 2.7
- Python3 3.11
- Ruby 3.2
ychin added a commit to ychin/macvim that referenced this pull request Dec 9, 2023
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:
- macvim-dev#1390 / macvim-dev#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Printing problem with Custom Version 9.0.472 (174) on MacOS Ventura 13.0.1
4 participants