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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion runtime/autoload/macvim.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
vim9script
# Support scripts for MacVim-specific functionality
# Maintainer: Yee Cheng Chin (macvim-dev@macvim.org)
# Last Change: 2022-10-14
# Last Change: 2023-03-15

# Retrieves the text under the selection, without polluting the registers.
# This is easier if we could yank, but we don't know what the user has been
Expand Down Expand Up @@ -76,4 +76,44 @@ export def ShowDefinitionUnderCursor()
endif
enddef

# Print functionality. We simply show the file in Preview and let the user
# decide what to do. This allows for more control instead of immediately
# piping the file to lpr which will actually print the file.
#
# PreviewConvertPostScript:
# Convert the provided PostScript file to PDF, then show in Preview. This is
# necessary in macOS 13+ as Preview doesn't support .ps files anymore.
# PreviewPostScript:
# Directly open PostScript file in Preview. Can use this if
# PreviewConvertPostScript doesn't work.
export def PreviewConvertPostScript(deltimer = 10000): number
# Convert PS to PDF because Preview can't use PS files in macOS 13+
system($"pstopdf {v:fname_in} -o {v:fname_in}.pdf")
if v:shell_error != 0
return v:shell_error
endif
system($"open -a Preview {v:fname_in}.pdf")
delete(v:fname_in)

# 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, so we just
# use a generous 10 secs timer.
# Note that we can't use `open -W` instead because 1) it will block
# synchronously, and 2) it will only return if Preview.app has closed, which
# may not happen for a while if it has other unrelated documents opened.
var to_delete_file = $"{v:fname_in}.pdf"
timer_start(deltimer, (timer) => delete(to_delete_file))

return v:shell_error
enddef

export def PreviewPostScript(deltimer = 10000): number
system($"open -a Preview {v:fname_in}")

var to_delete_file = v:fname_in
timer_start(deltimer, (timer) => delete(to_delete_file))

return v:shell_error
enddef

# vim: set sw=2 ts=2 et :
4 changes: 2 additions & 2 deletions runtime/doc/gui_mac.txt
Original file line number Diff line number Diff line change
Expand Up @@ -930,8 +930,8 @@ prominent bugs/missing features.
- Sometimes multibyte characters look "too wide", i.e. they overlap the
following character. It might help to change 'ambiwidth', or override the
automatic font substitution by setting 'guifontwide' manually.
- Printing. As a temporary solution <D-p> creates a PostScript file which is
then opened in Preview where it may be printed. See |hardcopy|.
- Built-in printing. |:hardcopy| / <D-p> creates a PDF file which is then
opened in Preview where it may be printed. See |pexpr-option|.

General bugs and issues are tracked on Github. If you find new bugs or have
feature requests then please file an issue there:
Expand Down
11 changes: 9 additions & 2 deletions runtime/doc/print.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Note: If you have problems printing with |:hardcopy|, an alternative is to use
paper size, duplex, etc.
Note: If you want PDF, there are tools such as
"ps2pdf" that can convert the PostScript to PDF.
On macOS, you can also use "pstopdf" which comes
bundled with the system.

:[range]ha[rdcopy][!] >{filename}
As above, but write the resulting PostScript in file
Expand Down Expand Up @@ -136,8 +138,13 @@ The arguments to the ":hardcopy" command are in |v:cmdarg|.
The expression must take care of deleting the file after printing it.
When there is an error, the expression must return a non-zero number.
If there is no error, return zero or an empty string.
The default for non MS-Windows or VMS systems is to simply use "lpr" to print
the file: >

The default for MacVim is to convert the PostScript file to PDF and then open
it in Preview.app where you can print from there, using the function
`macvim#PreviewConvertPostScript()`.

The default for other non MS-Windows or VMS systems is to simply use "lpr" to
print the file: >

system('lpr' .. (&printdevice == '' ? '' : ' -P' .. &printdevice)
.. ' ' .. v:fname_in) .. delete(v:fname_in) + v:shell_error
Expand Down
5 changes: 2 additions & 3 deletions src/MacVim/gvimrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ if empty(&guitablabel)
set guitablabel=%M%t
endif

" Send print jobs to Preview.app. This does not delete the temporary ps file
" that is generated by :hardcopy.
set printexpr=system('open\ -a\ Preview\ '.v:fname_in)\ +\ v:shell_error
" Send print jobs to Preview.app. The user can then print from it.
set printexpr=macvim#PreviewConvertPostScript()

" askpass
let $SSH_ASKPASS = simplify($VIM . '/../../Resources') . '/macvim-askpass'
Expand Down